blob: 3c3807358c843d64cb9326c6b544890919c603f6 [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"
Paolo Bonzini33c11872016-03-15 16:58:45 +010023#include "cpu.h"
Doug Gale5c9522b2017-12-02 20:30:37 -050024#include "trace-root.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020025#ifdef CONFIG_USER_ONLY
bellard1fddef42005-04-17 19:16:13 +000026#include "qemu.h"
27#else
Paolo Bonzini83c90892012-12-17 18:19:49 +010028#include "monitor/monitor.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040029#include "chardev/char.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040030#include "chardev/char-fe.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.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
bellard858693c2004-03-31 18:52:07 +0000300enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000301 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000302 RS_IDLE,
303 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400304 RS_GETLINE_ESC,
305 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000306 RS_CHKSUM1,
307 RS_CHKSUM2,
308};
bellard858693c2004-03-31 18:52:07 +0000309typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200310 CPUState *c_cpu; /* current CPU for step/continue ops */
311 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200312 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000313 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000314 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000315 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400316 int line_sum; /* running checksum */
317 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000318 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000319 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000320 int signal;
bellard41625032005-04-24 10:07:11 +0000321#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000322 int fd;
bellard41625032005-04-24 10:07:11 +0000323 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000324#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300325 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300326 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000327#endif
Meador Ingecdb432b2012-03-15 17:49:45 +0000328 char syscall_buf[256];
329 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000330} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000331
edgar_igl60897d32008-05-09 08:25:14 +0000332/* By default use no IRQs and no timers while single stepping so as to
333 * make single stepping like an ICE HW step.
334 */
335static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
336
aliguori880a7572008-11-18 20:30:24 +0000337static GDBState *gdbserver_state;
338
Andreas Färber5b50e792013-06-29 04:18:45 +0200339bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000340
bellard1fddef42005-04-17 19:16:13 +0000341#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000342/* XXX: This is not thread safe. Do we care? */
343static int gdbserver_fd = -1;
344
bellard858693c2004-03-31 18:52:07 +0000345static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000346{
347 uint8_t ch;
348 int ret;
349
350 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000351 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000352 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000353 if (errno == ECONNRESET)
354 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200355 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000356 return -1;
357 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000358 close(s->fd);
359 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000360 return -1;
361 } else {
362 break;
363 }
364 }
365 return ch;
366}
pbrook4046d912007-01-28 01:53:16 +0000367#endif
bellardb4608c02003-06-27 17:34:32 +0000368
blueswir1654efcf2009-04-18 07:29:59 +0000369static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000370 GDB_SYS_UNKNOWN,
371 GDB_SYS_ENABLED,
372 GDB_SYS_DISABLED,
373} gdb_syscall_mode;
374
Liviu Ionescua38bb072014-12-11 12:07:48 +0000375/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000376int use_gdb_syscalls(void)
377{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100378 SemihostingTarget target = semihosting_get_target();
379 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000380 /* -semihosting-config target=native */
381 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100382 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000383 /* -semihosting-config target=gdb */
384 return true;
385 }
386
387 /* -semihosting-config target=auto */
388 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000389 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000390 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
391 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000392 }
393 return gdb_syscall_mode == GDB_SYS_ENABLED;
394}
395
edgar_iglba70a622008-03-14 06:10:42 +0000396/* Resume execution. */
397static inline void gdb_continue(GDBState *s)
398{
Doug Gale5c9522b2017-12-02 20:30:37 -0500399
edgar_iglba70a622008-03-14 06:10:42 +0000400#ifdef CONFIG_USER_ONLY
401 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500402 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000403#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200404 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500405 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200406 vm_start();
407 }
edgar_iglba70a622008-03-14 06:10:42 +0000408#endif
409}
410
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100411/*
412 * Resume execution, per CPU actions. For user-mode emulation it's
413 * equivalent to gdb_continue.
414 */
415static int gdb_continue_partial(GDBState *s, char *newstates)
416{
417 CPUState *cpu;
418 int res = 0;
419#ifdef CONFIG_USER_ONLY
420 /*
421 * This is not exactly accurate, but it's an improvement compared to the
422 * previous situation, where only one CPU would be single-stepped.
423 */
424 CPU_FOREACH(cpu) {
425 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500426 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100427 cpu_single_step(cpu, sstep_flags);
428 }
429 }
430 s->running_state = 1;
431#else
432 int flag = 0;
433
434 if (!runstate_needs_reset()) {
435 if (vm_prepare_start()) {
436 return 0;
437 }
438
439 CPU_FOREACH(cpu) {
440 switch (newstates[cpu->cpu_index]) {
441 case 0:
442 case 1:
443 break; /* nothing to do here */
444 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500445 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100446 cpu_single_step(cpu, sstep_flags);
447 cpu_resume(cpu);
448 flag = 1;
449 break;
450 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500451 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100452 cpu_resume(cpu);
453 flag = 1;
454 break;
455 default:
456 res = -1;
457 break;
458 }
459 }
460 }
461 if (flag) {
462 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
463 }
464#endif
465 return res;
466}
467
bellard858693c2004-03-31 18:52:07 +0000468static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000469{
pbrook4046d912007-01-28 01:53:16 +0000470#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000471 int ret;
472
473 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000474 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000475 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200476 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000477 return;
478 } else {
479 buf += ret;
480 len -= ret;
481 }
482 }
pbrook4046d912007-01-28 01:53:16 +0000483#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100484 /* XXX this blocks entire thread. Rewrite to use
485 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300486 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000487#endif
bellardb4608c02003-06-27 17:34:32 +0000488}
489
490static inline int fromhex(int v)
491{
492 if (v >= '0' && v <= '9')
493 return v - '0';
494 else if (v >= 'A' && v <= 'F')
495 return v - 'A' + 10;
496 else if (v >= 'a' && v <= 'f')
497 return v - 'a' + 10;
498 else
499 return 0;
500}
501
502static inline int tohex(int v)
503{
504 if (v < 10)
505 return v + '0';
506 else
507 return v - 10 + 'a';
508}
509
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300510/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000511static void memtohex(char *buf, const uint8_t *mem, int len)
512{
513 int i, c;
514 char *q;
515 q = buf;
516 for(i = 0; i < len; i++) {
517 c = mem[i];
518 *q++ = tohex(c >> 4);
519 *q++ = tohex(c & 0xf);
520 }
521 *q = '\0';
522}
523
524static void hextomem(uint8_t *mem, const char *buf, int len)
525{
526 int i;
527
528 for(i = 0; i < len; i++) {
529 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
530 buf += 2;
531 }
532}
533
Doug Gale5c9522b2017-12-02 20:30:37 -0500534static void hexdump(const char *buf, int len,
535 void (*trace_fn)(size_t ofs, char const *text))
536{
537 char line_buffer[3 * 16 + 4 + 16 + 1];
538
539 size_t i;
540 for (i = 0; i < len || (i & 0xF); ++i) {
541 size_t byte_ofs = i & 15;
542
543 if (byte_ofs == 0) {
544 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
545 line_buffer[3 * 16 + 4 + 16] = 0;
546 }
547
548 size_t col_group = (i >> 2) & 3;
549 size_t hex_col = byte_ofs * 3 + col_group;
550 size_t txt_col = 3 * 16 + 4 + byte_ofs;
551
552 if (i < len) {
553 char value = buf[i];
554
555 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
556 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
557 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
558 ? value
559 : '.';
560 }
561
562 if (byte_ofs == 0xF)
563 trace_fn(i & -16, line_buffer);
564 }
565}
566
bellardb4608c02003-06-27 17:34:32 +0000567/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500568static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000569{
pbrook56aebc82008-10-11 17:55:29 +0000570 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000571 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000572
Doug Gale5c9522b2017-12-02 20:30:37 -0500573 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
574 hexdump(buf, len, trace_gdbstub_io_binaryreply);
575 }
576
bellardb4608c02003-06-27 17:34:32 +0000577 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000578 p = s->last_packet;
579 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000580 memcpy(p, buf, len);
581 p += len;
bellardb4608c02003-06-27 17:34:32 +0000582 csum = 0;
583 for(i = 0; i < len; i++) {
584 csum += buf[i];
585 }
pbrook4046d912007-01-28 01:53:16 +0000586 *(p++) = '#';
587 *(p++) = tohex((csum >> 4) & 0xf);
588 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000589
pbrook4046d912007-01-28 01:53:16 +0000590 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000591 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000592
pbrook4046d912007-01-28 01:53:16 +0000593#ifdef CONFIG_USER_ONLY
594 i = get_char(s);
595 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000596 return -1;
pbrook4046d912007-01-28 01:53:16 +0000597 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000598 break;
pbrook4046d912007-01-28 01:53:16 +0000599#else
600 break;
601#endif
bellardb4608c02003-06-27 17:34:32 +0000602 }
603 return 0;
604}
605
pbrook56aebc82008-10-11 17:55:29 +0000606/* return -1 if error, 0 if OK */
607static int put_packet(GDBState *s, const char *buf)
608{
Doug Gale5c9522b2017-12-02 20:30:37 -0500609 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000610
Doug Gale5c9522b2017-12-02 20:30:37 -0500611 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000612}
613
pbrook56aebc82008-10-11 17:55:29 +0000614/* Encode data using the encoding for 'x' packets. */
615static int memtox(char *buf, const char *mem, int len)
616{
617 char *p = buf;
618 char c;
619
620 while (len--) {
621 c = *(mem++);
622 switch (c) {
623 case '#': case '$': case '*': case '}':
624 *(p++) = '}';
625 *(p++) = c ^ 0x20;
626 break;
627 default:
628 *(p++) = c;
629 break;
630 }
631 }
632 return p - buf;
633}
634
Andreas Färber5b24c642013-07-07 15:08:22 +0200635static const char *get_feature_xml(const char *p, const char **newp,
636 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000637{
pbrook56aebc82008-10-11 17:55:29 +0000638 size_t len;
639 int i;
640 const char *name;
641 static char target_xml[1024];
642
643 len = 0;
644 while (p[len] && p[len] != ':')
645 len++;
646 *newp = p + len;
647
648 name = NULL;
649 if (strncmp(p, "target.xml", len) == 0) {
650 /* Generate the XML description for this CPU. */
651 if (!target_xml[0]) {
652 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200653 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000654
David Hildenbrandb3820e62015-12-03 13:14:41 +0100655 pstrcat(target_xml, sizeof(target_xml),
656 "<?xml version=\"1.0\"?>"
657 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
658 "<target>");
659 if (cc->gdb_arch_name) {
660 gchar *arch = cc->gdb_arch_name(cpu);
661 pstrcat(target_xml, sizeof(target_xml), "<architecture>");
662 pstrcat(target_xml, sizeof(target_xml), arch);
663 pstrcat(target_xml, sizeof(target_xml), "</architecture>");
664 g_free(arch);
665 }
666 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
667 pstrcat(target_xml, sizeof(target_xml), cc->gdb_core_xml_file);
668 pstrcat(target_xml, sizeof(target_xml), "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200669 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000670 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
671 pstrcat(target_xml, sizeof(target_xml), r->xml);
672 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000673 }
blueswir12dc766d2009-04-13 16:06:19 +0000674 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000675 }
676 return target_xml;
677 }
678 for (i = 0; ; i++) {
679 name = xml_builtin[i][0];
680 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
681 break;
682 }
683 return name ? xml_builtin[i][1] : NULL;
684}
pbrook56aebc82008-10-11 17:55:29 +0000685
Andreas Färber385b9f02013-06-27 18:25:36 +0200686static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000687{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200688 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200689 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000690 GDBRegisterState *r;
691
Andreas Färbera0e372f2013-06-28 23:18:47 +0200692 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200693 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200694 }
pbrook56aebc82008-10-11 17:55:29 +0000695
Andreas Färbereac8b352013-06-28 21:11:37 +0200696 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000697 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
698 return r->get_reg(env, mem_buf, reg - r->base_reg);
699 }
700 }
701 return 0;
702}
703
Andreas Färber385b9f02013-06-27 18:25:36 +0200704static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000705{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200706 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200707 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000708 GDBRegisterState *r;
709
Andreas Färbera0e372f2013-06-28 23:18:47 +0200710 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200711 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200712 }
pbrook56aebc82008-10-11 17:55:29 +0000713
Andreas Färbereac8b352013-06-28 21:11:37 +0200714 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000715 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
716 return r->set_reg(env, mem_buf, reg - r->base_reg);
717 }
718 }
719 return 0;
720}
721
722/* Register a supplemental set of CPU registers. If g_pos is nonzero it
723 specifies the first register number and these registers are included in
724 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
725 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
726 */
727
Andreas Färber22169d42013-06-28 21:27:39 +0200728void gdb_register_coprocessor(CPUState *cpu,
729 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
730 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000731{
732 GDBRegisterState *s;
733 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000734
Andreas Färbereac8b352013-06-28 21:11:37 +0200735 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000736 while (*p) {
737 /* Check for duplicates. */
738 if (strcmp((*p)->xml, xml) == 0)
739 return;
740 p = &(*p)->next;
741 }
Stefan Weil9643c252011-10-18 22:25:38 +0200742
743 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200744 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200745 s->num_regs = num_regs;
746 s->get_reg = get_reg;
747 s->set_reg = set_reg;
748 s->xml = xml;
749
pbrook56aebc82008-10-11 17:55:29 +0000750 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200751 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000752 *p = s;
753 if (g_pos) {
754 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800755 error_report("Error: Bad gdb register numbering for '%s', "
756 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200757 } else {
758 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000759 }
760 }
761}
762
aliguoria1d1bb32008-11-18 20:07:32 +0000763#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100764/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
765static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
766{
767 static const int xlat[] = {
768 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
769 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
770 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
771 };
772
773 CPUClass *cc = CPU_GET_CLASS(cpu);
774 int cputype = xlat[gdbtype];
775
776 if (cc->gdb_stop_before_watchpoint) {
777 cputype |= BP_STOP_BEFORE_ACCESS;
778 }
779 return cputype;
780}
aliguoria1d1bb32008-11-18 20:07:32 +0000781#endif
782
aliguori880a7572008-11-18 20:30:24 +0000783static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000784{
Andreas Färber182735e2013-05-29 22:29:20 +0200785 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000786 int err = 0;
787
Andreas Färber62278812013-06-27 17:12:06 +0200788 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200789 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200790 }
aliguorie22a25c2009-03-12 20:12:48 +0000791
aliguoria1d1bb32008-11-18 20:07:32 +0000792 switch (type) {
793 case GDB_BREAKPOINT_SW:
794 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200795 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200796 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
797 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000798 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200799 }
aliguori880a7572008-11-18 20:30:24 +0000800 }
801 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000802#ifndef CONFIG_USER_ONLY
803 case GDB_WATCHPOINT_WRITE:
804 case GDB_WATCHPOINT_READ:
805 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200806 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100807 err = cpu_watchpoint_insert(cpu, addr, len,
808 xlat_gdb_type(cpu, type), NULL);
809 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000810 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100811 }
aliguori880a7572008-11-18 20:30:24 +0000812 }
813 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000814#endif
815 default:
816 return -ENOSYS;
817 }
818}
819
aliguori880a7572008-11-18 20:30:24 +0000820static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000821{
Andreas Färber182735e2013-05-29 22:29:20 +0200822 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000823 int err = 0;
824
Andreas Färber62278812013-06-27 17:12:06 +0200825 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200826 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200827 }
aliguorie22a25c2009-03-12 20:12:48 +0000828
aliguoria1d1bb32008-11-18 20:07:32 +0000829 switch (type) {
830 case GDB_BREAKPOINT_SW:
831 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200832 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200833 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
834 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000835 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200836 }
aliguori880a7572008-11-18 20:30:24 +0000837 }
838 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000839#ifndef CONFIG_USER_ONLY
840 case GDB_WATCHPOINT_WRITE:
841 case GDB_WATCHPOINT_READ:
842 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200843 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100844 err = cpu_watchpoint_remove(cpu, addr, len,
845 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +0000846 if (err)
847 break;
848 }
849 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000850#endif
851 default:
852 return -ENOSYS;
853 }
854}
855
aliguori880a7572008-11-18 20:30:24 +0000856static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000857{
Andreas Färber182735e2013-05-29 22:29:20 +0200858 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000859
aliguorie22a25c2009-03-12 20:12:48 +0000860 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200861 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000862 return;
863 }
864
Andreas Färberbdc44642013-06-24 23:50:24 +0200865 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200866 cpu_breakpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000867#ifndef CONFIG_USER_ONLY
Andreas Färber75a34032013-09-02 16:57:02 +0200868 cpu_watchpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000869#endif
aliguori880a7572008-11-18 20:30:24 +0000870 }
aliguoria1d1bb32008-11-18 20:07:32 +0000871}
872
aurel32fab9d282009-04-08 21:29:37 +0000873static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
874{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200875 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200876
877 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -0700878 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +0000879}
880
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200881static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700882{
Andreas Färber0d342822012-12-17 07:12:13 +0100883 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700884
Andreas Färberbdc44642013-06-24 23:50:24 +0200885 CPU_FOREACH(cpu) {
Alex Bennéed2a6c852017-07-12 11:52:14 +0100886 if (cpu_gdb_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200887 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200888 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700889 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200890
891 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700892}
893
Jan Kiszka4dabe742015-02-07 09:38:43 +0100894static int is_query_packet(const char *p, const char *query, char separator)
895{
896 unsigned int query_len = strlen(query);
897
898 return strncmp(p, query, query_len) == 0 &&
899 (p[query_len] == '\0' || p[query_len] == separator);
900}
901
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100902/**
903 * gdb_handle_vcont - Parses and handles a vCont packet.
904 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
905 * a format error, 0 on success.
906 */
907static int gdb_handle_vcont(GDBState *s, const char *p)
908{
909 int res, idx, signal = 0;
910 char cur_action;
911 char *newstates;
912 unsigned long tmp;
913 CPUState *cpu;
914#ifdef CONFIG_USER_ONLY
915 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
916
917 CPU_FOREACH(cpu) {
918 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
919 }
920#endif
921 /* uninitialised CPUs stay 0 */
922 newstates = g_new0(char, max_cpus);
923
924 /* mark valid CPUs with 1 */
925 CPU_FOREACH(cpu) {
926 newstates[cpu->cpu_index] = 1;
927 }
928
929 /*
930 * res keeps track of what error we are returning, with -ENOTSUP meaning
931 * that the command is unknown or unsupported, thus returning an empty
932 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
933 * or incorrect parameters passed.
934 */
935 res = 0;
936 while (*p) {
937 if (*p++ != ';') {
938 res = -ENOTSUP;
939 goto out;
940 }
941
942 cur_action = *p++;
943 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +0100944 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100945 res = qemu_strtoul(p + 1, &p, 16, &tmp);
946 if (res) {
947 goto out;
948 }
949 signal = gdb_signal_to_target(tmp);
950 } else if (cur_action != 'c' && cur_action != 's') {
951 /* unknown/invalid/unsupported command */
952 res = -ENOTSUP;
953 goto out;
954 }
955 /* thread specification. special values: (none), -1 = all; 0 = any */
956 if ((p[0] == ':' && p[1] == '-' && p[2] == '1') || (p[0] != ':')) {
957 if (*p == ':') {
958 p += 3;
959 }
960 for (idx = 0; idx < max_cpus; idx++) {
961 if (newstates[idx] == 1) {
962 newstates[idx] = cur_action;
963 }
964 }
965 } else if (*p == ':') {
966 p++;
967 res = qemu_strtoul(p, &p, 16, &tmp);
968 if (res) {
969 goto out;
970 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100971
Alex Bennée5a6a1ad2017-07-12 11:52:16 +0100972 /* 0 means any thread, so we pick the first valid CPU */
973 cpu = tmp ? find_cpu(tmp) : first_cpu;
974
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100975 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +0100976 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100977 res = -EINVAL;
978 goto out;
979 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +0100980
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100981 /* only use if no previous match occourred */
982 if (newstates[cpu->cpu_index] == 1) {
983 newstates[cpu->cpu_index] = cur_action;
984 }
985 }
986 }
987 s->signal = signal;
988 gdb_continue_partial(s, newstates);
989
990out:
991 g_free(newstates);
992
993 return res;
994}
995
aliguori880a7572008-11-18 20:30:24 +0000996static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000997{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200998 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +0200999 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +00001000 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001001 uint32_t thread;
1002 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +00001003 uint8_t mem_buf[MAX_PACKET_LENGTH];
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -03001004 char buf[sizeof(mem_buf) + 1 /* trailing NUL */];
pbrook56aebc82008-10-11 17:55:29 +00001005 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001006 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +00001007
Doug Gale5c9522b2017-12-02 20:30:37 -05001008 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01001009
bellard858693c2004-03-31 18:52:07 +00001010 p = line_buf;
1011 ch = *p++;
1012 switch(ch) {
1013 case '?':
bellard1fddef42005-04-17 19:16:13 +00001014 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +00001015 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Alex Bennéed2a6c852017-07-12 11:52:14 +01001016 cpu_gdb_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +00001017 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001018 /* Remove all the breakpoints when this query is issued,
1019 * because gdb is doing and initial connect and the state
1020 * should be cleaned up.
1021 */
aliguori880a7572008-11-18 20:30:24 +00001022 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001023 break;
1024 case 'c':
1025 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001026 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001027 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001028 }
aurel32ca587a82008-12-18 22:44:13 +00001029 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001030 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001031 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001032 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001033 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1034 if (s->signal == -1)
1035 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001036 gdb_continue(s);
1037 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001038 case 'v':
1039 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001040 p += 4;
1041 if (*p == '?') {
1042 put_packet(s, "vCont;c;C;s;S");
1043 break;
1044 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001045
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001046 res = gdb_handle_vcont(s, p);
1047
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001048 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001049 if ((res == -EINVAL) || (res == -ERANGE)) {
1050 put_packet(s, "E22");
1051 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001052 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001053 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001054 }
1055 break;
1056 } else {
1057 goto unknown_command;
1058 }
edgar_igl7d03f822008-05-17 18:58:29 +00001059 case 'k':
1060 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001061 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001062 exit(0);
1063 case 'D':
1064 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +00001065 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -03001066 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +00001067 gdb_continue(s);
1068 put_packet(s, "OK");
1069 break;
bellard858693c2004-03-31 18:52:07 +00001070 case 's':
1071 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001072 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001073 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001074 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001075 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001076 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001077 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001078 case 'F':
1079 {
1080 target_ulong ret;
1081 target_ulong err;
1082
1083 ret = strtoull(p, (char **)&p, 16);
1084 if (*p == ',') {
1085 p++;
1086 err = strtoull(p, (char **)&p, 16);
1087 } else {
1088 err = 0;
1089 }
1090 if (*p == ',')
1091 p++;
1092 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001093 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001094 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001095 s->current_syscall_cb = NULL;
1096 }
pbrooka2d1eba2007-01-28 03:10:55 +00001097 if (type == 'C') {
1098 put_packet(s, "T02");
1099 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001100 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001101 }
1102 }
1103 break;
bellard858693c2004-03-31 18:52:07 +00001104 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001105 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001106 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001107 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001108 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001109 len += reg_size;
1110 }
1111 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001112 put_packet(s, buf);
1113 break;
1114 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001115 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001116 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001117 len = strlen(p) / 2;
1118 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001119 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001120 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001121 len -= reg_size;
1122 registers += reg_size;
1123 }
bellard858693c2004-03-31 18:52:07 +00001124 put_packet(s, "OK");
1125 break;
1126 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001127 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001128 if (*p == ',')
1129 p++;
bellard9d9754a2006-06-25 15:32:37 +00001130 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001131
1132 /* memtohex() doubles the required space */
1133 if (len > MAX_PACKET_LENGTH / 2) {
1134 put_packet (s, "E22");
1135 break;
1136 }
1137
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001138 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001139 put_packet (s, "E14");
1140 } else {
1141 memtohex(buf, mem_buf, len);
1142 put_packet(s, buf);
1143 }
bellard858693c2004-03-31 18:52:07 +00001144 break;
1145 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001146 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001147 if (*p == ',')
1148 p++;
bellard9d9754a2006-06-25 15:32:37 +00001149 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001150 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001151 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001152
1153 /* hextomem() reads 2*len bytes */
1154 if (len > strlen(p) / 2) {
1155 put_packet (s, "E22");
1156 break;
1157 }
bellard858693c2004-03-31 18:52:07 +00001158 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001159 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001160 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001161 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001162 } else {
bellard858693c2004-03-31 18:52:07 +00001163 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001164 }
bellard858693c2004-03-31 18:52:07 +00001165 break;
pbrook56aebc82008-10-11 17:55:29 +00001166 case 'p':
1167 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1168 This works, but can be very slow. Anything new enough to
1169 understand XML also knows how to use this properly. */
1170 if (!gdb_has_xml)
1171 goto unknown_command;
1172 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001173 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001174 if (reg_size) {
1175 memtohex(buf, mem_buf, reg_size);
1176 put_packet(s, buf);
1177 } else {
1178 put_packet(s, "E14");
1179 }
1180 break;
1181 case 'P':
1182 if (!gdb_has_xml)
1183 goto unknown_command;
1184 addr = strtoull(p, (char **)&p, 16);
1185 if (*p == '=')
1186 p++;
1187 reg_size = strlen(p) / 2;
1188 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001189 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001190 put_packet(s, "OK");
1191 break;
bellard858693c2004-03-31 18:52:07 +00001192 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001193 case 'z':
1194 type = strtoul(p, (char **)&p, 16);
1195 if (*p == ',')
1196 p++;
bellard9d9754a2006-06-25 15:32:37 +00001197 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001198 if (*p == ',')
1199 p++;
bellard9d9754a2006-06-25 15:32:37 +00001200 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001201 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001202 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001203 else
aliguori880a7572008-11-18 20:30:24 +00001204 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001205 if (res >= 0)
1206 put_packet(s, "OK");
1207 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001208 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001209 else
1210 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001211 break;
aliguori880a7572008-11-18 20:30:24 +00001212 case 'H':
1213 type = *p++;
1214 thread = strtoull(p, (char **)&p, 16);
1215 if (thread == -1 || thread == 0) {
1216 put_packet(s, "OK");
1217 break;
1218 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001219 cpu = find_cpu(thread);
1220 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001221 put_packet(s, "E22");
1222 break;
1223 }
1224 switch (type) {
1225 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001226 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001227 put_packet(s, "OK");
1228 break;
1229 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001230 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001231 put_packet(s, "OK");
1232 break;
1233 default:
1234 put_packet(s, "E22");
1235 break;
1236 }
1237 break;
1238 case 'T':
1239 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001240 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001241
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001242 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001243 put_packet(s, "OK");
1244 } else {
aliguori880a7572008-11-18 20:30:24 +00001245 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001246 }
aliguori880a7572008-11-18 20:30:24 +00001247 break;
pbrook978efd62006-06-17 18:30:42 +00001248 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001249 case 'Q':
1250 /* parse any 'q' packets here */
1251 if (!strcmp(p,"qemu.sstepbits")) {
1252 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001253 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1254 SSTEP_ENABLE,
1255 SSTEP_NOIRQ,
1256 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001257 put_packet(s, buf);
1258 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001259 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001260 /* Display or change the sstep_flags */
1261 p += 10;
1262 if (*p != '=') {
1263 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001264 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001265 put_packet(s, buf);
1266 break;
1267 }
1268 p++;
1269 type = strtoul(p, (char **)&p, 16);
1270 sstep_flags = type;
1271 put_packet(s, "OK");
1272 break;
aliguori880a7572008-11-18 20:30:24 +00001273 } else if (strcmp(p,"C") == 0) {
1274 /* "Current thread" remains vague in the spec, so always return
1275 * the first CPU (gdb returns the first thread). */
1276 put_packet(s, "QC1");
1277 break;
1278 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001279 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001280 goto report_cpuinfo;
1281 } else if (strcmp(p,"sThreadInfo") == 0) {
1282 report_cpuinfo:
1283 if (s->query_cpu) {
Alex Bennéed2a6c852017-07-12 11:52:14 +01001284 snprintf(buf, sizeof(buf), "m%x", cpu_gdb_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001285 put_packet(s, buf);
Andreas Färberbdc44642013-06-24 23:50:24 +02001286 s->query_cpu = CPU_NEXT(s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001287 } else
1288 put_packet(s, "l");
1289 break;
1290 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1291 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001292 cpu = find_cpu(thread);
1293 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001294 cpu_synchronize_state(cpu);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001295 /* memtohex() doubles the required space */
1296 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
Andreas Färber55e5c282012-12-17 06:18:02 +01001297 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001298 cpu->halted ? "halted " : "running");
Doug Gale5c9522b2017-12-02 20:30:37 -05001299 trace_gdbstub_op_extra_info((char *)mem_buf);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001300 memtohex(buf, mem_buf, len);
1301 put_packet(s, buf);
1302 }
aliguori880a7572008-11-18 20:30:24 +00001303 break;
edgar_igl60897d32008-05-09 08:25:14 +00001304 }
blueswir10b8a9882009-03-07 10:51:36 +00001305#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001306 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001307 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001308
blueswir1363a37d2008-08-21 17:58:08 +00001309 snprintf(buf, sizeof(buf),
1310 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1311 ";Bss=" TARGET_ABI_FMT_lx,
1312 ts->info->code_offset,
1313 ts->info->data_offset,
1314 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001315 put_packet(s, buf);
1316 break;
1317 }
blueswir10b8a9882009-03-07 10:51:36 +00001318#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001319 else if (strncmp(p, "Rcmd,", 5) == 0) {
1320 int len = strlen(p + 5);
1321
1322 if ((len % 2) != 0) {
1323 put_packet(s, "E01");
1324 break;
1325 }
aliguori8a34a0f2009-03-05 23:01:55 +00001326 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001327 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001328 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001329 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001330 put_packet(s, "OK");
1331 break;
1332 }
blueswir10b8a9882009-03-07 10:51:36 +00001333#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001334 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001335 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001336 cc = CPU_GET_CLASS(first_cpu);
1337 if (cc->gdb_core_xml_file != NULL) {
1338 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1339 }
pbrook56aebc82008-10-11 17:55:29 +00001340 put_packet(s, buf);
1341 break;
1342 }
pbrook56aebc82008-10-11 17:55:29 +00001343 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1344 const char *xml;
1345 target_ulong total_len;
1346
Andreas Färber5b24c642013-07-07 15:08:22 +02001347 cc = CPU_GET_CLASS(first_cpu);
1348 if (cc->gdb_core_xml_file == NULL) {
1349 goto unknown_command;
1350 }
1351
Andreas Färber5b50e792013-06-29 04:18:45 +02001352 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001353 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001354 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001355 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001356 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001357 put_packet(s, buf);
1358 break;
1359 }
1360
1361 if (*p == ':')
1362 p++;
1363 addr = strtoul(p, (char **)&p, 16);
1364 if (*p == ',')
1365 p++;
1366 len = strtoul(p, (char **)&p, 16);
1367
1368 total_len = strlen(xml);
1369 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001370 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001371 put_packet(s, buf);
1372 break;
1373 }
1374 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1375 len = (MAX_PACKET_LENGTH - 5) / 2;
1376 if (len < total_len - addr) {
1377 buf[0] = 'm';
1378 len = memtox(buf + 1, xml + addr, len);
1379 } else {
1380 buf[0] = 'l';
1381 len = memtox(buf + 1, xml + addr, total_len - addr);
1382 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001383 put_packet_binary(s, buf, len + 1, true);
pbrook56aebc82008-10-11 17:55:29 +00001384 break;
1385 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001386 if (is_query_packet(p, "Attached", ':')) {
1387 put_packet(s, GDB_ATTACHED);
1388 break;
1389 }
pbrook56aebc82008-10-11 17:55:29 +00001390 /* Unrecognised 'q' command. */
1391 goto unknown_command;
1392
bellard858693c2004-03-31 18:52:07 +00001393 default:
pbrook56aebc82008-10-11 17:55:29 +00001394 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001395 /* put empty packet */
1396 buf[0] = '\0';
1397 put_packet(s, buf);
1398 break;
1399 }
1400 return RS_IDLE;
1401}
1402
Andreas Färber64f6b342013-05-27 02:06:09 +02001403void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001404{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001405 gdbserver_state->c_cpu = cpu;
1406 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001407}
1408
bellard1fddef42005-04-17 19:16:13 +00001409#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001410static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001411{
aliguori880a7572008-11-18 20:30:24 +00001412 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001413 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001414 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001415 const char *type;
bellard858693c2004-03-31 18:52:07 +00001416 int ret;
1417
Meador Ingecdb432b2012-03-15 17:49:45 +00001418 if (running || s->state == RS_INACTIVE) {
1419 return;
1420 }
1421 /* Is there a GDB syscall waiting to be sent? */
1422 if (s->current_syscall_cb) {
1423 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001424 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001425 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001426 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001427 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001428 if (cpu->watchpoint_hit) {
1429 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001430 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001431 type = "r";
1432 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001433 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001434 type = "a";
1435 break;
1436 default:
1437 type = "";
1438 break;
1439 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001440 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
1441 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00001442 snprintf(buf, sizeof(buf),
1443 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Alex Bennéed2a6c852017-07-12 11:52:14 +01001444 GDB_SIGNAL_TRAP, cpu_gdb_index(cpu), type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001445 (target_ulong)cpu->watchpoint_hit->vaddr);
1446 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001447 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05001448 } else {
1449 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00001450 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001451 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001452 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001453 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001454 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05001455 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00001456 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001457 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001458 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05001459 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01001460 ret = GDB_SIGNAL_QUIT;
1461 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001462 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001463 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001464 ret = GDB_SIGNAL_IO;
1465 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001466 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05001467 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01001468 ret = GDB_SIGNAL_ALRM;
1469 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001470 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001471 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001472 ret = GDB_SIGNAL_ABRT;
1473 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001474 case RUN_STATE_SAVE_VM:
1475 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001476 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001477 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001478 ret = GDB_SIGNAL_XCPU;
1479 break;
1480 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05001481 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01001482 ret = GDB_SIGNAL_UNKNOWN;
1483 break;
bellardbbeb7b52006-04-23 18:42:15 +00001484 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001485 gdb_set_stop_cpu(cpu);
Alex Bennéed2a6c852017-07-12 11:52:14 +01001486 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_gdb_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001487
1488send_packet:
bellard858693c2004-03-31 18:52:07 +00001489 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001490
1491 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001492 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001493}
bellard1fddef42005-04-17 19:16:13 +00001494#endif
bellard858693c2004-03-31 18:52:07 +00001495
pbrooka2d1eba2007-01-28 03:10:55 +00001496/* Send a gdb syscall request.
1497 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001498 %x - target_ulong argument printed in hex.
1499 %lx - 64-bit argument printed in hex.
1500 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001501void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001502{
pbrooka2d1eba2007-01-28 03:10:55 +00001503 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001504 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001505 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001506 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001507 GDBState *s;
1508
aliguori880a7572008-11-18 20:30:24 +00001509 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001510 if (!s)
1511 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001512 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001513#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001514 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001515#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001516 p = s->syscall_buf;
1517 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001518 *(p++) = 'F';
1519 while (*fmt) {
1520 if (*fmt == '%') {
1521 fmt++;
1522 switch (*fmt++) {
1523 case 'x':
1524 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001525 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001526 break;
pbrooka87295e2007-05-26 15:09:38 +00001527 case 'l':
1528 if (*(fmt++) != 'x')
1529 goto bad_format;
1530 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001531 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001532 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001533 case 's':
1534 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001535 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001536 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001537 break;
1538 default:
pbrooka87295e2007-05-26 15:09:38 +00001539 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001540 error_report("gdbstub: Bad syscall format string '%s'",
1541 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001542 break;
1543 }
1544 } else {
1545 *(p++) = *(fmt++);
1546 }
1547 }
pbrook8a93e022007-08-06 13:19:15 +00001548 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001549#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001550 put_packet(s, s->syscall_buf);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001551 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001552#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001553 /* In this case wait to send the syscall packet until notification that
1554 the CPU has stopped. This must be done because if the packet is sent
1555 now the reply from the syscall request could be received while the CPU
1556 is still in the running state, which can cause packets to be dropped
1557 and state transition 'T' packets to be sent while the syscall is still
1558 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001559 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001560#endif
1561}
1562
Peter Maydell19239b32015-09-07 10:39:27 +01001563void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1564{
1565 va_list va;
1566
1567 va_start(va, fmt);
1568 gdb_do_syscallv(cb, fmt, va);
1569 va_end(va);
1570}
1571
bellard6a00d602005-11-21 23:25:50 +00001572static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001573{
ths60fe76f2007-12-16 03:02:09 +00001574 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001575
bellard1fddef42005-04-17 19:16:13 +00001576#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001577 if (s->last_packet_len) {
1578 /* Waiting for a response to the last packet. If we see the start
1579 of a new command then abandon the previous response. */
1580 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05001581 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00001582 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01001583 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05001584 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01001585 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05001586 trace_gdbstub_io_got_unexpected((uint8_t)ch);
pbrook4046d912007-01-28 01:53:16 +00001587 }
Alex Bennée118e2262017-07-12 11:52:13 +01001588
pbrook4046d912007-01-28 01:53:16 +00001589 if (ch == '+' || ch == '$')
1590 s->last_packet_len = 0;
1591 if (ch != '$')
1592 return;
1593 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001594 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001595 /* when the CPU is running, we cannot do anything except stop
1596 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001597 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001598 } else
bellard1fddef42005-04-17 19:16:13 +00001599#endif
bellard41625032005-04-24 10:07:11 +00001600 {
bellard858693c2004-03-31 18:52:07 +00001601 switch(s->state) {
1602 case RS_IDLE:
1603 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04001604 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00001605 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04001606 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00001607 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04001608 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05001609 trace_gdbstub_err_garbage((uint8_t)ch);
bellard4c3a88a2003-07-26 12:06:08 +00001610 }
1611 break;
bellard858693c2004-03-31 18:52:07 +00001612 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04001613 if (ch == '}') {
1614 /* start escape sequence */
1615 s->state = RS_GETLINE_ESC;
1616 s->line_sum += ch;
1617 } else if (ch == '*') {
1618 /* start run length encoding sequence */
1619 s->state = RS_GETLINE_RLE;
1620 s->line_sum += ch;
1621 } else if (ch == '#') {
1622 /* end of command, start of checksum*/
1623 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00001624 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001625 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00001626 s->state = RS_IDLE;
1627 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001628 /* unescaped command character */
1629 s->line_buf[s->line_buf_index++] = ch;
1630 s->line_sum += ch;
1631 }
1632 break;
1633 case RS_GETLINE_ESC:
1634 if (ch == '#') {
1635 /* unexpected end of command in escape sequence */
1636 s->state = RS_CHKSUM1;
1637 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1638 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05001639 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04001640 s->state = RS_IDLE;
1641 } else {
1642 /* parse escaped character and leave escape state */
1643 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
1644 s->line_sum += ch;
1645 s->state = RS_GETLINE;
1646 }
1647 break;
1648 case RS_GETLINE_RLE:
1649 if (ch < ' ') {
1650 /* invalid RLE count encoding */
Doug Gale5c9522b2017-12-02 20:30:37 -05001651 trace_gdbstub_err_invalid_repeat((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001652 s->state = RS_GETLINE;
1653 } else {
1654 /* decode repeat length */
1655 int repeat = (unsigned char)ch - ' ' + 3;
1656 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
1657 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05001658 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04001659 s->state = RS_IDLE;
1660 } else if (s->line_buf_index < 1) {
1661 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05001662 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04001663 s->state = RS_GETLINE;
1664 } else {
1665 /* repeat the last character */
1666 memset(s->line_buf + s->line_buf_index,
1667 s->line_buf[s->line_buf_index - 1], repeat);
1668 s->line_buf_index += repeat;
1669 s->line_sum += ch;
1670 s->state = RS_GETLINE;
1671 }
bellard858693c2004-03-31 18:52:07 +00001672 }
1673 break;
1674 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04001675 /* get high hex digit of checksum */
1676 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001677 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001678 s->state = RS_GETLINE;
1679 break;
1680 }
bellard858693c2004-03-31 18:52:07 +00001681 s->line_buf[s->line_buf_index] = '\0';
1682 s->line_csum = fromhex(ch) << 4;
1683 s->state = RS_CHKSUM2;
1684 break;
1685 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04001686 /* get low hex digit of checksum */
1687 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001688 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001689 s->state = RS_GETLINE;
1690 break;
bellard858693c2004-03-31 18:52:07 +00001691 }
Doug Gale4bf43122017-05-01 12:22:10 -04001692 s->line_csum |= fromhex(ch);
1693
1694 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001695 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04001696 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00001697 reply = '-';
1698 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00001699 s->state = RS_IDLE;
1700 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001701 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00001702 reply = '+';
1703 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001704 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001705 }
bellardb4608c02003-06-27 17:34:32 +00001706 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001707 default:
1708 abort();
bellardb4608c02003-06-27 17:34:32 +00001709 }
1710 }
bellard858693c2004-03-31 18:52:07 +00001711}
1712
Paul Brook0e1c9c52010-06-16 13:03:51 +01001713/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001714void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001715{
1716 GDBState *s;
1717 char buf[4];
1718
1719 s = gdbserver_state;
1720 if (!s) {
1721 return;
1722 }
1723#ifdef CONFIG_USER_ONLY
1724 if (gdbserver_fd < 0 || s->fd < 0) {
1725 return;
1726 }
1727#endif
1728
Doug Gale5c9522b2017-12-02 20:30:37 -05001729 trace_gdbstub_op_exiting((uint8_t)code);
1730
Paul Brook0e1c9c52010-06-16 13:03:51 +01001731 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1732 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001733
1734#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04001735 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001736#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001737}
1738
bellard1fddef42005-04-17 19:16:13 +00001739#ifdef CONFIG_USER_ONLY
1740int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001741gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001742{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001743 GDBState *s;
1744 char buf[256];
1745 int n;
bellard1fddef42005-04-17 19:16:13 +00001746
Andreas Färber5ca666c2013-06-24 19:20:57 +02001747 s = gdbserver_state;
1748 if (gdbserver_fd < 0 || s->fd < 0) {
1749 return sig;
bellard1fddef42005-04-17 19:16:13 +00001750 }
1751
Andreas Färber5ca666c2013-06-24 19:20:57 +02001752 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001753 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001754 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00001755
Andreas Färber5ca666c2013-06-24 19:20:57 +02001756 if (sig != 0) {
1757 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1758 put_packet(s, buf);
1759 }
1760 /* put_packet() might have detected that the peer terminated the
1761 connection. */
1762 if (s->fd < 0) {
1763 return sig;
1764 }
1765
1766 sig = 0;
1767 s->state = RS_IDLE;
1768 s->running_state = 0;
1769 while (s->running_state == 0) {
1770 n = read(s->fd, buf, 256);
1771 if (n > 0) {
1772 int i;
1773
1774 for (i = 0; i < n; i++) {
1775 gdb_read_byte(s, buf[i]);
1776 }
Peter Wu5819e3e2016-06-05 16:35:48 +02001777 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02001778 /* XXX: Connection closed. Should probably wait for another
1779 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02001780 if (n == 0) {
1781 close(s->fd);
1782 }
1783 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001784 return sig;
bellard1fddef42005-04-17 19:16:13 +00001785 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001786 }
1787 sig = s->signal;
1788 s->signal = 0;
1789 return sig;
bellard1fddef42005-04-17 19:16:13 +00001790}
bellarde9009672005-04-26 20:42:36 +00001791
aurel32ca587a82008-12-18 22:44:13 +00001792/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001793void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001794{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001795 GDBState *s;
1796 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001797
Andreas Färber5ca666c2013-06-24 19:20:57 +02001798 s = gdbserver_state;
1799 if (gdbserver_fd < 0 || s->fd < 0) {
1800 return;
1801 }
aurel32ca587a82008-12-18 22:44:13 +00001802
Andreas Färber5ca666c2013-06-24 19:20:57 +02001803 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1804 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001805}
bellard1fddef42005-04-17 19:16:13 +00001806
aliguori880a7572008-11-18 20:30:24 +00001807static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001808{
1809 GDBState *s;
1810 struct sockaddr_in sockaddr;
1811 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001812 int fd;
bellard858693c2004-03-31 18:52:07 +00001813
1814 for(;;) {
1815 len = sizeof(sockaddr);
1816 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1817 if (fd < 0 && errno != EINTR) {
1818 perror("accept");
1819 return;
1820 } else if (fd >= 0) {
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001821#ifndef _WIN32
1822 fcntl(fd, F_SETFD, FD_CLOEXEC);
1823#endif
bellard858693c2004-03-31 18:52:07 +00001824 break;
1825 }
1826 }
1827
1828 /* set short latency */
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001829 socket_set_nodelay(fd);
ths3b46e622007-09-17 08:09:54 +00001830
Anthony Liguori7267c092011-08-20 22:09:37 -05001831 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001832 s->c_cpu = first_cpu;
1833 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001834 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02001835 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00001836
aliguori880a7572008-11-18 20:30:24 +00001837 gdbserver_state = s;
bellard858693c2004-03-31 18:52:07 +00001838}
1839
1840static int gdbserver_open(int port)
1841{
1842 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001843 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00001844
1845 fd = socket(PF_INET, SOCK_STREAM, 0);
1846 if (fd < 0) {
1847 perror("socket");
1848 return -1;
1849 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001850#ifndef _WIN32
1851 fcntl(fd, F_SETFD, FD_CLOEXEC);
1852#endif
bellard858693c2004-03-31 18:52:07 +00001853
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001854 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00001855
1856 sockaddr.sin_family = AF_INET;
1857 sockaddr.sin_port = htons(port);
1858 sockaddr.sin_addr.s_addr = 0;
1859 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1860 if (ret < 0) {
1861 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001862 close(fd);
bellard858693c2004-03-31 18:52:07 +00001863 return -1;
1864 }
Peter Wu96165b92016-05-04 11:32:17 +02001865 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00001866 if (ret < 0) {
1867 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001868 close(fd);
bellard858693c2004-03-31 18:52:07 +00001869 return -1;
1870 }
bellard858693c2004-03-31 18:52:07 +00001871 return fd;
1872}
1873
1874int gdbserver_start(int port)
1875{
1876 gdbserver_fd = gdbserver_open(port);
1877 if (gdbserver_fd < 0)
1878 return -1;
1879 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00001880 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00001881 return 0;
1882}
aurel322b1319c2008-12-18 22:44:04 +00001883
1884/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07001885void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00001886{
1887 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02001888
1889 if (gdbserver_fd < 0 || s->fd < 0) {
1890 return;
1891 }
aurel322b1319c2008-12-18 22:44:04 +00001892 close(s->fd);
1893 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001894 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02001895 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00001896}
pbrook4046d912007-01-28 01:53:16 +00001897#else
thsaa1f17c2007-07-11 22:48:58 +00001898static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001899{
pbrook56aebc82008-10-11 17:55:29 +00001900 /* We can handle an arbitrarily large amount of data.
1901 Pick the maximum packet size, which is as good as anything. */
1902 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001903}
1904
thsaa1f17c2007-07-11 22:48:58 +00001905static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001906{
pbrook4046d912007-01-28 01:53:16 +00001907 int i;
1908
1909 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001910 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001911 }
1912}
1913
1914static void gdb_chr_event(void *opaque, int event)
1915{
1916 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301917 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001918 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02001919 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00001920 break;
1921 default:
1922 break;
1923 }
1924}
1925
aliguori8a34a0f2009-03-05 23:01:55 +00001926static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1927{
1928 char buf[MAX_PACKET_LENGTH];
1929
1930 buf[0] = 'O';
1931 if (len > (MAX_PACKET_LENGTH/2) - 1)
1932 len = (MAX_PACKET_LENGTH/2) - 1;
1933 memtohex(buf + 1, (uint8_t *)msg, len);
1934 put_packet(s, buf);
1935}
1936
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001937static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00001938{
1939 const char *p = (const char *)buf;
1940 int max_sz;
1941
1942 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1943 for (;;) {
1944 if (len <= max_sz) {
1945 gdb_monitor_output(gdbserver_state, p, len);
1946 break;
1947 }
1948 gdb_monitor_output(gdbserver_state, p, max_sz);
1949 p += max_sz;
1950 len -= max_sz;
1951 }
1952 return len;
1953}
1954
aliguori59030a82009-04-05 18:43:41 +00001955#ifndef _WIN32
1956static void gdb_sigterm_handler(int signal)
1957{
Luiz Capitulino13548692011-07-29 15:36:43 -03001958 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001959 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001960 }
aliguori59030a82009-04-05 18:43:41 +00001961}
1962#endif
1963
Marc-André Lureau777357d2016-12-07 18:39:10 +03001964static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
1965 bool *be_opened, Error **errp)
1966{
1967 *be_opened = false;
1968}
1969
1970static void char_gdb_class_init(ObjectClass *oc, void *data)
1971{
1972 ChardevClass *cc = CHARDEV_CLASS(oc);
1973
1974 cc->internal = true;
1975 cc->open = gdb_monitor_open;
1976 cc->chr_write = gdb_monitor_write;
1977}
1978
1979#define TYPE_CHARDEV_GDB "chardev-gdb"
1980
1981static const TypeInfo char_gdb_type_info = {
1982 .name = TYPE_CHARDEV_GDB,
1983 .parent = TYPE_CHARDEV,
1984 .class_init = char_gdb_class_init,
1985};
1986
aliguori59030a82009-04-05 18:43:41 +00001987int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00001988{
Doug Gale5c9522b2017-12-02 20:30:37 -05001989 trace_gdbstub_op_start(device);
1990
pbrook4046d912007-01-28 01:53:16 +00001991 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00001992 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001993 Chardev *chr = NULL;
1994 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00001995
Ziyue Yang508b4ec2017-01-18 16:02:41 +08001996 if (!first_cpu) {
1997 error_report("gdbstub: meaningless to attach gdb to a "
1998 "machine without any CPU.");
1999 return -1;
2000 }
2001
aliguori59030a82009-04-05 18:43:41 +00002002 if (!device)
2003 return -1;
2004 if (strcmp(device, "none") != 0) {
2005 if (strstart(device, "tcp:", NULL)) {
2006 /* enforce required TCP attributes */
2007 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2008 "%s,nowait,nodelay,server", device);
2009 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00002010 }
aliguori59030a82009-04-05 18:43:41 +00002011#ifndef _WIN32
2012 else if (strcmp(device, "stdio") == 0) {
2013 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00002014
aliguori59030a82009-04-05 18:43:41 +00002015 memset(&act, 0, sizeof(act));
2016 act.sa_handler = gdb_sigterm_handler;
2017 sigaction(SIGINT, &act, NULL);
2018 }
2019#endif
Marc-André Lureaub4948be2016-10-22 12:52:46 +03002020 chr = qemu_chr_new_noreplay("gdb", device);
aliguori36556b22009-03-28 18:05:53 +00002021 if (!chr)
2022 return -1;
pbrookcfc34752007-02-22 01:48:01 +00002023 }
2024
aliguori36556b22009-03-28 18:05:53 +00002025 s = gdbserver_state;
2026 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002027 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00002028 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002029
aliguori36556b22009-03-28 18:05:53 +00002030 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2031
2032 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03002033 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
2034 NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00002035 monitor_init(mon_chr, 0);
2036 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002037 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00002038 mon_chr = s->mon_chr;
2039 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002040 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002041 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002042 s->c_cpu = first_cpu;
2043 s->g_cpu = first_cpu;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002044 if (chr) {
2045 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002046 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Anton Nefedov81517ba2017-07-06 15:08:49 +03002047 gdb_chr_event, NULL, NULL, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002048 }
aliguori36556b22009-03-28 18:05:53 +00002049 s->state = chr ? RS_IDLE : RS_INACTIVE;
2050 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002051 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002052
pbrook4046d912007-01-28 01:53:16 +00002053 return 0;
2054}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002055
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01002056void gdbserver_cleanup(void)
2057{
2058 if (gdbserver_state) {
2059 put_packet(gdbserver_state, "W00");
2060 }
2061}
2062
Marc-André Lureau777357d2016-12-07 18:39:10 +03002063static void register_types(void)
2064{
2065 type_register_static(&char_gdb_type_info);
2066}
2067
2068type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002069#endif