blob: 2c7032f53ab6f694281d07222a552f9eb9d2095f [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 Michel7d8c87d2019-01-07 15:23:45 +0000759static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
760{
761 GDBProcess *process;
762 CPUState *cpu;
763
764 if (!tid) {
765 /* 0 means any thread, we take the first one */
766 tid = 1;
767 }
768
769 cpu = find_cpu(tid);
770
771 if (cpu == NULL) {
772 return NULL;
773 }
774
775 process = gdb_get_cpu_process(s, cpu);
776
777 if (process->pid != pid) {
778 return NULL;
779 }
780
781 if (!process->attached) {
782 return NULL;
783 }
784
785 return cpu;
786}
787
Luc Michele40e5202019-01-07 15:23:46 +0000788/* Return the cpu following @cpu, while ignoring unattached processes. */
789static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
790{
791 cpu = CPU_NEXT(cpu);
792
793 while (cpu) {
794 if (gdb_get_cpu_process(s, cpu)->attached) {
795 break;
796 }
797
798 cpu = CPU_NEXT(cpu);
799 }
800
801 return cpu;
802}
803
804/* Return the first attached cpu */
805static CPUState *gdb_first_attached_cpu(const GDBState *s)
806{
807 CPUState *cpu = first_cpu;
808 GDBProcess *process = gdb_get_cpu_process(s, cpu);
809
810 if (!process->attached) {
811 return gdb_next_attached_cpu(s, cpu);
812 }
813
814 return cpu;
815}
816
Luc Michelc145eea2019-01-07 15:23:46 +0000817static const char *get_feature_xml(const GDBState *s, const char *p,
818 const char **newp, GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000819{
pbrook56aebc82008-10-11 17:55:29 +0000820 size_t len;
821 int i;
822 const char *name;
Luc Michelc145eea2019-01-07 15:23:46 +0000823 CPUState *cpu = get_first_cpu_in_process(s, process);
824 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000825
826 len = 0;
827 while (p[len] && p[len] != ':')
828 len++;
829 *newp = p + len;
830
831 name = NULL;
832 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000833 char *buf = process->target_xml;
834 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000835
Luc Michelc145eea2019-01-07 15:23:46 +0000836 /* Generate the XML description for this CPU. */
837 if (!buf[0]) {
838 GDBRegisterState *r;
839
840 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100841 "<?xml version=\"1.0\"?>"
842 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
843 "<target>");
844 if (cc->gdb_arch_name) {
845 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000846 pstrcat(buf, buf_sz, "<architecture>");
847 pstrcat(buf, buf_sz, arch);
848 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100849 g_free(arch);
850 }
Luc Michelc145eea2019-01-07 15:23:46 +0000851 pstrcat(buf, buf_sz, "<xi:include href=\"");
852 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
853 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200854 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000855 pstrcat(buf, buf_sz, "<xi:include href=\"");
856 pstrcat(buf, buf_sz, r->xml);
857 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000858 }
Luc Michelc145eea2019-01-07 15:23:46 +0000859 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000860 }
Luc Michelc145eea2019-01-07 15:23:46 +0000861 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000862 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100863 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100864 char *xmlname = g_strndup(p, len);
865 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
866
867 g_free(xmlname);
868 if (xml) {
869 return xml;
870 }
871 }
pbrook56aebc82008-10-11 17:55:29 +0000872 for (i = 0; ; i++) {
873 name = xml_builtin[i][0];
874 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
875 break;
876 }
877 return name ? xml_builtin[i][1] : NULL;
878}
pbrook56aebc82008-10-11 17:55:29 +0000879
Andreas Färber385b9f02013-06-27 18:25:36 +0200880static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000881{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200882 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200883 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000884 GDBRegisterState *r;
885
Andreas Färbera0e372f2013-06-28 23:18:47 +0200886 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200887 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200888 }
pbrook56aebc82008-10-11 17:55:29 +0000889
Andreas Färbereac8b352013-06-28 21:11:37 +0200890 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000891 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
892 return r->get_reg(env, mem_buf, reg - r->base_reg);
893 }
894 }
895 return 0;
896}
897
Andreas Färber385b9f02013-06-27 18:25:36 +0200898static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000899{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200900 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200901 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000902 GDBRegisterState *r;
903
Andreas Färbera0e372f2013-06-28 23:18:47 +0200904 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200905 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200906 }
pbrook56aebc82008-10-11 17:55:29 +0000907
Andreas Färbereac8b352013-06-28 21:11:37 +0200908 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000909 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
910 return r->set_reg(env, mem_buf, reg - r->base_reg);
911 }
912 }
913 return 0;
914}
915
916/* Register a supplemental set of CPU registers. If g_pos is nonzero it
917 specifies the first register number and these registers are included in
918 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
919 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
920 */
921
Andreas Färber22169d42013-06-28 21:27:39 +0200922void gdb_register_coprocessor(CPUState *cpu,
923 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
924 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000925{
926 GDBRegisterState *s;
927 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000928
Andreas Färbereac8b352013-06-28 21:11:37 +0200929 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000930 while (*p) {
931 /* Check for duplicates. */
932 if (strcmp((*p)->xml, xml) == 0)
933 return;
934 p = &(*p)->next;
935 }
Stefan Weil9643c252011-10-18 22:25:38 +0200936
937 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200938 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200939 s->num_regs = num_regs;
940 s->get_reg = get_reg;
941 s->set_reg = set_reg;
942 s->xml = xml;
943
pbrook56aebc82008-10-11 17:55:29 +0000944 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200945 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000946 *p = s;
947 if (g_pos) {
948 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800949 error_report("Error: Bad gdb register numbering for '%s', "
950 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200951 } else {
952 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000953 }
954 }
955}
956
aliguoria1d1bb32008-11-18 20:07:32 +0000957#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100958/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
959static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
960{
961 static const int xlat[] = {
962 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
963 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
964 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
965 };
966
967 CPUClass *cc = CPU_GET_CLASS(cpu);
968 int cputype = xlat[gdbtype];
969
970 if (cc->gdb_stop_before_watchpoint) {
971 cputype |= BP_STOP_BEFORE_ACCESS;
972 }
973 return cputype;
974}
aliguoria1d1bb32008-11-18 20:07:32 +0000975#endif
976
aliguori880a7572008-11-18 20:30:24 +0000977static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000978{
Andreas Färber182735e2013-05-29 22:29:20 +0200979 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000980 int err = 0;
981
Andreas Färber62278812013-06-27 17:12:06 +0200982 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200983 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200984 }
aliguorie22a25c2009-03-12 20:12:48 +0000985
aliguoria1d1bb32008-11-18 20:07:32 +0000986 switch (type) {
987 case GDB_BREAKPOINT_SW:
988 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200989 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200990 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
991 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000992 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200993 }
aliguori880a7572008-11-18 20:30:24 +0000994 }
995 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000996#ifndef CONFIG_USER_ONLY
997 case GDB_WATCHPOINT_WRITE:
998 case GDB_WATCHPOINT_READ:
999 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001000 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001001 err = cpu_watchpoint_insert(cpu, addr, len,
1002 xlat_gdb_type(cpu, type), NULL);
1003 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001004 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +01001005 }
aliguori880a7572008-11-18 20:30:24 +00001006 }
1007 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001008#endif
1009 default:
1010 return -ENOSYS;
1011 }
1012}
1013
aliguori880a7572008-11-18 20:30:24 +00001014static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +00001015{
Andreas Färber182735e2013-05-29 22:29:20 +02001016 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001017 int err = 0;
1018
Andreas Färber62278812013-06-27 17:12:06 +02001019 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001020 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001021 }
aliguorie22a25c2009-03-12 20:12:48 +00001022
aliguoria1d1bb32008-11-18 20:07:32 +00001023 switch (type) {
1024 case GDB_BREAKPOINT_SW:
1025 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001026 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001027 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1028 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001029 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001030 }
aliguori880a7572008-11-18 20:30:24 +00001031 }
1032 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001033#ifndef CONFIG_USER_ONLY
1034 case GDB_WATCHPOINT_WRITE:
1035 case GDB_WATCHPOINT_READ:
1036 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001037 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001038 err = cpu_watchpoint_remove(cpu, addr, len,
1039 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001040 if (err)
1041 break;
1042 }
1043 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001044#endif
1045 default:
1046 return -ENOSYS;
1047 }
1048}
1049
Luc Michel546f3c62019-01-07 15:23:46 +00001050static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1051{
1052 cpu_breakpoint_remove_all(cpu, BP_GDB);
1053#ifndef CONFIG_USER_ONLY
1054 cpu_watchpoint_remove_all(cpu, BP_GDB);
1055#endif
1056}
1057
1058static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p)
1059{
1060 CPUState *cpu = get_first_cpu_in_process(s, p);
1061
1062 while (cpu) {
1063 gdb_cpu_breakpoint_remove_all(cpu);
1064 cpu = gdb_next_cpu_in_process(s, cpu);
1065 }
1066}
1067
aliguori880a7572008-11-18 20:30:24 +00001068static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001069{
Andreas Färber182735e2013-05-29 22:29:20 +02001070 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001071
aliguorie22a25c2009-03-12 20:12:48 +00001072 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001073 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001074 return;
1075 }
1076
Andreas Färberbdc44642013-06-24 23:50:24 +02001077 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001078 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001079 }
aliguoria1d1bb32008-11-18 20:07:32 +00001080}
1081
aurel32fab9d282009-04-08 21:29:37 +00001082static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1083{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001084 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001085
1086 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001087 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001088}
1089
Luc Michel1a227332019-01-07 15:23:45 +00001090static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1091 char *buf, size_t buf_size)
1092{
1093 if (s->multiprocess) {
1094 snprintf(buf, buf_size, "p%02x.%02x",
1095 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1096 } else {
1097 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1098 }
1099
1100 return buf;
1101}
1102
Luc Michel7d8c87d2019-01-07 15:23:45 +00001103typedef enum GDBThreadIdKind {
1104 GDB_ONE_THREAD = 0,
1105 GDB_ALL_THREADS, /* One process, all threads */
1106 GDB_ALL_PROCESSES,
1107 GDB_READ_THREAD_ERR
1108} GDBThreadIdKind;
1109
1110static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1111 uint32_t *pid, uint32_t *tid)
1112{
1113 unsigned long p, t;
1114 int ret;
1115
1116 if (*buf == 'p') {
1117 buf++;
1118 ret = qemu_strtoul(buf, &buf, 16, &p);
1119
1120 if (ret) {
1121 return GDB_READ_THREAD_ERR;
1122 }
1123
1124 /* Skip '.' */
1125 buf++;
1126 } else {
1127 p = 1;
1128 }
1129
1130 ret = qemu_strtoul(buf, &buf, 16, &t);
1131
1132 if (ret) {
1133 return GDB_READ_THREAD_ERR;
1134 }
1135
1136 *end_buf = buf;
1137
1138 if (p == -1) {
1139 return GDB_ALL_PROCESSES;
1140 }
1141
1142 if (pid) {
1143 *pid = p;
1144 }
1145
1146 if (t == -1) {
1147 return GDB_ALL_THREADS;
1148 }
1149
1150 if (tid) {
1151 *tid = t;
1152 }
1153
1154 return GDB_ONE_THREAD;
1155}
1156
Jan Kiszka4dabe742015-02-07 09:38:43 +01001157static int is_query_packet(const char *p, const char *query, char separator)
1158{
1159 unsigned int query_len = strlen(query);
1160
1161 return strncmp(p, query, query_len) == 0 &&
1162 (p[query_len] == '\0' || p[query_len] == separator);
1163}
1164
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001165/**
1166 * gdb_handle_vcont - Parses and handles a vCont packet.
1167 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1168 * a format error, 0 on success.
1169 */
1170static int gdb_handle_vcont(GDBState *s, const char *p)
1171{
Luc Michele40e5202019-01-07 15:23:46 +00001172 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001173 char cur_action;
1174 char *newstates;
1175 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001176 uint32_t pid, tid;
1177 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001178 CPUState *cpu;
1179#ifdef CONFIG_USER_ONLY
1180 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1181
1182 CPU_FOREACH(cpu) {
1183 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1184 }
1185#endif
1186 /* uninitialised CPUs stay 0 */
1187 newstates = g_new0(char, max_cpus);
1188
1189 /* mark valid CPUs with 1 */
1190 CPU_FOREACH(cpu) {
1191 newstates[cpu->cpu_index] = 1;
1192 }
1193
1194 /*
1195 * res keeps track of what error we are returning, with -ENOTSUP meaning
1196 * that the command is unknown or unsupported, thus returning an empty
1197 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1198 * or incorrect parameters passed.
1199 */
1200 res = 0;
1201 while (*p) {
1202 if (*p++ != ';') {
1203 res = -ENOTSUP;
1204 goto out;
1205 }
1206
1207 cur_action = *p++;
1208 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001209 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001210 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1211 if (res) {
1212 goto out;
1213 }
1214 signal = gdb_signal_to_target(tmp);
1215 } else if (cur_action != 'c' && cur_action != 's') {
1216 /* unknown/invalid/unsupported command */
1217 res = -ENOTSUP;
1218 goto out;
1219 }
Luc Michele40e5202019-01-07 15:23:46 +00001220
1221 if (*p++ != ':') {
1222 res = -ENOTSUP;
1223 goto out;
1224 }
1225
1226 switch (read_thread_id(p, &p, &pid, &tid)) {
1227 case GDB_READ_THREAD_ERR:
1228 res = -EINVAL;
1229 goto out;
1230
1231 case GDB_ALL_PROCESSES:
1232 cpu = gdb_first_attached_cpu(s);
1233 while (cpu) {
1234 if (newstates[cpu->cpu_index] == 1) {
1235 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001236 }
Luc Michele40e5202019-01-07 15:23:46 +00001237
1238 cpu = gdb_next_attached_cpu(s, cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001239 }
Luc Michele40e5202019-01-07 15:23:46 +00001240 break;
1241
1242 case GDB_ALL_THREADS:
1243 process = gdb_get_process(s, pid);
1244
1245 if (!process->attached) {
1246 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001247 goto out;
1248 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001249
Luc Michele40e5202019-01-07 15:23:46 +00001250 cpu = get_first_cpu_in_process(s, process);
1251 while (cpu) {
1252 if (newstates[cpu->cpu_index] == 1) {
1253 newstates[cpu->cpu_index] = cur_action;
1254 }
1255
1256 cpu = gdb_next_cpu_in_process(s, cpu);
1257 }
1258 break;
1259
1260 case GDB_ONE_THREAD:
1261 cpu = gdb_get_cpu(s, pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001262
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001263 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001264 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001265 res = -EINVAL;
1266 goto out;
1267 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001268
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001269 /* only use if no previous match occourred */
1270 if (newstates[cpu->cpu_index] == 1) {
1271 newstates[cpu->cpu_index] = cur_action;
1272 }
Luc Michele40e5202019-01-07 15:23:46 +00001273 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001274 }
1275 }
1276 s->signal = signal;
1277 gdb_continue_partial(s, newstates);
1278
1279out:
1280 g_free(newstates);
1281
1282 return res;
1283}
1284
aliguori880a7572008-11-18 20:30:24 +00001285static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +00001286{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001287 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001288 GDBProcess *process;
Andreas Färber5b24c642013-07-07 15:08:22 +02001289 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +00001290 const char *p;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001291 uint32_t pid, tid;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001292 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +00001293 uint8_t mem_buf[MAX_PACKET_LENGTH];
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -03001294 char buf[sizeof(mem_buf) + 1 /* trailing NUL */];
Luc Michel1a227332019-01-07 15:23:45 +00001295 char thread_id[16];
pbrook56aebc82008-10-11 17:55:29 +00001296 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001297 target_ulong addr, len;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001298 GDBThreadIdKind thread_kind;
ths3b46e622007-09-17 08:09:54 +00001299
Doug Gale5c9522b2017-12-02 20:30:37 -05001300 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01001301
bellard858693c2004-03-31 18:52:07 +00001302 p = line_buf;
1303 ch = *p++;
1304 switch(ch) {
1305 case '?':
bellard1fddef42005-04-17 19:16:13 +00001306 /* TODO: Make this return the correct value for user-mode. */
Luc Michel1a227332019-01-07 15:23:45 +00001307 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1308 gdb_fmt_thread_id(s, s->c_cpu, thread_id, sizeof(thread_id)));
bellard858693c2004-03-31 18:52:07 +00001309 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001310 /* Remove all the breakpoints when this query is issued,
1311 * because gdb is doing and initial connect and the state
1312 * should be cleaned up.
1313 */
aliguori880a7572008-11-18 20:30:24 +00001314 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001315 break;
1316 case 'c':
1317 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001318 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001319 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001320 }
aurel32ca587a82008-12-18 22:44:13 +00001321 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001322 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001323 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001324 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001325 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1326 if (s->signal == -1)
1327 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001328 gdb_continue(s);
1329 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001330 case 'v':
1331 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001332 p += 4;
1333 if (*p == '?') {
1334 put_packet(s, "vCont;c;C;s;S");
1335 break;
1336 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001337
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001338 res = gdb_handle_vcont(s, p);
1339
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001340 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001341 if ((res == -EINVAL) || (res == -ERANGE)) {
1342 put_packet(s, "E22");
1343 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001344 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001345 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001346 }
1347 break;
1348 } else {
1349 goto unknown_command;
1350 }
edgar_igl7d03f822008-05-17 18:58:29 +00001351 case 'k':
1352 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001353 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001354 exit(0);
1355 case 'D':
1356 /* Detach packet */
Luc Michel546f3c62019-01-07 15:23:46 +00001357 pid = 1;
1358
1359 if (s->multiprocess) {
1360 unsigned long lpid;
1361 if (*p != ';') {
1362 put_packet(s, "E22");
1363 break;
1364 }
1365
1366 if (qemu_strtoul(p + 1, &p, 16, &lpid)) {
1367 put_packet(s, "E22");
1368 break;
1369 }
1370
1371 pid = lpid;
1372 }
1373
1374 process = gdb_get_process(s, pid);
1375 gdb_process_breakpoint_remove_all(s, process);
1376 process->attached = false;
1377
1378 if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
1379 s->c_cpu = gdb_first_attached_cpu(s);
1380 }
1381
1382 if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
1383 s->g_cpu = gdb_first_attached_cpu(s);
1384 }
1385
1386 if (s->c_cpu == NULL) {
1387 /* No more process attached */
1388 gdb_syscall_mode = GDB_SYS_DISABLED;
1389 gdb_continue(s);
1390 }
edgar_igl7d03f822008-05-17 18:58:29 +00001391 put_packet(s, "OK");
1392 break;
bellard858693c2004-03-31 18:52:07 +00001393 case 's':
1394 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001395 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001396 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001397 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001398 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001399 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001400 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001401 case 'F':
1402 {
1403 target_ulong ret;
1404 target_ulong err;
1405
1406 ret = strtoull(p, (char **)&p, 16);
1407 if (*p == ',') {
1408 p++;
1409 err = strtoull(p, (char **)&p, 16);
1410 } else {
1411 err = 0;
1412 }
1413 if (*p == ',')
1414 p++;
1415 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001416 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001417 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001418 s->current_syscall_cb = NULL;
1419 }
pbrooka2d1eba2007-01-28 03:10:55 +00001420 if (type == 'C') {
1421 put_packet(s, "T02");
1422 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001423 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001424 }
1425 }
1426 break;
bellard858693c2004-03-31 18:52:07 +00001427 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001428 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001429 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001430 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001431 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001432 len += reg_size;
1433 }
1434 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001435 put_packet(s, buf);
1436 break;
1437 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001438 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001439 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001440 len = strlen(p) / 2;
1441 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001442 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001443 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001444 len -= reg_size;
1445 registers += reg_size;
1446 }
bellard858693c2004-03-31 18:52:07 +00001447 put_packet(s, "OK");
1448 break;
1449 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001450 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001451 if (*p == ',')
1452 p++;
bellard9d9754a2006-06-25 15:32:37 +00001453 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001454
1455 /* memtohex() doubles the required space */
1456 if (len > MAX_PACKET_LENGTH / 2) {
1457 put_packet (s, "E22");
1458 break;
1459 }
1460
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001461 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001462 put_packet (s, "E14");
1463 } else {
1464 memtohex(buf, mem_buf, len);
1465 put_packet(s, buf);
1466 }
bellard858693c2004-03-31 18:52:07 +00001467 break;
1468 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001469 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001470 if (*p == ',')
1471 p++;
bellard9d9754a2006-06-25 15:32:37 +00001472 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001473 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001474 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001475
1476 /* hextomem() reads 2*len bytes */
1477 if (len > strlen(p) / 2) {
1478 put_packet (s, "E22");
1479 break;
1480 }
bellard858693c2004-03-31 18:52:07 +00001481 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001482 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001483 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001484 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001485 } else {
bellard858693c2004-03-31 18:52:07 +00001486 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001487 }
bellard858693c2004-03-31 18:52:07 +00001488 break;
pbrook56aebc82008-10-11 17:55:29 +00001489 case 'p':
1490 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1491 This works, but can be very slow. Anything new enough to
1492 understand XML also knows how to use this properly. */
1493 if (!gdb_has_xml)
1494 goto unknown_command;
1495 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001496 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001497 if (reg_size) {
1498 memtohex(buf, mem_buf, reg_size);
1499 put_packet(s, buf);
1500 } else {
1501 put_packet(s, "E14");
1502 }
1503 break;
1504 case 'P':
1505 if (!gdb_has_xml)
1506 goto unknown_command;
1507 addr = strtoull(p, (char **)&p, 16);
1508 if (*p == '=')
1509 p++;
1510 reg_size = strlen(p) / 2;
1511 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001512 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001513 put_packet(s, "OK");
1514 break;
bellard858693c2004-03-31 18:52:07 +00001515 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001516 case 'z':
1517 type = strtoul(p, (char **)&p, 16);
1518 if (*p == ',')
1519 p++;
bellard9d9754a2006-06-25 15:32:37 +00001520 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001521 if (*p == ',')
1522 p++;
bellard9d9754a2006-06-25 15:32:37 +00001523 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001524 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001525 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001526 else
aliguori880a7572008-11-18 20:30:24 +00001527 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001528 if (res >= 0)
1529 put_packet(s, "OK");
1530 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001531 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001532 else
1533 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001534 break;
aliguori880a7572008-11-18 20:30:24 +00001535 case 'H':
1536 type = *p++;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001537
1538 thread_kind = read_thread_id(p, &p, &pid, &tid);
1539 if (thread_kind == GDB_READ_THREAD_ERR) {
1540 put_packet(s, "E22");
1541 break;
1542 }
1543
1544 if (thread_kind != GDB_ONE_THREAD) {
aliguori880a7572008-11-18 20:30:24 +00001545 put_packet(s, "OK");
1546 break;
1547 }
Luc Michel7d8c87d2019-01-07 15:23:45 +00001548 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001549 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001550 put_packet(s, "E22");
1551 break;
1552 }
1553 switch (type) {
1554 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001555 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001556 put_packet(s, "OK");
1557 break;
1558 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001559 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001560 put_packet(s, "OK");
1561 break;
1562 default:
1563 put_packet(s, "E22");
1564 break;
1565 }
1566 break;
1567 case 'T':
Luc Michel7d8c87d2019-01-07 15:23:45 +00001568 thread_kind = read_thread_id(p, &p, &pid, &tid);
1569 if (thread_kind == GDB_READ_THREAD_ERR) {
1570 put_packet(s, "E22");
1571 break;
1572 }
1573 cpu = gdb_get_cpu(s, pid, tid);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001574
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001575 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001576 put_packet(s, "OK");
1577 } else {
aliguori880a7572008-11-18 20:30:24 +00001578 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001579 }
aliguori880a7572008-11-18 20:30:24 +00001580 break;
pbrook978efd62006-06-17 18:30:42 +00001581 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001582 case 'Q':
1583 /* parse any 'q' packets here */
1584 if (!strcmp(p,"qemu.sstepbits")) {
1585 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001586 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1587 SSTEP_ENABLE,
1588 SSTEP_NOIRQ,
1589 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001590 put_packet(s, buf);
1591 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001592 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001593 /* Display or change the sstep_flags */
1594 p += 10;
1595 if (*p != '=') {
1596 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001597 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001598 put_packet(s, buf);
1599 break;
1600 }
1601 p++;
1602 type = strtoul(p, (char **)&p, 16);
1603 sstep_flags = type;
1604 put_packet(s, "OK");
1605 break;
aliguori880a7572008-11-18 20:30:24 +00001606 } else if (strcmp(p,"C") == 0) {
Luc Michel8dbbe9a2019-01-07 15:23:46 +00001607 /*
1608 * "Current thread" remains vague in the spec, so always return
1609 * the first thread of the current process (gdb returns the
1610 * first thread).
1611 */
1612 cpu = get_first_cpu_in_process(s, gdb_get_cpu_process(s, s->g_cpu));
1613 snprintf(buf, sizeof(buf), "QC%s",
1614 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)));
1615 put_packet(s, buf);
aliguori880a7572008-11-18 20:30:24 +00001616 break;
1617 } else if (strcmp(p,"fThreadInfo") == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001618 s->query_cpu = gdb_first_attached_cpu(s);
aliguori880a7572008-11-18 20:30:24 +00001619 goto report_cpuinfo;
1620 } else if (strcmp(p,"sThreadInfo") == 0) {
1621 report_cpuinfo:
1622 if (s->query_cpu) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001623 snprintf(buf, sizeof(buf), "m%s",
1624 gdb_fmt_thread_id(s, s->query_cpu,
1625 thread_id, sizeof(thread_id)));
aliguori880a7572008-11-18 20:30:24 +00001626 put_packet(s, buf);
Luc Michel7cf48f62019-01-07 15:23:46 +00001627 s->query_cpu = gdb_next_attached_cpu(s, s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001628 } else
1629 put_packet(s, "l");
1630 break;
1631 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001632 if (read_thread_id(p + 16, &p, &pid, &tid) == GDB_READ_THREAD_ERR) {
1633 put_packet(s, "E22");
1634 break;
1635 }
1636 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001637 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001638 cpu_synchronize_state(cpu);
Luc Michel7cf48f62019-01-07 15:23:46 +00001639
1640 if (s->multiprocess && (s->process_num > 1)) {
1641 /* Print the CPU model and name in multiprocess mode */
1642 ObjectClass *oc = object_get_class(OBJECT(cpu));
1643 const char *cpu_model = object_class_get_name(oc);
1644 char *cpu_name =
1645 object_get_canonical_path_component(OBJECT(cpu));
1646 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
1647 "%s %s [%s]", cpu_model, cpu_name,
1648 cpu->halted ? "halted " : "running");
1649 g_free(cpu_name);
1650 } else {
1651 /* memtohex() doubles the required space */
1652 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
1653 "CPU#%d [%s]", cpu->cpu_index,
1654 cpu->halted ? "halted " : "running");
1655 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001656 trace_gdbstub_op_extra_info((char *)mem_buf);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001657 memtohex(buf, mem_buf, len);
1658 put_packet(s, buf);
1659 }
aliguori880a7572008-11-18 20:30:24 +00001660 break;
edgar_igl60897d32008-05-09 08:25:14 +00001661 }
blueswir10b8a9882009-03-07 10:51:36 +00001662#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001663 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001664 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001665
blueswir1363a37d2008-08-21 17:58:08 +00001666 snprintf(buf, sizeof(buf),
1667 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1668 ";Bss=" TARGET_ABI_FMT_lx,
1669 ts->info->code_offset,
1670 ts->info->data_offset,
1671 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001672 put_packet(s, buf);
1673 break;
1674 }
blueswir10b8a9882009-03-07 10:51:36 +00001675#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001676 else if (strncmp(p, "Rcmd,", 5) == 0) {
1677 int len = strlen(p + 5);
1678
1679 if ((len % 2) != 0) {
1680 put_packet(s, "E01");
1681 break;
1682 }
aliguori8a34a0f2009-03-05 23:01:55 +00001683 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001684 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001685 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001686 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001687 put_packet(s, "OK");
1688 break;
1689 }
blueswir10b8a9882009-03-07 10:51:36 +00001690#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001691 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001692 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001693 cc = CPU_GET_CLASS(first_cpu);
1694 if (cc->gdb_core_xml_file != NULL) {
1695 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1696 }
pbrook56aebc82008-10-11 17:55:29 +00001697 put_packet(s, buf);
1698 break;
1699 }
pbrook56aebc82008-10-11 17:55:29 +00001700 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1701 const char *xml;
1702 target_ulong total_len;
1703
Luc Michelc145eea2019-01-07 15:23:46 +00001704 process = gdb_get_cpu_process(s, s->g_cpu);
1705 cc = CPU_GET_CLASS(s->g_cpu);
Andreas Färber5b24c642013-07-07 15:08:22 +02001706 if (cc->gdb_core_xml_file == NULL) {
1707 goto unknown_command;
1708 }
1709
Andreas Färber5b50e792013-06-29 04:18:45 +02001710 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001711 p += 19;
Luc Michelc145eea2019-01-07 15:23:46 +00001712 xml = get_feature_xml(s, p, &p, process);
pbrook56aebc82008-10-11 17:55:29 +00001713 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001714 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001715 put_packet(s, buf);
1716 break;
1717 }
1718
1719 if (*p == ':')
1720 p++;
1721 addr = strtoul(p, (char **)&p, 16);
1722 if (*p == ',')
1723 p++;
1724 len = strtoul(p, (char **)&p, 16);
1725
1726 total_len = strlen(xml);
1727 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001728 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001729 put_packet(s, buf);
1730 break;
1731 }
1732 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1733 len = (MAX_PACKET_LENGTH - 5) / 2;
1734 if (len < total_len - addr) {
1735 buf[0] = 'm';
1736 len = memtox(buf + 1, xml + addr, len);
1737 } else {
1738 buf[0] = 'l';
1739 len = memtox(buf + 1, xml + addr, total_len - addr);
1740 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001741 put_packet_binary(s, buf, len + 1, true);
pbrook56aebc82008-10-11 17:55:29 +00001742 break;
1743 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001744 if (is_query_packet(p, "Attached", ':')) {
1745 put_packet(s, GDB_ATTACHED);
1746 break;
1747 }
pbrook56aebc82008-10-11 17:55:29 +00001748 /* Unrecognised 'q' command. */
1749 goto unknown_command;
1750
bellard858693c2004-03-31 18:52:07 +00001751 default:
pbrook56aebc82008-10-11 17:55:29 +00001752 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001753 /* put empty packet */
1754 buf[0] = '\0';
1755 put_packet(s, buf);
1756 break;
1757 }
1758 return RS_IDLE;
1759}
1760
Andreas Färber64f6b342013-05-27 02:06:09 +02001761void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001762{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001763 gdbserver_state->c_cpu = cpu;
1764 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001765}
1766
bellard1fddef42005-04-17 19:16:13 +00001767#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001768static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001769{
aliguori880a7572008-11-18 20:30:24 +00001770 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001771 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001772 char buf[256];
Luc Michel95567c22019-01-07 15:23:46 +00001773 char thread_id[16];
aliguorid6fc1b32008-11-18 19:55:44 +00001774 const char *type;
bellard858693c2004-03-31 18:52:07 +00001775 int ret;
1776
Meador Ingecdb432b2012-03-15 17:49:45 +00001777 if (running || s->state == RS_INACTIVE) {
1778 return;
1779 }
1780 /* Is there a GDB syscall waiting to be sent? */
1781 if (s->current_syscall_cb) {
1782 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001783 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001784 }
Luc Michel95567c22019-01-07 15:23:46 +00001785
1786 if (cpu == NULL) {
1787 /* No process attached */
1788 return;
1789 }
1790
1791 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id));
1792
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001793 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001794 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001795 if (cpu->watchpoint_hit) {
1796 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001797 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001798 type = "r";
1799 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001800 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001801 type = "a";
1802 break;
1803 default:
1804 type = "";
1805 break;
1806 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001807 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
1808 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00001809 snprintf(buf, sizeof(buf),
Luc Michel95567c22019-01-07 15:23:46 +00001810 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
1811 GDB_SIGNAL_TRAP, thread_id, type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001812 (target_ulong)cpu->watchpoint_hit->vaddr);
1813 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001814 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05001815 } else {
1816 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00001817 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001818 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001819 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001820 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001821 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05001822 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00001823 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001824 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001825 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05001826 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01001827 ret = GDB_SIGNAL_QUIT;
1828 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001829 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001830 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001831 ret = GDB_SIGNAL_IO;
1832 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001833 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05001834 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01001835 ret = GDB_SIGNAL_ALRM;
1836 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001837 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001838 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001839 ret = GDB_SIGNAL_ABRT;
1840 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001841 case RUN_STATE_SAVE_VM:
1842 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001843 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001844 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001845 ret = GDB_SIGNAL_XCPU;
1846 break;
1847 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05001848 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01001849 ret = GDB_SIGNAL_UNKNOWN;
1850 break;
bellardbbeb7b52006-04-23 18:42:15 +00001851 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001852 gdb_set_stop_cpu(cpu);
Luc Michel95567c22019-01-07 15:23:46 +00001853 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
Jan Kiszka425189a2011-03-22 11:02:09 +01001854
1855send_packet:
bellard858693c2004-03-31 18:52:07 +00001856 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001857
1858 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001859 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001860}
bellard1fddef42005-04-17 19:16:13 +00001861#endif
bellard858693c2004-03-31 18:52:07 +00001862
pbrooka2d1eba2007-01-28 03:10:55 +00001863/* Send a gdb syscall request.
1864 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001865 %x - target_ulong argument printed in hex.
1866 %lx - 64-bit argument printed in hex.
1867 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001868void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001869{
pbrooka2d1eba2007-01-28 03:10:55 +00001870 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001871 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001872 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001873 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001874 GDBState *s;
1875
aliguori880a7572008-11-18 20:30:24 +00001876 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001877 if (!s)
1878 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001879 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001880#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001881 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001882#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001883 p = s->syscall_buf;
1884 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001885 *(p++) = 'F';
1886 while (*fmt) {
1887 if (*fmt == '%') {
1888 fmt++;
1889 switch (*fmt++) {
1890 case 'x':
1891 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001892 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001893 break;
pbrooka87295e2007-05-26 15:09:38 +00001894 case 'l':
1895 if (*(fmt++) != 'x')
1896 goto bad_format;
1897 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001898 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001899 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001900 case 's':
1901 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001902 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001903 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001904 break;
1905 default:
pbrooka87295e2007-05-26 15:09:38 +00001906 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001907 error_report("gdbstub: Bad syscall format string '%s'",
1908 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001909 break;
1910 }
1911 } else {
1912 *(p++) = *(fmt++);
1913 }
1914 }
pbrook8a93e022007-08-06 13:19:15 +00001915 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001916#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001917 put_packet(s, s->syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01001918 /* Return control to gdb for it to process the syscall request.
1919 * Since the protocol requires that gdb hands control back to us
1920 * using a "here are the results" F packet, we don't need to check
1921 * gdb_handlesig's return value (which is the signal to deliver if
1922 * execution was resumed via a continue packet).
1923 */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001924 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001925#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001926 /* In this case wait to send the syscall packet until notification that
1927 the CPU has stopped. This must be done because if the packet is sent
1928 now the reply from the syscall request could be received while the CPU
1929 is still in the running state, which can cause packets to be dropped
1930 and state transition 'T' packets to be sent while the syscall is still
1931 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001932 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001933#endif
1934}
1935
Peter Maydell19239b32015-09-07 10:39:27 +01001936void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1937{
1938 va_list va;
1939
1940 va_start(va, fmt);
1941 gdb_do_syscallv(cb, fmt, va);
1942 va_end(va);
1943}
1944
bellard6a00d602005-11-21 23:25:50 +00001945static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001946{
ths60fe76f2007-12-16 03:02:09 +00001947 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001948
bellard1fddef42005-04-17 19:16:13 +00001949#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001950 if (s->last_packet_len) {
1951 /* Waiting for a response to the last packet. If we see the start
1952 of a new command then abandon the previous response. */
1953 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05001954 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00001955 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01001956 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05001957 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01001958 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05001959 trace_gdbstub_io_got_unexpected((uint8_t)ch);
pbrook4046d912007-01-28 01:53:16 +00001960 }
Alex Bennée118e2262017-07-12 11:52:13 +01001961
pbrook4046d912007-01-28 01:53:16 +00001962 if (ch == '+' || ch == '$')
1963 s->last_packet_len = 0;
1964 if (ch != '$')
1965 return;
1966 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001967 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001968 /* when the CPU is running, we cannot do anything except stop
1969 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001970 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001971 } else
bellard1fddef42005-04-17 19:16:13 +00001972#endif
bellard41625032005-04-24 10:07:11 +00001973 {
bellard858693c2004-03-31 18:52:07 +00001974 switch(s->state) {
1975 case RS_IDLE:
1976 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04001977 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00001978 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04001979 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00001980 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04001981 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05001982 trace_gdbstub_err_garbage((uint8_t)ch);
bellard4c3a88a2003-07-26 12:06:08 +00001983 }
1984 break;
bellard858693c2004-03-31 18:52:07 +00001985 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04001986 if (ch == '}') {
1987 /* start escape sequence */
1988 s->state = RS_GETLINE_ESC;
1989 s->line_sum += ch;
1990 } else if (ch == '*') {
1991 /* start run length encoding sequence */
1992 s->state = RS_GETLINE_RLE;
1993 s->line_sum += ch;
1994 } else if (ch == '#') {
1995 /* end of command, start of checksum*/
1996 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00001997 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001998 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00001999 s->state = RS_IDLE;
2000 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002001 /* unescaped command character */
2002 s->line_buf[s->line_buf_index++] = ch;
2003 s->line_sum += ch;
2004 }
2005 break;
2006 case RS_GETLINE_ESC:
2007 if (ch == '#') {
2008 /* unexpected end of command in escape sequence */
2009 s->state = RS_CHKSUM1;
2010 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2011 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002012 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002013 s->state = RS_IDLE;
2014 } else {
2015 /* parse escaped character and leave escape state */
2016 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
2017 s->line_sum += ch;
2018 s->state = RS_GETLINE;
2019 }
2020 break;
2021 case RS_GETLINE_RLE:
2022 if (ch < ' ') {
2023 /* invalid RLE count encoding */
Doug Gale5c9522b2017-12-02 20:30:37 -05002024 trace_gdbstub_err_invalid_repeat((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002025 s->state = RS_GETLINE;
2026 } else {
2027 /* decode repeat length */
2028 int repeat = (unsigned char)ch - ' ' + 3;
2029 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
2030 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002031 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002032 s->state = RS_IDLE;
2033 } else if (s->line_buf_index < 1) {
2034 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002035 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04002036 s->state = RS_GETLINE;
2037 } else {
2038 /* repeat the last character */
2039 memset(s->line_buf + s->line_buf_index,
2040 s->line_buf[s->line_buf_index - 1], repeat);
2041 s->line_buf_index += repeat;
2042 s->line_sum += ch;
2043 s->state = RS_GETLINE;
2044 }
bellard858693c2004-03-31 18:52:07 +00002045 }
2046 break;
2047 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002048 /* get high hex digit of checksum */
2049 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002050 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002051 s->state = RS_GETLINE;
2052 break;
2053 }
bellard858693c2004-03-31 18:52:07 +00002054 s->line_buf[s->line_buf_index] = '\0';
2055 s->line_csum = fromhex(ch) << 4;
2056 s->state = RS_CHKSUM2;
2057 break;
2058 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002059 /* get low hex digit of checksum */
2060 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002061 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002062 s->state = RS_GETLINE;
2063 break;
bellard858693c2004-03-31 18:52:07 +00002064 }
Doug Gale4bf43122017-05-01 12:22:10 -04002065 s->line_csum |= fromhex(ch);
2066
2067 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002068 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002069 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002070 reply = '-';
2071 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002072 s->state = RS_IDLE;
2073 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002074 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002075 reply = '+';
2076 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002077 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002078 }
bellardb4608c02003-06-27 17:34:32 +00002079 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002080 default:
2081 abort();
bellardb4608c02003-06-27 17:34:32 +00002082 }
2083 }
bellard858693c2004-03-31 18:52:07 +00002084}
2085
Paul Brook0e1c9c52010-06-16 13:03:51 +01002086/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002087void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002088{
2089 GDBState *s;
2090 char buf[4];
2091
2092 s = gdbserver_state;
2093 if (!s) {
2094 return;
2095 }
2096#ifdef CONFIG_USER_ONLY
2097 if (gdbserver_fd < 0 || s->fd < 0) {
2098 return;
2099 }
2100#endif
2101
Doug Gale5c9522b2017-12-02 20:30:37 -05002102 trace_gdbstub_op_exiting((uint8_t)code);
2103
Paul Brook0e1c9c52010-06-16 13:03:51 +01002104 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2105 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002106
2107#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002108 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002109#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002110}
2111
Luc Michel8f468632019-01-07 15:23:45 +00002112/*
2113 * Create the process that will contain all the "orphan" CPUs (that are not
2114 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2115 * be attachable and thus will be invisible to the user.
2116 */
2117static void create_default_process(GDBState *s)
2118{
2119 GDBProcess *process;
2120 int max_pid = 0;
2121
2122 if (s->process_num) {
2123 max_pid = s->processes[s->process_num - 1].pid;
2124 }
2125
2126 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2127 process = &s->processes[s->process_num - 1];
2128
2129 /* We need an available PID slot for this process */
2130 assert(max_pid < UINT32_MAX);
2131
2132 process->pid = max_pid + 1;
2133 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002134 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002135}
2136
bellard1fddef42005-04-17 19:16:13 +00002137#ifdef CONFIG_USER_ONLY
2138int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02002139gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00002140{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002141 GDBState *s;
2142 char buf[256];
2143 int n;
bellard1fddef42005-04-17 19:16:13 +00002144
Andreas Färber5ca666c2013-06-24 19:20:57 +02002145 s = gdbserver_state;
2146 if (gdbserver_fd < 0 || s->fd < 0) {
2147 return sig;
bellard1fddef42005-04-17 19:16:13 +00002148 }
2149
Andreas Färber5ca666c2013-06-24 19:20:57 +02002150 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002151 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002152 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00002153
Andreas Färber5ca666c2013-06-24 19:20:57 +02002154 if (sig != 0) {
2155 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
2156 put_packet(s, buf);
2157 }
2158 /* put_packet() might have detected that the peer terminated the
2159 connection. */
2160 if (s->fd < 0) {
2161 return sig;
2162 }
2163
2164 sig = 0;
2165 s->state = RS_IDLE;
2166 s->running_state = 0;
2167 while (s->running_state == 0) {
2168 n = read(s->fd, buf, 256);
2169 if (n > 0) {
2170 int i;
2171
2172 for (i = 0; i < n; i++) {
2173 gdb_read_byte(s, buf[i]);
2174 }
Peter Wu5819e3e2016-06-05 16:35:48 +02002175 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02002176 /* XXX: Connection closed. Should probably wait for another
2177 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02002178 if (n == 0) {
2179 close(s->fd);
2180 }
2181 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02002182 return sig;
bellard1fddef42005-04-17 19:16:13 +00002183 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02002184 }
2185 sig = s->signal;
2186 s->signal = 0;
2187 return sig;
bellard1fddef42005-04-17 19:16:13 +00002188}
bellarde9009672005-04-26 20:42:36 +00002189
aurel32ca587a82008-12-18 22:44:13 +00002190/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002191void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00002192{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002193 GDBState *s;
2194 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00002195
Andreas Färber5ca666c2013-06-24 19:20:57 +02002196 s = gdbserver_state;
2197 if (gdbserver_fd < 0 || s->fd < 0) {
2198 return;
2199 }
aurel32ca587a82008-12-18 22:44:13 +00002200
Andreas Färber5ca666c2013-06-24 19:20:57 +02002201 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
2202 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00002203}
bellard1fddef42005-04-17 19:16:13 +00002204
Peter Maydell2f652222018-05-14 18:30:44 +01002205static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00002206{
2207 GDBState *s;
2208 struct sockaddr_in sockaddr;
2209 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09002210 int fd;
bellard858693c2004-03-31 18:52:07 +00002211
2212 for(;;) {
2213 len = sizeof(sockaddr);
2214 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2215 if (fd < 0 && errno != EINTR) {
2216 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01002217 return false;
bellard858693c2004-03-31 18:52:07 +00002218 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01002219 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002220 break;
2221 }
2222 }
2223
2224 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01002225 if (socket_set_nodelay(fd)) {
2226 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03002227 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01002228 return false;
2229 }
ths3b46e622007-09-17 08:09:54 +00002230
Anthony Liguori7267c092011-08-20 22:09:37 -05002231 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002232 s->c_cpu = first_cpu;
2233 s->g_cpu = first_cpu;
Luc Michel8f468632019-01-07 15:23:45 +00002234 create_default_process(s);
bellard858693c2004-03-31 18:52:07 +00002235 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02002236 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00002237
aliguori880a7572008-11-18 20:30:24 +00002238 gdbserver_state = s;
Peter Maydell2f652222018-05-14 18:30:44 +01002239 return true;
bellard858693c2004-03-31 18:52:07 +00002240}
2241
2242static int gdbserver_open(int port)
2243{
2244 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002245 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00002246
2247 fd = socket(PF_INET, SOCK_STREAM, 0);
2248 if (fd < 0) {
2249 perror("socket");
2250 return -1;
2251 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01002252 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002253
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002254 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00002255
2256 sockaddr.sin_family = AF_INET;
2257 sockaddr.sin_port = htons(port);
2258 sockaddr.sin_addr.s_addr = 0;
2259 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2260 if (ret < 0) {
2261 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00002262 close(fd);
bellard858693c2004-03-31 18:52:07 +00002263 return -1;
2264 }
Peter Wu96165b92016-05-04 11:32:17 +02002265 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00002266 if (ret < 0) {
2267 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00002268 close(fd);
bellard858693c2004-03-31 18:52:07 +00002269 return -1;
2270 }
bellard858693c2004-03-31 18:52:07 +00002271 return fd;
2272}
2273
2274int gdbserver_start(int port)
2275{
2276 gdbserver_fd = gdbserver_open(port);
2277 if (gdbserver_fd < 0)
2278 return -1;
2279 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01002280 if (!gdb_accept()) {
2281 close(gdbserver_fd);
2282 gdbserver_fd = -1;
2283 return -1;
2284 }
bellardb4608c02003-06-27 17:34:32 +00002285 return 0;
2286}
aurel322b1319c2008-12-18 22:44:04 +00002287
2288/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07002289void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00002290{
2291 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02002292
2293 if (gdbserver_fd < 0 || s->fd < 0) {
2294 return;
2295 }
aurel322b1319c2008-12-18 22:44:04 +00002296 close(s->fd);
2297 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02002298 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02002299 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00002300}
pbrook4046d912007-01-28 01:53:16 +00002301#else
thsaa1f17c2007-07-11 22:48:58 +00002302static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00002303{
pbrook56aebc82008-10-11 17:55:29 +00002304 /* We can handle an arbitrarily large amount of data.
2305 Pick the maximum packet size, which is as good as anything. */
2306 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00002307}
2308
thsaa1f17c2007-07-11 22:48:58 +00002309static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00002310{
pbrook4046d912007-01-28 01:53:16 +00002311 int i;
2312
2313 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00002314 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00002315 }
2316}
2317
2318static void gdb_chr_event(void *opaque, int event)
2319{
2320 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05302321 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002322 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02002323 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00002324 break;
2325 default:
2326 break;
2327 }
2328}
2329
aliguori8a34a0f2009-03-05 23:01:55 +00002330static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2331{
2332 char buf[MAX_PACKET_LENGTH];
2333
2334 buf[0] = 'O';
2335 if (len > (MAX_PACKET_LENGTH/2) - 1)
2336 len = (MAX_PACKET_LENGTH/2) - 1;
2337 memtohex(buf + 1, (uint8_t *)msg, len);
2338 put_packet(s, buf);
2339}
2340
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002341static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00002342{
2343 const char *p = (const char *)buf;
2344 int max_sz;
2345
2346 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2347 for (;;) {
2348 if (len <= max_sz) {
2349 gdb_monitor_output(gdbserver_state, p, len);
2350 break;
2351 }
2352 gdb_monitor_output(gdbserver_state, p, max_sz);
2353 p += max_sz;
2354 len -= max_sz;
2355 }
2356 return len;
2357}
2358
aliguori59030a82009-04-05 18:43:41 +00002359#ifndef _WIN32
2360static void gdb_sigterm_handler(int signal)
2361{
Luiz Capitulino13548692011-07-29 15:36:43 -03002362 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002363 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002364 }
aliguori59030a82009-04-05 18:43:41 +00002365}
2366#endif
2367
Marc-André Lureau777357d2016-12-07 18:39:10 +03002368static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
2369 bool *be_opened, Error **errp)
2370{
2371 *be_opened = false;
2372}
2373
2374static void char_gdb_class_init(ObjectClass *oc, void *data)
2375{
2376 ChardevClass *cc = CHARDEV_CLASS(oc);
2377
2378 cc->internal = true;
2379 cc->open = gdb_monitor_open;
2380 cc->chr_write = gdb_monitor_write;
2381}
2382
2383#define TYPE_CHARDEV_GDB "chardev-gdb"
2384
2385static const TypeInfo char_gdb_type_info = {
2386 .name = TYPE_CHARDEV_GDB,
2387 .parent = TYPE_CHARDEV,
2388 .class_init = char_gdb_class_init,
2389};
2390
Luc Michel8f468632019-01-07 15:23:45 +00002391static int find_cpu_clusters(Object *child, void *opaque)
2392{
2393 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
2394 GDBState *s = (GDBState *) opaque;
2395 CPUClusterState *cluster = CPU_CLUSTER(child);
2396 GDBProcess *process;
2397
2398 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2399
2400 process = &s->processes[s->process_num - 1];
2401
2402 /*
2403 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2404 * runtime, we enforce here that the machine does not use a cluster ID
2405 * that would lead to PID 0.
2406 */
2407 assert(cluster->cluster_id != UINT32_MAX);
2408 process->pid = cluster->cluster_id + 1;
2409 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002410 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002411
2412 return 0;
2413 }
2414
2415 return object_child_foreach(child, find_cpu_clusters, opaque);
2416}
2417
2418static int pid_order(const void *a, const void *b)
2419{
2420 GDBProcess *pa = (GDBProcess *) a;
2421 GDBProcess *pb = (GDBProcess *) b;
2422
2423 if (pa->pid < pb->pid) {
2424 return -1;
2425 } else if (pa->pid > pb->pid) {
2426 return 1;
2427 } else {
2428 return 0;
2429 }
2430}
2431
2432static void create_processes(GDBState *s)
2433{
2434 object_child_foreach(object_get_root(), find_cpu_clusters, s);
2435
2436 if (s->processes) {
2437 /* Sort by PID */
2438 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
2439 }
2440
2441 create_default_process(s);
2442}
2443
2444static void cleanup_processes(GDBState *s)
2445{
2446 g_free(s->processes);
2447 s->process_num = 0;
2448 s->processes = NULL;
2449}
2450
aliguori59030a82009-04-05 18:43:41 +00002451int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00002452{
Doug Gale5c9522b2017-12-02 20:30:37 -05002453 trace_gdbstub_op_start(device);
2454
pbrook4046d912007-01-28 01:53:16 +00002455 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00002456 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002457 Chardev *chr = NULL;
2458 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00002459
Ziyue Yang508b4ec2017-01-18 16:02:41 +08002460 if (!first_cpu) {
2461 error_report("gdbstub: meaningless to attach gdb to a "
2462 "machine without any CPU.");
2463 return -1;
2464 }
2465
aliguori59030a82009-04-05 18:43:41 +00002466 if (!device)
2467 return -1;
2468 if (strcmp(device, "none") != 0) {
2469 if (strstart(device, "tcp:", NULL)) {
2470 /* enforce required TCP attributes */
2471 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2472 "%s,nowait,nodelay,server", device);
2473 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00002474 }
aliguori59030a82009-04-05 18:43:41 +00002475#ifndef _WIN32
2476 else if (strcmp(device, "stdio") == 0) {
2477 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00002478
aliguori59030a82009-04-05 18:43:41 +00002479 memset(&act, 0, sizeof(act));
2480 act.sa_handler = gdb_sigterm_handler;
2481 sigaction(SIGINT, &act, NULL);
2482 }
2483#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02002484 /*
2485 * FIXME: it's a bit weird to allow using a mux chardev here
2486 * and implicitly setup a monitor. We may want to break this.
2487 */
2488 chr = qemu_chr_new_noreplay("gdb", device, true);
aliguori36556b22009-03-28 18:05:53 +00002489 if (!chr)
2490 return -1;
pbrookcfc34752007-02-22 01:48:01 +00002491 }
2492
aliguori36556b22009-03-28 18:05:53 +00002493 s = gdbserver_state;
2494 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002495 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00002496 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002497
aliguori36556b22009-03-28 18:05:53 +00002498 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2499
2500 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03002501 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
2502 NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00002503 monitor_init(mon_chr, 0);
2504 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002505 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00002506 mon_chr = s->mon_chr;
Luc Michel8f468632019-01-07 15:23:45 +00002507 cleanup_processes(s);
aliguori36556b22009-03-28 18:05:53 +00002508 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002509 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002510 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002511 s->c_cpu = first_cpu;
2512 s->g_cpu = first_cpu;
Luc Michel8f468632019-01-07 15:23:45 +00002513
2514 create_processes(s);
2515
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002516 if (chr) {
2517 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002518 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Anton Nefedov81517ba2017-07-06 15:08:49 +03002519 gdb_chr_event, NULL, NULL, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002520 }
aliguori36556b22009-03-28 18:05:53 +00002521 s->state = chr ? RS_IDLE : RS_INACTIVE;
2522 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002523 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002524
pbrook4046d912007-01-28 01:53:16 +00002525 return 0;
2526}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002527
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01002528void gdbserver_cleanup(void)
2529{
2530 if (gdbserver_state) {
2531 put_packet(gdbserver_state, "W00");
2532 }
2533}
2534
Marc-André Lureau777357d2016-12-07 18:39:10 +03002535static void register_types(void)
2536{
2537 type_register_static(&char_gdb_type_info);
2538}
2539
2540type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002541#endif