blob: 86eed4f97c72481798ea5c6579ac6278b44c8976 [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"
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"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020028#include "sysemu/char.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010029#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010030#include "exec/gdbstub.h"
bellard1fddef42005-04-17 19:16:13 +000031#endif
bellard67b915a2004-03-31 23:37:16 +000032
pbrook56aebc82008-10-11 17:55:29 +000033#define MAX_PACKET_LENGTH 4096
34
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010036#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/kvm.h"
Leon Alraecfe67ce2015-06-19 14:17:45 +010038#include "exec/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010039#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000040
Jan Kiszkaa3919382015-02-07 09:38:44 +010041#ifdef CONFIG_USER_ONLY
42#define GDB_ATTACHED "0"
43#else
44#define GDB_ATTACHED "1"
45#endif
46
Andreas Färberf3659ee2013-06-27 19:09:09 +020047static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
48 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020049{
Andreas Färberf3659ee2013-06-27 19:09:09 +020050 CPUClass *cc = CPU_GET_CLASS(cpu);
51
52 if (cc->memory_rw_debug) {
53 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
54 }
55 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020056}
aurel32ca587a82008-12-18 22:44:13 +000057
58enum {
59 GDB_SIGNAL_0 = 0,
60 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010061 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000062 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010063 GDB_SIGNAL_ABRT = 6,
64 GDB_SIGNAL_ALRM = 14,
65 GDB_SIGNAL_IO = 23,
66 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000067 GDB_SIGNAL_UNKNOWN = 143
68};
69
70#ifdef CONFIG_USER_ONLY
71
72/* Map target signal numbers to GDB protocol signal numbers and vice
73 * versa. For user emulation's currently supported systems, we can
74 * assume most signals are defined.
75 */
76
77static int gdb_signal_table[] = {
78 0,
79 TARGET_SIGHUP,
80 TARGET_SIGINT,
81 TARGET_SIGQUIT,
82 TARGET_SIGILL,
83 TARGET_SIGTRAP,
84 TARGET_SIGABRT,
85 -1, /* SIGEMT */
86 TARGET_SIGFPE,
87 TARGET_SIGKILL,
88 TARGET_SIGBUS,
89 TARGET_SIGSEGV,
90 TARGET_SIGSYS,
91 TARGET_SIGPIPE,
92 TARGET_SIGALRM,
93 TARGET_SIGTERM,
94 TARGET_SIGURG,
95 TARGET_SIGSTOP,
96 TARGET_SIGTSTP,
97 TARGET_SIGCONT,
98 TARGET_SIGCHLD,
99 TARGET_SIGTTIN,
100 TARGET_SIGTTOU,
101 TARGET_SIGIO,
102 TARGET_SIGXCPU,
103 TARGET_SIGXFSZ,
104 TARGET_SIGVTALRM,
105 TARGET_SIGPROF,
106 TARGET_SIGWINCH,
107 -1, /* SIGLOST */
108 TARGET_SIGUSR1,
109 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000110#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000111 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000112#else
113 -1,
114#endif
aurel32ca587a82008-12-18 22:44:13 +0000115 -1, /* SIGPOLL */
116 -1,
117 -1,
118 -1,
119 -1,
120 -1,
121 -1,
122 -1,
123 -1,
124 -1,
125 -1,
126 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000127#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000128 __SIGRTMIN + 1,
129 __SIGRTMIN + 2,
130 __SIGRTMIN + 3,
131 __SIGRTMIN + 4,
132 __SIGRTMIN + 5,
133 __SIGRTMIN + 6,
134 __SIGRTMIN + 7,
135 __SIGRTMIN + 8,
136 __SIGRTMIN + 9,
137 __SIGRTMIN + 10,
138 __SIGRTMIN + 11,
139 __SIGRTMIN + 12,
140 __SIGRTMIN + 13,
141 __SIGRTMIN + 14,
142 __SIGRTMIN + 15,
143 __SIGRTMIN + 16,
144 __SIGRTMIN + 17,
145 __SIGRTMIN + 18,
146 __SIGRTMIN + 19,
147 __SIGRTMIN + 20,
148 __SIGRTMIN + 21,
149 __SIGRTMIN + 22,
150 __SIGRTMIN + 23,
151 __SIGRTMIN + 24,
152 __SIGRTMIN + 25,
153 __SIGRTMIN + 26,
154 __SIGRTMIN + 27,
155 __SIGRTMIN + 28,
156 __SIGRTMIN + 29,
157 __SIGRTMIN + 30,
158 __SIGRTMIN + 31,
159 -1, /* SIGCANCEL */
160 __SIGRTMIN,
161 __SIGRTMIN + 32,
162 __SIGRTMIN + 33,
163 __SIGRTMIN + 34,
164 __SIGRTMIN + 35,
165 __SIGRTMIN + 36,
166 __SIGRTMIN + 37,
167 __SIGRTMIN + 38,
168 __SIGRTMIN + 39,
169 __SIGRTMIN + 40,
170 __SIGRTMIN + 41,
171 __SIGRTMIN + 42,
172 __SIGRTMIN + 43,
173 __SIGRTMIN + 44,
174 __SIGRTMIN + 45,
175 __SIGRTMIN + 46,
176 __SIGRTMIN + 47,
177 __SIGRTMIN + 48,
178 __SIGRTMIN + 49,
179 __SIGRTMIN + 50,
180 __SIGRTMIN + 51,
181 __SIGRTMIN + 52,
182 __SIGRTMIN + 53,
183 __SIGRTMIN + 54,
184 __SIGRTMIN + 55,
185 __SIGRTMIN + 56,
186 __SIGRTMIN + 57,
187 __SIGRTMIN + 58,
188 __SIGRTMIN + 59,
189 __SIGRTMIN + 60,
190 __SIGRTMIN + 61,
191 __SIGRTMIN + 62,
192 __SIGRTMIN + 63,
193 __SIGRTMIN + 64,
194 __SIGRTMIN + 65,
195 __SIGRTMIN + 66,
196 __SIGRTMIN + 67,
197 __SIGRTMIN + 68,
198 __SIGRTMIN + 69,
199 __SIGRTMIN + 70,
200 __SIGRTMIN + 71,
201 __SIGRTMIN + 72,
202 __SIGRTMIN + 73,
203 __SIGRTMIN + 74,
204 __SIGRTMIN + 75,
205 __SIGRTMIN + 76,
206 __SIGRTMIN + 77,
207 __SIGRTMIN + 78,
208 __SIGRTMIN + 79,
209 __SIGRTMIN + 80,
210 __SIGRTMIN + 81,
211 __SIGRTMIN + 82,
212 __SIGRTMIN + 83,
213 __SIGRTMIN + 84,
214 __SIGRTMIN + 85,
215 __SIGRTMIN + 86,
216 __SIGRTMIN + 87,
217 __SIGRTMIN + 88,
218 __SIGRTMIN + 89,
219 __SIGRTMIN + 90,
220 __SIGRTMIN + 91,
221 __SIGRTMIN + 92,
222 __SIGRTMIN + 93,
223 __SIGRTMIN + 94,
224 __SIGRTMIN + 95,
225 -1, /* SIGINFO */
226 -1, /* UNKNOWN */
227 -1, /* DEFAULT */
228 -1,
229 -1,
230 -1,
231 -1,
232 -1,
233 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000234#endif
aurel32ca587a82008-12-18 22:44:13 +0000235};
bellard8f447cc2006-06-14 15:21:14 +0000236#else
aurel32ca587a82008-12-18 22:44:13 +0000237/* In system mode we only need SIGINT and SIGTRAP; other signals
238 are not yet supported. */
239
240enum {
241 TARGET_SIGINT = 2,
242 TARGET_SIGTRAP = 5
243};
244
245static int gdb_signal_table[] = {
246 -1,
247 -1,
248 TARGET_SIGINT,
249 -1,
250 -1,
251 TARGET_SIGTRAP
252};
bellard8f447cc2006-06-14 15:21:14 +0000253#endif
bellardb4608c02003-06-27 17:34:32 +0000254
aurel32ca587a82008-12-18 22:44:13 +0000255#ifdef CONFIG_USER_ONLY
256static int target_signal_to_gdb (int sig)
257{
258 int i;
259 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
260 if (gdb_signal_table[i] == sig)
261 return i;
262 return GDB_SIGNAL_UNKNOWN;
263}
264#endif
265
266static int gdb_signal_to_target (int sig)
267{
268 if (sig < ARRAY_SIZE (gdb_signal_table))
269 return gdb_signal_table[sig];
270 else
271 return -1;
272}
273
bellard4abe6152003-07-26 18:01:58 +0000274//#define DEBUG_GDB
bellardb4608c02003-06-27 17:34:32 +0000275
pbrook56aebc82008-10-11 17:55:29 +0000276typedef struct GDBRegisterState {
277 int base_reg;
278 int num_regs;
279 gdb_reg_cb get_reg;
280 gdb_reg_cb set_reg;
281 const char *xml;
282 struct GDBRegisterState *next;
283} GDBRegisterState;
284
bellard858693c2004-03-31 18:52:07 +0000285enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000286 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000287 RS_IDLE,
288 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400289 RS_GETLINE_ESC,
290 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000291 RS_CHKSUM1,
292 RS_CHKSUM2,
293};
bellard858693c2004-03-31 18:52:07 +0000294typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200295 CPUState *c_cpu; /* current CPU for step/continue ops */
296 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200297 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000298 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000299 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000300 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400301 int line_sum; /* running checksum */
302 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000303 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000304 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000305 int signal;
bellard41625032005-04-24 10:07:11 +0000306#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000307 int fd;
bellard41625032005-04-24 10:07:11 +0000308 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000309#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300310 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300311 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000312#endif
Meador Ingecdb432b2012-03-15 17:49:45 +0000313 char syscall_buf[256];
314 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000315} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000316
edgar_igl60897d32008-05-09 08:25:14 +0000317/* By default use no IRQs and no timers while single stepping so as to
318 * make single stepping like an ICE HW step.
319 */
320static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
321
aliguori880a7572008-11-18 20:30:24 +0000322static GDBState *gdbserver_state;
323
Andreas Färber5b50e792013-06-29 04:18:45 +0200324bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000325
bellard1fddef42005-04-17 19:16:13 +0000326#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000327/* XXX: This is not thread safe. Do we care? */
328static int gdbserver_fd = -1;
329
bellard858693c2004-03-31 18:52:07 +0000330static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000331{
332 uint8_t ch;
333 int ret;
334
335 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000336 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000337 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000338 if (errno == ECONNRESET)
339 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200340 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000341 return -1;
342 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000343 close(s->fd);
344 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000345 return -1;
346 } else {
347 break;
348 }
349 }
350 return ch;
351}
pbrook4046d912007-01-28 01:53:16 +0000352#endif
bellardb4608c02003-06-27 17:34:32 +0000353
blueswir1654efcf2009-04-18 07:29:59 +0000354static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000355 GDB_SYS_UNKNOWN,
356 GDB_SYS_ENABLED,
357 GDB_SYS_DISABLED,
358} gdb_syscall_mode;
359
Liviu Ionescua38bb072014-12-11 12:07:48 +0000360/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000361int use_gdb_syscalls(void)
362{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100363 SemihostingTarget target = semihosting_get_target();
364 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000365 /* -semihosting-config target=native */
366 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100367 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000368 /* -semihosting-config target=gdb */
369 return true;
370 }
371
372 /* -semihosting-config target=auto */
373 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000374 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000375 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
376 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000377 }
378 return gdb_syscall_mode == GDB_SYS_ENABLED;
379}
380
edgar_iglba70a622008-03-14 06:10:42 +0000381/* Resume execution. */
382static inline void gdb_continue(GDBState *s)
383{
384#ifdef CONFIG_USER_ONLY
385 s->running_state = 1;
386#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200387 if (!runstate_needs_reset()) {
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200388 vm_start();
389 }
edgar_iglba70a622008-03-14 06:10:42 +0000390#endif
391}
392
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100393/*
394 * Resume execution, per CPU actions. For user-mode emulation it's
395 * equivalent to gdb_continue.
396 */
397static int gdb_continue_partial(GDBState *s, char *newstates)
398{
399 CPUState *cpu;
400 int res = 0;
401#ifdef CONFIG_USER_ONLY
402 /*
403 * This is not exactly accurate, but it's an improvement compared to the
404 * previous situation, where only one CPU would be single-stepped.
405 */
406 CPU_FOREACH(cpu) {
407 if (newstates[cpu->cpu_index] == 's') {
408 cpu_single_step(cpu, sstep_flags);
409 }
410 }
411 s->running_state = 1;
412#else
413 int flag = 0;
414
415 if (!runstate_needs_reset()) {
416 if (vm_prepare_start()) {
417 return 0;
418 }
419
420 CPU_FOREACH(cpu) {
421 switch (newstates[cpu->cpu_index]) {
422 case 0:
423 case 1:
424 break; /* nothing to do here */
425 case 's':
426 cpu_single_step(cpu, sstep_flags);
427 cpu_resume(cpu);
428 flag = 1;
429 break;
430 case 'c':
431 cpu_resume(cpu);
432 flag = 1;
433 break;
434 default:
435 res = -1;
436 break;
437 }
438 }
439 }
440 if (flag) {
441 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
442 }
443#endif
444 return res;
445}
446
bellard858693c2004-03-31 18:52:07 +0000447static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000448{
pbrook4046d912007-01-28 01:53:16 +0000449#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000450 int ret;
451
452 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000453 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000454 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200455 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000456 return;
457 } else {
458 buf += ret;
459 len -= ret;
460 }
461 }
pbrook4046d912007-01-28 01:53:16 +0000462#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100463 /* XXX this blocks entire thread. Rewrite to use
464 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300465 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000466#endif
bellardb4608c02003-06-27 17:34:32 +0000467}
468
469static inline int fromhex(int v)
470{
471 if (v >= '0' && v <= '9')
472 return v - '0';
473 else if (v >= 'A' && v <= 'F')
474 return v - 'A' + 10;
475 else if (v >= 'a' && v <= 'f')
476 return v - 'a' + 10;
477 else
478 return 0;
479}
480
481static inline int tohex(int v)
482{
483 if (v < 10)
484 return v + '0';
485 else
486 return v - 10 + 'a';
487}
488
489static void memtohex(char *buf, const uint8_t *mem, int len)
490{
491 int i, c;
492 char *q;
493 q = buf;
494 for(i = 0; i < len; i++) {
495 c = mem[i];
496 *q++ = tohex(c >> 4);
497 *q++ = tohex(c & 0xf);
498 }
499 *q = '\0';
500}
501
502static void hextomem(uint8_t *mem, const char *buf, int len)
503{
504 int i;
505
506 for(i = 0; i < len; i++) {
507 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
508 buf += 2;
509 }
510}
511
bellardb4608c02003-06-27 17:34:32 +0000512/* return -1 if error, 0 if OK */
pbrook56aebc82008-10-11 17:55:29 +0000513static int put_packet_binary(GDBState *s, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000514{
pbrook56aebc82008-10-11 17:55:29 +0000515 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000516 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000517
bellardb4608c02003-06-27 17:34:32 +0000518 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000519 p = s->last_packet;
520 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000521 memcpy(p, buf, len);
522 p += len;
bellardb4608c02003-06-27 17:34:32 +0000523 csum = 0;
524 for(i = 0; i < len; i++) {
525 csum += buf[i];
526 }
pbrook4046d912007-01-28 01:53:16 +0000527 *(p++) = '#';
528 *(p++) = tohex((csum >> 4) & 0xf);
529 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000530
pbrook4046d912007-01-28 01:53:16 +0000531 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000532 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000533
pbrook4046d912007-01-28 01:53:16 +0000534#ifdef CONFIG_USER_ONLY
535 i = get_char(s);
536 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000537 return -1;
pbrook4046d912007-01-28 01:53:16 +0000538 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000539 break;
pbrook4046d912007-01-28 01:53:16 +0000540#else
541 break;
542#endif
bellardb4608c02003-06-27 17:34:32 +0000543 }
544 return 0;
545}
546
pbrook56aebc82008-10-11 17:55:29 +0000547/* return -1 if error, 0 if OK */
548static int put_packet(GDBState *s, const char *buf)
549{
550#ifdef DEBUG_GDB
551 printf("reply='%s'\n", buf);
552#endif
553
554 return put_packet_binary(s, buf, strlen(buf));
555}
556
pbrook56aebc82008-10-11 17:55:29 +0000557/* Encode data using the encoding for 'x' packets. */
558static int memtox(char *buf, const char *mem, int len)
559{
560 char *p = buf;
561 char c;
562
563 while (len--) {
564 c = *(mem++);
565 switch (c) {
566 case '#': case '$': case '*': case '}':
567 *(p++) = '}';
568 *(p++) = c ^ 0x20;
569 break;
570 default:
571 *(p++) = c;
572 break;
573 }
574 }
575 return p - buf;
576}
577
Andreas Färber5b24c642013-07-07 15:08:22 +0200578static const char *get_feature_xml(const char *p, const char **newp,
579 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000580{
pbrook56aebc82008-10-11 17:55:29 +0000581 size_t len;
582 int i;
583 const char *name;
584 static char target_xml[1024];
585
586 len = 0;
587 while (p[len] && p[len] != ':')
588 len++;
589 *newp = p + len;
590
591 name = NULL;
592 if (strncmp(p, "target.xml", len) == 0) {
593 /* Generate the XML description for this CPU. */
594 if (!target_xml[0]) {
595 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200596 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000597
David Hildenbrandb3820e62015-12-03 13:14:41 +0100598 pstrcat(target_xml, sizeof(target_xml),
599 "<?xml version=\"1.0\"?>"
600 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
601 "<target>");
602 if (cc->gdb_arch_name) {
603 gchar *arch = cc->gdb_arch_name(cpu);
604 pstrcat(target_xml, sizeof(target_xml), "<architecture>");
605 pstrcat(target_xml, sizeof(target_xml), arch);
606 pstrcat(target_xml, sizeof(target_xml), "</architecture>");
607 g_free(arch);
608 }
609 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
610 pstrcat(target_xml, sizeof(target_xml), cc->gdb_core_xml_file);
611 pstrcat(target_xml, sizeof(target_xml), "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200612 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000613 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
614 pstrcat(target_xml, sizeof(target_xml), r->xml);
615 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000616 }
blueswir12dc766d2009-04-13 16:06:19 +0000617 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000618 }
619 return target_xml;
620 }
621 for (i = 0; ; i++) {
622 name = xml_builtin[i][0];
623 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
624 break;
625 }
626 return name ? xml_builtin[i][1] : NULL;
627}
pbrook56aebc82008-10-11 17:55:29 +0000628
Andreas Färber385b9f02013-06-27 18:25:36 +0200629static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000630{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200631 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200632 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000633 GDBRegisterState *r;
634
Andreas Färbera0e372f2013-06-28 23:18:47 +0200635 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200636 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200637 }
pbrook56aebc82008-10-11 17:55:29 +0000638
Andreas Färbereac8b352013-06-28 21:11:37 +0200639 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000640 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
641 return r->get_reg(env, mem_buf, reg - r->base_reg);
642 }
643 }
644 return 0;
645}
646
Andreas Färber385b9f02013-06-27 18:25:36 +0200647static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000648{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200649 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200650 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000651 GDBRegisterState *r;
652
Andreas Färbera0e372f2013-06-28 23:18:47 +0200653 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200654 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200655 }
pbrook56aebc82008-10-11 17:55:29 +0000656
Andreas Färbereac8b352013-06-28 21:11:37 +0200657 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000658 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
659 return r->set_reg(env, mem_buf, reg - r->base_reg);
660 }
661 }
662 return 0;
663}
664
665/* Register a supplemental set of CPU registers. If g_pos is nonzero it
666 specifies the first register number and these registers are included in
667 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
668 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
669 */
670
Andreas Färber22169d42013-06-28 21:27:39 +0200671void gdb_register_coprocessor(CPUState *cpu,
672 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
673 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000674{
675 GDBRegisterState *s;
676 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000677
Andreas Färbereac8b352013-06-28 21:11:37 +0200678 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000679 while (*p) {
680 /* Check for duplicates. */
681 if (strcmp((*p)->xml, xml) == 0)
682 return;
683 p = &(*p)->next;
684 }
Stefan Weil9643c252011-10-18 22:25:38 +0200685
686 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200687 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200688 s->num_regs = num_regs;
689 s->get_reg = get_reg;
690 s->set_reg = set_reg;
691 s->xml = xml;
692
pbrook56aebc82008-10-11 17:55:29 +0000693 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200694 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000695 *p = s;
696 if (g_pos) {
697 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800698 error_report("Error: Bad gdb register numbering for '%s', "
699 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200700 } else {
701 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000702 }
703 }
704}
705
aliguoria1d1bb32008-11-18 20:07:32 +0000706#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100707/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
708static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
709{
710 static const int xlat[] = {
711 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
712 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
713 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
714 };
715
716 CPUClass *cc = CPU_GET_CLASS(cpu);
717 int cputype = xlat[gdbtype];
718
719 if (cc->gdb_stop_before_watchpoint) {
720 cputype |= BP_STOP_BEFORE_ACCESS;
721 }
722 return cputype;
723}
aliguoria1d1bb32008-11-18 20:07:32 +0000724#endif
725
aliguori880a7572008-11-18 20:30:24 +0000726static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000727{
Andreas Färber182735e2013-05-29 22:29:20 +0200728 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000729 int err = 0;
730
Andreas Färber62278812013-06-27 17:12:06 +0200731 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200732 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200733 }
aliguorie22a25c2009-03-12 20:12:48 +0000734
aliguoria1d1bb32008-11-18 20:07:32 +0000735 switch (type) {
736 case GDB_BREAKPOINT_SW:
737 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200738 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200739 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
740 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000741 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200742 }
aliguori880a7572008-11-18 20:30:24 +0000743 }
744 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000745#ifndef CONFIG_USER_ONLY
746 case GDB_WATCHPOINT_WRITE:
747 case GDB_WATCHPOINT_READ:
748 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200749 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100750 err = cpu_watchpoint_insert(cpu, addr, len,
751 xlat_gdb_type(cpu, type), NULL);
752 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000753 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100754 }
aliguori880a7572008-11-18 20:30:24 +0000755 }
756 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000757#endif
758 default:
759 return -ENOSYS;
760 }
761}
762
aliguori880a7572008-11-18 20:30:24 +0000763static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000764{
Andreas Färber182735e2013-05-29 22:29:20 +0200765 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000766 int err = 0;
767
Andreas Färber62278812013-06-27 17:12:06 +0200768 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200769 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200770 }
aliguorie22a25c2009-03-12 20:12:48 +0000771
aliguoria1d1bb32008-11-18 20:07:32 +0000772 switch (type) {
773 case GDB_BREAKPOINT_SW:
774 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200775 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200776 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
777 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000778 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200779 }
aliguori880a7572008-11-18 20:30:24 +0000780 }
781 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000782#ifndef CONFIG_USER_ONLY
783 case GDB_WATCHPOINT_WRITE:
784 case GDB_WATCHPOINT_READ:
785 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200786 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100787 err = cpu_watchpoint_remove(cpu, addr, len,
788 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +0000789 if (err)
790 break;
791 }
792 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000793#endif
794 default:
795 return -ENOSYS;
796 }
797}
798
aliguori880a7572008-11-18 20:30:24 +0000799static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000800{
Andreas Färber182735e2013-05-29 22:29:20 +0200801 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000802
aliguorie22a25c2009-03-12 20:12:48 +0000803 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200804 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000805 return;
806 }
807
Andreas Färberbdc44642013-06-24 23:50:24 +0200808 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200809 cpu_breakpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000810#ifndef CONFIG_USER_ONLY
Andreas Färber75a34032013-09-02 16:57:02 +0200811 cpu_watchpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000812#endif
aliguori880a7572008-11-18 20:30:24 +0000813 }
aliguoria1d1bb32008-11-18 20:07:32 +0000814}
815
aurel32fab9d282009-04-08 21:29:37 +0000816static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
817{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200818 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200819
820 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -0700821 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +0000822}
823
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200824static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700825{
Andreas Färber0d342822012-12-17 07:12:13 +0100826 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700827
Andreas Färberbdc44642013-06-24 23:50:24 +0200828 CPU_FOREACH(cpu) {
Andreas Färberaa48dd92013-07-09 20:50:52 +0200829 if (cpu_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200830 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200831 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700832 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200833
834 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700835}
836
Jan Kiszka4dabe742015-02-07 09:38:43 +0100837static int is_query_packet(const char *p, const char *query, char separator)
838{
839 unsigned int query_len = strlen(query);
840
841 return strncmp(p, query, query_len) == 0 &&
842 (p[query_len] == '\0' || p[query_len] == separator);
843}
844
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100845/**
846 * gdb_handle_vcont - Parses and handles a vCont packet.
847 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
848 * a format error, 0 on success.
849 */
850static int gdb_handle_vcont(GDBState *s, const char *p)
851{
852 int res, idx, signal = 0;
853 char cur_action;
854 char *newstates;
855 unsigned long tmp;
856 CPUState *cpu;
857#ifdef CONFIG_USER_ONLY
858 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
859
860 CPU_FOREACH(cpu) {
861 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
862 }
863#endif
864 /* uninitialised CPUs stay 0 */
865 newstates = g_new0(char, max_cpus);
866
867 /* mark valid CPUs with 1 */
868 CPU_FOREACH(cpu) {
869 newstates[cpu->cpu_index] = 1;
870 }
871
872 /*
873 * res keeps track of what error we are returning, with -ENOTSUP meaning
874 * that the command is unknown or unsupported, thus returning an empty
875 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
876 * or incorrect parameters passed.
877 */
878 res = 0;
879 while (*p) {
880 if (*p++ != ';') {
881 res = -ENOTSUP;
882 goto out;
883 }
884
885 cur_action = *p++;
886 if (cur_action == 'C' || cur_action == 'S') {
887 cur_action = tolower(cur_action);
888 res = qemu_strtoul(p + 1, &p, 16, &tmp);
889 if (res) {
890 goto out;
891 }
892 signal = gdb_signal_to_target(tmp);
893 } else if (cur_action != 'c' && cur_action != 's') {
894 /* unknown/invalid/unsupported command */
895 res = -ENOTSUP;
896 goto out;
897 }
898 /* thread specification. special values: (none), -1 = all; 0 = any */
899 if ((p[0] == ':' && p[1] == '-' && p[2] == '1') || (p[0] != ':')) {
900 if (*p == ':') {
901 p += 3;
902 }
903 for (idx = 0; idx < max_cpus; idx++) {
904 if (newstates[idx] == 1) {
905 newstates[idx] = cur_action;
906 }
907 }
908 } else if (*p == ':') {
909 p++;
910 res = qemu_strtoul(p, &p, 16, &tmp);
911 if (res) {
912 goto out;
913 }
914 idx = tmp;
915 /* 0 means any thread, so we pick the first valid CPU */
916 if (!idx) {
917 idx = cpu_index(first_cpu);
918 }
919
920 /*
921 * If we are in user mode, the thread specified is actually a
922 * thread id, and not an index. We need to find the actual
923 * CPU first, and only then we can use its index.
924 */
925 cpu = find_cpu(idx);
926 /* invalid CPU/thread specified */
927 if (!idx || !cpu) {
928 res = -EINVAL;
929 goto out;
930 }
931 /* only use if no previous match occourred */
932 if (newstates[cpu->cpu_index] == 1) {
933 newstates[cpu->cpu_index] = cur_action;
934 }
935 }
936 }
937 s->signal = signal;
938 gdb_continue_partial(s, newstates);
939
940out:
941 g_free(newstates);
942
943 return res;
944}
945
aliguori880a7572008-11-18 20:30:24 +0000946static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000947{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200948 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +0200949 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +0000950 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700951 uint32_t thread;
952 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +0000953 char buf[MAX_PACKET_LENGTH];
954 uint8_t mem_buf[MAX_PACKET_LENGTH];
955 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +0000956 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +0000957
bellard858693c2004-03-31 18:52:07 +0000958#ifdef DEBUG_GDB
959 printf("command='%s'\n", line_buf);
bellard4c3a88a2003-07-26 12:06:08 +0000960#endif
bellard858693c2004-03-31 18:52:07 +0000961 p = line_buf;
962 ch = *p++;
963 switch(ch) {
964 case '?':
bellard1fddef42005-04-17 19:16:13 +0000965 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +0000966 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200967 cpu_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +0000968 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +0000969 /* Remove all the breakpoints when this query is issued,
970 * because gdb is doing and initial connect and the state
971 * should be cleaned up.
972 */
aliguori880a7572008-11-18 20:30:24 +0000973 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +0000974 break;
975 case 'c':
976 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +0000977 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000978 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000979 }
aurel32ca587a82008-12-18 22:44:13 +0000980 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +0000981 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000982 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +0000983 case 'C':
aurel32ca587a82008-12-18 22:44:13 +0000984 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
985 if (s->signal == -1)
986 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +0000987 gdb_continue(s);
988 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200989 case 'v':
990 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200991 p += 4;
992 if (*p == '?') {
993 put_packet(s, "vCont;c;C;s;S");
994 break;
995 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200996
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100997 res = gdb_handle_vcont(s, p);
998
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200999 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001000 if ((res == -EINVAL) || (res == -ERANGE)) {
1001 put_packet(s, "E22");
1002 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001003 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001004 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001005 }
1006 break;
1007 } else {
1008 goto unknown_command;
1009 }
edgar_igl7d03f822008-05-17 18:58:29 +00001010 case 'k':
1011 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001012 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001013 exit(0);
1014 case 'D':
1015 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +00001016 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -03001017 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +00001018 gdb_continue(s);
1019 put_packet(s, "OK");
1020 break;
bellard858693c2004-03-31 18:52:07 +00001021 case 's':
1022 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001023 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001024 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001025 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001026 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001027 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001028 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001029 case 'F':
1030 {
1031 target_ulong ret;
1032 target_ulong err;
1033
1034 ret = strtoull(p, (char **)&p, 16);
1035 if (*p == ',') {
1036 p++;
1037 err = strtoull(p, (char **)&p, 16);
1038 } else {
1039 err = 0;
1040 }
1041 if (*p == ',')
1042 p++;
1043 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001044 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001045 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001046 s->current_syscall_cb = NULL;
1047 }
pbrooka2d1eba2007-01-28 03:10:55 +00001048 if (type == 'C') {
1049 put_packet(s, "T02");
1050 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001051 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001052 }
1053 }
1054 break;
bellard858693c2004-03-31 18:52:07 +00001055 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001056 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001057 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001058 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001059 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001060 len += reg_size;
1061 }
1062 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001063 put_packet(s, buf);
1064 break;
1065 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001066 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001067 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001068 len = strlen(p) / 2;
1069 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001070 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001071 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001072 len -= reg_size;
1073 registers += reg_size;
1074 }
bellard858693c2004-03-31 18:52:07 +00001075 put_packet(s, "OK");
1076 break;
1077 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001078 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001079 if (*p == ',')
1080 p++;
bellard9d9754a2006-06-25 15:32:37 +00001081 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001082
1083 /* memtohex() doubles the required space */
1084 if (len > MAX_PACKET_LENGTH / 2) {
1085 put_packet (s, "E22");
1086 break;
1087 }
1088
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001089 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001090 put_packet (s, "E14");
1091 } else {
1092 memtohex(buf, mem_buf, len);
1093 put_packet(s, buf);
1094 }
bellard858693c2004-03-31 18:52:07 +00001095 break;
1096 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001097 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001098 if (*p == ',')
1099 p++;
bellard9d9754a2006-06-25 15:32:37 +00001100 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001101 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001102 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001103
1104 /* hextomem() reads 2*len bytes */
1105 if (len > strlen(p) / 2) {
1106 put_packet (s, "E22");
1107 break;
1108 }
bellard858693c2004-03-31 18:52:07 +00001109 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001110 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001111 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001112 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001113 } else {
bellard858693c2004-03-31 18:52:07 +00001114 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001115 }
bellard858693c2004-03-31 18:52:07 +00001116 break;
pbrook56aebc82008-10-11 17:55:29 +00001117 case 'p':
1118 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1119 This works, but can be very slow. Anything new enough to
1120 understand XML also knows how to use this properly. */
1121 if (!gdb_has_xml)
1122 goto unknown_command;
1123 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001124 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001125 if (reg_size) {
1126 memtohex(buf, mem_buf, reg_size);
1127 put_packet(s, buf);
1128 } else {
1129 put_packet(s, "E14");
1130 }
1131 break;
1132 case 'P':
1133 if (!gdb_has_xml)
1134 goto unknown_command;
1135 addr = strtoull(p, (char **)&p, 16);
1136 if (*p == '=')
1137 p++;
1138 reg_size = strlen(p) / 2;
1139 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001140 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001141 put_packet(s, "OK");
1142 break;
bellard858693c2004-03-31 18:52:07 +00001143 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001144 case 'z':
1145 type = strtoul(p, (char **)&p, 16);
1146 if (*p == ',')
1147 p++;
bellard9d9754a2006-06-25 15:32:37 +00001148 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001149 if (*p == ',')
1150 p++;
bellard9d9754a2006-06-25 15:32:37 +00001151 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001152 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001153 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001154 else
aliguori880a7572008-11-18 20:30:24 +00001155 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001156 if (res >= 0)
1157 put_packet(s, "OK");
1158 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001159 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001160 else
1161 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001162 break;
aliguori880a7572008-11-18 20:30:24 +00001163 case 'H':
1164 type = *p++;
1165 thread = strtoull(p, (char **)&p, 16);
1166 if (thread == -1 || thread == 0) {
1167 put_packet(s, "OK");
1168 break;
1169 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001170 cpu = find_cpu(thread);
1171 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001172 put_packet(s, "E22");
1173 break;
1174 }
1175 switch (type) {
1176 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001177 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001178 put_packet(s, "OK");
1179 break;
1180 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001181 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001182 put_packet(s, "OK");
1183 break;
1184 default:
1185 put_packet(s, "E22");
1186 break;
1187 }
1188 break;
1189 case 'T':
1190 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001191 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001192
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001193 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001194 put_packet(s, "OK");
1195 } else {
aliguori880a7572008-11-18 20:30:24 +00001196 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001197 }
aliguori880a7572008-11-18 20:30:24 +00001198 break;
pbrook978efd62006-06-17 18:30:42 +00001199 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001200 case 'Q':
1201 /* parse any 'q' packets here */
1202 if (!strcmp(p,"qemu.sstepbits")) {
1203 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001204 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1205 SSTEP_ENABLE,
1206 SSTEP_NOIRQ,
1207 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001208 put_packet(s, buf);
1209 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001210 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001211 /* Display or change the sstep_flags */
1212 p += 10;
1213 if (*p != '=') {
1214 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001215 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001216 put_packet(s, buf);
1217 break;
1218 }
1219 p++;
1220 type = strtoul(p, (char **)&p, 16);
1221 sstep_flags = type;
1222 put_packet(s, "OK");
1223 break;
aliguori880a7572008-11-18 20:30:24 +00001224 } else if (strcmp(p,"C") == 0) {
1225 /* "Current thread" remains vague in the spec, so always return
1226 * the first CPU (gdb returns the first thread). */
1227 put_packet(s, "QC1");
1228 break;
1229 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001230 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001231 goto report_cpuinfo;
1232 } else if (strcmp(p,"sThreadInfo") == 0) {
1233 report_cpuinfo:
1234 if (s->query_cpu) {
Andreas Färber52f34622013-06-27 13:44:40 +02001235 snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001236 put_packet(s, buf);
Andreas Färberbdc44642013-06-24 23:50:24 +02001237 s->query_cpu = CPU_NEXT(s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001238 } else
1239 put_packet(s, "l");
1240 break;
1241 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1242 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001243 cpu = find_cpu(thread);
1244 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001245 cpu_synchronize_state(cpu);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001246 /* memtohex() doubles the required space */
1247 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
Andreas Färber55e5c282012-12-17 06:18:02 +01001248 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001249 cpu->halted ? "halted " : "running");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001250 memtohex(buf, mem_buf, len);
1251 put_packet(s, buf);
1252 }
aliguori880a7572008-11-18 20:30:24 +00001253 break;
edgar_igl60897d32008-05-09 08:25:14 +00001254 }
blueswir10b8a9882009-03-07 10:51:36 +00001255#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001256 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001257 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001258
blueswir1363a37d2008-08-21 17:58:08 +00001259 snprintf(buf, sizeof(buf),
1260 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1261 ";Bss=" TARGET_ABI_FMT_lx,
1262 ts->info->code_offset,
1263 ts->info->data_offset,
1264 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001265 put_packet(s, buf);
1266 break;
1267 }
blueswir10b8a9882009-03-07 10:51:36 +00001268#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001269 else if (strncmp(p, "Rcmd,", 5) == 0) {
1270 int len = strlen(p + 5);
1271
1272 if ((len % 2) != 0) {
1273 put_packet(s, "E01");
1274 break;
1275 }
aliguori8a34a0f2009-03-05 23:01:55 +00001276 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001277 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001278 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001279 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001280 put_packet(s, "OK");
1281 break;
1282 }
blueswir10b8a9882009-03-07 10:51:36 +00001283#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001284 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001285 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001286 cc = CPU_GET_CLASS(first_cpu);
1287 if (cc->gdb_core_xml_file != NULL) {
1288 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1289 }
pbrook56aebc82008-10-11 17:55:29 +00001290 put_packet(s, buf);
1291 break;
1292 }
pbrook56aebc82008-10-11 17:55:29 +00001293 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1294 const char *xml;
1295 target_ulong total_len;
1296
Andreas Färber5b24c642013-07-07 15:08:22 +02001297 cc = CPU_GET_CLASS(first_cpu);
1298 if (cc->gdb_core_xml_file == NULL) {
1299 goto unknown_command;
1300 }
1301
Andreas Färber5b50e792013-06-29 04:18:45 +02001302 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001303 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001304 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001305 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001306 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001307 put_packet(s, buf);
1308 break;
1309 }
1310
1311 if (*p == ':')
1312 p++;
1313 addr = strtoul(p, (char **)&p, 16);
1314 if (*p == ',')
1315 p++;
1316 len = strtoul(p, (char **)&p, 16);
1317
1318 total_len = strlen(xml);
1319 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001320 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001321 put_packet(s, buf);
1322 break;
1323 }
1324 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1325 len = (MAX_PACKET_LENGTH - 5) / 2;
1326 if (len < total_len - addr) {
1327 buf[0] = 'm';
1328 len = memtox(buf + 1, xml + addr, len);
1329 } else {
1330 buf[0] = 'l';
1331 len = memtox(buf + 1, xml + addr, total_len - addr);
1332 }
1333 put_packet_binary(s, buf, len + 1);
1334 break;
1335 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001336 if (is_query_packet(p, "Attached", ':')) {
1337 put_packet(s, GDB_ATTACHED);
1338 break;
1339 }
pbrook56aebc82008-10-11 17:55:29 +00001340 /* Unrecognised 'q' command. */
1341 goto unknown_command;
1342
bellard858693c2004-03-31 18:52:07 +00001343 default:
pbrook56aebc82008-10-11 17:55:29 +00001344 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001345 /* put empty packet */
1346 buf[0] = '\0';
1347 put_packet(s, buf);
1348 break;
1349 }
1350 return RS_IDLE;
1351}
1352
Andreas Färber64f6b342013-05-27 02:06:09 +02001353void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001354{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001355 gdbserver_state->c_cpu = cpu;
1356 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001357}
1358
bellard1fddef42005-04-17 19:16:13 +00001359#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001360static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001361{
aliguori880a7572008-11-18 20:30:24 +00001362 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001363 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001364 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001365 const char *type;
bellard858693c2004-03-31 18:52:07 +00001366 int ret;
1367
Meador Ingecdb432b2012-03-15 17:49:45 +00001368 if (running || s->state == RS_INACTIVE) {
1369 return;
1370 }
1371 /* Is there a GDB syscall waiting to be sent? */
1372 if (s->current_syscall_cb) {
1373 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001374 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001375 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001376 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001377 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001378 if (cpu->watchpoint_hit) {
1379 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001380 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001381 type = "r";
1382 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001383 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001384 type = "a";
1385 break;
1386 default:
1387 type = "";
1388 break;
1389 }
aliguori880a7572008-11-18 20:30:24 +00001390 snprintf(buf, sizeof(buf),
1391 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Andreas Färber0d342822012-12-17 07:12:13 +01001392 GDB_SIGNAL_TRAP, cpu_index(cpu), type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001393 (target_ulong)cpu->watchpoint_hit->vaddr);
1394 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001395 goto send_packet;
pbrook6658ffb2007-03-16 23:58:11 +00001396 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001397 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001398 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001399 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001400 case RUN_STATE_PAUSED:
aliguori9781e042009-01-22 17:15:29 +00001401 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001402 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001403 case RUN_STATE_SHUTDOWN:
Jan Kiszka425189a2011-03-22 11:02:09 +01001404 ret = GDB_SIGNAL_QUIT;
1405 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001406 case RUN_STATE_IO_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001407 ret = GDB_SIGNAL_IO;
1408 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001409 case RUN_STATE_WATCHDOG:
Jan Kiszka425189a2011-03-22 11:02:09 +01001410 ret = GDB_SIGNAL_ALRM;
1411 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001412 case RUN_STATE_INTERNAL_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001413 ret = GDB_SIGNAL_ABRT;
1414 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001415 case RUN_STATE_SAVE_VM:
1416 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001417 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001418 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001419 ret = GDB_SIGNAL_XCPU;
1420 break;
1421 default:
1422 ret = GDB_SIGNAL_UNKNOWN;
1423 break;
bellardbbeb7b52006-04-23 18:42:15 +00001424 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001425 gdb_set_stop_cpu(cpu);
Andreas Färber0d342822012-12-17 07:12:13 +01001426 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001427
1428send_packet:
bellard858693c2004-03-31 18:52:07 +00001429 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001430
1431 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001432 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001433}
bellard1fddef42005-04-17 19:16:13 +00001434#endif
bellard858693c2004-03-31 18:52:07 +00001435
pbrooka2d1eba2007-01-28 03:10:55 +00001436/* Send a gdb syscall request.
1437 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001438 %x - target_ulong argument printed in hex.
1439 %lx - 64-bit argument printed in hex.
1440 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001441void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001442{
pbrooka2d1eba2007-01-28 03:10:55 +00001443 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001444 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001445 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001446 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001447 GDBState *s;
1448
aliguori880a7572008-11-18 20:30:24 +00001449 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001450 if (!s)
1451 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001452 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001453#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001454 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001455#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001456 p = s->syscall_buf;
1457 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001458 *(p++) = 'F';
1459 while (*fmt) {
1460 if (*fmt == '%') {
1461 fmt++;
1462 switch (*fmt++) {
1463 case 'x':
1464 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001465 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001466 break;
pbrooka87295e2007-05-26 15:09:38 +00001467 case 'l':
1468 if (*(fmt++) != 'x')
1469 goto bad_format;
1470 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001471 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001472 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001473 case 's':
1474 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001475 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001476 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001477 break;
1478 default:
pbrooka87295e2007-05-26 15:09:38 +00001479 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001480 error_report("gdbstub: Bad syscall format string '%s'",
1481 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001482 break;
1483 }
1484 } else {
1485 *(p++) = *(fmt++);
1486 }
1487 }
pbrook8a93e022007-08-06 13:19:15 +00001488 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001489#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001490 put_packet(s, s->syscall_buf);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001491 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001492#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001493 /* In this case wait to send the syscall packet until notification that
1494 the CPU has stopped. This must be done because if the packet is sent
1495 now the reply from the syscall request could be received while the CPU
1496 is still in the running state, which can cause packets to be dropped
1497 and state transition 'T' packets to be sent while the syscall is still
1498 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001499 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001500#endif
1501}
1502
Peter Maydell19239b32015-09-07 10:39:27 +01001503void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1504{
1505 va_list va;
1506
1507 va_start(va, fmt);
1508 gdb_do_syscallv(cb, fmt, va);
1509 va_end(va);
1510}
1511
bellard6a00d602005-11-21 23:25:50 +00001512static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001513{
ths60fe76f2007-12-16 03:02:09 +00001514 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001515
bellard1fddef42005-04-17 19:16:13 +00001516#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001517 if (s->last_packet_len) {
1518 /* Waiting for a response to the last packet. If we see the start
1519 of a new command then abandon the previous response. */
1520 if (ch == '-') {
1521#ifdef DEBUG_GDB
1522 printf("Got NACK, retransmitting\n");
1523#endif
thsffe8ab82007-12-16 03:16:05 +00001524 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
pbrook4046d912007-01-28 01:53:16 +00001525 }
1526#ifdef DEBUG_GDB
1527 else if (ch == '+')
1528 printf("Got ACK\n");
1529 else
1530 printf("Got '%c' when expecting ACK/NACK\n", ch);
1531#endif
1532 if (ch == '+' || ch == '$')
1533 s->last_packet_len = 0;
1534 if (ch != '$')
1535 return;
1536 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001537 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001538 /* when the CPU is running, we cannot do anything except stop
1539 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001540 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001541 } else
bellard1fddef42005-04-17 19:16:13 +00001542#endif
bellard41625032005-04-24 10:07:11 +00001543 {
bellard858693c2004-03-31 18:52:07 +00001544 switch(s->state) {
1545 case RS_IDLE:
1546 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04001547 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00001548 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04001549 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00001550 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04001551 } else {
1552#ifdef DEBUG_GDB
1553 printf("gdbstub received garbage between packets: 0x%x\n", ch);
1554#endif
bellard4c3a88a2003-07-26 12:06:08 +00001555 }
1556 break;
bellard858693c2004-03-31 18:52:07 +00001557 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04001558 if (ch == '}') {
1559 /* start escape sequence */
1560 s->state = RS_GETLINE_ESC;
1561 s->line_sum += ch;
1562 } else if (ch == '*') {
1563 /* start run length encoding sequence */
1564 s->state = RS_GETLINE_RLE;
1565 s->line_sum += ch;
1566 } else if (ch == '#') {
1567 /* end of command, start of checksum*/
1568 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00001569 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04001570#ifdef DEBUG_GDB
1571 printf("gdbstub command buffer overrun, dropping command\n");
1572#endif
bellard858693c2004-03-31 18:52:07 +00001573 s->state = RS_IDLE;
1574 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001575 /* unescaped command character */
1576 s->line_buf[s->line_buf_index++] = ch;
1577 s->line_sum += ch;
1578 }
1579 break;
1580 case RS_GETLINE_ESC:
1581 if (ch == '#') {
1582 /* unexpected end of command in escape sequence */
1583 s->state = RS_CHKSUM1;
1584 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1585 /* command buffer overrun */
1586#ifdef DEBUG_GDB
1587 printf("gdbstub command buffer overrun, dropping command\n");
1588#endif
1589 s->state = RS_IDLE;
1590 } else {
1591 /* parse escaped character and leave escape state */
1592 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
1593 s->line_sum += ch;
1594 s->state = RS_GETLINE;
1595 }
1596 break;
1597 case RS_GETLINE_RLE:
1598 if (ch < ' ') {
1599 /* invalid RLE count encoding */
1600#ifdef DEBUG_GDB
1601 printf("gdbstub got invalid RLE count: 0x%x\n", ch);
1602#endif
1603 s->state = RS_GETLINE;
1604 } else {
1605 /* decode repeat length */
1606 int repeat = (unsigned char)ch - ' ' + 3;
1607 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
1608 /* that many repeats would overrun the command buffer */
1609#ifdef DEBUG_GDB
1610 printf("gdbstub command buffer overrun,"
1611 " dropping command\n");
1612#endif
1613 s->state = RS_IDLE;
1614 } else if (s->line_buf_index < 1) {
1615 /* got a repeat but we have nothing to repeat */
1616#ifdef DEBUG_GDB
1617 printf("gdbstub got invalid RLE sequence\n");
1618#endif
1619 s->state = RS_GETLINE;
1620 } else {
1621 /* repeat the last character */
1622 memset(s->line_buf + s->line_buf_index,
1623 s->line_buf[s->line_buf_index - 1], repeat);
1624 s->line_buf_index += repeat;
1625 s->line_sum += ch;
1626 s->state = RS_GETLINE;
1627 }
bellard858693c2004-03-31 18:52:07 +00001628 }
1629 break;
1630 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04001631 /* get high hex digit of checksum */
1632 if (!isxdigit(ch)) {
1633#ifdef DEBUG_GDB
1634 printf("gdbstub got invalid command checksum digit\n");
1635#endif
1636 s->state = RS_GETLINE;
1637 break;
1638 }
bellard858693c2004-03-31 18:52:07 +00001639 s->line_buf[s->line_buf_index] = '\0';
1640 s->line_csum = fromhex(ch) << 4;
1641 s->state = RS_CHKSUM2;
1642 break;
1643 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04001644 /* get low hex digit of checksum */
1645 if (!isxdigit(ch)) {
1646#ifdef DEBUG_GDB
1647 printf("gdbstub got invalid command checksum digit\n");
1648#endif
1649 s->state = RS_GETLINE;
1650 break;
bellard858693c2004-03-31 18:52:07 +00001651 }
Doug Gale4bf43122017-05-01 12:22:10 -04001652 s->line_csum |= fromhex(ch);
1653
1654 if (s->line_csum != (s->line_sum & 0xff)) {
1655 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00001656 reply = '-';
1657 put_buffer(s, &reply, 1);
Doug Gale4bf43122017-05-01 12:22:10 -04001658#ifdef DEBUG_GDB
1659 printf("gdbstub got command packet with incorrect checksum\n");
1660#endif
bellard858693c2004-03-31 18:52:07 +00001661 s->state = RS_IDLE;
1662 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001663 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00001664 reply = '+';
1665 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001666 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001667 }
bellardb4608c02003-06-27 17:34:32 +00001668 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001669 default:
1670 abort();
bellardb4608c02003-06-27 17:34:32 +00001671 }
1672 }
bellard858693c2004-03-31 18:52:07 +00001673}
1674
Paul Brook0e1c9c52010-06-16 13:03:51 +01001675/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001676void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001677{
1678 GDBState *s;
1679 char buf[4];
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001680#ifndef CONFIG_USER_ONLY
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001681 Chardev *chr;
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001682#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001683
1684 s = gdbserver_state;
1685 if (!s) {
1686 return;
1687 }
1688#ifdef CONFIG_USER_ONLY
1689 if (gdbserver_fd < 0 || s->fd < 0) {
1690 return;
1691 }
Paolo Bonzini3d0f4412015-03-02 13:26:58 +01001692#else
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001693 chr = qemu_chr_fe_get_driver(&s->chr);
1694 if (!chr) {
Paolo Bonzini3d0f4412015-03-02 13:26:58 +01001695 return;
1696 }
Paul Brook0e1c9c52010-06-16 13:03:51 +01001697#endif
1698
1699 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1700 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001701
1702#ifndef CONFIG_USER_ONLY
Marc-André Lureauc39860e2016-10-22 12:52:58 +03001703 qemu_chr_fe_deinit(&s->chr);
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +03001704 object_unparent(OBJECT(chr));
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001705#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001706}
1707
bellard1fddef42005-04-17 19:16:13 +00001708#ifdef CONFIG_USER_ONLY
1709int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001710gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001711{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001712 GDBState *s;
1713 char buf[256];
1714 int n;
bellard1fddef42005-04-17 19:16:13 +00001715
Andreas Färber5ca666c2013-06-24 19:20:57 +02001716 s = gdbserver_state;
1717 if (gdbserver_fd < 0 || s->fd < 0) {
1718 return sig;
bellard1fddef42005-04-17 19:16:13 +00001719 }
1720
Andreas Färber5ca666c2013-06-24 19:20:57 +02001721 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001722 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001723 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00001724
Andreas Färber5ca666c2013-06-24 19:20:57 +02001725 if (sig != 0) {
1726 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1727 put_packet(s, buf);
1728 }
1729 /* put_packet() might have detected that the peer terminated the
1730 connection. */
1731 if (s->fd < 0) {
1732 return sig;
1733 }
1734
1735 sig = 0;
1736 s->state = RS_IDLE;
1737 s->running_state = 0;
1738 while (s->running_state == 0) {
1739 n = read(s->fd, buf, 256);
1740 if (n > 0) {
1741 int i;
1742
1743 for (i = 0; i < n; i++) {
1744 gdb_read_byte(s, buf[i]);
1745 }
Peter Wu5819e3e2016-06-05 16:35:48 +02001746 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02001747 /* XXX: Connection closed. Should probably wait for another
1748 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02001749 if (n == 0) {
1750 close(s->fd);
1751 }
1752 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001753 return sig;
bellard1fddef42005-04-17 19:16:13 +00001754 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001755 }
1756 sig = s->signal;
1757 s->signal = 0;
1758 return sig;
bellard1fddef42005-04-17 19:16:13 +00001759}
bellarde9009672005-04-26 20:42:36 +00001760
aurel32ca587a82008-12-18 22:44:13 +00001761/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001762void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001763{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001764 GDBState *s;
1765 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001766
Andreas Färber5ca666c2013-06-24 19:20:57 +02001767 s = gdbserver_state;
1768 if (gdbserver_fd < 0 || s->fd < 0) {
1769 return;
1770 }
aurel32ca587a82008-12-18 22:44:13 +00001771
Andreas Färber5ca666c2013-06-24 19:20:57 +02001772 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1773 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001774}
bellard1fddef42005-04-17 19:16:13 +00001775
aliguori880a7572008-11-18 20:30:24 +00001776static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001777{
1778 GDBState *s;
1779 struct sockaddr_in sockaddr;
1780 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001781 int fd;
bellard858693c2004-03-31 18:52:07 +00001782
1783 for(;;) {
1784 len = sizeof(sockaddr);
1785 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1786 if (fd < 0 && errno != EINTR) {
1787 perror("accept");
1788 return;
1789 } else if (fd >= 0) {
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001790#ifndef _WIN32
1791 fcntl(fd, F_SETFD, FD_CLOEXEC);
1792#endif
bellard858693c2004-03-31 18:52:07 +00001793 break;
1794 }
1795 }
1796
1797 /* set short latency */
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001798 socket_set_nodelay(fd);
ths3b46e622007-09-17 08:09:54 +00001799
Anthony Liguori7267c092011-08-20 22:09:37 -05001800 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001801 s->c_cpu = first_cpu;
1802 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001803 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02001804 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00001805
aliguori880a7572008-11-18 20:30:24 +00001806 gdbserver_state = s;
bellard858693c2004-03-31 18:52:07 +00001807}
1808
1809static int gdbserver_open(int port)
1810{
1811 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001812 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00001813
1814 fd = socket(PF_INET, SOCK_STREAM, 0);
1815 if (fd < 0) {
1816 perror("socket");
1817 return -1;
1818 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001819#ifndef _WIN32
1820 fcntl(fd, F_SETFD, FD_CLOEXEC);
1821#endif
bellard858693c2004-03-31 18:52:07 +00001822
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001823 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00001824
1825 sockaddr.sin_family = AF_INET;
1826 sockaddr.sin_port = htons(port);
1827 sockaddr.sin_addr.s_addr = 0;
1828 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1829 if (ret < 0) {
1830 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001831 close(fd);
bellard858693c2004-03-31 18:52:07 +00001832 return -1;
1833 }
Peter Wu96165b92016-05-04 11:32:17 +02001834 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00001835 if (ret < 0) {
1836 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001837 close(fd);
bellard858693c2004-03-31 18:52:07 +00001838 return -1;
1839 }
bellard858693c2004-03-31 18:52:07 +00001840 return fd;
1841}
1842
1843int gdbserver_start(int port)
1844{
1845 gdbserver_fd = gdbserver_open(port);
1846 if (gdbserver_fd < 0)
1847 return -1;
1848 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00001849 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00001850 return 0;
1851}
aurel322b1319c2008-12-18 22:44:04 +00001852
1853/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07001854void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00001855{
1856 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02001857
1858 if (gdbserver_fd < 0 || s->fd < 0) {
1859 return;
1860 }
aurel322b1319c2008-12-18 22:44:04 +00001861 close(s->fd);
1862 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001863 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02001864 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00001865}
pbrook4046d912007-01-28 01:53:16 +00001866#else
thsaa1f17c2007-07-11 22:48:58 +00001867static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001868{
pbrook56aebc82008-10-11 17:55:29 +00001869 /* We can handle an arbitrarily large amount of data.
1870 Pick the maximum packet size, which is as good as anything. */
1871 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001872}
1873
thsaa1f17c2007-07-11 22:48:58 +00001874static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001875{
pbrook4046d912007-01-28 01:53:16 +00001876 int i;
1877
1878 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001879 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001880 }
1881}
1882
1883static void gdb_chr_event(void *opaque, int event)
1884{
1885 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301886 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001887 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02001888 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00001889 break;
1890 default:
1891 break;
1892 }
1893}
1894
aliguori8a34a0f2009-03-05 23:01:55 +00001895static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1896{
1897 char buf[MAX_PACKET_LENGTH];
1898
1899 buf[0] = 'O';
1900 if (len > (MAX_PACKET_LENGTH/2) - 1)
1901 len = (MAX_PACKET_LENGTH/2) - 1;
1902 memtohex(buf + 1, (uint8_t *)msg, len);
1903 put_packet(s, buf);
1904}
1905
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001906static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00001907{
1908 const char *p = (const char *)buf;
1909 int max_sz;
1910
1911 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1912 for (;;) {
1913 if (len <= max_sz) {
1914 gdb_monitor_output(gdbserver_state, p, len);
1915 break;
1916 }
1917 gdb_monitor_output(gdbserver_state, p, max_sz);
1918 p += max_sz;
1919 len -= max_sz;
1920 }
1921 return len;
1922}
1923
aliguori59030a82009-04-05 18:43:41 +00001924#ifndef _WIN32
1925static void gdb_sigterm_handler(int signal)
1926{
Luiz Capitulino13548692011-07-29 15:36:43 -03001927 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001928 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001929 }
aliguori59030a82009-04-05 18:43:41 +00001930}
1931#endif
1932
Marc-André Lureau777357d2016-12-07 18:39:10 +03001933static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
1934 bool *be_opened, Error **errp)
1935{
1936 *be_opened = false;
1937}
1938
1939static void char_gdb_class_init(ObjectClass *oc, void *data)
1940{
1941 ChardevClass *cc = CHARDEV_CLASS(oc);
1942
1943 cc->internal = true;
1944 cc->open = gdb_monitor_open;
1945 cc->chr_write = gdb_monitor_write;
1946}
1947
1948#define TYPE_CHARDEV_GDB "chardev-gdb"
1949
1950static const TypeInfo char_gdb_type_info = {
1951 .name = TYPE_CHARDEV_GDB,
1952 .parent = TYPE_CHARDEV,
1953 .class_init = char_gdb_class_init,
1954};
1955
aliguori59030a82009-04-05 18:43:41 +00001956int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00001957{
1958 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00001959 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001960 Chardev *chr = NULL;
1961 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00001962
Ziyue Yang508b4ec2017-01-18 16:02:41 +08001963 if (!first_cpu) {
1964 error_report("gdbstub: meaningless to attach gdb to a "
1965 "machine without any CPU.");
1966 return -1;
1967 }
1968
aliguori59030a82009-04-05 18:43:41 +00001969 if (!device)
1970 return -1;
1971 if (strcmp(device, "none") != 0) {
1972 if (strstart(device, "tcp:", NULL)) {
1973 /* enforce required TCP attributes */
1974 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1975 "%s,nowait,nodelay,server", device);
1976 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00001977 }
aliguori59030a82009-04-05 18:43:41 +00001978#ifndef _WIN32
1979 else if (strcmp(device, "stdio") == 0) {
1980 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00001981
aliguori59030a82009-04-05 18:43:41 +00001982 memset(&act, 0, sizeof(act));
1983 act.sa_handler = gdb_sigterm_handler;
1984 sigaction(SIGINT, &act, NULL);
1985 }
1986#endif
Marc-André Lureaub4948be2016-10-22 12:52:46 +03001987 chr = qemu_chr_new_noreplay("gdb", device);
aliguori36556b22009-03-28 18:05:53 +00001988 if (!chr)
1989 return -1;
pbrookcfc34752007-02-22 01:48:01 +00001990 }
1991
aliguori36556b22009-03-28 18:05:53 +00001992 s = gdbserver_state;
1993 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001994 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00001995 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00001996
aliguori36556b22009-03-28 18:05:53 +00001997 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1998
1999 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03002000 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
2001 NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00002002 monitor_init(mon_chr, 0);
2003 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002004 if (qemu_chr_fe_get_driver(&s->chr)) {
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +03002005 object_unparent(OBJECT(qemu_chr_fe_get_driver(&s->chr)));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002006 }
aliguori36556b22009-03-28 18:05:53 +00002007 mon_chr = s->mon_chr;
2008 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002009 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002010 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002011 s->c_cpu = first_cpu;
2012 s->g_cpu = first_cpu;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002013 if (chr) {
2014 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002015 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +03002016 gdb_chr_event, NULL, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002017 }
aliguori36556b22009-03-28 18:05:53 +00002018 s->state = chr ? RS_IDLE : RS_INACTIVE;
2019 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002020 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002021
pbrook4046d912007-01-28 01:53:16 +00002022 return 0;
2023}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002024
2025static void register_types(void)
2026{
2027 type_register_static(&char_gdb_type_info);
2028}
2029
2030type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002031#endif