blob: bc774ae992588b9659db7e5b5e963bbae82a1d0c [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 */
Peter Maydelld38ea872016-01-29 17:50:05 +000019#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010020#include "qapi/error.h"
Ziyue Yang508b4ec2017-01-18 16:02:41 +080021#include "qemu/error-report.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020022#include "qemu/cutils.h"
Doug Gale5c9522b2017-12-02 20:30:37 -050023#include "trace-root.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020024#ifdef CONFIG_USER_ONLY
bellard1fddef42005-04-17 19:16:13 +000025#include "qemu.h"
26#else
Paolo Bonzini83c90892012-12-17 18:19:49 +010027#include "monitor/monitor.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040028#include "chardev/char.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040029#include "chardev/char-fe.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010030#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010031#include "exec/gdbstub.h"
Luc Michel8f468632019-01-07 15:23:45 +000032#include "hw/cpu/cluster.h"
bellard1fddef42005-04-17 19:16:13 +000033#endif
bellard67b915a2004-03-31 23:37:16 +000034
pbrook56aebc82008-10-11 17:55:29 +000035#define MAX_PACKET_LENGTH 4096
36
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010038#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/kvm.h"
Leon Alraecfe67ce2015-06-19 14:17:45 +010040#include "exec/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010041#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000042
Jan Kiszkaa3919382015-02-07 09:38:44 +010043#ifdef CONFIG_USER_ONLY
44#define GDB_ATTACHED "0"
45#else
46#define GDB_ATTACHED "1"
47#endif
48
Andreas Färberf3659ee2013-06-27 19:09:09 +020049static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
50 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020051{
Andreas Färberf3659ee2013-06-27 19:09:09 +020052 CPUClass *cc = CPU_GET_CLASS(cpu);
53
54 if (cc->memory_rw_debug) {
55 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
56 }
57 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020058}
aurel32ca587a82008-12-18 22:44:13 +000059
Alex Bennéed2a6c852017-07-12 11:52:14 +010060/* Return the GDB index for a given vCPU state.
61 *
62 * For user mode this is simply the thread id. In system mode GDB
63 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
64 */
65static inline int cpu_gdb_index(CPUState *cpu)
66{
67#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010068 TaskState *ts = (TaskState *) cpu->opaque;
69 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010070#else
71 return cpu->cpu_index + 1;
72#endif
73}
74
aurel32ca587a82008-12-18 22:44:13 +000075enum {
76 GDB_SIGNAL_0 = 0,
77 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010078 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000079 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010080 GDB_SIGNAL_ABRT = 6,
81 GDB_SIGNAL_ALRM = 14,
82 GDB_SIGNAL_IO = 23,
83 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000084 GDB_SIGNAL_UNKNOWN = 143
85};
86
87#ifdef CONFIG_USER_ONLY
88
89/* Map target signal numbers to GDB protocol signal numbers and vice
90 * versa. For user emulation's currently supported systems, we can
91 * assume most signals are defined.
92 */
93
94static int gdb_signal_table[] = {
95 0,
96 TARGET_SIGHUP,
97 TARGET_SIGINT,
98 TARGET_SIGQUIT,
99 TARGET_SIGILL,
100 TARGET_SIGTRAP,
101 TARGET_SIGABRT,
102 -1, /* SIGEMT */
103 TARGET_SIGFPE,
104 TARGET_SIGKILL,
105 TARGET_SIGBUS,
106 TARGET_SIGSEGV,
107 TARGET_SIGSYS,
108 TARGET_SIGPIPE,
109 TARGET_SIGALRM,
110 TARGET_SIGTERM,
111 TARGET_SIGURG,
112 TARGET_SIGSTOP,
113 TARGET_SIGTSTP,
114 TARGET_SIGCONT,
115 TARGET_SIGCHLD,
116 TARGET_SIGTTIN,
117 TARGET_SIGTTOU,
118 TARGET_SIGIO,
119 TARGET_SIGXCPU,
120 TARGET_SIGXFSZ,
121 TARGET_SIGVTALRM,
122 TARGET_SIGPROF,
123 TARGET_SIGWINCH,
124 -1, /* SIGLOST */
125 TARGET_SIGUSR1,
126 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000127#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000128 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000129#else
130 -1,
131#endif
aurel32ca587a82008-12-18 22:44:13 +0000132 -1, /* SIGPOLL */
133 -1,
134 -1,
135 -1,
136 -1,
137 -1,
138 -1,
139 -1,
140 -1,
141 -1,
142 -1,
143 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000144#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000145 __SIGRTMIN + 1,
146 __SIGRTMIN + 2,
147 __SIGRTMIN + 3,
148 __SIGRTMIN + 4,
149 __SIGRTMIN + 5,
150 __SIGRTMIN + 6,
151 __SIGRTMIN + 7,
152 __SIGRTMIN + 8,
153 __SIGRTMIN + 9,
154 __SIGRTMIN + 10,
155 __SIGRTMIN + 11,
156 __SIGRTMIN + 12,
157 __SIGRTMIN + 13,
158 __SIGRTMIN + 14,
159 __SIGRTMIN + 15,
160 __SIGRTMIN + 16,
161 __SIGRTMIN + 17,
162 __SIGRTMIN + 18,
163 __SIGRTMIN + 19,
164 __SIGRTMIN + 20,
165 __SIGRTMIN + 21,
166 __SIGRTMIN + 22,
167 __SIGRTMIN + 23,
168 __SIGRTMIN + 24,
169 __SIGRTMIN + 25,
170 __SIGRTMIN + 26,
171 __SIGRTMIN + 27,
172 __SIGRTMIN + 28,
173 __SIGRTMIN + 29,
174 __SIGRTMIN + 30,
175 __SIGRTMIN + 31,
176 -1, /* SIGCANCEL */
177 __SIGRTMIN,
178 __SIGRTMIN + 32,
179 __SIGRTMIN + 33,
180 __SIGRTMIN + 34,
181 __SIGRTMIN + 35,
182 __SIGRTMIN + 36,
183 __SIGRTMIN + 37,
184 __SIGRTMIN + 38,
185 __SIGRTMIN + 39,
186 __SIGRTMIN + 40,
187 __SIGRTMIN + 41,
188 __SIGRTMIN + 42,
189 __SIGRTMIN + 43,
190 __SIGRTMIN + 44,
191 __SIGRTMIN + 45,
192 __SIGRTMIN + 46,
193 __SIGRTMIN + 47,
194 __SIGRTMIN + 48,
195 __SIGRTMIN + 49,
196 __SIGRTMIN + 50,
197 __SIGRTMIN + 51,
198 __SIGRTMIN + 52,
199 __SIGRTMIN + 53,
200 __SIGRTMIN + 54,
201 __SIGRTMIN + 55,
202 __SIGRTMIN + 56,
203 __SIGRTMIN + 57,
204 __SIGRTMIN + 58,
205 __SIGRTMIN + 59,
206 __SIGRTMIN + 60,
207 __SIGRTMIN + 61,
208 __SIGRTMIN + 62,
209 __SIGRTMIN + 63,
210 __SIGRTMIN + 64,
211 __SIGRTMIN + 65,
212 __SIGRTMIN + 66,
213 __SIGRTMIN + 67,
214 __SIGRTMIN + 68,
215 __SIGRTMIN + 69,
216 __SIGRTMIN + 70,
217 __SIGRTMIN + 71,
218 __SIGRTMIN + 72,
219 __SIGRTMIN + 73,
220 __SIGRTMIN + 74,
221 __SIGRTMIN + 75,
222 __SIGRTMIN + 76,
223 __SIGRTMIN + 77,
224 __SIGRTMIN + 78,
225 __SIGRTMIN + 79,
226 __SIGRTMIN + 80,
227 __SIGRTMIN + 81,
228 __SIGRTMIN + 82,
229 __SIGRTMIN + 83,
230 __SIGRTMIN + 84,
231 __SIGRTMIN + 85,
232 __SIGRTMIN + 86,
233 __SIGRTMIN + 87,
234 __SIGRTMIN + 88,
235 __SIGRTMIN + 89,
236 __SIGRTMIN + 90,
237 __SIGRTMIN + 91,
238 __SIGRTMIN + 92,
239 __SIGRTMIN + 93,
240 __SIGRTMIN + 94,
241 __SIGRTMIN + 95,
242 -1, /* SIGINFO */
243 -1, /* UNKNOWN */
244 -1, /* DEFAULT */
245 -1,
246 -1,
247 -1,
248 -1,
249 -1,
250 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000251#endif
aurel32ca587a82008-12-18 22:44:13 +0000252};
bellard8f447cc2006-06-14 15:21:14 +0000253#else
aurel32ca587a82008-12-18 22:44:13 +0000254/* In system mode we only need SIGINT and SIGTRAP; other signals
255 are not yet supported. */
256
257enum {
258 TARGET_SIGINT = 2,
259 TARGET_SIGTRAP = 5
260};
261
262static int gdb_signal_table[] = {
263 -1,
264 -1,
265 TARGET_SIGINT,
266 -1,
267 -1,
268 TARGET_SIGTRAP
269};
bellard8f447cc2006-06-14 15:21:14 +0000270#endif
bellardb4608c02003-06-27 17:34:32 +0000271
aurel32ca587a82008-12-18 22:44:13 +0000272#ifdef CONFIG_USER_ONLY
273static int target_signal_to_gdb (int sig)
274{
275 int i;
276 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
277 if (gdb_signal_table[i] == sig)
278 return i;
279 return GDB_SIGNAL_UNKNOWN;
280}
281#endif
282
283static int gdb_signal_to_target (int sig)
284{
285 if (sig < ARRAY_SIZE (gdb_signal_table))
286 return gdb_signal_table[sig];
287 else
288 return -1;
289}
290
pbrook56aebc82008-10-11 17:55:29 +0000291typedef struct GDBRegisterState {
292 int base_reg;
293 int num_regs;
294 gdb_reg_cb get_reg;
295 gdb_reg_cb set_reg;
296 const char *xml;
297 struct GDBRegisterState *next;
298} GDBRegisterState;
299
Luc Michel8f468632019-01-07 15:23:45 +0000300typedef struct GDBProcess {
301 uint32_t pid;
302 bool attached;
Luc Michelc145eea2019-01-07 15:23:46 +0000303
304 char target_xml[1024];
Luc Michel8f468632019-01-07 15:23:45 +0000305} GDBProcess;
306
bellard858693c2004-03-31 18:52:07 +0000307enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000308 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000309 RS_IDLE,
310 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400311 RS_GETLINE_ESC,
312 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000313 RS_CHKSUM1,
314 RS_CHKSUM2,
315};
bellard858693c2004-03-31 18:52:07 +0000316typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200317 CPUState *c_cpu; /* current CPU for step/continue ops */
318 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200319 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000320 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000321 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000322 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400323 int line_sum; /* running checksum */
324 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000325 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000326 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000327 int signal;
bellard41625032005-04-24 10:07:11 +0000328#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000329 int fd;
bellard41625032005-04-24 10:07:11 +0000330 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000331#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300332 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300333 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000334#endif
Luc Michel8f468632019-01-07 15:23:45 +0000335 bool multiprocess;
336 GDBProcess *processes;
337 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000338 char syscall_buf[256];
339 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000340} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000341
edgar_igl60897d32008-05-09 08:25:14 +0000342/* By default use no IRQs and no timers while single stepping so as to
343 * make single stepping like an ICE HW step.
344 */
345static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
346
aliguori880a7572008-11-18 20:30:24 +0000347static GDBState *gdbserver_state;
348
Andreas Färber5b50e792013-06-29 04:18:45 +0200349bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000350
bellard1fddef42005-04-17 19:16:13 +0000351#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000352/* XXX: This is not thread safe. Do we care? */
353static int gdbserver_fd = -1;
354
bellard858693c2004-03-31 18:52:07 +0000355static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000356{
357 uint8_t ch;
358 int ret;
359
360 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000361 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000362 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000363 if (errno == ECONNRESET)
364 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200365 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000366 return -1;
367 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000368 close(s->fd);
369 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000370 return -1;
371 } else {
372 break;
373 }
374 }
375 return ch;
376}
pbrook4046d912007-01-28 01:53:16 +0000377#endif
bellardb4608c02003-06-27 17:34:32 +0000378
blueswir1654efcf2009-04-18 07:29:59 +0000379static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000380 GDB_SYS_UNKNOWN,
381 GDB_SYS_ENABLED,
382 GDB_SYS_DISABLED,
383} gdb_syscall_mode;
384
Liviu Ionescua38bb072014-12-11 12:07:48 +0000385/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000386int use_gdb_syscalls(void)
387{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100388 SemihostingTarget target = semihosting_get_target();
389 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000390 /* -semihosting-config target=native */
391 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100392 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000393 /* -semihosting-config target=gdb */
394 return true;
395 }
396
397 /* -semihosting-config target=auto */
398 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000399 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000400 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
401 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000402 }
403 return gdb_syscall_mode == GDB_SYS_ENABLED;
404}
405
edgar_iglba70a622008-03-14 06:10:42 +0000406/* Resume execution. */
407static inline void gdb_continue(GDBState *s)
408{
Doug Gale5c9522b2017-12-02 20:30:37 -0500409
edgar_iglba70a622008-03-14 06:10:42 +0000410#ifdef CONFIG_USER_ONLY
411 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500412 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000413#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200414 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500415 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200416 vm_start();
417 }
edgar_iglba70a622008-03-14 06:10:42 +0000418#endif
419}
420
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100421/*
422 * Resume execution, per CPU actions. For user-mode emulation it's
423 * equivalent to gdb_continue.
424 */
425static int gdb_continue_partial(GDBState *s, char *newstates)
426{
427 CPUState *cpu;
428 int res = 0;
429#ifdef CONFIG_USER_ONLY
430 /*
431 * This is not exactly accurate, but it's an improvement compared to the
432 * previous situation, where only one CPU would be single-stepped.
433 */
434 CPU_FOREACH(cpu) {
435 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500436 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100437 cpu_single_step(cpu, sstep_flags);
438 }
439 }
440 s->running_state = 1;
441#else
442 int flag = 0;
443
444 if (!runstate_needs_reset()) {
445 if (vm_prepare_start()) {
446 return 0;
447 }
448
449 CPU_FOREACH(cpu) {
450 switch (newstates[cpu->cpu_index]) {
451 case 0:
452 case 1:
453 break; /* nothing to do here */
454 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500455 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100456 cpu_single_step(cpu, sstep_flags);
457 cpu_resume(cpu);
458 flag = 1;
459 break;
460 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500461 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100462 cpu_resume(cpu);
463 flag = 1;
464 break;
465 default:
466 res = -1;
467 break;
468 }
469 }
470 }
471 if (flag) {
472 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
473 }
474#endif
475 return res;
476}
477
bellard858693c2004-03-31 18:52:07 +0000478static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000479{
pbrook4046d912007-01-28 01:53:16 +0000480#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000481 int ret;
482
483 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000484 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000485 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200486 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000487 return;
488 } else {
489 buf += ret;
490 len -= ret;
491 }
492 }
pbrook4046d912007-01-28 01:53:16 +0000493#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100494 /* XXX this blocks entire thread. Rewrite to use
495 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300496 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000497#endif
bellardb4608c02003-06-27 17:34:32 +0000498}
499
500static inline int fromhex(int v)
501{
502 if (v >= '0' && v <= '9')
503 return v - '0';
504 else if (v >= 'A' && v <= 'F')
505 return v - 'A' + 10;
506 else if (v >= 'a' && v <= 'f')
507 return v - 'a' + 10;
508 else
509 return 0;
510}
511
512static inline int tohex(int v)
513{
514 if (v < 10)
515 return v + '0';
516 else
517 return v - 10 + 'a';
518}
519
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300520/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000521static void memtohex(char *buf, const uint8_t *mem, int len)
522{
523 int i, c;
524 char *q;
525 q = buf;
526 for(i = 0; i < len; i++) {
527 c = mem[i];
528 *q++ = tohex(c >> 4);
529 *q++ = tohex(c & 0xf);
530 }
531 *q = '\0';
532}
533
534static void hextomem(uint8_t *mem, const char *buf, int len)
535{
536 int i;
537
538 for(i = 0; i < len; i++) {
539 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
540 buf += 2;
541 }
542}
543
Doug Gale5c9522b2017-12-02 20:30:37 -0500544static void hexdump(const char *buf, int len,
545 void (*trace_fn)(size_t ofs, char const *text))
546{
547 char line_buffer[3 * 16 + 4 + 16 + 1];
548
549 size_t i;
550 for (i = 0; i < len || (i & 0xF); ++i) {
551 size_t byte_ofs = i & 15;
552
553 if (byte_ofs == 0) {
554 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
555 line_buffer[3 * 16 + 4 + 16] = 0;
556 }
557
558 size_t col_group = (i >> 2) & 3;
559 size_t hex_col = byte_ofs * 3 + col_group;
560 size_t txt_col = 3 * 16 + 4 + byte_ofs;
561
562 if (i < len) {
563 char value = buf[i];
564
565 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
566 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
567 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
568 ? value
569 : '.';
570 }
571
572 if (byte_ofs == 0xF)
573 trace_fn(i & -16, line_buffer);
574 }
575}
576
bellardb4608c02003-06-27 17:34:32 +0000577/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500578static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000579{
pbrook56aebc82008-10-11 17:55:29 +0000580 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000581 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000582
Doug Gale5c9522b2017-12-02 20:30:37 -0500583 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
584 hexdump(buf, len, trace_gdbstub_io_binaryreply);
585 }
586
bellardb4608c02003-06-27 17:34:32 +0000587 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000588 p = s->last_packet;
589 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000590 memcpy(p, buf, len);
591 p += len;
bellardb4608c02003-06-27 17:34:32 +0000592 csum = 0;
593 for(i = 0; i < len; i++) {
594 csum += buf[i];
595 }
pbrook4046d912007-01-28 01:53:16 +0000596 *(p++) = '#';
597 *(p++) = tohex((csum >> 4) & 0xf);
598 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000599
pbrook4046d912007-01-28 01:53:16 +0000600 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000601 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000602
pbrook4046d912007-01-28 01:53:16 +0000603#ifdef CONFIG_USER_ONLY
604 i = get_char(s);
605 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000606 return -1;
pbrook4046d912007-01-28 01:53:16 +0000607 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000608 break;
pbrook4046d912007-01-28 01:53:16 +0000609#else
610 break;
611#endif
bellardb4608c02003-06-27 17:34:32 +0000612 }
613 return 0;
614}
615
pbrook56aebc82008-10-11 17:55:29 +0000616/* return -1 if error, 0 if OK */
617static int put_packet(GDBState *s, const char *buf)
618{
Doug Gale5c9522b2017-12-02 20:30:37 -0500619 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000620
Doug Gale5c9522b2017-12-02 20:30:37 -0500621 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000622}
623
pbrook56aebc82008-10-11 17:55:29 +0000624/* Encode data using the encoding for 'x' packets. */
625static int memtox(char *buf, const char *mem, int len)
626{
627 char *p = buf;
628 char c;
629
630 while (len--) {
631 c = *(mem++);
632 switch (c) {
633 case '#': case '$': case '*': case '}':
634 *(p++) = '}';
635 *(p++) = c ^ 0x20;
636 break;
637 default:
638 *(p++) = c;
639 break;
640 }
641 }
642 return p - buf;
643}
644
Luc Michel1a227332019-01-07 15:23:45 +0000645static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
646{
Luc Michel1a227332019-01-07 15:23:45 +0000647 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000648 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
649 /* Return the default process' PID */
650 return s->processes[s->process_num - 1].pid;
651 }
652 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000653}
654
Luc Michel7d8c87d2019-01-07 15:23:45 +0000655static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
656{
657 int i;
658
659 if (!pid) {
660 /* 0 means any process, we take the first one */
661 return &s->processes[0];
662 }
663
664 for (i = 0; i < s->process_num; i++) {
665 if (s->processes[i].pid == pid) {
666 return &s->processes[i];
667 }
668 }
669
670 return NULL;
671}
672
673static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
674{
675 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
676}
677
678static CPUState *find_cpu(uint32_t thread_id)
679{
680 CPUState *cpu;
681
682 CPU_FOREACH(cpu) {
683 if (cpu_gdb_index(cpu) == thread_id) {
684 return cpu;
685 }
686 }
687
688 return NULL;
689}
690
Luc Michele40e5202019-01-07 15:23:46 +0000691static CPUState *get_first_cpu_in_process(const GDBState *s,
692 GDBProcess *process)
693{
694 CPUState *cpu;
695
696 CPU_FOREACH(cpu) {
697 if (gdb_get_cpu_pid(s, cpu) == process->pid) {
698 return cpu;
699 }
700 }
701
702 return NULL;
703}
704
705static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu)
706{
707 uint32_t pid = gdb_get_cpu_pid(s, cpu);
708 cpu = CPU_NEXT(cpu);
709
710 while (cpu) {
711 if (gdb_get_cpu_pid(s, cpu) == pid) {
712 break;
713 }
714
715 cpu = CPU_NEXT(cpu);
716 }
717
718 return cpu;
719}
720
Luc Michele40e5202019-01-07 15:23:46 +0000721/* Return the cpu following @cpu, while ignoring unattached processes. */
722static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
723{
724 cpu = CPU_NEXT(cpu);
725
726 while (cpu) {
727 if (gdb_get_cpu_process(s, cpu)->attached) {
728 break;
729 }
730
731 cpu = CPU_NEXT(cpu);
732 }
733
734 return cpu;
735}
736
737/* Return the first attached cpu */
738static CPUState *gdb_first_attached_cpu(const GDBState *s)
739{
740 CPUState *cpu = first_cpu;
741 GDBProcess *process = gdb_get_cpu_process(s, cpu);
742
743 if (!process->attached) {
744 return gdb_next_attached_cpu(s, cpu);
745 }
746
747 return cpu;
748}
749
Luc Michelab65eed2019-01-29 11:46:03 +0000750static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
751{
752 GDBProcess *process;
753 CPUState *cpu;
754
755 if (!pid && !tid) {
756 /* 0 means any process/thread, we take the first attached one */
757 return gdb_first_attached_cpu(s);
758 } else if (pid && !tid) {
759 /* any thread in a specific process */
760 process = gdb_get_process(s, pid);
761
762 if (process == NULL) {
763 return NULL;
764 }
765
766 if (!process->attached) {
767 return NULL;
768 }
769
770 return get_first_cpu_in_process(s, process);
771 } else {
772 /* a specific thread */
773 cpu = find_cpu(tid);
774
775 if (cpu == NULL) {
776 return NULL;
777 }
778
779 process = gdb_get_cpu_process(s, cpu);
780
781 if (pid && process->pid != pid) {
782 return NULL;
783 }
784
785 if (!process->attached) {
786 return NULL;
787 }
788
789 return cpu;
790 }
791}
792
Luc Michelc145eea2019-01-07 15:23:46 +0000793static const char *get_feature_xml(const GDBState *s, const char *p,
794 const char **newp, GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000795{
pbrook56aebc82008-10-11 17:55:29 +0000796 size_t len;
797 int i;
798 const char *name;
Luc Michelc145eea2019-01-07 15:23:46 +0000799 CPUState *cpu = get_first_cpu_in_process(s, process);
800 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000801
802 len = 0;
803 while (p[len] && p[len] != ':')
804 len++;
805 *newp = p + len;
806
807 name = NULL;
808 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000809 char *buf = process->target_xml;
810 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000811
Luc Michelc145eea2019-01-07 15:23:46 +0000812 /* Generate the XML description for this CPU. */
813 if (!buf[0]) {
814 GDBRegisterState *r;
815
816 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100817 "<?xml version=\"1.0\"?>"
818 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
819 "<target>");
820 if (cc->gdb_arch_name) {
821 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000822 pstrcat(buf, buf_sz, "<architecture>");
823 pstrcat(buf, buf_sz, arch);
824 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100825 g_free(arch);
826 }
Luc Michelc145eea2019-01-07 15:23:46 +0000827 pstrcat(buf, buf_sz, "<xi:include href=\"");
828 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
829 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200830 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000831 pstrcat(buf, buf_sz, "<xi:include href=\"");
832 pstrcat(buf, buf_sz, r->xml);
833 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000834 }
Luc Michelc145eea2019-01-07 15:23:46 +0000835 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000836 }
Luc Michelc145eea2019-01-07 15:23:46 +0000837 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000838 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100839 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100840 char *xmlname = g_strndup(p, len);
841 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
842
843 g_free(xmlname);
844 if (xml) {
845 return xml;
846 }
847 }
pbrook56aebc82008-10-11 17:55:29 +0000848 for (i = 0; ; i++) {
849 name = xml_builtin[i][0];
850 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
851 break;
852 }
853 return name ? xml_builtin[i][1] : NULL;
854}
pbrook56aebc82008-10-11 17:55:29 +0000855
Andreas Färber385b9f02013-06-27 18:25:36 +0200856static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000857{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200858 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200859 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000860 GDBRegisterState *r;
861
Andreas Färbera0e372f2013-06-28 23:18:47 +0200862 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200863 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200864 }
pbrook56aebc82008-10-11 17:55:29 +0000865
Andreas Färbereac8b352013-06-28 21:11:37 +0200866 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000867 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
868 return r->get_reg(env, mem_buf, reg - r->base_reg);
869 }
870 }
871 return 0;
872}
873
Andreas Färber385b9f02013-06-27 18:25:36 +0200874static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000875{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200876 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200877 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000878 GDBRegisterState *r;
879
Andreas Färbera0e372f2013-06-28 23:18:47 +0200880 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200881 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200882 }
pbrook56aebc82008-10-11 17:55:29 +0000883
Andreas Färbereac8b352013-06-28 21:11:37 +0200884 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000885 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
886 return r->set_reg(env, mem_buf, reg - r->base_reg);
887 }
888 }
889 return 0;
890}
891
892/* Register a supplemental set of CPU registers. If g_pos is nonzero it
893 specifies the first register number and these registers are included in
894 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
895 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
896 */
897
Andreas Färber22169d42013-06-28 21:27:39 +0200898void gdb_register_coprocessor(CPUState *cpu,
899 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
900 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000901{
902 GDBRegisterState *s;
903 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000904
Andreas Färbereac8b352013-06-28 21:11:37 +0200905 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000906 while (*p) {
907 /* Check for duplicates. */
908 if (strcmp((*p)->xml, xml) == 0)
909 return;
910 p = &(*p)->next;
911 }
Stefan Weil9643c252011-10-18 22:25:38 +0200912
913 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200914 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200915 s->num_regs = num_regs;
916 s->get_reg = get_reg;
917 s->set_reg = set_reg;
918 s->xml = xml;
919
pbrook56aebc82008-10-11 17:55:29 +0000920 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200921 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000922 *p = s;
923 if (g_pos) {
924 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800925 error_report("Error: Bad gdb register numbering for '%s', "
926 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200927 } else {
928 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000929 }
930 }
931}
932
aliguoria1d1bb32008-11-18 20:07:32 +0000933#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100934/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
935static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
936{
937 static const int xlat[] = {
938 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
939 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
940 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
941 };
942
943 CPUClass *cc = CPU_GET_CLASS(cpu);
944 int cputype = xlat[gdbtype];
945
946 if (cc->gdb_stop_before_watchpoint) {
947 cputype |= BP_STOP_BEFORE_ACCESS;
948 }
949 return cputype;
950}
aliguoria1d1bb32008-11-18 20:07:32 +0000951#endif
952
aliguori880a7572008-11-18 20:30:24 +0000953static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000954{
Andreas Färber182735e2013-05-29 22:29:20 +0200955 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000956 int err = 0;
957
Andreas Färber62278812013-06-27 17:12:06 +0200958 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200959 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200960 }
aliguorie22a25c2009-03-12 20:12:48 +0000961
aliguoria1d1bb32008-11-18 20:07:32 +0000962 switch (type) {
963 case GDB_BREAKPOINT_SW:
964 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200965 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200966 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
967 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000968 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200969 }
aliguori880a7572008-11-18 20:30:24 +0000970 }
971 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000972#ifndef CONFIG_USER_ONLY
973 case GDB_WATCHPOINT_WRITE:
974 case GDB_WATCHPOINT_READ:
975 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200976 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100977 err = cpu_watchpoint_insert(cpu, addr, len,
978 xlat_gdb_type(cpu, type), NULL);
979 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000980 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100981 }
aliguori880a7572008-11-18 20:30:24 +0000982 }
983 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000984#endif
985 default:
986 return -ENOSYS;
987 }
988}
989
aliguori880a7572008-11-18 20:30:24 +0000990static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000991{
Andreas Färber182735e2013-05-29 22:29:20 +0200992 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000993 int err = 0;
994
Andreas Färber62278812013-06-27 17:12:06 +0200995 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200996 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200997 }
aliguorie22a25c2009-03-12 20:12:48 +0000998
aliguoria1d1bb32008-11-18 20:07:32 +0000999 switch (type) {
1000 case GDB_BREAKPOINT_SW:
1001 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001002 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001003 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1004 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001005 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001006 }
aliguori880a7572008-11-18 20:30:24 +00001007 }
1008 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001009#ifndef CONFIG_USER_ONLY
1010 case GDB_WATCHPOINT_WRITE:
1011 case GDB_WATCHPOINT_READ:
1012 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001013 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001014 err = cpu_watchpoint_remove(cpu, addr, len,
1015 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001016 if (err)
1017 break;
1018 }
1019 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001020#endif
1021 default:
1022 return -ENOSYS;
1023 }
1024}
1025
Luc Michel546f3c62019-01-07 15:23:46 +00001026static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1027{
1028 cpu_breakpoint_remove_all(cpu, BP_GDB);
1029#ifndef CONFIG_USER_ONLY
1030 cpu_watchpoint_remove_all(cpu, BP_GDB);
1031#endif
1032}
1033
1034static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p)
1035{
1036 CPUState *cpu = get_first_cpu_in_process(s, p);
1037
1038 while (cpu) {
1039 gdb_cpu_breakpoint_remove_all(cpu);
1040 cpu = gdb_next_cpu_in_process(s, cpu);
1041 }
1042}
1043
aliguori880a7572008-11-18 20:30:24 +00001044static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001045{
Andreas Färber182735e2013-05-29 22:29:20 +02001046 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001047
aliguorie22a25c2009-03-12 20:12:48 +00001048 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001049 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001050 return;
1051 }
1052
Andreas Färberbdc44642013-06-24 23:50:24 +02001053 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001054 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001055 }
aliguoria1d1bb32008-11-18 20:07:32 +00001056}
1057
aurel32fab9d282009-04-08 21:29:37 +00001058static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1059{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001060 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001061
1062 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001063 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001064}
1065
Luc Michel1a227332019-01-07 15:23:45 +00001066static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1067 char *buf, size_t buf_size)
1068{
1069 if (s->multiprocess) {
1070 snprintf(buf, buf_size, "p%02x.%02x",
1071 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1072 } else {
1073 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1074 }
1075
1076 return buf;
1077}
1078
Luc Michel7d8c87d2019-01-07 15:23:45 +00001079typedef enum GDBThreadIdKind {
1080 GDB_ONE_THREAD = 0,
1081 GDB_ALL_THREADS, /* One process, all threads */
1082 GDB_ALL_PROCESSES,
1083 GDB_READ_THREAD_ERR
1084} GDBThreadIdKind;
1085
1086static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1087 uint32_t *pid, uint32_t *tid)
1088{
1089 unsigned long p, t;
1090 int ret;
1091
1092 if (*buf == 'p') {
1093 buf++;
1094 ret = qemu_strtoul(buf, &buf, 16, &p);
1095
1096 if (ret) {
1097 return GDB_READ_THREAD_ERR;
1098 }
1099
1100 /* Skip '.' */
1101 buf++;
1102 } else {
1103 p = 1;
1104 }
1105
1106 ret = qemu_strtoul(buf, &buf, 16, &t);
1107
1108 if (ret) {
1109 return GDB_READ_THREAD_ERR;
1110 }
1111
1112 *end_buf = buf;
1113
1114 if (p == -1) {
1115 return GDB_ALL_PROCESSES;
1116 }
1117
1118 if (pid) {
1119 *pid = p;
1120 }
1121
1122 if (t == -1) {
1123 return GDB_ALL_THREADS;
1124 }
1125
1126 if (tid) {
1127 *tid = t;
1128 }
1129
1130 return GDB_ONE_THREAD;
1131}
1132
Jan Kiszka4dabe742015-02-07 09:38:43 +01001133static int is_query_packet(const char *p, const char *query, char separator)
1134{
1135 unsigned int query_len = strlen(query);
1136
1137 return strncmp(p, query, query_len) == 0 &&
1138 (p[query_len] == '\0' || p[query_len] == separator);
1139}
1140
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001141/**
1142 * gdb_handle_vcont - Parses and handles a vCont packet.
1143 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1144 * a format error, 0 on success.
1145 */
1146static int gdb_handle_vcont(GDBState *s, const char *p)
1147{
Luc Michele40e5202019-01-07 15:23:46 +00001148 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001149 char cur_action;
1150 char *newstates;
1151 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001152 uint32_t pid, tid;
1153 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001154 CPUState *cpu;
1155#ifdef CONFIG_USER_ONLY
1156 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1157
1158 CPU_FOREACH(cpu) {
1159 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1160 }
1161#endif
1162 /* uninitialised CPUs stay 0 */
1163 newstates = g_new0(char, max_cpus);
1164
1165 /* mark valid CPUs with 1 */
1166 CPU_FOREACH(cpu) {
1167 newstates[cpu->cpu_index] = 1;
1168 }
1169
1170 /*
1171 * res keeps track of what error we are returning, with -ENOTSUP meaning
1172 * that the command is unknown or unsupported, thus returning an empty
1173 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1174 * or incorrect parameters passed.
1175 */
1176 res = 0;
1177 while (*p) {
1178 if (*p++ != ';') {
1179 res = -ENOTSUP;
1180 goto out;
1181 }
1182
1183 cur_action = *p++;
1184 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001185 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001186 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1187 if (res) {
1188 goto out;
1189 }
1190 signal = gdb_signal_to_target(tmp);
1191 } else if (cur_action != 'c' && cur_action != 's') {
1192 /* unknown/invalid/unsupported command */
1193 res = -ENOTSUP;
1194 goto out;
1195 }
Luc Michele40e5202019-01-07 15:23:46 +00001196
1197 if (*p++ != ':') {
1198 res = -ENOTSUP;
1199 goto out;
1200 }
1201
1202 switch (read_thread_id(p, &p, &pid, &tid)) {
1203 case GDB_READ_THREAD_ERR:
1204 res = -EINVAL;
1205 goto out;
1206
1207 case GDB_ALL_PROCESSES:
1208 cpu = gdb_first_attached_cpu(s);
1209 while (cpu) {
1210 if (newstates[cpu->cpu_index] == 1) {
1211 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001212 }
Luc Michele40e5202019-01-07 15:23:46 +00001213
1214 cpu = gdb_next_attached_cpu(s, cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001215 }
Luc Michele40e5202019-01-07 15:23:46 +00001216 break;
1217
1218 case GDB_ALL_THREADS:
1219 process = gdb_get_process(s, pid);
1220
1221 if (!process->attached) {
1222 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001223 goto out;
1224 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001225
Luc Michele40e5202019-01-07 15:23:46 +00001226 cpu = get_first_cpu_in_process(s, process);
1227 while (cpu) {
1228 if (newstates[cpu->cpu_index] == 1) {
1229 newstates[cpu->cpu_index] = cur_action;
1230 }
1231
1232 cpu = gdb_next_cpu_in_process(s, cpu);
1233 }
1234 break;
1235
1236 case GDB_ONE_THREAD:
1237 cpu = gdb_get_cpu(s, pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001238
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001239 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001240 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001241 res = -EINVAL;
1242 goto out;
1243 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001244
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001245 /* only use if no previous match occourred */
1246 if (newstates[cpu->cpu_index] == 1) {
1247 newstates[cpu->cpu_index] = cur_action;
1248 }
Luc Michele40e5202019-01-07 15:23:46 +00001249 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001250 }
1251 }
1252 s->signal = signal;
1253 gdb_continue_partial(s, newstates);
1254
1255out:
1256 g_free(newstates);
1257
1258 return res;
1259}
1260
aliguori880a7572008-11-18 20:30:24 +00001261static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +00001262{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001263 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001264 GDBProcess *process;
Andreas Färber5b24c642013-07-07 15:08:22 +02001265 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +00001266 const char *p;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001267 uint32_t pid, tid;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001268 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +00001269 uint8_t mem_buf[MAX_PACKET_LENGTH];
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -03001270 char buf[sizeof(mem_buf) + 1 /* trailing NUL */];
Luc Michel1a227332019-01-07 15:23:45 +00001271 char thread_id[16];
pbrook56aebc82008-10-11 17:55:29 +00001272 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001273 target_ulong addr, len;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001274 GDBThreadIdKind thread_kind;
ths3b46e622007-09-17 08:09:54 +00001275
Doug Gale5c9522b2017-12-02 20:30:37 -05001276 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01001277
bellard858693c2004-03-31 18:52:07 +00001278 p = line_buf;
1279 ch = *p++;
1280 switch(ch) {
Luc Michel53fd6552019-01-07 15:23:46 +00001281 case '!':
1282 put_packet(s, "OK");
1283 break;
bellard858693c2004-03-31 18:52:07 +00001284 case '?':
bellard1fddef42005-04-17 19:16:13 +00001285 /* TODO: Make this return the correct value for user-mode. */
Luc Michel1a227332019-01-07 15:23:45 +00001286 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1287 gdb_fmt_thread_id(s, s->c_cpu, thread_id, sizeof(thread_id)));
bellard858693c2004-03-31 18:52:07 +00001288 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001289 /* Remove all the breakpoints when this query is issued,
1290 * because gdb is doing and initial connect and the state
1291 * should be cleaned up.
1292 */
aliguori880a7572008-11-18 20:30:24 +00001293 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001294 break;
1295 case 'c':
1296 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001297 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001298 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001299 }
aurel32ca587a82008-12-18 22:44:13 +00001300 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001301 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001302 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001303 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001304 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1305 if (s->signal == -1)
1306 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001307 gdb_continue(s);
1308 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001309 case 'v':
1310 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001311 p += 4;
1312 if (*p == '?') {
1313 put_packet(s, "vCont;c;C;s;S");
1314 break;
1315 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001316
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001317 res = gdb_handle_vcont(s, p);
1318
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001319 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001320 if ((res == -EINVAL) || (res == -ERANGE)) {
1321 put_packet(s, "E22");
1322 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001323 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001324 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001325 }
1326 break;
Luc Michel3f940dc2019-01-07 15:23:46 +00001327 } else if (strncmp(p, "Attach;", 7) == 0) {
1328 unsigned long pid;
1329
1330 p += 7;
1331
1332 if (qemu_strtoul(p, &p, 16, &pid)) {
1333 put_packet(s, "E22");
1334 break;
1335 }
1336
1337 process = gdb_get_process(s, pid);
1338
1339 if (process == NULL) {
1340 put_packet(s, "E22");
1341 break;
1342 }
1343
1344 cpu = get_first_cpu_in_process(s, process);
1345
1346 if (cpu == NULL) {
1347 /* Refuse to attach an empty process */
1348 put_packet(s, "E22");
1349 break;
1350 }
1351
1352 process->attached = true;
1353
1354 s->g_cpu = cpu;
1355 s->c_cpu = cpu;
1356
1357 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1358 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)));
1359
1360 put_packet(s, buf);
1361 break;
Max Filippov45a4de22019-02-05 16:52:41 +00001362 } else if (strncmp(p, "Kill;", 5) == 0) {
1363 /* Kill the target */
Sandra Loosemore0f8b09b2019-02-15 09:56:41 +00001364 put_packet(s, "OK");
Max Filippov45a4de22019-02-05 16:52:41 +00001365 error_report("QEMU: Terminated via GDBstub");
1366 exit(0);
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001367 } else {
1368 goto unknown_command;
1369 }
edgar_igl7d03f822008-05-17 18:58:29 +00001370 case 'k':
1371 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001372 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001373 exit(0);
1374 case 'D':
1375 /* Detach packet */
Luc Michel546f3c62019-01-07 15:23:46 +00001376 pid = 1;
1377
1378 if (s->multiprocess) {
1379 unsigned long lpid;
1380 if (*p != ';') {
1381 put_packet(s, "E22");
1382 break;
1383 }
1384
1385 if (qemu_strtoul(p + 1, &p, 16, &lpid)) {
1386 put_packet(s, "E22");
1387 break;
1388 }
1389
1390 pid = lpid;
1391 }
1392
1393 process = gdb_get_process(s, pid);
1394 gdb_process_breakpoint_remove_all(s, process);
1395 process->attached = false;
1396
1397 if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
1398 s->c_cpu = gdb_first_attached_cpu(s);
1399 }
1400
1401 if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
1402 s->g_cpu = gdb_first_attached_cpu(s);
1403 }
1404
1405 if (s->c_cpu == NULL) {
1406 /* No more process attached */
1407 gdb_syscall_mode = GDB_SYS_DISABLED;
1408 gdb_continue(s);
1409 }
edgar_igl7d03f822008-05-17 18:58:29 +00001410 put_packet(s, "OK");
1411 break;
bellard858693c2004-03-31 18:52:07 +00001412 case 's':
1413 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001414 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001415 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001416 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001417 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001418 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001419 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001420 case 'F':
1421 {
1422 target_ulong ret;
1423 target_ulong err;
1424
1425 ret = strtoull(p, (char **)&p, 16);
1426 if (*p == ',') {
1427 p++;
1428 err = strtoull(p, (char **)&p, 16);
1429 } else {
1430 err = 0;
1431 }
1432 if (*p == ',')
1433 p++;
1434 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001435 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001436 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001437 s->current_syscall_cb = NULL;
1438 }
pbrooka2d1eba2007-01-28 03:10:55 +00001439 if (type == 'C') {
1440 put_packet(s, "T02");
1441 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001442 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001443 }
1444 }
1445 break;
bellard858693c2004-03-31 18:52:07 +00001446 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001447 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001448 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001449 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001450 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001451 len += reg_size;
1452 }
1453 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001454 put_packet(s, buf);
1455 break;
1456 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001457 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001458 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001459 len = strlen(p) / 2;
1460 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001461 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001462 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001463 len -= reg_size;
1464 registers += reg_size;
1465 }
bellard858693c2004-03-31 18:52:07 +00001466 put_packet(s, "OK");
1467 break;
1468 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001469 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001470 if (*p == ',')
1471 p++;
bellard9d9754a2006-06-25 15:32:37 +00001472 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001473
1474 /* memtohex() doubles the required space */
1475 if (len > MAX_PACKET_LENGTH / 2) {
1476 put_packet (s, "E22");
1477 break;
1478 }
1479
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001480 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001481 put_packet (s, "E14");
1482 } else {
1483 memtohex(buf, mem_buf, len);
1484 put_packet(s, buf);
1485 }
bellard858693c2004-03-31 18:52:07 +00001486 break;
1487 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001488 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001489 if (*p == ',')
1490 p++;
bellard9d9754a2006-06-25 15:32:37 +00001491 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001492 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001493 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001494
1495 /* hextomem() reads 2*len bytes */
1496 if (len > strlen(p) / 2) {
1497 put_packet (s, "E22");
1498 break;
1499 }
bellard858693c2004-03-31 18:52:07 +00001500 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001501 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001502 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001503 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001504 } else {
bellard858693c2004-03-31 18:52:07 +00001505 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001506 }
bellard858693c2004-03-31 18:52:07 +00001507 break;
pbrook56aebc82008-10-11 17:55:29 +00001508 case 'p':
1509 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1510 This works, but can be very slow. Anything new enough to
1511 understand XML also knows how to use this properly. */
1512 if (!gdb_has_xml)
1513 goto unknown_command;
1514 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001515 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001516 if (reg_size) {
1517 memtohex(buf, mem_buf, reg_size);
1518 put_packet(s, buf);
1519 } else {
1520 put_packet(s, "E14");
1521 }
1522 break;
1523 case 'P':
1524 if (!gdb_has_xml)
1525 goto unknown_command;
1526 addr = strtoull(p, (char **)&p, 16);
1527 if (*p == '=')
1528 p++;
1529 reg_size = strlen(p) / 2;
1530 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001531 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001532 put_packet(s, "OK");
1533 break;
bellard858693c2004-03-31 18:52:07 +00001534 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001535 case 'z':
1536 type = strtoul(p, (char **)&p, 16);
1537 if (*p == ',')
1538 p++;
bellard9d9754a2006-06-25 15:32:37 +00001539 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001540 if (*p == ',')
1541 p++;
bellard9d9754a2006-06-25 15:32:37 +00001542 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001543 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001544 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001545 else
aliguori880a7572008-11-18 20:30:24 +00001546 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001547 if (res >= 0)
1548 put_packet(s, "OK");
1549 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001550 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001551 else
1552 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001553 break;
aliguori880a7572008-11-18 20:30:24 +00001554 case 'H':
1555 type = *p++;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001556
1557 thread_kind = read_thread_id(p, &p, &pid, &tid);
1558 if (thread_kind == GDB_READ_THREAD_ERR) {
1559 put_packet(s, "E22");
1560 break;
1561 }
1562
1563 if (thread_kind != GDB_ONE_THREAD) {
aliguori880a7572008-11-18 20:30:24 +00001564 put_packet(s, "OK");
1565 break;
1566 }
Luc Michel7d8c87d2019-01-07 15:23:45 +00001567 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001568 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001569 put_packet(s, "E22");
1570 break;
1571 }
1572 switch (type) {
1573 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001574 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001575 put_packet(s, "OK");
1576 break;
1577 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001578 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001579 put_packet(s, "OK");
1580 break;
1581 default:
1582 put_packet(s, "E22");
1583 break;
1584 }
1585 break;
1586 case 'T':
Luc Michel7d8c87d2019-01-07 15:23:45 +00001587 thread_kind = read_thread_id(p, &p, &pid, &tid);
1588 if (thread_kind == GDB_READ_THREAD_ERR) {
1589 put_packet(s, "E22");
1590 break;
1591 }
1592 cpu = gdb_get_cpu(s, pid, tid);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001593
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001594 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001595 put_packet(s, "OK");
1596 } else {
aliguori880a7572008-11-18 20:30:24 +00001597 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001598 }
aliguori880a7572008-11-18 20:30:24 +00001599 break;
pbrook978efd62006-06-17 18:30:42 +00001600 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001601 case 'Q':
1602 /* parse any 'q' packets here */
1603 if (!strcmp(p,"qemu.sstepbits")) {
1604 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001605 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1606 SSTEP_ENABLE,
1607 SSTEP_NOIRQ,
1608 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001609 put_packet(s, buf);
1610 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001611 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001612 /* Display or change the sstep_flags */
1613 p += 10;
1614 if (*p != '=') {
1615 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001616 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001617 put_packet(s, buf);
1618 break;
1619 }
1620 p++;
1621 type = strtoul(p, (char **)&p, 16);
1622 sstep_flags = type;
1623 put_packet(s, "OK");
1624 break;
aliguori880a7572008-11-18 20:30:24 +00001625 } else if (strcmp(p,"C") == 0) {
Luc Michel8dbbe9a2019-01-07 15:23:46 +00001626 /*
1627 * "Current thread" remains vague in the spec, so always return
1628 * the first thread of the current process (gdb returns the
1629 * first thread).
1630 */
1631 cpu = get_first_cpu_in_process(s, gdb_get_cpu_process(s, s->g_cpu));
1632 snprintf(buf, sizeof(buf), "QC%s",
1633 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)));
1634 put_packet(s, buf);
aliguori880a7572008-11-18 20:30:24 +00001635 break;
1636 } else if (strcmp(p,"fThreadInfo") == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001637 s->query_cpu = gdb_first_attached_cpu(s);
aliguori880a7572008-11-18 20:30:24 +00001638 goto report_cpuinfo;
1639 } else if (strcmp(p,"sThreadInfo") == 0) {
1640 report_cpuinfo:
1641 if (s->query_cpu) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001642 snprintf(buf, sizeof(buf), "m%s",
1643 gdb_fmt_thread_id(s, s->query_cpu,
1644 thread_id, sizeof(thread_id)));
aliguori880a7572008-11-18 20:30:24 +00001645 put_packet(s, buf);
Luc Michel7cf48f62019-01-07 15:23:46 +00001646 s->query_cpu = gdb_next_attached_cpu(s, s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001647 } else
1648 put_packet(s, "l");
1649 break;
1650 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001651 if (read_thread_id(p + 16, &p, &pid, &tid) == GDB_READ_THREAD_ERR) {
1652 put_packet(s, "E22");
1653 break;
1654 }
1655 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001656 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001657 cpu_synchronize_state(cpu);
Luc Michel7cf48f62019-01-07 15:23:46 +00001658
1659 if (s->multiprocess && (s->process_num > 1)) {
1660 /* Print the CPU model and name in multiprocess mode */
1661 ObjectClass *oc = object_get_class(OBJECT(cpu));
1662 const char *cpu_model = object_class_get_name(oc);
1663 char *cpu_name =
1664 object_get_canonical_path_component(OBJECT(cpu));
1665 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
1666 "%s %s [%s]", cpu_model, cpu_name,
1667 cpu->halted ? "halted " : "running");
1668 g_free(cpu_name);
1669 } else {
1670 /* memtohex() doubles the required space */
1671 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
1672 "CPU#%d [%s]", cpu->cpu_index,
1673 cpu->halted ? "halted " : "running");
1674 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001675 trace_gdbstub_op_extra_info((char *)mem_buf);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001676 memtohex(buf, mem_buf, len);
1677 put_packet(s, buf);
1678 }
aliguori880a7572008-11-18 20:30:24 +00001679 break;
edgar_igl60897d32008-05-09 08:25:14 +00001680 }
blueswir10b8a9882009-03-07 10:51:36 +00001681#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001682 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001683 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001684
blueswir1363a37d2008-08-21 17:58:08 +00001685 snprintf(buf, sizeof(buf),
1686 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1687 ";Bss=" TARGET_ABI_FMT_lx,
1688 ts->info->code_offset,
1689 ts->info->data_offset,
1690 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001691 put_packet(s, buf);
1692 break;
1693 }
blueswir10b8a9882009-03-07 10:51:36 +00001694#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001695 else if (strncmp(p, "Rcmd,", 5) == 0) {
1696 int len = strlen(p + 5);
1697
1698 if ((len % 2) != 0) {
1699 put_packet(s, "E01");
1700 break;
1701 }
aliguori8a34a0f2009-03-05 23:01:55 +00001702 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001703 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001704 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001705 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001706 put_packet(s, "OK");
1707 break;
1708 }
blueswir10b8a9882009-03-07 10:51:36 +00001709#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001710 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001711 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001712 cc = CPU_GET_CLASS(first_cpu);
1713 if (cc->gdb_core_xml_file != NULL) {
1714 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1715 }
Luc Michel364fce62019-01-07 15:23:46 +00001716
1717 if (strstr(p, "multiprocess+")) {
1718 s->multiprocess = true;
1719 }
1720 pstrcat(buf, sizeof(buf), ";multiprocess+");
1721
pbrook56aebc82008-10-11 17:55:29 +00001722 put_packet(s, buf);
1723 break;
1724 }
pbrook56aebc82008-10-11 17:55:29 +00001725 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1726 const char *xml;
1727 target_ulong total_len;
1728
Luc Michelc145eea2019-01-07 15:23:46 +00001729 process = gdb_get_cpu_process(s, s->g_cpu);
1730 cc = CPU_GET_CLASS(s->g_cpu);
Andreas Färber5b24c642013-07-07 15:08:22 +02001731 if (cc->gdb_core_xml_file == NULL) {
1732 goto unknown_command;
1733 }
1734
Andreas Färber5b50e792013-06-29 04:18:45 +02001735 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001736 p += 19;
Luc Michelc145eea2019-01-07 15:23:46 +00001737 xml = get_feature_xml(s, p, &p, process);
pbrook56aebc82008-10-11 17:55:29 +00001738 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001739 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001740 put_packet(s, buf);
1741 break;
1742 }
1743
1744 if (*p == ':')
1745 p++;
1746 addr = strtoul(p, (char **)&p, 16);
1747 if (*p == ',')
1748 p++;
1749 len = strtoul(p, (char **)&p, 16);
1750
1751 total_len = strlen(xml);
1752 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001753 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001754 put_packet(s, buf);
1755 break;
1756 }
1757 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1758 len = (MAX_PACKET_LENGTH - 5) / 2;
1759 if (len < total_len - addr) {
1760 buf[0] = 'm';
1761 len = memtox(buf + 1, xml + addr, len);
1762 } else {
1763 buf[0] = 'l';
1764 len = memtox(buf + 1, xml + addr, total_len - addr);
1765 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001766 put_packet_binary(s, buf, len + 1, true);
pbrook56aebc82008-10-11 17:55:29 +00001767 break;
1768 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001769 if (is_query_packet(p, "Attached", ':')) {
1770 put_packet(s, GDB_ATTACHED);
1771 break;
1772 }
pbrook56aebc82008-10-11 17:55:29 +00001773 /* Unrecognised 'q' command. */
1774 goto unknown_command;
1775
bellard858693c2004-03-31 18:52:07 +00001776 default:
pbrook56aebc82008-10-11 17:55:29 +00001777 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001778 /* put empty packet */
1779 buf[0] = '\0';
1780 put_packet(s, buf);
1781 break;
1782 }
1783 return RS_IDLE;
1784}
1785
Andreas Färber64f6b342013-05-27 02:06:09 +02001786void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001787{
Luc Michel160d8582019-01-07 15:23:46 +00001788 GDBProcess *p = gdb_get_cpu_process(gdbserver_state, cpu);
1789
1790 if (!p->attached) {
1791 /*
1792 * Having a stop CPU corresponding to a process that is not attached
1793 * confuses GDB. So we ignore the request.
1794 */
1795 return;
1796 }
1797
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001798 gdbserver_state->c_cpu = cpu;
1799 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001800}
1801
bellard1fddef42005-04-17 19:16:13 +00001802#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001803static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001804{
aliguori880a7572008-11-18 20:30:24 +00001805 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001806 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001807 char buf[256];
Luc Michel95567c22019-01-07 15:23:46 +00001808 char thread_id[16];
aliguorid6fc1b32008-11-18 19:55:44 +00001809 const char *type;
bellard858693c2004-03-31 18:52:07 +00001810 int ret;
1811
Meador Ingecdb432b2012-03-15 17:49:45 +00001812 if (running || s->state == RS_INACTIVE) {
1813 return;
1814 }
1815 /* Is there a GDB syscall waiting to be sent? */
1816 if (s->current_syscall_cb) {
1817 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001818 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001819 }
Luc Michel95567c22019-01-07 15:23:46 +00001820
1821 if (cpu == NULL) {
1822 /* No process attached */
1823 return;
1824 }
1825
1826 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id));
1827
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001828 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001829 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001830 if (cpu->watchpoint_hit) {
1831 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001832 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001833 type = "r";
1834 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001835 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001836 type = "a";
1837 break;
1838 default:
1839 type = "";
1840 break;
1841 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001842 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
1843 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00001844 snprintf(buf, sizeof(buf),
Luc Michel95567c22019-01-07 15:23:46 +00001845 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
1846 GDB_SIGNAL_TRAP, thread_id, type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001847 (target_ulong)cpu->watchpoint_hit->vaddr);
1848 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001849 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05001850 } else {
1851 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00001852 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001853 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001854 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001855 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001856 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05001857 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00001858 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001859 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001860 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05001861 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01001862 ret = GDB_SIGNAL_QUIT;
1863 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001864 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001865 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001866 ret = GDB_SIGNAL_IO;
1867 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001868 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05001869 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01001870 ret = GDB_SIGNAL_ALRM;
1871 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001872 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001873 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001874 ret = GDB_SIGNAL_ABRT;
1875 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001876 case RUN_STATE_SAVE_VM:
1877 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001878 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001879 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001880 ret = GDB_SIGNAL_XCPU;
1881 break;
1882 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05001883 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01001884 ret = GDB_SIGNAL_UNKNOWN;
1885 break;
bellardbbeb7b52006-04-23 18:42:15 +00001886 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001887 gdb_set_stop_cpu(cpu);
Luc Michel95567c22019-01-07 15:23:46 +00001888 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
Jan Kiszka425189a2011-03-22 11:02:09 +01001889
1890send_packet:
bellard858693c2004-03-31 18:52:07 +00001891 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001892
1893 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001894 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001895}
bellard1fddef42005-04-17 19:16:13 +00001896#endif
bellard858693c2004-03-31 18:52:07 +00001897
pbrooka2d1eba2007-01-28 03:10:55 +00001898/* Send a gdb syscall request.
1899 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001900 %x - target_ulong argument printed in hex.
1901 %lx - 64-bit argument printed in hex.
1902 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001903void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001904{
pbrooka2d1eba2007-01-28 03:10:55 +00001905 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001906 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001907 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001908 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001909 GDBState *s;
1910
aliguori880a7572008-11-18 20:30:24 +00001911 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001912 if (!s)
1913 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001914 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001915#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001916 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001917#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001918 p = s->syscall_buf;
1919 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001920 *(p++) = 'F';
1921 while (*fmt) {
1922 if (*fmt == '%') {
1923 fmt++;
1924 switch (*fmt++) {
1925 case 'x':
1926 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001927 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001928 break;
pbrooka87295e2007-05-26 15:09:38 +00001929 case 'l':
1930 if (*(fmt++) != 'x')
1931 goto bad_format;
1932 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001933 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001934 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001935 case 's':
1936 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001937 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001938 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001939 break;
1940 default:
pbrooka87295e2007-05-26 15:09:38 +00001941 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001942 error_report("gdbstub: Bad syscall format string '%s'",
1943 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001944 break;
1945 }
1946 } else {
1947 *(p++) = *(fmt++);
1948 }
1949 }
pbrook8a93e022007-08-06 13:19:15 +00001950 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001951#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001952 put_packet(s, s->syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01001953 /* Return control to gdb for it to process the syscall request.
1954 * Since the protocol requires that gdb hands control back to us
1955 * using a "here are the results" F packet, we don't need to check
1956 * gdb_handlesig's return value (which is the signal to deliver if
1957 * execution was resumed via a continue packet).
1958 */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001959 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001960#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001961 /* In this case wait to send the syscall packet until notification that
1962 the CPU has stopped. This must be done because if the packet is sent
1963 now the reply from the syscall request could be received while the CPU
1964 is still in the running state, which can cause packets to be dropped
1965 and state transition 'T' packets to be sent while the syscall is still
1966 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001967 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001968#endif
1969}
1970
Peter Maydell19239b32015-09-07 10:39:27 +01001971void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1972{
1973 va_list va;
1974
1975 va_start(va, fmt);
1976 gdb_do_syscallv(cb, fmt, va);
1977 va_end(va);
1978}
1979
bellard6a00d602005-11-21 23:25:50 +00001980static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001981{
ths60fe76f2007-12-16 03:02:09 +00001982 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001983
bellard1fddef42005-04-17 19:16:13 +00001984#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001985 if (s->last_packet_len) {
1986 /* Waiting for a response to the last packet. If we see the start
1987 of a new command then abandon the previous response. */
1988 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05001989 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00001990 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01001991 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05001992 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01001993 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05001994 trace_gdbstub_io_got_unexpected((uint8_t)ch);
pbrook4046d912007-01-28 01:53:16 +00001995 }
Alex Bennée118e2262017-07-12 11:52:13 +01001996
pbrook4046d912007-01-28 01:53:16 +00001997 if (ch == '+' || ch == '$')
1998 s->last_packet_len = 0;
1999 if (ch != '$')
2000 return;
2001 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002002 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002003 /* when the CPU is running, we cannot do anything except stop
2004 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002005 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002006 } else
bellard1fddef42005-04-17 19:16:13 +00002007#endif
bellard41625032005-04-24 10:07:11 +00002008 {
bellard858693c2004-03-31 18:52:07 +00002009 switch(s->state) {
2010 case RS_IDLE:
2011 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002012 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00002013 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04002014 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00002015 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002016 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05002017 trace_gdbstub_err_garbage((uint8_t)ch);
bellard4c3a88a2003-07-26 12:06:08 +00002018 }
2019 break;
bellard858693c2004-03-31 18:52:07 +00002020 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002021 if (ch == '}') {
2022 /* start escape sequence */
2023 s->state = RS_GETLINE_ESC;
2024 s->line_sum += ch;
2025 } else if (ch == '*') {
2026 /* start run length encoding sequence */
2027 s->state = RS_GETLINE_RLE;
2028 s->line_sum += ch;
2029 } else if (ch == '#') {
2030 /* end of command, start of checksum*/
2031 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00002032 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002033 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00002034 s->state = RS_IDLE;
2035 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002036 /* unescaped command character */
2037 s->line_buf[s->line_buf_index++] = ch;
2038 s->line_sum += ch;
2039 }
2040 break;
2041 case RS_GETLINE_ESC:
2042 if (ch == '#') {
2043 /* unexpected end of command in escape sequence */
2044 s->state = RS_CHKSUM1;
2045 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2046 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002047 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002048 s->state = RS_IDLE;
2049 } else {
2050 /* parse escaped character and leave escape state */
2051 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
2052 s->line_sum += ch;
2053 s->state = RS_GETLINE;
2054 }
2055 break;
2056 case RS_GETLINE_RLE:
2057 if (ch < ' ') {
2058 /* invalid RLE count encoding */
Doug Gale5c9522b2017-12-02 20:30:37 -05002059 trace_gdbstub_err_invalid_repeat((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002060 s->state = RS_GETLINE;
2061 } else {
2062 /* decode repeat length */
2063 int repeat = (unsigned char)ch - ' ' + 3;
2064 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
2065 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002066 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002067 s->state = RS_IDLE;
2068 } else if (s->line_buf_index < 1) {
2069 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002070 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04002071 s->state = RS_GETLINE;
2072 } else {
2073 /* repeat the last character */
2074 memset(s->line_buf + s->line_buf_index,
2075 s->line_buf[s->line_buf_index - 1], repeat);
2076 s->line_buf_index += repeat;
2077 s->line_sum += ch;
2078 s->state = RS_GETLINE;
2079 }
bellard858693c2004-03-31 18:52:07 +00002080 }
2081 break;
2082 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002083 /* get high hex digit of checksum */
2084 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002085 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002086 s->state = RS_GETLINE;
2087 break;
2088 }
bellard858693c2004-03-31 18:52:07 +00002089 s->line_buf[s->line_buf_index] = '\0';
2090 s->line_csum = fromhex(ch) << 4;
2091 s->state = RS_CHKSUM2;
2092 break;
2093 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002094 /* get low hex digit of checksum */
2095 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002096 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002097 s->state = RS_GETLINE;
2098 break;
bellard858693c2004-03-31 18:52:07 +00002099 }
Doug Gale4bf43122017-05-01 12:22:10 -04002100 s->line_csum |= fromhex(ch);
2101
2102 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002103 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002104 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002105 reply = '-';
2106 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002107 s->state = RS_IDLE;
2108 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002109 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002110 reply = '+';
2111 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002112 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002113 }
bellardb4608c02003-06-27 17:34:32 +00002114 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002115 default:
2116 abort();
bellardb4608c02003-06-27 17:34:32 +00002117 }
2118 }
bellard858693c2004-03-31 18:52:07 +00002119}
2120
Paul Brook0e1c9c52010-06-16 13:03:51 +01002121/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002122void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002123{
2124 GDBState *s;
2125 char buf[4];
2126
2127 s = gdbserver_state;
2128 if (!s) {
2129 return;
2130 }
2131#ifdef CONFIG_USER_ONLY
2132 if (gdbserver_fd < 0 || s->fd < 0) {
2133 return;
2134 }
2135#endif
2136
Doug Gale5c9522b2017-12-02 20:30:37 -05002137 trace_gdbstub_op_exiting((uint8_t)code);
2138
Paul Brook0e1c9c52010-06-16 13:03:51 +01002139 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2140 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002141
2142#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002143 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002144#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002145}
2146
Luc Michel8f468632019-01-07 15:23:45 +00002147/*
2148 * Create the process that will contain all the "orphan" CPUs (that are not
2149 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2150 * be attachable and thus will be invisible to the user.
2151 */
2152static void create_default_process(GDBState *s)
2153{
2154 GDBProcess *process;
2155 int max_pid = 0;
2156
2157 if (s->process_num) {
2158 max_pid = s->processes[s->process_num - 1].pid;
2159 }
2160
2161 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2162 process = &s->processes[s->process_num - 1];
2163
2164 /* We need an available PID slot for this process */
2165 assert(max_pid < UINT32_MAX);
2166
2167 process->pid = max_pid + 1;
2168 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002169 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002170}
2171
bellard1fddef42005-04-17 19:16:13 +00002172#ifdef CONFIG_USER_ONLY
2173int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02002174gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00002175{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002176 GDBState *s;
2177 char buf[256];
2178 int n;
bellard1fddef42005-04-17 19:16:13 +00002179
Andreas Färber5ca666c2013-06-24 19:20:57 +02002180 s = gdbserver_state;
2181 if (gdbserver_fd < 0 || s->fd < 0) {
2182 return sig;
bellard1fddef42005-04-17 19:16:13 +00002183 }
2184
Andreas Färber5ca666c2013-06-24 19:20:57 +02002185 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002186 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002187 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00002188
Andreas Färber5ca666c2013-06-24 19:20:57 +02002189 if (sig != 0) {
2190 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
2191 put_packet(s, buf);
2192 }
2193 /* put_packet() might have detected that the peer terminated the
2194 connection. */
2195 if (s->fd < 0) {
2196 return sig;
2197 }
2198
2199 sig = 0;
2200 s->state = RS_IDLE;
2201 s->running_state = 0;
2202 while (s->running_state == 0) {
2203 n = read(s->fd, buf, 256);
2204 if (n > 0) {
2205 int i;
2206
2207 for (i = 0; i < n; i++) {
2208 gdb_read_byte(s, buf[i]);
2209 }
Peter Wu5819e3e2016-06-05 16:35:48 +02002210 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02002211 /* XXX: Connection closed. Should probably wait for another
2212 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02002213 if (n == 0) {
2214 close(s->fd);
2215 }
2216 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02002217 return sig;
bellard1fddef42005-04-17 19:16:13 +00002218 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02002219 }
2220 sig = s->signal;
2221 s->signal = 0;
2222 return sig;
bellard1fddef42005-04-17 19:16:13 +00002223}
bellarde9009672005-04-26 20:42:36 +00002224
aurel32ca587a82008-12-18 22:44:13 +00002225/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002226void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00002227{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002228 GDBState *s;
2229 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00002230
Andreas Färber5ca666c2013-06-24 19:20:57 +02002231 s = gdbserver_state;
2232 if (gdbserver_fd < 0 || s->fd < 0) {
2233 return;
2234 }
aurel32ca587a82008-12-18 22:44:13 +00002235
Andreas Färber5ca666c2013-06-24 19:20:57 +02002236 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
2237 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00002238}
bellard1fddef42005-04-17 19:16:13 +00002239
Peter Maydell2f652222018-05-14 18:30:44 +01002240static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00002241{
2242 GDBState *s;
2243 struct sockaddr_in sockaddr;
2244 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09002245 int fd;
bellard858693c2004-03-31 18:52:07 +00002246
2247 for(;;) {
2248 len = sizeof(sockaddr);
2249 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2250 if (fd < 0 && errno != EINTR) {
2251 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01002252 return false;
bellard858693c2004-03-31 18:52:07 +00002253 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01002254 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002255 break;
2256 }
2257 }
2258
2259 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01002260 if (socket_set_nodelay(fd)) {
2261 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03002262 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01002263 return false;
2264 }
ths3b46e622007-09-17 08:09:54 +00002265
Anthony Liguori7267c092011-08-20 22:09:37 -05002266 s = g_malloc0(sizeof(GDBState));
Luc Michel8f468632019-01-07 15:23:45 +00002267 create_default_process(s);
Luc Michel970ed902019-01-07 15:23:46 +00002268 s->processes[0].attached = true;
2269 s->c_cpu = gdb_first_attached_cpu(s);
2270 s->g_cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00002271 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02002272 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00002273
aliguori880a7572008-11-18 20:30:24 +00002274 gdbserver_state = s;
Peter Maydell2f652222018-05-14 18:30:44 +01002275 return true;
bellard858693c2004-03-31 18:52:07 +00002276}
2277
2278static int gdbserver_open(int port)
2279{
2280 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002281 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00002282
2283 fd = socket(PF_INET, SOCK_STREAM, 0);
2284 if (fd < 0) {
2285 perror("socket");
2286 return -1;
2287 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01002288 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002289
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002290 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00002291
2292 sockaddr.sin_family = AF_INET;
2293 sockaddr.sin_port = htons(port);
2294 sockaddr.sin_addr.s_addr = 0;
2295 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2296 if (ret < 0) {
2297 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00002298 close(fd);
bellard858693c2004-03-31 18:52:07 +00002299 return -1;
2300 }
Peter Wu96165b92016-05-04 11:32:17 +02002301 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00002302 if (ret < 0) {
2303 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00002304 close(fd);
bellard858693c2004-03-31 18:52:07 +00002305 return -1;
2306 }
bellard858693c2004-03-31 18:52:07 +00002307 return fd;
2308}
2309
2310int gdbserver_start(int port)
2311{
2312 gdbserver_fd = gdbserver_open(port);
2313 if (gdbserver_fd < 0)
2314 return -1;
2315 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01002316 if (!gdb_accept()) {
2317 close(gdbserver_fd);
2318 gdbserver_fd = -1;
2319 return -1;
2320 }
bellardb4608c02003-06-27 17:34:32 +00002321 return 0;
2322}
aurel322b1319c2008-12-18 22:44:04 +00002323
2324/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07002325void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00002326{
2327 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02002328
2329 if (gdbserver_fd < 0 || s->fd < 0) {
2330 return;
2331 }
aurel322b1319c2008-12-18 22:44:04 +00002332 close(s->fd);
2333 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02002334 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02002335 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00002336}
pbrook4046d912007-01-28 01:53:16 +00002337#else
thsaa1f17c2007-07-11 22:48:58 +00002338static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00002339{
pbrook56aebc82008-10-11 17:55:29 +00002340 /* We can handle an arbitrarily large amount of data.
2341 Pick the maximum packet size, which is as good as anything. */
2342 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00002343}
2344
thsaa1f17c2007-07-11 22:48:58 +00002345static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00002346{
pbrook4046d912007-01-28 01:53:16 +00002347 int i;
2348
2349 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00002350 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00002351 }
2352}
2353
2354static void gdb_chr_event(void *opaque, int event)
2355{
Luc Michel970ed902019-01-07 15:23:46 +00002356 int i;
2357 GDBState *s = (GDBState *) opaque;
2358
pbrook4046d912007-01-28 01:53:16 +00002359 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05302360 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00002361 /* Start with first process attached, others detached */
2362 for (i = 0; i < s->process_num; i++) {
2363 s->processes[i].attached = !i;
2364 }
2365
2366 s->c_cpu = gdb_first_attached_cpu(s);
2367 s->g_cpu = s->c_cpu;
2368
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002369 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02002370 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00002371 break;
2372 default:
2373 break;
2374 }
2375}
2376
aliguori8a34a0f2009-03-05 23:01:55 +00002377static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2378{
2379 char buf[MAX_PACKET_LENGTH];
2380
2381 buf[0] = 'O';
2382 if (len > (MAX_PACKET_LENGTH/2) - 1)
2383 len = (MAX_PACKET_LENGTH/2) - 1;
2384 memtohex(buf + 1, (uint8_t *)msg, len);
2385 put_packet(s, buf);
2386}
2387
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002388static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00002389{
2390 const char *p = (const char *)buf;
2391 int max_sz;
2392
2393 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2394 for (;;) {
2395 if (len <= max_sz) {
2396 gdb_monitor_output(gdbserver_state, p, len);
2397 break;
2398 }
2399 gdb_monitor_output(gdbserver_state, p, max_sz);
2400 p += max_sz;
2401 len -= max_sz;
2402 }
2403 return len;
2404}
2405
aliguori59030a82009-04-05 18:43:41 +00002406#ifndef _WIN32
2407static void gdb_sigterm_handler(int signal)
2408{
Luiz Capitulino13548692011-07-29 15:36:43 -03002409 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002410 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002411 }
aliguori59030a82009-04-05 18:43:41 +00002412}
2413#endif
2414
Marc-André Lureau777357d2016-12-07 18:39:10 +03002415static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
2416 bool *be_opened, Error **errp)
2417{
2418 *be_opened = false;
2419}
2420
2421static void char_gdb_class_init(ObjectClass *oc, void *data)
2422{
2423 ChardevClass *cc = CHARDEV_CLASS(oc);
2424
2425 cc->internal = true;
2426 cc->open = gdb_monitor_open;
2427 cc->chr_write = gdb_monitor_write;
2428}
2429
2430#define TYPE_CHARDEV_GDB "chardev-gdb"
2431
2432static const TypeInfo char_gdb_type_info = {
2433 .name = TYPE_CHARDEV_GDB,
2434 .parent = TYPE_CHARDEV,
2435 .class_init = char_gdb_class_init,
2436};
2437
Luc Michel8f468632019-01-07 15:23:45 +00002438static int find_cpu_clusters(Object *child, void *opaque)
2439{
2440 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
2441 GDBState *s = (GDBState *) opaque;
2442 CPUClusterState *cluster = CPU_CLUSTER(child);
2443 GDBProcess *process;
2444
2445 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2446
2447 process = &s->processes[s->process_num - 1];
2448
2449 /*
2450 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2451 * runtime, we enforce here that the machine does not use a cluster ID
2452 * that would lead to PID 0.
2453 */
2454 assert(cluster->cluster_id != UINT32_MAX);
2455 process->pid = cluster->cluster_id + 1;
2456 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002457 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002458
2459 return 0;
2460 }
2461
2462 return object_child_foreach(child, find_cpu_clusters, opaque);
2463}
2464
2465static int pid_order(const void *a, const void *b)
2466{
2467 GDBProcess *pa = (GDBProcess *) a;
2468 GDBProcess *pb = (GDBProcess *) b;
2469
2470 if (pa->pid < pb->pid) {
2471 return -1;
2472 } else if (pa->pid > pb->pid) {
2473 return 1;
2474 } else {
2475 return 0;
2476 }
2477}
2478
2479static void create_processes(GDBState *s)
2480{
2481 object_child_foreach(object_get_root(), find_cpu_clusters, s);
2482
2483 if (s->processes) {
2484 /* Sort by PID */
2485 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
2486 }
2487
2488 create_default_process(s);
2489}
2490
2491static void cleanup_processes(GDBState *s)
2492{
2493 g_free(s->processes);
2494 s->process_num = 0;
2495 s->processes = NULL;
2496}
2497
aliguori59030a82009-04-05 18:43:41 +00002498int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00002499{
Doug Gale5c9522b2017-12-02 20:30:37 -05002500 trace_gdbstub_op_start(device);
2501
pbrook4046d912007-01-28 01:53:16 +00002502 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00002503 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002504 Chardev *chr = NULL;
2505 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00002506
Ziyue Yang508b4ec2017-01-18 16:02:41 +08002507 if (!first_cpu) {
2508 error_report("gdbstub: meaningless to attach gdb to a "
2509 "machine without any CPU.");
2510 return -1;
2511 }
2512
aliguori59030a82009-04-05 18:43:41 +00002513 if (!device)
2514 return -1;
2515 if (strcmp(device, "none") != 0) {
2516 if (strstart(device, "tcp:", NULL)) {
2517 /* enforce required TCP attributes */
2518 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2519 "%s,nowait,nodelay,server", device);
2520 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00002521 }
aliguori59030a82009-04-05 18:43:41 +00002522#ifndef _WIN32
2523 else if (strcmp(device, "stdio") == 0) {
2524 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00002525
aliguori59030a82009-04-05 18:43:41 +00002526 memset(&act, 0, sizeof(act));
2527 act.sa_handler = gdb_sigterm_handler;
2528 sigaction(SIGINT, &act, NULL);
2529 }
2530#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02002531 /*
2532 * FIXME: it's a bit weird to allow using a mux chardev here
2533 * and implicitly setup a monitor. We may want to break this.
2534 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01002535 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
aliguori36556b22009-03-28 18:05:53 +00002536 if (!chr)
2537 return -1;
pbrookcfc34752007-02-22 01:48:01 +00002538 }
2539
aliguori36556b22009-03-28 18:05:53 +00002540 s = gdbserver_state;
2541 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002542 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00002543 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002544
aliguori36556b22009-03-28 18:05:53 +00002545 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2546
2547 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03002548 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01002549 NULL, NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00002550 monitor_init(mon_chr, 0);
2551 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002552 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00002553 mon_chr = s->mon_chr;
Luc Michel8f468632019-01-07 15:23:45 +00002554 cleanup_processes(s);
aliguori36556b22009-03-28 18:05:53 +00002555 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002556 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002557 }
Luc Michel8f468632019-01-07 15:23:45 +00002558
2559 create_processes(s);
2560
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002561 if (chr) {
2562 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002563 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Luc Michel970ed902019-01-07 15:23:46 +00002564 gdb_chr_event, NULL, s, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002565 }
aliguori36556b22009-03-28 18:05:53 +00002566 s->state = chr ? RS_IDLE : RS_INACTIVE;
2567 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002568 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002569
pbrook4046d912007-01-28 01:53:16 +00002570 return 0;
2571}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002572
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01002573void gdbserver_cleanup(void)
2574{
2575 if (gdbserver_state) {
2576 put_packet(gdbserver_state, "W00");
2577 }
2578}
2579
Marc-André Lureau777357d2016-12-07 18:39:10 +03002580static void register_types(void)
2581{
2582 type_register_static(&char_gdb_type_info);
2583}
2584
2585type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002586#endif