blob: c4e4f9f08219ddfd6be5dfe28d50d171bff987d4 [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"
bellard1fddef42005-04-17 19:16:13 +000032#endif
bellard67b915a2004-03-31 23:37:16 +000033
pbrook56aebc82008-10-11 17:55:29 +000034#define MAX_PACKET_LENGTH 4096
35
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010036#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010037#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/kvm.h"
Leon Alraecfe67ce2015-06-19 14:17:45 +010039#include "exec/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010040#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000041
Jan Kiszkaa3919382015-02-07 09:38:44 +010042#ifdef CONFIG_USER_ONLY
43#define GDB_ATTACHED "0"
44#else
45#define GDB_ATTACHED "1"
46#endif
47
Andreas Färberf3659ee2013-06-27 19:09:09 +020048static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
49 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020050{
Andreas Färberf3659ee2013-06-27 19:09:09 +020051 CPUClass *cc = CPU_GET_CLASS(cpu);
52
53 if (cc->memory_rw_debug) {
54 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
55 }
56 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020057}
aurel32ca587a82008-12-18 22:44:13 +000058
Alex Bennéed2a6c852017-07-12 11:52:14 +010059/* Return the GDB index for a given vCPU state.
60 *
61 * For user mode this is simply the thread id. In system mode GDB
62 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
63 */
64static inline int cpu_gdb_index(CPUState *cpu)
65{
66#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010067 TaskState *ts = (TaskState *) cpu->opaque;
68 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010069#else
70 return cpu->cpu_index + 1;
71#endif
72}
73
aurel32ca587a82008-12-18 22:44:13 +000074enum {
75 GDB_SIGNAL_0 = 0,
76 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010077 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000078 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010079 GDB_SIGNAL_ABRT = 6,
80 GDB_SIGNAL_ALRM = 14,
81 GDB_SIGNAL_IO = 23,
82 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000083 GDB_SIGNAL_UNKNOWN = 143
84};
85
86#ifdef CONFIG_USER_ONLY
87
88/* Map target signal numbers to GDB protocol signal numbers and vice
89 * versa. For user emulation's currently supported systems, we can
90 * assume most signals are defined.
91 */
92
93static int gdb_signal_table[] = {
94 0,
95 TARGET_SIGHUP,
96 TARGET_SIGINT,
97 TARGET_SIGQUIT,
98 TARGET_SIGILL,
99 TARGET_SIGTRAP,
100 TARGET_SIGABRT,
101 -1, /* SIGEMT */
102 TARGET_SIGFPE,
103 TARGET_SIGKILL,
104 TARGET_SIGBUS,
105 TARGET_SIGSEGV,
106 TARGET_SIGSYS,
107 TARGET_SIGPIPE,
108 TARGET_SIGALRM,
109 TARGET_SIGTERM,
110 TARGET_SIGURG,
111 TARGET_SIGSTOP,
112 TARGET_SIGTSTP,
113 TARGET_SIGCONT,
114 TARGET_SIGCHLD,
115 TARGET_SIGTTIN,
116 TARGET_SIGTTOU,
117 TARGET_SIGIO,
118 TARGET_SIGXCPU,
119 TARGET_SIGXFSZ,
120 TARGET_SIGVTALRM,
121 TARGET_SIGPROF,
122 TARGET_SIGWINCH,
123 -1, /* SIGLOST */
124 TARGET_SIGUSR1,
125 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000126#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000127 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000128#else
129 -1,
130#endif
aurel32ca587a82008-12-18 22:44:13 +0000131 -1, /* SIGPOLL */
132 -1,
133 -1,
134 -1,
135 -1,
136 -1,
137 -1,
138 -1,
139 -1,
140 -1,
141 -1,
142 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000143#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000144 __SIGRTMIN + 1,
145 __SIGRTMIN + 2,
146 __SIGRTMIN + 3,
147 __SIGRTMIN + 4,
148 __SIGRTMIN + 5,
149 __SIGRTMIN + 6,
150 __SIGRTMIN + 7,
151 __SIGRTMIN + 8,
152 __SIGRTMIN + 9,
153 __SIGRTMIN + 10,
154 __SIGRTMIN + 11,
155 __SIGRTMIN + 12,
156 __SIGRTMIN + 13,
157 __SIGRTMIN + 14,
158 __SIGRTMIN + 15,
159 __SIGRTMIN + 16,
160 __SIGRTMIN + 17,
161 __SIGRTMIN + 18,
162 __SIGRTMIN + 19,
163 __SIGRTMIN + 20,
164 __SIGRTMIN + 21,
165 __SIGRTMIN + 22,
166 __SIGRTMIN + 23,
167 __SIGRTMIN + 24,
168 __SIGRTMIN + 25,
169 __SIGRTMIN + 26,
170 __SIGRTMIN + 27,
171 __SIGRTMIN + 28,
172 __SIGRTMIN + 29,
173 __SIGRTMIN + 30,
174 __SIGRTMIN + 31,
175 -1, /* SIGCANCEL */
176 __SIGRTMIN,
177 __SIGRTMIN + 32,
178 __SIGRTMIN + 33,
179 __SIGRTMIN + 34,
180 __SIGRTMIN + 35,
181 __SIGRTMIN + 36,
182 __SIGRTMIN + 37,
183 __SIGRTMIN + 38,
184 __SIGRTMIN + 39,
185 __SIGRTMIN + 40,
186 __SIGRTMIN + 41,
187 __SIGRTMIN + 42,
188 __SIGRTMIN + 43,
189 __SIGRTMIN + 44,
190 __SIGRTMIN + 45,
191 __SIGRTMIN + 46,
192 __SIGRTMIN + 47,
193 __SIGRTMIN + 48,
194 __SIGRTMIN + 49,
195 __SIGRTMIN + 50,
196 __SIGRTMIN + 51,
197 __SIGRTMIN + 52,
198 __SIGRTMIN + 53,
199 __SIGRTMIN + 54,
200 __SIGRTMIN + 55,
201 __SIGRTMIN + 56,
202 __SIGRTMIN + 57,
203 __SIGRTMIN + 58,
204 __SIGRTMIN + 59,
205 __SIGRTMIN + 60,
206 __SIGRTMIN + 61,
207 __SIGRTMIN + 62,
208 __SIGRTMIN + 63,
209 __SIGRTMIN + 64,
210 __SIGRTMIN + 65,
211 __SIGRTMIN + 66,
212 __SIGRTMIN + 67,
213 __SIGRTMIN + 68,
214 __SIGRTMIN + 69,
215 __SIGRTMIN + 70,
216 __SIGRTMIN + 71,
217 __SIGRTMIN + 72,
218 __SIGRTMIN + 73,
219 __SIGRTMIN + 74,
220 __SIGRTMIN + 75,
221 __SIGRTMIN + 76,
222 __SIGRTMIN + 77,
223 __SIGRTMIN + 78,
224 __SIGRTMIN + 79,
225 __SIGRTMIN + 80,
226 __SIGRTMIN + 81,
227 __SIGRTMIN + 82,
228 __SIGRTMIN + 83,
229 __SIGRTMIN + 84,
230 __SIGRTMIN + 85,
231 __SIGRTMIN + 86,
232 __SIGRTMIN + 87,
233 __SIGRTMIN + 88,
234 __SIGRTMIN + 89,
235 __SIGRTMIN + 90,
236 __SIGRTMIN + 91,
237 __SIGRTMIN + 92,
238 __SIGRTMIN + 93,
239 __SIGRTMIN + 94,
240 __SIGRTMIN + 95,
241 -1, /* SIGINFO */
242 -1, /* UNKNOWN */
243 -1, /* DEFAULT */
244 -1,
245 -1,
246 -1,
247 -1,
248 -1,
249 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000250#endif
aurel32ca587a82008-12-18 22:44:13 +0000251};
bellard8f447cc2006-06-14 15:21:14 +0000252#else
aurel32ca587a82008-12-18 22:44:13 +0000253/* In system mode we only need SIGINT and SIGTRAP; other signals
254 are not yet supported. */
255
256enum {
257 TARGET_SIGINT = 2,
258 TARGET_SIGTRAP = 5
259};
260
261static int gdb_signal_table[] = {
262 -1,
263 -1,
264 TARGET_SIGINT,
265 -1,
266 -1,
267 TARGET_SIGTRAP
268};
bellard8f447cc2006-06-14 15:21:14 +0000269#endif
bellardb4608c02003-06-27 17:34:32 +0000270
aurel32ca587a82008-12-18 22:44:13 +0000271#ifdef CONFIG_USER_ONLY
272static int target_signal_to_gdb (int sig)
273{
274 int i;
275 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
276 if (gdb_signal_table[i] == sig)
277 return i;
278 return GDB_SIGNAL_UNKNOWN;
279}
280#endif
281
282static int gdb_signal_to_target (int sig)
283{
284 if (sig < ARRAY_SIZE (gdb_signal_table))
285 return gdb_signal_table[sig];
286 else
287 return -1;
288}
289
pbrook56aebc82008-10-11 17:55:29 +0000290typedef struct GDBRegisterState {
291 int base_reg;
292 int num_regs;
293 gdb_reg_cb get_reg;
294 gdb_reg_cb set_reg;
295 const char *xml;
296 struct GDBRegisterState *next;
297} GDBRegisterState;
298
bellard858693c2004-03-31 18:52:07 +0000299enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000300 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000301 RS_IDLE,
302 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400303 RS_GETLINE_ESC,
304 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000305 RS_CHKSUM1,
306 RS_CHKSUM2,
307};
bellard858693c2004-03-31 18:52:07 +0000308typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200309 CPUState *c_cpu; /* current CPU for step/continue ops */
310 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200311 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000312 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000313 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000314 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400315 int line_sum; /* running checksum */
316 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000317 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000318 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000319 int signal;
bellard41625032005-04-24 10:07:11 +0000320#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000321 int fd;
bellard41625032005-04-24 10:07:11 +0000322 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000323#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300324 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300325 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000326#endif
Meador Ingecdb432b2012-03-15 17:49:45 +0000327 char syscall_buf[256];
328 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000329} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000330
edgar_igl60897d32008-05-09 08:25:14 +0000331/* By default use no IRQs and no timers while single stepping so as to
332 * make single stepping like an ICE HW step.
333 */
334static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
335
aliguori880a7572008-11-18 20:30:24 +0000336static GDBState *gdbserver_state;
337
Andreas Färber5b50e792013-06-29 04:18:45 +0200338bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000339
bellard1fddef42005-04-17 19:16:13 +0000340#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000341/* XXX: This is not thread safe. Do we care? */
342static int gdbserver_fd = -1;
343
bellard858693c2004-03-31 18:52:07 +0000344static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000345{
346 uint8_t ch;
347 int ret;
348
349 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000350 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000351 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000352 if (errno == ECONNRESET)
353 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200354 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000355 return -1;
356 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000357 close(s->fd);
358 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000359 return -1;
360 } else {
361 break;
362 }
363 }
364 return ch;
365}
pbrook4046d912007-01-28 01:53:16 +0000366#endif
bellardb4608c02003-06-27 17:34:32 +0000367
blueswir1654efcf2009-04-18 07:29:59 +0000368static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000369 GDB_SYS_UNKNOWN,
370 GDB_SYS_ENABLED,
371 GDB_SYS_DISABLED,
372} gdb_syscall_mode;
373
Liviu Ionescua38bb072014-12-11 12:07:48 +0000374/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000375int use_gdb_syscalls(void)
376{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100377 SemihostingTarget target = semihosting_get_target();
378 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000379 /* -semihosting-config target=native */
380 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100381 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000382 /* -semihosting-config target=gdb */
383 return true;
384 }
385
386 /* -semihosting-config target=auto */
387 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000388 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000389 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
390 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000391 }
392 return gdb_syscall_mode == GDB_SYS_ENABLED;
393}
394
edgar_iglba70a622008-03-14 06:10:42 +0000395/* Resume execution. */
396static inline void gdb_continue(GDBState *s)
397{
Doug Gale5c9522b2017-12-02 20:30:37 -0500398
edgar_iglba70a622008-03-14 06:10:42 +0000399#ifdef CONFIG_USER_ONLY
400 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500401 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000402#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200403 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500404 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200405 vm_start();
406 }
edgar_iglba70a622008-03-14 06:10:42 +0000407#endif
408}
409
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100410/*
411 * Resume execution, per CPU actions. For user-mode emulation it's
412 * equivalent to gdb_continue.
413 */
414static int gdb_continue_partial(GDBState *s, char *newstates)
415{
416 CPUState *cpu;
417 int res = 0;
418#ifdef CONFIG_USER_ONLY
419 /*
420 * This is not exactly accurate, but it's an improvement compared to the
421 * previous situation, where only one CPU would be single-stepped.
422 */
423 CPU_FOREACH(cpu) {
424 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500425 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100426 cpu_single_step(cpu, sstep_flags);
427 }
428 }
429 s->running_state = 1;
430#else
431 int flag = 0;
432
433 if (!runstate_needs_reset()) {
434 if (vm_prepare_start()) {
435 return 0;
436 }
437
438 CPU_FOREACH(cpu) {
439 switch (newstates[cpu->cpu_index]) {
440 case 0:
441 case 1:
442 break; /* nothing to do here */
443 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500444 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100445 cpu_single_step(cpu, sstep_flags);
446 cpu_resume(cpu);
447 flag = 1;
448 break;
449 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500450 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100451 cpu_resume(cpu);
452 flag = 1;
453 break;
454 default:
455 res = -1;
456 break;
457 }
458 }
459 }
460 if (flag) {
461 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
462 }
463#endif
464 return res;
465}
466
bellard858693c2004-03-31 18:52:07 +0000467static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000468{
pbrook4046d912007-01-28 01:53:16 +0000469#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000470 int ret;
471
472 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000473 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000474 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200475 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000476 return;
477 } else {
478 buf += ret;
479 len -= ret;
480 }
481 }
pbrook4046d912007-01-28 01:53:16 +0000482#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100483 /* XXX this blocks entire thread. Rewrite to use
484 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300485 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000486#endif
bellardb4608c02003-06-27 17:34:32 +0000487}
488
489static inline int fromhex(int v)
490{
491 if (v >= '0' && v <= '9')
492 return v - '0';
493 else if (v >= 'A' && v <= 'F')
494 return v - 'A' + 10;
495 else if (v >= 'a' && v <= 'f')
496 return v - 'a' + 10;
497 else
498 return 0;
499}
500
501static inline int tohex(int v)
502{
503 if (v < 10)
504 return v + '0';
505 else
506 return v - 10 + 'a';
507}
508
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300509/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000510static void memtohex(char *buf, const uint8_t *mem, int len)
511{
512 int i, c;
513 char *q;
514 q = buf;
515 for(i = 0; i < len; i++) {
516 c = mem[i];
517 *q++ = tohex(c >> 4);
518 *q++ = tohex(c & 0xf);
519 }
520 *q = '\0';
521}
522
523static void hextomem(uint8_t *mem, const char *buf, int len)
524{
525 int i;
526
527 for(i = 0; i < len; i++) {
528 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
529 buf += 2;
530 }
531}
532
Doug Gale5c9522b2017-12-02 20:30:37 -0500533static void hexdump(const char *buf, int len,
534 void (*trace_fn)(size_t ofs, char const *text))
535{
536 char line_buffer[3 * 16 + 4 + 16 + 1];
537
538 size_t i;
539 for (i = 0; i < len || (i & 0xF); ++i) {
540 size_t byte_ofs = i & 15;
541
542 if (byte_ofs == 0) {
543 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
544 line_buffer[3 * 16 + 4 + 16] = 0;
545 }
546
547 size_t col_group = (i >> 2) & 3;
548 size_t hex_col = byte_ofs * 3 + col_group;
549 size_t txt_col = 3 * 16 + 4 + byte_ofs;
550
551 if (i < len) {
552 char value = buf[i];
553
554 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
555 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
556 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
557 ? value
558 : '.';
559 }
560
561 if (byte_ofs == 0xF)
562 trace_fn(i & -16, line_buffer);
563 }
564}
565
bellardb4608c02003-06-27 17:34:32 +0000566/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500567static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000568{
pbrook56aebc82008-10-11 17:55:29 +0000569 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000570 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000571
Doug Gale5c9522b2017-12-02 20:30:37 -0500572 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
573 hexdump(buf, len, trace_gdbstub_io_binaryreply);
574 }
575
bellardb4608c02003-06-27 17:34:32 +0000576 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000577 p = s->last_packet;
578 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000579 memcpy(p, buf, len);
580 p += len;
bellardb4608c02003-06-27 17:34:32 +0000581 csum = 0;
582 for(i = 0; i < len; i++) {
583 csum += buf[i];
584 }
pbrook4046d912007-01-28 01:53:16 +0000585 *(p++) = '#';
586 *(p++) = tohex((csum >> 4) & 0xf);
587 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000588
pbrook4046d912007-01-28 01:53:16 +0000589 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000590 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000591
pbrook4046d912007-01-28 01:53:16 +0000592#ifdef CONFIG_USER_ONLY
593 i = get_char(s);
594 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000595 return -1;
pbrook4046d912007-01-28 01:53:16 +0000596 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000597 break;
pbrook4046d912007-01-28 01:53:16 +0000598#else
599 break;
600#endif
bellardb4608c02003-06-27 17:34:32 +0000601 }
602 return 0;
603}
604
pbrook56aebc82008-10-11 17:55:29 +0000605/* return -1 if error, 0 if OK */
606static int put_packet(GDBState *s, const char *buf)
607{
Doug Gale5c9522b2017-12-02 20:30:37 -0500608 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000609
Doug Gale5c9522b2017-12-02 20:30:37 -0500610 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000611}
612
pbrook56aebc82008-10-11 17:55:29 +0000613/* Encode data using the encoding for 'x' packets. */
614static int memtox(char *buf, const char *mem, int len)
615{
616 char *p = buf;
617 char c;
618
619 while (len--) {
620 c = *(mem++);
621 switch (c) {
622 case '#': case '$': case '*': case '}':
623 *(p++) = '}';
624 *(p++) = c ^ 0x20;
625 break;
626 default:
627 *(p++) = c;
628 break;
629 }
630 }
631 return p - buf;
632}
633
Andreas Färber5b24c642013-07-07 15:08:22 +0200634static const char *get_feature_xml(const char *p, const char **newp,
635 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000636{
pbrook56aebc82008-10-11 17:55:29 +0000637 size_t len;
638 int i;
639 const char *name;
640 static char target_xml[1024];
641
642 len = 0;
643 while (p[len] && p[len] != ':')
644 len++;
645 *newp = p + len;
646
647 name = NULL;
648 if (strncmp(p, "target.xml", len) == 0) {
649 /* Generate the XML description for this CPU. */
650 if (!target_xml[0]) {
651 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200652 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000653
David Hildenbrandb3820e62015-12-03 13:14:41 +0100654 pstrcat(target_xml, sizeof(target_xml),
655 "<?xml version=\"1.0\"?>"
656 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
657 "<target>");
658 if (cc->gdb_arch_name) {
659 gchar *arch = cc->gdb_arch_name(cpu);
660 pstrcat(target_xml, sizeof(target_xml), "<architecture>");
661 pstrcat(target_xml, sizeof(target_xml), arch);
662 pstrcat(target_xml, sizeof(target_xml), "</architecture>");
663 g_free(arch);
664 }
665 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
666 pstrcat(target_xml, sizeof(target_xml), cc->gdb_core_xml_file);
667 pstrcat(target_xml, sizeof(target_xml), "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200668 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000669 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
670 pstrcat(target_xml, sizeof(target_xml), r->xml);
671 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000672 }
blueswir12dc766d2009-04-13 16:06:19 +0000673 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000674 }
675 return target_xml;
676 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100677 if (cc->gdb_get_dynamic_xml) {
678 CPUState *cpu = first_cpu;
679 char *xmlname = g_strndup(p, len);
680 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
681
682 g_free(xmlname);
683 if (xml) {
684 return xml;
685 }
686 }
pbrook56aebc82008-10-11 17:55:29 +0000687 for (i = 0; ; i++) {
688 name = xml_builtin[i][0];
689 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
690 break;
691 }
692 return name ? xml_builtin[i][1] : NULL;
693}
pbrook56aebc82008-10-11 17:55:29 +0000694
Andreas Färber385b9f02013-06-27 18:25:36 +0200695static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000696{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200697 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200698 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000699 GDBRegisterState *r;
700
Andreas Färbera0e372f2013-06-28 23:18:47 +0200701 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200702 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200703 }
pbrook56aebc82008-10-11 17:55:29 +0000704
Andreas Färbereac8b352013-06-28 21:11:37 +0200705 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000706 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
707 return r->get_reg(env, mem_buf, reg - r->base_reg);
708 }
709 }
710 return 0;
711}
712
Andreas Färber385b9f02013-06-27 18:25:36 +0200713static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000714{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200715 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200716 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000717 GDBRegisterState *r;
718
Andreas Färbera0e372f2013-06-28 23:18:47 +0200719 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200720 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200721 }
pbrook56aebc82008-10-11 17:55:29 +0000722
Andreas Färbereac8b352013-06-28 21:11:37 +0200723 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000724 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
725 return r->set_reg(env, mem_buf, reg - r->base_reg);
726 }
727 }
728 return 0;
729}
730
731/* Register a supplemental set of CPU registers. If g_pos is nonzero it
732 specifies the first register number and these registers are included in
733 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
734 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
735 */
736
Andreas Färber22169d42013-06-28 21:27:39 +0200737void gdb_register_coprocessor(CPUState *cpu,
738 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
739 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000740{
741 GDBRegisterState *s;
742 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000743
Andreas Färbereac8b352013-06-28 21:11:37 +0200744 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000745 while (*p) {
746 /* Check for duplicates. */
747 if (strcmp((*p)->xml, xml) == 0)
748 return;
749 p = &(*p)->next;
750 }
Stefan Weil9643c252011-10-18 22:25:38 +0200751
752 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200753 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200754 s->num_regs = num_regs;
755 s->get_reg = get_reg;
756 s->set_reg = set_reg;
757 s->xml = xml;
758
pbrook56aebc82008-10-11 17:55:29 +0000759 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200760 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000761 *p = s;
762 if (g_pos) {
763 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800764 error_report("Error: Bad gdb register numbering for '%s', "
765 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200766 } else {
767 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000768 }
769 }
770}
771
aliguoria1d1bb32008-11-18 20:07:32 +0000772#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100773/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
774static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
775{
776 static const int xlat[] = {
777 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
778 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
779 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
780 };
781
782 CPUClass *cc = CPU_GET_CLASS(cpu);
783 int cputype = xlat[gdbtype];
784
785 if (cc->gdb_stop_before_watchpoint) {
786 cputype |= BP_STOP_BEFORE_ACCESS;
787 }
788 return cputype;
789}
aliguoria1d1bb32008-11-18 20:07:32 +0000790#endif
791
aliguori880a7572008-11-18 20:30:24 +0000792static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000793{
Andreas Färber182735e2013-05-29 22:29:20 +0200794 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000795 int err = 0;
796
Andreas Färber62278812013-06-27 17:12:06 +0200797 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200798 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200799 }
aliguorie22a25c2009-03-12 20:12:48 +0000800
aliguoria1d1bb32008-11-18 20:07:32 +0000801 switch (type) {
802 case GDB_BREAKPOINT_SW:
803 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200804 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200805 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
806 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000807 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200808 }
aliguori880a7572008-11-18 20:30:24 +0000809 }
810 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000811#ifndef CONFIG_USER_ONLY
812 case GDB_WATCHPOINT_WRITE:
813 case GDB_WATCHPOINT_READ:
814 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200815 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100816 err = cpu_watchpoint_insert(cpu, addr, len,
817 xlat_gdb_type(cpu, type), NULL);
818 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000819 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100820 }
aliguori880a7572008-11-18 20:30:24 +0000821 }
822 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000823#endif
824 default:
825 return -ENOSYS;
826 }
827}
828
aliguori880a7572008-11-18 20:30:24 +0000829static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000830{
Andreas Färber182735e2013-05-29 22:29:20 +0200831 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000832 int err = 0;
833
Andreas Färber62278812013-06-27 17:12:06 +0200834 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200835 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200836 }
aliguorie22a25c2009-03-12 20:12:48 +0000837
aliguoria1d1bb32008-11-18 20:07:32 +0000838 switch (type) {
839 case GDB_BREAKPOINT_SW:
840 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200841 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200842 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
843 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000844 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200845 }
aliguori880a7572008-11-18 20:30:24 +0000846 }
847 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000848#ifndef CONFIG_USER_ONLY
849 case GDB_WATCHPOINT_WRITE:
850 case GDB_WATCHPOINT_READ:
851 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200852 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100853 err = cpu_watchpoint_remove(cpu, addr, len,
854 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +0000855 if (err)
856 break;
857 }
858 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000859#endif
860 default:
861 return -ENOSYS;
862 }
863}
864
aliguori880a7572008-11-18 20:30:24 +0000865static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000866{
Andreas Färber182735e2013-05-29 22:29:20 +0200867 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000868
aliguorie22a25c2009-03-12 20:12:48 +0000869 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200870 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000871 return;
872 }
873
Andreas Färberbdc44642013-06-24 23:50:24 +0200874 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200875 cpu_breakpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000876#ifndef CONFIG_USER_ONLY
Andreas Färber75a34032013-09-02 16:57:02 +0200877 cpu_watchpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000878#endif
aliguori880a7572008-11-18 20:30:24 +0000879 }
aliguoria1d1bb32008-11-18 20:07:32 +0000880}
881
aurel32fab9d282009-04-08 21:29:37 +0000882static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
883{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200884 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200885
886 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -0700887 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +0000888}
889
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200890static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700891{
Andreas Färber0d342822012-12-17 07:12:13 +0100892 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700893
Andreas Färberbdc44642013-06-24 23:50:24 +0200894 CPU_FOREACH(cpu) {
Alex Bennéed2a6c852017-07-12 11:52:14 +0100895 if (cpu_gdb_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200896 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200897 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700898 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200899
900 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700901}
902
Jan Kiszka4dabe742015-02-07 09:38:43 +0100903static int is_query_packet(const char *p, const char *query, char separator)
904{
905 unsigned int query_len = strlen(query);
906
907 return strncmp(p, query, query_len) == 0 &&
908 (p[query_len] == '\0' || p[query_len] == separator);
909}
910
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100911/**
912 * gdb_handle_vcont - Parses and handles a vCont packet.
913 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
914 * a format error, 0 on success.
915 */
916static int gdb_handle_vcont(GDBState *s, const char *p)
917{
918 int res, idx, signal = 0;
919 char cur_action;
920 char *newstates;
921 unsigned long tmp;
922 CPUState *cpu;
923#ifdef CONFIG_USER_ONLY
924 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
925
926 CPU_FOREACH(cpu) {
927 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
928 }
929#endif
930 /* uninitialised CPUs stay 0 */
931 newstates = g_new0(char, max_cpus);
932
933 /* mark valid CPUs with 1 */
934 CPU_FOREACH(cpu) {
935 newstates[cpu->cpu_index] = 1;
936 }
937
938 /*
939 * res keeps track of what error we are returning, with -ENOTSUP meaning
940 * that the command is unknown or unsupported, thus returning an empty
941 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
942 * or incorrect parameters passed.
943 */
944 res = 0;
945 while (*p) {
946 if (*p++ != ';') {
947 res = -ENOTSUP;
948 goto out;
949 }
950
951 cur_action = *p++;
952 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +0100953 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100954 res = qemu_strtoul(p + 1, &p, 16, &tmp);
955 if (res) {
956 goto out;
957 }
958 signal = gdb_signal_to_target(tmp);
959 } else if (cur_action != 'c' && cur_action != 's') {
960 /* unknown/invalid/unsupported command */
961 res = -ENOTSUP;
962 goto out;
963 }
964 /* thread specification. special values: (none), -1 = all; 0 = any */
965 if ((p[0] == ':' && p[1] == '-' && p[2] == '1') || (p[0] != ':')) {
966 if (*p == ':') {
967 p += 3;
968 }
969 for (idx = 0; idx < max_cpus; idx++) {
970 if (newstates[idx] == 1) {
971 newstates[idx] = cur_action;
972 }
973 }
974 } else if (*p == ':') {
975 p++;
976 res = qemu_strtoul(p, &p, 16, &tmp);
977 if (res) {
978 goto out;
979 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100980
Alex Bennée5a6a1ad2017-07-12 11:52:16 +0100981 /* 0 means any thread, so we pick the first valid CPU */
982 cpu = tmp ? find_cpu(tmp) : first_cpu;
983
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100984 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +0100985 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100986 res = -EINVAL;
987 goto out;
988 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +0100989
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100990 /* only use if no previous match occourred */
991 if (newstates[cpu->cpu_index] == 1) {
992 newstates[cpu->cpu_index] = cur_action;
993 }
994 }
995 }
996 s->signal = signal;
997 gdb_continue_partial(s, newstates);
998
999out:
1000 g_free(newstates);
1001
1002 return res;
1003}
1004
aliguori880a7572008-11-18 20:30:24 +00001005static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +00001006{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001007 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +02001008 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +00001009 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001010 uint32_t thread;
1011 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +00001012 uint8_t mem_buf[MAX_PACKET_LENGTH];
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -03001013 char buf[sizeof(mem_buf) + 1 /* trailing NUL */];
pbrook56aebc82008-10-11 17:55:29 +00001014 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001015 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +00001016
Doug Gale5c9522b2017-12-02 20:30:37 -05001017 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01001018
bellard858693c2004-03-31 18:52:07 +00001019 p = line_buf;
1020 ch = *p++;
1021 switch(ch) {
1022 case '?':
bellard1fddef42005-04-17 19:16:13 +00001023 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +00001024 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Alex Bennéed2a6c852017-07-12 11:52:14 +01001025 cpu_gdb_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +00001026 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001027 /* Remove all the breakpoints when this query is issued,
1028 * because gdb is doing and initial connect and the state
1029 * should be cleaned up.
1030 */
aliguori880a7572008-11-18 20:30:24 +00001031 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001032 break;
1033 case 'c':
1034 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001035 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001036 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001037 }
aurel32ca587a82008-12-18 22:44:13 +00001038 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001039 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001040 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001041 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001042 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1043 if (s->signal == -1)
1044 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001045 gdb_continue(s);
1046 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001047 case 'v':
1048 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001049 p += 4;
1050 if (*p == '?') {
1051 put_packet(s, "vCont;c;C;s;S");
1052 break;
1053 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001054
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001055 res = gdb_handle_vcont(s, p);
1056
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001057 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001058 if ((res == -EINVAL) || (res == -ERANGE)) {
1059 put_packet(s, "E22");
1060 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001061 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001062 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001063 }
1064 break;
1065 } else {
1066 goto unknown_command;
1067 }
edgar_igl7d03f822008-05-17 18:58:29 +00001068 case 'k':
1069 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001070 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001071 exit(0);
1072 case 'D':
1073 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +00001074 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -03001075 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +00001076 gdb_continue(s);
1077 put_packet(s, "OK");
1078 break;
bellard858693c2004-03-31 18:52:07 +00001079 case 's':
1080 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001081 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001082 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001083 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001084 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001085 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001086 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001087 case 'F':
1088 {
1089 target_ulong ret;
1090 target_ulong err;
1091
1092 ret = strtoull(p, (char **)&p, 16);
1093 if (*p == ',') {
1094 p++;
1095 err = strtoull(p, (char **)&p, 16);
1096 } else {
1097 err = 0;
1098 }
1099 if (*p == ',')
1100 p++;
1101 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001102 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001103 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001104 s->current_syscall_cb = NULL;
1105 }
pbrooka2d1eba2007-01-28 03:10:55 +00001106 if (type == 'C') {
1107 put_packet(s, "T02");
1108 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001109 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001110 }
1111 }
1112 break;
bellard858693c2004-03-31 18:52:07 +00001113 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001114 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001115 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001116 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001117 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001118 len += reg_size;
1119 }
1120 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001121 put_packet(s, buf);
1122 break;
1123 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001124 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001125 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001126 len = strlen(p) / 2;
1127 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001128 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001129 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001130 len -= reg_size;
1131 registers += reg_size;
1132 }
bellard858693c2004-03-31 18:52:07 +00001133 put_packet(s, "OK");
1134 break;
1135 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001136 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001137 if (*p == ',')
1138 p++;
bellard9d9754a2006-06-25 15:32:37 +00001139 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001140
1141 /* memtohex() doubles the required space */
1142 if (len > MAX_PACKET_LENGTH / 2) {
1143 put_packet (s, "E22");
1144 break;
1145 }
1146
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001147 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001148 put_packet (s, "E14");
1149 } else {
1150 memtohex(buf, mem_buf, len);
1151 put_packet(s, buf);
1152 }
bellard858693c2004-03-31 18:52:07 +00001153 break;
1154 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001155 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001156 if (*p == ',')
1157 p++;
bellard9d9754a2006-06-25 15:32:37 +00001158 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001159 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001160 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001161
1162 /* hextomem() reads 2*len bytes */
1163 if (len > strlen(p) / 2) {
1164 put_packet (s, "E22");
1165 break;
1166 }
bellard858693c2004-03-31 18:52:07 +00001167 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001168 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001169 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001170 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001171 } else {
bellard858693c2004-03-31 18:52:07 +00001172 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001173 }
bellard858693c2004-03-31 18:52:07 +00001174 break;
pbrook56aebc82008-10-11 17:55:29 +00001175 case 'p':
1176 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1177 This works, but can be very slow. Anything new enough to
1178 understand XML also knows how to use this properly. */
1179 if (!gdb_has_xml)
1180 goto unknown_command;
1181 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001182 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001183 if (reg_size) {
1184 memtohex(buf, mem_buf, reg_size);
1185 put_packet(s, buf);
1186 } else {
1187 put_packet(s, "E14");
1188 }
1189 break;
1190 case 'P':
1191 if (!gdb_has_xml)
1192 goto unknown_command;
1193 addr = strtoull(p, (char **)&p, 16);
1194 if (*p == '=')
1195 p++;
1196 reg_size = strlen(p) / 2;
1197 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001198 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001199 put_packet(s, "OK");
1200 break;
bellard858693c2004-03-31 18:52:07 +00001201 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001202 case 'z':
1203 type = strtoul(p, (char **)&p, 16);
1204 if (*p == ',')
1205 p++;
bellard9d9754a2006-06-25 15:32:37 +00001206 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001207 if (*p == ',')
1208 p++;
bellard9d9754a2006-06-25 15:32:37 +00001209 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001210 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001211 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001212 else
aliguori880a7572008-11-18 20:30:24 +00001213 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001214 if (res >= 0)
1215 put_packet(s, "OK");
1216 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001217 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001218 else
1219 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001220 break;
aliguori880a7572008-11-18 20:30:24 +00001221 case 'H':
1222 type = *p++;
1223 thread = strtoull(p, (char **)&p, 16);
1224 if (thread == -1 || thread == 0) {
1225 put_packet(s, "OK");
1226 break;
1227 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001228 cpu = find_cpu(thread);
1229 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001230 put_packet(s, "E22");
1231 break;
1232 }
1233 switch (type) {
1234 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001235 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001236 put_packet(s, "OK");
1237 break;
1238 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001239 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001240 put_packet(s, "OK");
1241 break;
1242 default:
1243 put_packet(s, "E22");
1244 break;
1245 }
1246 break;
1247 case 'T':
1248 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001249 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001250
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001251 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001252 put_packet(s, "OK");
1253 } else {
aliguori880a7572008-11-18 20:30:24 +00001254 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001255 }
aliguori880a7572008-11-18 20:30:24 +00001256 break;
pbrook978efd62006-06-17 18:30:42 +00001257 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001258 case 'Q':
1259 /* parse any 'q' packets here */
1260 if (!strcmp(p,"qemu.sstepbits")) {
1261 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001262 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1263 SSTEP_ENABLE,
1264 SSTEP_NOIRQ,
1265 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001266 put_packet(s, buf);
1267 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001268 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001269 /* Display or change the sstep_flags */
1270 p += 10;
1271 if (*p != '=') {
1272 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001273 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001274 put_packet(s, buf);
1275 break;
1276 }
1277 p++;
1278 type = strtoul(p, (char **)&p, 16);
1279 sstep_flags = type;
1280 put_packet(s, "OK");
1281 break;
aliguori880a7572008-11-18 20:30:24 +00001282 } else if (strcmp(p,"C") == 0) {
1283 /* "Current thread" remains vague in the spec, so always return
1284 * the first CPU (gdb returns the first thread). */
1285 put_packet(s, "QC1");
1286 break;
1287 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001288 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001289 goto report_cpuinfo;
1290 } else if (strcmp(p,"sThreadInfo") == 0) {
1291 report_cpuinfo:
1292 if (s->query_cpu) {
Alex Bennéed2a6c852017-07-12 11:52:14 +01001293 snprintf(buf, sizeof(buf), "m%x", cpu_gdb_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001294 put_packet(s, buf);
Andreas Färberbdc44642013-06-24 23:50:24 +02001295 s->query_cpu = CPU_NEXT(s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001296 } else
1297 put_packet(s, "l");
1298 break;
1299 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1300 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001301 cpu = find_cpu(thread);
1302 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001303 cpu_synchronize_state(cpu);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001304 /* memtohex() doubles the required space */
1305 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
Andreas Färber55e5c282012-12-17 06:18:02 +01001306 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001307 cpu->halted ? "halted " : "running");
Doug Gale5c9522b2017-12-02 20:30:37 -05001308 trace_gdbstub_op_extra_info((char *)mem_buf);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001309 memtohex(buf, mem_buf, len);
1310 put_packet(s, buf);
1311 }
aliguori880a7572008-11-18 20:30:24 +00001312 break;
edgar_igl60897d32008-05-09 08:25:14 +00001313 }
blueswir10b8a9882009-03-07 10:51:36 +00001314#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001315 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001316 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001317
blueswir1363a37d2008-08-21 17:58:08 +00001318 snprintf(buf, sizeof(buf),
1319 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1320 ";Bss=" TARGET_ABI_FMT_lx,
1321 ts->info->code_offset,
1322 ts->info->data_offset,
1323 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001324 put_packet(s, buf);
1325 break;
1326 }
blueswir10b8a9882009-03-07 10:51:36 +00001327#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001328 else if (strncmp(p, "Rcmd,", 5) == 0) {
1329 int len = strlen(p + 5);
1330
1331 if ((len % 2) != 0) {
1332 put_packet(s, "E01");
1333 break;
1334 }
aliguori8a34a0f2009-03-05 23:01:55 +00001335 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001336 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001337 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001338 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001339 put_packet(s, "OK");
1340 break;
1341 }
blueswir10b8a9882009-03-07 10:51:36 +00001342#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001343 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001344 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001345 cc = CPU_GET_CLASS(first_cpu);
1346 if (cc->gdb_core_xml_file != NULL) {
1347 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1348 }
pbrook56aebc82008-10-11 17:55:29 +00001349 put_packet(s, buf);
1350 break;
1351 }
pbrook56aebc82008-10-11 17:55:29 +00001352 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1353 const char *xml;
1354 target_ulong total_len;
1355
Andreas Färber5b24c642013-07-07 15:08:22 +02001356 cc = CPU_GET_CLASS(first_cpu);
1357 if (cc->gdb_core_xml_file == NULL) {
1358 goto unknown_command;
1359 }
1360
Andreas Färber5b50e792013-06-29 04:18:45 +02001361 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001362 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001363 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001364 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001365 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001366 put_packet(s, buf);
1367 break;
1368 }
1369
1370 if (*p == ':')
1371 p++;
1372 addr = strtoul(p, (char **)&p, 16);
1373 if (*p == ',')
1374 p++;
1375 len = strtoul(p, (char **)&p, 16);
1376
1377 total_len = strlen(xml);
1378 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001379 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001380 put_packet(s, buf);
1381 break;
1382 }
1383 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1384 len = (MAX_PACKET_LENGTH - 5) / 2;
1385 if (len < total_len - addr) {
1386 buf[0] = 'm';
1387 len = memtox(buf + 1, xml + addr, len);
1388 } else {
1389 buf[0] = 'l';
1390 len = memtox(buf + 1, xml + addr, total_len - addr);
1391 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001392 put_packet_binary(s, buf, len + 1, true);
pbrook56aebc82008-10-11 17:55:29 +00001393 break;
1394 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001395 if (is_query_packet(p, "Attached", ':')) {
1396 put_packet(s, GDB_ATTACHED);
1397 break;
1398 }
pbrook56aebc82008-10-11 17:55:29 +00001399 /* Unrecognised 'q' command. */
1400 goto unknown_command;
1401
bellard858693c2004-03-31 18:52:07 +00001402 default:
pbrook56aebc82008-10-11 17:55:29 +00001403 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001404 /* put empty packet */
1405 buf[0] = '\0';
1406 put_packet(s, buf);
1407 break;
1408 }
1409 return RS_IDLE;
1410}
1411
Andreas Färber64f6b342013-05-27 02:06:09 +02001412void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001413{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001414 gdbserver_state->c_cpu = cpu;
1415 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001416}
1417
bellard1fddef42005-04-17 19:16:13 +00001418#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001419static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001420{
aliguori880a7572008-11-18 20:30:24 +00001421 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001422 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001423 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001424 const char *type;
bellard858693c2004-03-31 18:52:07 +00001425 int ret;
1426
Meador Ingecdb432b2012-03-15 17:49:45 +00001427 if (running || s->state == RS_INACTIVE) {
1428 return;
1429 }
1430 /* Is there a GDB syscall waiting to be sent? */
1431 if (s->current_syscall_cb) {
1432 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001433 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001434 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001435 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001436 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001437 if (cpu->watchpoint_hit) {
1438 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001439 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001440 type = "r";
1441 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001442 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001443 type = "a";
1444 break;
1445 default:
1446 type = "";
1447 break;
1448 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001449 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
1450 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00001451 snprintf(buf, sizeof(buf),
1452 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Alex Bennéed2a6c852017-07-12 11:52:14 +01001453 GDB_SIGNAL_TRAP, cpu_gdb_index(cpu), type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001454 (target_ulong)cpu->watchpoint_hit->vaddr);
1455 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001456 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05001457 } else {
1458 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00001459 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001460 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001461 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001462 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001463 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05001464 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00001465 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001466 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001467 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05001468 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01001469 ret = GDB_SIGNAL_QUIT;
1470 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001471 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001472 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001473 ret = GDB_SIGNAL_IO;
1474 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001475 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05001476 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01001477 ret = GDB_SIGNAL_ALRM;
1478 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001479 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001480 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001481 ret = GDB_SIGNAL_ABRT;
1482 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001483 case RUN_STATE_SAVE_VM:
1484 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001485 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001486 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001487 ret = GDB_SIGNAL_XCPU;
1488 break;
1489 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05001490 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01001491 ret = GDB_SIGNAL_UNKNOWN;
1492 break;
bellardbbeb7b52006-04-23 18:42:15 +00001493 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001494 gdb_set_stop_cpu(cpu);
Alex Bennéed2a6c852017-07-12 11:52:14 +01001495 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_gdb_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001496
1497send_packet:
bellard858693c2004-03-31 18:52:07 +00001498 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001499
1500 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001501 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001502}
bellard1fddef42005-04-17 19:16:13 +00001503#endif
bellard858693c2004-03-31 18:52:07 +00001504
pbrooka2d1eba2007-01-28 03:10:55 +00001505/* Send a gdb syscall request.
1506 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001507 %x - target_ulong argument printed in hex.
1508 %lx - 64-bit argument printed in hex.
1509 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001510void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001511{
pbrooka2d1eba2007-01-28 03:10:55 +00001512 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001513 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001514 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001515 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001516 GDBState *s;
1517
aliguori880a7572008-11-18 20:30:24 +00001518 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001519 if (!s)
1520 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001521 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001522#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001523 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001524#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001525 p = s->syscall_buf;
1526 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001527 *(p++) = 'F';
1528 while (*fmt) {
1529 if (*fmt == '%') {
1530 fmt++;
1531 switch (*fmt++) {
1532 case 'x':
1533 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001534 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001535 break;
pbrooka87295e2007-05-26 15:09:38 +00001536 case 'l':
1537 if (*(fmt++) != 'x')
1538 goto bad_format;
1539 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001540 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001541 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001542 case 's':
1543 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001544 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001545 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001546 break;
1547 default:
pbrooka87295e2007-05-26 15:09:38 +00001548 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001549 error_report("gdbstub: Bad syscall format string '%s'",
1550 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001551 break;
1552 }
1553 } else {
1554 *(p++) = *(fmt++);
1555 }
1556 }
pbrook8a93e022007-08-06 13:19:15 +00001557 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001558#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001559 put_packet(s, s->syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01001560 /* Return control to gdb for it to process the syscall request.
1561 * Since the protocol requires that gdb hands control back to us
1562 * using a "here are the results" F packet, we don't need to check
1563 * gdb_handlesig's return value (which is the signal to deliver if
1564 * execution was resumed via a continue packet).
1565 */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001566 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001567#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001568 /* In this case wait to send the syscall packet until notification that
1569 the CPU has stopped. This must be done because if the packet is sent
1570 now the reply from the syscall request could be received while the CPU
1571 is still in the running state, which can cause packets to be dropped
1572 and state transition 'T' packets to be sent while the syscall is still
1573 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001574 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001575#endif
1576}
1577
Peter Maydell19239b32015-09-07 10:39:27 +01001578void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1579{
1580 va_list va;
1581
1582 va_start(va, fmt);
1583 gdb_do_syscallv(cb, fmt, va);
1584 va_end(va);
1585}
1586
bellard6a00d602005-11-21 23:25:50 +00001587static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001588{
ths60fe76f2007-12-16 03:02:09 +00001589 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001590
bellard1fddef42005-04-17 19:16:13 +00001591#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001592 if (s->last_packet_len) {
1593 /* Waiting for a response to the last packet. If we see the start
1594 of a new command then abandon the previous response. */
1595 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05001596 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00001597 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01001598 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05001599 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01001600 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05001601 trace_gdbstub_io_got_unexpected((uint8_t)ch);
pbrook4046d912007-01-28 01:53:16 +00001602 }
Alex Bennée118e2262017-07-12 11:52:13 +01001603
pbrook4046d912007-01-28 01:53:16 +00001604 if (ch == '+' || ch == '$')
1605 s->last_packet_len = 0;
1606 if (ch != '$')
1607 return;
1608 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001609 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001610 /* when the CPU is running, we cannot do anything except stop
1611 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001612 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001613 } else
bellard1fddef42005-04-17 19:16:13 +00001614#endif
bellard41625032005-04-24 10:07:11 +00001615 {
bellard858693c2004-03-31 18:52:07 +00001616 switch(s->state) {
1617 case RS_IDLE:
1618 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04001619 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00001620 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04001621 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00001622 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04001623 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05001624 trace_gdbstub_err_garbage((uint8_t)ch);
bellard4c3a88a2003-07-26 12:06:08 +00001625 }
1626 break;
bellard858693c2004-03-31 18:52:07 +00001627 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04001628 if (ch == '}') {
1629 /* start escape sequence */
1630 s->state = RS_GETLINE_ESC;
1631 s->line_sum += ch;
1632 } else if (ch == '*') {
1633 /* start run length encoding sequence */
1634 s->state = RS_GETLINE_RLE;
1635 s->line_sum += ch;
1636 } else if (ch == '#') {
1637 /* end of command, start of checksum*/
1638 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00001639 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001640 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00001641 s->state = RS_IDLE;
1642 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001643 /* unescaped command character */
1644 s->line_buf[s->line_buf_index++] = ch;
1645 s->line_sum += ch;
1646 }
1647 break;
1648 case RS_GETLINE_ESC:
1649 if (ch == '#') {
1650 /* unexpected end of command in escape sequence */
1651 s->state = RS_CHKSUM1;
1652 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1653 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05001654 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04001655 s->state = RS_IDLE;
1656 } else {
1657 /* parse escaped character and leave escape state */
1658 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
1659 s->line_sum += ch;
1660 s->state = RS_GETLINE;
1661 }
1662 break;
1663 case RS_GETLINE_RLE:
1664 if (ch < ' ') {
1665 /* invalid RLE count encoding */
Doug Gale5c9522b2017-12-02 20:30:37 -05001666 trace_gdbstub_err_invalid_repeat((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001667 s->state = RS_GETLINE;
1668 } else {
1669 /* decode repeat length */
1670 int repeat = (unsigned char)ch - ' ' + 3;
1671 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
1672 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05001673 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04001674 s->state = RS_IDLE;
1675 } else if (s->line_buf_index < 1) {
1676 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05001677 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04001678 s->state = RS_GETLINE;
1679 } else {
1680 /* repeat the last character */
1681 memset(s->line_buf + s->line_buf_index,
1682 s->line_buf[s->line_buf_index - 1], repeat);
1683 s->line_buf_index += repeat;
1684 s->line_sum += ch;
1685 s->state = RS_GETLINE;
1686 }
bellard858693c2004-03-31 18:52:07 +00001687 }
1688 break;
1689 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04001690 /* get high hex digit of checksum */
1691 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001692 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001693 s->state = RS_GETLINE;
1694 break;
1695 }
bellard858693c2004-03-31 18:52:07 +00001696 s->line_buf[s->line_buf_index] = '\0';
1697 s->line_csum = fromhex(ch) << 4;
1698 s->state = RS_CHKSUM2;
1699 break;
1700 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04001701 /* get low hex digit of checksum */
1702 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001703 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001704 s->state = RS_GETLINE;
1705 break;
bellard858693c2004-03-31 18:52:07 +00001706 }
Doug Gale4bf43122017-05-01 12:22:10 -04001707 s->line_csum |= fromhex(ch);
1708
1709 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001710 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04001711 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00001712 reply = '-';
1713 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00001714 s->state = RS_IDLE;
1715 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001716 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00001717 reply = '+';
1718 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001719 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001720 }
bellardb4608c02003-06-27 17:34:32 +00001721 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001722 default:
1723 abort();
bellardb4608c02003-06-27 17:34:32 +00001724 }
1725 }
bellard858693c2004-03-31 18:52:07 +00001726}
1727
Paul Brook0e1c9c52010-06-16 13:03:51 +01001728/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001729void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001730{
1731 GDBState *s;
1732 char buf[4];
1733
1734 s = gdbserver_state;
1735 if (!s) {
1736 return;
1737 }
1738#ifdef CONFIG_USER_ONLY
1739 if (gdbserver_fd < 0 || s->fd < 0) {
1740 return;
1741 }
1742#endif
1743
Doug Gale5c9522b2017-12-02 20:30:37 -05001744 trace_gdbstub_op_exiting((uint8_t)code);
1745
Paul Brook0e1c9c52010-06-16 13:03:51 +01001746 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1747 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001748
1749#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04001750 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001751#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001752}
1753
bellard1fddef42005-04-17 19:16:13 +00001754#ifdef CONFIG_USER_ONLY
1755int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001756gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001757{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001758 GDBState *s;
1759 char buf[256];
1760 int n;
bellard1fddef42005-04-17 19:16:13 +00001761
Andreas Färber5ca666c2013-06-24 19:20:57 +02001762 s = gdbserver_state;
1763 if (gdbserver_fd < 0 || s->fd < 0) {
1764 return sig;
bellard1fddef42005-04-17 19:16:13 +00001765 }
1766
Andreas Färber5ca666c2013-06-24 19:20:57 +02001767 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001768 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001769 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00001770
Andreas Färber5ca666c2013-06-24 19:20:57 +02001771 if (sig != 0) {
1772 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1773 put_packet(s, buf);
1774 }
1775 /* put_packet() might have detected that the peer terminated the
1776 connection. */
1777 if (s->fd < 0) {
1778 return sig;
1779 }
1780
1781 sig = 0;
1782 s->state = RS_IDLE;
1783 s->running_state = 0;
1784 while (s->running_state == 0) {
1785 n = read(s->fd, buf, 256);
1786 if (n > 0) {
1787 int i;
1788
1789 for (i = 0; i < n; i++) {
1790 gdb_read_byte(s, buf[i]);
1791 }
Peter Wu5819e3e2016-06-05 16:35:48 +02001792 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02001793 /* XXX: Connection closed. Should probably wait for another
1794 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02001795 if (n == 0) {
1796 close(s->fd);
1797 }
1798 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001799 return sig;
bellard1fddef42005-04-17 19:16:13 +00001800 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001801 }
1802 sig = s->signal;
1803 s->signal = 0;
1804 return sig;
bellard1fddef42005-04-17 19:16:13 +00001805}
bellarde9009672005-04-26 20:42:36 +00001806
aurel32ca587a82008-12-18 22:44:13 +00001807/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001808void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001809{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001810 GDBState *s;
1811 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001812
Andreas Färber5ca666c2013-06-24 19:20:57 +02001813 s = gdbserver_state;
1814 if (gdbserver_fd < 0 || s->fd < 0) {
1815 return;
1816 }
aurel32ca587a82008-12-18 22:44:13 +00001817
Andreas Färber5ca666c2013-06-24 19:20:57 +02001818 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1819 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001820}
bellard1fddef42005-04-17 19:16:13 +00001821
Peter Maydell2f652222018-05-14 18:30:44 +01001822static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001823{
1824 GDBState *s;
1825 struct sockaddr_in sockaddr;
1826 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001827 int fd;
bellard858693c2004-03-31 18:52:07 +00001828
1829 for(;;) {
1830 len = sizeof(sockaddr);
1831 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1832 if (fd < 0 && errno != EINTR) {
1833 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01001834 return false;
bellard858693c2004-03-31 18:52:07 +00001835 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01001836 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00001837 break;
1838 }
1839 }
1840
1841 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01001842 if (socket_set_nodelay(fd)) {
1843 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03001844 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01001845 return false;
1846 }
ths3b46e622007-09-17 08:09:54 +00001847
Anthony Liguori7267c092011-08-20 22:09:37 -05001848 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001849 s->c_cpu = first_cpu;
1850 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001851 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02001852 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00001853
aliguori880a7572008-11-18 20:30:24 +00001854 gdbserver_state = s;
Peter Maydell2f652222018-05-14 18:30:44 +01001855 return true;
bellard858693c2004-03-31 18:52:07 +00001856}
1857
1858static int gdbserver_open(int port)
1859{
1860 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001861 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00001862
1863 fd = socket(PF_INET, SOCK_STREAM, 0);
1864 if (fd < 0) {
1865 perror("socket");
1866 return -1;
1867 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01001868 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00001869
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001870 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00001871
1872 sockaddr.sin_family = AF_INET;
1873 sockaddr.sin_port = htons(port);
1874 sockaddr.sin_addr.s_addr = 0;
1875 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1876 if (ret < 0) {
1877 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001878 close(fd);
bellard858693c2004-03-31 18:52:07 +00001879 return -1;
1880 }
Peter Wu96165b92016-05-04 11:32:17 +02001881 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00001882 if (ret < 0) {
1883 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001884 close(fd);
bellard858693c2004-03-31 18:52:07 +00001885 return -1;
1886 }
bellard858693c2004-03-31 18:52:07 +00001887 return fd;
1888}
1889
1890int gdbserver_start(int port)
1891{
1892 gdbserver_fd = gdbserver_open(port);
1893 if (gdbserver_fd < 0)
1894 return -1;
1895 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01001896 if (!gdb_accept()) {
1897 close(gdbserver_fd);
1898 gdbserver_fd = -1;
1899 return -1;
1900 }
bellardb4608c02003-06-27 17:34:32 +00001901 return 0;
1902}
aurel322b1319c2008-12-18 22:44:04 +00001903
1904/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07001905void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00001906{
1907 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02001908
1909 if (gdbserver_fd < 0 || s->fd < 0) {
1910 return;
1911 }
aurel322b1319c2008-12-18 22:44:04 +00001912 close(s->fd);
1913 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001914 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02001915 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00001916}
pbrook4046d912007-01-28 01:53:16 +00001917#else
thsaa1f17c2007-07-11 22:48:58 +00001918static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001919{
pbrook56aebc82008-10-11 17:55:29 +00001920 /* We can handle an arbitrarily large amount of data.
1921 Pick the maximum packet size, which is as good as anything. */
1922 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001923}
1924
thsaa1f17c2007-07-11 22:48:58 +00001925static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001926{
pbrook4046d912007-01-28 01:53:16 +00001927 int i;
1928
1929 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001930 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001931 }
1932}
1933
1934static void gdb_chr_event(void *opaque, int event)
1935{
1936 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301937 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001938 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02001939 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00001940 break;
1941 default:
1942 break;
1943 }
1944}
1945
aliguori8a34a0f2009-03-05 23:01:55 +00001946static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1947{
1948 char buf[MAX_PACKET_LENGTH];
1949
1950 buf[0] = 'O';
1951 if (len > (MAX_PACKET_LENGTH/2) - 1)
1952 len = (MAX_PACKET_LENGTH/2) - 1;
1953 memtohex(buf + 1, (uint8_t *)msg, len);
1954 put_packet(s, buf);
1955}
1956
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001957static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00001958{
1959 const char *p = (const char *)buf;
1960 int max_sz;
1961
1962 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1963 for (;;) {
1964 if (len <= max_sz) {
1965 gdb_monitor_output(gdbserver_state, p, len);
1966 break;
1967 }
1968 gdb_monitor_output(gdbserver_state, p, max_sz);
1969 p += max_sz;
1970 len -= max_sz;
1971 }
1972 return len;
1973}
1974
aliguori59030a82009-04-05 18:43:41 +00001975#ifndef _WIN32
1976static void gdb_sigterm_handler(int signal)
1977{
Luiz Capitulino13548692011-07-29 15:36:43 -03001978 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001979 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001980 }
aliguori59030a82009-04-05 18:43:41 +00001981}
1982#endif
1983
Marc-André Lureau777357d2016-12-07 18:39:10 +03001984static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
1985 bool *be_opened, Error **errp)
1986{
1987 *be_opened = false;
1988}
1989
1990static void char_gdb_class_init(ObjectClass *oc, void *data)
1991{
1992 ChardevClass *cc = CHARDEV_CLASS(oc);
1993
1994 cc->internal = true;
1995 cc->open = gdb_monitor_open;
1996 cc->chr_write = gdb_monitor_write;
1997}
1998
1999#define TYPE_CHARDEV_GDB "chardev-gdb"
2000
2001static const TypeInfo char_gdb_type_info = {
2002 .name = TYPE_CHARDEV_GDB,
2003 .parent = TYPE_CHARDEV,
2004 .class_init = char_gdb_class_init,
2005};
2006
aliguori59030a82009-04-05 18:43:41 +00002007int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00002008{
Doug Gale5c9522b2017-12-02 20:30:37 -05002009 trace_gdbstub_op_start(device);
2010
pbrook4046d912007-01-28 01:53:16 +00002011 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00002012 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002013 Chardev *chr = NULL;
2014 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00002015
Ziyue Yang508b4ec2017-01-18 16:02:41 +08002016 if (!first_cpu) {
2017 error_report("gdbstub: meaningless to attach gdb to a "
2018 "machine without any CPU.");
2019 return -1;
2020 }
2021
aliguori59030a82009-04-05 18:43:41 +00002022 if (!device)
2023 return -1;
2024 if (strcmp(device, "none") != 0) {
2025 if (strstart(device, "tcp:", NULL)) {
2026 /* enforce required TCP attributes */
2027 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2028 "%s,nowait,nodelay,server", device);
2029 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00002030 }
aliguori59030a82009-04-05 18:43:41 +00002031#ifndef _WIN32
2032 else if (strcmp(device, "stdio") == 0) {
2033 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00002034
aliguori59030a82009-04-05 18:43:41 +00002035 memset(&act, 0, sizeof(act));
2036 act.sa_handler = gdb_sigterm_handler;
2037 sigaction(SIGINT, &act, NULL);
2038 }
2039#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02002040 /*
2041 * FIXME: it's a bit weird to allow using a mux chardev here
2042 * and implicitly setup a monitor. We may want to break this.
2043 */
2044 chr = qemu_chr_new_noreplay("gdb", device, true);
aliguori36556b22009-03-28 18:05:53 +00002045 if (!chr)
2046 return -1;
pbrookcfc34752007-02-22 01:48:01 +00002047 }
2048
aliguori36556b22009-03-28 18:05:53 +00002049 s = gdbserver_state;
2050 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002051 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00002052 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002053
aliguori36556b22009-03-28 18:05:53 +00002054 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2055
2056 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03002057 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
2058 NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00002059 monitor_init(mon_chr, 0);
2060 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002061 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00002062 mon_chr = s->mon_chr;
2063 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002064 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002065 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002066 s->c_cpu = first_cpu;
2067 s->g_cpu = first_cpu;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002068 if (chr) {
2069 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002070 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Anton Nefedov81517ba2017-07-06 15:08:49 +03002071 gdb_chr_event, NULL, NULL, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002072 }
aliguori36556b22009-03-28 18:05:53 +00002073 s->state = chr ? RS_IDLE : RS_INACTIVE;
2074 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002075 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002076
pbrook4046d912007-01-28 01:53:16 +00002077 return 0;
2078}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002079
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01002080void gdbserver_cleanup(void)
2081{
2082 if (gdbserver_state) {
2083 put_packet(gdbserver_state, "W00");
2084 }
2085}
2086
Marc-André Lureau777357d2016-12-07 18:39:10 +03002087static void register_types(void)
2088{
2089 type_register_static(&char_gdb_type_info);
2090}
2091
2092type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002093#endif