blob: f70b5a326feb636414deed05b46e0a183c6b3198 [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;
303} GDBProcess;
304
bellard858693c2004-03-31 18:52:07 +0000305enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000306 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000307 RS_IDLE,
308 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400309 RS_GETLINE_ESC,
310 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000311 RS_CHKSUM1,
312 RS_CHKSUM2,
313};
bellard858693c2004-03-31 18:52:07 +0000314typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200315 CPUState *c_cpu; /* current CPU for step/continue ops */
316 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200317 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000318 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000319 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000320 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400321 int line_sum; /* running checksum */
322 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000323 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000324 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000325 int signal;
bellard41625032005-04-24 10:07:11 +0000326#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000327 int fd;
bellard41625032005-04-24 10:07:11 +0000328 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000329#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300330 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300331 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000332#endif
Luc Michel8f468632019-01-07 15:23:45 +0000333 bool multiprocess;
334 GDBProcess *processes;
335 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000336 char syscall_buf[256];
337 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000338} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000339
edgar_igl60897d32008-05-09 08:25:14 +0000340/* By default use no IRQs and no timers while single stepping so as to
341 * make single stepping like an ICE HW step.
342 */
343static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
344
aliguori880a7572008-11-18 20:30:24 +0000345static GDBState *gdbserver_state;
346
Andreas Färber5b50e792013-06-29 04:18:45 +0200347bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000348
bellard1fddef42005-04-17 19:16:13 +0000349#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000350/* XXX: This is not thread safe. Do we care? */
351static int gdbserver_fd = -1;
352
bellard858693c2004-03-31 18:52:07 +0000353static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000354{
355 uint8_t ch;
356 int ret;
357
358 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000359 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000360 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000361 if (errno == ECONNRESET)
362 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200363 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000364 return -1;
365 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000366 close(s->fd);
367 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000368 return -1;
369 } else {
370 break;
371 }
372 }
373 return ch;
374}
pbrook4046d912007-01-28 01:53:16 +0000375#endif
bellardb4608c02003-06-27 17:34:32 +0000376
blueswir1654efcf2009-04-18 07:29:59 +0000377static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000378 GDB_SYS_UNKNOWN,
379 GDB_SYS_ENABLED,
380 GDB_SYS_DISABLED,
381} gdb_syscall_mode;
382
Liviu Ionescua38bb072014-12-11 12:07:48 +0000383/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000384int use_gdb_syscalls(void)
385{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100386 SemihostingTarget target = semihosting_get_target();
387 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000388 /* -semihosting-config target=native */
389 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100390 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000391 /* -semihosting-config target=gdb */
392 return true;
393 }
394
395 /* -semihosting-config target=auto */
396 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000397 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000398 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
399 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000400 }
401 return gdb_syscall_mode == GDB_SYS_ENABLED;
402}
403
edgar_iglba70a622008-03-14 06:10:42 +0000404/* Resume execution. */
405static inline void gdb_continue(GDBState *s)
406{
Doug Gale5c9522b2017-12-02 20:30:37 -0500407
edgar_iglba70a622008-03-14 06:10:42 +0000408#ifdef CONFIG_USER_ONLY
409 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500410 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000411#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200412 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500413 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200414 vm_start();
415 }
edgar_iglba70a622008-03-14 06:10:42 +0000416#endif
417}
418
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100419/*
420 * Resume execution, per CPU actions. For user-mode emulation it's
421 * equivalent to gdb_continue.
422 */
423static int gdb_continue_partial(GDBState *s, char *newstates)
424{
425 CPUState *cpu;
426 int res = 0;
427#ifdef CONFIG_USER_ONLY
428 /*
429 * This is not exactly accurate, but it's an improvement compared to the
430 * previous situation, where only one CPU would be single-stepped.
431 */
432 CPU_FOREACH(cpu) {
433 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500434 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100435 cpu_single_step(cpu, sstep_flags);
436 }
437 }
438 s->running_state = 1;
439#else
440 int flag = 0;
441
442 if (!runstate_needs_reset()) {
443 if (vm_prepare_start()) {
444 return 0;
445 }
446
447 CPU_FOREACH(cpu) {
448 switch (newstates[cpu->cpu_index]) {
449 case 0:
450 case 1:
451 break; /* nothing to do here */
452 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500453 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100454 cpu_single_step(cpu, sstep_flags);
455 cpu_resume(cpu);
456 flag = 1;
457 break;
458 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500459 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100460 cpu_resume(cpu);
461 flag = 1;
462 break;
463 default:
464 res = -1;
465 break;
466 }
467 }
468 }
469 if (flag) {
470 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
471 }
472#endif
473 return res;
474}
475
bellard858693c2004-03-31 18:52:07 +0000476static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000477{
pbrook4046d912007-01-28 01:53:16 +0000478#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000479 int ret;
480
481 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000482 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000483 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200484 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000485 return;
486 } else {
487 buf += ret;
488 len -= ret;
489 }
490 }
pbrook4046d912007-01-28 01:53:16 +0000491#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100492 /* XXX this blocks entire thread. Rewrite to use
493 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300494 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000495#endif
bellardb4608c02003-06-27 17:34:32 +0000496}
497
498static inline int fromhex(int v)
499{
500 if (v >= '0' && v <= '9')
501 return v - '0';
502 else if (v >= 'A' && v <= 'F')
503 return v - 'A' + 10;
504 else if (v >= 'a' && v <= 'f')
505 return v - 'a' + 10;
506 else
507 return 0;
508}
509
510static inline int tohex(int v)
511{
512 if (v < 10)
513 return v + '0';
514 else
515 return v - 10 + 'a';
516}
517
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300518/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000519static void memtohex(char *buf, const uint8_t *mem, int len)
520{
521 int i, c;
522 char *q;
523 q = buf;
524 for(i = 0; i < len; i++) {
525 c = mem[i];
526 *q++ = tohex(c >> 4);
527 *q++ = tohex(c & 0xf);
528 }
529 *q = '\0';
530}
531
532static void hextomem(uint8_t *mem, const char *buf, int len)
533{
534 int i;
535
536 for(i = 0; i < len; i++) {
537 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
538 buf += 2;
539 }
540}
541
Doug Gale5c9522b2017-12-02 20:30:37 -0500542static void hexdump(const char *buf, int len,
543 void (*trace_fn)(size_t ofs, char const *text))
544{
545 char line_buffer[3 * 16 + 4 + 16 + 1];
546
547 size_t i;
548 for (i = 0; i < len || (i & 0xF); ++i) {
549 size_t byte_ofs = i & 15;
550
551 if (byte_ofs == 0) {
552 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
553 line_buffer[3 * 16 + 4 + 16] = 0;
554 }
555
556 size_t col_group = (i >> 2) & 3;
557 size_t hex_col = byte_ofs * 3 + col_group;
558 size_t txt_col = 3 * 16 + 4 + byte_ofs;
559
560 if (i < len) {
561 char value = buf[i];
562
563 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
564 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
565 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
566 ? value
567 : '.';
568 }
569
570 if (byte_ofs == 0xF)
571 trace_fn(i & -16, line_buffer);
572 }
573}
574
bellardb4608c02003-06-27 17:34:32 +0000575/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500576static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000577{
pbrook56aebc82008-10-11 17:55:29 +0000578 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000579 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000580
Doug Gale5c9522b2017-12-02 20:30:37 -0500581 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
582 hexdump(buf, len, trace_gdbstub_io_binaryreply);
583 }
584
bellardb4608c02003-06-27 17:34:32 +0000585 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000586 p = s->last_packet;
587 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000588 memcpy(p, buf, len);
589 p += len;
bellardb4608c02003-06-27 17:34:32 +0000590 csum = 0;
591 for(i = 0; i < len; i++) {
592 csum += buf[i];
593 }
pbrook4046d912007-01-28 01:53:16 +0000594 *(p++) = '#';
595 *(p++) = tohex((csum >> 4) & 0xf);
596 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000597
pbrook4046d912007-01-28 01:53:16 +0000598 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000599 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000600
pbrook4046d912007-01-28 01:53:16 +0000601#ifdef CONFIG_USER_ONLY
602 i = get_char(s);
603 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000604 return -1;
pbrook4046d912007-01-28 01:53:16 +0000605 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000606 break;
pbrook4046d912007-01-28 01:53:16 +0000607#else
608 break;
609#endif
bellardb4608c02003-06-27 17:34:32 +0000610 }
611 return 0;
612}
613
pbrook56aebc82008-10-11 17:55:29 +0000614/* return -1 if error, 0 if OK */
615static int put_packet(GDBState *s, const char *buf)
616{
Doug Gale5c9522b2017-12-02 20:30:37 -0500617 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000618
Doug Gale5c9522b2017-12-02 20:30:37 -0500619 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000620}
621
pbrook56aebc82008-10-11 17:55:29 +0000622/* Encode data using the encoding for 'x' packets. */
623static int memtox(char *buf, const char *mem, int len)
624{
625 char *p = buf;
626 char c;
627
628 while (len--) {
629 c = *(mem++);
630 switch (c) {
631 case '#': case '$': case '*': case '}':
632 *(p++) = '}';
633 *(p++) = c ^ 0x20;
634 break;
635 default:
636 *(p++) = c;
637 break;
638 }
639 }
640 return p - buf;
641}
642
Luc Michel1a227332019-01-07 15:23:45 +0000643static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
644{
645#ifndef CONFIG_USER_ONLY
646 gchar *path, *name = NULL;
647 Object *obj;
648 CPUClusterState *cluster;
649 uint32_t ret;
650
651 path = object_get_canonical_path(OBJECT(cpu));
652
653 if (path == NULL) {
654 /* Return the default process' PID */
655 ret = s->processes[s->process_num - 1].pid;
656 goto out;
657 }
658
659 name = object_get_canonical_path_component(OBJECT(cpu));
660 assert(name != NULL);
661
662 /*
663 * Retrieve the CPU parent path by removing the last '/' and the CPU name
664 * from the CPU canonical path.
665 */
666 path[strlen(path) - strlen(name) - 1] = '\0';
667
668 obj = object_resolve_path_type(path, TYPE_CPU_CLUSTER, NULL);
669
670 if (obj == NULL) {
671 /* Return the default process' PID */
672 ret = s->processes[s->process_num - 1].pid;
673 goto out;
674 }
675
676 cluster = CPU_CLUSTER(obj);
677 ret = cluster->cluster_id + 1;
678
679out:
680 g_free(name);
681 g_free(path);
682
683 return ret;
684
685#else
686 /* TODO: In user mode, we should use the task state PID */
687 return s->processes[s->process_num - 1].pid;
688#endif
689}
690
Luc Michel7d8c87d2019-01-07 15:23:45 +0000691static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
692{
693 int i;
694
695 if (!pid) {
696 /* 0 means any process, we take the first one */
697 return &s->processes[0];
698 }
699
700 for (i = 0; i < s->process_num; i++) {
701 if (s->processes[i].pid == pid) {
702 return &s->processes[i];
703 }
704 }
705
706 return NULL;
707}
708
709static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
710{
711 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
712}
713
714static CPUState *find_cpu(uint32_t thread_id)
715{
716 CPUState *cpu;
717
718 CPU_FOREACH(cpu) {
719 if (cpu_gdb_index(cpu) == thread_id) {
720 return cpu;
721 }
722 }
723
724 return NULL;
725}
726
Luc Michele40e5202019-01-07 15:23:46 +0000727static CPUState *get_first_cpu_in_process(const GDBState *s,
728 GDBProcess *process)
729{
730 CPUState *cpu;
731
732 CPU_FOREACH(cpu) {
733 if (gdb_get_cpu_pid(s, cpu) == process->pid) {
734 return cpu;
735 }
736 }
737
738 return NULL;
739}
740
741static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu)
742{
743 uint32_t pid = gdb_get_cpu_pid(s, cpu);
744 cpu = CPU_NEXT(cpu);
745
746 while (cpu) {
747 if (gdb_get_cpu_pid(s, cpu) == pid) {
748 break;
749 }
750
751 cpu = CPU_NEXT(cpu);
752 }
753
754 return cpu;
755}
756
Luc Michel7d8c87d2019-01-07 15:23:45 +0000757static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
758{
759 GDBProcess *process;
760 CPUState *cpu;
761
762 if (!tid) {
763 /* 0 means any thread, we take the first one */
764 tid = 1;
765 }
766
767 cpu = find_cpu(tid);
768
769 if (cpu == NULL) {
770 return NULL;
771 }
772
773 process = gdb_get_cpu_process(s, cpu);
774
775 if (process->pid != pid) {
776 return NULL;
777 }
778
779 if (!process->attached) {
780 return NULL;
781 }
782
783 return cpu;
784}
785
Luc Michele40e5202019-01-07 15:23:46 +0000786/* Return the cpu following @cpu, while ignoring unattached processes. */
787static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
788{
789 cpu = CPU_NEXT(cpu);
790
791 while (cpu) {
792 if (gdb_get_cpu_process(s, cpu)->attached) {
793 break;
794 }
795
796 cpu = CPU_NEXT(cpu);
797 }
798
799 return cpu;
800}
801
802/* Return the first attached cpu */
803static CPUState *gdb_first_attached_cpu(const GDBState *s)
804{
805 CPUState *cpu = first_cpu;
806 GDBProcess *process = gdb_get_cpu_process(s, cpu);
807
808 if (!process->attached) {
809 return gdb_next_attached_cpu(s, cpu);
810 }
811
812 return cpu;
813}
814
Andreas Färber5b24c642013-07-07 15:08:22 +0200815static const char *get_feature_xml(const char *p, const char **newp,
816 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000817{
pbrook56aebc82008-10-11 17:55:29 +0000818 size_t len;
819 int i;
820 const char *name;
821 static char target_xml[1024];
822
823 len = 0;
824 while (p[len] && p[len] != ':')
825 len++;
826 *newp = p + len;
827
828 name = NULL;
829 if (strncmp(p, "target.xml", len) == 0) {
830 /* Generate the XML description for this CPU. */
831 if (!target_xml[0]) {
832 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200833 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000834
David Hildenbrandb3820e62015-12-03 13:14:41 +0100835 pstrcat(target_xml, sizeof(target_xml),
836 "<?xml version=\"1.0\"?>"
837 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
838 "<target>");
839 if (cc->gdb_arch_name) {
840 gchar *arch = cc->gdb_arch_name(cpu);
841 pstrcat(target_xml, sizeof(target_xml), "<architecture>");
842 pstrcat(target_xml, sizeof(target_xml), arch);
843 pstrcat(target_xml, sizeof(target_xml), "</architecture>");
844 g_free(arch);
845 }
846 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
847 pstrcat(target_xml, sizeof(target_xml), cc->gdb_core_xml_file);
848 pstrcat(target_xml, sizeof(target_xml), "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200849 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000850 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
851 pstrcat(target_xml, sizeof(target_xml), r->xml);
852 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000853 }
blueswir12dc766d2009-04-13 16:06:19 +0000854 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000855 }
856 return target_xml;
857 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100858 if (cc->gdb_get_dynamic_xml) {
859 CPUState *cpu = first_cpu;
860 char *xmlname = g_strndup(p, len);
861 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
862
863 g_free(xmlname);
864 if (xml) {
865 return xml;
866 }
867 }
pbrook56aebc82008-10-11 17:55:29 +0000868 for (i = 0; ; i++) {
869 name = xml_builtin[i][0];
870 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
871 break;
872 }
873 return name ? xml_builtin[i][1] : NULL;
874}
pbrook56aebc82008-10-11 17:55:29 +0000875
Andreas Färber385b9f02013-06-27 18:25:36 +0200876static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000877{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200878 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200879 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000880 GDBRegisterState *r;
881
Andreas Färbera0e372f2013-06-28 23:18:47 +0200882 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200883 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200884 }
pbrook56aebc82008-10-11 17:55:29 +0000885
Andreas Färbereac8b352013-06-28 21:11:37 +0200886 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000887 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
888 return r->get_reg(env, mem_buf, reg - r->base_reg);
889 }
890 }
891 return 0;
892}
893
Andreas Färber385b9f02013-06-27 18:25:36 +0200894static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000895{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200896 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200897 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000898 GDBRegisterState *r;
899
Andreas Färbera0e372f2013-06-28 23:18:47 +0200900 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200901 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200902 }
pbrook56aebc82008-10-11 17:55:29 +0000903
Andreas Färbereac8b352013-06-28 21:11:37 +0200904 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000905 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
906 return r->set_reg(env, mem_buf, reg - r->base_reg);
907 }
908 }
909 return 0;
910}
911
912/* Register a supplemental set of CPU registers. If g_pos is nonzero it
913 specifies the first register number and these registers are included in
914 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
915 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
916 */
917
Andreas Färber22169d42013-06-28 21:27:39 +0200918void gdb_register_coprocessor(CPUState *cpu,
919 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
920 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000921{
922 GDBRegisterState *s;
923 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000924
Andreas Färbereac8b352013-06-28 21:11:37 +0200925 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000926 while (*p) {
927 /* Check for duplicates. */
928 if (strcmp((*p)->xml, xml) == 0)
929 return;
930 p = &(*p)->next;
931 }
Stefan Weil9643c252011-10-18 22:25:38 +0200932
933 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200934 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200935 s->num_regs = num_regs;
936 s->get_reg = get_reg;
937 s->set_reg = set_reg;
938 s->xml = xml;
939
pbrook56aebc82008-10-11 17:55:29 +0000940 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200941 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000942 *p = s;
943 if (g_pos) {
944 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800945 error_report("Error: Bad gdb register numbering for '%s', "
946 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200947 } else {
948 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000949 }
950 }
951}
952
aliguoria1d1bb32008-11-18 20:07:32 +0000953#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100954/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
955static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
956{
957 static const int xlat[] = {
958 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
959 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
960 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
961 };
962
963 CPUClass *cc = CPU_GET_CLASS(cpu);
964 int cputype = xlat[gdbtype];
965
966 if (cc->gdb_stop_before_watchpoint) {
967 cputype |= BP_STOP_BEFORE_ACCESS;
968 }
969 return cputype;
970}
aliguoria1d1bb32008-11-18 20:07:32 +0000971#endif
972
aliguori880a7572008-11-18 20:30:24 +0000973static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000974{
Andreas Färber182735e2013-05-29 22:29:20 +0200975 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000976 int err = 0;
977
Andreas Färber62278812013-06-27 17:12:06 +0200978 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200979 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200980 }
aliguorie22a25c2009-03-12 20:12:48 +0000981
aliguoria1d1bb32008-11-18 20:07:32 +0000982 switch (type) {
983 case GDB_BREAKPOINT_SW:
984 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200985 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200986 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
987 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000988 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200989 }
aliguori880a7572008-11-18 20:30:24 +0000990 }
991 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000992#ifndef CONFIG_USER_ONLY
993 case GDB_WATCHPOINT_WRITE:
994 case GDB_WATCHPOINT_READ:
995 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200996 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100997 err = cpu_watchpoint_insert(cpu, addr, len,
998 xlat_gdb_type(cpu, type), NULL);
999 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001000 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +01001001 }
aliguori880a7572008-11-18 20:30:24 +00001002 }
1003 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001004#endif
1005 default:
1006 return -ENOSYS;
1007 }
1008}
1009
aliguori880a7572008-11-18 20:30:24 +00001010static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +00001011{
Andreas Färber182735e2013-05-29 22:29:20 +02001012 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001013 int err = 0;
1014
Andreas Färber62278812013-06-27 17:12:06 +02001015 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001016 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001017 }
aliguorie22a25c2009-03-12 20:12:48 +00001018
aliguoria1d1bb32008-11-18 20:07:32 +00001019 switch (type) {
1020 case GDB_BREAKPOINT_SW:
1021 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001022 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001023 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1024 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001025 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001026 }
aliguori880a7572008-11-18 20:30:24 +00001027 }
1028 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001029#ifndef CONFIG_USER_ONLY
1030 case GDB_WATCHPOINT_WRITE:
1031 case GDB_WATCHPOINT_READ:
1032 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001033 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001034 err = cpu_watchpoint_remove(cpu, addr, len,
1035 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001036 if (err)
1037 break;
1038 }
1039 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001040#endif
1041 default:
1042 return -ENOSYS;
1043 }
1044}
1045
aliguori880a7572008-11-18 20:30:24 +00001046static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001047{
Andreas Färber182735e2013-05-29 22:29:20 +02001048 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001049
aliguorie22a25c2009-03-12 20:12:48 +00001050 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001051 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001052 return;
1053 }
1054
Andreas Färberbdc44642013-06-24 23:50:24 +02001055 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001056 cpu_breakpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +00001057#ifndef CONFIG_USER_ONLY
Andreas Färber75a34032013-09-02 16:57:02 +02001058 cpu_watchpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +00001059#endif
aliguori880a7572008-11-18 20:30:24 +00001060 }
aliguoria1d1bb32008-11-18 20:07:32 +00001061}
1062
aurel32fab9d282009-04-08 21:29:37 +00001063static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1064{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001065 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001066
1067 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001068 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001069}
1070
Luc Michel1a227332019-01-07 15:23:45 +00001071static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1072 char *buf, size_t buf_size)
1073{
1074 if (s->multiprocess) {
1075 snprintf(buf, buf_size, "p%02x.%02x",
1076 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1077 } else {
1078 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1079 }
1080
1081 return buf;
1082}
1083
Luc Michel7d8c87d2019-01-07 15:23:45 +00001084typedef enum GDBThreadIdKind {
1085 GDB_ONE_THREAD = 0,
1086 GDB_ALL_THREADS, /* One process, all threads */
1087 GDB_ALL_PROCESSES,
1088 GDB_READ_THREAD_ERR
1089} GDBThreadIdKind;
1090
1091static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1092 uint32_t *pid, uint32_t *tid)
1093{
1094 unsigned long p, t;
1095 int ret;
1096
1097 if (*buf == 'p') {
1098 buf++;
1099 ret = qemu_strtoul(buf, &buf, 16, &p);
1100
1101 if (ret) {
1102 return GDB_READ_THREAD_ERR;
1103 }
1104
1105 /* Skip '.' */
1106 buf++;
1107 } else {
1108 p = 1;
1109 }
1110
1111 ret = qemu_strtoul(buf, &buf, 16, &t);
1112
1113 if (ret) {
1114 return GDB_READ_THREAD_ERR;
1115 }
1116
1117 *end_buf = buf;
1118
1119 if (p == -1) {
1120 return GDB_ALL_PROCESSES;
1121 }
1122
1123 if (pid) {
1124 *pid = p;
1125 }
1126
1127 if (t == -1) {
1128 return GDB_ALL_THREADS;
1129 }
1130
1131 if (tid) {
1132 *tid = t;
1133 }
1134
1135 return GDB_ONE_THREAD;
1136}
1137
Jan Kiszka4dabe742015-02-07 09:38:43 +01001138static int is_query_packet(const char *p, const char *query, char separator)
1139{
1140 unsigned int query_len = strlen(query);
1141
1142 return strncmp(p, query, query_len) == 0 &&
1143 (p[query_len] == '\0' || p[query_len] == separator);
1144}
1145
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001146/**
1147 * gdb_handle_vcont - Parses and handles a vCont packet.
1148 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1149 * a format error, 0 on success.
1150 */
1151static int gdb_handle_vcont(GDBState *s, const char *p)
1152{
Luc Michele40e5202019-01-07 15:23:46 +00001153 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001154 char cur_action;
1155 char *newstates;
1156 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001157 uint32_t pid, tid;
1158 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001159 CPUState *cpu;
1160#ifdef CONFIG_USER_ONLY
1161 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1162
1163 CPU_FOREACH(cpu) {
1164 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1165 }
1166#endif
1167 /* uninitialised CPUs stay 0 */
1168 newstates = g_new0(char, max_cpus);
1169
1170 /* mark valid CPUs with 1 */
1171 CPU_FOREACH(cpu) {
1172 newstates[cpu->cpu_index] = 1;
1173 }
1174
1175 /*
1176 * res keeps track of what error we are returning, with -ENOTSUP meaning
1177 * that the command is unknown or unsupported, thus returning an empty
1178 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1179 * or incorrect parameters passed.
1180 */
1181 res = 0;
1182 while (*p) {
1183 if (*p++ != ';') {
1184 res = -ENOTSUP;
1185 goto out;
1186 }
1187
1188 cur_action = *p++;
1189 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001190 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001191 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1192 if (res) {
1193 goto out;
1194 }
1195 signal = gdb_signal_to_target(tmp);
1196 } else if (cur_action != 'c' && cur_action != 's') {
1197 /* unknown/invalid/unsupported command */
1198 res = -ENOTSUP;
1199 goto out;
1200 }
Luc Michele40e5202019-01-07 15:23:46 +00001201
1202 if (*p++ != ':') {
1203 res = -ENOTSUP;
1204 goto out;
1205 }
1206
1207 switch (read_thread_id(p, &p, &pid, &tid)) {
1208 case GDB_READ_THREAD_ERR:
1209 res = -EINVAL;
1210 goto out;
1211
1212 case GDB_ALL_PROCESSES:
1213 cpu = gdb_first_attached_cpu(s);
1214 while (cpu) {
1215 if (newstates[cpu->cpu_index] == 1) {
1216 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001217 }
Luc Michele40e5202019-01-07 15:23:46 +00001218
1219 cpu = gdb_next_attached_cpu(s, cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001220 }
Luc Michele40e5202019-01-07 15:23:46 +00001221 break;
1222
1223 case GDB_ALL_THREADS:
1224 process = gdb_get_process(s, pid);
1225
1226 if (!process->attached) {
1227 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001228 goto out;
1229 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001230
Luc Michele40e5202019-01-07 15:23:46 +00001231 cpu = get_first_cpu_in_process(s, process);
1232 while (cpu) {
1233 if (newstates[cpu->cpu_index] == 1) {
1234 newstates[cpu->cpu_index] = cur_action;
1235 }
1236
1237 cpu = gdb_next_cpu_in_process(s, cpu);
1238 }
1239 break;
1240
1241 case GDB_ONE_THREAD:
1242 cpu = gdb_get_cpu(s, pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001243
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001244 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001245 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001246 res = -EINVAL;
1247 goto out;
1248 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001249
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001250 /* only use if no previous match occourred */
1251 if (newstates[cpu->cpu_index] == 1) {
1252 newstates[cpu->cpu_index] = cur_action;
1253 }
Luc Michele40e5202019-01-07 15:23:46 +00001254 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001255 }
1256 }
1257 s->signal = signal;
1258 gdb_continue_partial(s, newstates);
1259
1260out:
1261 g_free(newstates);
1262
1263 return res;
1264}
1265
aliguori880a7572008-11-18 20:30:24 +00001266static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +00001267{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001268 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +02001269 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +00001270 const char *p;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001271 uint32_t pid, tid;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001272 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +00001273 uint8_t mem_buf[MAX_PACKET_LENGTH];
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -03001274 char buf[sizeof(mem_buf) + 1 /* trailing NUL */];
Luc Michel1a227332019-01-07 15:23:45 +00001275 char thread_id[16];
pbrook56aebc82008-10-11 17:55:29 +00001276 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001277 target_ulong addr, len;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001278 GDBThreadIdKind thread_kind;
ths3b46e622007-09-17 08:09:54 +00001279
Doug Gale5c9522b2017-12-02 20:30:37 -05001280 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01001281
bellard858693c2004-03-31 18:52:07 +00001282 p = line_buf;
1283 ch = *p++;
1284 switch(ch) {
1285 case '?':
bellard1fddef42005-04-17 19:16:13 +00001286 /* TODO: Make this return the correct value for user-mode. */
Luc Michel1a227332019-01-07 15:23:45 +00001287 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1288 gdb_fmt_thread_id(s, s->c_cpu, thread_id, sizeof(thread_id)));
bellard858693c2004-03-31 18:52:07 +00001289 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001290 /* Remove all the breakpoints when this query is issued,
1291 * because gdb is doing and initial connect and the state
1292 * should be cleaned up.
1293 */
aliguori880a7572008-11-18 20:30:24 +00001294 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001295 break;
1296 case 'c':
1297 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001298 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001299 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001300 }
aurel32ca587a82008-12-18 22:44:13 +00001301 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001302 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001303 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001304 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001305 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1306 if (s->signal == -1)
1307 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001308 gdb_continue(s);
1309 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001310 case 'v':
1311 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001312 p += 4;
1313 if (*p == '?') {
1314 put_packet(s, "vCont;c;C;s;S");
1315 break;
1316 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001317
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001318 res = gdb_handle_vcont(s, p);
1319
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001320 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001321 if ((res == -EINVAL) || (res == -ERANGE)) {
1322 put_packet(s, "E22");
1323 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001324 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001325 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001326 }
1327 break;
1328 } else {
1329 goto unknown_command;
1330 }
edgar_igl7d03f822008-05-17 18:58:29 +00001331 case 'k':
1332 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001333 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001334 exit(0);
1335 case 'D':
1336 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +00001337 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -03001338 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +00001339 gdb_continue(s);
1340 put_packet(s, "OK");
1341 break;
bellard858693c2004-03-31 18:52:07 +00001342 case 's':
1343 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001344 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001345 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001346 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001347 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001348 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001349 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001350 case 'F':
1351 {
1352 target_ulong ret;
1353 target_ulong err;
1354
1355 ret = strtoull(p, (char **)&p, 16);
1356 if (*p == ',') {
1357 p++;
1358 err = strtoull(p, (char **)&p, 16);
1359 } else {
1360 err = 0;
1361 }
1362 if (*p == ',')
1363 p++;
1364 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001365 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001366 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001367 s->current_syscall_cb = NULL;
1368 }
pbrooka2d1eba2007-01-28 03:10:55 +00001369 if (type == 'C') {
1370 put_packet(s, "T02");
1371 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001372 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001373 }
1374 }
1375 break;
bellard858693c2004-03-31 18:52:07 +00001376 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001377 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001378 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001379 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001380 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001381 len += reg_size;
1382 }
1383 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001384 put_packet(s, buf);
1385 break;
1386 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001387 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001388 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001389 len = strlen(p) / 2;
1390 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001391 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001392 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001393 len -= reg_size;
1394 registers += reg_size;
1395 }
bellard858693c2004-03-31 18:52:07 +00001396 put_packet(s, "OK");
1397 break;
1398 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001399 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001400 if (*p == ',')
1401 p++;
bellard9d9754a2006-06-25 15:32:37 +00001402 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001403
1404 /* memtohex() doubles the required space */
1405 if (len > MAX_PACKET_LENGTH / 2) {
1406 put_packet (s, "E22");
1407 break;
1408 }
1409
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001410 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001411 put_packet (s, "E14");
1412 } else {
1413 memtohex(buf, mem_buf, len);
1414 put_packet(s, buf);
1415 }
bellard858693c2004-03-31 18:52:07 +00001416 break;
1417 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001418 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001419 if (*p == ',')
1420 p++;
bellard9d9754a2006-06-25 15:32:37 +00001421 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001422 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001423 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001424
1425 /* hextomem() reads 2*len bytes */
1426 if (len > strlen(p) / 2) {
1427 put_packet (s, "E22");
1428 break;
1429 }
bellard858693c2004-03-31 18:52:07 +00001430 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001431 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001432 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001433 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001434 } else {
bellard858693c2004-03-31 18:52:07 +00001435 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001436 }
bellard858693c2004-03-31 18:52:07 +00001437 break;
pbrook56aebc82008-10-11 17:55:29 +00001438 case 'p':
1439 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1440 This works, but can be very slow. Anything new enough to
1441 understand XML also knows how to use this properly. */
1442 if (!gdb_has_xml)
1443 goto unknown_command;
1444 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001445 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001446 if (reg_size) {
1447 memtohex(buf, mem_buf, reg_size);
1448 put_packet(s, buf);
1449 } else {
1450 put_packet(s, "E14");
1451 }
1452 break;
1453 case 'P':
1454 if (!gdb_has_xml)
1455 goto unknown_command;
1456 addr = strtoull(p, (char **)&p, 16);
1457 if (*p == '=')
1458 p++;
1459 reg_size = strlen(p) / 2;
1460 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001461 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001462 put_packet(s, "OK");
1463 break;
bellard858693c2004-03-31 18:52:07 +00001464 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001465 case 'z':
1466 type = strtoul(p, (char **)&p, 16);
1467 if (*p == ',')
1468 p++;
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, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001473 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001474 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001475 else
aliguori880a7572008-11-18 20:30:24 +00001476 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001477 if (res >= 0)
1478 put_packet(s, "OK");
1479 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001480 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001481 else
1482 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001483 break;
aliguori880a7572008-11-18 20:30:24 +00001484 case 'H':
1485 type = *p++;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001486
1487 thread_kind = read_thread_id(p, &p, &pid, &tid);
1488 if (thread_kind == GDB_READ_THREAD_ERR) {
1489 put_packet(s, "E22");
1490 break;
1491 }
1492
1493 if (thread_kind != GDB_ONE_THREAD) {
aliguori880a7572008-11-18 20:30:24 +00001494 put_packet(s, "OK");
1495 break;
1496 }
Luc Michel7d8c87d2019-01-07 15:23:45 +00001497 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001498 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001499 put_packet(s, "E22");
1500 break;
1501 }
1502 switch (type) {
1503 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001504 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001505 put_packet(s, "OK");
1506 break;
1507 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001508 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001509 put_packet(s, "OK");
1510 break;
1511 default:
1512 put_packet(s, "E22");
1513 break;
1514 }
1515 break;
1516 case 'T':
Luc Michel7d8c87d2019-01-07 15:23:45 +00001517 thread_kind = read_thread_id(p, &p, &pid, &tid);
1518 if (thread_kind == GDB_READ_THREAD_ERR) {
1519 put_packet(s, "E22");
1520 break;
1521 }
1522 cpu = gdb_get_cpu(s, pid, tid);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001523
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001524 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001525 put_packet(s, "OK");
1526 } else {
aliguori880a7572008-11-18 20:30:24 +00001527 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001528 }
aliguori880a7572008-11-18 20:30:24 +00001529 break;
pbrook978efd62006-06-17 18:30:42 +00001530 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001531 case 'Q':
1532 /* parse any 'q' packets here */
1533 if (!strcmp(p,"qemu.sstepbits")) {
1534 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001535 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1536 SSTEP_ENABLE,
1537 SSTEP_NOIRQ,
1538 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001539 put_packet(s, buf);
1540 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001541 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001542 /* Display or change the sstep_flags */
1543 p += 10;
1544 if (*p != '=') {
1545 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001546 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001547 put_packet(s, buf);
1548 break;
1549 }
1550 p++;
1551 type = strtoul(p, (char **)&p, 16);
1552 sstep_flags = type;
1553 put_packet(s, "OK");
1554 break;
aliguori880a7572008-11-18 20:30:24 +00001555 } else if (strcmp(p,"C") == 0) {
Luc Michel8dbbe9a2019-01-07 15:23:46 +00001556 /*
1557 * "Current thread" remains vague in the spec, so always return
1558 * the first thread of the current process (gdb returns the
1559 * first thread).
1560 */
1561 cpu = get_first_cpu_in_process(s, gdb_get_cpu_process(s, s->g_cpu));
1562 snprintf(buf, sizeof(buf), "QC%s",
1563 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)));
1564 put_packet(s, buf);
aliguori880a7572008-11-18 20:30:24 +00001565 break;
1566 } else if (strcmp(p,"fThreadInfo") == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001567 s->query_cpu = gdb_first_attached_cpu(s);
aliguori880a7572008-11-18 20:30:24 +00001568 goto report_cpuinfo;
1569 } else if (strcmp(p,"sThreadInfo") == 0) {
1570 report_cpuinfo:
1571 if (s->query_cpu) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001572 snprintf(buf, sizeof(buf), "m%s",
1573 gdb_fmt_thread_id(s, s->query_cpu,
1574 thread_id, sizeof(thread_id)));
aliguori880a7572008-11-18 20:30:24 +00001575 put_packet(s, buf);
Luc Michel7cf48f62019-01-07 15:23:46 +00001576 s->query_cpu = gdb_next_attached_cpu(s, s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001577 } else
1578 put_packet(s, "l");
1579 break;
1580 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001581 if (read_thread_id(p + 16, &p, &pid, &tid) == GDB_READ_THREAD_ERR) {
1582 put_packet(s, "E22");
1583 break;
1584 }
1585 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001586 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001587 cpu_synchronize_state(cpu);
Luc Michel7cf48f62019-01-07 15:23:46 +00001588
1589 if (s->multiprocess && (s->process_num > 1)) {
1590 /* Print the CPU model and name in multiprocess mode */
1591 ObjectClass *oc = object_get_class(OBJECT(cpu));
1592 const char *cpu_model = object_class_get_name(oc);
1593 char *cpu_name =
1594 object_get_canonical_path_component(OBJECT(cpu));
1595 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
1596 "%s %s [%s]", cpu_model, cpu_name,
1597 cpu->halted ? "halted " : "running");
1598 g_free(cpu_name);
1599 } else {
1600 /* memtohex() doubles the required space */
1601 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
1602 "CPU#%d [%s]", cpu->cpu_index,
1603 cpu->halted ? "halted " : "running");
1604 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001605 trace_gdbstub_op_extra_info((char *)mem_buf);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001606 memtohex(buf, mem_buf, len);
1607 put_packet(s, buf);
1608 }
aliguori880a7572008-11-18 20:30:24 +00001609 break;
edgar_igl60897d32008-05-09 08:25:14 +00001610 }
blueswir10b8a9882009-03-07 10:51:36 +00001611#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001612 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001613 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001614
blueswir1363a37d2008-08-21 17:58:08 +00001615 snprintf(buf, sizeof(buf),
1616 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1617 ";Bss=" TARGET_ABI_FMT_lx,
1618 ts->info->code_offset,
1619 ts->info->data_offset,
1620 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001621 put_packet(s, buf);
1622 break;
1623 }
blueswir10b8a9882009-03-07 10:51:36 +00001624#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001625 else if (strncmp(p, "Rcmd,", 5) == 0) {
1626 int len = strlen(p + 5);
1627
1628 if ((len % 2) != 0) {
1629 put_packet(s, "E01");
1630 break;
1631 }
aliguori8a34a0f2009-03-05 23:01:55 +00001632 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001633 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001634 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001635 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001636 put_packet(s, "OK");
1637 break;
1638 }
blueswir10b8a9882009-03-07 10:51:36 +00001639#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001640 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001641 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001642 cc = CPU_GET_CLASS(first_cpu);
1643 if (cc->gdb_core_xml_file != NULL) {
1644 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1645 }
pbrook56aebc82008-10-11 17:55:29 +00001646 put_packet(s, buf);
1647 break;
1648 }
pbrook56aebc82008-10-11 17:55:29 +00001649 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1650 const char *xml;
1651 target_ulong total_len;
1652
Andreas Färber5b24c642013-07-07 15:08:22 +02001653 cc = CPU_GET_CLASS(first_cpu);
1654 if (cc->gdb_core_xml_file == NULL) {
1655 goto unknown_command;
1656 }
1657
Andreas Färber5b50e792013-06-29 04:18:45 +02001658 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001659 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001660 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001661 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001662 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001663 put_packet(s, buf);
1664 break;
1665 }
1666
1667 if (*p == ':')
1668 p++;
1669 addr = strtoul(p, (char **)&p, 16);
1670 if (*p == ',')
1671 p++;
1672 len = strtoul(p, (char **)&p, 16);
1673
1674 total_len = strlen(xml);
1675 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001676 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001677 put_packet(s, buf);
1678 break;
1679 }
1680 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1681 len = (MAX_PACKET_LENGTH - 5) / 2;
1682 if (len < total_len - addr) {
1683 buf[0] = 'm';
1684 len = memtox(buf + 1, xml + addr, len);
1685 } else {
1686 buf[0] = 'l';
1687 len = memtox(buf + 1, xml + addr, total_len - addr);
1688 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001689 put_packet_binary(s, buf, len + 1, true);
pbrook56aebc82008-10-11 17:55:29 +00001690 break;
1691 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001692 if (is_query_packet(p, "Attached", ':')) {
1693 put_packet(s, GDB_ATTACHED);
1694 break;
1695 }
pbrook56aebc82008-10-11 17:55:29 +00001696 /* Unrecognised 'q' command. */
1697 goto unknown_command;
1698
bellard858693c2004-03-31 18:52:07 +00001699 default:
pbrook56aebc82008-10-11 17:55:29 +00001700 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001701 /* put empty packet */
1702 buf[0] = '\0';
1703 put_packet(s, buf);
1704 break;
1705 }
1706 return RS_IDLE;
1707}
1708
Andreas Färber64f6b342013-05-27 02:06:09 +02001709void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001710{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001711 gdbserver_state->c_cpu = cpu;
1712 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001713}
1714
bellard1fddef42005-04-17 19:16:13 +00001715#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001716static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001717{
aliguori880a7572008-11-18 20:30:24 +00001718 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001719 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001720 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001721 const char *type;
bellard858693c2004-03-31 18:52:07 +00001722 int ret;
1723
Meador Ingecdb432b2012-03-15 17:49:45 +00001724 if (running || s->state == RS_INACTIVE) {
1725 return;
1726 }
1727 /* Is there a GDB syscall waiting to be sent? */
1728 if (s->current_syscall_cb) {
1729 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001730 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001731 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001732 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001733 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001734 if (cpu->watchpoint_hit) {
1735 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001736 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001737 type = "r";
1738 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001739 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001740 type = "a";
1741 break;
1742 default:
1743 type = "";
1744 break;
1745 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001746 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
1747 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00001748 snprintf(buf, sizeof(buf),
1749 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Alex Bennéed2a6c852017-07-12 11:52:14 +01001750 GDB_SIGNAL_TRAP, cpu_gdb_index(cpu), type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001751 (target_ulong)cpu->watchpoint_hit->vaddr);
1752 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001753 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05001754 } else {
1755 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00001756 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001757 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001758 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001759 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001760 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05001761 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00001762 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001763 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001764 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05001765 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01001766 ret = GDB_SIGNAL_QUIT;
1767 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001768 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001769 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001770 ret = GDB_SIGNAL_IO;
1771 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001772 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05001773 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01001774 ret = GDB_SIGNAL_ALRM;
1775 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001776 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001777 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001778 ret = GDB_SIGNAL_ABRT;
1779 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001780 case RUN_STATE_SAVE_VM:
1781 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001782 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001783 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001784 ret = GDB_SIGNAL_XCPU;
1785 break;
1786 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05001787 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01001788 ret = GDB_SIGNAL_UNKNOWN;
1789 break;
bellardbbeb7b52006-04-23 18:42:15 +00001790 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001791 gdb_set_stop_cpu(cpu);
Alex Bennéed2a6c852017-07-12 11:52:14 +01001792 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_gdb_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001793
1794send_packet:
bellard858693c2004-03-31 18:52:07 +00001795 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001796
1797 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001798 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001799}
bellard1fddef42005-04-17 19:16:13 +00001800#endif
bellard858693c2004-03-31 18:52:07 +00001801
pbrooka2d1eba2007-01-28 03:10:55 +00001802/* Send a gdb syscall request.
1803 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001804 %x - target_ulong argument printed in hex.
1805 %lx - 64-bit argument printed in hex.
1806 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001807void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001808{
pbrooka2d1eba2007-01-28 03:10:55 +00001809 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001810 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001811 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001812 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001813 GDBState *s;
1814
aliguori880a7572008-11-18 20:30:24 +00001815 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001816 if (!s)
1817 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001818 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001819#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001820 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001821#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001822 p = s->syscall_buf;
1823 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001824 *(p++) = 'F';
1825 while (*fmt) {
1826 if (*fmt == '%') {
1827 fmt++;
1828 switch (*fmt++) {
1829 case 'x':
1830 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001831 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001832 break;
pbrooka87295e2007-05-26 15:09:38 +00001833 case 'l':
1834 if (*(fmt++) != 'x')
1835 goto bad_format;
1836 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001837 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001838 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001839 case 's':
1840 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001841 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001842 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001843 break;
1844 default:
pbrooka87295e2007-05-26 15:09:38 +00001845 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001846 error_report("gdbstub: Bad syscall format string '%s'",
1847 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001848 break;
1849 }
1850 } else {
1851 *(p++) = *(fmt++);
1852 }
1853 }
pbrook8a93e022007-08-06 13:19:15 +00001854 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001855#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001856 put_packet(s, s->syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01001857 /* Return control to gdb for it to process the syscall request.
1858 * Since the protocol requires that gdb hands control back to us
1859 * using a "here are the results" F packet, we don't need to check
1860 * gdb_handlesig's return value (which is the signal to deliver if
1861 * execution was resumed via a continue packet).
1862 */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001863 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001864#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001865 /* In this case wait to send the syscall packet until notification that
1866 the CPU has stopped. This must be done because if the packet is sent
1867 now the reply from the syscall request could be received while the CPU
1868 is still in the running state, which can cause packets to be dropped
1869 and state transition 'T' packets to be sent while the syscall is still
1870 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001871 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001872#endif
1873}
1874
Peter Maydell19239b32015-09-07 10:39:27 +01001875void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1876{
1877 va_list va;
1878
1879 va_start(va, fmt);
1880 gdb_do_syscallv(cb, fmt, va);
1881 va_end(va);
1882}
1883
bellard6a00d602005-11-21 23:25:50 +00001884static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001885{
ths60fe76f2007-12-16 03:02:09 +00001886 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001887
bellard1fddef42005-04-17 19:16:13 +00001888#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001889 if (s->last_packet_len) {
1890 /* Waiting for a response to the last packet. If we see the start
1891 of a new command then abandon the previous response. */
1892 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05001893 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00001894 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01001895 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05001896 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01001897 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05001898 trace_gdbstub_io_got_unexpected((uint8_t)ch);
pbrook4046d912007-01-28 01:53:16 +00001899 }
Alex Bennée118e2262017-07-12 11:52:13 +01001900
pbrook4046d912007-01-28 01:53:16 +00001901 if (ch == '+' || ch == '$')
1902 s->last_packet_len = 0;
1903 if (ch != '$')
1904 return;
1905 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001906 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001907 /* when the CPU is running, we cannot do anything except stop
1908 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001909 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001910 } else
bellard1fddef42005-04-17 19:16:13 +00001911#endif
bellard41625032005-04-24 10:07:11 +00001912 {
bellard858693c2004-03-31 18:52:07 +00001913 switch(s->state) {
1914 case RS_IDLE:
1915 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04001916 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00001917 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04001918 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00001919 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04001920 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05001921 trace_gdbstub_err_garbage((uint8_t)ch);
bellard4c3a88a2003-07-26 12:06:08 +00001922 }
1923 break;
bellard858693c2004-03-31 18:52:07 +00001924 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04001925 if (ch == '}') {
1926 /* start escape sequence */
1927 s->state = RS_GETLINE_ESC;
1928 s->line_sum += ch;
1929 } else if (ch == '*') {
1930 /* start run length encoding sequence */
1931 s->state = RS_GETLINE_RLE;
1932 s->line_sum += ch;
1933 } else if (ch == '#') {
1934 /* end of command, start of checksum*/
1935 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00001936 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001937 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00001938 s->state = RS_IDLE;
1939 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001940 /* unescaped command character */
1941 s->line_buf[s->line_buf_index++] = ch;
1942 s->line_sum += ch;
1943 }
1944 break;
1945 case RS_GETLINE_ESC:
1946 if (ch == '#') {
1947 /* unexpected end of command in escape sequence */
1948 s->state = RS_CHKSUM1;
1949 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1950 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05001951 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04001952 s->state = RS_IDLE;
1953 } else {
1954 /* parse escaped character and leave escape state */
1955 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
1956 s->line_sum += ch;
1957 s->state = RS_GETLINE;
1958 }
1959 break;
1960 case RS_GETLINE_RLE:
1961 if (ch < ' ') {
1962 /* invalid RLE count encoding */
Doug Gale5c9522b2017-12-02 20:30:37 -05001963 trace_gdbstub_err_invalid_repeat((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001964 s->state = RS_GETLINE;
1965 } else {
1966 /* decode repeat length */
1967 int repeat = (unsigned char)ch - ' ' + 3;
1968 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
1969 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05001970 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04001971 s->state = RS_IDLE;
1972 } else if (s->line_buf_index < 1) {
1973 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05001974 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04001975 s->state = RS_GETLINE;
1976 } else {
1977 /* repeat the last character */
1978 memset(s->line_buf + s->line_buf_index,
1979 s->line_buf[s->line_buf_index - 1], repeat);
1980 s->line_buf_index += repeat;
1981 s->line_sum += ch;
1982 s->state = RS_GETLINE;
1983 }
bellard858693c2004-03-31 18:52:07 +00001984 }
1985 break;
1986 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04001987 /* get high hex digit of checksum */
1988 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001989 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001990 s->state = RS_GETLINE;
1991 break;
1992 }
bellard858693c2004-03-31 18:52:07 +00001993 s->line_buf[s->line_buf_index] = '\0';
1994 s->line_csum = fromhex(ch) << 4;
1995 s->state = RS_CHKSUM2;
1996 break;
1997 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04001998 /* get low hex digit of checksum */
1999 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002000 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002001 s->state = RS_GETLINE;
2002 break;
bellard858693c2004-03-31 18:52:07 +00002003 }
Doug Gale4bf43122017-05-01 12:22:10 -04002004 s->line_csum |= fromhex(ch);
2005
2006 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002007 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002008 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002009 reply = '-';
2010 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002011 s->state = RS_IDLE;
2012 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002013 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002014 reply = '+';
2015 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002016 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002017 }
bellardb4608c02003-06-27 17:34:32 +00002018 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002019 default:
2020 abort();
bellardb4608c02003-06-27 17:34:32 +00002021 }
2022 }
bellard858693c2004-03-31 18:52:07 +00002023}
2024
Paul Brook0e1c9c52010-06-16 13:03:51 +01002025/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002026void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002027{
2028 GDBState *s;
2029 char buf[4];
2030
2031 s = gdbserver_state;
2032 if (!s) {
2033 return;
2034 }
2035#ifdef CONFIG_USER_ONLY
2036 if (gdbserver_fd < 0 || s->fd < 0) {
2037 return;
2038 }
2039#endif
2040
Doug Gale5c9522b2017-12-02 20:30:37 -05002041 trace_gdbstub_op_exiting((uint8_t)code);
2042
Paul Brook0e1c9c52010-06-16 13:03:51 +01002043 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2044 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002045
2046#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002047 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002048#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002049}
2050
Luc Michel8f468632019-01-07 15:23:45 +00002051/*
2052 * Create the process that will contain all the "orphan" CPUs (that are not
2053 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2054 * be attachable and thus will be invisible to the user.
2055 */
2056static void create_default_process(GDBState *s)
2057{
2058 GDBProcess *process;
2059 int max_pid = 0;
2060
2061 if (s->process_num) {
2062 max_pid = s->processes[s->process_num - 1].pid;
2063 }
2064
2065 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2066 process = &s->processes[s->process_num - 1];
2067
2068 /* We need an available PID slot for this process */
2069 assert(max_pid < UINT32_MAX);
2070
2071 process->pid = max_pid + 1;
2072 process->attached = false;
2073}
2074
bellard1fddef42005-04-17 19:16:13 +00002075#ifdef CONFIG_USER_ONLY
2076int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02002077gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00002078{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002079 GDBState *s;
2080 char buf[256];
2081 int n;
bellard1fddef42005-04-17 19:16:13 +00002082
Andreas Färber5ca666c2013-06-24 19:20:57 +02002083 s = gdbserver_state;
2084 if (gdbserver_fd < 0 || s->fd < 0) {
2085 return sig;
bellard1fddef42005-04-17 19:16:13 +00002086 }
2087
Andreas Färber5ca666c2013-06-24 19:20:57 +02002088 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002089 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002090 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00002091
Andreas Färber5ca666c2013-06-24 19:20:57 +02002092 if (sig != 0) {
2093 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
2094 put_packet(s, buf);
2095 }
2096 /* put_packet() might have detected that the peer terminated the
2097 connection. */
2098 if (s->fd < 0) {
2099 return sig;
2100 }
2101
2102 sig = 0;
2103 s->state = RS_IDLE;
2104 s->running_state = 0;
2105 while (s->running_state == 0) {
2106 n = read(s->fd, buf, 256);
2107 if (n > 0) {
2108 int i;
2109
2110 for (i = 0; i < n; i++) {
2111 gdb_read_byte(s, buf[i]);
2112 }
Peter Wu5819e3e2016-06-05 16:35:48 +02002113 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02002114 /* XXX: Connection closed. Should probably wait for another
2115 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02002116 if (n == 0) {
2117 close(s->fd);
2118 }
2119 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02002120 return sig;
bellard1fddef42005-04-17 19:16:13 +00002121 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02002122 }
2123 sig = s->signal;
2124 s->signal = 0;
2125 return sig;
bellard1fddef42005-04-17 19:16:13 +00002126}
bellarde9009672005-04-26 20:42:36 +00002127
aurel32ca587a82008-12-18 22:44:13 +00002128/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002129void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00002130{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002131 GDBState *s;
2132 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00002133
Andreas Färber5ca666c2013-06-24 19:20:57 +02002134 s = gdbserver_state;
2135 if (gdbserver_fd < 0 || s->fd < 0) {
2136 return;
2137 }
aurel32ca587a82008-12-18 22:44:13 +00002138
Andreas Färber5ca666c2013-06-24 19:20:57 +02002139 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
2140 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00002141}
bellard1fddef42005-04-17 19:16:13 +00002142
Peter Maydell2f652222018-05-14 18:30:44 +01002143static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00002144{
2145 GDBState *s;
2146 struct sockaddr_in sockaddr;
2147 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09002148 int fd;
bellard858693c2004-03-31 18:52:07 +00002149
2150 for(;;) {
2151 len = sizeof(sockaddr);
2152 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2153 if (fd < 0 && errno != EINTR) {
2154 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01002155 return false;
bellard858693c2004-03-31 18:52:07 +00002156 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01002157 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002158 break;
2159 }
2160 }
2161
2162 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01002163 if (socket_set_nodelay(fd)) {
2164 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03002165 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01002166 return false;
2167 }
ths3b46e622007-09-17 08:09:54 +00002168
Anthony Liguori7267c092011-08-20 22:09:37 -05002169 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002170 s->c_cpu = first_cpu;
2171 s->g_cpu = first_cpu;
Luc Michel8f468632019-01-07 15:23:45 +00002172 create_default_process(s);
bellard858693c2004-03-31 18:52:07 +00002173 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02002174 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00002175
aliguori880a7572008-11-18 20:30:24 +00002176 gdbserver_state = s;
Peter Maydell2f652222018-05-14 18:30:44 +01002177 return true;
bellard858693c2004-03-31 18:52:07 +00002178}
2179
2180static int gdbserver_open(int port)
2181{
2182 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002183 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00002184
2185 fd = socket(PF_INET, SOCK_STREAM, 0);
2186 if (fd < 0) {
2187 perror("socket");
2188 return -1;
2189 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01002190 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002191
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002192 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00002193
2194 sockaddr.sin_family = AF_INET;
2195 sockaddr.sin_port = htons(port);
2196 sockaddr.sin_addr.s_addr = 0;
2197 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2198 if (ret < 0) {
2199 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00002200 close(fd);
bellard858693c2004-03-31 18:52:07 +00002201 return -1;
2202 }
Peter Wu96165b92016-05-04 11:32:17 +02002203 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00002204 if (ret < 0) {
2205 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00002206 close(fd);
bellard858693c2004-03-31 18:52:07 +00002207 return -1;
2208 }
bellard858693c2004-03-31 18:52:07 +00002209 return fd;
2210}
2211
2212int gdbserver_start(int port)
2213{
2214 gdbserver_fd = gdbserver_open(port);
2215 if (gdbserver_fd < 0)
2216 return -1;
2217 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01002218 if (!gdb_accept()) {
2219 close(gdbserver_fd);
2220 gdbserver_fd = -1;
2221 return -1;
2222 }
bellardb4608c02003-06-27 17:34:32 +00002223 return 0;
2224}
aurel322b1319c2008-12-18 22:44:04 +00002225
2226/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07002227void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00002228{
2229 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02002230
2231 if (gdbserver_fd < 0 || s->fd < 0) {
2232 return;
2233 }
aurel322b1319c2008-12-18 22:44:04 +00002234 close(s->fd);
2235 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02002236 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02002237 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00002238}
pbrook4046d912007-01-28 01:53:16 +00002239#else
thsaa1f17c2007-07-11 22:48:58 +00002240static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00002241{
pbrook56aebc82008-10-11 17:55:29 +00002242 /* We can handle an arbitrarily large amount of data.
2243 Pick the maximum packet size, which is as good as anything. */
2244 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00002245}
2246
thsaa1f17c2007-07-11 22:48:58 +00002247static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00002248{
pbrook4046d912007-01-28 01:53:16 +00002249 int i;
2250
2251 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00002252 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00002253 }
2254}
2255
2256static void gdb_chr_event(void *opaque, int event)
2257{
2258 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05302259 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002260 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02002261 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00002262 break;
2263 default:
2264 break;
2265 }
2266}
2267
aliguori8a34a0f2009-03-05 23:01:55 +00002268static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2269{
2270 char buf[MAX_PACKET_LENGTH];
2271
2272 buf[0] = 'O';
2273 if (len > (MAX_PACKET_LENGTH/2) - 1)
2274 len = (MAX_PACKET_LENGTH/2) - 1;
2275 memtohex(buf + 1, (uint8_t *)msg, len);
2276 put_packet(s, buf);
2277}
2278
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002279static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00002280{
2281 const char *p = (const char *)buf;
2282 int max_sz;
2283
2284 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2285 for (;;) {
2286 if (len <= max_sz) {
2287 gdb_monitor_output(gdbserver_state, p, len);
2288 break;
2289 }
2290 gdb_monitor_output(gdbserver_state, p, max_sz);
2291 p += max_sz;
2292 len -= max_sz;
2293 }
2294 return len;
2295}
2296
aliguori59030a82009-04-05 18:43:41 +00002297#ifndef _WIN32
2298static void gdb_sigterm_handler(int signal)
2299{
Luiz Capitulino13548692011-07-29 15:36:43 -03002300 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002301 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002302 }
aliguori59030a82009-04-05 18:43:41 +00002303}
2304#endif
2305
Marc-André Lureau777357d2016-12-07 18:39:10 +03002306static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
2307 bool *be_opened, Error **errp)
2308{
2309 *be_opened = false;
2310}
2311
2312static void char_gdb_class_init(ObjectClass *oc, void *data)
2313{
2314 ChardevClass *cc = CHARDEV_CLASS(oc);
2315
2316 cc->internal = true;
2317 cc->open = gdb_monitor_open;
2318 cc->chr_write = gdb_monitor_write;
2319}
2320
2321#define TYPE_CHARDEV_GDB "chardev-gdb"
2322
2323static const TypeInfo char_gdb_type_info = {
2324 .name = TYPE_CHARDEV_GDB,
2325 .parent = TYPE_CHARDEV,
2326 .class_init = char_gdb_class_init,
2327};
2328
Luc Michel8f468632019-01-07 15:23:45 +00002329static int find_cpu_clusters(Object *child, void *opaque)
2330{
2331 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
2332 GDBState *s = (GDBState *) opaque;
2333 CPUClusterState *cluster = CPU_CLUSTER(child);
2334 GDBProcess *process;
2335
2336 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2337
2338 process = &s->processes[s->process_num - 1];
2339
2340 /*
2341 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2342 * runtime, we enforce here that the machine does not use a cluster ID
2343 * that would lead to PID 0.
2344 */
2345 assert(cluster->cluster_id != UINT32_MAX);
2346 process->pid = cluster->cluster_id + 1;
2347 process->attached = false;
2348
2349 return 0;
2350 }
2351
2352 return object_child_foreach(child, find_cpu_clusters, opaque);
2353}
2354
2355static int pid_order(const void *a, const void *b)
2356{
2357 GDBProcess *pa = (GDBProcess *) a;
2358 GDBProcess *pb = (GDBProcess *) b;
2359
2360 if (pa->pid < pb->pid) {
2361 return -1;
2362 } else if (pa->pid > pb->pid) {
2363 return 1;
2364 } else {
2365 return 0;
2366 }
2367}
2368
2369static void create_processes(GDBState *s)
2370{
2371 object_child_foreach(object_get_root(), find_cpu_clusters, s);
2372
2373 if (s->processes) {
2374 /* Sort by PID */
2375 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
2376 }
2377
2378 create_default_process(s);
2379}
2380
2381static void cleanup_processes(GDBState *s)
2382{
2383 g_free(s->processes);
2384 s->process_num = 0;
2385 s->processes = NULL;
2386}
2387
aliguori59030a82009-04-05 18:43:41 +00002388int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00002389{
Doug Gale5c9522b2017-12-02 20:30:37 -05002390 trace_gdbstub_op_start(device);
2391
pbrook4046d912007-01-28 01:53:16 +00002392 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00002393 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002394 Chardev *chr = NULL;
2395 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00002396
Ziyue Yang508b4ec2017-01-18 16:02:41 +08002397 if (!first_cpu) {
2398 error_report("gdbstub: meaningless to attach gdb to a "
2399 "machine without any CPU.");
2400 return -1;
2401 }
2402
aliguori59030a82009-04-05 18:43:41 +00002403 if (!device)
2404 return -1;
2405 if (strcmp(device, "none") != 0) {
2406 if (strstart(device, "tcp:", NULL)) {
2407 /* enforce required TCP attributes */
2408 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2409 "%s,nowait,nodelay,server", device);
2410 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00002411 }
aliguori59030a82009-04-05 18:43:41 +00002412#ifndef _WIN32
2413 else if (strcmp(device, "stdio") == 0) {
2414 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00002415
aliguori59030a82009-04-05 18:43:41 +00002416 memset(&act, 0, sizeof(act));
2417 act.sa_handler = gdb_sigterm_handler;
2418 sigaction(SIGINT, &act, NULL);
2419 }
2420#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02002421 /*
2422 * FIXME: it's a bit weird to allow using a mux chardev here
2423 * and implicitly setup a monitor. We may want to break this.
2424 */
2425 chr = qemu_chr_new_noreplay("gdb", device, true);
aliguori36556b22009-03-28 18:05:53 +00002426 if (!chr)
2427 return -1;
pbrookcfc34752007-02-22 01:48:01 +00002428 }
2429
aliguori36556b22009-03-28 18:05:53 +00002430 s = gdbserver_state;
2431 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002432 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00002433 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002434
aliguori36556b22009-03-28 18:05:53 +00002435 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2436
2437 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03002438 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
2439 NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00002440 monitor_init(mon_chr, 0);
2441 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002442 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00002443 mon_chr = s->mon_chr;
Luc Michel8f468632019-01-07 15:23:45 +00002444 cleanup_processes(s);
aliguori36556b22009-03-28 18:05:53 +00002445 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002446 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002447 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002448 s->c_cpu = first_cpu;
2449 s->g_cpu = first_cpu;
Luc Michel8f468632019-01-07 15:23:45 +00002450
2451 create_processes(s);
2452
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002453 if (chr) {
2454 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002455 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Anton Nefedov81517ba2017-07-06 15:08:49 +03002456 gdb_chr_event, NULL, NULL, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002457 }
aliguori36556b22009-03-28 18:05:53 +00002458 s->state = chr ? RS_IDLE : RS_INACTIVE;
2459 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002460 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002461
pbrook4046d912007-01-28 01:53:16 +00002462 return 0;
2463}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002464
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01002465void gdbserver_cleanup(void)
2466{
2467 if (gdbserver_state) {
2468 put_packet(gdbserver_state, "W00");
2469 }
2470}
2471
Marc-André Lureau777357d2016-12-07 18:39:10 +03002472static void register_types(void)
2473{
2474 type_register_static(&char_gdb_type_info);
2475}
2476
2477type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002478#endif