blob: 1ba80f09f6fab2cd62b02486e9261c5d74a395ae [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 */
Markus Armbruster856dfd82019-05-23 16:35:06 +020019
Peter Maydelld38ea872016-01-29 17:50:05 +000020#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010021#include "qapi/error.h"
Ziyue Yang508b4ec2017-01-18 16:02:41 +080022#include "qemu/error-report.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020023#include "qemu/ctype.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020024#include "qemu/cutils.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020025#include "qemu/module.h"
Doug Gale5c9522b2017-12-02 20:30:37 -050026#include "trace-root.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020027#ifdef CONFIG_USER_ONLY
bellard1fddef42005-04-17 19:16:13 +000028#include "qemu.h"
29#else
Paolo Bonzini83c90892012-12-17 18:19:49 +010030#include "monitor/monitor.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040031#include "chardev/char.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040032#include "chardev/char-fe.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010034#include "exec/gdbstub.h"
Luc Michel8f468632019-01-07 15:23:45 +000035#include "hw/cpu/cluster.h"
bellard1fddef42005-04-17 19:16:13 +000036#endif
bellard67b915a2004-03-31 23:37:16 +000037
pbrook56aebc82008-10-11 17:55:29 +000038#define MAX_PACKET_LENGTH 4096
39
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010040#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010041#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010042#include "sysemu/kvm.h"
Alex Bennéef1672e62019-05-13 14:43:57 +010043#include "hw/semihosting/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010044#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000045
Jan Kiszkaa3919382015-02-07 09:38:44 +010046#ifdef CONFIG_USER_ONLY
47#define GDB_ATTACHED "0"
48#else
49#define GDB_ATTACHED "1"
50#endif
51
Andreas Färberf3659ee2013-06-27 19:09:09 +020052static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
53 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020054{
Andreas Färberf3659ee2013-06-27 19:09:09 +020055 CPUClass *cc = CPU_GET_CLASS(cpu);
56
57 if (cc->memory_rw_debug) {
58 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
59 }
60 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020061}
aurel32ca587a82008-12-18 22:44:13 +000062
Alex Bennéed2a6c852017-07-12 11:52:14 +010063/* Return the GDB index for a given vCPU state.
64 *
65 * For user mode this is simply the thread id. In system mode GDB
66 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
67 */
68static inline int cpu_gdb_index(CPUState *cpu)
69{
70#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010071 TaskState *ts = (TaskState *) cpu->opaque;
72 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010073#else
74 return cpu->cpu_index + 1;
75#endif
76}
77
aurel32ca587a82008-12-18 22:44:13 +000078enum {
79 GDB_SIGNAL_0 = 0,
80 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010081 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000082 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010083 GDB_SIGNAL_ABRT = 6,
84 GDB_SIGNAL_ALRM = 14,
85 GDB_SIGNAL_IO = 23,
86 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000087 GDB_SIGNAL_UNKNOWN = 143
88};
89
90#ifdef CONFIG_USER_ONLY
91
92/* Map target signal numbers to GDB protocol signal numbers and vice
93 * versa. For user emulation's currently supported systems, we can
94 * assume most signals are defined.
95 */
96
97static int gdb_signal_table[] = {
98 0,
99 TARGET_SIGHUP,
100 TARGET_SIGINT,
101 TARGET_SIGQUIT,
102 TARGET_SIGILL,
103 TARGET_SIGTRAP,
104 TARGET_SIGABRT,
105 -1, /* SIGEMT */
106 TARGET_SIGFPE,
107 TARGET_SIGKILL,
108 TARGET_SIGBUS,
109 TARGET_SIGSEGV,
110 TARGET_SIGSYS,
111 TARGET_SIGPIPE,
112 TARGET_SIGALRM,
113 TARGET_SIGTERM,
114 TARGET_SIGURG,
115 TARGET_SIGSTOP,
116 TARGET_SIGTSTP,
117 TARGET_SIGCONT,
118 TARGET_SIGCHLD,
119 TARGET_SIGTTIN,
120 TARGET_SIGTTOU,
121 TARGET_SIGIO,
122 TARGET_SIGXCPU,
123 TARGET_SIGXFSZ,
124 TARGET_SIGVTALRM,
125 TARGET_SIGPROF,
126 TARGET_SIGWINCH,
127 -1, /* SIGLOST */
128 TARGET_SIGUSR1,
129 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000130#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000131 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000132#else
133 -1,
134#endif
aurel32ca587a82008-12-18 22:44:13 +0000135 -1, /* SIGPOLL */
136 -1,
137 -1,
138 -1,
139 -1,
140 -1,
141 -1,
142 -1,
143 -1,
144 -1,
145 -1,
146 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000147#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000148 __SIGRTMIN + 1,
149 __SIGRTMIN + 2,
150 __SIGRTMIN + 3,
151 __SIGRTMIN + 4,
152 __SIGRTMIN + 5,
153 __SIGRTMIN + 6,
154 __SIGRTMIN + 7,
155 __SIGRTMIN + 8,
156 __SIGRTMIN + 9,
157 __SIGRTMIN + 10,
158 __SIGRTMIN + 11,
159 __SIGRTMIN + 12,
160 __SIGRTMIN + 13,
161 __SIGRTMIN + 14,
162 __SIGRTMIN + 15,
163 __SIGRTMIN + 16,
164 __SIGRTMIN + 17,
165 __SIGRTMIN + 18,
166 __SIGRTMIN + 19,
167 __SIGRTMIN + 20,
168 __SIGRTMIN + 21,
169 __SIGRTMIN + 22,
170 __SIGRTMIN + 23,
171 __SIGRTMIN + 24,
172 __SIGRTMIN + 25,
173 __SIGRTMIN + 26,
174 __SIGRTMIN + 27,
175 __SIGRTMIN + 28,
176 __SIGRTMIN + 29,
177 __SIGRTMIN + 30,
178 __SIGRTMIN + 31,
179 -1, /* SIGCANCEL */
180 __SIGRTMIN,
181 __SIGRTMIN + 32,
182 __SIGRTMIN + 33,
183 __SIGRTMIN + 34,
184 __SIGRTMIN + 35,
185 __SIGRTMIN + 36,
186 __SIGRTMIN + 37,
187 __SIGRTMIN + 38,
188 __SIGRTMIN + 39,
189 __SIGRTMIN + 40,
190 __SIGRTMIN + 41,
191 __SIGRTMIN + 42,
192 __SIGRTMIN + 43,
193 __SIGRTMIN + 44,
194 __SIGRTMIN + 45,
195 __SIGRTMIN + 46,
196 __SIGRTMIN + 47,
197 __SIGRTMIN + 48,
198 __SIGRTMIN + 49,
199 __SIGRTMIN + 50,
200 __SIGRTMIN + 51,
201 __SIGRTMIN + 52,
202 __SIGRTMIN + 53,
203 __SIGRTMIN + 54,
204 __SIGRTMIN + 55,
205 __SIGRTMIN + 56,
206 __SIGRTMIN + 57,
207 __SIGRTMIN + 58,
208 __SIGRTMIN + 59,
209 __SIGRTMIN + 60,
210 __SIGRTMIN + 61,
211 __SIGRTMIN + 62,
212 __SIGRTMIN + 63,
213 __SIGRTMIN + 64,
214 __SIGRTMIN + 65,
215 __SIGRTMIN + 66,
216 __SIGRTMIN + 67,
217 __SIGRTMIN + 68,
218 __SIGRTMIN + 69,
219 __SIGRTMIN + 70,
220 __SIGRTMIN + 71,
221 __SIGRTMIN + 72,
222 __SIGRTMIN + 73,
223 __SIGRTMIN + 74,
224 __SIGRTMIN + 75,
225 __SIGRTMIN + 76,
226 __SIGRTMIN + 77,
227 __SIGRTMIN + 78,
228 __SIGRTMIN + 79,
229 __SIGRTMIN + 80,
230 __SIGRTMIN + 81,
231 __SIGRTMIN + 82,
232 __SIGRTMIN + 83,
233 __SIGRTMIN + 84,
234 __SIGRTMIN + 85,
235 __SIGRTMIN + 86,
236 __SIGRTMIN + 87,
237 __SIGRTMIN + 88,
238 __SIGRTMIN + 89,
239 __SIGRTMIN + 90,
240 __SIGRTMIN + 91,
241 __SIGRTMIN + 92,
242 __SIGRTMIN + 93,
243 __SIGRTMIN + 94,
244 __SIGRTMIN + 95,
245 -1, /* SIGINFO */
246 -1, /* UNKNOWN */
247 -1, /* DEFAULT */
248 -1,
249 -1,
250 -1,
251 -1,
252 -1,
253 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000254#endif
aurel32ca587a82008-12-18 22:44:13 +0000255};
bellard8f447cc2006-06-14 15:21:14 +0000256#else
aurel32ca587a82008-12-18 22:44:13 +0000257/* In system mode we only need SIGINT and SIGTRAP; other signals
258 are not yet supported. */
259
260enum {
261 TARGET_SIGINT = 2,
262 TARGET_SIGTRAP = 5
263};
264
265static int gdb_signal_table[] = {
266 -1,
267 -1,
268 TARGET_SIGINT,
269 -1,
270 -1,
271 TARGET_SIGTRAP
272};
bellard8f447cc2006-06-14 15:21:14 +0000273#endif
bellardb4608c02003-06-27 17:34:32 +0000274
aurel32ca587a82008-12-18 22:44:13 +0000275#ifdef CONFIG_USER_ONLY
276static int target_signal_to_gdb (int sig)
277{
278 int i;
279 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
280 if (gdb_signal_table[i] == sig)
281 return i;
282 return GDB_SIGNAL_UNKNOWN;
283}
284#endif
285
286static int gdb_signal_to_target (int sig)
287{
288 if (sig < ARRAY_SIZE (gdb_signal_table))
289 return gdb_signal_table[sig];
290 else
291 return -1;
292}
293
pbrook56aebc82008-10-11 17:55:29 +0000294typedef struct GDBRegisterState {
295 int base_reg;
296 int num_regs;
297 gdb_reg_cb get_reg;
298 gdb_reg_cb set_reg;
299 const char *xml;
300 struct GDBRegisterState *next;
301} GDBRegisterState;
302
Luc Michel8f468632019-01-07 15:23:45 +0000303typedef struct GDBProcess {
304 uint32_t pid;
305 bool attached;
Luc Michelc145eea2019-01-07 15:23:46 +0000306
307 char target_xml[1024];
Luc Michel8f468632019-01-07 15:23:45 +0000308} GDBProcess;
309
bellard858693c2004-03-31 18:52:07 +0000310enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000311 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000312 RS_IDLE,
313 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400314 RS_GETLINE_ESC,
315 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000316 RS_CHKSUM1,
317 RS_CHKSUM2,
318};
bellard858693c2004-03-31 18:52:07 +0000319typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200320 CPUState *c_cpu; /* current CPU for step/continue ops */
321 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200322 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000323 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000324 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000325 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400326 int line_sum; /* running checksum */
327 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000328 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000329 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000330 int signal;
bellard41625032005-04-24 10:07:11 +0000331#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000332 int fd;
bellard41625032005-04-24 10:07:11 +0000333 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000334#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300335 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300336 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000337#endif
Luc Michel8f468632019-01-07 15:23:45 +0000338 bool multiprocess;
339 GDBProcess *processes;
340 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000341 char syscall_buf[256];
342 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000343} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000344
edgar_igl60897d32008-05-09 08:25:14 +0000345/* By default use no IRQs and no timers while single stepping so as to
346 * make single stepping like an ICE HW step.
347 */
348static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
349
aliguori880a7572008-11-18 20:30:24 +0000350static GDBState *gdbserver_state;
351
Andreas Färber5b50e792013-06-29 04:18:45 +0200352bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000353
bellard1fddef42005-04-17 19:16:13 +0000354#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000355/* XXX: This is not thread safe. Do we care? */
356static int gdbserver_fd = -1;
357
bellard858693c2004-03-31 18:52:07 +0000358static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000359{
360 uint8_t ch;
361 int ret;
362
363 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000364 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000365 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000366 if (errno == ECONNRESET)
367 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200368 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000369 return -1;
370 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000371 close(s->fd);
372 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000373 return -1;
374 } else {
375 break;
376 }
377 }
378 return ch;
379}
pbrook4046d912007-01-28 01:53:16 +0000380#endif
bellardb4608c02003-06-27 17:34:32 +0000381
blueswir1654efcf2009-04-18 07:29:59 +0000382static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000383 GDB_SYS_UNKNOWN,
384 GDB_SYS_ENABLED,
385 GDB_SYS_DISABLED,
386} gdb_syscall_mode;
387
Liviu Ionescua38bb072014-12-11 12:07:48 +0000388/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000389int use_gdb_syscalls(void)
390{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100391 SemihostingTarget target = semihosting_get_target();
392 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000393 /* -semihosting-config target=native */
394 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100395 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000396 /* -semihosting-config target=gdb */
397 return true;
398 }
399
400 /* -semihosting-config target=auto */
401 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000402 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000403 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
404 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000405 }
406 return gdb_syscall_mode == GDB_SYS_ENABLED;
407}
408
edgar_iglba70a622008-03-14 06:10:42 +0000409/* Resume execution. */
410static inline void gdb_continue(GDBState *s)
411{
Doug Gale5c9522b2017-12-02 20:30:37 -0500412
edgar_iglba70a622008-03-14 06:10:42 +0000413#ifdef CONFIG_USER_ONLY
414 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500415 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000416#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200417 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500418 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200419 vm_start();
420 }
edgar_iglba70a622008-03-14 06:10:42 +0000421#endif
422}
423
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100424/*
425 * Resume execution, per CPU actions. For user-mode emulation it's
426 * equivalent to gdb_continue.
427 */
428static int gdb_continue_partial(GDBState *s, char *newstates)
429{
430 CPUState *cpu;
431 int res = 0;
432#ifdef CONFIG_USER_ONLY
433 /*
434 * This is not exactly accurate, but it's an improvement compared to the
435 * previous situation, where only one CPU would be single-stepped.
436 */
437 CPU_FOREACH(cpu) {
438 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500439 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100440 cpu_single_step(cpu, sstep_flags);
441 }
442 }
443 s->running_state = 1;
444#else
445 int flag = 0;
446
447 if (!runstate_needs_reset()) {
448 if (vm_prepare_start()) {
449 return 0;
450 }
451
452 CPU_FOREACH(cpu) {
453 switch (newstates[cpu->cpu_index]) {
454 case 0:
455 case 1:
456 break; /* nothing to do here */
457 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500458 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100459 cpu_single_step(cpu, sstep_flags);
460 cpu_resume(cpu);
461 flag = 1;
462 break;
463 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500464 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100465 cpu_resume(cpu);
466 flag = 1;
467 break;
468 default:
469 res = -1;
470 break;
471 }
472 }
473 }
474 if (flag) {
475 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
476 }
477#endif
478 return res;
479}
480
bellard858693c2004-03-31 18:52:07 +0000481static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000482{
pbrook4046d912007-01-28 01:53:16 +0000483#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000484 int ret;
485
486 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000487 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000488 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200489 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000490 return;
491 } else {
492 buf += ret;
493 len -= ret;
494 }
495 }
pbrook4046d912007-01-28 01:53:16 +0000496#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100497 /* XXX this blocks entire thread. Rewrite to use
498 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300499 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000500#endif
bellardb4608c02003-06-27 17:34:32 +0000501}
502
503static inline int fromhex(int v)
504{
505 if (v >= '0' && v <= '9')
506 return v - '0';
507 else if (v >= 'A' && v <= 'F')
508 return v - 'A' + 10;
509 else if (v >= 'a' && v <= 'f')
510 return v - 'a' + 10;
511 else
512 return 0;
513}
514
515static inline int tohex(int v)
516{
517 if (v < 10)
518 return v + '0';
519 else
520 return v - 10 + 'a';
521}
522
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300523/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000524static void memtohex(char *buf, const uint8_t *mem, int len)
525{
526 int i, c;
527 char *q;
528 q = buf;
529 for(i = 0; i < len; i++) {
530 c = mem[i];
531 *q++ = tohex(c >> 4);
532 *q++ = tohex(c & 0xf);
533 }
534 *q = '\0';
535}
536
537static void hextomem(uint8_t *mem, const char *buf, int len)
538{
539 int i;
540
541 for(i = 0; i < len; i++) {
542 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
543 buf += 2;
544 }
545}
546
Doug Gale5c9522b2017-12-02 20:30:37 -0500547static void hexdump(const char *buf, int len,
548 void (*trace_fn)(size_t ofs, char const *text))
549{
550 char line_buffer[3 * 16 + 4 + 16 + 1];
551
552 size_t i;
553 for (i = 0; i < len || (i & 0xF); ++i) {
554 size_t byte_ofs = i & 15;
555
556 if (byte_ofs == 0) {
557 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
558 line_buffer[3 * 16 + 4 + 16] = 0;
559 }
560
561 size_t col_group = (i >> 2) & 3;
562 size_t hex_col = byte_ofs * 3 + col_group;
563 size_t txt_col = 3 * 16 + 4 + byte_ofs;
564
565 if (i < len) {
566 char value = buf[i];
567
568 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
569 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
570 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
571 ? value
572 : '.';
573 }
574
575 if (byte_ofs == 0xF)
576 trace_fn(i & -16, line_buffer);
577 }
578}
579
bellardb4608c02003-06-27 17:34:32 +0000580/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500581static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000582{
pbrook56aebc82008-10-11 17:55:29 +0000583 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000584 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000585
Doug Gale5c9522b2017-12-02 20:30:37 -0500586 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
587 hexdump(buf, len, trace_gdbstub_io_binaryreply);
588 }
589
bellardb4608c02003-06-27 17:34:32 +0000590 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000591 p = s->last_packet;
592 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000593 memcpy(p, buf, len);
594 p += len;
bellardb4608c02003-06-27 17:34:32 +0000595 csum = 0;
596 for(i = 0; i < len; i++) {
597 csum += buf[i];
598 }
pbrook4046d912007-01-28 01:53:16 +0000599 *(p++) = '#';
600 *(p++) = tohex((csum >> 4) & 0xf);
601 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000602
pbrook4046d912007-01-28 01:53:16 +0000603 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000604 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000605
pbrook4046d912007-01-28 01:53:16 +0000606#ifdef CONFIG_USER_ONLY
607 i = get_char(s);
608 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000609 return -1;
pbrook4046d912007-01-28 01:53:16 +0000610 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000611 break;
pbrook4046d912007-01-28 01:53:16 +0000612#else
613 break;
614#endif
bellardb4608c02003-06-27 17:34:32 +0000615 }
616 return 0;
617}
618
pbrook56aebc82008-10-11 17:55:29 +0000619/* return -1 if error, 0 if OK */
620static int put_packet(GDBState *s, const char *buf)
621{
Doug Gale5c9522b2017-12-02 20:30:37 -0500622 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000623
Doug Gale5c9522b2017-12-02 20:30:37 -0500624 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000625}
626
pbrook56aebc82008-10-11 17:55:29 +0000627/* Encode data using the encoding for 'x' packets. */
628static int memtox(char *buf, const char *mem, int len)
629{
630 char *p = buf;
631 char c;
632
633 while (len--) {
634 c = *(mem++);
635 switch (c) {
636 case '#': case '$': case '*': case '}':
637 *(p++) = '}';
638 *(p++) = c ^ 0x20;
639 break;
640 default:
641 *(p++) = c;
642 break;
643 }
644 }
645 return p - buf;
646}
647
Luc Michel1a227332019-01-07 15:23:45 +0000648static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
649{
Luc Michel1a227332019-01-07 15:23:45 +0000650 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000651 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
652 /* Return the default process' PID */
653 return s->processes[s->process_num - 1].pid;
654 }
655 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000656}
657
Luc Michel7d8c87d2019-01-07 15:23:45 +0000658static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
659{
660 int i;
661
662 if (!pid) {
663 /* 0 means any process, we take the first one */
664 return &s->processes[0];
665 }
666
667 for (i = 0; i < s->process_num; i++) {
668 if (s->processes[i].pid == pid) {
669 return &s->processes[i];
670 }
671 }
672
673 return NULL;
674}
675
676static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
677{
678 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
679}
680
681static CPUState *find_cpu(uint32_t thread_id)
682{
683 CPUState *cpu;
684
685 CPU_FOREACH(cpu) {
686 if (cpu_gdb_index(cpu) == thread_id) {
687 return cpu;
688 }
689 }
690
691 return NULL;
692}
693
Luc Michele40e5202019-01-07 15:23:46 +0000694static CPUState *get_first_cpu_in_process(const GDBState *s,
695 GDBProcess *process)
696{
697 CPUState *cpu;
698
699 CPU_FOREACH(cpu) {
700 if (gdb_get_cpu_pid(s, cpu) == process->pid) {
701 return cpu;
702 }
703 }
704
705 return NULL;
706}
707
708static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu)
709{
710 uint32_t pid = gdb_get_cpu_pid(s, cpu);
711 cpu = CPU_NEXT(cpu);
712
713 while (cpu) {
714 if (gdb_get_cpu_pid(s, cpu) == pid) {
715 break;
716 }
717
718 cpu = CPU_NEXT(cpu);
719 }
720
721 return cpu;
722}
723
Luc Michele40e5202019-01-07 15:23:46 +0000724/* Return the cpu following @cpu, while ignoring unattached processes. */
725static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
726{
727 cpu = CPU_NEXT(cpu);
728
729 while (cpu) {
730 if (gdb_get_cpu_process(s, cpu)->attached) {
731 break;
732 }
733
734 cpu = CPU_NEXT(cpu);
735 }
736
737 return cpu;
738}
739
740/* Return the first attached cpu */
741static CPUState *gdb_first_attached_cpu(const GDBState *s)
742{
743 CPUState *cpu = first_cpu;
744 GDBProcess *process = gdb_get_cpu_process(s, cpu);
745
746 if (!process->attached) {
747 return gdb_next_attached_cpu(s, cpu);
748 }
749
750 return cpu;
751}
752
Luc Michelab65eed2019-01-29 11:46:03 +0000753static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
754{
755 GDBProcess *process;
756 CPUState *cpu;
757
758 if (!pid && !tid) {
759 /* 0 means any process/thread, we take the first attached one */
760 return gdb_first_attached_cpu(s);
761 } else if (pid && !tid) {
762 /* any thread in a specific process */
763 process = gdb_get_process(s, pid);
764
765 if (process == NULL) {
766 return NULL;
767 }
768
769 if (!process->attached) {
770 return NULL;
771 }
772
773 return get_first_cpu_in_process(s, process);
774 } else {
775 /* a specific thread */
776 cpu = find_cpu(tid);
777
778 if (cpu == NULL) {
779 return NULL;
780 }
781
782 process = gdb_get_cpu_process(s, cpu);
783
784 if (pid && process->pid != pid) {
785 return NULL;
786 }
787
788 if (!process->attached) {
789 return NULL;
790 }
791
792 return cpu;
793 }
794}
795
Luc Michelc145eea2019-01-07 15:23:46 +0000796static const char *get_feature_xml(const GDBState *s, const char *p,
797 const char **newp, GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000798{
pbrook56aebc82008-10-11 17:55:29 +0000799 size_t len;
800 int i;
801 const char *name;
Luc Michelc145eea2019-01-07 15:23:46 +0000802 CPUState *cpu = get_first_cpu_in_process(s, process);
803 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000804
805 len = 0;
806 while (p[len] && p[len] != ':')
807 len++;
808 *newp = p + len;
809
810 name = NULL;
811 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000812 char *buf = process->target_xml;
813 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000814
Luc Michelc145eea2019-01-07 15:23:46 +0000815 /* Generate the XML description for this CPU. */
816 if (!buf[0]) {
817 GDBRegisterState *r;
818
819 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100820 "<?xml version=\"1.0\"?>"
821 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
822 "<target>");
823 if (cc->gdb_arch_name) {
824 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000825 pstrcat(buf, buf_sz, "<architecture>");
826 pstrcat(buf, buf_sz, arch);
827 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100828 g_free(arch);
829 }
Luc Michelc145eea2019-01-07 15:23:46 +0000830 pstrcat(buf, buf_sz, "<xi:include href=\"");
831 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
832 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200833 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000834 pstrcat(buf, buf_sz, "<xi:include href=\"");
835 pstrcat(buf, buf_sz, r->xml);
836 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000837 }
Luc Michelc145eea2019-01-07 15:23:46 +0000838 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000839 }
Luc Michelc145eea2019-01-07 15:23:46 +0000840 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000841 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100842 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100843 char *xmlname = g_strndup(p, len);
844 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
845
846 g_free(xmlname);
847 if (xml) {
848 return xml;
849 }
850 }
pbrook56aebc82008-10-11 17:55:29 +0000851 for (i = 0; ; i++) {
852 name = xml_builtin[i][0];
853 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
854 break;
855 }
856 return name ? xml_builtin[i][1] : NULL;
857}
pbrook56aebc82008-10-11 17:55:29 +0000858
Andreas Färber385b9f02013-06-27 18:25:36 +0200859static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000860{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200861 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200862 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000863 GDBRegisterState *r;
864
Andreas Färbera0e372f2013-06-28 23:18:47 +0200865 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200866 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200867 }
pbrook56aebc82008-10-11 17:55:29 +0000868
Andreas Färbereac8b352013-06-28 21:11:37 +0200869 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000870 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
871 return r->get_reg(env, mem_buf, reg - r->base_reg);
872 }
873 }
874 return 0;
875}
876
Andreas Färber385b9f02013-06-27 18:25:36 +0200877static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000878{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200879 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200880 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000881 GDBRegisterState *r;
882
Andreas Färbera0e372f2013-06-28 23:18:47 +0200883 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200884 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200885 }
pbrook56aebc82008-10-11 17:55:29 +0000886
Andreas Färbereac8b352013-06-28 21:11:37 +0200887 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000888 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
889 return r->set_reg(env, mem_buf, reg - r->base_reg);
890 }
891 }
892 return 0;
893}
894
895/* Register a supplemental set of CPU registers. If g_pos is nonzero it
896 specifies the first register number and these registers are included in
897 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
898 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
899 */
900
Andreas Färber22169d42013-06-28 21:27:39 +0200901void gdb_register_coprocessor(CPUState *cpu,
902 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
903 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000904{
905 GDBRegisterState *s;
906 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000907
Andreas Färbereac8b352013-06-28 21:11:37 +0200908 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000909 while (*p) {
910 /* Check for duplicates. */
911 if (strcmp((*p)->xml, xml) == 0)
912 return;
913 p = &(*p)->next;
914 }
Stefan Weil9643c252011-10-18 22:25:38 +0200915
916 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200917 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200918 s->num_regs = num_regs;
919 s->get_reg = get_reg;
920 s->set_reg = set_reg;
921 s->xml = xml;
922
pbrook56aebc82008-10-11 17:55:29 +0000923 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200924 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000925 *p = s;
926 if (g_pos) {
927 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800928 error_report("Error: Bad gdb register numbering for '%s', "
929 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200930 } else {
931 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000932 }
933 }
934}
935
aliguoria1d1bb32008-11-18 20:07:32 +0000936#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100937/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
938static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
939{
940 static const int xlat[] = {
941 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
942 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
943 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
944 };
945
946 CPUClass *cc = CPU_GET_CLASS(cpu);
947 int cputype = xlat[gdbtype];
948
949 if (cc->gdb_stop_before_watchpoint) {
950 cputype |= BP_STOP_BEFORE_ACCESS;
951 }
952 return cputype;
953}
aliguoria1d1bb32008-11-18 20:07:32 +0000954#endif
955
aliguori880a7572008-11-18 20:30:24 +0000956static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000957{
Andreas Färber182735e2013-05-29 22:29:20 +0200958 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000959 int err = 0;
960
Andreas Färber62278812013-06-27 17:12:06 +0200961 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200962 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200963 }
aliguorie22a25c2009-03-12 20:12:48 +0000964
aliguoria1d1bb32008-11-18 20:07:32 +0000965 switch (type) {
966 case GDB_BREAKPOINT_SW:
967 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200968 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200969 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
970 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000971 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200972 }
aliguori880a7572008-11-18 20:30:24 +0000973 }
974 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000975#ifndef CONFIG_USER_ONLY
976 case GDB_WATCHPOINT_WRITE:
977 case GDB_WATCHPOINT_READ:
978 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200979 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100980 err = cpu_watchpoint_insert(cpu, addr, len,
981 xlat_gdb_type(cpu, type), NULL);
982 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000983 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100984 }
aliguori880a7572008-11-18 20:30:24 +0000985 }
986 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000987#endif
988 default:
989 return -ENOSYS;
990 }
991}
992
aliguori880a7572008-11-18 20:30:24 +0000993static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000994{
Andreas Färber182735e2013-05-29 22:29:20 +0200995 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000996 int err = 0;
997
Andreas Färber62278812013-06-27 17:12:06 +0200998 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200999 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001000 }
aliguorie22a25c2009-03-12 20:12:48 +00001001
aliguoria1d1bb32008-11-18 20:07:32 +00001002 switch (type) {
1003 case GDB_BREAKPOINT_SW:
1004 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001005 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001006 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1007 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001008 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001009 }
aliguori880a7572008-11-18 20:30:24 +00001010 }
1011 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001012#ifndef CONFIG_USER_ONLY
1013 case GDB_WATCHPOINT_WRITE:
1014 case GDB_WATCHPOINT_READ:
1015 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001016 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001017 err = cpu_watchpoint_remove(cpu, addr, len,
1018 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001019 if (err)
1020 break;
1021 }
1022 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001023#endif
1024 default:
1025 return -ENOSYS;
1026 }
1027}
1028
Luc Michel546f3c62019-01-07 15:23:46 +00001029static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1030{
1031 cpu_breakpoint_remove_all(cpu, BP_GDB);
1032#ifndef CONFIG_USER_ONLY
1033 cpu_watchpoint_remove_all(cpu, BP_GDB);
1034#endif
1035}
1036
1037static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p)
1038{
1039 CPUState *cpu = get_first_cpu_in_process(s, p);
1040
1041 while (cpu) {
1042 gdb_cpu_breakpoint_remove_all(cpu);
1043 cpu = gdb_next_cpu_in_process(s, cpu);
1044 }
1045}
1046
aliguori880a7572008-11-18 20:30:24 +00001047static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001048{
Andreas Färber182735e2013-05-29 22:29:20 +02001049 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001050
aliguorie22a25c2009-03-12 20:12:48 +00001051 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001052 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001053 return;
1054 }
1055
Andreas Färberbdc44642013-06-24 23:50:24 +02001056 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001057 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001058 }
aliguoria1d1bb32008-11-18 20:07:32 +00001059}
1060
aurel32fab9d282009-04-08 21:29:37 +00001061static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1062{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001063 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001064
1065 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001066 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001067}
1068
Luc Michel1a227332019-01-07 15:23:45 +00001069static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1070 char *buf, size_t buf_size)
1071{
1072 if (s->multiprocess) {
1073 snprintf(buf, buf_size, "p%02x.%02x",
1074 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1075 } else {
1076 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1077 }
1078
1079 return buf;
1080}
1081
Luc Michel7d8c87d2019-01-07 15:23:45 +00001082typedef enum GDBThreadIdKind {
1083 GDB_ONE_THREAD = 0,
1084 GDB_ALL_THREADS, /* One process, all threads */
1085 GDB_ALL_PROCESSES,
1086 GDB_READ_THREAD_ERR
1087} GDBThreadIdKind;
1088
1089static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1090 uint32_t *pid, uint32_t *tid)
1091{
1092 unsigned long p, t;
1093 int ret;
1094
1095 if (*buf == 'p') {
1096 buf++;
1097 ret = qemu_strtoul(buf, &buf, 16, &p);
1098
1099 if (ret) {
1100 return GDB_READ_THREAD_ERR;
1101 }
1102
1103 /* Skip '.' */
1104 buf++;
1105 } else {
1106 p = 1;
1107 }
1108
1109 ret = qemu_strtoul(buf, &buf, 16, &t);
1110
1111 if (ret) {
1112 return GDB_READ_THREAD_ERR;
1113 }
1114
1115 *end_buf = buf;
1116
1117 if (p == -1) {
1118 return GDB_ALL_PROCESSES;
1119 }
1120
1121 if (pid) {
1122 *pid = p;
1123 }
1124
1125 if (t == -1) {
1126 return GDB_ALL_THREADS;
1127 }
1128
1129 if (tid) {
1130 *tid = t;
1131 }
1132
1133 return GDB_ONE_THREAD;
1134}
1135
Jan Kiszka4dabe742015-02-07 09:38:43 +01001136static int is_query_packet(const char *p, const char *query, char separator)
1137{
1138 unsigned int query_len = strlen(query);
1139
1140 return strncmp(p, query, query_len) == 0 &&
1141 (p[query_len] == '\0' || p[query_len] == separator);
1142}
1143
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001144/**
1145 * gdb_handle_vcont - Parses and handles a vCont packet.
1146 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1147 * a format error, 0 on success.
1148 */
1149static int gdb_handle_vcont(GDBState *s, const char *p)
1150{
Luc Michele40e5202019-01-07 15:23:46 +00001151 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001152 char cur_action;
1153 char *newstates;
1154 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001155 uint32_t pid, tid;
1156 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001157 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +00001158 GDBThreadIdKind kind;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001159#ifdef CONFIG_USER_ONLY
1160 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1161
1162 CPU_FOREACH(cpu) {
1163 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1164 }
1165#endif
1166 /* uninitialised CPUs stay 0 */
1167 newstates = g_new0(char, max_cpus);
1168
1169 /* mark valid CPUs with 1 */
1170 CPU_FOREACH(cpu) {
1171 newstates[cpu->cpu_index] = 1;
1172 }
1173
1174 /*
1175 * res keeps track of what error we are returning, with -ENOTSUP meaning
1176 * that the command is unknown or unsupported, thus returning an empty
1177 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1178 * or incorrect parameters passed.
1179 */
1180 res = 0;
1181 while (*p) {
1182 if (*p++ != ';') {
1183 res = -ENOTSUP;
1184 goto out;
1185 }
1186
1187 cur_action = *p++;
1188 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001189 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001190 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1191 if (res) {
1192 goto out;
1193 }
1194 signal = gdb_signal_to_target(tmp);
1195 } else if (cur_action != 'c' && cur_action != 's') {
1196 /* unknown/invalid/unsupported command */
1197 res = -ENOTSUP;
1198 goto out;
1199 }
Luc Michele40e5202019-01-07 15:23:46 +00001200
Luc Michelc99ef792019-03-26 12:53:26 +00001201 if (*p == '\0' || *p == ';') {
1202 /*
1203 * No thread specifier, action is on "all threads". The
1204 * specification is unclear regarding the process to act on. We
1205 * choose all processes.
1206 */
1207 kind = GDB_ALL_PROCESSES;
1208 } else if (*p++ == ':') {
1209 kind = read_thread_id(p, &p, &pid, &tid);
1210 } else {
Luc Michele40e5202019-01-07 15:23:46 +00001211 res = -ENOTSUP;
1212 goto out;
1213 }
1214
Luc Michelc99ef792019-03-26 12:53:26 +00001215 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +00001216 case GDB_READ_THREAD_ERR:
1217 res = -EINVAL;
1218 goto out;
1219
1220 case GDB_ALL_PROCESSES:
1221 cpu = gdb_first_attached_cpu(s);
1222 while (cpu) {
1223 if (newstates[cpu->cpu_index] == 1) {
1224 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001225 }
Luc Michele40e5202019-01-07 15:23:46 +00001226
1227 cpu = gdb_next_attached_cpu(s, cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001228 }
Luc Michele40e5202019-01-07 15:23:46 +00001229 break;
1230
1231 case GDB_ALL_THREADS:
1232 process = gdb_get_process(s, pid);
1233
1234 if (!process->attached) {
1235 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001236 goto out;
1237 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001238
Luc Michele40e5202019-01-07 15:23:46 +00001239 cpu = get_first_cpu_in_process(s, process);
1240 while (cpu) {
1241 if (newstates[cpu->cpu_index] == 1) {
1242 newstates[cpu->cpu_index] = cur_action;
1243 }
1244
1245 cpu = gdb_next_cpu_in_process(s, cpu);
1246 }
1247 break;
1248
1249 case GDB_ONE_THREAD:
1250 cpu = gdb_get_cpu(s, pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001251
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001252 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001253 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001254 res = -EINVAL;
1255 goto out;
1256 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001257
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001258 /* only use if no previous match occourred */
1259 if (newstates[cpu->cpu_index] == 1) {
1260 newstates[cpu->cpu_index] = cur_action;
1261 }
Luc Michele40e5202019-01-07 15:23:46 +00001262 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001263 }
1264 }
1265 s->signal = signal;
1266 gdb_continue_partial(s, newstates);
1267
1268out:
1269 g_free(newstates);
1270
1271 return res;
1272}
1273
aliguori880a7572008-11-18 20:30:24 +00001274static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +00001275{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001276 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001277 GDBProcess *process;
Andreas Färber5b24c642013-07-07 15:08:22 +02001278 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +00001279 const char *p;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001280 uint32_t pid, tid;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001281 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +00001282 uint8_t mem_buf[MAX_PACKET_LENGTH];
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -03001283 char buf[sizeof(mem_buf) + 1 /* trailing NUL */];
Luc Michel1a227332019-01-07 15:23:45 +00001284 char thread_id[16];
pbrook56aebc82008-10-11 17:55:29 +00001285 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001286 target_ulong addr, len;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001287 GDBThreadIdKind thread_kind;
ths3b46e622007-09-17 08:09:54 +00001288
Doug Gale5c9522b2017-12-02 20:30:37 -05001289 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01001290
bellard858693c2004-03-31 18:52:07 +00001291 p = line_buf;
1292 ch = *p++;
1293 switch(ch) {
Luc Michel53fd6552019-01-07 15:23:46 +00001294 case '!':
1295 put_packet(s, "OK");
1296 break;
bellard858693c2004-03-31 18:52:07 +00001297 case '?':
bellard1fddef42005-04-17 19:16:13 +00001298 /* TODO: Make this return the correct value for user-mode. */
Luc Michel1a227332019-01-07 15:23:45 +00001299 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1300 gdb_fmt_thread_id(s, s->c_cpu, thread_id, sizeof(thread_id)));
bellard858693c2004-03-31 18:52:07 +00001301 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001302 /* Remove all the breakpoints when this query is issued,
1303 * because gdb is doing and initial connect and the state
1304 * should be cleaned up.
1305 */
aliguori880a7572008-11-18 20:30:24 +00001306 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001307 break;
1308 case 'c':
1309 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001310 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001311 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001312 }
aurel32ca587a82008-12-18 22:44:13 +00001313 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001314 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001315 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001316 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001317 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1318 if (s->signal == -1)
1319 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001320 gdb_continue(s);
1321 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001322 case 'v':
1323 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001324 p += 4;
1325 if (*p == '?') {
1326 put_packet(s, "vCont;c;C;s;S");
1327 break;
1328 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001329
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001330 res = gdb_handle_vcont(s, p);
1331
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001332 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001333 if ((res == -EINVAL) || (res == -ERANGE)) {
1334 put_packet(s, "E22");
1335 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001336 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001337 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001338 }
1339 break;
Luc Michel3f940dc2019-01-07 15:23:46 +00001340 } else if (strncmp(p, "Attach;", 7) == 0) {
1341 unsigned long pid;
1342
1343 p += 7;
1344
1345 if (qemu_strtoul(p, &p, 16, &pid)) {
1346 put_packet(s, "E22");
1347 break;
1348 }
1349
1350 process = gdb_get_process(s, pid);
1351
1352 if (process == NULL) {
1353 put_packet(s, "E22");
1354 break;
1355 }
1356
1357 cpu = get_first_cpu_in_process(s, process);
1358
1359 if (cpu == NULL) {
1360 /* Refuse to attach an empty process */
1361 put_packet(s, "E22");
1362 break;
1363 }
1364
1365 process->attached = true;
1366
1367 s->g_cpu = cpu;
1368 s->c_cpu = cpu;
1369
1370 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1371 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)));
1372
1373 put_packet(s, buf);
1374 break;
Max Filippov45a4de22019-02-05 16:52:41 +00001375 } else if (strncmp(p, "Kill;", 5) == 0) {
1376 /* Kill the target */
Sandra Loosemore0f8b09b2019-02-15 09:56:41 +00001377 put_packet(s, "OK");
Max Filippov45a4de22019-02-05 16:52:41 +00001378 error_report("QEMU: Terminated via GDBstub");
1379 exit(0);
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001380 } else {
1381 goto unknown_command;
1382 }
edgar_igl7d03f822008-05-17 18:58:29 +00001383 case 'k':
1384 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001385 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001386 exit(0);
1387 case 'D':
1388 /* Detach packet */
Luc Michel546f3c62019-01-07 15:23:46 +00001389 pid = 1;
1390
1391 if (s->multiprocess) {
1392 unsigned long lpid;
1393 if (*p != ';') {
1394 put_packet(s, "E22");
1395 break;
1396 }
1397
1398 if (qemu_strtoul(p + 1, &p, 16, &lpid)) {
1399 put_packet(s, "E22");
1400 break;
1401 }
1402
1403 pid = lpid;
1404 }
1405
1406 process = gdb_get_process(s, pid);
1407 gdb_process_breakpoint_remove_all(s, process);
1408 process->attached = false;
1409
1410 if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
1411 s->c_cpu = gdb_first_attached_cpu(s);
1412 }
1413
1414 if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
1415 s->g_cpu = gdb_first_attached_cpu(s);
1416 }
1417
1418 if (s->c_cpu == NULL) {
1419 /* No more process attached */
1420 gdb_syscall_mode = GDB_SYS_DISABLED;
1421 gdb_continue(s);
1422 }
edgar_igl7d03f822008-05-17 18:58:29 +00001423 put_packet(s, "OK");
1424 break;
bellard858693c2004-03-31 18:52:07 +00001425 case 's':
1426 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001427 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001428 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001429 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001430 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001431 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001432 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001433 case 'F':
1434 {
1435 target_ulong ret;
1436 target_ulong err;
1437
1438 ret = strtoull(p, (char **)&p, 16);
1439 if (*p == ',') {
1440 p++;
1441 err = strtoull(p, (char **)&p, 16);
1442 } else {
1443 err = 0;
1444 }
1445 if (*p == ',')
1446 p++;
1447 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001448 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001449 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001450 s->current_syscall_cb = NULL;
1451 }
pbrooka2d1eba2007-01-28 03:10:55 +00001452 if (type == 'C') {
1453 put_packet(s, "T02");
1454 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001455 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001456 }
1457 }
1458 break;
bellard858693c2004-03-31 18:52:07 +00001459 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001460 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001461 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001462 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001463 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001464 len += reg_size;
1465 }
1466 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001467 put_packet(s, buf);
1468 break;
1469 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001470 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001471 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001472 len = strlen(p) / 2;
1473 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001474 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001475 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001476 len -= reg_size;
1477 registers += reg_size;
1478 }
bellard858693c2004-03-31 18:52:07 +00001479 put_packet(s, "OK");
1480 break;
1481 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001482 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001483 if (*p == ',')
1484 p++;
bellard9d9754a2006-06-25 15:32:37 +00001485 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001486
1487 /* memtohex() doubles the required space */
1488 if (len > MAX_PACKET_LENGTH / 2) {
1489 put_packet (s, "E22");
1490 break;
1491 }
1492
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001493 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001494 put_packet (s, "E14");
1495 } else {
1496 memtohex(buf, mem_buf, len);
1497 put_packet(s, buf);
1498 }
bellard858693c2004-03-31 18:52:07 +00001499 break;
1500 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001501 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001502 if (*p == ',')
1503 p++;
bellard9d9754a2006-06-25 15:32:37 +00001504 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001505 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001506 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001507
1508 /* hextomem() reads 2*len bytes */
1509 if (len > strlen(p) / 2) {
1510 put_packet (s, "E22");
1511 break;
1512 }
bellard858693c2004-03-31 18:52:07 +00001513 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001514 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001515 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001516 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001517 } else {
bellard858693c2004-03-31 18:52:07 +00001518 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001519 }
bellard858693c2004-03-31 18:52:07 +00001520 break;
pbrook56aebc82008-10-11 17:55:29 +00001521 case 'p':
1522 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1523 This works, but can be very slow. Anything new enough to
1524 understand XML also knows how to use this properly. */
1525 if (!gdb_has_xml)
1526 goto unknown_command;
1527 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001528 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001529 if (reg_size) {
1530 memtohex(buf, mem_buf, reg_size);
1531 put_packet(s, buf);
1532 } else {
1533 put_packet(s, "E14");
1534 }
1535 break;
1536 case 'P':
1537 if (!gdb_has_xml)
1538 goto unknown_command;
1539 addr = strtoull(p, (char **)&p, 16);
1540 if (*p == '=')
1541 p++;
1542 reg_size = strlen(p) / 2;
1543 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001544 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001545 put_packet(s, "OK");
1546 break;
bellard858693c2004-03-31 18:52:07 +00001547 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001548 case 'z':
1549 type = strtoul(p, (char **)&p, 16);
1550 if (*p == ',')
1551 p++;
bellard9d9754a2006-06-25 15:32:37 +00001552 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001553 if (*p == ',')
1554 p++;
bellard9d9754a2006-06-25 15:32:37 +00001555 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001556 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001557 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001558 else
aliguori880a7572008-11-18 20:30:24 +00001559 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001560 if (res >= 0)
1561 put_packet(s, "OK");
1562 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001563 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001564 else
1565 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001566 break;
aliguori880a7572008-11-18 20:30:24 +00001567 case 'H':
1568 type = *p++;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001569
1570 thread_kind = read_thread_id(p, &p, &pid, &tid);
1571 if (thread_kind == GDB_READ_THREAD_ERR) {
1572 put_packet(s, "E22");
1573 break;
1574 }
1575
1576 if (thread_kind != GDB_ONE_THREAD) {
aliguori880a7572008-11-18 20:30:24 +00001577 put_packet(s, "OK");
1578 break;
1579 }
Luc Michel7d8c87d2019-01-07 15:23:45 +00001580 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001581 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001582 put_packet(s, "E22");
1583 break;
1584 }
1585 switch (type) {
1586 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001587 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001588 put_packet(s, "OK");
1589 break;
1590 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001591 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001592 put_packet(s, "OK");
1593 break;
1594 default:
1595 put_packet(s, "E22");
1596 break;
1597 }
1598 break;
1599 case 'T':
Luc Michel7d8c87d2019-01-07 15:23:45 +00001600 thread_kind = read_thread_id(p, &p, &pid, &tid);
1601 if (thread_kind == GDB_READ_THREAD_ERR) {
1602 put_packet(s, "E22");
1603 break;
1604 }
1605 cpu = gdb_get_cpu(s, pid, tid);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001606
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001607 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001608 put_packet(s, "OK");
1609 } else {
aliguori880a7572008-11-18 20:30:24 +00001610 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001611 }
aliguori880a7572008-11-18 20:30:24 +00001612 break;
pbrook978efd62006-06-17 18:30:42 +00001613 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001614 case 'Q':
1615 /* parse any 'q' packets here */
1616 if (!strcmp(p,"qemu.sstepbits")) {
1617 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001618 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1619 SSTEP_ENABLE,
1620 SSTEP_NOIRQ,
1621 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001622 put_packet(s, buf);
1623 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001624 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001625 /* Display or change the sstep_flags */
1626 p += 10;
1627 if (*p != '=') {
1628 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001629 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001630 put_packet(s, buf);
1631 break;
1632 }
1633 p++;
1634 type = strtoul(p, (char **)&p, 16);
1635 sstep_flags = type;
1636 put_packet(s, "OK");
1637 break;
aliguori880a7572008-11-18 20:30:24 +00001638 } else if (strcmp(p,"C") == 0) {
Luc Michel8dbbe9a2019-01-07 15:23:46 +00001639 /*
1640 * "Current thread" remains vague in the spec, so always return
1641 * the first thread of the current process (gdb returns the
1642 * first thread).
1643 */
1644 cpu = get_first_cpu_in_process(s, gdb_get_cpu_process(s, s->g_cpu));
1645 snprintf(buf, sizeof(buf), "QC%s",
1646 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)));
1647 put_packet(s, buf);
aliguori880a7572008-11-18 20:30:24 +00001648 break;
1649 } else if (strcmp(p,"fThreadInfo") == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001650 s->query_cpu = gdb_first_attached_cpu(s);
aliguori880a7572008-11-18 20:30:24 +00001651 goto report_cpuinfo;
1652 } else if (strcmp(p,"sThreadInfo") == 0) {
1653 report_cpuinfo:
1654 if (s->query_cpu) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001655 snprintf(buf, sizeof(buf), "m%s",
1656 gdb_fmt_thread_id(s, s->query_cpu,
1657 thread_id, sizeof(thread_id)));
aliguori880a7572008-11-18 20:30:24 +00001658 put_packet(s, buf);
Luc Michel7cf48f62019-01-07 15:23:46 +00001659 s->query_cpu = gdb_next_attached_cpu(s, s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001660 } else
1661 put_packet(s, "l");
1662 break;
1663 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001664 if (read_thread_id(p + 16, &p, &pid, &tid) == GDB_READ_THREAD_ERR) {
1665 put_packet(s, "E22");
1666 break;
1667 }
1668 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001669 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001670 cpu_synchronize_state(cpu);
Luc Michel7cf48f62019-01-07 15:23:46 +00001671
1672 if (s->multiprocess && (s->process_num > 1)) {
1673 /* Print the CPU model and name in multiprocess mode */
1674 ObjectClass *oc = object_get_class(OBJECT(cpu));
1675 const char *cpu_model = object_class_get_name(oc);
1676 char *cpu_name =
1677 object_get_canonical_path_component(OBJECT(cpu));
1678 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
1679 "%s %s [%s]", cpu_model, cpu_name,
1680 cpu->halted ? "halted " : "running");
1681 g_free(cpu_name);
1682 } else {
1683 /* memtohex() doubles the required space */
1684 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
1685 "CPU#%d [%s]", cpu->cpu_index,
1686 cpu->halted ? "halted " : "running");
1687 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001688 trace_gdbstub_op_extra_info((char *)mem_buf);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001689 memtohex(buf, mem_buf, len);
1690 put_packet(s, buf);
1691 }
aliguori880a7572008-11-18 20:30:24 +00001692 break;
edgar_igl60897d32008-05-09 08:25:14 +00001693 }
blueswir10b8a9882009-03-07 10:51:36 +00001694#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001695 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001696 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001697
blueswir1363a37d2008-08-21 17:58:08 +00001698 snprintf(buf, sizeof(buf),
1699 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1700 ";Bss=" TARGET_ABI_FMT_lx,
1701 ts->info->code_offset,
1702 ts->info->data_offset,
1703 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001704 put_packet(s, buf);
1705 break;
1706 }
blueswir10b8a9882009-03-07 10:51:36 +00001707#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001708 else if (strncmp(p, "Rcmd,", 5) == 0) {
1709 int len = strlen(p + 5);
1710
1711 if ((len % 2) != 0) {
1712 put_packet(s, "E01");
1713 break;
1714 }
aliguori8a34a0f2009-03-05 23:01:55 +00001715 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001716 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001717 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001718 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001719 put_packet(s, "OK");
1720 break;
1721 }
blueswir10b8a9882009-03-07 10:51:36 +00001722#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001723 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001724 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001725 cc = CPU_GET_CLASS(first_cpu);
1726 if (cc->gdb_core_xml_file != NULL) {
1727 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1728 }
Luc Michel364fce62019-01-07 15:23:46 +00001729
1730 if (strstr(p, "multiprocess+")) {
1731 s->multiprocess = true;
1732 }
1733 pstrcat(buf, sizeof(buf), ";multiprocess+");
1734
pbrook56aebc82008-10-11 17:55:29 +00001735 put_packet(s, buf);
1736 break;
1737 }
pbrook56aebc82008-10-11 17:55:29 +00001738 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1739 const char *xml;
1740 target_ulong total_len;
1741
Luc Michelc145eea2019-01-07 15:23:46 +00001742 process = gdb_get_cpu_process(s, s->g_cpu);
1743 cc = CPU_GET_CLASS(s->g_cpu);
Andreas Färber5b24c642013-07-07 15:08:22 +02001744 if (cc->gdb_core_xml_file == NULL) {
1745 goto unknown_command;
1746 }
1747
Andreas Färber5b50e792013-06-29 04:18:45 +02001748 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001749 p += 19;
Luc Michelc145eea2019-01-07 15:23:46 +00001750 xml = get_feature_xml(s, p, &p, process);
pbrook56aebc82008-10-11 17:55:29 +00001751 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001752 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001753 put_packet(s, buf);
1754 break;
1755 }
1756
1757 if (*p == ':')
1758 p++;
1759 addr = strtoul(p, (char **)&p, 16);
1760 if (*p == ',')
1761 p++;
1762 len = strtoul(p, (char **)&p, 16);
1763
1764 total_len = strlen(xml);
1765 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001766 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001767 put_packet(s, buf);
1768 break;
1769 }
1770 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1771 len = (MAX_PACKET_LENGTH - 5) / 2;
1772 if (len < total_len - addr) {
1773 buf[0] = 'm';
1774 len = memtox(buf + 1, xml + addr, len);
1775 } else {
1776 buf[0] = 'l';
1777 len = memtox(buf + 1, xml + addr, total_len - addr);
1778 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001779 put_packet_binary(s, buf, len + 1, true);
pbrook56aebc82008-10-11 17:55:29 +00001780 break;
1781 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001782 if (is_query_packet(p, "Attached", ':')) {
1783 put_packet(s, GDB_ATTACHED);
1784 break;
1785 }
pbrook56aebc82008-10-11 17:55:29 +00001786 /* Unrecognised 'q' command. */
1787 goto unknown_command;
1788
bellard858693c2004-03-31 18:52:07 +00001789 default:
pbrook56aebc82008-10-11 17:55:29 +00001790 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001791 /* put empty packet */
1792 buf[0] = '\0';
1793 put_packet(s, buf);
1794 break;
1795 }
1796 return RS_IDLE;
1797}
1798
Andreas Färber64f6b342013-05-27 02:06:09 +02001799void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001800{
Luc Michel160d8582019-01-07 15:23:46 +00001801 GDBProcess *p = gdb_get_cpu_process(gdbserver_state, cpu);
1802
1803 if (!p->attached) {
1804 /*
1805 * Having a stop CPU corresponding to a process that is not attached
1806 * confuses GDB. So we ignore the request.
1807 */
1808 return;
1809 }
1810
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001811 gdbserver_state->c_cpu = cpu;
1812 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001813}
1814
bellard1fddef42005-04-17 19:16:13 +00001815#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001816static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001817{
aliguori880a7572008-11-18 20:30:24 +00001818 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001819 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001820 char buf[256];
Luc Michel95567c22019-01-07 15:23:46 +00001821 char thread_id[16];
aliguorid6fc1b32008-11-18 19:55:44 +00001822 const char *type;
bellard858693c2004-03-31 18:52:07 +00001823 int ret;
1824
Meador Ingecdb432b2012-03-15 17:49:45 +00001825 if (running || s->state == RS_INACTIVE) {
1826 return;
1827 }
1828 /* Is there a GDB syscall waiting to be sent? */
1829 if (s->current_syscall_cb) {
1830 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001831 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001832 }
Luc Michel95567c22019-01-07 15:23:46 +00001833
1834 if (cpu == NULL) {
1835 /* No process attached */
1836 return;
1837 }
1838
1839 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id));
1840
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001841 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001842 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001843 if (cpu->watchpoint_hit) {
1844 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001845 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001846 type = "r";
1847 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001848 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001849 type = "a";
1850 break;
1851 default:
1852 type = "";
1853 break;
1854 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001855 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
1856 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00001857 snprintf(buf, sizeof(buf),
Luc Michel95567c22019-01-07 15:23:46 +00001858 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
1859 GDB_SIGNAL_TRAP, thread_id, type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001860 (target_ulong)cpu->watchpoint_hit->vaddr);
1861 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001862 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05001863 } else {
1864 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00001865 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001866 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001867 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001868 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001869 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05001870 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00001871 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001872 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001873 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05001874 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01001875 ret = GDB_SIGNAL_QUIT;
1876 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001877 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001878 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001879 ret = GDB_SIGNAL_IO;
1880 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001881 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05001882 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01001883 ret = GDB_SIGNAL_ALRM;
1884 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001885 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001886 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001887 ret = GDB_SIGNAL_ABRT;
1888 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001889 case RUN_STATE_SAVE_VM:
1890 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001891 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001892 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001893 ret = GDB_SIGNAL_XCPU;
1894 break;
1895 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05001896 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01001897 ret = GDB_SIGNAL_UNKNOWN;
1898 break;
bellardbbeb7b52006-04-23 18:42:15 +00001899 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001900 gdb_set_stop_cpu(cpu);
Luc Michel95567c22019-01-07 15:23:46 +00001901 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
Jan Kiszka425189a2011-03-22 11:02:09 +01001902
1903send_packet:
bellard858693c2004-03-31 18:52:07 +00001904 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001905
1906 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001907 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001908}
bellard1fddef42005-04-17 19:16:13 +00001909#endif
bellard858693c2004-03-31 18:52:07 +00001910
pbrooka2d1eba2007-01-28 03:10:55 +00001911/* Send a gdb syscall request.
1912 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001913 %x - target_ulong argument printed in hex.
1914 %lx - 64-bit argument printed in hex.
1915 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001916void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001917{
pbrooka2d1eba2007-01-28 03:10:55 +00001918 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001919 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001920 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001921 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001922 GDBState *s;
1923
aliguori880a7572008-11-18 20:30:24 +00001924 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001925 if (!s)
1926 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001927 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001928#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001929 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001930#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001931 p = s->syscall_buf;
1932 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001933 *(p++) = 'F';
1934 while (*fmt) {
1935 if (*fmt == '%') {
1936 fmt++;
1937 switch (*fmt++) {
1938 case 'x':
1939 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001940 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001941 break;
pbrooka87295e2007-05-26 15:09:38 +00001942 case 'l':
1943 if (*(fmt++) != 'x')
1944 goto bad_format;
1945 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001946 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001947 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001948 case 's':
1949 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001950 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001951 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001952 break;
1953 default:
pbrooka87295e2007-05-26 15:09:38 +00001954 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001955 error_report("gdbstub: Bad syscall format string '%s'",
1956 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001957 break;
1958 }
1959 } else {
1960 *(p++) = *(fmt++);
1961 }
1962 }
pbrook8a93e022007-08-06 13:19:15 +00001963 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001964#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001965 put_packet(s, s->syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01001966 /* Return control to gdb for it to process the syscall request.
1967 * Since the protocol requires that gdb hands control back to us
1968 * using a "here are the results" F packet, we don't need to check
1969 * gdb_handlesig's return value (which is the signal to deliver if
1970 * execution was resumed via a continue packet).
1971 */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001972 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001973#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001974 /* In this case wait to send the syscall packet until notification that
1975 the CPU has stopped. This must be done because if the packet is sent
1976 now the reply from the syscall request could be received while the CPU
1977 is still in the running state, which can cause packets to be dropped
1978 and state transition 'T' packets to be sent while the syscall is still
1979 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001980 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001981#endif
1982}
1983
Peter Maydell19239b32015-09-07 10:39:27 +01001984void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1985{
1986 va_list va;
1987
1988 va_start(va, fmt);
1989 gdb_do_syscallv(cb, fmt, va);
1990 va_end(va);
1991}
1992
Markus Armbruster33c846e2019-05-14 20:03:09 +02001993static void gdb_read_byte(GDBState *s, uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00001994{
ths60fe76f2007-12-16 03:02:09 +00001995 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001996
bellard1fddef42005-04-17 19:16:13 +00001997#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001998 if (s->last_packet_len) {
1999 /* Waiting for a response to the last packet. If we see the start
2000 of a new command then abandon the previous response. */
2001 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002002 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00002003 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01002004 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002005 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002006 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002007 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002008 }
Alex Bennée118e2262017-07-12 11:52:13 +01002009
pbrook4046d912007-01-28 01:53:16 +00002010 if (ch == '+' || ch == '$')
2011 s->last_packet_len = 0;
2012 if (ch != '$')
2013 return;
2014 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002015 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002016 /* when the CPU is running, we cannot do anything except stop
2017 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002018 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002019 } else
bellard1fddef42005-04-17 19:16:13 +00002020#endif
bellard41625032005-04-24 10:07:11 +00002021 {
bellard858693c2004-03-31 18:52:07 +00002022 switch(s->state) {
2023 case RS_IDLE:
2024 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002025 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00002026 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04002027 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00002028 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002029 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002030 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00002031 }
2032 break;
bellard858693c2004-03-31 18:52:07 +00002033 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002034 if (ch == '}') {
2035 /* start escape sequence */
2036 s->state = RS_GETLINE_ESC;
2037 s->line_sum += ch;
2038 } else if (ch == '*') {
2039 /* start run length encoding sequence */
2040 s->state = RS_GETLINE_RLE;
2041 s->line_sum += ch;
2042 } else if (ch == '#') {
2043 /* end of command, start of checksum*/
2044 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00002045 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002046 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00002047 s->state = RS_IDLE;
2048 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002049 /* unescaped command character */
2050 s->line_buf[s->line_buf_index++] = ch;
2051 s->line_sum += ch;
2052 }
2053 break;
2054 case RS_GETLINE_ESC:
2055 if (ch == '#') {
2056 /* unexpected end of command in escape sequence */
2057 s->state = RS_CHKSUM1;
2058 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2059 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002060 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002061 s->state = RS_IDLE;
2062 } else {
2063 /* parse escaped character and leave escape state */
2064 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
2065 s->line_sum += ch;
2066 s->state = RS_GETLINE;
2067 }
2068 break;
2069 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02002070 /*
2071 * Run-length encoding is explained in "Debugging with GDB /
2072 * Appendix E GDB Remote Serial Protocol / Overview".
2073 */
2074 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04002075 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002076 trace_gdbstub_err_invalid_repeat(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002077 s->state = RS_GETLINE;
2078 } else {
2079 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002080 int repeat = ch - ' ' + 3;
Doug Gale4bf43122017-05-01 12:22:10 -04002081 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
2082 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002083 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002084 s->state = RS_IDLE;
2085 } else if (s->line_buf_index < 1) {
2086 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002087 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04002088 s->state = RS_GETLINE;
2089 } else {
2090 /* repeat the last character */
2091 memset(s->line_buf + s->line_buf_index,
2092 s->line_buf[s->line_buf_index - 1], repeat);
2093 s->line_buf_index += repeat;
2094 s->line_sum += ch;
2095 s->state = RS_GETLINE;
2096 }
bellard858693c2004-03-31 18:52:07 +00002097 }
2098 break;
2099 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002100 /* get high hex digit of checksum */
2101 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002102 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002103 s->state = RS_GETLINE;
2104 break;
2105 }
bellard858693c2004-03-31 18:52:07 +00002106 s->line_buf[s->line_buf_index] = '\0';
2107 s->line_csum = fromhex(ch) << 4;
2108 s->state = RS_CHKSUM2;
2109 break;
2110 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002111 /* get low hex digit of checksum */
2112 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002113 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002114 s->state = RS_GETLINE;
2115 break;
bellard858693c2004-03-31 18:52:07 +00002116 }
Doug Gale4bf43122017-05-01 12:22:10 -04002117 s->line_csum |= fromhex(ch);
2118
2119 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002120 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002121 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002122 reply = '-';
2123 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002124 s->state = RS_IDLE;
2125 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002126 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002127 reply = '+';
2128 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002129 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002130 }
bellardb4608c02003-06-27 17:34:32 +00002131 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002132 default:
2133 abort();
bellardb4608c02003-06-27 17:34:32 +00002134 }
2135 }
bellard858693c2004-03-31 18:52:07 +00002136}
2137
Paul Brook0e1c9c52010-06-16 13:03:51 +01002138/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002139void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002140{
2141 GDBState *s;
2142 char buf[4];
2143
2144 s = gdbserver_state;
2145 if (!s) {
2146 return;
2147 }
2148#ifdef CONFIG_USER_ONLY
2149 if (gdbserver_fd < 0 || s->fd < 0) {
2150 return;
2151 }
2152#endif
2153
Doug Gale5c9522b2017-12-02 20:30:37 -05002154 trace_gdbstub_op_exiting((uint8_t)code);
2155
Paul Brook0e1c9c52010-06-16 13:03:51 +01002156 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2157 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002158
2159#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002160 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002161#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002162}
2163
Luc Michel8f468632019-01-07 15:23:45 +00002164/*
2165 * Create the process that will contain all the "orphan" CPUs (that are not
2166 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2167 * be attachable and thus will be invisible to the user.
2168 */
2169static void create_default_process(GDBState *s)
2170{
2171 GDBProcess *process;
2172 int max_pid = 0;
2173
2174 if (s->process_num) {
2175 max_pid = s->processes[s->process_num - 1].pid;
2176 }
2177
2178 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2179 process = &s->processes[s->process_num - 1];
2180
2181 /* We need an available PID slot for this process */
2182 assert(max_pid < UINT32_MAX);
2183
2184 process->pid = max_pid + 1;
2185 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002186 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002187}
2188
bellard1fddef42005-04-17 19:16:13 +00002189#ifdef CONFIG_USER_ONLY
2190int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02002191gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00002192{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002193 GDBState *s;
2194 char buf[256];
2195 int n;
bellard1fddef42005-04-17 19:16:13 +00002196
Andreas Färber5ca666c2013-06-24 19:20:57 +02002197 s = gdbserver_state;
2198 if (gdbserver_fd < 0 || s->fd < 0) {
2199 return sig;
bellard1fddef42005-04-17 19:16:13 +00002200 }
2201
Andreas Färber5ca666c2013-06-24 19:20:57 +02002202 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002203 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002204 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00002205
Andreas Färber5ca666c2013-06-24 19:20:57 +02002206 if (sig != 0) {
2207 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
2208 put_packet(s, buf);
2209 }
2210 /* put_packet() might have detected that the peer terminated the
2211 connection. */
2212 if (s->fd < 0) {
2213 return sig;
2214 }
2215
2216 sig = 0;
2217 s->state = RS_IDLE;
2218 s->running_state = 0;
2219 while (s->running_state == 0) {
2220 n = read(s->fd, buf, 256);
2221 if (n > 0) {
2222 int i;
2223
2224 for (i = 0; i < n; i++) {
2225 gdb_read_byte(s, buf[i]);
2226 }
Peter Wu5819e3e2016-06-05 16:35:48 +02002227 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02002228 /* XXX: Connection closed. Should probably wait for another
2229 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02002230 if (n == 0) {
2231 close(s->fd);
2232 }
2233 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02002234 return sig;
bellard1fddef42005-04-17 19:16:13 +00002235 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02002236 }
2237 sig = s->signal;
2238 s->signal = 0;
2239 return sig;
bellard1fddef42005-04-17 19:16:13 +00002240}
bellarde9009672005-04-26 20:42:36 +00002241
aurel32ca587a82008-12-18 22:44:13 +00002242/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002243void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00002244{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002245 GDBState *s;
2246 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00002247
Andreas Färber5ca666c2013-06-24 19:20:57 +02002248 s = gdbserver_state;
2249 if (gdbserver_fd < 0 || s->fd < 0) {
2250 return;
2251 }
aurel32ca587a82008-12-18 22:44:13 +00002252
Andreas Färber5ca666c2013-06-24 19:20:57 +02002253 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
2254 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00002255}
bellard1fddef42005-04-17 19:16:13 +00002256
Peter Maydell2f652222018-05-14 18:30:44 +01002257static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00002258{
2259 GDBState *s;
2260 struct sockaddr_in sockaddr;
2261 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09002262 int fd;
bellard858693c2004-03-31 18:52:07 +00002263
2264 for(;;) {
2265 len = sizeof(sockaddr);
2266 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2267 if (fd < 0 && errno != EINTR) {
2268 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01002269 return false;
bellard858693c2004-03-31 18:52:07 +00002270 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01002271 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002272 break;
2273 }
2274 }
2275
2276 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01002277 if (socket_set_nodelay(fd)) {
2278 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03002279 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01002280 return false;
2281 }
ths3b46e622007-09-17 08:09:54 +00002282
Anthony Liguori7267c092011-08-20 22:09:37 -05002283 s = g_malloc0(sizeof(GDBState));
Luc Michel8f468632019-01-07 15:23:45 +00002284 create_default_process(s);
Luc Michel970ed902019-01-07 15:23:46 +00002285 s->processes[0].attached = true;
2286 s->c_cpu = gdb_first_attached_cpu(s);
2287 s->g_cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00002288 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02002289 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00002290
aliguori880a7572008-11-18 20:30:24 +00002291 gdbserver_state = s;
Peter Maydell2f652222018-05-14 18:30:44 +01002292 return true;
bellard858693c2004-03-31 18:52:07 +00002293}
2294
2295static int gdbserver_open(int port)
2296{
2297 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002298 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00002299
2300 fd = socket(PF_INET, SOCK_STREAM, 0);
2301 if (fd < 0) {
2302 perror("socket");
2303 return -1;
2304 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01002305 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002306
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002307 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00002308
2309 sockaddr.sin_family = AF_INET;
2310 sockaddr.sin_port = htons(port);
2311 sockaddr.sin_addr.s_addr = 0;
2312 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2313 if (ret < 0) {
2314 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00002315 close(fd);
bellard858693c2004-03-31 18:52:07 +00002316 return -1;
2317 }
Peter Wu96165b92016-05-04 11:32:17 +02002318 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00002319 if (ret < 0) {
2320 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00002321 close(fd);
bellard858693c2004-03-31 18:52:07 +00002322 return -1;
2323 }
bellard858693c2004-03-31 18:52:07 +00002324 return fd;
2325}
2326
2327int gdbserver_start(int port)
2328{
2329 gdbserver_fd = gdbserver_open(port);
2330 if (gdbserver_fd < 0)
2331 return -1;
2332 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01002333 if (!gdb_accept()) {
2334 close(gdbserver_fd);
2335 gdbserver_fd = -1;
2336 return -1;
2337 }
bellardb4608c02003-06-27 17:34:32 +00002338 return 0;
2339}
aurel322b1319c2008-12-18 22:44:04 +00002340
2341/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07002342void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00002343{
2344 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02002345
2346 if (gdbserver_fd < 0 || s->fd < 0) {
2347 return;
2348 }
aurel322b1319c2008-12-18 22:44:04 +00002349 close(s->fd);
2350 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02002351 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02002352 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00002353}
pbrook4046d912007-01-28 01:53:16 +00002354#else
thsaa1f17c2007-07-11 22:48:58 +00002355static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00002356{
pbrook56aebc82008-10-11 17:55:29 +00002357 /* We can handle an arbitrarily large amount of data.
2358 Pick the maximum packet size, which is as good as anything. */
2359 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00002360}
2361
thsaa1f17c2007-07-11 22:48:58 +00002362static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00002363{
pbrook4046d912007-01-28 01:53:16 +00002364 int i;
2365
2366 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00002367 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00002368 }
2369}
2370
2371static void gdb_chr_event(void *opaque, int event)
2372{
Luc Michel970ed902019-01-07 15:23:46 +00002373 int i;
2374 GDBState *s = (GDBState *) opaque;
2375
pbrook4046d912007-01-28 01:53:16 +00002376 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05302377 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00002378 /* Start with first process attached, others detached */
2379 for (i = 0; i < s->process_num; i++) {
2380 s->processes[i].attached = !i;
2381 }
2382
2383 s->c_cpu = gdb_first_attached_cpu(s);
2384 s->g_cpu = s->c_cpu;
2385
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002386 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02002387 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00002388 break;
2389 default:
2390 break;
2391 }
2392}
2393
aliguori8a34a0f2009-03-05 23:01:55 +00002394static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2395{
2396 char buf[MAX_PACKET_LENGTH];
2397
2398 buf[0] = 'O';
2399 if (len > (MAX_PACKET_LENGTH/2) - 1)
2400 len = (MAX_PACKET_LENGTH/2) - 1;
2401 memtohex(buf + 1, (uint8_t *)msg, len);
2402 put_packet(s, buf);
2403}
2404
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002405static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00002406{
2407 const char *p = (const char *)buf;
2408 int max_sz;
2409
2410 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2411 for (;;) {
2412 if (len <= max_sz) {
2413 gdb_monitor_output(gdbserver_state, p, len);
2414 break;
2415 }
2416 gdb_monitor_output(gdbserver_state, p, max_sz);
2417 p += max_sz;
2418 len -= max_sz;
2419 }
2420 return len;
2421}
2422
aliguori59030a82009-04-05 18:43:41 +00002423#ifndef _WIN32
2424static void gdb_sigterm_handler(int signal)
2425{
Luiz Capitulino13548692011-07-29 15:36:43 -03002426 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002427 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002428 }
aliguori59030a82009-04-05 18:43:41 +00002429}
2430#endif
2431
Marc-André Lureau777357d2016-12-07 18:39:10 +03002432static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
2433 bool *be_opened, Error **errp)
2434{
2435 *be_opened = false;
2436}
2437
2438static void char_gdb_class_init(ObjectClass *oc, void *data)
2439{
2440 ChardevClass *cc = CHARDEV_CLASS(oc);
2441
2442 cc->internal = true;
2443 cc->open = gdb_monitor_open;
2444 cc->chr_write = gdb_monitor_write;
2445}
2446
2447#define TYPE_CHARDEV_GDB "chardev-gdb"
2448
2449static const TypeInfo char_gdb_type_info = {
2450 .name = TYPE_CHARDEV_GDB,
2451 .parent = TYPE_CHARDEV,
2452 .class_init = char_gdb_class_init,
2453};
2454
Luc Michel8f468632019-01-07 15:23:45 +00002455static int find_cpu_clusters(Object *child, void *opaque)
2456{
2457 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
2458 GDBState *s = (GDBState *) opaque;
2459 CPUClusterState *cluster = CPU_CLUSTER(child);
2460 GDBProcess *process;
2461
2462 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2463
2464 process = &s->processes[s->process_num - 1];
2465
2466 /*
2467 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2468 * runtime, we enforce here that the machine does not use a cluster ID
2469 * that would lead to PID 0.
2470 */
2471 assert(cluster->cluster_id != UINT32_MAX);
2472 process->pid = cluster->cluster_id + 1;
2473 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002474 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002475
2476 return 0;
2477 }
2478
2479 return object_child_foreach(child, find_cpu_clusters, opaque);
2480}
2481
2482static int pid_order(const void *a, const void *b)
2483{
2484 GDBProcess *pa = (GDBProcess *) a;
2485 GDBProcess *pb = (GDBProcess *) b;
2486
2487 if (pa->pid < pb->pid) {
2488 return -1;
2489 } else if (pa->pid > pb->pid) {
2490 return 1;
2491 } else {
2492 return 0;
2493 }
2494}
2495
2496static void create_processes(GDBState *s)
2497{
2498 object_child_foreach(object_get_root(), find_cpu_clusters, s);
2499
2500 if (s->processes) {
2501 /* Sort by PID */
2502 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
2503 }
2504
2505 create_default_process(s);
2506}
2507
2508static void cleanup_processes(GDBState *s)
2509{
2510 g_free(s->processes);
2511 s->process_num = 0;
2512 s->processes = NULL;
2513}
2514
aliguori59030a82009-04-05 18:43:41 +00002515int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00002516{
Doug Gale5c9522b2017-12-02 20:30:37 -05002517 trace_gdbstub_op_start(device);
2518
pbrook4046d912007-01-28 01:53:16 +00002519 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00002520 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002521 Chardev *chr = NULL;
2522 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00002523
Ziyue Yang508b4ec2017-01-18 16:02:41 +08002524 if (!first_cpu) {
2525 error_report("gdbstub: meaningless to attach gdb to a "
2526 "machine without any CPU.");
2527 return -1;
2528 }
2529
aliguori59030a82009-04-05 18:43:41 +00002530 if (!device)
2531 return -1;
2532 if (strcmp(device, "none") != 0) {
2533 if (strstart(device, "tcp:", NULL)) {
2534 /* enforce required TCP attributes */
2535 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2536 "%s,nowait,nodelay,server", device);
2537 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00002538 }
aliguori59030a82009-04-05 18:43:41 +00002539#ifndef _WIN32
2540 else if (strcmp(device, "stdio") == 0) {
2541 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00002542
aliguori59030a82009-04-05 18:43:41 +00002543 memset(&act, 0, sizeof(act));
2544 act.sa_handler = gdb_sigterm_handler;
2545 sigaction(SIGINT, &act, NULL);
2546 }
2547#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02002548 /*
2549 * FIXME: it's a bit weird to allow using a mux chardev here
2550 * and implicitly setup a monitor. We may want to break this.
2551 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01002552 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
aliguori36556b22009-03-28 18:05:53 +00002553 if (!chr)
2554 return -1;
pbrookcfc34752007-02-22 01:48:01 +00002555 }
2556
aliguori36556b22009-03-28 18:05:53 +00002557 s = gdbserver_state;
2558 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002559 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00002560 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002561
aliguori36556b22009-03-28 18:05:53 +00002562 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2563
2564 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03002565 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01002566 NULL, NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00002567 monitor_init(mon_chr, 0);
2568 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002569 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00002570 mon_chr = s->mon_chr;
Luc Michel8f468632019-01-07 15:23:45 +00002571 cleanup_processes(s);
aliguori36556b22009-03-28 18:05:53 +00002572 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002573 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002574 }
Luc Michel8f468632019-01-07 15:23:45 +00002575
2576 create_processes(s);
2577
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002578 if (chr) {
2579 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002580 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Luc Michel970ed902019-01-07 15:23:46 +00002581 gdb_chr_event, NULL, s, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002582 }
aliguori36556b22009-03-28 18:05:53 +00002583 s->state = chr ? RS_IDLE : RS_INACTIVE;
2584 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002585 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002586
pbrook4046d912007-01-28 01:53:16 +00002587 return 0;
2588}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002589
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01002590void gdbserver_cleanup(void)
2591{
2592 if (gdbserver_state) {
2593 put_packet(gdbserver_state, "W00");
2594 }
2595}
2596
Marc-André Lureau777357d2016-12-07 18:39:10 +03002597static void register_types(void)
2598{
2599 type_register_static(&char_gdb_type_info);
2600}
2601
2602type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002603#endif