blob: 4251d2389892fc9a51f5acf04515dada40859b97 [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"
Marc-André Lureau8228e352017-01-26 17:19:46 +040028#include "chardev/char.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040029#include "chardev/char-fe.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010030#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010031#include "exec/gdbstub.h"
bellard1fddef42005-04-17 19:16:13 +000032#endif
bellard67b915a2004-03-31 23:37:16 +000033
pbrook56aebc82008-10-11 17:55:29 +000034#define MAX_PACKET_LENGTH 4096
35
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010036#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010037#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/kvm.h"
Leon Alraecfe67ce2015-06-19 14:17:45 +010039#include "exec/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010040#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000041
Jan Kiszkaa3919382015-02-07 09:38:44 +010042#ifdef CONFIG_USER_ONLY
43#define GDB_ATTACHED "0"
44#else
45#define GDB_ATTACHED "1"
46#endif
47
Andreas Färberf3659ee2013-06-27 19:09:09 +020048static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
49 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020050{
Andreas Färberf3659ee2013-06-27 19:09:09 +020051 CPUClass *cc = CPU_GET_CLASS(cpu);
52
53 if (cc->memory_rw_debug) {
54 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
55 }
56 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020057}
aurel32ca587a82008-12-18 22:44:13 +000058
59enum {
60 GDB_SIGNAL_0 = 0,
61 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010062 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000063 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010064 GDB_SIGNAL_ABRT = 6,
65 GDB_SIGNAL_ALRM = 14,
66 GDB_SIGNAL_IO = 23,
67 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000068 GDB_SIGNAL_UNKNOWN = 143
69};
70
71#ifdef CONFIG_USER_ONLY
72
73/* Map target signal numbers to GDB protocol signal numbers and vice
74 * versa. For user emulation's currently supported systems, we can
75 * assume most signals are defined.
76 */
77
78static int gdb_signal_table[] = {
79 0,
80 TARGET_SIGHUP,
81 TARGET_SIGINT,
82 TARGET_SIGQUIT,
83 TARGET_SIGILL,
84 TARGET_SIGTRAP,
85 TARGET_SIGABRT,
86 -1, /* SIGEMT */
87 TARGET_SIGFPE,
88 TARGET_SIGKILL,
89 TARGET_SIGBUS,
90 TARGET_SIGSEGV,
91 TARGET_SIGSYS,
92 TARGET_SIGPIPE,
93 TARGET_SIGALRM,
94 TARGET_SIGTERM,
95 TARGET_SIGURG,
96 TARGET_SIGSTOP,
97 TARGET_SIGTSTP,
98 TARGET_SIGCONT,
99 TARGET_SIGCHLD,
100 TARGET_SIGTTIN,
101 TARGET_SIGTTOU,
102 TARGET_SIGIO,
103 TARGET_SIGXCPU,
104 TARGET_SIGXFSZ,
105 TARGET_SIGVTALRM,
106 TARGET_SIGPROF,
107 TARGET_SIGWINCH,
108 -1, /* SIGLOST */
109 TARGET_SIGUSR1,
110 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000111#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000112 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000113#else
114 -1,
115#endif
aurel32ca587a82008-12-18 22:44:13 +0000116 -1, /* SIGPOLL */
117 -1,
118 -1,
119 -1,
120 -1,
121 -1,
122 -1,
123 -1,
124 -1,
125 -1,
126 -1,
127 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000128#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000129 __SIGRTMIN + 1,
130 __SIGRTMIN + 2,
131 __SIGRTMIN + 3,
132 __SIGRTMIN + 4,
133 __SIGRTMIN + 5,
134 __SIGRTMIN + 6,
135 __SIGRTMIN + 7,
136 __SIGRTMIN + 8,
137 __SIGRTMIN + 9,
138 __SIGRTMIN + 10,
139 __SIGRTMIN + 11,
140 __SIGRTMIN + 12,
141 __SIGRTMIN + 13,
142 __SIGRTMIN + 14,
143 __SIGRTMIN + 15,
144 __SIGRTMIN + 16,
145 __SIGRTMIN + 17,
146 __SIGRTMIN + 18,
147 __SIGRTMIN + 19,
148 __SIGRTMIN + 20,
149 __SIGRTMIN + 21,
150 __SIGRTMIN + 22,
151 __SIGRTMIN + 23,
152 __SIGRTMIN + 24,
153 __SIGRTMIN + 25,
154 __SIGRTMIN + 26,
155 __SIGRTMIN + 27,
156 __SIGRTMIN + 28,
157 __SIGRTMIN + 29,
158 __SIGRTMIN + 30,
159 __SIGRTMIN + 31,
160 -1, /* SIGCANCEL */
161 __SIGRTMIN,
162 __SIGRTMIN + 32,
163 __SIGRTMIN + 33,
164 __SIGRTMIN + 34,
165 __SIGRTMIN + 35,
166 __SIGRTMIN + 36,
167 __SIGRTMIN + 37,
168 __SIGRTMIN + 38,
169 __SIGRTMIN + 39,
170 __SIGRTMIN + 40,
171 __SIGRTMIN + 41,
172 __SIGRTMIN + 42,
173 __SIGRTMIN + 43,
174 __SIGRTMIN + 44,
175 __SIGRTMIN + 45,
176 __SIGRTMIN + 46,
177 __SIGRTMIN + 47,
178 __SIGRTMIN + 48,
179 __SIGRTMIN + 49,
180 __SIGRTMIN + 50,
181 __SIGRTMIN + 51,
182 __SIGRTMIN + 52,
183 __SIGRTMIN + 53,
184 __SIGRTMIN + 54,
185 __SIGRTMIN + 55,
186 __SIGRTMIN + 56,
187 __SIGRTMIN + 57,
188 __SIGRTMIN + 58,
189 __SIGRTMIN + 59,
190 __SIGRTMIN + 60,
191 __SIGRTMIN + 61,
192 __SIGRTMIN + 62,
193 __SIGRTMIN + 63,
194 __SIGRTMIN + 64,
195 __SIGRTMIN + 65,
196 __SIGRTMIN + 66,
197 __SIGRTMIN + 67,
198 __SIGRTMIN + 68,
199 __SIGRTMIN + 69,
200 __SIGRTMIN + 70,
201 __SIGRTMIN + 71,
202 __SIGRTMIN + 72,
203 __SIGRTMIN + 73,
204 __SIGRTMIN + 74,
205 __SIGRTMIN + 75,
206 __SIGRTMIN + 76,
207 __SIGRTMIN + 77,
208 __SIGRTMIN + 78,
209 __SIGRTMIN + 79,
210 __SIGRTMIN + 80,
211 __SIGRTMIN + 81,
212 __SIGRTMIN + 82,
213 __SIGRTMIN + 83,
214 __SIGRTMIN + 84,
215 __SIGRTMIN + 85,
216 __SIGRTMIN + 86,
217 __SIGRTMIN + 87,
218 __SIGRTMIN + 88,
219 __SIGRTMIN + 89,
220 __SIGRTMIN + 90,
221 __SIGRTMIN + 91,
222 __SIGRTMIN + 92,
223 __SIGRTMIN + 93,
224 __SIGRTMIN + 94,
225 __SIGRTMIN + 95,
226 -1, /* SIGINFO */
227 -1, /* UNKNOWN */
228 -1, /* DEFAULT */
229 -1,
230 -1,
231 -1,
232 -1,
233 -1,
234 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000235#endif
aurel32ca587a82008-12-18 22:44:13 +0000236};
bellard8f447cc2006-06-14 15:21:14 +0000237#else
aurel32ca587a82008-12-18 22:44:13 +0000238/* In system mode we only need SIGINT and SIGTRAP; other signals
239 are not yet supported. */
240
241enum {
242 TARGET_SIGINT = 2,
243 TARGET_SIGTRAP = 5
244};
245
246static int gdb_signal_table[] = {
247 -1,
248 -1,
249 TARGET_SIGINT,
250 -1,
251 -1,
252 TARGET_SIGTRAP
253};
bellard8f447cc2006-06-14 15:21:14 +0000254#endif
bellardb4608c02003-06-27 17:34:32 +0000255
aurel32ca587a82008-12-18 22:44:13 +0000256#ifdef CONFIG_USER_ONLY
257static int target_signal_to_gdb (int sig)
258{
259 int i;
260 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
261 if (gdb_signal_table[i] == sig)
262 return i;
263 return GDB_SIGNAL_UNKNOWN;
264}
265#endif
266
267static int gdb_signal_to_target (int sig)
268{
269 if (sig < ARRAY_SIZE (gdb_signal_table))
270 return gdb_signal_table[sig];
271 else
272 return -1;
273}
274
bellard4abe6152003-07-26 18:01:58 +0000275//#define DEBUG_GDB
bellardb4608c02003-06-27 17:34:32 +0000276
pbrook56aebc82008-10-11 17:55:29 +0000277typedef struct GDBRegisterState {
278 int base_reg;
279 int num_regs;
280 gdb_reg_cb get_reg;
281 gdb_reg_cb set_reg;
282 const char *xml;
283 struct GDBRegisterState *next;
284} GDBRegisterState;
285
bellard858693c2004-03-31 18:52:07 +0000286enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000287 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000288 RS_IDLE,
289 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400290 RS_GETLINE_ESC,
291 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000292 RS_CHKSUM1,
293 RS_CHKSUM2,
294};
bellard858693c2004-03-31 18:52:07 +0000295typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200296 CPUState *c_cpu; /* current CPU for step/continue ops */
297 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200298 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000299 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000300 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000301 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400302 int line_sum; /* running checksum */
303 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000304 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000305 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000306 int signal;
bellard41625032005-04-24 10:07:11 +0000307#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000308 int fd;
bellard41625032005-04-24 10:07:11 +0000309 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000310#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300311 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300312 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000313#endif
Meador Ingecdb432b2012-03-15 17:49:45 +0000314 char syscall_buf[256];
315 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000316} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000317
edgar_igl60897d32008-05-09 08:25:14 +0000318/* By default use no IRQs and no timers while single stepping so as to
319 * make single stepping like an ICE HW step.
320 */
321static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
322
aliguori880a7572008-11-18 20:30:24 +0000323static GDBState *gdbserver_state;
324
Andreas Färber5b50e792013-06-29 04:18:45 +0200325bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000326
bellard1fddef42005-04-17 19:16:13 +0000327#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000328/* XXX: This is not thread safe. Do we care? */
329static int gdbserver_fd = -1;
330
bellard858693c2004-03-31 18:52:07 +0000331static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000332{
333 uint8_t ch;
334 int ret;
335
336 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000337 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000338 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000339 if (errno == ECONNRESET)
340 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200341 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000342 return -1;
343 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000344 close(s->fd);
345 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000346 return -1;
347 } else {
348 break;
349 }
350 }
351 return ch;
352}
pbrook4046d912007-01-28 01:53:16 +0000353#endif
bellardb4608c02003-06-27 17:34:32 +0000354
blueswir1654efcf2009-04-18 07:29:59 +0000355static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000356 GDB_SYS_UNKNOWN,
357 GDB_SYS_ENABLED,
358 GDB_SYS_DISABLED,
359} gdb_syscall_mode;
360
Liviu Ionescua38bb072014-12-11 12:07:48 +0000361/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000362int use_gdb_syscalls(void)
363{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100364 SemihostingTarget target = semihosting_get_target();
365 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000366 /* -semihosting-config target=native */
367 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100368 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000369 /* -semihosting-config target=gdb */
370 return true;
371 }
372
373 /* -semihosting-config target=auto */
374 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000375 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000376 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
377 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000378 }
379 return gdb_syscall_mode == GDB_SYS_ENABLED;
380}
381
edgar_iglba70a622008-03-14 06:10:42 +0000382/* Resume execution. */
383static inline void gdb_continue(GDBState *s)
384{
385#ifdef CONFIG_USER_ONLY
386 s->running_state = 1;
387#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200388 if (!runstate_needs_reset()) {
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200389 vm_start();
390 }
edgar_iglba70a622008-03-14 06:10:42 +0000391#endif
392}
393
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100394/*
395 * Resume execution, per CPU actions. For user-mode emulation it's
396 * equivalent to gdb_continue.
397 */
398static int gdb_continue_partial(GDBState *s, char *newstates)
399{
400 CPUState *cpu;
401 int res = 0;
402#ifdef CONFIG_USER_ONLY
403 /*
404 * This is not exactly accurate, but it's an improvement compared to the
405 * previous situation, where only one CPU would be single-stepped.
406 */
407 CPU_FOREACH(cpu) {
408 if (newstates[cpu->cpu_index] == 's') {
409 cpu_single_step(cpu, sstep_flags);
410 }
411 }
412 s->running_state = 1;
413#else
414 int flag = 0;
415
416 if (!runstate_needs_reset()) {
417 if (vm_prepare_start()) {
418 return 0;
419 }
420
421 CPU_FOREACH(cpu) {
422 switch (newstates[cpu->cpu_index]) {
423 case 0:
424 case 1:
425 break; /* nothing to do here */
426 case 's':
427 cpu_single_step(cpu, sstep_flags);
428 cpu_resume(cpu);
429 flag = 1;
430 break;
431 case 'c':
432 cpu_resume(cpu);
433 flag = 1;
434 break;
435 default:
436 res = -1;
437 break;
438 }
439 }
440 }
441 if (flag) {
442 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
443 }
444#endif
445 return res;
446}
447
bellard858693c2004-03-31 18:52:07 +0000448static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000449{
pbrook4046d912007-01-28 01:53:16 +0000450#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000451 int ret;
452
453 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000454 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000455 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200456 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000457 return;
458 } else {
459 buf += ret;
460 len -= ret;
461 }
462 }
pbrook4046d912007-01-28 01:53:16 +0000463#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100464 /* XXX this blocks entire thread. Rewrite to use
465 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300466 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000467#endif
bellardb4608c02003-06-27 17:34:32 +0000468}
469
470static inline int fromhex(int v)
471{
472 if (v >= '0' && v <= '9')
473 return v - '0';
474 else if (v >= 'A' && v <= 'F')
475 return v - 'A' + 10;
476 else if (v >= 'a' && v <= 'f')
477 return v - 'a' + 10;
478 else
479 return 0;
480}
481
482static inline int tohex(int v)
483{
484 if (v < 10)
485 return v + '0';
486 else
487 return v - 10 + 'a';
488}
489
490static void memtohex(char *buf, const uint8_t *mem, int len)
491{
492 int i, c;
493 char *q;
494 q = buf;
495 for(i = 0; i < len; i++) {
496 c = mem[i];
497 *q++ = tohex(c >> 4);
498 *q++ = tohex(c & 0xf);
499 }
500 *q = '\0';
501}
502
503static void hextomem(uint8_t *mem, const char *buf, int len)
504{
505 int i;
506
507 for(i = 0; i < len; i++) {
508 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
509 buf += 2;
510 }
511}
512
bellardb4608c02003-06-27 17:34:32 +0000513/* return -1 if error, 0 if OK */
pbrook56aebc82008-10-11 17:55:29 +0000514static int put_packet_binary(GDBState *s, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000515{
pbrook56aebc82008-10-11 17:55:29 +0000516 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000517 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000518
bellardb4608c02003-06-27 17:34:32 +0000519 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000520 p = s->last_packet;
521 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000522 memcpy(p, buf, len);
523 p += len;
bellardb4608c02003-06-27 17:34:32 +0000524 csum = 0;
525 for(i = 0; i < len; i++) {
526 csum += buf[i];
527 }
pbrook4046d912007-01-28 01:53:16 +0000528 *(p++) = '#';
529 *(p++) = tohex((csum >> 4) & 0xf);
530 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000531
pbrook4046d912007-01-28 01:53:16 +0000532 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000533 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000534
pbrook4046d912007-01-28 01:53:16 +0000535#ifdef CONFIG_USER_ONLY
536 i = get_char(s);
537 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000538 return -1;
pbrook4046d912007-01-28 01:53:16 +0000539 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000540 break;
pbrook4046d912007-01-28 01:53:16 +0000541#else
542 break;
543#endif
bellardb4608c02003-06-27 17:34:32 +0000544 }
545 return 0;
546}
547
pbrook56aebc82008-10-11 17:55:29 +0000548/* return -1 if error, 0 if OK */
549static int put_packet(GDBState *s, const char *buf)
550{
551#ifdef DEBUG_GDB
552 printf("reply='%s'\n", buf);
553#endif
554
555 return put_packet_binary(s, buf, strlen(buf));
556}
557
pbrook56aebc82008-10-11 17:55:29 +0000558/* Encode data using the encoding for 'x' packets. */
559static int memtox(char *buf, const char *mem, int len)
560{
561 char *p = buf;
562 char c;
563
564 while (len--) {
565 c = *(mem++);
566 switch (c) {
567 case '#': case '$': case '*': case '}':
568 *(p++) = '}';
569 *(p++) = c ^ 0x20;
570 break;
571 default:
572 *(p++) = c;
573 break;
574 }
575 }
576 return p - buf;
577}
578
Andreas Färber5b24c642013-07-07 15:08:22 +0200579static const char *get_feature_xml(const char *p, const char **newp,
580 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000581{
pbrook56aebc82008-10-11 17:55:29 +0000582 size_t len;
583 int i;
584 const char *name;
585 static char target_xml[1024];
586
587 len = 0;
588 while (p[len] && p[len] != ':')
589 len++;
590 *newp = p + len;
591
592 name = NULL;
593 if (strncmp(p, "target.xml", len) == 0) {
594 /* Generate the XML description for this CPU. */
595 if (!target_xml[0]) {
596 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200597 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000598
David Hildenbrandb3820e62015-12-03 13:14:41 +0100599 pstrcat(target_xml, sizeof(target_xml),
600 "<?xml version=\"1.0\"?>"
601 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
602 "<target>");
603 if (cc->gdb_arch_name) {
604 gchar *arch = cc->gdb_arch_name(cpu);
605 pstrcat(target_xml, sizeof(target_xml), "<architecture>");
606 pstrcat(target_xml, sizeof(target_xml), arch);
607 pstrcat(target_xml, sizeof(target_xml), "</architecture>");
608 g_free(arch);
609 }
610 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
611 pstrcat(target_xml, sizeof(target_xml), cc->gdb_core_xml_file);
612 pstrcat(target_xml, sizeof(target_xml), "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200613 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000614 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
615 pstrcat(target_xml, sizeof(target_xml), r->xml);
616 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000617 }
blueswir12dc766d2009-04-13 16:06:19 +0000618 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000619 }
620 return target_xml;
621 }
622 for (i = 0; ; i++) {
623 name = xml_builtin[i][0];
624 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
625 break;
626 }
627 return name ? xml_builtin[i][1] : NULL;
628}
pbrook56aebc82008-10-11 17:55:29 +0000629
Andreas Färber385b9f02013-06-27 18:25:36 +0200630static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000631{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200632 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200633 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000634 GDBRegisterState *r;
635
Andreas Färbera0e372f2013-06-28 23:18:47 +0200636 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200637 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200638 }
pbrook56aebc82008-10-11 17:55:29 +0000639
Andreas Färbereac8b352013-06-28 21:11:37 +0200640 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000641 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
642 return r->get_reg(env, mem_buf, reg - r->base_reg);
643 }
644 }
645 return 0;
646}
647
Andreas Färber385b9f02013-06-27 18:25:36 +0200648static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000649{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200650 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200651 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000652 GDBRegisterState *r;
653
Andreas Färbera0e372f2013-06-28 23:18:47 +0200654 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200655 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200656 }
pbrook56aebc82008-10-11 17:55:29 +0000657
Andreas Färbereac8b352013-06-28 21:11:37 +0200658 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000659 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
660 return r->set_reg(env, mem_buf, reg - r->base_reg);
661 }
662 }
663 return 0;
664}
665
666/* Register a supplemental set of CPU registers. If g_pos is nonzero it
667 specifies the first register number and these registers are included in
668 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
669 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
670 */
671
Andreas Färber22169d42013-06-28 21:27:39 +0200672void gdb_register_coprocessor(CPUState *cpu,
673 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
674 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000675{
676 GDBRegisterState *s;
677 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000678
Andreas Färbereac8b352013-06-28 21:11:37 +0200679 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000680 while (*p) {
681 /* Check for duplicates. */
682 if (strcmp((*p)->xml, xml) == 0)
683 return;
684 p = &(*p)->next;
685 }
Stefan Weil9643c252011-10-18 22:25:38 +0200686
687 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200688 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200689 s->num_regs = num_regs;
690 s->get_reg = get_reg;
691 s->set_reg = set_reg;
692 s->xml = xml;
693
pbrook56aebc82008-10-11 17:55:29 +0000694 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200695 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000696 *p = s;
697 if (g_pos) {
698 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800699 error_report("Error: Bad gdb register numbering for '%s', "
700 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200701 } else {
702 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000703 }
704 }
705}
706
aliguoria1d1bb32008-11-18 20:07:32 +0000707#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100708/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
709static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
710{
711 static const int xlat[] = {
712 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
713 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
714 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
715 };
716
717 CPUClass *cc = CPU_GET_CLASS(cpu);
718 int cputype = xlat[gdbtype];
719
720 if (cc->gdb_stop_before_watchpoint) {
721 cputype |= BP_STOP_BEFORE_ACCESS;
722 }
723 return cputype;
724}
aliguoria1d1bb32008-11-18 20:07:32 +0000725#endif
726
aliguori880a7572008-11-18 20:30:24 +0000727static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000728{
Andreas Färber182735e2013-05-29 22:29:20 +0200729 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000730 int err = 0;
731
Andreas Färber62278812013-06-27 17:12:06 +0200732 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200733 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200734 }
aliguorie22a25c2009-03-12 20:12:48 +0000735
aliguoria1d1bb32008-11-18 20:07:32 +0000736 switch (type) {
737 case GDB_BREAKPOINT_SW:
738 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200739 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200740 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
741 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000742 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200743 }
aliguori880a7572008-11-18 20:30:24 +0000744 }
745 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000746#ifndef CONFIG_USER_ONLY
747 case GDB_WATCHPOINT_WRITE:
748 case GDB_WATCHPOINT_READ:
749 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200750 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100751 err = cpu_watchpoint_insert(cpu, addr, len,
752 xlat_gdb_type(cpu, type), NULL);
753 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000754 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100755 }
aliguori880a7572008-11-18 20:30:24 +0000756 }
757 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000758#endif
759 default:
760 return -ENOSYS;
761 }
762}
763
aliguori880a7572008-11-18 20:30:24 +0000764static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000765{
Andreas Färber182735e2013-05-29 22:29:20 +0200766 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000767 int err = 0;
768
Andreas Färber62278812013-06-27 17:12:06 +0200769 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200770 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200771 }
aliguorie22a25c2009-03-12 20:12:48 +0000772
aliguoria1d1bb32008-11-18 20:07:32 +0000773 switch (type) {
774 case GDB_BREAKPOINT_SW:
775 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200776 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200777 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
778 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000779 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200780 }
aliguori880a7572008-11-18 20:30:24 +0000781 }
782 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000783#ifndef CONFIG_USER_ONLY
784 case GDB_WATCHPOINT_WRITE:
785 case GDB_WATCHPOINT_READ:
786 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200787 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100788 err = cpu_watchpoint_remove(cpu, addr, len,
789 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +0000790 if (err)
791 break;
792 }
793 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000794#endif
795 default:
796 return -ENOSYS;
797 }
798}
799
aliguori880a7572008-11-18 20:30:24 +0000800static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000801{
Andreas Färber182735e2013-05-29 22:29:20 +0200802 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000803
aliguorie22a25c2009-03-12 20:12:48 +0000804 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200805 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000806 return;
807 }
808
Andreas Färberbdc44642013-06-24 23:50:24 +0200809 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200810 cpu_breakpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000811#ifndef CONFIG_USER_ONLY
Andreas Färber75a34032013-09-02 16:57:02 +0200812 cpu_watchpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000813#endif
aliguori880a7572008-11-18 20:30:24 +0000814 }
aliguoria1d1bb32008-11-18 20:07:32 +0000815}
816
aurel32fab9d282009-04-08 21:29:37 +0000817static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
818{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200819 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200820
821 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -0700822 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +0000823}
824
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200825static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700826{
Andreas Färber0d342822012-12-17 07:12:13 +0100827 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700828
Andreas Färberbdc44642013-06-24 23:50:24 +0200829 CPU_FOREACH(cpu) {
Andreas Färberaa48dd92013-07-09 20:50:52 +0200830 if (cpu_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200831 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200832 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700833 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200834
835 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700836}
837
Jan Kiszka4dabe742015-02-07 09:38:43 +0100838static int is_query_packet(const char *p, const char *query, char separator)
839{
840 unsigned int query_len = strlen(query);
841
842 return strncmp(p, query, query_len) == 0 &&
843 (p[query_len] == '\0' || p[query_len] == separator);
844}
845
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100846/**
847 * gdb_handle_vcont - Parses and handles a vCont packet.
848 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
849 * a format error, 0 on success.
850 */
851static int gdb_handle_vcont(GDBState *s, const char *p)
852{
853 int res, idx, signal = 0;
854 char cur_action;
855 char *newstates;
856 unsigned long tmp;
857 CPUState *cpu;
858#ifdef CONFIG_USER_ONLY
859 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
860
861 CPU_FOREACH(cpu) {
862 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
863 }
864#endif
865 /* uninitialised CPUs stay 0 */
866 newstates = g_new0(char, max_cpus);
867
868 /* mark valid CPUs with 1 */
869 CPU_FOREACH(cpu) {
870 newstates[cpu->cpu_index] = 1;
871 }
872
873 /*
874 * res keeps track of what error we are returning, with -ENOTSUP meaning
875 * that the command is unknown or unsupported, thus returning an empty
876 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
877 * or incorrect parameters passed.
878 */
879 res = 0;
880 while (*p) {
881 if (*p++ != ';') {
882 res = -ENOTSUP;
883 goto out;
884 }
885
886 cur_action = *p++;
887 if (cur_action == 'C' || cur_action == 'S') {
888 cur_action = tolower(cur_action);
889 res = qemu_strtoul(p + 1, &p, 16, &tmp);
890 if (res) {
891 goto out;
892 }
893 signal = gdb_signal_to_target(tmp);
894 } else if (cur_action != 'c' && cur_action != 's') {
895 /* unknown/invalid/unsupported command */
896 res = -ENOTSUP;
897 goto out;
898 }
899 /* thread specification. special values: (none), -1 = all; 0 = any */
900 if ((p[0] == ':' && p[1] == '-' && p[2] == '1') || (p[0] != ':')) {
901 if (*p == ':') {
902 p += 3;
903 }
904 for (idx = 0; idx < max_cpus; idx++) {
905 if (newstates[idx] == 1) {
906 newstates[idx] = cur_action;
907 }
908 }
909 } else if (*p == ':') {
910 p++;
911 res = qemu_strtoul(p, &p, 16, &tmp);
912 if (res) {
913 goto out;
914 }
915 idx = tmp;
916 /* 0 means any thread, so we pick the first valid CPU */
917 if (!idx) {
918 idx = cpu_index(first_cpu);
919 }
920
921 /*
922 * If we are in user mode, the thread specified is actually a
923 * thread id, and not an index. We need to find the actual
924 * CPU first, and only then we can use its index.
925 */
926 cpu = find_cpu(idx);
927 /* invalid CPU/thread specified */
928 if (!idx || !cpu) {
929 res = -EINVAL;
930 goto out;
931 }
932 /* only use if no previous match occourred */
933 if (newstates[cpu->cpu_index] == 1) {
934 newstates[cpu->cpu_index] = cur_action;
935 }
936 }
937 }
938 s->signal = signal;
939 gdb_continue_partial(s, newstates);
940
941out:
942 g_free(newstates);
943
944 return res;
945}
946
aliguori880a7572008-11-18 20:30:24 +0000947static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000948{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200949 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +0200950 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +0000951 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700952 uint32_t thread;
953 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +0000954 char buf[MAX_PACKET_LENGTH];
955 uint8_t mem_buf[MAX_PACKET_LENGTH];
956 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +0000957 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +0000958
bellard858693c2004-03-31 18:52:07 +0000959#ifdef DEBUG_GDB
960 printf("command='%s'\n", line_buf);
bellard4c3a88a2003-07-26 12:06:08 +0000961#endif
bellard858693c2004-03-31 18:52:07 +0000962 p = line_buf;
963 ch = *p++;
964 switch(ch) {
965 case '?':
bellard1fddef42005-04-17 19:16:13 +0000966 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +0000967 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200968 cpu_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +0000969 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +0000970 /* Remove all the breakpoints when this query is issued,
971 * because gdb is doing and initial connect and the state
972 * should be cleaned up.
973 */
aliguori880a7572008-11-18 20:30:24 +0000974 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +0000975 break;
976 case 'c':
977 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +0000978 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000979 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000980 }
aurel32ca587a82008-12-18 22:44:13 +0000981 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +0000982 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000983 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +0000984 case 'C':
aurel32ca587a82008-12-18 22:44:13 +0000985 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
986 if (s->signal == -1)
987 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +0000988 gdb_continue(s);
989 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200990 case 'v':
991 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200992 p += 4;
993 if (*p == '?') {
994 put_packet(s, "vCont;c;C;s;S");
995 break;
996 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200997
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100998 res = gdb_handle_vcont(s, p);
999
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001000 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001001 if ((res == -EINVAL) || (res == -ERANGE)) {
1002 put_packet(s, "E22");
1003 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001004 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001005 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001006 }
1007 break;
1008 } else {
1009 goto unknown_command;
1010 }
edgar_igl7d03f822008-05-17 18:58:29 +00001011 case 'k':
1012 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001013 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001014 exit(0);
1015 case 'D':
1016 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +00001017 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -03001018 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +00001019 gdb_continue(s);
1020 put_packet(s, "OK");
1021 break;
bellard858693c2004-03-31 18:52:07 +00001022 case 's':
1023 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001024 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001025 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001026 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001027 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001028 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001029 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001030 case 'F':
1031 {
1032 target_ulong ret;
1033 target_ulong err;
1034
1035 ret = strtoull(p, (char **)&p, 16);
1036 if (*p == ',') {
1037 p++;
1038 err = strtoull(p, (char **)&p, 16);
1039 } else {
1040 err = 0;
1041 }
1042 if (*p == ',')
1043 p++;
1044 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001045 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001046 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001047 s->current_syscall_cb = NULL;
1048 }
pbrooka2d1eba2007-01-28 03:10:55 +00001049 if (type == 'C') {
1050 put_packet(s, "T02");
1051 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001052 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001053 }
1054 }
1055 break;
bellard858693c2004-03-31 18:52:07 +00001056 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001057 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001058 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001059 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001060 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001061 len += reg_size;
1062 }
1063 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001064 put_packet(s, buf);
1065 break;
1066 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001067 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001068 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001069 len = strlen(p) / 2;
1070 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001071 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001072 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001073 len -= reg_size;
1074 registers += reg_size;
1075 }
bellard858693c2004-03-31 18:52:07 +00001076 put_packet(s, "OK");
1077 break;
1078 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001079 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001080 if (*p == ',')
1081 p++;
bellard9d9754a2006-06-25 15:32:37 +00001082 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001083
1084 /* memtohex() doubles the required space */
1085 if (len > MAX_PACKET_LENGTH / 2) {
1086 put_packet (s, "E22");
1087 break;
1088 }
1089
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001090 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001091 put_packet (s, "E14");
1092 } else {
1093 memtohex(buf, mem_buf, len);
1094 put_packet(s, buf);
1095 }
bellard858693c2004-03-31 18:52:07 +00001096 break;
1097 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001098 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001099 if (*p == ',')
1100 p++;
bellard9d9754a2006-06-25 15:32:37 +00001101 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001102 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001103 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001104
1105 /* hextomem() reads 2*len bytes */
1106 if (len > strlen(p) / 2) {
1107 put_packet (s, "E22");
1108 break;
1109 }
bellard858693c2004-03-31 18:52:07 +00001110 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001111 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001112 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001113 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001114 } else {
bellard858693c2004-03-31 18:52:07 +00001115 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001116 }
bellard858693c2004-03-31 18:52:07 +00001117 break;
pbrook56aebc82008-10-11 17:55:29 +00001118 case 'p':
1119 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1120 This works, but can be very slow. Anything new enough to
1121 understand XML also knows how to use this properly. */
1122 if (!gdb_has_xml)
1123 goto unknown_command;
1124 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001125 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001126 if (reg_size) {
1127 memtohex(buf, mem_buf, reg_size);
1128 put_packet(s, buf);
1129 } else {
1130 put_packet(s, "E14");
1131 }
1132 break;
1133 case 'P':
1134 if (!gdb_has_xml)
1135 goto unknown_command;
1136 addr = strtoull(p, (char **)&p, 16);
1137 if (*p == '=')
1138 p++;
1139 reg_size = strlen(p) / 2;
1140 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001141 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001142 put_packet(s, "OK");
1143 break;
bellard858693c2004-03-31 18:52:07 +00001144 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001145 case 'z':
1146 type = strtoul(p, (char **)&p, 16);
1147 if (*p == ',')
1148 p++;
bellard9d9754a2006-06-25 15:32:37 +00001149 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001150 if (*p == ',')
1151 p++;
bellard9d9754a2006-06-25 15:32:37 +00001152 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001153 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001154 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001155 else
aliguori880a7572008-11-18 20:30:24 +00001156 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001157 if (res >= 0)
1158 put_packet(s, "OK");
1159 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001160 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001161 else
1162 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001163 break;
aliguori880a7572008-11-18 20:30:24 +00001164 case 'H':
1165 type = *p++;
1166 thread = strtoull(p, (char **)&p, 16);
1167 if (thread == -1 || thread == 0) {
1168 put_packet(s, "OK");
1169 break;
1170 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001171 cpu = find_cpu(thread);
1172 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001173 put_packet(s, "E22");
1174 break;
1175 }
1176 switch (type) {
1177 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001178 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001179 put_packet(s, "OK");
1180 break;
1181 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001182 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001183 put_packet(s, "OK");
1184 break;
1185 default:
1186 put_packet(s, "E22");
1187 break;
1188 }
1189 break;
1190 case 'T':
1191 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001192 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001193
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001194 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001195 put_packet(s, "OK");
1196 } else {
aliguori880a7572008-11-18 20:30:24 +00001197 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001198 }
aliguori880a7572008-11-18 20:30:24 +00001199 break;
pbrook978efd62006-06-17 18:30:42 +00001200 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001201 case 'Q':
1202 /* parse any 'q' packets here */
1203 if (!strcmp(p,"qemu.sstepbits")) {
1204 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001205 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1206 SSTEP_ENABLE,
1207 SSTEP_NOIRQ,
1208 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001209 put_packet(s, buf);
1210 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001211 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001212 /* Display or change the sstep_flags */
1213 p += 10;
1214 if (*p != '=') {
1215 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001216 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001217 put_packet(s, buf);
1218 break;
1219 }
1220 p++;
1221 type = strtoul(p, (char **)&p, 16);
1222 sstep_flags = type;
1223 put_packet(s, "OK");
1224 break;
aliguori880a7572008-11-18 20:30:24 +00001225 } else if (strcmp(p,"C") == 0) {
1226 /* "Current thread" remains vague in the spec, so always return
1227 * the first CPU (gdb returns the first thread). */
1228 put_packet(s, "QC1");
1229 break;
1230 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001231 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001232 goto report_cpuinfo;
1233 } else if (strcmp(p,"sThreadInfo") == 0) {
1234 report_cpuinfo:
1235 if (s->query_cpu) {
Andreas Färber52f34622013-06-27 13:44:40 +02001236 snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001237 put_packet(s, buf);
Andreas Färberbdc44642013-06-24 23:50:24 +02001238 s->query_cpu = CPU_NEXT(s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001239 } else
1240 put_packet(s, "l");
1241 break;
1242 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1243 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001244 cpu = find_cpu(thread);
1245 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001246 cpu_synchronize_state(cpu);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001247 /* memtohex() doubles the required space */
1248 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
Andreas Färber55e5c282012-12-17 06:18:02 +01001249 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001250 cpu->halted ? "halted " : "running");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001251 memtohex(buf, mem_buf, len);
1252 put_packet(s, buf);
1253 }
aliguori880a7572008-11-18 20:30:24 +00001254 break;
edgar_igl60897d32008-05-09 08:25:14 +00001255 }
blueswir10b8a9882009-03-07 10:51:36 +00001256#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001257 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001258 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001259
blueswir1363a37d2008-08-21 17:58:08 +00001260 snprintf(buf, sizeof(buf),
1261 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1262 ";Bss=" TARGET_ABI_FMT_lx,
1263 ts->info->code_offset,
1264 ts->info->data_offset,
1265 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001266 put_packet(s, buf);
1267 break;
1268 }
blueswir10b8a9882009-03-07 10:51:36 +00001269#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001270 else if (strncmp(p, "Rcmd,", 5) == 0) {
1271 int len = strlen(p + 5);
1272
1273 if ((len % 2) != 0) {
1274 put_packet(s, "E01");
1275 break;
1276 }
aliguori8a34a0f2009-03-05 23:01:55 +00001277 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001278 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001279 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001280 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001281 put_packet(s, "OK");
1282 break;
1283 }
blueswir10b8a9882009-03-07 10:51:36 +00001284#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001285 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001286 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001287 cc = CPU_GET_CLASS(first_cpu);
1288 if (cc->gdb_core_xml_file != NULL) {
1289 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1290 }
pbrook56aebc82008-10-11 17:55:29 +00001291 put_packet(s, buf);
1292 break;
1293 }
pbrook56aebc82008-10-11 17:55:29 +00001294 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1295 const char *xml;
1296 target_ulong total_len;
1297
Andreas Färber5b24c642013-07-07 15:08:22 +02001298 cc = CPU_GET_CLASS(first_cpu);
1299 if (cc->gdb_core_xml_file == NULL) {
1300 goto unknown_command;
1301 }
1302
Andreas Färber5b50e792013-06-29 04:18:45 +02001303 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001304 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001305 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001306 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001307 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001308 put_packet(s, buf);
1309 break;
1310 }
1311
1312 if (*p == ':')
1313 p++;
1314 addr = strtoul(p, (char **)&p, 16);
1315 if (*p == ',')
1316 p++;
1317 len = strtoul(p, (char **)&p, 16);
1318
1319 total_len = strlen(xml);
1320 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001321 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001322 put_packet(s, buf);
1323 break;
1324 }
1325 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1326 len = (MAX_PACKET_LENGTH - 5) / 2;
1327 if (len < total_len - addr) {
1328 buf[0] = 'm';
1329 len = memtox(buf + 1, xml + addr, len);
1330 } else {
1331 buf[0] = 'l';
1332 len = memtox(buf + 1, xml + addr, total_len - addr);
1333 }
1334 put_packet_binary(s, buf, len + 1);
1335 break;
1336 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001337 if (is_query_packet(p, "Attached", ':')) {
1338 put_packet(s, GDB_ATTACHED);
1339 break;
1340 }
pbrook56aebc82008-10-11 17:55:29 +00001341 /* Unrecognised 'q' command. */
1342 goto unknown_command;
1343
bellard858693c2004-03-31 18:52:07 +00001344 default:
pbrook56aebc82008-10-11 17:55:29 +00001345 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001346 /* put empty packet */
1347 buf[0] = '\0';
1348 put_packet(s, buf);
1349 break;
1350 }
1351 return RS_IDLE;
1352}
1353
Andreas Färber64f6b342013-05-27 02:06:09 +02001354void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001355{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001356 gdbserver_state->c_cpu = cpu;
1357 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001358}
1359
bellard1fddef42005-04-17 19:16:13 +00001360#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001361static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001362{
aliguori880a7572008-11-18 20:30:24 +00001363 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001364 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001365 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001366 const char *type;
bellard858693c2004-03-31 18:52:07 +00001367 int ret;
1368
Meador Ingecdb432b2012-03-15 17:49:45 +00001369 if (running || s->state == RS_INACTIVE) {
1370 return;
1371 }
1372 /* Is there a GDB syscall waiting to be sent? */
1373 if (s->current_syscall_cb) {
1374 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001375 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001376 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001377 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001378 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001379 if (cpu->watchpoint_hit) {
1380 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001381 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001382 type = "r";
1383 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001384 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001385 type = "a";
1386 break;
1387 default:
1388 type = "";
1389 break;
1390 }
aliguori880a7572008-11-18 20:30:24 +00001391 snprintf(buf, sizeof(buf),
1392 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Andreas Färber0d342822012-12-17 07:12:13 +01001393 GDB_SIGNAL_TRAP, cpu_index(cpu), type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001394 (target_ulong)cpu->watchpoint_hit->vaddr);
1395 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001396 goto send_packet;
pbrook6658ffb2007-03-16 23:58:11 +00001397 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001398 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001399 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001400 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001401 case RUN_STATE_PAUSED:
aliguori9781e042009-01-22 17:15:29 +00001402 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001403 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001404 case RUN_STATE_SHUTDOWN:
Jan Kiszka425189a2011-03-22 11:02:09 +01001405 ret = GDB_SIGNAL_QUIT;
1406 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001407 case RUN_STATE_IO_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001408 ret = GDB_SIGNAL_IO;
1409 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001410 case RUN_STATE_WATCHDOG:
Jan Kiszka425189a2011-03-22 11:02:09 +01001411 ret = GDB_SIGNAL_ALRM;
1412 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001413 case RUN_STATE_INTERNAL_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001414 ret = GDB_SIGNAL_ABRT;
1415 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001416 case RUN_STATE_SAVE_VM:
1417 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001418 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001419 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001420 ret = GDB_SIGNAL_XCPU;
1421 break;
1422 default:
1423 ret = GDB_SIGNAL_UNKNOWN;
1424 break;
bellardbbeb7b52006-04-23 18:42:15 +00001425 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001426 gdb_set_stop_cpu(cpu);
Andreas Färber0d342822012-12-17 07:12:13 +01001427 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001428
1429send_packet:
bellard858693c2004-03-31 18:52:07 +00001430 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001431
1432 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001433 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001434}
bellard1fddef42005-04-17 19:16:13 +00001435#endif
bellard858693c2004-03-31 18:52:07 +00001436
pbrooka2d1eba2007-01-28 03:10:55 +00001437/* Send a gdb syscall request.
1438 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001439 %x - target_ulong argument printed in hex.
1440 %lx - 64-bit argument printed in hex.
1441 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001442void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001443{
pbrooka2d1eba2007-01-28 03:10:55 +00001444 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001445 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001446 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001447 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001448 GDBState *s;
1449
aliguori880a7572008-11-18 20:30:24 +00001450 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001451 if (!s)
1452 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001453 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001454#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001455 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001456#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001457 p = s->syscall_buf;
1458 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001459 *(p++) = 'F';
1460 while (*fmt) {
1461 if (*fmt == '%') {
1462 fmt++;
1463 switch (*fmt++) {
1464 case 'x':
1465 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001466 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001467 break;
pbrooka87295e2007-05-26 15:09:38 +00001468 case 'l':
1469 if (*(fmt++) != 'x')
1470 goto bad_format;
1471 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001472 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001473 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001474 case 's':
1475 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001476 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001477 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001478 break;
1479 default:
pbrooka87295e2007-05-26 15:09:38 +00001480 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001481 error_report("gdbstub: Bad syscall format string '%s'",
1482 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001483 break;
1484 }
1485 } else {
1486 *(p++) = *(fmt++);
1487 }
1488 }
pbrook8a93e022007-08-06 13:19:15 +00001489 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001490#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001491 put_packet(s, s->syscall_buf);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001492 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001493#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001494 /* In this case wait to send the syscall packet until notification that
1495 the CPU has stopped. This must be done because if the packet is sent
1496 now the reply from the syscall request could be received while the CPU
1497 is still in the running state, which can cause packets to be dropped
1498 and state transition 'T' packets to be sent while the syscall is still
1499 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001500 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001501#endif
1502}
1503
Peter Maydell19239b32015-09-07 10:39:27 +01001504void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1505{
1506 va_list va;
1507
1508 va_start(va, fmt);
1509 gdb_do_syscallv(cb, fmt, va);
1510 va_end(va);
1511}
1512
bellard6a00d602005-11-21 23:25:50 +00001513static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001514{
ths60fe76f2007-12-16 03:02:09 +00001515 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001516
bellard1fddef42005-04-17 19:16:13 +00001517#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001518 if (s->last_packet_len) {
1519 /* Waiting for a response to the last packet. If we see the start
1520 of a new command then abandon the previous response. */
1521 if (ch == '-') {
1522#ifdef DEBUG_GDB
1523 printf("Got NACK, retransmitting\n");
1524#endif
thsffe8ab82007-12-16 03:16:05 +00001525 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
pbrook4046d912007-01-28 01:53:16 +00001526 }
1527#ifdef DEBUG_GDB
1528 else if (ch == '+')
1529 printf("Got ACK\n");
1530 else
1531 printf("Got '%c' when expecting ACK/NACK\n", ch);
1532#endif
1533 if (ch == '+' || ch == '$')
1534 s->last_packet_len = 0;
1535 if (ch != '$')
1536 return;
1537 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001538 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001539 /* when the CPU is running, we cannot do anything except stop
1540 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001541 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001542 } else
bellard1fddef42005-04-17 19:16:13 +00001543#endif
bellard41625032005-04-24 10:07:11 +00001544 {
bellard858693c2004-03-31 18:52:07 +00001545 switch(s->state) {
1546 case RS_IDLE:
1547 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04001548 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00001549 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04001550 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00001551 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04001552 } else {
1553#ifdef DEBUG_GDB
1554 printf("gdbstub received garbage between packets: 0x%x\n", ch);
1555#endif
bellard4c3a88a2003-07-26 12:06:08 +00001556 }
1557 break;
bellard858693c2004-03-31 18:52:07 +00001558 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04001559 if (ch == '}') {
1560 /* start escape sequence */
1561 s->state = RS_GETLINE_ESC;
1562 s->line_sum += ch;
1563 } else if (ch == '*') {
1564 /* start run length encoding sequence */
1565 s->state = RS_GETLINE_RLE;
1566 s->line_sum += ch;
1567 } else if (ch == '#') {
1568 /* end of command, start of checksum*/
1569 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00001570 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04001571#ifdef DEBUG_GDB
1572 printf("gdbstub command buffer overrun, dropping command\n");
1573#endif
bellard858693c2004-03-31 18:52:07 +00001574 s->state = RS_IDLE;
1575 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001576 /* unescaped command character */
1577 s->line_buf[s->line_buf_index++] = ch;
1578 s->line_sum += ch;
1579 }
1580 break;
1581 case RS_GETLINE_ESC:
1582 if (ch == '#') {
1583 /* unexpected end of command in escape sequence */
1584 s->state = RS_CHKSUM1;
1585 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1586 /* command buffer overrun */
1587#ifdef DEBUG_GDB
1588 printf("gdbstub command buffer overrun, dropping command\n");
1589#endif
1590 s->state = RS_IDLE;
1591 } else {
1592 /* parse escaped character and leave escape state */
1593 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
1594 s->line_sum += ch;
1595 s->state = RS_GETLINE;
1596 }
1597 break;
1598 case RS_GETLINE_RLE:
1599 if (ch < ' ') {
1600 /* invalid RLE count encoding */
1601#ifdef DEBUG_GDB
1602 printf("gdbstub got invalid RLE count: 0x%x\n", ch);
1603#endif
1604 s->state = RS_GETLINE;
1605 } else {
1606 /* decode repeat length */
1607 int repeat = (unsigned char)ch - ' ' + 3;
1608 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
1609 /* that many repeats would overrun the command buffer */
1610#ifdef DEBUG_GDB
1611 printf("gdbstub command buffer overrun,"
1612 " dropping command\n");
1613#endif
1614 s->state = RS_IDLE;
1615 } else if (s->line_buf_index < 1) {
1616 /* got a repeat but we have nothing to repeat */
1617#ifdef DEBUG_GDB
1618 printf("gdbstub got invalid RLE sequence\n");
1619#endif
1620 s->state = RS_GETLINE;
1621 } else {
1622 /* repeat the last character */
1623 memset(s->line_buf + s->line_buf_index,
1624 s->line_buf[s->line_buf_index - 1], repeat);
1625 s->line_buf_index += repeat;
1626 s->line_sum += ch;
1627 s->state = RS_GETLINE;
1628 }
bellard858693c2004-03-31 18:52:07 +00001629 }
1630 break;
1631 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04001632 /* get high hex digit of checksum */
1633 if (!isxdigit(ch)) {
1634#ifdef DEBUG_GDB
1635 printf("gdbstub got invalid command checksum digit\n");
1636#endif
1637 s->state = RS_GETLINE;
1638 break;
1639 }
bellard858693c2004-03-31 18:52:07 +00001640 s->line_buf[s->line_buf_index] = '\0';
1641 s->line_csum = fromhex(ch) << 4;
1642 s->state = RS_CHKSUM2;
1643 break;
1644 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04001645 /* get low hex digit of checksum */
1646 if (!isxdigit(ch)) {
1647#ifdef DEBUG_GDB
1648 printf("gdbstub got invalid command checksum digit\n");
1649#endif
1650 s->state = RS_GETLINE;
1651 break;
bellard858693c2004-03-31 18:52:07 +00001652 }
Doug Gale4bf43122017-05-01 12:22:10 -04001653 s->line_csum |= fromhex(ch);
1654
1655 if (s->line_csum != (s->line_sum & 0xff)) {
1656 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00001657 reply = '-';
1658 put_buffer(s, &reply, 1);
Doug Gale4bf43122017-05-01 12:22:10 -04001659#ifdef DEBUG_GDB
1660 printf("gdbstub got command packet with incorrect checksum\n");
1661#endif
bellard858693c2004-03-31 18:52:07 +00001662 s->state = RS_IDLE;
1663 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001664 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00001665 reply = '+';
1666 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001667 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001668 }
bellardb4608c02003-06-27 17:34:32 +00001669 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001670 default:
1671 abort();
bellardb4608c02003-06-27 17:34:32 +00001672 }
1673 }
bellard858693c2004-03-31 18:52:07 +00001674}
1675
Paul Brook0e1c9c52010-06-16 13:03:51 +01001676/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001677void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001678{
1679 GDBState *s;
1680 char buf[4];
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001681#ifndef CONFIG_USER_ONLY
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001682 Chardev *chr;
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001683#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001684
1685 s = gdbserver_state;
1686 if (!s) {
1687 return;
1688 }
1689#ifdef CONFIG_USER_ONLY
1690 if (gdbserver_fd < 0 || s->fd < 0) {
1691 return;
1692 }
Paolo Bonzini3d0f4412015-03-02 13:26:58 +01001693#else
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001694 chr = qemu_chr_fe_get_driver(&s->chr);
1695 if (!chr) {
Paolo Bonzini3d0f4412015-03-02 13:26:58 +01001696 return;
1697 }
Paul Brook0e1c9c52010-06-16 13:03:51 +01001698#endif
1699
1700 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1701 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001702
1703#ifndef CONFIG_USER_ONLY
Marc-André Lureauc39860e2016-10-22 12:52:58 +03001704 qemu_chr_fe_deinit(&s->chr);
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +03001705 object_unparent(OBJECT(chr));
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001706#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001707}
1708
bellard1fddef42005-04-17 19:16:13 +00001709#ifdef CONFIG_USER_ONLY
1710int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001711gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001712{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001713 GDBState *s;
1714 char buf[256];
1715 int n;
bellard1fddef42005-04-17 19:16:13 +00001716
Andreas Färber5ca666c2013-06-24 19:20:57 +02001717 s = gdbserver_state;
1718 if (gdbserver_fd < 0 || s->fd < 0) {
1719 return sig;
bellard1fddef42005-04-17 19:16:13 +00001720 }
1721
Andreas Färber5ca666c2013-06-24 19:20:57 +02001722 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001723 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001724 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00001725
Andreas Färber5ca666c2013-06-24 19:20:57 +02001726 if (sig != 0) {
1727 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1728 put_packet(s, buf);
1729 }
1730 /* put_packet() might have detected that the peer terminated the
1731 connection. */
1732 if (s->fd < 0) {
1733 return sig;
1734 }
1735
1736 sig = 0;
1737 s->state = RS_IDLE;
1738 s->running_state = 0;
1739 while (s->running_state == 0) {
1740 n = read(s->fd, buf, 256);
1741 if (n > 0) {
1742 int i;
1743
1744 for (i = 0; i < n; i++) {
1745 gdb_read_byte(s, buf[i]);
1746 }
Peter Wu5819e3e2016-06-05 16:35:48 +02001747 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02001748 /* XXX: Connection closed. Should probably wait for another
1749 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02001750 if (n == 0) {
1751 close(s->fd);
1752 }
1753 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001754 return sig;
bellard1fddef42005-04-17 19:16:13 +00001755 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001756 }
1757 sig = s->signal;
1758 s->signal = 0;
1759 return sig;
bellard1fddef42005-04-17 19:16:13 +00001760}
bellarde9009672005-04-26 20:42:36 +00001761
aurel32ca587a82008-12-18 22:44:13 +00001762/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001763void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001764{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001765 GDBState *s;
1766 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001767
Andreas Färber5ca666c2013-06-24 19:20:57 +02001768 s = gdbserver_state;
1769 if (gdbserver_fd < 0 || s->fd < 0) {
1770 return;
1771 }
aurel32ca587a82008-12-18 22:44:13 +00001772
Andreas Färber5ca666c2013-06-24 19:20:57 +02001773 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1774 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001775}
bellard1fddef42005-04-17 19:16:13 +00001776
aliguori880a7572008-11-18 20:30:24 +00001777static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001778{
1779 GDBState *s;
1780 struct sockaddr_in sockaddr;
1781 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001782 int fd;
bellard858693c2004-03-31 18:52:07 +00001783
1784 for(;;) {
1785 len = sizeof(sockaddr);
1786 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1787 if (fd < 0 && errno != EINTR) {
1788 perror("accept");
1789 return;
1790 } else if (fd >= 0) {
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001791#ifndef _WIN32
1792 fcntl(fd, F_SETFD, FD_CLOEXEC);
1793#endif
bellard858693c2004-03-31 18:52:07 +00001794 break;
1795 }
1796 }
1797
1798 /* set short latency */
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001799 socket_set_nodelay(fd);
ths3b46e622007-09-17 08:09:54 +00001800
Anthony Liguori7267c092011-08-20 22:09:37 -05001801 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001802 s->c_cpu = first_cpu;
1803 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001804 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02001805 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00001806
aliguori880a7572008-11-18 20:30:24 +00001807 gdbserver_state = s;
bellard858693c2004-03-31 18:52:07 +00001808}
1809
1810static int gdbserver_open(int port)
1811{
1812 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001813 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00001814
1815 fd = socket(PF_INET, SOCK_STREAM, 0);
1816 if (fd < 0) {
1817 perror("socket");
1818 return -1;
1819 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001820#ifndef _WIN32
1821 fcntl(fd, F_SETFD, FD_CLOEXEC);
1822#endif
bellard858693c2004-03-31 18:52:07 +00001823
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001824 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00001825
1826 sockaddr.sin_family = AF_INET;
1827 sockaddr.sin_port = htons(port);
1828 sockaddr.sin_addr.s_addr = 0;
1829 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1830 if (ret < 0) {
1831 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001832 close(fd);
bellard858693c2004-03-31 18:52:07 +00001833 return -1;
1834 }
Peter Wu96165b92016-05-04 11:32:17 +02001835 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00001836 if (ret < 0) {
1837 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001838 close(fd);
bellard858693c2004-03-31 18:52:07 +00001839 return -1;
1840 }
bellard858693c2004-03-31 18:52:07 +00001841 return fd;
1842}
1843
1844int gdbserver_start(int port)
1845{
1846 gdbserver_fd = gdbserver_open(port);
1847 if (gdbserver_fd < 0)
1848 return -1;
1849 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00001850 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00001851 return 0;
1852}
aurel322b1319c2008-12-18 22:44:04 +00001853
1854/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07001855void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00001856{
1857 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02001858
1859 if (gdbserver_fd < 0 || s->fd < 0) {
1860 return;
1861 }
aurel322b1319c2008-12-18 22:44:04 +00001862 close(s->fd);
1863 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001864 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02001865 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00001866}
pbrook4046d912007-01-28 01:53:16 +00001867#else
thsaa1f17c2007-07-11 22:48:58 +00001868static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001869{
pbrook56aebc82008-10-11 17:55:29 +00001870 /* We can handle an arbitrarily large amount of data.
1871 Pick the maximum packet size, which is as good as anything. */
1872 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001873}
1874
thsaa1f17c2007-07-11 22:48:58 +00001875static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001876{
pbrook4046d912007-01-28 01:53:16 +00001877 int i;
1878
1879 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001880 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001881 }
1882}
1883
1884static void gdb_chr_event(void *opaque, int event)
1885{
1886 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301887 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001888 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02001889 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00001890 break;
1891 default:
1892 break;
1893 }
1894}
1895
aliguori8a34a0f2009-03-05 23:01:55 +00001896static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1897{
1898 char buf[MAX_PACKET_LENGTH];
1899
1900 buf[0] = 'O';
1901 if (len > (MAX_PACKET_LENGTH/2) - 1)
1902 len = (MAX_PACKET_LENGTH/2) - 1;
1903 memtohex(buf + 1, (uint8_t *)msg, len);
1904 put_packet(s, buf);
1905}
1906
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001907static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00001908{
1909 const char *p = (const char *)buf;
1910 int max_sz;
1911
1912 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1913 for (;;) {
1914 if (len <= max_sz) {
1915 gdb_monitor_output(gdbserver_state, p, len);
1916 break;
1917 }
1918 gdb_monitor_output(gdbserver_state, p, max_sz);
1919 p += max_sz;
1920 len -= max_sz;
1921 }
1922 return len;
1923}
1924
aliguori59030a82009-04-05 18:43:41 +00001925#ifndef _WIN32
1926static void gdb_sigterm_handler(int signal)
1927{
Luiz Capitulino13548692011-07-29 15:36:43 -03001928 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001929 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001930 }
aliguori59030a82009-04-05 18:43:41 +00001931}
1932#endif
1933
Marc-André Lureau777357d2016-12-07 18:39:10 +03001934static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
1935 bool *be_opened, Error **errp)
1936{
1937 *be_opened = false;
1938}
1939
1940static void char_gdb_class_init(ObjectClass *oc, void *data)
1941{
1942 ChardevClass *cc = CHARDEV_CLASS(oc);
1943
1944 cc->internal = true;
1945 cc->open = gdb_monitor_open;
1946 cc->chr_write = gdb_monitor_write;
1947}
1948
1949#define TYPE_CHARDEV_GDB "chardev-gdb"
1950
1951static const TypeInfo char_gdb_type_info = {
1952 .name = TYPE_CHARDEV_GDB,
1953 .parent = TYPE_CHARDEV,
1954 .class_init = char_gdb_class_init,
1955};
1956
aliguori59030a82009-04-05 18:43:41 +00001957int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00001958{
1959 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00001960 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001961 Chardev *chr = NULL;
1962 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00001963
Ziyue Yang508b4ec2017-01-18 16:02:41 +08001964 if (!first_cpu) {
1965 error_report("gdbstub: meaningless to attach gdb to a "
1966 "machine without any CPU.");
1967 return -1;
1968 }
1969
aliguori59030a82009-04-05 18:43:41 +00001970 if (!device)
1971 return -1;
1972 if (strcmp(device, "none") != 0) {
1973 if (strstart(device, "tcp:", NULL)) {
1974 /* enforce required TCP attributes */
1975 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1976 "%s,nowait,nodelay,server", device);
1977 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00001978 }
aliguori59030a82009-04-05 18:43:41 +00001979#ifndef _WIN32
1980 else if (strcmp(device, "stdio") == 0) {
1981 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00001982
aliguori59030a82009-04-05 18:43:41 +00001983 memset(&act, 0, sizeof(act));
1984 act.sa_handler = gdb_sigterm_handler;
1985 sigaction(SIGINT, &act, NULL);
1986 }
1987#endif
Marc-André Lureaub4948be2016-10-22 12:52:46 +03001988 chr = qemu_chr_new_noreplay("gdb", device);
aliguori36556b22009-03-28 18:05:53 +00001989 if (!chr)
1990 return -1;
pbrookcfc34752007-02-22 01:48:01 +00001991 }
1992
aliguori36556b22009-03-28 18:05:53 +00001993 s = gdbserver_state;
1994 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001995 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00001996 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00001997
aliguori36556b22009-03-28 18:05:53 +00001998 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1999
2000 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03002001 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
2002 NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00002003 monitor_init(mon_chr, 0);
2004 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002005 if (qemu_chr_fe_get_driver(&s->chr)) {
Marc-André Lureau2f5d45a2016-12-14 15:23:36 +03002006 object_unparent(OBJECT(qemu_chr_fe_get_driver(&s->chr)));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002007 }
aliguori36556b22009-03-28 18:05:53 +00002008 mon_chr = s->mon_chr;
2009 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002010 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002011 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002012 s->c_cpu = first_cpu;
2013 s->g_cpu = first_cpu;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002014 if (chr) {
2015 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002016 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +03002017 gdb_chr_event, NULL, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002018 }
aliguori36556b22009-03-28 18:05:53 +00002019 s->state = chr ? RS_IDLE : RS_INACTIVE;
2020 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002021 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002022
pbrook4046d912007-01-28 01:53:16 +00002023 return 0;
2024}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002025
2026static void register_types(void)
2027{
2028 type_register_static(&char_gdb_type_info);
2029}
2030
2031type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002032#endif