blob: d4cc6ecf99b557f4fc8518fb05b6de6f45fe6a4d [file] [log] [blame]
bellardb4608c02003-06-27 17:34:32 +00001/*
2 * gdb server stub
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard34751872005-07-02 14:31:34 +00004 * Copyright (c) 2003-2005 Fabrice Bellard
bellardb4608c02003-06-27 17:34:32 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
bellardb4608c02003-06-27 17:34:32 +000018 */
Peter Maydelld38ea872016-01-29 17:50:05 +000019#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010020#include "qapi/error.h"
Ziyue Yang508b4ec2017-01-18 16:02:41 +080021#include "qemu/error-report.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020022#include "qemu/cutils.h"
Doug Gale5c9522b2017-12-02 20:30:37 -050023#include "trace-root.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020024#ifdef CONFIG_USER_ONLY
bellard1fddef42005-04-17 19:16:13 +000025#include "qemu.h"
26#else
Paolo Bonzini83c90892012-12-17 18:19:49 +010027#include "monitor/monitor.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040028#include "chardev/char.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040029#include "chardev/char-fe.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010030#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010031#include "exec/gdbstub.h"
Luc Michel8f468632019-01-07 15:23:45 +000032#include "hw/cpu/cluster.h"
bellard1fddef42005-04-17 19:16:13 +000033#endif
bellard67b915a2004-03-31 23:37:16 +000034
pbrook56aebc82008-10-11 17:55:29 +000035#define MAX_PACKET_LENGTH 4096
36
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010038#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/kvm.h"
Leon Alraecfe67ce2015-06-19 14:17:45 +010040#include "exec/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010041#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000042
Jan Kiszkaa3919382015-02-07 09:38:44 +010043#ifdef CONFIG_USER_ONLY
44#define GDB_ATTACHED "0"
45#else
46#define GDB_ATTACHED "1"
47#endif
48
Andreas Färberf3659ee2013-06-27 19:09:09 +020049static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
50 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020051{
Andreas Färberf3659ee2013-06-27 19:09:09 +020052 CPUClass *cc = CPU_GET_CLASS(cpu);
53
54 if (cc->memory_rw_debug) {
55 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
56 }
57 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020058}
aurel32ca587a82008-12-18 22:44:13 +000059
Alex Bennéed2a6c852017-07-12 11:52:14 +010060/* Return the GDB index for a given vCPU state.
61 *
62 * For user mode this is simply the thread id. In system mode GDB
63 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
64 */
65static inline int cpu_gdb_index(CPUState *cpu)
66{
67#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010068 TaskState *ts = (TaskState *) cpu->opaque;
69 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010070#else
71 return cpu->cpu_index + 1;
72#endif
73}
74
aurel32ca587a82008-12-18 22:44:13 +000075enum {
76 GDB_SIGNAL_0 = 0,
77 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010078 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000079 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010080 GDB_SIGNAL_ABRT = 6,
81 GDB_SIGNAL_ALRM = 14,
82 GDB_SIGNAL_IO = 23,
83 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000084 GDB_SIGNAL_UNKNOWN = 143
85};
86
87#ifdef CONFIG_USER_ONLY
88
89/* Map target signal numbers to GDB protocol signal numbers and vice
90 * versa. For user emulation's currently supported systems, we can
91 * assume most signals are defined.
92 */
93
94static int gdb_signal_table[] = {
95 0,
96 TARGET_SIGHUP,
97 TARGET_SIGINT,
98 TARGET_SIGQUIT,
99 TARGET_SIGILL,
100 TARGET_SIGTRAP,
101 TARGET_SIGABRT,
102 -1, /* SIGEMT */
103 TARGET_SIGFPE,
104 TARGET_SIGKILL,
105 TARGET_SIGBUS,
106 TARGET_SIGSEGV,
107 TARGET_SIGSYS,
108 TARGET_SIGPIPE,
109 TARGET_SIGALRM,
110 TARGET_SIGTERM,
111 TARGET_SIGURG,
112 TARGET_SIGSTOP,
113 TARGET_SIGTSTP,
114 TARGET_SIGCONT,
115 TARGET_SIGCHLD,
116 TARGET_SIGTTIN,
117 TARGET_SIGTTOU,
118 TARGET_SIGIO,
119 TARGET_SIGXCPU,
120 TARGET_SIGXFSZ,
121 TARGET_SIGVTALRM,
122 TARGET_SIGPROF,
123 TARGET_SIGWINCH,
124 -1, /* SIGLOST */
125 TARGET_SIGUSR1,
126 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000127#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000128 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000129#else
130 -1,
131#endif
aurel32ca587a82008-12-18 22:44:13 +0000132 -1, /* SIGPOLL */
133 -1,
134 -1,
135 -1,
136 -1,
137 -1,
138 -1,
139 -1,
140 -1,
141 -1,
142 -1,
143 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000144#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000145 __SIGRTMIN + 1,
146 __SIGRTMIN + 2,
147 __SIGRTMIN + 3,
148 __SIGRTMIN + 4,
149 __SIGRTMIN + 5,
150 __SIGRTMIN + 6,
151 __SIGRTMIN + 7,
152 __SIGRTMIN + 8,
153 __SIGRTMIN + 9,
154 __SIGRTMIN + 10,
155 __SIGRTMIN + 11,
156 __SIGRTMIN + 12,
157 __SIGRTMIN + 13,
158 __SIGRTMIN + 14,
159 __SIGRTMIN + 15,
160 __SIGRTMIN + 16,
161 __SIGRTMIN + 17,
162 __SIGRTMIN + 18,
163 __SIGRTMIN + 19,
164 __SIGRTMIN + 20,
165 __SIGRTMIN + 21,
166 __SIGRTMIN + 22,
167 __SIGRTMIN + 23,
168 __SIGRTMIN + 24,
169 __SIGRTMIN + 25,
170 __SIGRTMIN + 26,
171 __SIGRTMIN + 27,
172 __SIGRTMIN + 28,
173 __SIGRTMIN + 29,
174 __SIGRTMIN + 30,
175 __SIGRTMIN + 31,
176 -1, /* SIGCANCEL */
177 __SIGRTMIN,
178 __SIGRTMIN + 32,
179 __SIGRTMIN + 33,
180 __SIGRTMIN + 34,
181 __SIGRTMIN + 35,
182 __SIGRTMIN + 36,
183 __SIGRTMIN + 37,
184 __SIGRTMIN + 38,
185 __SIGRTMIN + 39,
186 __SIGRTMIN + 40,
187 __SIGRTMIN + 41,
188 __SIGRTMIN + 42,
189 __SIGRTMIN + 43,
190 __SIGRTMIN + 44,
191 __SIGRTMIN + 45,
192 __SIGRTMIN + 46,
193 __SIGRTMIN + 47,
194 __SIGRTMIN + 48,
195 __SIGRTMIN + 49,
196 __SIGRTMIN + 50,
197 __SIGRTMIN + 51,
198 __SIGRTMIN + 52,
199 __SIGRTMIN + 53,
200 __SIGRTMIN + 54,
201 __SIGRTMIN + 55,
202 __SIGRTMIN + 56,
203 __SIGRTMIN + 57,
204 __SIGRTMIN + 58,
205 __SIGRTMIN + 59,
206 __SIGRTMIN + 60,
207 __SIGRTMIN + 61,
208 __SIGRTMIN + 62,
209 __SIGRTMIN + 63,
210 __SIGRTMIN + 64,
211 __SIGRTMIN + 65,
212 __SIGRTMIN + 66,
213 __SIGRTMIN + 67,
214 __SIGRTMIN + 68,
215 __SIGRTMIN + 69,
216 __SIGRTMIN + 70,
217 __SIGRTMIN + 71,
218 __SIGRTMIN + 72,
219 __SIGRTMIN + 73,
220 __SIGRTMIN + 74,
221 __SIGRTMIN + 75,
222 __SIGRTMIN + 76,
223 __SIGRTMIN + 77,
224 __SIGRTMIN + 78,
225 __SIGRTMIN + 79,
226 __SIGRTMIN + 80,
227 __SIGRTMIN + 81,
228 __SIGRTMIN + 82,
229 __SIGRTMIN + 83,
230 __SIGRTMIN + 84,
231 __SIGRTMIN + 85,
232 __SIGRTMIN + 86,
233 __SIGRTMIN + 87,
234 __SIGRTMIN + 88,
235 __SIGRTMIN + 89,
236 __SIGRTMIN + 90,
237 __SIGRTMIN + 91,
238 __SIGRTMIN + 92,
239 __SIGRTMIN + 93,
240 __SIGRTMIN + 94,
241 __SIGRTMIN + 95,
242 -1, /* SIGINFO */
243 -1, /* UNKNOWN */
244 -1, /* DEFAULT */
245 -1,
246 -1,
247 -1,
248 -1,
249 -1,
250 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000251#endif
aurel32ca587a82008-12-18 22:44:13 +0000252};
bellard8f447cc2006-06-14 15:21:14 +0000253#else
aurel32ca587a82008-12-18 22:44:13 +0000254/* In system mode we only need SIGINT and SIGTRAP; other signals
255 are not yet supported. */
256
257enum {
258 TARGET_SIGINT = 2,
259 TARGET_SIGTRAP = 5
260};
261
262static int gdb_signal_table[] = {
263 -1,
264 -1,
265 TARGET_SIGINT,
266 -1,
267 -1,
268 TARGET_SIGTRAP
269};
bellard8f447cc2006-06-14 15:21:14 +0000270#endif
bellardb4608c02003-06-27 17:34:32 +0000271
aurel32ca587a82008-12-18 22:44:13 +0000272#ifdef CONFIG_USER_ONLY
273static int target_signal_to_gdb (int sig)
274{
275 int i;
276 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
277 if (gdb_signal_table[i] == sig)
278 return i;
279 return GDB_SIGNAL_UNKNOWN;
280}
281#endif
282
283static int gdb_signal_to_target (int sig)
284{
285 if (sig < ARRAY_SIZE (gdb_signal_table))
286 return gdb_signal_table[sig];
287 else
288 return -1;
289}
290
pbrook56aebc82008-10-11 17:55:29 +0000291typedef struct GDBRegisterState {
292 int base_reg;
293 int num_regs;
294 gdb_reg_cb get_reg;
295 gdb_reg_cb set_reg;
296 const char *xml;
297 struct GDBRegisterState *next;
298} GDBRegisterState;
299
Luc Michel8f468632019-01-07 15:23:45 +0000300typedef struct GDBProcess {
301 uint32_t pid;
302 bool attached;
Luc Michelc145eea2019-01-07 15:23:46 +0000303
304 char target_xml[1024];
Luc Michel8f468632019-01-07 15:23:45 +0000305} GDBProcess;
306
bellard858693c2004-03-31 18:52:07 +0000307enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000308 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000309 RS_IDLE,
310 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400311 RS_GETLINE_ESC,
312 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000313 RS_CHKSUM1,
314 RS_CHKSUM2,
315};
bellard858693c2004-03-31 18:52:07 +0000316typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200317 CPUState *c_cpu; /* current CPU for step/continue ops */
318 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200319 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000320 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000321 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000322 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400323 int line_sum; /* running checksum */
324 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000325 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000326 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000327 int signal;
bellard41625032005-04-24 10:07:11 +0000328#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000329 int fd;
bellard41625032005-04-24 10:07:11 +0000330 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000331#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300332 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300333 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000334#endif
Luc Michel8f468632019-01-07 15:23:45 +0000335 bool multiprocess;
336 GDBProcess *processes;
337 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000338 char syscall_buf[256];
339 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000340} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000341
edgar_igl60897d32008-05-09 08:25:14 +0000342/* By default use no IRQs and no timers while single stepping so as to
343 * make single stepping like an ICE HW step.
344 */
345static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
346
aliguori880a7572008-11-18 20:30:24 +0000347static GDBState *gdbserver_state;
348
Andreas Färber5b50e792013-06-29 04:18:45 +0200349bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000350
bellard1fddef42005-04-17 19:16:13 +0000351#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000352/* XXX: This is not thread safe. Do we care? */
353static int gdbserver_fd = -1;
354
bellard858693c2004-03-31 18:52:07 +0000355static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000356{
357 uint8_t ch;
358 int ret;
359
360 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000361 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000362 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000363 if (errno == ECONNRESET)
364 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200365 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000366 return -1;
367 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000368 close(s->fd);
369 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000370 return -1;
371 } else {
372 break;
373 }
374 }
375 return ch;
376}
pbrook4046d912007-01-28 01:53:16 +0000377#endif
bellardb4608c02003-06-27 17:34:32 +0000378
blueswir1654efcf2009-04-18 07:29:59 +0000379static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000380 GDB_SYS_UNKNOWN,
381 GDB_SYS_ENABLED,
382 GDB_SYS_DISABLED,
383} gdb_syscall_mode;
384
Liviu Ionescua38bb072014-12-11 12:07:48 +0000385/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000386int use_gdb_syscalls(void)
387{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100388 SemihostingTarget target = semihosting_get_target();
389 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000390 /* -semihosting-config target=native */
391 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100392 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000393 /* -semihosting-config target=gdb */
394 return true;
395 }
396
397 /* -semihosting-config target=auto */
398 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000399 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000400 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
401 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000402 }
403 return gdb_syscall_mode == GDB_SYS_ENABLED;
404}
405
edgar_iglba70a622008-03-14 06:10:42 +0000406/* Resume execution. */
407static inline void gdb_continue(GDBState *s)
408{
Doug Gale5c9522b2017-12-02 20:30:37 -0500409
edgar_iglba70a622008-03-14 06:10:42 +0000410#ifdef CONFIG_USER_ONLY
411 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500412 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000413#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200414 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500415 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200416 vm_start();
417 }
edgar_iglba70a622008-03-14 06:10:42 +0000418#endif
419}
420
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100421/*
422 * Resume execution, per CPU actions. For user-mode emulation it's
423 * equivalent to gdb_continue.
424 */
425static int gdb_continue_partial(GDBState *s, char *newstates)
426{
427 CPUState *cpu;
428 int res = 0;
429#ifdef CONFIG_USER_ONLY
430 /*
431 * This is not exactly accurate, but it's an improvement compared to the
432 * previous situation, where only one CPU would be single-stepped.
433 */
434 CPU_FOREACH(cpu) {
435 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500436 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100437 cpu_single_step(cpu, sstep_flags);
438 }
439 }
440 s->running_state = 1;
441#else
442 int flag = 0;
443
444 if (!runstate_needs_reset()) {
445 if (vm_prepare_start()) {
446 return 0;
447 }
448
449 CPU_FOREACH(cpu) {
450 switch (newstates[cpu->cpu_index]) {
451 case 0:
452 case 1:
453 break; /* nothing to do here */
454 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500455 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100456 cpu_single_step(cpu, sstep_flags);
457 cpu_resume(cpu);
458 flag = 1;
459 break;
460 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500461 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100462 cpu_resume(cpu);
463 flag = 1;
464 break;
465 default:
466 res = -1;
467 break;
468 }
469 }
470 }
471 if (flag) {
472 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
473 }
474#endif
475 return res;
476}
477
bellard858693c2004-03-31 18:52:07 +0000478static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000479{
pbrook4046d912007-01-28 01:53:16 +0000480#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000481 int ret;
482
483 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000484 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000485 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200486 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000487 return;
488 } else {
489 buf += ret;
490 len -= ret;
491 }
492 }
pbrook4046d912007-01-28 01:53:16 +0000493#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100494 /* XXX this blocks entire thread. Rewrite to use
495 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300496 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000497#endif
bellardb4608c02003-06-27 17:34:32 +0000498}
499
500static inline int fromhex(int v)
501{
502 if (v >= '0' && v <= '9')
503 return v - '0';
504 else if (v >= 'A' && v <= 'F')
505 return v - 'A' + 10;
506 else if (v >= 'a' && v <= 'f')
507 return v - 'a' + 10;
508 else
509 return 0;
510}
511
512static inline int tohex(int v)
513{
514 if (v < 10)
515 return v + '0';
516 else
517 return v - 10 + 'a';
518}
519
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300520/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000521static void memtohex(char *buf, const uint8_t *mem, int len)
522{
523 int i, c;
524 char *q;
525 q = buf;
526 for(i = 0; i < len; i++) {
527 c = mem[i];
528 *q++ = tohex(c >> 4);
529 *q++ = tohex(c & 0xf);
530 }
531 *q = '\0';
532}
533
534static void hextomem(uint8_t *mem, const char *buf, int len)
535{
536 int i;
537
538 for(i = 0; i < len; i++) {
539 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
540 buf += 2;
541 }
542}
543
Doug Gale5c9522b2017-12-02 20:30:37 -0500544static void hexdump(const char *buf, int len,
545 void (*trace_fn)(size_t ofs, char const *text))
546{
547 char line_buffer[3 * 16 + 4 + 16 + 1];
548
549 size_t i;
550 for (i = 0; i < len || (i & 0xF); ++i) {
551 size_t byte_ofs = i & 15;
552
553 if (byte_ofs == 0) {
554 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
555 line_buffer[3 * 16 + 4 + 16] = 0;
556 }
557
558 size_t col_group = (i >> 2) & 3;
559 size_t hex_col = byte_ofs * 3 + col_group;
560 size_t txt_col = 3 * 16 + 4 + byte_ofs;
561
562 if (i < len) {
563 char value = buf[i];
564
565 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
566 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
567 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
568 ? value
569 : '.';
570 }
571
572 if (byte_ofs == 0xF)
573 trace_fn(i & -16, line_buffer);
574 }
575}
576
bellardb4608c02003-06-27 17:34:32 +0000577/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500578static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000579{
pbrook56aebc82008-10-11 17:55:29 +0000580 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000581 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000582
Doug Gale5c9522b2017-12-02 20:30:37 -0500583 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
584 hexdump(buf, len, trace_gdbstub_io_binaryreply);
585 }
586
bellardb4608c02003-06-27 17:34:32 +0000587 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000588 p = s->last_packet;
589 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000590 memcpy(p, buf, len);
591 p += len;
bellardb4608c02003-06-27 17:34:32 +0000592 csum = 0;
593 for(i = 0; i < len; i++) {
594 csum += buf[i];
595 }
pbrook4046d912007-01-28 01:53:16 +0000596 *(p++) = '#';
597 *(p++) = tohex((csum >> 4) & 0xf);
598 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000599
pbrook4046d912007-01-28 01:53:16 +0000600 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000601 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000602
pbrook4046d912007-01-28 01:53:16 +0000603#ifdef CONFIG_USER_ONLY
604 i = get_char(s);
605 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000606 return -1;
pbrook4046d912007-01-28 01:53:16 +0000607 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000608 break;
pbrook4046d912007-01-28 01:53:16 +0000609#else
610 break;
611#endif
bellardb4608c02003-06-27 17:34:32 +0000612 }
613 return 0;
614}
615
pbrook56aebc82008-10-11 17:55:29 +0000616/* return -1 if error, 0 if OK */
617static int put_packet(GDBState *s, const char *buf)
618{
Doug Gale5c9522b2017-12-02 20:30:37 -0500619 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000620
Doug Gale5c9522b2017-12-02 20:30:37 -0500621 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000622}
623
pbrook56aebc82008-10-11 17:55:29 +0000624/* Encode data using the encoding for 'x' packets. */
625static int memtox(char *buf, const char *mem, int len)
626{
627 char *p = buf;
628 char c;
629
630 while (len--) {
631 c = *(mem++);
632 switch (c) {
633 case '#': case '$': case '*': case '}':
634 *(p++) = '}';
635 *(p++) = c ^ 0x20;
636 break;
637 default:
638 *(p++) = c;
639 break;
640 }
641 }
642 return p - buf;
643}
644
Luc Michel1a227332019-01-07 15:23:45 +0000645static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
646{
647#ifndef CONFIG_USER_ONLY
648 gchar *path, *name = NULL;
649 Object *obj;
650 CPUClusterState *cluster;
651 uint32_t ret;
652
653 path = object_get_canonical_path(OBJECT(cpu));
654
655 if (path == NULL) {
656 /* Return the default process' PID */
657 ret = s->processes[s->process_num - 1].pid;
658 goto out;
659 }
660
661 name = object_get_canonical_path_component(OBJECT(cpu));
662 assert(name != NULL);
663
664 /*
665 * Retrieve the CPU parent path by removing the last '/' and the CPU name
666 * from the CPU canonical path.
667 */
668 path[strlen(path) - strlen(name) - 1] = '\0';
669
670 obj = object_resolve_path_type(path, TYPE_CPU_CLUSTER, NULL);
671
672 if (obj == NULL) {
673 /* Return the default process' PID */
674 ret = s->processes[s->process_num - 1].pid;
675 goto out;
676 }
677
678 cluster = CPU_CLUSTER(obj);
679 ret = cluster->cluster_id + 1;
680
681out:
682 g_free(name);
683 g_free(path);
684
685 return ret;
686
687#else
688 /* TODO: In user mode, we should use the task state PID */
689 return s->processes[s->process_num - 1].pid;
690#endif
691}
692
Luc Michel7d8c87d2019-01-07 15:23:45 +0000693static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
694{
695 int i;
696
697 if (!pid) {
698 /* 0 means any process, we take the first one */
699 return &s->processes[0];
700 }
701
702 for (i = 0; i < s->process_num; i++) {
703 if (s->processes[i].pid == pid) {
704 return &s->processes[i];
705 }
706 }
707
708 return NULL;
709}
710
711static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
712{
713 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
714}
715
716static CPUState *find_cpu(uint32_t thread_id)
717{
718 CPUState *cpu;
719
720 CPU_FOREACH(cpu) {
721 if (cpu_gdb_index(cpu) == thread_id) {
722 return cpu;
723 }
724 }
725
726 return NULL;
727}
728
Luc Michele40e5202019-01-07 15:23:46 +0000729static CPUState *get_first_cpu_in_process(const GDBState *s,
730 GDBProcess *process)
731{
732 CPUState *cpu;
733
734 CPU_FOREACH(cpu) {
735 if (gdb_get_cpu_pid(s, cpu) == process->pid) {
736 return cpu;
737 }
738 }
739
740 return NULL;
741}
742
743static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu)
744{
745 uint32_t pid = gdb_get_cpu_pid(s, cpu);
746 cpu = CPU_NEXT(cpu);
747
748 while (cpu) {
749 if (gdb_get_cpu_pid(s, cpu) == pid) {
750 break;
751 }
752
753 cpu = CPU_NEXT(cpu);
754 }
755
756 return cpu;
757}
758
Luc Michele40e5202019-01-07 15:23:46 +0000759/* Return the cpu following @cpu, while ignoring unattached processes. */
760static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
761{
762 cpu = CPU_NEXT(cpu);
763
764 while (cpu) {
765 if (gdb_get_cpu_process(s, cpu)->attached) {
766 break;
767 }
768
769 cpu = CPU_NEXT(cpu);
770 }
771
772 return cpu;
773}
774
775/* Return the first attached cpu */
776static CPUState *gdb_first_attached_cpu(const GDBState *s)
777{
778 CPUState *cpu = first_cpu;
779 GDBProcess *process = gdb_get_cpu_process(s, cpu);
780
781 if (!process->attached) {
782 return gdb_next_attached_cpu(s, cpu);
783 }
784
785 return cpu;
786}
787
Luc Michelab65eed2019-01-29 11:46:03 +0000788static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
789{
790 GDBProcess *process;
791 CPUState *cpu;
792
793 if (!pid && !tid) {
794 /* 0 means any process/thread, we take the first attached one */
795 return gdb_first_attached_cpu(s);
796 } else if (pid && !tid) {
797 /* any thread in a specific process */
798 process = gdb_get_process(s, pid);
799
800 if (process == NULL) {
801 return NULL;
802 }
803
804 if (!process->attached) {
805 return NULL;
806 }
807
808 return get_first_cpu_in_process(s, process);
809 } else {
810 /* a specific thread */
811 cpu = find_cpu(tid);
812
813 if (cpu == NULL) {
814 return NULL;
815 }
816
817 process = gdb_get_cpu_process(s, cpu);
818
819 if (pid && process->pid != pid) {
820 return NULL;
821 }
822
823 if (!process->attached) {
824 return NULL;
825 }
826
827 return cpu;
828 }
829}
830
Luc Michelc145eea2019-01-07 15:23:46 +0000831static const char *get_feature_xml(const GDBState *s, const char *p,
832 const char **newp, GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000833{
pbrook56aebc82008-10-11 17:55:29 +0000834 size_t len;
835 int i;
836 const char *name;
Luc Michelc145eea2019-01-07 15:23:46 +0000837 CPUState *cpu = get_first_cpu_in_process(s, process);
838 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000839
840 len = 0;
841 while (p[len] && p[len] != ':')
842 len++;
843 *newp = p + len;
844
845 name = NULL;
846 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000847 char *buf = process->target_xml;
848 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000849
Luc Michelc145eea2019-01-07 15:23:46 +0000850 /* Generate the XML description for this CPU. */
851 if (!buf[0]) {
852 GDBRegisterState *r;
853
854 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100855 "<?xml version=\"1.0\"?>"
856 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
857 "<target>");
858 if (cc->gdb_arch_name) {
859 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000860 pstrcat(buf, buf_sz, "<architecture>");
861 pstrcat(buf, buf_sz, arch);
862 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100863 g_free(arch);
864 }
Luc Michelc145eea2019-01-07 15:23:46 +0000865 pstrcat(buf, buf_sz, "<xi:include href=\"");
866 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
867 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200868 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000869 pstrcat(buf, buf_sz, "<xi:include href=\"");
870 pstrcat(buf, buf_sz, r->xml);
871 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000872 }
Luc Michelc145eea2019-01-07 15:23:46 +0000873 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000874 }
Luc Michelc145eea2019-01-07 15:23:46 +0000875 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000876 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100877 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100878 char *xmlname = g_strndup(p, len);
879 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
880
881 g_free(xmlname);
882 if (xml) {
883 return xml;
884 }
885 }
pbrook56aebc82008-10-11 17:55:29 +0000886 for (i = 0; ; i++) {
887 name = xml_builtin[i][0];
888 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
889 break;
890 }
891 return name ? xml_builtin[i][1] : NULL;
892}
pbrook56aebc82008-10-11 17:55:29 +0000893
Andreas Färber385b9f02013-06-27 18:25:36 +0200894static int gdb_read_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_read_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->get_reg(env, mem_buf, reg - r->base_reg);
907 }
908 }
909 return 0;
910}
911
Andreas Färber385b9f02013-06-27 18:25:36 +0200912static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000913{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200914 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200915 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000916 GDBRegisterState *r;
917
Andreas Färbera0e372f2013-06-28 23:18:47 +0200918 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200919 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200920 }
pbrook56aebc82008-10-11 17:55:29 +0000921
Andreas Färbereac8b352013-06-28 21:11:37 +0200922 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000923 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
924 return r->set_reg(env, mem_buf, reg - r->base_reg);
925 }
926 }
927 return 0;
928}
929
930/* Register a supplemental set of CPU registers. If g_pos is nonzero it
931 specifies the first register number and these registers are included in
932 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
933 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
934 */
935
Andreas Färber22169d42013-06-28 21:27:39 +0200936void gdb_register_coprocessor(CPUState *cpu,
937 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
938 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000939{
940 GDBRegisterState *s;
941 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000942
Andreas Färbereac8b352013-06-28 21:11:37 +0200943 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000944 while (*p) {
945 /* Check for duplicates. */
946 if (strcmp((*p)->xml, xml) == 0)
947 return;
948 p = &(*p)->next;
949 }
Stefan Weil9643c252011-10-18 22:25:38 +0200950
951 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200952 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200953 s->num_regs = num_regs;
954 s->get_reg = get_reg;
955 s->set_reg = set_reg;
956 s->xml = xml;
957
pbrook56aebc82008-10-11 17:55:29 +0000958 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200959 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000960 *p = s;
961 if (g_pos) {
962 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800963 error_report("Error: Bad gdb register numbering for '%s', "
964 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200965 } else {
966 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000967 }
968 }
969}
970
aliguoria1d1bb32008-11-18 20:07:32 +0000971#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100972/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
973static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
974{
975 static const int xlat[] = {
976 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
977 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
978 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
979 };
980
981 CPUClass *cc = CPU_GET_CLASS(cpu);
982 int cputype = xlat[gdbtype];
983
984 if (cc->gdb_stop_before_watchpoint) {
985 cputype |= BP_STOP_BEFORE_ACCESS;
986 }
987 return cputype;
988}
aliguoria1d1bb32008-11-18 20:07:32 +0000989#endif
990
aliguori880a7572008-11-18 20:30:24 +0000991static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000992{
Andreas Färber182735e2013-05-29 22:29:20 +0200993 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000994 int err = 0;
995
Andreas Färber62278812013-06-27 17:12:06 +0200996 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200997 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200998 }
aliguorie22a25c2009-03-12 20:12:48 +0000999
aliguoria1d1bb32008-11-18 20:07:32 +00001000 switch (type) {
1001 case GDB_BREAKPOINT_SW:
1002 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001003 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001004 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
1005 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001006 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001007 }
aliguori880a7572008-11-18 20:30:24 +00001008 }
1009 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001010#ifndef CONFIG_USER_ONLY
1011 case GDB_WATCHPOINT_WRITE:
1012 case GDB_WATCHPOINT_READ:
1013 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001014 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001015 err = cpu_watchpoint_insert(cpu, addr, len,
1016 xlat_gdb_type(cpu, type), NULL);
1017 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001018 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +01001019 }
aliguori880a7572008-11-18 20:30:24 +00001020 }
1021 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001022#endif
1023 default:
1024 return -ENOSYS;
1025 }
1026}
1027
aliguori880a7572008-11-18 20:30:24 +00001028static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +00001029{
Andreas Färber182735e2013-05-29 22:29:20 +02001030 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001031 int err = 0;
1032
Andreas Färber62278812013-06-27 17:12:06 +02001033 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001034 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001035 }
aliguorie22a25c2009-03-12 20:12:48 +00001036
aliguoria1d1bb32008-11-18 20:07:32 +00001037 switch (type) {
1038 case GDB_BREAKPOINT_SW:
1039 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001040 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001041 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1042 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001043 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001044 }
aliguori880a7572008-11-18 20:30:24 +00001045 }
1046 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001047#ifndef CONFIG_USER_ONLY
1048 case GDB_WATCHPOINT_WRITE:
1049 case GDB_WATCHPOINT_READ:
1050 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001051 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001052 err = cpu_watchpoint_remove(cpu, addr, len,
1053 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001054 if (err)
1055 break;
1056 }
1057 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001058#endif
1059 default:
1060 return -ENOSYS;
1061 }
1062}
1063
Luc Michel546f3c62019-01-07 15:23:46 +00001064static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1065{
1066 cpu_breakpoint_remove_all(cpu, BP_GDB);
1067#ifndef CONFIG_USER_ONLY
1068 cpu_watchpoint_remove_all(cpu, BP_GDB);
1069#endif
1070}
1071
1072static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p)
1073{
1074 CPUState *cpu = get_first_cpu_in_process(s, p);
1075
1076 while (cpu) {
1077 gdb_cpu_breakpoint_remove_all(cpu);
1078 cpu = gdb_next_cpu_in_process(s, cpu);
1079 }
1080}
1081
aliguori880a7572008-11-18 20:30:24 +00001082static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001083{
Andreas Färber182735e2013-05-29 22:29:20 +02001084 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001085
aliguorie22a25c2009-03-12 20:12:48 +00001086 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001087 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001088 return;
1089 }
1090
Andreas Färberbdc44642013-06-24 23:50:24 +02001091 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001092 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001093 }
aliguoria1d1bb32008-11-18 20:07:32 +00001094}
1095
aurel32fab9d282009-04-08 21:29:37 +00001096static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1097{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001098 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001099
1100 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001101 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001102}
1103
Luc Michel1a227332019-01-07 15:23:45 +00001104static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1105 char *buf, size_t buf_size)
1106{
1107 if (s->multiprocess) {
1108 snprintf(buf, buf_size, "p%02x.%02x",
1109 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1110 } else {
1111 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1112 }
1113
1114 return buf;
1115}
1116
Luc Michel7d8c87d2019-01-07 15:23:45 +00001117typedef enum GDBThreadIdKind {
1118 GDB_ONE_THREAD = 0,
1119 GDB_ALL_THREADS, /* One process, all threads */
1120 GDB_ALL_PROCESSES,
1121 GDB_READ_THREAD_ERR
1122} GDBThreadIdKind;
1123
1124static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1125 uint32_t *pid, uint32_t *tid)
1126{
1127 unsigned long p, t;
1128 int ret;
1129
1130 if (*buf == 'p') {
1131 buf++;
1132 ret = qemu_strtoul(buf, &buf, 16, &p);
1133
1134 if (ret) {
1135 return GDB_READ_THREAD_ERR;
1136 }
1137
1138 /* Skip '.' */
1139 buf++;
1140 } else {
1141 p = 1;
1142 }
1143
1144 ret = qemu_strtoul(buf, &buf, 16, &t);
1145
1146 if (ret) {
1147 return GDB_READ_THREAD_ERR;
1148 }
1149
1150 *end_buf = buf;
1151
1152 if (p == -1) {
1153 return GDB_ALL_PROCESSES;
1154 }
1155
1156 if (pid) {
1157 *pid = p;
1158 }
1159
1160 if (t == -1) {
1161 return GDB_ALL_THREADS;
1162 }
1163
1164 if (tid) {
1165 *tid = t;
1166 }
1167
1168 return GDB_ONE_THREAD;
1169}
1170
Jan Kiszka4dabe742015-02-07 09:38:43 +01001171static int is_query_packet(const char *p, const char *query, char separator)
1172{
1173 unsigned int query_len = strlen(query);
1174
1175 return strncmp(p, query, query_len) == 0 &&
1176 (p[query_len] == '\0' || p[query_len] == separator);
1177}
1178
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001179/**
1180 * gdb_handle_vcont - Parses and handles a vCont packet.
1181 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1182 * a format error, 0 on success.
1183 */
1184static int gdb_handle_vcont(GDBState *s, const char *p)
1185{
Luc Michele40e5202019-01-07 15:23:46 +00001186 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001187 char cur_action;
1188 char *newstates;
1189 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001190 uint32_t pid, tid;
1191 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001192 CPUState *cpu;
1193#ifdef CONFIG_USER_ONLY
1194 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1195
1196 CPU_FOREACH(cpu) {
1197 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1198 }
1199#endif
1200 /* uninitialised CPUs stay 0 */
1201 newstates = g_new0(char, max_cpus);
1202
1203 /* mark valid CPUs with 1 */
1204 CPU_FOREACH(cpu) {
1205 newstates[cpu->cpu_index] = 1;
1206 }
1207
1208 /*
1209 * res keeps track of what error we are returning, with -ENOTSUP meaning
1210 * that the command is unknown or unsupported, thus returning an empty
1211 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1212 * or incorrect parameters passed.
1213 */
1214 res = 0;
1215 while (*p) {
1216 if (*p++ != ';') {
1217 res = -ENOTSUP;
1218 goto out;
1219 }
1220
1221 cur_action = *p++;
1222 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001223 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001224 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1225 if (res) {
1226 goto out;
1227 }
1228 signal = gdb_signal_to_target(tmp);
1229 } else if (cur_action != 'c' && cur_action != 's') {
1230 /* unknown/invalid/unsupported command */
1231 res = -ENOTSUP;
1232 goto out;
1233 }
Luc Michele40e5202019-01-07 15:23:46 +00001234
1235 if (*p++ != ':') {
1236 res = -ENOTSUP;
1237 goto out;
1238 }
1239
1240 switch (read_thread_id(p, &p, &pid, &tid)) {
1241 case GDB_READ_THREAD_ERR:
1242 res = -EINVAL;
1243 goto out;
1244
1245 case GDB_ALL_PROCESSES:
1246 cpu = gdb_first_attached_cpu(s);
1247 while (cpu) {
1248 if (newstates[cpu->cpu_index] == 1) {
1249 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001250 }
Luc Michele40e5202019-01-07 15:23:46 +00001251
1252 cpu = gdb_next_attached_cpu(s, cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001253 }
Luc Michele40e5202019-01-07 15:23:46 +00001254 break;
1255
1256 case GDB_ALL_THREADS:
1257 process = gdb_get_process(s, pid);
1258
1259 if (!process->attached) {
1260 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001261 goto out;
1262 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001263
Luc Michele40e5202019-01-07 15:23:46 +00001264 cpu = get_first_cpu_in_process(s, process);
1265 while (cpu) {
1266 if (newstates[cpu->cpu_index] == 1) {
1267 newstates[cpu->cpu_index] = cur_action;
1268 }
1269
1270 cpu = gdb_next_cpu_in_process(s, cpu);
1271 }
1272 break;
1273
1274 case GDB_ONE_THREAD:
1275 cpu = gdb_get_cpu(s, pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001276
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001277 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001278 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001279 res = -EINVAL;
1280 goto out;
1281 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001282
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001283 /* only use if no previous match occourred */
1284 if (newstates[cpu->cpu_index] == 1) {
1285 newstates[cpu->cpu_index] = cur_action;
1286 }
Luc Michele40e5202019-01-07 15:23:46 +00001287 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001288 }
1289 }
1290 s->signal = signal;
1291 gdb_continue_partial(s, newstates);
1292
1293out:
1294 g_free(newstates);
1295
1296 return res;
1297}
1298
aliguori880a7572008-11-18 20:30:24 +00001299static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +00001300{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001301 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001302 GDBProcess *process;
Andreas Färber5b24c642013-07-07 15:08:22 +02001303 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +00001304 const char *p;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001305 uint32_t pid, tid;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001306 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +00001307 uint8_t mem_buf[MAX_PACKET_LENGTH];
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -03001308 char buf[sizeof(mem_buf) + 1 /* trailing NUL */];
Luc Michel1a227332019-01-07 15:23:45 +00001309 char thread_id[16];
pbrook56aebc82008-10-11 17:55:29 +00001310 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001311 target_ulong addr, len;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001312 GDBThreadIdKind thread_kind;
ths3b46e622007-09-17 08:09:54 +00001313
Doug Gale5c9522b2017-12-02 20:30:37 -05001314 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01001315
bellard858693c2004-03-31 18:52:07 +00001316 p = line_buf;
1317 ch = *p++;
1318 switch(ch) {
Luc Michel53fd6552019-01-07 15:23:46 +00001319 case '!':
1320 put_packet(s, "OK");
1321 break;
bellard858693c2004-03-31 18:52:07 +00001322 case '?':
bellard1fddef42005-04-17 19:16:13 +00001323 /* TODO: Make this return the correct value for user-mode. */
Luc Michel1a227332019-01-07 15:23:45 +00001324 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1325 gdb_fmt_thread_id(s, s->c_cpu, thread_id, sizeof(thread_id)));
bellard858693c2004-03-31 18:52:07 +00001326 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001327 /* Remove all the breakpoints when this query is issued,
1328 * because gdb is doing and initial connect and the state
1329 * should be cleaned up.
1330 */
aliguori880a7572008-11-18 20:30:24 +00001331 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001332 break;
1333 case 'c':
1334 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001335 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001336 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001337 }
aurel32ca587a82008-12-18 22:44:13 +00001338 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001339 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001340 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001341 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001342 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1343 if (s->signal == -1)
1344 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001345 gdb_continue(s);
1346 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001347 case 'v':
1348 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001349 p += 4;
1350 if (*p == '?') {
1351 put_packet(s, "vCont;c;C;s;S");
1352 break;
1353 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001354
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001355 res = gdb_handle_vcont(s, p);
1356
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001357 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001358 if ((res == -EINVAL) || (res == -ERANGE)) {
1359 put_packet(s, "E22");
1360 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001361 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001362 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001363 }
1364 break;
Luc Michel3f940dc2019-01-07 15:23:46 +00001365 } else if (strncmp(p, "Attach;", 7) == 0) {
1366 unsigned long pid;
1367
1368 p += 7;
1369
1370 if (qemu_strtoul(p, &p, 16, &pid)) {
1371 put_packet(s, "E22");
1372 break;
1373 }
1374
1375 process = gdb_get_process(s, pid);
1376
1377 if (process == NULL) {
1378 put_packet(s, "E22");
1379 break;
1380 }
1381
1382 cpu = get_first_cpu_in_process(s, process);
1383
1384 if (cpu == NULL) {
1385 /* Refuse to attach an empty process */
1386 put_packet(s, "E22");
1387 break;
1388 }
1389
1390 process->attached = true;
1391
1392 s->g_cpu = cpu;
1393 s->c_cpu = cpu;
1394
1395 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1396 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)));
1397
1398 put_packet(s, buf);
1399 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001400 } else {
1401 goto unknown_command;
1402 }
edgar_igl7d03f822008-05-17 18:58:29 +00001403 case 'k':
1404 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001405 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001406 exit(0);
1407 case 'D':
1408 /* Detach packet */
Luc Michel546f3c62019-01-07 15:23:46 +00001409 pid = 1;
1410
1411 if (s->multiprocess) {
1412 unsigned long lpid;
1413 if (*p != ';') {
1414 put_packet(s, "E22");
1415 break;
1416 }
1417
1418 if (qemu_strtoul(p + 1, &p, 16, &lpid)) {
1419 put_packet(s, "E22");
1420 break;
1421 }
1422
1423 pid = lpid;
1424 }
1425
1426 process = gdb_get_process(s, pid);
1427 gdb_process_breakpoint_remove_all(s, process);
1428 process->attached = false;
1429
1430 if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
1431 s->c_cpu = gdb_first_attached_cpu(s);
1432 }
1433
1434 if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
1435 s->g_cpu = gdb_first_attached_cpu(s);
1436 }
1437
1438 if (s->c_cpu == NULL) {
1439 /* No more process attached */
1440 gdb_syscall_mode = GDB_SYS_DISABLED;
1441 gdb_continue(s);
1442 }
edgar_igl7d03f822008-05-17 18:58:29 +00001443 put_packet(s, "OK");
1444 break;
bellard858693c2004-03-31 18:52:07 +00001445 case 's':
1446 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001447 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001448 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001449 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001450 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001451 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001452 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001453 case 'F':
1454 {
1455 target_ulong ret;
1456 target_ulong err;
1457
1458 ret = strtoull(p, (char **)&p, 16);
1459 if (*p == ',') {
1460 p++;
1461 err = strtoull(p, (char **)&p, 16);
1462 } else {
1463 err = 0;
1464 }
1465 if (*p == ',')
1466 p++;
1467 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001468 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001469 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001470 s->current_syscall_cb = NULL;
1471 }
pbrooka2d1eba2007-01-28 03:10:55 +00001472 if (type == 'C') {
1473 put_packet(s, "T02");
1474 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001475 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001476 }
1477 }
1478 break;
bellard858693c2004-03-31 18:52:07 +00001479 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001480 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001481 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001482 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001483 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001484 len += reg_size;
1485 }
1486 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001487 put_packet(s, buf);
1488 break;
1489 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001490 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001491 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001492 len = strlen(p) / 2;
1493 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001494 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001495 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001496 len -= reg_size;
1497 registers += reg_size;
1498 }
bellard858693c2004-03-31 18:52:07 +00001499 put_packet(s, "OK");
1500 break;
1501 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001502 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001503 if (*p == ',')
1504 p++;
bellard9d9754a2006-06-25 15:32:37 +00001505 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001506
1507 /* memtohex() doubles the required space */
1508 if (len > MAX_PACKET_LENGTH / 2) {
1509 put_packet (s, "E22");
1510 break;
1511 }
1512
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001513 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001514 put_packet (s, "E14");
1515 } else {
1516 memtohex(buf, mem_buf, len);
1517 put_packet(s, buf);
1518 }
bellard858693c2004-03-31 18:52:07 +00001519 break;
1520 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001521 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001522 if (*p == ',')
1523 p++;
bellard9d9754a2006-06-25 15:32:37 +00001524 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001525 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001526 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001527
1528 /* hextomem() reads 2*len bytes */
1529 if (len > strlen(p) / 2) {
1530 put_packet (s, "E22");
1531 break;
1532 }
bellard858693c2004-03-31 18:52:07 +00001533 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001534 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001535 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001536 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001537 } else {
bellard858693c2004-03-31 18:52:07 +00001538 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001539 }
bellard858693c2004-03-31 18:52:07 +00001540 break;
pbrook56aebc82008-10-11 17:55:29 +00001541 case 'p':
1542 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1543 This works, but can be very slow. Anything new enough to
1544 understand XML also knows how to use this properly. */
1545 if (!gdb_has_xml)
1546 goto unknown_command;
1547 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001548 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001549 if (reg_size) {
1550 memtohex(buf, mem_buf, reg_size);
1551 put_packet(s, buf);
1552 } else {
1553 put_packet(s, "E14");
1554 }
1555 break;
1556 case 'P':
1557 if (!gdb_has_xml)
1558 goto unknown_command;
1559 addr = strtoull(p, (char **)&p, 16);
1560 if (*p == '=')
1561 p++;
1562 reg_size = strlen(p) / 2;
1563 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001564 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001565 put_packet(s, "OK");
1566 break;
bellard858693c2004-03-31 18:52:07 +00001567 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001568 case 'z':
1569 type = strtoul(p, (char **)&p, 16);
1570 if (*p == ',')
1571 p++;
bellard9d9754a2006-06-25 15:32:37 +00001572 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001573 if (*p == ',')
1574 p++;
bellard9d9754a2006-06-25 15:32:37 +00001575 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001576 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001577 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001578 else
aliguori880a7572008-11-18 20:30:24 +00001579 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001580 if (res >= 0)
1581 put_packet(s, "OK");
1582 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001583 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001584 else
1585 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001586 break;
aliguori880a7572008-11-18 20:30:24 +00001587 case 'H':
1588 type = *p++;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001589
1590 thread_kind = read_thread_id(p, &p, &pid, &tid);
1591 if (thread_kind == GDB_READ_THREAD_ERR) {
1592 put_packet(s, "E22");
1593 break;
1594 }
1595
1596 if (thread_kind != GDB_ONE_THREAD) {
aliguori880a7572008-11-18 20:30:24 +00001597 put_packet(s, "OK");
1598 break;
1599 }
Luc Michel7d8c87d2019-01-07 15:23:45 +00001600 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001601 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001602 put_packet(s, "E22");
1603 break;
1604 }
1605 switch (type) {
1606 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001607 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001608 put_packet(s, "OK");
1609 break;
1610 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001611 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001612 put_packet(s, "OK");
1613 break;
1614 default:
1615 put_packet(s, "E22");
1616 break;
1617 }
1618 break;
1619 case 'T':
Luc Michel7d8c87d2019-01-07 15:23:45 +00001620 thread_kind = read_thread_id(p, &p, &pid, &tid);
1621 if (thread_kind == GDB_READ_THREAD_ERR) {
1622 put_packet(s, "E22");
1623 break;
1624 }
1625 cpu = gdb_get_cpu(s, pid, tid);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001626
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001627 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001628 put_packet(s, "OK");
1629 } else {
aliguori880a7572008-11-18 20:30:24 +00001630 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001631 }
aliguori880a7572008-11-18 20:30:24 +00001632 break;
pbrook978efd62006-06-17 18:30:42 +00001633 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001634 case 'Q':
1635 /* parse any 'q' packets here */
1636 if (!strcmp(p,"qemu.sstepbits")) {
1637 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001638 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1639 SSTEP_ENABLE,
1640 SSTEP_NOIRQ,
1641 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001642 put_packet(s, buf);
1643 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001644 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001645 /* Display or change the sstep_flags */
1646 p += 10;
1647 if (*p != '=') {
1648 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001649 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001650 put_packet(s, buf);
1651 break;
1652 }
1653 p++;
1654 type = strtoul(p, (char **)&p, 16);
1655 sstep_flags = type;
1656 put_packet(s, "OK");
1657 break;
aliguori880a7572008-11-18 20:30:24 +00001658 } else if (strcmp(p,"C") == 0) {
Luc Michel8dbbe9a2019-01-07 15:23:46 +00001659 /*
1660 * "Current thread" remains vague in the spec, so always return
1661 * the first thread of the current process (gdb returns the
1662 * first thread).
1663 */
1664 cpu = get_first_cpu_in_process(s, gdb_get_cpu_process(s, s->g_cpu));
1665 snprintf(buf, sizeof(buf), "QC%s",
1666 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)));
1667 put_packet(s, buf);
aliguori880a7572008-11-18 20:30:24 +00001668 break;
1669 } else if (strcmp(p,"fThreadInfo") == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001670 s->query_cpu = gdb_first_attached_cpu(s);
aliguori880a7572008-11-18 20:30:24 +00001671 goto report_cpuinfo;
1672 } else if (strcmp(p,"sThreadInfo") == 0) {
1673 report_cpuinfo:
1674 if (s->query_cpu) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001675 snprintf(buf, sizeof(buf), "m%s",
1676 gdb_fmt_thread_id(s, s->query_cpu,
1677 thread_id, sizeof(thread_id)));
aliguori880a7572008-11-18 20:30:24 +00001678 put_packet(s, buf);
Luc Michel7cf48f62019-01-07 15:23:46 +00001679 s->query_cpu = gdb_next_attached_cpu(s, s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001680 } else
1681 put_packet(s, "l");
1682 break;
1683 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001684 if (read_thread_id(p + 16, &p, &pid, &tid) == GDB_READ_THREAD_ERR) {
1685 put_packet(s, "E22");
1686 break;
1687 }
1688 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001689 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001690 cpu_synchronize_state(cpu);
Luc Michel7cf48f62019-01-07 15:23:46 +00001691
1692 if (s->multiprocess && (s->process_num > 1)) {
1693 /* Print the CPU model and name in multiprocess mode */
1694 ObjectClass *oc = object_get_class(OBJECT(cpu));
1695 const char *cpu_model = object_class_get_name(oc);
1696 char *cpu_name =
1697 object_get_canonical_path_component(OBJECT(cpu));
1698 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
1699 "%s %s [%s]", cpu_model, cpu_name,
1700 cpu->halted ? "halted " : "running");
1701 g_free(cpu_name);
1702 } else {
1703 /* memtohex() doubles the required space */
1704 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
1705 "CPU#%d [%s]", cpu->cpu_index,
1706 cpu->halted ? "halted " : "running");
1707 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001708 trace_gdbstub_op_extra_info((char *)mem_buf);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001709 memtohex(buf, mem_buf, len);
1710 put_packet(s, buf);
1711 }
aliguori880a7572008-11-18 20:30:24 +00001712 break;
edgar_igl60897d32008-05-09 08:25:14 +00001713 }
blueswir10b8a9882009-03-07 10:51:36 +00001714#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001715 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001716 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001717
blueswir1363a37d2008-08-21 17:58:08 +00001718 snprintf(buf, sizeof(buf),
1719 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1720 ";Bss=" TARGET_ABI_FMT_lx,
1721 ts->info->code_offset,
1722 ts->info->data_offset,
1723 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001724 put_packet(s, buf);
1725 break;
1726 }
blueswir10b8a9882009-03-07 10:51:36 +00001727#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001728 else if (strncmp(p, "Rcmd,", 5) == 0) {
1729 int len = strlen(p + 5);
1730
1731 if ((len % 2) != 0) {
1732 put_packet(s, "E01");
1733 break;
1734 }
aliguori8a34a0f2009-03-05 23:01:55 +00001735 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001736 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001737 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001738 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001739 put_packet(s, "OK");
1740 break;
1741 }
blueswir10b8a9882009-03-07 10:51:36 +00001742#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001743 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001744 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001745 cc = CPU_GET_CLASS(first_cpu);
1746 if (cc->gdb_core_xml_file != NULL) {
1747 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1748 }
Luc Michel364fce62019-01-07 15:23:46 +00001749
1750 if (strstr(p, "multiprocess+")) {
1751 s->multiprocess = true;
1752 }
1753 pstrcat(buf, sizeof(buf), ";multiprocess+");
1754
pbrook56aebc82008-10-11 17:55:29 +00001755 put_packet(s, buf);
1756 break;
1757 }
pbrook56aebc82008-10-11 17:55:29 +00001758 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1759 const char *xml;
1760 target_ulong total_len;
1761
Luc Michelc145eea2019-01-07 15:23:46 +00001762 process = gdb_get_cpu_process(s, s->g_cpu);
1763 cc = CPU_GET_CLASS(s->g_cpu);
Andreas Färber5b24c642013-07-07 15:08:22 +02001764 if (cc->gdb_core_xml_file == NULL) {
1765 goto unknown_command;
1766 }
1767
Andreas Färber5b50e792013-06-29 04:18:45 +02001768 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001769 p += 19;
Luc Michelc145eea2019-01-07 15:23:46 +00001770 xml = get_feature_xml(s, p, &p, process);
pbrook56aebc82008-10-11 17:55:29 +00001771 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001772 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001773 put_packet(s, buf);
1774 break;
1775 }
1776
1777 if (*p == ':')
1778 p++;
1779 addr = strtoul(p, (char **)&p, 16);
1780 if (*p == ',')
1781 p++;
1782 len = strtoul(p, (char **)&p, 16);
1783
1784 total_len = strlen(xml);
1785 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001786 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001787 put_packet(s, buf);
1788 break;
1789 }
1790 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1791 len = (MAX_PACKET_LENGTH - 5) / 2;
1792 if (len < total_len - addr) {
1793 buf[0] = 'm';
1794 len = memtox(buf + 1, xml + addr, len);
1795 } else {
1796 buf[0] = 'l';
1797 len = memtox(buf + 1, xml + addr, total_len - addr);
1798 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001799 put_packet_binary(s, buf, len + 1, true);
pbrook56aebc82008-10-11 17:55:29 +00001800 break;
1801 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001802 if (is_query_packet(p, "Attached", ':')) {
1803 put_packet(s, GDB_ATTACHED);
1804 break;
1805 }
pbrook56aebc82008-10-11 17:55:29 +00001806 /* Unrecognised 'q' command. */
1807 goto unknown_command;
1808
bellard858693c2004-03-31 18:52:07 +00001809 default:
pbrook56aebc82008-10-11 17:55:29 +00001810 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001811 /* put empty packet */
1812 buf[0] = '\0';
1813 put_packet(s, buf);
1814 break;
1815 }
1816 return RS_IDLE;
1817}
1818
Andreas Färber64f6b342013-05-27 02:06:09 +02001819void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001820{
Luc Michel160d8582019-01-07 15:23:46 +00001821 GDBProcess *p = gdb_get_cpu_process(gdbserver_state, cpu);
1822
1823 if (!p->attached) {
1824 /*
1825 * Having a stop CPU corresponding to a process that is not attached
1826 * confuses GDB. So we ignore the request.
1827 */
1828 return;
1829 }
1830
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001831 gdbserver_state->c_cpu = cpu;
1832 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001833}
1834
bellard1fddef42005-04-17 19:16:13 +00001835#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001836static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001837{
aliguori880a7572008-11-18 20:30:24 +00001838 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001839 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001840 char buf[256];
Luc Michel95567c22019-01-07 15:23:46 +00001841 char thread_id[16];
aliguorid6fc1b32008-11-18 19:55:44 +00001842 const char *type;
bellard858693c2004-03-31 18:52:07 +00001843 int ret;
1844
Meador Ingecdb432b2012-03-15 17:49:45 +00001845 if (running || s->state == RS_INACTIVE) {
1846 return;
1847 }
1848 /* Is there a GDB syscall waiting to be sent? */
1849 if (s->current_syscall_cb) {
1850 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001851 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001852 }
Luc Michel95567c22019-01-07 15:23:46 +00001853
1854 if (cpu == NULL) {
1855 /* No process attached */
1856 return;
1857 }
1858
1859 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id));
1860
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001861 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001862 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001863 if (cpu->watchpoint_hit) {
1864 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001865 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001866 type = "r";
1867 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001868 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001869 type = "a";
1870 break;
1871 default:
1872 type = "";
1873 break;
1874 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001875 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
1876 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00001877 snprintf(buf, sizeof(buf),
Luc Michel95567c22019-01-07 15:23:46 +00001878 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
1879 GDB_SIGNAL_TRAP, thread_id, type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001880 (target_ulong)cpu->watchpoint_hit->vaddr);
1881 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001882 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05001883 } else {
1884 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00001885 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001886 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001887 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001888 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001889 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05001890 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00001891 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001892 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001893 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05001894 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01001895 ret = GDB_SIGNAL_QUIT;
1896 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001897 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001898 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001899 ret = GDB_SIGNAL_IO;
1900 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001901 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05001902 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01001903 ret = GDB_SIGNAL_ALRM;
1904 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001905 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001906 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001907 ret = GDB_SIGNAL_ABRT;
1908 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001909 case RUN_STATE_SAVE_VM:
1910 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001911 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001912 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001913 ret = GDB_SIGNAL_XCPU;
1914 break;
1915 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05001916 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01001917 ret = GDB_SIGNAL_UNKNOWN;
1918 break;
bellardbbeb7b52006-04-23 18:42:15 +00001919 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001920 gdb_set_stop_cpu(cpu);
Luc Michel95567c22019-01-07 15:23:46 +00001921 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
Jan Kiszka425189a2011-03-22 11:02:09 +01001922
1923send_packet:
bellard858693c2004-03-31 18:52:07 +00001924 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001925
1926 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001927 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001928}
bellard1fddef42005-04-17 19:16:13 +00001929#endif
bellard858693c2004-03-31 18:52:07 +00001930
pbrooka2d1eba2007-01-28 03:10:55 +00001931/* Send a gdb syscall request.
1932 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001933 %x - target_ulong argument printed in hex.
1934 %lx - 64-bit argument printed in hex.
1935 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001936void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001937{
pbrooka2d1eba2007-01-28 03:10:55 +00001938 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001939 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001940 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001941 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001942 GDBState *s;
1943
aliguori880a7572008-11-18 20:30:24 +00001944 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001945 if (!s)
1946 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001947 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001948#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001949 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001950#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001951 p = s->syscall_buf;
1952 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001953 *(p++) = 'F';
1954 while (*fmt) {
1955 if (*fmt == '%') {
1956 fmt++;
1957 switch (*fmt++) {
1958 case 'x':
1959 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001960 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001961 break;
pbrooka87295e2007-05-26 15:09:38 +00001962 case 'l':
1963 if (*(fmt++) != 'x')
1964 goto bad_format;
1965 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001966 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001967 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001968 case 's':
1969 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001970 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001971 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001972 break;
1973 default:
pbrooka87295e2007-05-26 15:09:38 +00001974 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001975 error_report("gdbstub: Bad syscall format string '%s'",
1976 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001977 break;
1978 }
1979 } else {
1980 *(p++) = *(fmt++);
1981 }
1982 }
pbrook8a93e022007-08-06 13:19:15 +00001983 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001984#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001985 put_packet(s, s->syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01001986 /* Return control to gdb for it to process the syscall request.
1987 * Since the protocol requires that gdb hands control back to us
1988 * using a "here are the results" F packet, we don't need to check
1989 * gdb_handlesig's return value (which is the signal to deliver if
1990 * execution was resumed via a continue packet).
1991 */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001992 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001993#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001994 /* In this case wait to send the syscall packet until notification that
1995 the CPU has stopped. This must be done because if the packet is sent
1996 now the reply from the syscall request could be received while the CPU
1997 is still in the running state, which can cause packets to be dropped
1998 and state transition 'T' packets to be sent while the syscall is still
1999 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07002000 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002001#endif
2002}
2003
Peter Maydell19239b32015-09-07 10:39:27 +01002004void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2005{
2006 va_list va;
2007
2008 va_start(va, fmt);
2009 gdb_do_syscallv(cb, fmt, va);
2010 va_end(va);
2011}
2012
bellard6a00d602005-11-21 23:25:50 +00002013static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00002014{
ths60fe76f2007-12-16 03:02:09 +00002015 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002016
bellard1fddef42005-04-17 19:16:13 +00002017#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00002018 if (s->last_packet_len) {
2019 /* Waiting for a response to the last packet. If we see the start
2020 of a new command then abandon the previous response. */
2021 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002022 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00002023 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01002024 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002025 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002026 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05002027 trace_gdbstub_io_got_unexpected((uint8_t)ch);
pbrook4046d912007-01-28 01:53:16 +00002028 }
Alex Bennée118e2262017-07-12 11:52:13 +01002029
pbrook4046d912007-01-28 01:53:16 +00002030 if (ch == '+' || ch == '$')
2031 s->last_packet_len = 0;
2032 if (ch != '$')
2033 return;
2034 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002035 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002036 /* when the CPU is running, we cannot do anything except stop
2037 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002038 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002039 } else
bellard1fddef42005-04-17 19:16:13 +00002040#endif
bellard41625032005-04-24 10:07:11 +00002041 {
bellard858693c2004-03-31 18:52:07 +00002042 switch(s->state) {
2043 case RS_IDLE:
2044 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002045 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00002046 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04002047 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00002048 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002049 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05002050 trace_gdbstub_err_garbage((uint8_t)ch);
bellard4c3a88a2003-07-26 12:06:08 +00002051 }
2052 break;
bellard858693c2004-03-31 18:52:07 +00002053 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002054 if (ch == '}') {
2055 /* start escape sequence */
2056 s->state = RS_GETLINE_ESC;
2057 s->line_sum += ch;
2058 } else if (ch == '*') {
2059 /* start run length encoding sequence */
2060 s->state = RS_GETLINE_RLE;
2061 s->line_sum += ch;
2062 } else if (ch == '#') {
2063 /* end of command, start of checksum*/
2064 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00002065 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002066 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00002067 s->state = RS_IDLE;
2068 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002069 /* unescaped command character */
2070 s->line_buf[s->line_buf_index++] = ch;
2071 s->line_sum += ch;
2072 }
2073 break;
2074 case RS_GETLINE_ESC:
2075 if (ch == '#') {
2076 /* unexpected end of command in escape sequence */
2077 s->state = RS_CHKSUM1;
2078 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2079 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002080 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002081 s->state = RS_IDLE;
2082 } else {
2083 /* parse escaped character and leave escape state */
2084 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
2085 s->line_sum += ch;
2086 s->state = RS_GETLINE;
2087 }
2088 break;
2089 case RS_GETLINE_RLE:
2090 if (ch < ' ') {
2091 /* invalid RLE count encoding */
Doug Gale5c9522b2017-12-02 20:30:37 -05002092 trace_gdbstub_err_invalid_repeat((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002093 s->state = RS_GETLINE;
2094 } else {
2095 /* decode repeat length */
2096 int repeat = (unsigned char)ch - ' ' + 3;
2097 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
2098 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002099 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002100 s->state = RS_IDLE;
2101 } else if (s->line_buf_index < 1) {
2102 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002103 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04002104 s->state = RS_GETLINE;
2105 } else {
2106 /* repeat the last character */
2107 memset(s->line_buf + s->line_buf_index,
2108 s->line_buf[s->line_buf_index - 1], repeat);
2109 s->line_buf_index += repeat;
2110 s->line_sum += ch;
2111 s->state = RS_GETLINE;
2112 }
bellard858693c2004-03-31 18:52:07 +00002113 }
2114 break;
2115 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002116 /* get high hex digit of checksum */
2117 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002118 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002119 s->state = RS_GETLINE;
2120 break;
2121 }
bellard858693c2004-03-31 18:52:07 +00002122 s->line_buf[s->line_buf_index] = '\0';
2123 s->line_csum = fromhex(ch) << 4;
2124 s->state = RS_CHKSUM2;
2125 break;
2126 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002127 /* get low hex digit of checksum */
2128 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002129 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002130 s->state = RS_GETLINE;
2131 break;
bellard858693c2004-03-31 18:52:07 +00002132 }
Doug Gale4bf43122017-05-01 12:22:10 -04002133 s->line_csum |= fromhex(ch);
2134
2135 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002136 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002137 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002138 reply = '-';
2139 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002140 s->state = RS_IDLE;
2141 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002142 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002143 reply = '+';
2144 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002145 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002146 }
bellardb4608c02003-06-27 17:34:32 +00002147 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002148 default:
2149 abort();
bellardb4608c02003-06-27 17:34:32 +00002150 }
2151 }
bellard858693c2004-03-31 18:52:07 +00002152}
2153
Paul Brook0e1c9c52010-06-16 13:03:51 +01002154/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002155void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002156{
2157 GDBState *s;
2158 char buf[4];
2159
2160 s = gdbserver_state;
2161 if (!s) {
2162 return;
2163 }
2164#ifdef CONFIG_USER_ONLY
2165 if (gdbserver_fd < 0 || s->fd < 0) {
2166 return;
2167 }
2168#endif
2169
Doug Gale5c9522b2017-12-02 20:30:37 -05002170 trace_gdbstub_op_exiting((uint8_t)code);
2171
Paul Brook0e1c9c52010-06-16 13:03:51 +01002172 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2173 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002174
2175#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002176 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002177#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002178}
2179
Luc Michel8f468632019-01-07 15:23:45 +00002180/*
2181 * Create the process that will contain all the "orphan" CPUs (that are not
2182 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2183 * be attachable and thus will be invisible to the user.
2184 */
2185static void create_default_process(GDBState *s)
2186{
2187 GDBProcess *process;
2188 int max_pid = 0;
2189
2190 if (s->process_num) {
2191 max_pid = s->processes[s->process_num - 1].pid;
2192 }
2193
2194 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2195 process = &s->processes[s->process_num - 1];
2196
2197 /* We need an available PID slot for this process */
2198 assert(max_pid < UINT32_MAX);
2199
2200 process->pid = max_pid + 1;
2201 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002202 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002203}
2204
bellard1fddef42005-04-17 19:16:13 +00002205#ifdef CONFIG_USER_ONLY
2206int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02002207gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00002208{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002209 GDBState *s;
2210 char buf[256];
2211 int n;
bellard1fddef42005-04-17 19:16:13 +00002212
Andreas Färber5ca666c2013-06-24 19:20:57 +02002213 s = gdbserver_state;
2214 if (gdbserver_fd < 0 || s->fd < 0) {
2215 return sig;
bellard1fddef42005-04-17 19:16:13 +00002216 }
2217
Andreas Färber5ca666c2013-06-24 19:20:57 +02002218 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002219 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002220 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00002221
Andreas Färber5ca666c2013-06-24 19:20:57 +02002222 if (sig != 0) {
2223 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
2224 put_packet(s, buf);
2225 }
2226 /* put_packet() might have detected that the peer terminated the
2227 connection. */
2228 if (s->fd < 0) {
2229 return sig;
2230 }
2231
2232 sig = 0;
2233 s->state = RS_IDLE;
2234 s->running_state = 0;
2235 while (s->running_state == 0) {
2236 n = read(s->fd, buf, 256);
2237 if (n > 0) {
2238 int i;
2239
2240 for (i = 0; i < n; i++) {
2241 gdb_read_byte(s, buf[i]);
2242 }
Peter Wu5819e3e2016-06-05 16:35:48 +02002243 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02002244 /* XXX: Connection closed. Should probably wait for another
2245 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02002246 if (n == 0) {
2247 close(s->fd);
2248 }
2249 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02002250 return sig;
bellard1fddef42005-04-17 19:16:13 +00002251 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02002252 }
2253 sig = s->signal;
2254 s->signal = 0;
2255 return sig;
bellard1fddef42005-04-17 19:16:13 +00002256}
bellarde9009672005-04-26 20:42:36 +00002257
aurel32ca587a82008-12-18 22:44:13 +00002258/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002259void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00002260{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002261 GDBState *s;
2262 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00002263
Andreas Färber5ca666c2013-06-24 19:20:57 +02002264 s = gdbserver_state;
2265 if (gdbserver_fd < 0 || s->fd < 0) {
2266 return;
2267 }
aurel32ca587a82008-12-18 22:44:13 +00002268
Andreas Färber5ca666c2013-06-24 19:20:57 +02002269 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
2270 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00002271}
bellard1fddef42005-04-17 19:16:13 +00002272
Peter Maydell2f652222018-05-14 18:30:44 +01002273static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00002274{
2275 GDBState *s;
2276 struct sockaddr_in sockaddr;
2277 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09002278 int fd;
bellard858693c2004-03-31 18:52:07 +00002279
2280 for(;;) {
2281 len = sizeof(sockaddr);
2282 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2283 if (fd < 0 && errno != EINTR) {
2284 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01002285 return false;
bellard858693c2004-03-31 18:52:07 +00002286 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01002287 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002288 break;
2289 }
2290 }
2291
2292 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01002293 if (socket_set_nodelay(fd)) {
2294 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03002295 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01002296 return false;
2297 }
ths3b46e622007-09-17 08:09:54 +00002298
Anthony Liguori7267c092011-08-20 22:09:37 -05002299 s = g_malloc0(sizeof(GDBState));
Luc Michel8f468632019-01-07 15:23:45 +00002300 create_default_process(s);
Luc Michel970ed902019-01-07 15:23:46 +00002301 s->processes[0].attached = true;
2302 s->c_cpu = gdb_first_attached_cpu(s);
2303 s->g_cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00002304 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02002305 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00002306
aliguori880a7572008-11-18 20:30:24 +00002307 gdbserver_state = s;
Peter Maydell2f652222018-05-14 18:30:44 +01002308 return true;
bellard858693c2004-03-31 18:52:07 +00002309}
2310
2311static int gdbserver_open(int port)
2312{
2313 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002314 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00002315
2316 fd = socket(PF_INET, SOCK_STREAM, 0);
2317 if (fd < 0) {
2318 perror("socket");
2319 return -1;
2320 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01002321 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002322
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002323 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00002324
2325 sockaddr.sin_family = AF_INET;
2326 sockaddr.sin_port = htons(port);
2327 sockaddr.sin_addr.s_addr = 0;
2328 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2329 if (ret < 0) {
2330 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00002331 close(fd);
bellard858693c2004-03-31 18:52:07 +00002332 return -1;
2333 }
Peter Wu96165b92016-05-04 11:32:17 +02002334 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00002335 if (ret < 0) {
2336 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00002337 close(fd);
bellard858693c2004-03-31 18:52:07 +00002338 return -1;
2339 }
bellard858693c2004-03-31 18:52:07 +00002340 return fd;
2341}
2342
2343int gdbserver_start(int port)
2344{
2345 gdbserver_fd = gdbserver_open(port);
2346 if (gdbserver_fd < 0)
2347 return -1;
2348 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01002349 if (!gdb_accept()) {
2350 close(gdbserver_fd);
2351 gdbserver_fd = -1;
2352 return -1;
2353 }
bellardb4608c02003-06-27 17:34:32 +00002354 return 0;
2355}
aurel322b1319c2008-12-18 22:44:04 +00002356
2357/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07002358void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00002359{
2360 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02002361
2362 if (gdbserver_fd < 0 || s->fd < 0) {
2363 return;
2364 }
aurel322b1319c2008-12-18 22:44:04 +00002365 close(s->fd);
2366 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02002367 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02002368 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00002369}
pbrook4046d912007-01-28 01:53:16 +00002370#else
thsaa1f17c2007-07-11 22:48:58 +00002371static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00002372{
pbrook56aebc82008-10-11 17:55:29 +00002373 /* We can handle an arbitrarily large amount of data.
2374 Pick the maximum packet size, which is as good as anything. */
2375 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00002376}
2377
thsaa1f17c2007-07-11 22:48:58 +00002378static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00002379{
pbrook4046d912007-01-28 01:53:16 +00002380 int i;
2381
2382 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00002383 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00002384 }
2385}
2386
2387static void gdb_chr_event(void *opaque, int event)
2388{
Luc Michel970ed902019-01-07 15:23:46 +00002389 int i;
2390 GDBState *s = (GDBState *) opaque;
2391
pbrook4046d912007-01-28 01:53:16 +00002392 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05302393 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00002394 /* Start with first process attached, others detached */
2395 for (i = 0; i < s->process_num; i++) {
2396 s->processes[i].attached = !i;
2397 }
2398
2399 s->c_cpu = gdb_first_attached_cpu(s);
2400 s->g_cpu = s->c_cpu;
2401
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002402 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02002403 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00002404 break;
2405 default:
2406 break;
2407 }
2408}
2409
aliguori8a34a0f2009-03-05 23:01:55 +00002410static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2411{
2412 char buf[MAX_PACKET_LENGTH];
2413
2414 buf[0] = 'O';
2415 if (len > (MAX_PACKET_LENGTH/2) - 1)
2416 len = (MAX_PACKET_LENGTH/2) - 1;
2417 memtohex(buf + 1, (uint8_t *)msg, len);
2418 put_packet(s, buf);
2419}
2420
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002421static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00002422{
2423 const char *p = (const char *)buf;
2424 int max_sz;
2425
2426 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2427 for (;;) {
2428 if (len <= max_sz) {
2429 gdb_monitor_output(gdbserver_state, p, len);
2430 break;
2431 }
2432 gdb_monitor_output(gdbserver_state, p, max_sz);
2433 p += max_sz;
2434 len -= max_sz;
2435 }
2436 return len;
2437}
2438
aliguori59030a82009-04-05 18:43:41 +00002439#ifndef _WIN32
2440static void gdb_sigterm_handler(int signal)
2441{
Luiz Capitulino13548692011-07-29 15:36:43 -03002442 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002443 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002444 }
aliguori59030a82009-04-05 18:43:41 +00002445}
2446#endif
2447
Marc-André Lureau777357d2016-12-07 18:39:10 +03002448static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
2449 bool *be_opened, Error **errp)
2450{
2451 *be_opened = false;
2452}
2453
2454static void char_gdb_class_init(ObjectClass *oc, void *data)
2455{
2456 ChardevClass *cc = CHARDEV_CLASS(oc);
2457
2458 cc->internal = true;
2459 cc->open = gdb_monitor_open;
2460 cc->chr_write = gdb_monitor_write;
2461}
2462
2463#define TYPE_CHARDEV_GDB "chardev-gdb"
2464
2465static const TypeInfo char_gdb_type_info = {
2466 .name = TYPE_CHARDEV_GDB,
2467 .parent = TYPE_CHARDEV,
2468 .class_init = char_gdb_class_init,
2469};
2470
Luc Michel8f468632019-01-07 15:23:45 +00002471static int find_cpu_clusters(Object *child, void *opaque)
2472{
2473 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
2474 GDBState *s = (GDBState *) opaque;
2475 CPUClusterState *cluster = CPU_CLUSTER(child);
2476 GDBProcess *process;
2477
2478 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2479
2480 process = &s->processes[s->process_num - 1];
2481
2482 /*
2483 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2484 * runtime, we enforce here that the machine does not use a cluster ID
2485 * that would lead to PID 0.
2486 */
2487 assert(cluster->cluster_id != UINT32_MAX);
2488 process->pid = cluster->cluster_id + 1;
2489 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002490 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002491
2492 return 0;
2493 }
2494
2495 return object_child_foreach(child, find_cpu_clusters, opaque);
2496}
2497
2498static int pid_order(const void *a, const void *b)
2499{
2500 GDBProcess *pa = (GDBProcess *) a;
2501 GDBProcess *pb = (GDBProcess *) b;
2502
2503 if (pa->pid < pb->pid) {
2504 return -1;
2505 } else if (pa->pid > pb->pid) {
2506 return 1;
2507 } else {
2508 return 0;
2509 }
2510}
2511
2512static void create_processes(GDBState *s)
2513{
2514 object_child_foreach(object_get_root(), find_cpu_clusters, s);
2515
2516 if (s->processes) {
2517 /* Sort by PID */
2518 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
2519 }
2520
2521 create_default_process(s);
2522}
2523
2524static void cleanup_processes(GDBState *s)
2525{
2526 g_free(s->processes);
2527 s->process_num = 0;
2528 s->processes = NULL;
2529}
2530
aliguori59030a82009-04-05 18:43:41 +00002531int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00002532{
Doug Gale5c9522b2017-12-02 20:30:37 -05002533 trace_gdbstub_op_start(device);
2534
pbrook4046d912007-01-28 01:53:16 +00002535 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00002536 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002537 Chardev *chr = NULL;
2538 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00002539
Ziyue Yang508b4ec2017-01-18 16:02:41 +08002540 if (!first_cpu) {
2541 error_report("gdbstub: meaningless to attach gdb to a "
2542 "machine without any CPU.");
2543 return -1;
2544 }
2545
aliguori59030a82009-04-05 18:43:41 +00002546 if (!device)
2547 return -1;
2548 if (strcmp(device, "none") != 0) {
2549 if (strstart(device, "tcp:", NULL)) {
2550 /* enforce required TCP attributes */
2551 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2552 "%s,nowait,nodelay,server", device);
2553 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00002554 }
aliguori59030a82009-04-05 18:43:41 +00002555#ifndef _WIN32
2556 else if (strcmp(device, "stdio") == 0) {
2557 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00002558
aliguori59030a82009-04-05 18:43:41 +00002559 memset(&act, 0, sizeof(act));
2560 act.sa_handler = gdb_sigterm_handler;
2561 sigaction(SIGINT, &act, NULL);
2562 }
2563#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02002564 /*
2565 * FIXME: it's a bit weird to allow using a mux chardev here
2566 * and implicitly setup a monitor. We may want to break this.
2567 */
2568 chr = qemu_chr_new_noreplay("gdb", device, true);
aliguori36556b22009-03-28 18:05:53 +00002569 if (!chr)
2570 return -1;
pbrookcfc34752007-02-22 01:48:01 +00002571 }
2572
aliguori36556b22009-03-28 18:05:53 +00002573 s = gdbserver_state;
2574 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002575 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00002576 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002577
aliguori36556b22009-03-28 18:05:53 +00002578 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2579
2580 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03002581 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
2582 NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00002583 monitor_init(mon_chr, 0);
2584 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002585 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00002586 mon_chr = s->mon_chr;
Luc Michel8f468632019-01-07 15:23:45 +00002587 cleanup_processes(s);
aliguori36556b22009-03-28 18:05:53 +00002588 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002589 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002590 }
Luc Michel8f468632019-01-07 15:23:45 +00002591
2592 create_processes(s);
2593
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002594 if (chr) {
2595 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002596 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Luc Michel970ed902019-01-07 15:23:46 +00002597 gdb_chr_event, NULL, s, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002598 }
aliguori36556b22009-03-28 18:05:53 +00002599 s->state = chr ? RS_IDLE : RS_INACTIVE;
2600 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002601 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002602
pbrook4046d912007-01-28 01:53:16 +00002603 return 0;
2604}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002605
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01002606void gdbserver_cleanup(void)
2607{
2608 if (gdbserver_state) {
2609 put_packet(gdbserver_state, "W00");
2610 }
2611}
2612
Marc-André Lureau777357d2016-12-07 18:39:10 +03002613static void register_types(void)
2614{
2615 type_register_static(&char_gdb_type_info);
2616}
2617
2618type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002619#endif