blob: b1def7e71d205f42dc12136a08cf067c93b0c5f6 [file] [log] [blame]
bellardb4608c02003-06-27 17:34:32 +00001/*
2 * gdb server stub
ths5fafdf22007-09-16 21:08:06 +00003 *
Alex Bennée42a09592019-07-05 13:28:19 +01004 * This implements a subset of the remote protocol as described in:
5 *
6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
7 *
bellard34751872005-07-02 14:31:34 +00008 * Copyright (c) 2003-2005 Fabrice Bellard
bellardb4608c02003-06-27 17:34:32 +00009 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000021 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Alex Bennée42a09592019-07-05 13:28:19 +010022 *
Philippe Mathieu-Daudéb14d0642024-09-11 17:12:04 +020023 * SPDX-License-Identifier: LGPL-2.0-or-later
bellardb4608c02003-06-27 17:34:32 +000024 */
Markus Armbruster856dfd82019-05-23 16:35:06 +020025
Peter Maydelld38ea872016-01-29 17:50:05 +000026#include "qemu/osdep.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020027#include "qemu/ctype.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020028#include "qemu/cutils.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020029#include "qemu/module.h"
Richard Hendersoncc37d982023-03-15 17:43:13 +000030#include "qemu/error-report.h"
Alex Bennée842b42d2022-09-29 12:42:22 +010031#include "trace.h"
Peter Maydell85b4fa02021-09-08 16:44:04 +010032#include "exec/gdbstub.h"
Gustavo Romero133f2022024-07-05 09:40:38 +010033#include "gdbstub/commands.h"
Alex Bennéec5660802023-03-02 18:57:57 -080034#include "gdbstub/syscalls.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020035#ifdef CONFIG_USER_ONLY
Philippe Mathieu-Daudé59272462024-04-25 11:12:19 +020036#include "accel/tcg/vcpu-state.h"
Alex Bennéed96bf492023-03-02 18:57:47 -080037#include "gdbstub/user.h"
bellard1fddef42005-04-17 19:16:13 +000038#else
Luc Michel8f468632019-01-07 15:23:45 +000039#include "hw/cpu/cluster.h"
Like Xu5cc87672019-05-19 04:54:21 +080040#include "hw/boards.h"
bellard1fddef42005-04-17 19:16:13 +000041#endif
Philippe Mathieu-Daudéfe766732023-12-07 16:52:16 +010042#include "hw/core/cpu.h"
bellard67b915a2004-03-31 23:37:16 +000043
Vincent Palatinb3946622017-01-10 11:59:55 +010044#include "sysemu/hw_accel.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020045#include "sysemu/runstate.h"
Philippe Mathieu-Daudé5b5968c2022-12-19 18:09:43 +010046#include "exec/replay-core.h"
Alex Bennée548c9602023-03-02 18:57:43 -080047#include "exec/hwaddr.h"
aurel32ca587a82008-12-18 22:44:13 +000048
Alex Bennéeae7467b2022-09-29 12:42:24 +010049#include "internals.h"
50
pbrook56aebc82008-10-11 17:55:29 +000051typedef struct GDBRegisterState {
52 int base_reg;
Alex Bennéea010bdb2020-03-16 17:21:41 +000053 gdb_get_reg_cb get_reg;
54 gdb_set_reg_cb set_reg;
Akihiko Odakic494f8f2024-02-27 14:43:15 +000055 const GDBFeature *feature;
pbrook56aebc82008-10-11 17:55:29 +000056} GDBRegisterState;
57
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -080058GDBState gdbserver_state;
Alex Bennée8d98c442020-03-16 17:21:33 +000059
Alex Bennée36e067b2023-03-02 18:57:45 -080060void gdb_init_gdbserver_state(void)
Alex Bennée8d98c442020-03-16 17:21:33 +000061{
62 g_assert(!gdbserver_state.init);
63 memset(&gdbserver_state, 0, sizeof(GDBState));
64 gdbserver_state.init = true;
Alex Bennée308f9e82020-03-16 17:21:35 +000065 gdbserver_state.str_buf = g_string_new(NULL);
Alex Bennée4a25f1b2020-03-16 17:21:36 +000066 gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
Damien Hedded116e812020-03-16 17:21:53 +000067 gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4);
Maxim Levitskyecd39d62021-11-11 12:06:02 +010068
69 /*
Alex Bennée3b7a9382022-09-29 12:42:23 +010070 * What single-step modes are supported is accelerator dependent.
71 * By default try to use no IRQs and no timers while single
72 * stepping so as to make single stepping like a typical ICE HW step.
Maxim Levitskyecd39d62021-11-11 12:06:02 +010073 */
Alex Bennée3b7a9382022-09-29 12:42:23 +010074 gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags();
Maxim Levitsky12bc5b42021-11-11 12:06:03 +010075 gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
76 gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags;
Alex Bennée8d98c442020-03-16 17:21:33 +000077}
78
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -030079/* writes 2*len+1 bytes in buf */
Alex Bennée36e067b2023-03-02 18:57:45 -080080void gdb_memtohex(GString *buf, const uint8_t *mem, int len)
bellardb4608c02003-06-27 17:34:32 +000081{
82 int i, c;
bellardb4608c02003-06-27 17:34:32 +000083 for(i = 0; i < len; i++) {
84 c = mem[i];
Alex Bennée308f9e82020-03-16 17:21:35 +000085 g_string_append_c(buf, tohex(c >> 4));
86 g_string_append_c(buf, tohex(c & 0xf));
bellardb4608c02003-06-27 17:34:32 +000087 }
Alex Bennée308f9e82020-03-16 17:21:35 +000088 g_string_append_c(buf, '\0');
bellardb4608c02003-06-27 17:34:32 +000089}
90
Alex Bennée36e067b2023-03-02 18:57:45 -080091void gdb_hextomem(GByteArray *mem, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +000092{
93 int i;
94
95 for(i = 0; i < len; i++) {
Alex Bennée4a25f1b2020-03-16 17:21:36 +000096 guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
97 g_byte_array_append(mem, &byte, 1);
bellardb4608c02003-06-27 17:34:32 +000098 buf += 2;
99 }
100}
101
Doug Gale5c9522b2017-12-02 20:30:37 -0500102static void hexdump(const char *buf, int len,
103 void (*trace_fn)(size_t ofs, char const *text))
104{
105 char line_buffer[3 * 16 + 4 + 16 + 1];
106
107 size_t i;
108 for (i = 0; i < len || (i & 0xF); ++i) {
109 size_t byte_ofs = i & 15;
110
111 if (byte_ofs == 0) {
112 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
113 line_buffer[3 * 16 + 4 + 16] = 0;
114 }
115
116 size_t col_group = (i >> 2) & 3;
117 size_t hex_col = byte_ofs * 3 + col_group;
118 size_t txt_col = 3 * 16 + 4 + byte_ofs;
119
120 if (i < len) {
121 char value = buf[i];
122
123 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
124 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
125 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
126 ? value
127 : '.';
128 }
129
130 if (byte_ofs == 0xF)
131 trace_fn(i & -16, line_buffer);
132 }
133}
134
bellardb4608c02003-06-27 17:34:32 +0000135/* return -1 if error, 0 if OK */
Alex Bennée36e067b2023-03-02 18:57:45 -0800136int gdb_put_packet_binary(const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000137{
pbrook56aebc82008-10-11 17:55:29 +0000138 int csum, i;
Damien Hedded116e812020-03-16 17:21:53 +0000139 uint8_t footer[3];
bellardb4608c02003-06-27 17:34:32 +0000140
Doug Gale5c9522b2017-12-02 20:30:37 -0500141 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
142 hexdump(buf, len, trace_gdbstub_io_binaryreply);
143 }
144
bellardb4608c02003-06-27 17:34:32 +0000145 for(;;) {
Damien Hedded116e812020-03-16 17:21:53 +0000146 g_byte_array_set_size(gdbserver_state.last_packet, 0);
147 g_byte_array_append(gdbserver_state.last_packet,
148 (const uint8_t *) "$", 1);
149 g_byte_array_append(gdbserver_state.last_packet,
150 (const uint8_t *) buf, len);
bellardb4608c02003-06-27 17:34:32 +0000151 csum = 0;
152 for(i = 0; i < len; i++) {
153 csum += buf[i];
154 }
Damien Hedded116e812020-03-16 17:21:53 +0000155 footer[0] = '#';
156 footer[1] = tohex((csum >> 4) & 0xf);
157 footer[2] = tohex((csum) & 0xf);
158 g_byte_array_append(gdbserver_state.last_packet, footer, 3);
bellardb4608c02003-06-27 17:34:32 +0000159
Alex Bennée36e067b2023-03-02 18:57:45 -0800160 gdb_put_buffer(gdbserver_state.last_packet->data,
Damien Hedded116e812020-03-16 17:21:53 +0000161 gdbserver_state.last_packet->len);
bellardb4608c02003-06-27 17:34:32 +0000162
Alex Bennéea7e0f9b2023-03-02 18:57:49 -0800163 if (gdb_got_immediate_ack()) {
bellardb4608c02003-06-27 17:34:32 +0000164 break;
Alex Bennéea7e0f9b2023-03-02 18:57:49 -0800165 }
bellardb4608c02003-06-27 17:34:32 +0000166 }
167 return 0;
168}
169
pbrook56aebc82008-10-11 17:55:29 +0000170/* return -1 if error, 0 if OK */
Alex Bennée36e067b2023-03-02 18:57:45 -0800171int gdb_put_packet(const char *buf)
pbrook56aebc82008-10-11 17:55:29 +0000172{
Doug Gale5c9522b2017-12-02 20:30:37 -0500173 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000174
Alex Bennée36e067b2023-03-02 18:57:45 -0800175 return gdb_put_packet_binary(buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000176}
177
Alex Bennée36e067b2023-03-02 18:57:45 -0800178void gdb_put_strbuf(void)
pbrook56aebc82008-10-11 17:55:29 +0000179{
Alex Bennée36e067b2023-03-02 18:57:45 -0800180 gdb_put_packet(gdbserver_state.str_buf->str);
Alex Bennée308f9e82020-03-16 17:21:35 +0000181}
182
183/* Encode data using the encoding for 'x' packets. */
Alex Bennée36e067b2023-03-02 18:57:45 -0800184void gdb_memtox(GString *buf, const char *mem, int len)
Alex Bennée308f9e82020-03-16 17:21:35 +0000185{
pbrook56aebc82008-10-11 17:55:29 +0000186 char c;
187
188 while (len--) {
189 c = *(mem++);
190 switch (c) {
191 case '#': case '$': case '*': case '}':
Alex Bennée308f9e82020-03-16 17:21:35 +0000192 g_string_append_c(buf, '}');
193 g_string_append_c(buf, c ^ 0x20);
pbrook56aebc82008-10-11 17:55:29 +0000194 break;
195 default:
Alex Bennée308f9e82020-03-16 17:21:35 +0000196 g_string_append_c(buf, c);
pbrook56aebc82008-10-11 17:55:29 +0000197 break;
198 }
199 }
pbrook56aebc82008-10-11 17:55:29 +0000200}
201
Alex Bennéea346af32020-03-16 17:21:34 +0000202static uint32_t gdb_get_cpu_pid(CPUState *cpu)
Luc Michel1a227332019-01-07 15:23:45 +0000203{
Ilya Leoshkevichdc14a7a2023-06-30 19:04:20 +0100204#ifdef CONFIG_USER_ONLY
205 return getpid();
206#else
Peter Maydell46f5abc2019-01-29 11:46:06 +0000207 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
208 /* Return the default process' PID */
Alex Bennéea346af32020-03-16 17:21:34 +0000209 int index = gdbserver_state.process_num - 1;
210 return gdbserver_state.processes[index].pid;
Peter Maydell46f5abc2019-01-29 11:46:06 +0000211 }
212 return cpu->cluster_index + 1;
Ilya Leoshkevichdc14a7a2023-06-30 19:04:20 +0100213#endif
Luc Michel1a227332019-01-07 15:23:45 +0000214}
215
Ilya Leoshkevicha3fcc112023-06-30 19:04:19 +0100216GDBProcess *gdb_get_process(uint32_t pid)
Luc Michel7d8c87d2019-01-07 15:23:45 +0000217{
218 int i;
219
220 if (!pid) {
221 /* 0 means any process, we take the first one */
Alex Bennéea346af32020-03-16 17:21:34 +0000222 return &gdbserver_state.processes[0];
Luc Michel7d8c87d2019-01-07 15:23:45 +0000223 }
224
Alex Bennéea346af32020-03-16 17:21:34 +0000225 for (i = 0; i < gdbserver_state.process_num; i++) {
226 if (gdbserver_state.processes[i].pid == pid) {
227 return &gdbserver_state.processes[i];
Luc Michel7d8c87d2019-01-07 15:23:45 +0000228 }
229 }
230
231 return NULL;
232}
233
Alex Bennéea346af32020-03-16 17:21:34 +0000234static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
Luc Michel7d8c87d2019-01-07 15:23:45 +0000235{
Alex Bennéea346af32020-03-16 17:21:34 +0000236 return gdb_get_process(gdb_get_cpu_pid(cpu));
Luc Michel7d8c87d2019-01-07 15:23:45 +0000237}
238
239static CPUState *find_cpu(uint32_t thread_id)
240{
241 CPUState *cpu;
242
243 CPU_FOREACH(cpu) {
Alex Bennée36e067b2023-03-02 18:57:45 -0800244 if (gdb_get_cpu_index(cpu) == thread_id) {
Luc Michel7d8c87d2019-01-07 15:23:45 +0000245 return cpu;
246 }
247 }
248
249 return NULL;
250}
251
Ilya Leoshkevicha3fcc112023-06-30 19:04:19 +0100252CPUState *gdb_get_first_cpu_in_process(GDBProcess *process)
Luc Michele40e5202019-01-07 15:23:46 +0000253{
254 CPUState *cpu;
255
256 CPU_FOREACH(cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000257 if (gdb_get_cpu_pid(cpu) == process->pid) {
Luc Michele40e5202019-01-07 15:23:46 +0000258 return cpu;
259 }
260 }
261
262 return NULL;
263}
264
Alex Bennéea346af32020-03-16 17:21:34 +0000265static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
Luc Michele40e5202019-01-07 15:23:46 +0000266{
Alex Bennéea346af32020-03-16 17:21:34 +0000267 uint32_t pid = gdb_get_cpu_pid(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000268 cpu = CPU_NEXT(cpu);
269
270 while (cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000271 if (gdb_get_cpu_pid(cpu) == pid) {
Luc Michele40e5202019-01-07 15:23:46 +0000272 break;
273 }
274
275 cpu = CPU_NEXT(cpu);
276 }
277
278 return cpu;
279}
280
Luc Michele40e5202019-01-07 15:23:46 +0000281/* Return the cpu following @cpu, while ignoring unattached processes. */
Alex Bennéea346af32020-03-16 17:21:34 +0000282static CPUState *gdb_next_attached_cpu(CPUState *cpu)
Luc Michele40e5202019-01-07 15:23:46 +0000283{
284 cpu = CPU_NEXT(cpu);
285
286 while (cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000287 if (gdb_get_cpu_process(cpu)->attached) {
Luc Michele40e5202019-01-07 15:23:46 +0000288 break;
289 }
290
291 cpu = CPU_NEXT(cpu);
292 }
293
294 return cpu;
295}
296
297/* Return the first attached cpu */
Alex Bennée36e067b2023-03-02 18:57:45 -0800298CPUState *gdb_first_attached_cpu(void)
Luc Michele40e5202019-01-07 15:23:46 +0000299{
300 CPUState *cpu = first_cpu;
Alex Bennéea346af32020-03-16 17:21:34 +0000301 GDBProcess *process = gdb_get_cpu_process(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000302
303 if (!process->attached) {
Alex Bennéea346af32020-03-16 17:21:34 +0000304 return gdb_next_attached_cpu(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000305 }
306
307 return cpu;
308}
309
Alex Bennéea346af32020-03-16 17:21:34 +0000310static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
Luc Michelab65eed2019-01-29 11:46:03 +0000311{
312 GDBProcess *process;
313 CPUState *cpu;
314
315 if (!pid && !tid) {
316 /* 0 means any process/thread, we take the first attached one */
Alex Bennéea346af32020-03-16 17:21:34 +0000317 return gdb_first_attached_cpu();
Luc Michelab65eed2019-01-29 11:46:03 +0000318 } else if (pid && !tid) {
319 /* any thread in a specific process */
Alex Bennéea346af32020-03-16 17:21:34 +0000320 process = gdb_get_process(pid);
Luc Michelab65eed2019-01-29 11:46:03 +0000321
322 if (process == NULL) {
323 return NULL;
324 }
325
326 if (!process->attached) {
327 return NULL;
328 }
329
Ilya Leoshkevicha3fcc112023-06-30 19:04:19 +0100330 return gdb_get_first_cpu_in_process(process);
Luc Michelab65eed2019-01-29 11:46:03 +0000331 } else {
332 /* a specific thread */
333 cpu = find_cpu(tid);
334
335 if (cpu == NULL) {
336 return NULL;
337 }
338
Alex Bennéea346af32020-03-16 17:21:34 +0000339 process = gdb_get_cpu_process(cpu);
Luc Michelab65eed2019-01-29 11:46:03 +0000340
341 if (pid && process->pid != pid) {
342 return NULL;
343 }
344
345 if (!process->attached) {
346 return NULL;
347 }
348
349 return cpu;
350 }
351}
352
Alex Bennéea346af32020-03-16 17:21:34 +0000353static const char *get_feature_xml(const char *p, const char **newp,
354 GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000355{
Ilya Leoshkevicha3fcc112023-06-30 19:04:19 +0100356 CPUState *cpu = gdb_get_first_cpu_in_process(process);
Luc Michelc145eea2019-01-07 15:23:46 +0000357 CPUClass *cc = CPU_GET_CLASS(cpu);
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000358 GDBRegisterState *r;
Alex Bennée56e534b2023-08-29 17:15:26 +0100359 size_t len;
pbrook56aebc82008-10-11 17:55:29 +0000360
Alex Bennée56e534b2023-08-29 17:15:26 +0100361 /*
362 * qXfer:features:read:ANNEX:OFFSET,LENGTH'
363 * ^p ^newp
364 */
365 char *term = strchr(p, ':');
366 *newp = term + 1;
367 len = term - p;
pbrook56aebc82008-10-11 17:55:29 +0000368
Alex Bennée56e534b2023-08-29 17:15:26 +0100369 /* Is it the main target xml? */
pbrook56aebc82008-10-11 17:55:29 +0000370 if (strncmp(p, "target.xml", len) == 0) {
Alex Bennée56e534b2023-08-29 17:15:26 +0100371 if (!process->target_xml) {
Akihiko Odaki6d8f77a2023-10-09 17:40:54 +0100372 g_autoptr(GPtrArray) xml = g_ptr_array_new_with_free_func(g_free);
Luc Michelc145eea2019-01-07 15:23:46 +0000373
Akihiko Odaki6d8f77a2023-10-09 17:40:54 +0100374 g_ptr_array_add(
375 xml,
376 g_strdup("<?xml version=\"1.0\"?>"
377 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
378 "<target>"));
Alex Bennée56e534b2023-08-29 17:15:26 +0100379
David Hildenbrandb3820e62015-12-03 13:14:41 +0100380 if (cc->gdb_arch_name) {
Akihiko Odaki6d8f77a2023-10-09 17:40:54 +0100381 g_ptr_array_add(
382 xml,
383 g_markup_printf_escaped("<architecture>%s</architecture>",
384 cc->gdb_arch_name(cpu)));
David Hildenbrandb3820e62015-12-03 13:14:41 +0100385 }
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000386 for (guint i = 0; i < cpu->gdb_regs->len; i++) {
387 r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
388 g_ptr_array_add(
389 xml,
390 g_markup_printf_escaped("<xi:include href=\"%s\"/>",
391 r->feature->xmlname));
pbrook56aebc82008-10-11 17:55:29 +0000392 }
Akihiko Odaki6d8f77a2023-10-09 17:40:54 +0100393 g_ptr_array_add(xml, g_strdup("</target>"));
394 g_ptr_array_add(xml, NULL);
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100395
Akihiko Odaki6d8f77a2023-10-09 17:40:54 +0100396 process->target_xml = g_strjoinv(NULL, (void *)xml->pdata);
Alex Bennée56e534b2023-08-29 17:15:26 +0100397 }
Akihiko Odaki5d1ab242023-10-09 17:40:48 +0100398 return process->target_xml;
Alex Bennée56e534b2023-08-29 17:15:26 +0100399 }
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000400 /* Is it one of the features? */
401 for (guint i = 0; i < cpu->gdb_regs->len; i++) {
402 r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
403 if (strncmp(p, r->feature->xmlname, len) == 0) {
404 return r->feature->xml;
Alex Bennée56e534b2023-08-29 17:15:26 +0100405 }
pbrook56aebc82008-10-11 17:55:29 +0000406 }
Alex Bennée56e534b2023-08-29 17:15:26 +0100407
408 /* failed */
409 return NULL;
pbrook56aebc82008-10-11 17:55:29 +0000410}
pbrook56aebc82008-10-11 17:55:29 +0000411
Akihiko Odakie84f4522023-11-06 18:51:00 +0000412void gdb_feature_builder_init(GDBFeatureBuilder *builder, GDBFeature *feature,
413 const char *name, const char *xmlname,
414 int base_reg)
415{
416 char *header = g_markup_printf_escaped(
417 "<?xml version=\"1.0\"?>"
418 "<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">"
419 "<feature name=\"%s\">",
420 name);
421
422 builder->feature = feature;
423 builder->xml = g_ptr_array_new();
424 g_ptr_array_add(builder->xml, header);
Akihiko Odakieb370862024-02-27 14:43:20 +0000425 builder->regs = g_ptr_array_new();
Akihiko Odakie84f4522023-11-06 18:51:00 +0000426 builder->base_reg = base_reg;
427 feature->xmlname = xmlname;
Akihiko Odakieb370862024-02-27 14:43:20 +0000428 feature->name = name;
Akihiko Odakie84f4522023-11-06 18:51:00 +0000429}
430
431void gdb_feature_builder_append_tag(const GDBFeatureBuilder *builder,
432 const char *format, ...)
433{
434 va_list ap;
435 va_start(ap, format);
436 g_ptr_array_add(builder->xml, g_markup_vprintf_escaped(format, ap));
437 va_end(ap);
438}
439
440void gdb_feature_builder_append_reg(const GDBFeatureBuilder *builder,
441 const char *name,
442 int bitsize,
443 int regnum,
444 const char *type,
445 const char *group)
446{
Akihiko Odakieb370862024-02-27 14:43:20 +0000447 if (builder->regs->len <= regnum) {
448 g_ptr_array_set_size(builder->regs, regnum + 1);
Akihiko Odakie84f4522023-11-06 18:51:00 +0000449 }
450
Akihiko Odakieb370862024-02-27 14:43:20 +0000451 builder->regs->pdata[regnum] = (gpointer *)name;
452
Akihiko Odakie84f4522023-11-06 18:51:00 +0000453 if (group) {
454 gdb_feature_builder_append_tag(
455 builder,
456 "<reg name=\"%s\" bitsize=\"%d\" regnum=\"%d\" type=\"%s\" group=\"%s\"/>",
457 name, bitsize, builder->base_reg + regnum, type, group);
458 } else {
459 gdb_feature_builder_append_tag(
460 builder,
461 "<reg name=\"%s\" bitsize=\"%d\" regnum=\"%d\" type=\"%s\"/>",
462 name, bitsize, builder->base_reg + regnum, type);
463 }
464}
465
466void gdb_feature_builder_end(const GDBFeatureBuilder *builder)
467{
468 g_ptr_array_add(builder->xml, (void *)"</feature>");
469 g_ptr_array_add(builder->xml, NULL);
470
471 builder->feature->xml = g_strjoinv(NULL, (void *)builder->xml->pdata);
472
473 for (guint i = 0; i < builder->xml->len - 2; i++) {
474 g_free(g_ptr_array_index(builder->xml, i));
475 }
476
477 g_ptr_array_free(builder->xml, TRUE);
Akihiko Odakieb370862024-02-27 14:43:20 +0000478
479 builder->feature->num_regs = builder->regs->len;
480 builder->feature->regs = (void *)g_ptr_array_free(builder->regs, FALSE);
Akihiko Odakie84f4522023-11-06 18:51:00 +0000481}
482
Akihiko Odaki1218b682023-11-06 18:50:59 +0000483const GDBFeature *gdb_find_static_feature(const char *xmlname)
484{
485 const GDBFeature *feature;
486
487 for (feature = gdb_static_features; feature->xmlname; feature++) {
488 if (!strcmp(feature->xmlname, xmlname)) {
489 return feature;
490 }
491 }
492
493 g_assert_not_reached();
494}
495
Alex Bennéec3d0b462024-02-27 14:43:27 +0000496GArray *gdb_get_register_list(CPUState *cpu)
497{
498 GArray *results = g_array_new(true, true, sizeof(GDBRegDesc));
499
500 /* registers are only available once the CPU is initialised */
501 if (!cpu->gdb_regs) {
502 return results;
503 }
504
505 for (int f = 0; f < cpu->gdb_regs->len; f++) {
506 GDBRegisterState *r = &g_array_index(cpu->gdb_regs, GDBRegisterState, f);
507 for (int i = 0; i < r->feature->num_regs; i++) {
508 const char *name = r->feature->regs[i];
509 GDBRegDesc desc = {
510 r->base_reg + i,
511 name,
512 r->feature->name
513 };
514 g_array_append_val(results, desc);
515 }
516 }
517
518 return results;
519}
520
521int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000522{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200523 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000524 GDBRegisterState *r;
525
Andreas Färbera0e372f2013-06-28 23:18:47 +0200526 if (reg < cc->gdb_num_core_regs) {
Alex Bennéea010bdb2020-03-16 17:21:41 +0000527 return cc->gdb_read_register(cpu, buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200528 }
pbrook56aebc82008-10-11 17:55:29 +0000529
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000530 for (guint i = 0; i < cpu->gdb_regs->len; i++) {
531 r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
532 if (r->base_reg <= reg && reg < r->base_reg + r->feature->num_regs) {
533 return r->get_reg(cpu, buf, reg - r->base_reg);
pbrook56aebc82008-10-11 17:55:29 +0000534 }
535 }
536 return 0;
537}
538
Andreas Färber385b9f02013-06-27 18:25:36 +0200539static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000540{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200541 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000542 GDBRegisterState *r;
543
Andreas Färbera0e372f2013-06-28 23:18:47 +0200544 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200545 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200546 }
pbrook56aebc82008-10-11 17:55:29 +0000547
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000548 for (guint i = 0; i < cpu->gdb_regs->len; i++) {
549 r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
550 if (r->base_reg <= reg && reg < r->base_reg + r->feature->num_regs) {
551 return r->set_reg(cpu, mem_buf, reg - r->base_reg);
pbrook56aebc82008-10-11 17:55:29 +0000552 }
553 }
554 return 0;
555}
556
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000557static void gdb_register_feature(CPUState *cpu, int base_reg,
558 gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
559 const GDBFeature *feature)
560{
561 GDBRegisterState s = {
562 .base_reg = base_reg,
563 .get_reg = get_reg,
564 .set_reg = set_reg,
565 .feature = feature
566 };
567
568 g_array_append_val(cpu->gdb_regs, s);
569}
570
571void gdb_init_cpu(CPUState *cpu)
572{
573 CPUClass *cc = CPU_GET_CLASS(cpu);
574 const GDBFeature *feature;
575
576 cpu->gdb_regs = g_array_new(false, false, sizeof(GDBRegisterState));
577
578 if (cc->gdb_core_xml_file) {
579 feature = gdb_find_static_feature(cc->gdb_core_xml_file);
580 gdb_register_feature(cpu, 0,
581 cc->gdb_read_register, cc->gdb_write_register,
582 feature);
Akihiko Odakiecd6f6a2024-02-27 14:43:18 +0000583 cpu->gdb_num_regs = cpu->gdb_num_g_regs = feature->num_regs;
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000584 }
585
Akihiko Odakiecd6f6a2024-02-27 14:43:18 +0000586 if (cc->gdb_num_core_regs) {
587 cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs;
588 }
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000589}
590
Andreas Färber22169d42013-06-28 21:27:39 +0200591void gdb_register_coprocessor(CPUState *cpu,
Alex Bennéea010bdb2020-03-16 17:21:41 +0000592 gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
Akihiko Odakiac1e8672024-02-27 14:43:14 +0000593 const GDBFeature *feature, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000594{
595 GDBRegisterState *s;
Akihiko Odaki73c392c2023-10-09 17:40:58 +0100596 guint i;
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000597 int base_reg = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000598
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000599 for (i = 0; i < cpu->gdb_regs->len; i++) {
600 /* Check for duplicates. */
601 s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
602 if (s->feature == feature) {
603 return;
Akihiko Odaki73c392c2023-10-09 17:40:58 +0100604 }
pbrook56aebc82008-10-11 17:55:29 +0000605 }
Stefan Weil9643c252011-10-18 22:25:38 +0200606
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000607 gdb_register_feature(cpu, base_reg, get_reg, set_reg, feature);
Stefan Weil9643c252011-10-18 22:25:38 +0200608
pbrook56aebc82008-10-11 17:55:29 +0000609 /* Add to end of list. */
Akihiko Odakiac1e8672024-02-27 14:43:14 +0000610 cpu->gdb_num_regs += feature->num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000611 if (g_pos) {
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000612 if (g_pos != base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800613 error_report("Error: Bad gdb register numbering for '%s', "
Akihiko Odakiee59fa12024-02-27 14:43:17 +0000614 "expected %d got %d", feature->xml, g_pos, base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200615 } else {
616 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000617 }
618 }
619}
620
Salil Mehta242da182024-07-16 12:15:02 +0100621void gdb_unregister_coprocessor_all(CPUState *cpu)
622{
623 /*
624 * Safe to nuke everything. GDBRegisterState::xml is static const char so
625 * it won't be freed
626 */
627 g_array_free(cpu->gdb_regs, true);
628
629 cpu->gdb_regs = NULL;
630 cpu->gdb_num_regs = 0;
631 cpu->gdb_num_g_regs = 0;
632}
633
Alex Bennéea346af32020-03-16 17:21:34 +0000634static void gdb_process_breakpoint_remove_all(GDBProcess *p)
Luc Michel546f3c62019-01-07 15:23:46 +0000635{
Ilya Leoshkevicha3fcc112023-06-30 19:04:19 +0100636 CPUState *cpu = gdb_get_first_cpu_in_process(p);
Luc Michel546f3c62019-01-07 15:23:46 +0000637
638 while (cpu) {
Alex Bennéeae7467b2022-09-29 12:42:24 +0100639 gdb_breakpoint_remove_all(cpu);
Alex Bennéea346af32020-03-16 17:21:34 +0000640 cpu = gdb_next_cpu_in_process(cpu);
Luc Michel546f3c62019-01-07 15:23:46 +0000641 }
642}
643
aliguoria1d1bb32008-11-18 20:07:32 +0000644
Alex Bennéeb428ad12023-03-02 18:57:54 -0800645static void gdb_set_cpu_pc(vaddr pc)
aurel32fab9d282009-04-08 21:29:37 +0000646{
Alex Bennéea346af32020-03-16 17:21:34 +0000647 CPUState *cpu = gdbserver_state.c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200648
649 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -0700650 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +0000651}
652
Alex Bennée36e067b2023-03-02 18:57:45 -0800653void gdb_append_thread_id(CPUState *cpu, GString *buf)
Luc Michel1a227332019-01-07 15:23:45 +0000654{
Alex Bennéea346af32020-03-16 17:21:34 +0000655 if (gdbserver_state.multiprocess) {
Alex Bennée308f9e82020-03-16 17:21:35 +0000656 g_string_append_printf(buf, "p%02x.%02x",
Alex Bennée36e067b2023-03-02 18:57:45 -0800657 gdb_get_cpu_pid(cpu), gdb_get_cpu_index(cpu));
Luc Michel1a227332019-01-07 15:23:45 +0000658 } else {
Alex Bennée36e067b2023-03-02 18:57:45 -0800659 g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu));
Luc Michel1a227332019-01-07 15:23:45 +0000660 }
Luc Michel1a227332019-01-07 15:23:45 +0000661}
662
Luc Michel7d8c87d2019-01-07 15:23:45 +0000663static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
664 uint32_t *pid, uint32_t *tid)
665{
666 unsigned long p, t;
667 int ret;
668
669 if (*buf == 'p') {
670 buf++;
671 ret = qemu_strtoul(buf, &buf, 16, &p);
672
673 if (ret) {
674 return GDB_READ_THREAD_ERR;
675 }
676
677 /* Skip '.' */
678 buf++;
679 } else {
Matheus Tavares Bernardino6c78de62023-08-01 12:37:11 -0300680 p = 0;
Luc Michel7d8c87d2019-01-07 15:23:45 +0000681 }
682
683 ret = qemu_strtoul(buf, &buf, 16, &t);
684
685 if (ret) {
686 return GDB_READ_THREAD_ERR;
687 }
688
689 *end_buf = buf;
690
691 if (p == -1) {
692 return GDB_ALL_PROCESSES;
693 }
694
695 if (pid) {
696 *pid = p;
697 }
698
699 if (t == -1) {
700 return GDB_ALL_THREADS;
701 }
702
703 if (tid) {
704 *tid = t;
705 }
706
707 return GDB_ONE_THREAD;
708}
709
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100710/**
711 * gdb_handle_vcont - Parses and handles a vCont packet.
712 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
713 * a format error, 0 on success.
714 */
Alex Bennéea346af32020-03-16 17:21:34 +0000715static int gdb_handle_vcont(const char *p)
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100716{
Luc Michele40e5202019-01-07 15:23:46 +0000717 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100718 char cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100719 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +0000720 uint32_t pid, tid;
721 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100722 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +0000723 GDBThreadIdKind kind;
Alex Bennée7ea0c332023-03-02 18:57:52 -0800724 unsigned int max_cpus = gdb_get_max_cpus();
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100725 /* uninitialised CPUs stay 0 */
Alex Bennée2261b732023-06-30 19:04:15 +0100726 g_autofree char *newstates = g_new0(char, max_cpus);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100727
728 /* mark valid CPUs with 1 */
729 CPU_FOREACH(cpu) {
730 newstates[cpu->cpu_index] = 1;
731 }
732
733 /*
734 * res keeps track of what error we are returning, with -ENOTSUP meaning
735 * that the command is unknown or unsupported, thus returning an empty
736 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
737 * or incorrect parameters passed.
738 */
739 res = 0;
Matheus Branco Borella761e3c12023-08-29 17:15:24 +0100740
741 /*
742 * target_count and last_target keep track of how many CPUs we are going to
743 * step or resume, and a pointer to the state structure of one of them,
Michael Tokarevac2786f2023-11-14 19:08:08 +0300744 * respectively
Matheus Branco Borella761e3c12023-08-29 17:15:24 +0100745 */
746 int target_count = 0;
747 CPUState *last_target = NULL;
748
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100749 while (*p) {
750 if (*p++ != ';') {
Alex Bennée2261b732023-06-30 19:04:15 +0100751 return -ENOTSUP;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100752 }
753
754 cur_action = *p++;
755 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +0100756 cur_action = qemu_tolower(cur_action);
Peter Maydell3ddd9032020-11-21 21:03:42 +0000757 res = qemu_strtoul(p, &p, 16, &tmp);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100758 if (res) {
Alex Bennée2261b732023-06-30 19:04:15 +0100759 return res;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100760 }
761 signal = gdb_signal_to_target(tmp);
762 } else if (cur_action != 'c' && cur_action != 's') {
763 /* unknown/invalid/unsupported command */
Alex Bennée2261b732023-06-30 19:04:15 +0100764 return -ENOTSUP;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100765 }
Luc Michele40e5202019-01-07 15:23:46 +0000766
Luc Michelc99ef792019-03-26 12:53:26 +0000767 if (*p == '\0' || *p == ';') {
768 /*
769 * No thread specifier, action is on "all threads". The
770 * specification is unclear regarding the process to act on. We
771 * choose all processes.
772 */
773 kind = GDB_ALL_PROCESSES;
774 } else if (*p++ == ':') {
775 kind = read_thread_id(p, &p, &pid, &tid);
776 } else {
Alex Bennée2261b732023-06-30 19:04:15 +0100777 return -ENOTSUP;
Luc Michele40e5202019-01-07 15:23:46 +0000778 }
779
Luc Michelc99ef792019-03-26 12:53:26 +0000780 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +0000781 case GDB_READ_THREAD_ERR:
Alex Bennée2261b732023-06-30 19:04:15 +0100782 return -EINVAL;
Luc Michele40e5202019-01-07 15:23:46 +0000783
784 case GDB_ALL_PROCESSES:
Alex Bennéea346af32020-03-16 17:21:34 +0000785 cpu = gdb_first_attached_cpu();
Luc Michele40e5202019-01-07 15:23:46 +0000786 while (cpu) {
787 if (newstates[cpu->cpu_index] == 1) {
788 newstates[cpu->cpu_index] = cur_action;
Matheus Branco Borella761e3c12023-08-29 17:15:24 +0100789
790 target_count++;
791 last_target = cpu;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100792 }
Luc Michele40e5202019-01-07 15:23:46 +0000793
Alex Bennéea346af32020-03-16 17:21:34 +0000794 cpu = gdb_next_attached_cpu(cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100795 }
Luc Michele40e5202019-01-07 15:23:46 +0000796 break;
797
798 case GDB_ALL_THREADS:
Alex Bennéea346af32020-03-16 17:21:34 +0000799 process = gdb_get_process(pid);
Luc Michele40e5202019-01-07 15:23:46 +0000800
801 if (!process->attached) {
Alex Bennée2261b732023-06-30 19:04:15 +0100802 return -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100803 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100804
Ilya Leoshkevicha3fcc112023-06-30 19:04:19 +0100805 cpu = gdb_get_first_cpu_in_process(process);
Luc Michele40e5202019-01-07 15:23:46 +0000806 while (cpu) {
807 if (newstates[cpu->cpu_index] == 1) {
808 newstates[cpu->cpu_index] = cur_action;
Matheus Branco Borella761e3c12023-08-29 17:15:24 +0100809
810 target_count++;
811 last_target = cpu;
Luc Michele40e5202019-01-07 15:23:46 +0000812 }
813
Alex Bennéea346af32020-03-16 17:21:34 +0000814 cpu = gdb_next_cpu_in_process(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000815 }
816 break;
817
818 case GDB_ONE_THREAD:
Alex Bennéea346af32020-03-16 17:21:34 +0000819 cpu = gdb_get_cpu(pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +0100820
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100821 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +0100822 if (!cpu) {
Alex Bennée2261b732023-06-30 19:04:15 +0100823 return -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100824 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +0100825
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100826 /* only use if no previous match occourred */
827 if (newstates[cpu->cpu_index] == 1) {
828 newstates[cpu->cpu_index] = cur_action;
Matheus Branco Borella761e3c12023-08-29 17:15:24 +0100829
830 target_count++;
831 last_target = cpu;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100832 }
Luc Michele40e5202019-01-07 15:23:46 +0000833 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100834 }
835 }
Alex Bennée2261b732023-06-30 19:04:15 +0100836
Matheus Branco Borella761e3c12023-08-29 17:15:24 +0100837 /*
838 * if we're about to resume a specific set of CPUs/threads, make it so that
839 * in case execution gets interrupted, we can send GDB a stop reply with a
840 * correct value. it doesn't really matter which CPU we tell GDB the signal
841 * happened in (VM pauses stop all of them anyway), so long as it is one of
842 * the ones we resumed/single stepped here.
843 */
844 if (target_count > 0) {
845 gdbserver_state.c_cpu = last_target;
846 }
847
Alex Bennéea346af32020-03-16 17:21:34 +0000848 gdbserver_state.signal = signal;
849 gdb_continue_partial(newstates);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100850 return res;
851}
852
Jon Dorond14055d2019-05-29 09:41:29 +0300853static const char *cmd_next_param(const char *param, const char delimiter)
854{
855 static const char all_delimiters[] = ",;:=";
856 char curr_delimiters[2] = {0};
857 const char *delimiters;
858
859 if (delimiter == '?') {
860 delimiters = all_delimiters;
861 } else if (delimiter == '0') {
862 return strchr(param, '\0');
863 } else if (delimiter == '.' && *param) {
864 return param + 1;
865 } else {
866 curr_delimiters[0] = delimiter;
867 delimiters = curr_delimiters;
868 }
869
870 param += strcspn(param, delimiters);
871 if (*param) {
872 param++;
873 }
874 return param;
875}
876
877static int cmd_parse_params(const char *data, const char *schema,
Alex Bennée26a16182021-05-25 09:24:14 +0100878 GArray *params)
Jon Dorond14055d2019-05-29 09:41:29 +0300879{
Jon Dorond14055d2019-05-29 09:41:29 +0300880 const char *curr_schema, *curr_data;
881
Alex Bennée26a16182021-05-25 09:24:14 +0100882 g_assert(schema);
883 g_assert(params->len == 0);
Jon Dorond14055d2019-05-29 09:41:29 +0300884
885 curr_schema = schema;
Jon Dorond14055d2019-05-29 09:41:29 +0300886 curr_data = data;
887 while (curr_schema[0] && curr_schema[1] && *curr_data) {
Alex Bennée26a16182021-05-25 09:24:14 +0100888 GdbCmdVariant this_param;
889
Jon Dorond14055d2019-05-29 09:41:29 +0300890 switch (curr_schema[0]) {
891 case 'l':
892 if (qemu_strtoul(curr_data, &curr_data, 16,
Alex Bennée26a16182021-05-25 09:24:14 +0100893 &this_param.val_ul)) {
Jon Dorond14055d2019-05-29 09:41:29 +0300894 return -EINVAL;
895 }
Jon Dorond14055d2019-05-29 09:41:29 +0300896 curr_data = cmd_next_param(curr_data, curr_schema[1]);
Alex Bennée26a16182021-05-25 09:24:14 +0100897 g_array_append_val(params, this_param);
Jon Dorond14055d2019-05-29 09:41:29 +0300898 break;
899 case 'L':
900 if (qemu_strtou64(curr_data, &curr_data, 16,
Alex Bennée26a16182021-05-25 09:24:14 +0100901 (uint64_t *)&this_param.val_ull)) {
Jon Dorond14055d2019-05-29 09:41:29 +0300902 return -EINVAL;
903 }
Jon Dorond14055d2019-05-29 09:41:29 +0300904 curr_data = cmd_next_param(curr_data, curr_schema[1]);
Alex Bennée26a16182021-05-25 09:24:14 +0100905 g_array_append_val(params, this_param);
Jon Dorond14055d2019-05-29 09:41:29 +0300906 break;
907 case 's':
Alex Bennée26a16182021-05-25 09:24:14 +0100908 this_param.data = curr_data;
Jon Dorond14055d2019-05-29 09:41:29 +0300909 curr_data = cmd_next_param(curr_data, curr_schema[1]);
Alex Bennée26a16182021-05-25 09:24:14 +0100910 g_array_append_val(params, this_param);
Jon Dorond14055d2019-05-29 09:41:29 +0300911 break;
912 case 'o':
Alex Bennée26a16182021-05-25 09:24:14 +0100913 this_param.opcode = *(uint8_t *)curr_data;
Jon Dorond14055d2019-05-29 09:41:29 +0300914 curr_data = cmd_next_param(curr_data, curr_schema[1]);
Alex Bennée26a16182021-05-25 09:24:14 +0100915 g_array_append_val(params, this_param);
Jon Dorond14055d2019-05-29 09:41:29 +0300916 break;
917 case 't':
Alex Bennée26a16182021-05-25 09:24:14 +0100918 this_param.thread_id.kind =
Jon Dorond14055d2019-05-29 09:41:29 +0300919 read_thread_id(curr_data, &curr_data,
Alex Bennée26a16182021-05-25 09:24:14 +0100920 &this_param.thread_id.pid,
921 &this_param.thread_id.tid);
Jon Dorond14055d2019-05-29 09:41:29 +0300922 curr_data = cmd_next_param(curr_data, curr_schema[1]);
Alex Bennée26a16182021-05-25 09:24:14 +0100923 g_array_append_val(params, this_param);
Jon Dorond14055d2019-05-29 09:41:29 +0300924 break;
925 case '?':
926 curr_data = cmd_next_param(curr_data, curr_schema[1]);
927 break;
928 default:
929 return -EINVAL;
930 }
931 curr_schema += 2;
932 }
933
Jon Dorond14055d2019-05-29 09:41:29 +0300934 return 0;
935}
936
Jon Dorond14055d2019-05-29 09:41:29 +0300937static inline int startswith(const char *string, const char *pattern)
938{
939 return !strncmp(string, pattern, strlen(pattern));
940}
941
Gustavo Romero0ef6b122024-07-05 09:40:37 +0100942static bool process_string_cmd(const char *data,
943 const GdbCmdParseEntry *cmds, int num_cmds)
Jon Dorond14055d2019-05-29 09:41:29 +0300944{
Alex Bennée26a16182021-05-25 09:24:14 +0100945 int i;
946 g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant));
Jon Dorond14055d2019-05-29 09:41:29 +0300947
948 if (!cmds) {
Gustavo Romero0ef6b122024-07-05 09:40:37 +0100949 return false;
Jon Dorond14055d2019-05-29 09:41:29 +0300950 }
951
952 for (i = 0; i < num_cmds; i++) {
953 const GdbCmdParseEntry *cmd = &cmds[i];
Gustavo Romero2be4d5d2024-07-05 09:40:44 +0100954 void *user_ctx = NULL;
Jon Dorond14055d2019-05-29 09:41:29 +0300955 g_assert(cmd->handler && cmd->cmd);
956
957 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
958 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
959 continue;
960 }
961
962 if (cmd->schema) {
Alex Bennée26a16182021-05-25 09:24:14 +0100963 if (cmd_parse_params(&data[strlen(cmd->cmd)],
964 cmd->schema, params)) {
Gustavo Romero0ef6b122024-07-05 09:40:37 +0100965 return false;
Jon Dorond14055d2019-05-29 09:41:29 +0300966 }
Jon Dorond14055d2019-05-29 09:41:29 +0300967 }
968
Gustavo Romero2be4d5d2024-07-05 09:40:44 +0100969 if (cmd->need_cpu_context) {
970 user_ctx = (void *)gdbserver_state.g_cpu;
971 }
972
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -0300973 gdbserver_state.allow_stop_reply = cmd->allow_stop_reply;
Gustavo Romero2be4d5d2024-07-05 09:40:44 +0100974 cmd->handler(params, user_ctx);
Gustavo Romero0ef6b122024-07-05 09:40:37 +0100975 return true;
Jon Dorond14055d2019-05-29 09:41:29 +0300976 }
977
Gustavo Romero0ef6b122024-07-05 09:40:37 +0100978 return false;
Jon Dorond14055d2019-05-29 09:41:29 +0300979}
980
Alex Bennéea346af32020-03-16 17:21:34 +0000981static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
Jon Doron3e2c1262019-05-29 09:41:30 +0300982{
983 if (!data) {
984 return;
985 }
986
Alex Bennée308f9e82020-03-16 17:21:35 +0000987 g_string_set_size(gdbserver_state.str_buf, 0);
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000988 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
Alex Bennée308f9e82020-03-16 17:21:35 +0000989
Jon Doron3e2c1262019-05-29 09:41:30 +0300990 /* In case there was an error during the command parsing we must
991 * send a NULL packet to indicate the command is not supported */
Gustavo Romero0ef6b122024-07-05 09:40:37 +0100992 if (!process_string_cmd(data, cmd, 1)) {
Alex Bennée36e067b2023-03-02 18:57:45 -0800993 gdb_put_packet("");
Jon Doron3e2c1262019-05-29 09:41:30 +0300994 }
995}
996
Alex Bennée26a16182021-05-25 09:24:14 +0100997static void handle_detach(GArray *params, void *user_ctx)
Jon Doron3e2c1262019-05-29 09:41:30 +0300998{
999 GDBProcess *process;
Jon Doron3e2c1262019-05-29 09:41:30 +03001000 uint32_t pid = 1;
1001
Alex Bennéea346af32020-03-16 17:21:34 +00001002 if (gdbserver_state.multiprocess) {
Alex Bennée26a16182021-05-25 09:24:14 +01001003 if (!params->len) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001004 gdb_put_packet("E22");
Jon Doron3e2c1262019-05-29 09:41:30 +03001005 return;
1006 }
1007
Gustavo Romero133f2022024-07-05 09:40:38 +01001008 pid = gdb_get_cmd_param(params, 0)->val_ul;
Jon Doron3e2c1262019-05-29 09:41:30 +03001009 }
1010
Ilya Leoshkevich539cb4e2024-03-05 12:09:47 +00001011#ifdef CONFIG_USER_ONLY
1012 if (gdb_handle_detach_user(pid)) {
1013 return;
1014 }
1015#endif
1016
Alex Bennéea346af32020-03-16 17:21:34 +00001017 process = gdb_get_process(pid);
1018 gdb_process_breakpoint_remove_all(process);
Jon Doron3e2c1262019-05-29 09:41:30 +03001019 process->attached = false;
1020
Alex Bennéea346af32020-03-16 17:21:34 +00001021 if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
1022 gdbserver_state.c_cpu = gdb_first_attached_cpu();
Jon Doron3e2c1262019-05-29 09:41:30 +03001023 }
1024
Alex Bennéea346af32020-03-16 17:21:34 +00001025 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
1026 gdbserver_state.g_cpu = gdb_first_attached_cpu();
Jon Doron3e2c1262019-05-29 09:41:30 +03001027 }
1028
Alex Bennéea346af32020-03-16 17:21:34 +00001029 if (!gdbserver_state.c_cpu) {
Jon Doron3e2c1262019-05-29 09:41:30 +03001030 /* No more process attached */
Alex Bennéec5660802023-03-02 18:57:57 -08001031 gdb_disable_syscalls();
Alex Bennéea346af32020-03-16 17:21:34 +00001032 gdb_continue();
Jon Doron3e2c1262019-05-29 09:41:30 +03001033 }
Alex Bennée36e067b2023-03-02 18:57:45 -08001034 gdb_put_packet("OK");
Jon Doron3e2c1262019-05-29 09:41:30 +03001035}
1036
Alex Bennée26a16182021-05-25 09:24:14 +01001037static void handle_thread_alive(GArray *params, void *user_ctx)
Jon Doron44ffded2019-05-29 09:41:31 +03001038{
1039 CPUState *cpu;
1040
Alex Bennée26a16182021-05-25 09:24:14 +01001041 if (!params->len) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001042 gdb_put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001043 return;
1044 }
1045
Gustavo Romero133f2022024-07-05 09:40:38 +01001046 if (gdb_get_cmd_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001047 gdb_put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001048 return;
1049 }
1050
Gustavo Romero133f2022024-07-05 09:40:38 +01001051 cpu = gdb_get_cpu(gdb_get_cmd_param(params, 0)->thread_id.pid,
1052 gdb_get_cmd_param(params, 0)->thread_id.tid);
Jon Doron44ffded2019-05-29 09:41:31 +03001053 if (!cpu) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001054 gdb_put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001055 return;
1056 }
1057
Alex Bennée36e067b2023-03-02 18:57:45 -08001058 gdb_put_packet("OK");
Jon Doron44ffded2019-05-29 09:41:31 +03001059}
1060
Alex Bennée26a16182021-05-25 09:24:14 +01001061static void handle_continue(GArray *params, void *user_ctx)
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001062{
Alex Bennée26a16182021-05-25 09:24:14 +01001063 if (params->len) {
Gustavo Romero133f2022024-07-05 09:40:38 +01001064 gdb_set_cpu_pc(gdb_get_cmd_param(params, 0)->val_ull);
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001065 }
1066
Alex Bennéea346af32020-03-16 17:21:34 +00001067 gdbserver_state.signal = 0;
1068 gdb_continue();
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001069}
1070
Alex Bennée26a16182021-05-25 09:24:14 +01001071static void handle_cont_with_sig(GArray *params, void *user_ctx)
Jon Doronccc47d52019-05-29 09:41:33 +03001072{
1073 unsigned long signal = 0;
1074
1075 /*
1076 * Note: C sig;[addr] is currently unsupported and we simply
1077 * omit the addr parameter
1078 */
Alex Bennée26a16182021-05-25 09:24:14 +01001079 if (params->len) {
Gustavo Romero133f2022024-07-05 09:40:38 +01001080 signal = gdb_get_cmd_param(params, 0)->val_ul;
Jon Doronccc47d52019-05-29 09:41:33 +03001081 }
1082
Alex Bennéea346af32020-03-16 17:21:34 +00001083 gdbserver_state.signal = gdb_signal_to_target(signal);
1084 if (gdbserver_state.signal == -1) {
1085 gdbserver_state.signal = 0;
Jon Doronccc47d52019-05-29 09:41:33 +03001086 }
Alex Bennéea346af32020-03-16 17:21:34 +00001087 gdb_continue();
Jon Doronccc47d52019-05-29 09:41:33 +03001088}
1089
Alex Bennée26a16182021-05-25 09:24:14 +01001090static void handle_set_thread(GArray *params, void *user_ctx)
Jon Doron3a9651d2019-05-29 09:41:34 +03001091{
Ilya Leoshkeviche454f2f2024-03-05 12:09:46 +00001092 uint32_t pid, tid;
Jon Doron3a9651d2019-05-29 09:41:34 +03001093 CPUState *cpu;
1094
Alex Bennée26a16182021-05-25 09:24:14 +01001095 if (params->len != 2) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001096 gdb_put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001097 return;
1098 }
1099
Gustavo Romero133f2022024-07-05 09:40:38 +01001100 if (gdb_get_cmd_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001101 gdb_put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001102 return;
1103 }
1104
Gustavo Romero133f2022024-07-05 09:40:38 +01001105 if (gdb_get_cmd_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001106 gdb_put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001107 return;
1108 }
1109
Gustavo Romero133f2022024-07-05 09:40:38 +01001110 pid = gdb_get_cmd_param(params, 1)->thread_id.pid;
1111 tid = gdb_get_cmd_param(params, 1)->thread_id.tid;
Ilya Leoshkeviche454f2f2024-03-05 12:09:46 +00001112#ifdef CONFIG_USER_ONLY
1113 if (gdb_handle_set_thread_user(pid, tid)) {
1114 return;
1115 }
1116#endif
1117 cpu = gdb_get_cpu(pid, tid);
Jon Doron3a9651d2019-05-29 09:41:34 +03001118 if (!cpu) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001119 gdb_put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001120 return;
1121 }
1122
1123 /*
1124 * Note: This command is deprecated and modern gdb's will be using the
1125 * vCont command instead.
1126 */
Gustavo Romero133f2022024-07-05 09:40:38 +01001127 switch (gdb_get_cmd_param(params, 0)->opcode) {
Jon Doron3a9651d2019-05-29 09:41:34 +03001128 case 'c':
Alex Bennéea346af32020-03-16 17:21:34 +00001129 gdbserver_state.c_cpu = cpu;
Alex Bennée36e067b2023-03-02 18:57:45 -08001130 gdb_put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001131 break;
1132 case 'g':
Alex Bennéea346af32020-03-16 17:21:34 +00001133 gdbserver_state.g_cpu = cpu;
Alex Bennée36e067b2023-03-02 18:57:45 -08001134 gdb_put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001135 break;
1136 default:
Alex Bennée36e067b2023-03-02 18:57:45 -08001137 gdb_put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001138 break;
1139 }
1140}
1141
Alex Bennée26a16182021-05-25 09:24:14 +01001142static void handle_insert_bp(GArray *params, void *user_ctx)
Jon Doron77f6ce52019-05-29 09:41:35 +03001143{
1144 int res;
1145
Alex Bennée26a16182021-05-25 09:24:14 +01001146 if (params->len != 3) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001147 gdb_put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001148 return;
1149 }
1150
Alex Bennéeae7467b2022-09-29 12:42:24 +01001151 res = gdb_breakpoint_insert(gdbserver_state.c_cpu,
Gustavo Romero133f2022024-07-05 09:40:38 +01001152 gdb_get_cmd_param(params, 0)->val_ul,
1153 gdb_get_cmd_param(params, 1)->val_ull,
1154 gdb_get_cmd_param(params, 2)->val_ull);
Jon Doron77f6ce52019-05-29 09:41:35 +03001155 if (res >= 0) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001156 gdb_put_packet("OK");
Jon Doron77f6ce52019-05-29 09:41:35 +03001157 return;
1158 } else if (res == -ENOSYS) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001159 gdb_put_packet("");
Jon Doron77f6ce52019-05-29 09:41:35 +03001160 return;
1161 }
1162
Alex Bennée36e067b2023-03-02 18:57:45 -08001163 gdb_put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001164}
1165
Alex Bennée26a16182021-05-25 09:24:14 +01001166static void handle_remove_bp(GArray *params, void *user_ctx)
Jon Doron77f6ce52019-05-29 09:41:35 +03001167{
1168 int res;
1169
Alex Bennée26a16182021-05-25 09:24:14 +01001170 if (params->len != 3) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001171 gdb_put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001172 return;
1173 }
1174
Alex Bennéeae7467b2022-09-29 12:42:24 +01001175 res = gdb_breakpoint_remove(gdbserver_state.c_cpu,
Gustavo Romero133f2022024-07-05 09:40:38 +01001176 gdb_get_cmd_param(params, 0)->val_ul,
1177 gdb_get_cmd_param(params, 1)->val_ull,
1178 gdb_get_cmd_param(params, 2)->val_ull);
Jon Doron77f6ce52019-05-29 09:41:35 +03001179 if (res >= 0) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001180 gdb_put_packet("OK");
Jon Doron77f6ce52019-05-29 09:41:35 +03001181 return;
1182 } else if (res == -ENOSYS) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001183 gdb_put_packet("");
Jon Doron77f6ce52019-05-29 09:41:35 +03001184 return;
1185 }
1186
Alex Bennée36e067b2023-03-02 18:57:45 -08001187 gdb_put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001188}
1189
Alex Bennée94b2a622019-07-05 14:23:07 +01001190/*
1191 * handle_set/get_reg
1192 *
1193 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1194 * This works, but can be very slow. Anything new enough to understand
1195 * XML also knows how to use this properly. However to use this we
1196 * need to define a local XML file as well as be talking to a
1197 * reasonably modern gdb. Responding with an empty packet will cause
1198 * the remote gdb to fallback to older methods.
1199 */
1200
Alex Bennée26a16182021-05-25 09:24:14 +01001201static void handle_set_reg(GArray *params, void *user_ctx)
Jon Doron62b33202019-05-29 09:41:36 +03001202{
1203 int reg_size;
1204
Alex Bennée26a16182021-05-25 09:24:14 +01001205 if (params->len != 2) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001206 gdb_put_packet("E22");
Jon Doron62b33202019-05-29 09:41:36 +03001207 return;
1208 }
1209
Gustavo Romero133f2022024-07-05 09:40:38 +01001210 reg_size = strlen(gdb_get_cmd_param(params, 1)->data) / 2;
1211 gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 1)->data, reg_size);
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001212 gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
Gustavo Romero133f2022024-07-05 09:40:38 +01001213 gdb_get_cmd_param(params, 0)->val_ull);
Alex Bennée36e067b2023-03-02 18:57:45 -08001214 gdb_put_packet("OK");
Jon Doron62b33202019-05-29 09:41:36 +03001215}
1216
Alex Bennée26a16182021-05-25 09:24:14 +01001217static void handle_get_reg(GArray *params, void *user_ctx)
Jon Doron5d0e57b2019-05-29 09:41:37 +03001218{
1219 int reg_size;
1220
Alex Bennée26a16182021-05-25 09:24:14 +01001221 if (!params->len) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001222 gdb_put_packet("E14");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001223 return;
1224 }
1225
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001226 reg_size = gdb_read_register(gdbserver_state.g_cpu,
Alex Bennéea010bdb2020-03-16 17:21:41 +00001227 gdbserver_state.mem_buf,
Gustavo Romero133f2022024-07-05 09:40:38 +01001228 gdb_get_cmd_param(params, 0)->val_ull);
Jon Doron5d0e57b2019-05-29 09:41:37 +03001229 if (!reg_size) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001230 gdb_put_packet("E14");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001231 return;
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001232 } else {
1233 g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
Jon Doron5d0e57b2019-05-29 09:41:37 +03001234 }
1235
Alex Bennée36e067b2023-03-02 18:57:45 -08001236 gdb_memtohex(gdbserver_state.str_buf,
1237 gdbserver_state.mem_buf->data, reg_size);
1238 gdb_put_strbuf();
Jon Doron5d0e57b2019-05-29 09:41:37 +03001239}
1240
Alex Bennée26a16182021-05-25 09:24:14 +01001241static void handle_write_mem(GArray *params, void *user_ctx)
Jon Doroncc0ecc72019-05-29 09:41:38 +03001242{
Alex Bennée26a16182021-05-25 09:24:14 +01001243 if (params->len != 3) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001244 gdb_put_packet("E22");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001245 return;
1246 }
1247
Alex Bennée36e067b2023-03-02 18:57:45 -08001248 /* gdb_hextomem() reads 2*len bytes */
Gustavo Romero133f2022024-07-05 09:40:38 +01001249 if (gdb_get_cmd_param(params, 1)->val_ull >
1250 strlen(gdb_get_cmd_param(params, 2)->data) / 2) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001251 gdb_put_packet("E22");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001252 return;
1253 }
1254
Gustavo Romero133f2022024-07-05 09:40:38 +01001255 gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 2)->data,
1256 gdb_get_cmd_param(params, 1)->val_ull);
Alex Bennée589a5862023-03-02 18:57:51 -08001257 if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
Gustavo Romero133f2022024-07-05 09:40:38 +01001258 gdb_get_cmd_param(params, 0)->val_ull,
Alex Bennée589a5862023-03-02 18:57:51 -08001259 gdbserver_state.mem_buf->data,
1260 gdbserver_state.mem_buf->len, true)) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001261 gdb_put_packet("E14");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001262 return;
1263 }
1264
Alex Bennée36e067b2023-03-02 18:57:45 -08001265 gdb_put_packet("OK");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001266}
1267
Alex Bennée26a16182021-05-25 09:24:14 +01001268static void handle_read_mem(GArray *params, void *user_ctx)
Jon Doronda92e232019-05-29 09:41:39 +03001269{
Alex Bennée26a16182021-05-25 09:24:14 +01001270 if (params->len != 2) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001271 gdb_put_packet("E22");
Jon Doronda92e232019-05-29 09:41:39 +03001272 return;
1273 }
1274
Alex Bennée36e067b2023-03-02 18:57:45 -08001275 /* gdb_memtohex() doubles the required space */
Gustavo Romero133f2022024-07-05 09:40:38 +01001276 if (gdb_get_cmd_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001277 gdb_put_packet("E22");
Jon Doronda92e232019-05-29 09:41:39 +03001278 return;
1279 }
1280
Alex Bennée26a16182021-05-25 09:24:14 +01001281 g_byte_array_set_size(gdbserver_state.mem_buf,
Gustavo Romero133f2022024-07-05 09:40:38 +01001282 gdb_get_cmd_param(params, 1)->val_ull);
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001283
Alex Bennée589a5862023-03-02 18:57:51 -08001284 if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
Gustavo Romero133f2022024-07-05 09:40:38 +01001285 gdb_get_cmd_param(params, 0)->val_ull,
Alex Bennée589a5862023-03-02 18:57:51 -08001286 gdbserver_state.mem_buf->data,
1287 gdbserver_state.mem_buf->len, false)) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001288 gdb_put_packet("E14");
Jon Doronda92e232019-05-29 09:41:39 +03001289 return;
1290 }
1291
Alex Bennée36e067b2023-03-02 18:57:45 -08001292 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001293 gdbserver_state.mem_buf->len);
Alex Bennée36e067b2023-03-02 18:57:45 -08001294 gdb_put_strbuf();
Jon Doronda92e232019-05-29 09:41:39 +03001295}
1296
Alex Bennée26a16182021-05-25 09:24:14 +01001297static void handle_write_all_regs(GArray *params, void *user_ctx)
Jon Doron287ca122019-05-29 09:41:40 +03001298{
Alex Bennée379b42e2023-03-02 18:57:55 -08001299 int reg_id;
1300 size_t len;
Jon Doron287ca122019-05-29 09:41:40 +03001301 uint8_t *registers;
1302 int reg_size;
1303
Alex Bennée26a16182021-05-25 09:24:14 +01001304 if (!params->len) {
Jon Doron287ca122019-05-29 09:41:40 +03001305 return;
1306 }
1307
Alex Bennéea346af32020-03-16 17:21:34 +00001308 cpu_synchronize_state(gdbserver_state.g_cpu);
Gustavo Romero133f2022024-07-05 09:40:38 +01001309 len = strlen(gdb_get_cmd_param(params, 0)->data) / 2;
1310 gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 0)->data, len);
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001311 registers = gdbserver_state.mem_buf->data;
Alex Bennée379b42e2023-03-02 18:57:55 -08001312 for (reg_id = 0;
1313 reg_id < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
1314 reg_id++) {
1315 reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, reg_id);
Jon Doron287ca122019-05-29 09:41:40 +03001316 len -= reg_size;
1317 registers += reg_size;
1318 }
Alex Bennée36e067b2023-03-02 18:57:45 -08001319 gdb_put_packet("OK");
Jon Doron287ca122019-05-29 09:41:40 +03001320}
1321
Alex Bennée26a16182021-05-25 09:24:14 +01001322static void handle_read_all_regs(GArray *params, void *user_ctx)
Jon Doron397d1372019-05-29 09:41:41 +03001323{
Alex Bennée379b42e2023-03-02 18:57:55 -08001324 int reg_id;
1325 size_t len;
Jon Doron397d1372019-05-29 09:41:41 +03001326
Alex Bennéea346af32020-03-16 17:21:34 +00001327 cpu_synchronize_state(gdbserver_state.g_cpu);
Alex Bennéea010bdb2020-03-16 17:21:41 +00001328 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
Jon Doron397d1372019-05-29 09:41:41 +03001329 len = 0;
Alex Bennée379b42e2023-03-02 18:57:55 -08001330 for (reg_id = 0; reg_id < gdbserver_state.g_cpu->gdb_num_g_regs; reg_id++) {
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001331 len += gdb_read_register(gdbserver_state.g_cpu,
Alex Bennéea010bdb2020-03-16 17:21:41 +00001332 gdbserver_state.mem_buf,
Alex Bennée379b42e2023-03-02 18:57:55 -08001333 reg_id);
Jon Doron397d1372019-05-29 09:41:41 +03001334 }
Alex Bennéea010bdb2020-03-16 17:21:41 +00001335 g_assert(len == gdbserver_state.mem_buf->len);
Jon Doron397d1372019-05-29 09:41:41 +03001336
Alex Bennée36e067b2023-03-02 18:57:45 -08001337 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
1338 gdb_put_strbuf();
Jon Doron397d1372019-05-29 09:41:41 +03001339}
1340
Jon Doron4b20fab2019-05-29 09:41:42 +03001341
Alex Bennée26a16182021-05-25 09:24:14 +01001342static void handle_step(GArray *params, void *user_ctx)
Jon Doron933f80d2019-05-29 09:41:43 +03001343{
Alex Bennée26a16182021-05-25 09:24:14 +01001344 if (params->len) {
Gustavo Romero133f2022024-07-05 09:40:38 +01001345 gdb_set_cpu_pc(gdb_get_cmd_param(params, 0)->val_ull);
Jon Doron933f80d2019-05-29 09:41:43 +03001346 }
1347
Maxim Levitskyecd39d62021-11-11 12:06:02 +01001348 cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
Alex Bennéea346af32020-03-16 17:21:34 +00001349 gdb_continue();
Jon Doron933f80d2019-05-29 09:41:43 +03001350}
1351
Alex Bennée26a16182021-05-25 09:24:14 +01001352static void handle_backward(GArray *params, void *user_ctx)
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001353{
Alex Bennée505601d2023-03-02 18:57:53 -08001354 if (!gdb_can_reverse()) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001355 gdb_put_packet("E22");
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001356 }
Alex Bennée26a16182021-05-25 09:24:14 +01001357 if (params->len == 1) {
Gustavo Romero133f2022024-07-05 09:40:38 +01001358 switch (gdb_get_cmd_param(params, 0)->opcode) {
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001359 case 's':
1360 if (replay_reverse_step()) {
1361 gdb_continue();
1362 } else {
Alex Bennée36e067b2023-03-02 18:57:45 -08001363 gdb_put_packet("E14");
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001364 }
1365 return;
Pavel Dovgalyukcda38252020-10-03 20:13:49 +03001366 case 'c':
1367 if (replay_reverse_continue()) {
1368 gdb_continue();
1369 } else {
Alex Bennée36e067b2023-03-02 18:57:45 -08001370 gdb_put_packet("E14");
Pavel Dovgalyukcda38252020-10-03 20:13:49 +03001371 }
1372 return;
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001373 }
1374 }
1375
1376 /* Default invalid command */
Alex Bennée36e067b2023-03-02 18:57:45 -08001377 gdb_put_packet("");
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001378}
1379
Alex Bennée26a16182021-05-25 09:24:14 +01001380static void handle_v_cont_query(GArray *params, void *user_ctx)
Jon Doron8536ec02019-05-29 09:41:44 +03001381{
Alex Bennée36e067b2023-03-02 18:57:45 -08001382 gdb_put_packet("vCont;c;C;s;S");
Jon Doron8536ec02019-05-29 09:41:44 +03001383}
1384
Alex Bennée26a16182021-05-25 09:24:14 +01001385static void handle_v_cont(GArray *params, void *user_ctx)
Jon Doron8536ec02019-05-29 09:41:44 +03001386{
1387 int res;
1388
Alex Bennée26a16182021-05-25 09:24:14 +01001389 if (!params->len) {
Jon Doron8536ec02019-05-29 09:41:44 +03001390 return;
1391 }
1392
Gustavo Romero133f2022024-07-05 09:40:38 +01001393 res = gdb_handle_vcont(gdb_get_cmd_param(params, 0)->data);
Jon Doron8536ec02019-05-29 09:41:44 +03001394 if ((res == -EINVAL) || (res == -ERANGE)) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001395 gdb_put_packet("E22");
Jon Doron8536ec02019-05-29 09:41:44 +03001396 } else if (res) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001397 gdb_put_packet("");
Jon Doron8536ec02019-05-29 09:41:44 +03001398 }
1399}
1400
Alex Bennée26a16182021-05-25 09:24:14 +01001401static void handle_v_attach(GArray *params, void *user_ctx)
Jon Doron8536ec02019-05-29 09:41:44 +03001402{
1403 GDBProcess *process;
1404 CPUState *cpu;
Jon Doron8536ec02019-05-29 09:41:44 +03001405
Alex Bennée308f9e82020-03-16 17:21:35 +00001406 g_string_assign(gdbserver_state.str_buf, "E22");
Alex Bennée26a16182021-05-25 09:24:14 +01001407 if (!params->len) {
Jon Doron8536ec02019-05-29 09:41:44 +03001408 goto cleanup;
1409 }
1410
Gustavo Romero133f2022024-07-05 09:40:38 +01001411 process = gdb_get_process(gdb_get_cmd_param(params, 0)->val_ul);
Jon Doron8536ec02019-05-29 09:41:44 +03001412 if (!process) {
1413 goto cleanup;
1414 }
1415
Ilya Leoshkevicha3fcc112023-06-30 19:04:19 +01001416 cpu = gdb_get_first_cpu_in_process(process);
Jon Doron8536ec02019-05-29 09:41:44 +03001417 if (!cpu) {
1418 goto cleanup;
1419 }
1420
1421 process->attached = true;
Alex Bennéea346af32020-03-16 17:21:34 +00001422 gdbserver_state.g_cpu = cpu;
1423 gdbserver_state.c_cpu = cpu;
Jon Doron8536ec02019-05-29 09:41:44 +03001424
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -03001425 if (gdbserver_state.allow_stop_reply) {
1426 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1427 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1428 g_string_append_c(gdbserver_state.str_buf, ';');
1429 gdbserver_state.allow_stop_reply = false;
Jon Doron8536ec02019-05-29 09:41:44 +03001430cleanup:
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -03001431 gdb_put_strbuf();
1432 }
Jon Doron8536ec02019-05-29 09:41:44 +03001433}
1434
Alex Bennée26a16182021-05-25 09:24:14 +01001435static void handle_v_kill(GArray *params, void *user_ctx)
Jon Doron8536ec02019-05-29 09:41:44 +03001436{
1437 /* Kill the target */
Alex Bennée36e067b2023-03-02 18:57:45 -08001438 gdb_put_packet("OK");
Jon Doron8536ec02019-05-29 09:41:44 +03001439 error_report("QEMU: Terminated via GDBstub");
Alex Bennéeb9e10c62021-01-08 22:42:45 +00001440 gdb_exit(0);
Clément Chigote2162562023-10-03 09:14:27 +02001441 gdb_qemu_exit(0);
Jon Doron8536ec02019-05-29 09:41:44 +03001442}
1443
Philippe Mathieu-Daudé305bea02021-05-20 18:42:59 +01001444static const GdbCmdParseEntry gdb_v_commands_table[] = {
Jon Doron8536ec02019-05-29 09:41:44 +03001445 /* Order is important if has same prefix */
1446 {
1447 .handler = handle_v_cont_query,
1448 .cmd = "Cont?",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001449 .cmd_startswith = true
Jon Doron8536ec02019-05-29 09:41:44 +03001450 },
1451 {
1452 .handler = handle_v_cont,
1453 .cmd = "Cont",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001454 .cmd_startswith = true,
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -03001455 .allow_stop_reply = true,
Jon Doron8536ec02019-05-29 09:41:44 +03001456 .schema = "s0"
1457 },
1458 {
1459 .handler = handle_v_attach,
1460 .cmd = "Attach;",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001461 .cmd_startswith = true,
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -03001462 .allow_stop_reply = true,
Jon Doron8536ec02019-05-29 09:41:44 +03001463 .schema = "l0"
1464 },
1465 {
1466 .handler = handle_v_kill,
1467 .cmd = "Kill;",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001468 .cmd_startswith = true
Jon Doron8536ec02019-05-29 09:41:44 +03001469 },
Ilya Leoshkeviche2820102023-06-30 19:04:21 +01001470#ifdef CONFIG_USER_ONLY
1471 /*
1472 * Host I/O Packets. See [1] for details.
1473 * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html
1474 */
1475 {
1476 .handler = gdb_handle_v_file_open,
1477 .cmd = "File:open:",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001478 .cmd_startswith = true,
Ilya Leoshkeviche2820102023-06-30 19:04:21 +01001479 .schema = "s,L,L0"
1480 },
1481 {
1482 .handler = gdb_handle_v_file_close,
1483 .cmd = "File:close:",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001484 .cmd_startswith = true,
Ilya Leoshkeviche2820102023-06-30 19:04:21 +01001485 .schema = "l0"
1486 },
1487 {
1488 .handler = gdb_handle_v_file_pread,
1489 .cmd = "File:pread:",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001490 .cmd_startswith = true,
Ilya Leoshkeviche2820102023-06-30 19:04:21 +01001491 .schema = "l,L,L0"
1492 },
1493 {
1494 .handler = gdb_handle_v_file_readlink,
1495 .cmd = "File:readlink:",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001496 .cmd_startswith = true,
Ilya Leoshkeviche2820102023-06-30 19:04:21 +01001497 .schema = "s0"
1498 },
1499#endif
Jon Doron8536ec02019-05-29 09:41:44 +03001500};
1501
Alex Bennée26a16182021-05-25 09:24:14 +01001502static void handle_v_commands(GArray *params, void *user_ctx)
Jon Doron8536ec02019-05-29 09:41:44 +03001503{
Alex Bennée26a16182021-05-25 09:24:14 +01001504 if (!params->len) {
Jon Doron8536ec02019-05-29 09:41:44 +03001505 return;
1506 }
1507
Gustavo Romero133f2022024-07-05 09:40:38 +01001508 if (!process_string_cmd(gdb_get_cmd_param(params, 0)->data,
Gustavo Romero0ef6b122024-07-05 09:40:37 +01001509 gdb_v_commands_table,
1510 ARRAY_SIZE(gdb_v_commands_table))) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001511 gdb_put_packet("");
Jon Doron8536ec02019-05-29 09:41:44 +03001512 }
1513}
1514
Alex Bennée26a16182021-05-25 09:24:14 +01001515static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03001516{
Maxim Levitskyecd39d62021-11-11 12:06:02 +01001517 g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE);
1518
1519 if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) {
1520 g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x",
1521 SSTEP_NOIRQ);
1522 }
1523
1524 if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) {
1525 g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x",
1526 SSTEP_NOTIMER);
1527 }
1528
Alex Bennée36e067b2023-03-02 18:57:45 -08001529 gdb_put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03001530}
1531
Alex Bennée26a16182021-05-25 09:24:14 +01001532static void handle_set_qemu_sstep(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03001533{
Maxim Levitskyecd39d62021-11-11 12:06:02 +01001534 int new_sstep_flags;
1535
Alex Bennée26a16182021-05-25 09:24:14 +01001536 if (!params->len) {
Jon Doron2704efa2019-05-29 09:41:45 +03001537 return;
1538 }
1539
Gustavo Romero133f2022024-07-05 09:40:38 +01001540 new_sstep_flags = gdb_get_cmd_param(params, 0)->val_ul;
Maxim Levitskyecd39d62021-11-11 12:06:02 +01001541
1542 if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001543 gdb_put_packet("E22");
Maxim Levitskyecd39d62021-11-11 12:06:02 +01001544 return;
1545 }
1546
1547 gdbserver_state.sstep_flags = new_sstep_flags;
Alex Bennée36e067b2023-03-02 18:57:45 -08001548 gdb_put_packet("OK");
Jon Doron2704efa2019-05-29 09:41:45 +03001549}
1550
Alex Bennée26a16182021-05-25 09:24:14 +01001551static void handle_query_qemu_sstep(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03001552{
Maxim Levitskyecd39d62021-11-11 12:06:02 +01001553 g_string_printf(gdbserver_state.str_buf, "0x%x",
1554 gdbserver_state.sstep_flags);
Alex Bennée36e067b2023-03-02 18:57:45 -08001555 gdb_put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03001556}
1557
Alex Bennée26a16182021-05-25 09:24:14 +01001558static void handle_query_curr_tid(GArray *params, void *user_ctx)
bellardb4608c02003-06-27 17:34:32 +00001559{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001560 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001561 GDBProcess *process;
Jon Doron2704efa2019-05-29 09:41:45 +03001562
1563 /*
1564 * "Current thread" remains vague in the spec, so always return
1565 * the first thread of the current process (gdb returns the
1566 * first thread).
1567 */
Alex Bennéea346af32020-03-16 17:21:34 +00001568 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
Ilya Leoshkevicha3fcc112023-06-30 19:04:19 +01001569 cpu = gdb_get_first_cpu_in_process(process);
Alex Bennée308f9e82020-03-16 17:21:35 +00001570 g_string_assign(gdbserver_state.str_buf, "QC");
1571 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
Alex Bennée36e067b2023-03-02 18:57:45 -08001572 gdb_put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03001573}
1574
Alex Bennée26a16182021-05-25 09:24:14 +01001575static void handle_query_threads(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03001576{
Alex Bennéea346af32020-03-16 17:21:34 +00001577 if (!gdbserver_state.query_cpu) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001578 gdb_put_packet("l");
Jon Doron2704efa2019-05-29 09:41:45 +03001579 return;
1580 }
1581
Alex Bennée308f9e82020-03-16 17:21:35 +00001582 g_string_assign(gdbserver_state.str_buf, "m");
1583 gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
Alex Bennée36e067b2023-03-02 18:57:45 -08001584 gdb_put_strbuf();
Alex Bennéea346af32020-03-16 17:21:34 +00001585 gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
Jon Doron2704efa2019-05-29 09:41:45 +03001586}
1587
Alex Bennée26a16182021-05-25 09:24:14 +01001588static void handle_query_first_threads(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03001589{
Alex Bennéea346af32020-03-16 17:21:34 +00001590 gdbserver_state.query_cpu = gdb_first_attached_cpu();
Alex Bennée26a16182021-05-25 09:24:14 +01001591 handle_query_threads(params, user_ctx);
Jon Doron2704efa2019-05-29 09:41:45 +03001592}
1593
Alex Bennée26a16182021-05-25 09:24:14 +01001594static void handle_query_thread_extra(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03001595{
Alex Bennée308f9e82020-03-16 17:21:35 +00001596 g_autoptr(GString) rs = g_string_new(NULL);
Jon Doron2704efa2019-05-29 09:41:45 +03001597 CPUState *cpu;
Jon Doron2704efa2019-05-29 09:41:45 +03001598
Alex Bennée26a16182021-05-25 09:24:14 +01001599 if (!params->len ||
Gustavo Romero133f2022024-07-05 09:40:38 +01001600 gdb_get_cmd_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001601 gdb_put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03001602 return;
1603 }
1604
Gustavo Romero133f2022024-07-05 09:40:38 +01001605 cpu = gdb_get_cpu(gdb_get_cmd_param(params, 0)->thread_id.pid,
1606 gdb_get_cmd_param(params, 0)->thread_id.tid);
Jon Doron2704efa2019-05-29 09:41:45 +03001607 if (!cpu) {
1608 return;
1609 }
1610
1611 cpu_synchronize_state(cpu);
1612
Alex Bennéea346af32020-03-16 17:21:34 +00001613 if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
Jon Doron2704efa2019-05-29 09:41:45 +03001614 /* Print the CPU model and name in multiprocess mode */
1615 ObjectClass *oc = object_get_class(OBJECT(cpu));
1616 const char *cpu_model = object_class_get_name(oc);
Markus Armbruster7a309cc2020-07-14 18:02:00 +02001617 const char *cpu_name =
Denis Plotnikov076b2fa2020-04-03 20:11:44 +01001618 object_get_canonical_path_component(OBJECT(cpu));
Alex Bennée308f9e82020-03-16 17:21:35 +00001619 g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
1620 cpu->halted ? "halted " : "running");
Jon Doron2704efa2019-05-29 09:41:45 +03001621 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00001622 g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
Jon Doron2704efa2019-05-29 09:41:45 +03001623 cpu->halted ? "halted " : "running");
1624 }
Alex Bennée308f9e82020-03-16 17:21:35 +00001625 trace_gdbstub_op_extra_info(rs->str);
Alex Bennée36e067b2023-03-02 18:57:45 -08001626 gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
1627 gdb_put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03001628}
1629
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001630
Alex Bennéee8122a72024-07-18 10:45:11 +01001631static char **extra_query_flags;
1632
1633void gdb_extend_qsupported_features(char *qflags)
1634{
1635 if (!extra_query_flags) {
1636 extra_query_flags = g_new0(char *, 2);
1637 extra_query_flags[0] = g_strdup(qflags);
1638 } else if (!g_strv_contains((const gchar * const *) extra_query_flags,
1639 qflags)) {
1640 int len = g_strv_length(extra_query_flags);
1641 extra_query_flags = g_realloc_n(extra_query_flags, len + 2,
1642 sizeof(char *));
1643 extra_query_flags[len] = g_strdup(qflags);
1644 }
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001645}
1646
Alex Bennée26a16182021-05-25 09:24:14 +01001647static void handle_query_supported(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03001648{
Andreas Färber5b24c642013-07-07 15:08:22 +02001649 CPUClass *cc;
Jon Doron2704efa2019-05-29 09:41:45 +03001650
Alex Bennée308f9e82020-03-16 17:21:35 +00001651 g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
Jon Doron2704efa2019-05-29 09:41:45 +03001652 cc = CPU_GET_CLASS(first_cpu);
1653 if (cc->gdb_core_xml_file) {
Alex Bennée308f9e82020-03-16 17:21:35 +00001654 g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
Jon Doron2704efa2019-05-29 09:41:45 +03001655 }
1656
Alex Bennée505601d2023-03-02 18:57:53 -08001657 if (gdb_can_reverse()) {
Pavel Dovgalyukcda38252020-10-03 20:13:49 +03001658 g_string_append(gdbserver_state.str_buf,
1659 ";ReverseStep+;ReverseContinue+");
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001660 }
1661
Ilya Leoshkeviche2820102023-06-30 19:04:21 +01001662#if defined(CONFIG_USER_ONLY)
1663#if defined(CONFIG_LINUX)
Philippe Mathieu-Daudé59272462024-04-25 11:12:19 +02001664 if (get_task_state(gdbserver_state.c_cpu)) {
Lirong Yuan51c623b2021-01-08 22:42:42 +00001665 g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
1666 }
Ilya Leoshkevich046f1432024-02-07 16:38:11 +00001667 g_string_append(gdbserver_state.str_buf, ";QCatchSyscalls+");
Gustavo Romero9ae58012024-03-09 03:09:00 +00001668
1669 g_string_append(gdbserver_state.str_buf, ";qXfer:siginfo:read+");
Lirong Yuan51c623b2021-01-08 22:42:42 +00001670#endif
Ilya Leoshkeviche2820102023-06-30 19:04:21 +01001671 g_string_append(gdbserver_state.str_buf, ";qXfer:exec-file:read+");
1672#endif
Lirong Yuan51c623b2021-01-08 22:42:42 +00001673
Ilya Leoshkevich6d923112024-03-05 12:09:45 +00001674 if (params->len) {
Gustavo Romero133f2022024-07-05 09:40:38 +01001675 const char *gdb_supported = gdb_get_cmd_param(params, 0)->data;
Ilya Leoshkevich6d923112024-03-05 12:09:45 +00001676
1677 if (strstr(gdb_supported, "multiprocess+")) {
1678 gdbserver_state.multiprocess = true;
1679 }
1680#if defined(CONFIG_USER_ONLY)
1681 gdb_handle_query_supported_user(gdb_supported);
1682#endif
Jon Doron2704efa2019-05-29 09:41:45 +03001683 }
1684
Changbin Du3bc26092020-03-16 17:21:55 +00001685 g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001686
Alex Bennéee8122a72024-07-18 10:45:11 +01001687 if (extra_query_flags) {
1688 int extras = g_strv_length(extra_query_flags);
1689 for (int i = 0; i < extras; i++) {
1690 g_string_append(gdbserver_state.str_buf, extra_query_flags[i]);
1691 }
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001692 }
1693
Alex Bennée36e067b2023-03-02 18:57:45 -08001694 gdb_put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03001695}
1696
Alex Bennée26a16182021-05-25 09:24:14 +01001697static void handle_query_xfer_features(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03001698{
1699 GDBProcess *process;
1700 CPUClass *cc;
1701 unsigned long len, total_len, addr;
1702 const char *xml;
bellardb4608c02003-06-27 17:34:32 +00001703 const char *p;
Jon Doron2704efa2019-05-29 09:41:45 +03001704
Alex Bennée26a16182021-05-25 09:24:14 +01001705 if (params->len < 3) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001706 gdb_put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03001707 return;
1708 }
1709
Alex Bennéea346af32020-03-16 17:21:34 +00001710 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
1711 cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
Jon Doron2704efa2019-05-29 09:41:45 +03001712 if (!cc->gdb_core_xml_file) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001713 gdb_put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03001714 return;
1715 }
1716
Gustavo Romero133f2022024-07-05 09:40:38 +01001717 p = gdb_get_cmd_param(params, 0)->data;
Alex Bennéea346af32020-03-16 17:21:34 +00001718 xml = get_feature_xml(p, &p, process);
Jon Doron2704efa2019-05-29 09:41:45 +03001719 if (!xml) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001720 gdb_put_packet("E00");
Jon Doron2704efa2019-05-29 09:41:45 +03001721 return;
1722 }
1723
Gustavo Romero133f2022024-07-05 09:40:38 +01001724 addr = gdb_get_cmd_param(params, 1)->val_ul;
1725 len = gdb_get_cmd_param(params, 2)->val_ul;
Jon Doron2704efa2019-05-29 09:41:45 +03001726 total_len = strlen(xml);
1727 if (addr > total_len) {
Alex Bennée36e067b2023-03-02 18:57:45 -08001728 gdb_put_packet("E00");
Jon Doron2704efa2019-05-29 09:41:45 +03001729 return;
1730 }
1731
1732 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
1733 len = (MAX_PACKET_LENGTH - 5) / 2;
1734 }
1735
1736 if (len < total_len - addr) {
Alex Bennée308f9e82020-03-16 17:21:35 +00001737 g_string_assign(gdbserver_state.str_buf, "m");
Alex Bennée36e067b2023-03-02 18:57:45 -08001738 gdb_memtox(gdbserver_state.str_buf, xml + addr, len);
Jon Doron2704efa2019-05-29 09:41:45 +03001739 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00001740 g_string_assign(gdbserver_state.str_buf, "l");
Alex Bennée36e067b2023-03-02 18:57:45 -08001741 gdb_memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
Jon Doron2704efa2019-05-29 09:41:45 +03001742 }
1743
Alex Bennée36e067b2023-03-02 18:57:45 -08001744 gdb_put_packet_binary(gdbserver_state.str_buf->str,
Alex Bennée308f9e82020-03-16 17:21:35 +00001745 gdbserver_state.str_buf->len, true);
Jon Doron2704efa2019-05-29 09:41:45 +03001746}
1747
Alex Bennée26a16182021-05-25 09:24:14 +01001748static void handle_query_qemu_supported(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03001749{
Alex Bennée308f9e82020-03-16 17:21:35 +00001750 g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
Jon Doronab4752e2019-05-29 09:41:48 +03001751#ifndef CONFIG_USER_ONLY
Alex Bennée308f9e82020-03-16 17:21:35 +00001752 g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
Jon Doronab4752e2019-05-29 09:41:48 +03001753#endif
Alex Bennée36e067b2023-03-02 18:57:45 -08001754 gdb_put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03001755}
1756
Philippe Mathieu-Daudé305bea02021-05-20 18:42:59 +01001757static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
Jon Doron2704efa2019-05-29 09:41:45 +03001758 /* Order is important if has same prefix */
1759 {
1760 .handler = handle_query_qemu_sstepbits,
1761 .cmd = "qemu.sstepbits",
1762 },
1763 {
1764 .handler = handle_query_qemu_sstep,
1765 .cmd = "qemu.sstep",
1766 },
1767 {
1768 .handler = handle_set_qemu_sstep,
1769 .cmd = "qemu.sstep=",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001770 .cmd_startswith = true,
Jon Doron2704efa2019-05-29 09:41:45 +03001771 .schema = "l0"
1772 },
1773};
1774
Alex Bennéee8122a72024-07-18 10:45:11 +01001775/**
1776 * extend_table() - extend one of the command tables
1777 * @table: the command table to extend (or NULL)
1778 * @extensions: a list of GdbCmdParseEntry pointers
1779 *
1780 * The entries themselves should be pointers to static const
1781 * GdbCmdParseEntry entries. If the entry is already in the table we
1782 * skip adding it again.
1783 *
1784 * Returns (a potentially freshly allocated) GPtrArray of GdbCmdParseEntry
1785 */
1786static GPtrArray *extend_table(GPtrArray *table, GPtrArray *extensions)
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001787{
Alex Bennéee8122a72024-07-18 10:45:11 +01001788 if (!table) {
1789 table = g_ptr_array_new();
1790 }
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001791
Alex Bennéee8122a72024-07-18 10:45:11 +01001792 for (int i = 0; i < extensions->len; i++) {
1793 gpointer entry = g_ptr_array_index(extensions, i);
1794 if (!g_ptr_array_find(table, entry, NULL)) {
1795 g_ptr_array_add(table, entry);
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001796 }
1797 }
1798
Alex Bennéee8122a72024-07-18 10:45:11 +01001799 return table;
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001800}
1801
Alex Bennéee8122a72024-07-18 10:45:11 +01001802/**
1803 * process_extended_table() - run through an extended command table
1804 * @table: the command table to check
1805 * @data: parameters
1806 *
1807 * returns true if the command was found and executed
1808 */
1809static bool process_extended_table(GPtrArray *table, const char *data)
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001810{
Alex Bennéee8122a72024-07-18 10:45:11 +01001811 for (int i = 0; i < table->len; i++) {
1812 const GdbCmdParseEntry *entry = g_ptr_array_index(table, i);
1813 if (process_string_cmd(data, entry, 1)) {
1814 return true;
1815 }
1816 }
1817 return false;
1818}
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001819
Alex Bennéee8122a72024-07-18 10:45:11 +01001820
1821/* Ptr to GdbCmdParseEntry */
1822static GPtrArray *extended_query_table;
1823
1824void gdb_extend_query_table(GPtrArray *new_queries)
1825{
1826 extended_query_table = extend_table(extended_query_table, new_queries);
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001827}
1828
Philippe Mathieu-Daudé305bea02021-05-20 18:42:59 +01001829static const GdbCmdParseEntry gdb_gen_query_table[] = {
Jon Doron2704efa2019-05-29 09:41:45 +03001830 {
1831 .handler = handle_query_curr_tid,
1832 .cmd = "C",
1833 },
1834 {
1835 .handler = handle_query_threads,
1836 .cmd = "sThreadInfo",
1837 },
1838 {
1839 .handler = handle_query_first_threads,
1840 .cmd = "fThreadInfo",
1841 },
1842 {
1843 .handler = handle_query_thread_extra,
1844 .cmd = "ThreadExtraInfo,",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001845 .cmd_startswith = true,
Jon Doron2704efa2019-05-29 09:41:45 +03001846 .schema = "t0"
1847 },
1848#ifdef CONFIG_USER_ONLY
1849 {
Alex Bennéed96bf492023-03-02 18:57:47 -08001850 .handler = gdb_handle_query_offsets,
Jon Doron2704efa2019-05-29 09:41:45 +03001851 .cmd = "Offsets",
1852 },
1853#else
1854 {
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -08001855 .handler = gdb_handle_query_rcmd,
Jon Doron2704efa2019-05-29 09:41:45 +03001856 .cmd = "Rcmd,",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001857 .cmd_startswith = true,
Jon Doron2704efa2019-05-29 09:41:45 +03001858 .schema = "s0"
1859 },
1860#endif
1861 {
1862 .handler = handle_query_supported,
1863 .cmd = "Supported:",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001864 .cmd_startswith = true,
Jon Doron2704efa2019-05-29 09:41:45 +03001865 .schema = "s0"
1866 },
1867 {
1868 .handler = handle_query_supported,
1869 .cmd = "Supported",
1870 .schema = "s0"
1871 },
1872 {
1873 .handler = handle_query_xfer_features,
1874 .cmd = "Xfer:features:read:",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001875 .cmd_startswith = true,
Jon Doron2704efa2019-05-29 09:41:45 +03001876 .schema = "s:l,l0"
1877 },
Ilya Leoshkeviche2820102023-06-30 19:04:21 +01001878#if defined(CONFIG_USER_ONLY)
1879#if defined(CONFIG_LINUX)
Lirong Yuan51c623b2021-01-08 22:42:42 +00001880 {
Alex Bennéed96bf492023-03-02 18:57:47 -08001881 .handler = gdb_handle_query_xfer_auxv,
Lirong Yuan51c623b2021-01-08 22:42:42 +00001882 .cmd = "Xfer:auxv:read::",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001883 .cmd_startswith = true,
Lirong Yuan51c623b2021-01-08 22:42:42 +00001884 .schema = "l,l0"
1885 },
Gustavo Romero9ae58012024-03-09 03:09:00 +00001886 {
1887 .handler = gdb_handle_query_xfer_siginfo,
1888 .cmd = "Xfer:siginfo:read::",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001889 .cmd_startswith = true,
Gustavo Romero9ae58012024-03-09 03:09:00 +00001890 .schema = "l,l0"
1891 },
Lirong Yuan51c623b2021-01-08 22:42:42 +00001892#endif
Jon Doron2704efa2019-05-29 09:41:45 +03001893 {
Ilya Leoshkeviche2820102023-06-30 19:04:21 +01001894 .handler = gdb_handle_query_xfer_exec_file,
1895 .cmd = "Xfer:exec-file:read:",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001896 .cmd_startswith = true,
Ilya Leoshkeviche2820102023-06-30 19:04:21 +01001897 .schema = "l:l,l0"
1898 },
1899#endif
1900 {
Alex Bennée8a2025b2023-03-02 18:57:50 -08001901 .handler = gdb_handle_query_attached,
Jon Doron2704efa2019-05-29 09:41:45 +03001902 .cmd = "Attached:",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001903 .cmd_startswith = true
Jon Doron2704efa2019-05-29 09:41:45 +03001904 },
1905 {
Alex Bennée8a2025b2023-03-02 18:57:50 -08001906 .handler = gdb_handle_query_attached,
Jon Doron2704efa2019-05-29 09:41:45 +03001907 .cmd = "Attached",
1908 },
1909 {
1910 .handler = handle_query_qemu_supported,
1911 .cmd = "qemu.Supported",
1912 },
Jon Doronab4752e2019-05-29 09:41:48 +03001913#ifndef CONFIG_USER_ONLY
1914 {
Alex Bennée589a5862023-03-02 18:57:51 -08001915 .handler = gdb_handle_query_qemu_phy_mem_mode,
Jon Doronab4752e2019-05-29 09:41:48 +03001916 .cmd = "qemu.PhyMemMode",
1917 },
1918#endif
Jon Doron2704efa2019-05-29 09:41:45 +03001919};
1920
Alex Bennéee8122a72024-07-18 10:45:11 +01001921/* Ptr to GdbCmdParseEntry */
1922static GPtrArray *extended_set_table;
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001923
Alex Bennéee8122a72024-07-18 10:45:11 +01001924void gdb_extend_set_table(GPtrArray *new_set)
1925{
1926 extended_set_table = extend_table(extended_set_table, new_set);
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001927}
1928
Philippe Mathieu-Daudé305bea02021-05-20 18:42:59 +01001929static const GdbCmdParseEntry gdb_gen_set_table[] = {
Jon Doron2704efa2019-05-29 09:41:45 +03001930 /* Order is important if has same prefix */
1931 {
1932 .handler = handle_set_qemu_sstep,
1933 .cmd = "qemu.sstep:",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001934 .cmd_startswith = true,
Jon Doron2704efa2019-05-29 09:41:45 +03001935 .schema = "l0"
1936 },
Jon Doronab4752e2019-05-29 09:41:48 +03001937#ifndef CONFIG_USER_ONLY
1938 {
Alex Bennée589a5862023-03-02 18:57:51 -08001939 .handler = gdb_handle_set_qemu_phy_mem_mode,
Jon Doronab4752e2019-05-29 09:41:48 +03001940 .cmd = "qemu.PhyMemMode:",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001941 .cmd_startswith = true,
Jon Doronab4752e2019-05-29 09:41:48 +03001942 .schema = "l0"
1943 },
1944#endif
Ilya Leoshkevich046f1432024-02-07 16:38:11 +00001945#if defined(CONFIG_USER_ONLY)
1946 {
1947 .handler = gdb_handle_set_catch_syscalls,
1948 .cmd = "CatchSyscalls:",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01001949 .cmd_startswith = true,
Ilya Leoshkevich046f1432024-02-07 16:38:11 +00001950 .schema = "s0",
1951 },
1952#endif
Jon Doron2704efa2019-05-29 09:41:45 +03001953};
1954
Alex Bennée26a16182021-05-25 09:24:14 +01001955static void handle_gen_query(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03001956{
Alex Bennéee8122a72024-07-18 10:45:11 +01001957 const char *data;
1958
Alex Bennée26a16182021-05-25 09:24:14 +01001959 if (!params->len) {
Jon Doron2704efa2019-05-29 09:41:45 +03001960 return;
1961 }
1962
Alex Bennéee8122a72024-07-18 10:45:11 +01001963 data = gdb_get_cmd_param(params, 0)->data;
1964
1965 if (process_string_cmd(data,
Gustavo Romero0ef6b122024-07-05 09:40:37 +01001966 gdb_gen_query_set_common_table,
1967 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
Jon Doron2704efa2019-05-29 09:41:45 +03001968 return;
1969 }
1970
Alex Bennéee8122a72024-07-18 10:45:11 +01001971 if (process_string_cmd(data,
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001972 gdb_gen_query_table,
1973 ARRAY_SIZE(gdb_gen_query_table))) {
1974 return;
Jon Doron2704efa2019-05-29 09:41:45 +03001975 }
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001976
1977 if (extended_query_table &&
Alex Bennéee8122a72024-07-18 10:45:11 +01001978 process_extended_table(extended_query_table, data)) {
Gustavo Romero60f4ce82024-07-05 09:40:39 +01001979 return;
1980 }
1981
1982 /* Can't handle query, return Empty response. */
1983 gdb_put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03001984}
1985
Alex Bennée26a16182021-05-25 09:24:14 +01001986static void handle_gen_set(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03001987{
Alex Bennéee8122a72024-07-18 10:45:11 +01001988 const char *data;
1989
Alex Bennée26a16182021-05-25 09:24:14 +01001990 if (!params->len) {
Jon Doron2704efa2019-05-29 09:41:45 +03001991 return;
1992 }
1993
Alex Bennéee8122a72024-07-18 10:45:11 +01001994 data = gdb_get_cmd_param(params, 0)->data;
1995
1996 if (process_string_cmd(data,
Gustavo Romero0ef6b122024-07-05 09:40:37 +01001997 gdb_gen_query_set_common_table,
1998 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
Jon Doron2704efa2019-05-29 09:41:45 +03001999 return;
2000 }
2001
Alex Bennéee8122a72024-07-18 10:45:11 +01002002 if (process_string_cmd(data,
Jon Doron2704efa2019-05-29 09:41:45 +03002003 gdb_gen_set_table,
2004 ARRAY_SIZE(gdb_gen_set_table))) {
Gustavo Romero60f4ce82024-07-05 09:40:39 +01002005 return;
Jon Doron2704efa2019-05-29 09:41:45 +03002006 }
Gustavo Romero60f4ce82024-07-05 09:40:39 +01002007
2008 if (extended_set_table &&
Alex Bennéee8122a72024-07-18 10:45:11 +01002009 process_extended_table(extended_set_table, data)) {
Gustavo Romero60f4ce82024-07-05 09:40:39 +01002010 return;
2011 }
2012
2013 /* Can't handle set, return Empty response. */
2014 gdb_put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002015}
2016
Alex Bennée26a16182021-05-25 09:24:14 +01002017static void handle_target_halt(GArray *params, void *user_ctx)
Jon Doron7009d572019-05-29 09:41:46 +03002018{
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -03002019 if (gdbserver_state.allow_stop_reply) {
2020 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
2021 gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
2022 g_string_append_c(gdbserver_state.str_buf, ';');
2023 gdb_put_strbuf();
2024 gdbserver_state.allow_stop_reply = false;
2025 }
Jon Doron7009d572019-05-29 09:41:46 +03002026 /*
2027 * Remove all the breakpoints when this query is issued,
2028 * because gdb is doing an initial connect and the state
2029 * should be cleaned up.
2030 */
Alex Bennéeae7467b2022-09-29 12:42:24 +01002031 gdb_breakpoint_remove_all(gdbserver_state.c_cpu);
Jon Doron7009d572019-05-29 09:41:46 +03002032}
2033
Alex Bennéea346af32020-03-16 17:21:34 +00002034static int gdb_handle_packet(const char *line_buf)
Jon Doron2704efa2019-05-29 09:41:45 +03002035{
Jon Doron3e2c1262019-05-29 09:41:30 +03002036 const GdbCmdParseEntry *cmd_parser = NULL;
ths3b46e622007-09-17 08:09:54 +00002037
Doug Gale5c9522b2017-12-02 20:30:37 -05002038 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01002039
Jon Doron3f1cbac2019-05-29 09:41:47 +03002040 switch (line_buf[0]) {
Luc Michel53fd6552019-01-07 15:23:46 +00002041 case '!':
Alex Bennée36e067b2023-03-02 18:57:45 -08002042 gdb_put_packet("OK");
Luc Michel53fd6552019-01-07 15:23:46 +00002043 break;
bellard858693c2004-03-31 18:52:07 +00002044 case '?':
Jon Doron7009d572019-05-29 09:41:46 +03002045 {
2046 static const GdbCmdParseEntry target_halted_cmd_desc = {
2047 .handler = handle_target_halt,
2048 .cmd = "?",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002049 .cmd_startswith = true,
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -03002050 .allow_stop_reply = true,
Jon Doron7009d572019-05-29 09:41:46 +03002051 };
2052 cmd_parser = &target_halted_cmd_desc;
2053 }
bellard858693c2004-03-31 18:52:07 +00002054 break;
2055 case 'c':
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002056 {
2057 static const GdbCmdParseEntry continue_cmd_desc = {
2058 .handler = handle_continue,
2059 .cmd = "c",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002060 .cmd_startswith = true,
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -03002061 .allow_stop_reply = true,
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002062 .schema = "L0"
2063 };
2064 cmd_parser = &continue_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002065 }
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002066 break;
edgar_igl1f487ee2008-05-17 22:20:53 +00002067 case 'C':
Jon Doronccc47d52019-05-29 09:41:33 +03002068 {
2069 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2070 .handler = handle_cont_with_sig,
2071 .cmd = "C",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002072 .cmd_startswith = true,
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -03002073 .allow_stop_reply = true,
Jon Doronccc47d52019-05-29 09:41:33 +03002074 .schema = "l0"
2075 };
2076 cmd_parser = &cont_with_sig_cmd_desc;
2077 }
2078 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002079 case 'v':
Jon Doron8536ec02019-05-29 09:41:44 +03002080 {
2081 static const GdbCmdParseEntry v_cmd_desc = {
2082 .handler = handle_v_commands,
2083 .cmd = "v",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002084 .cmd_startswith = true,
Jon Doron8536ec02019-05-29 09:41:44 +03002085 .schema = "s0"
2086 };
2087 cmd_parser = &v_cmd_desc;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002088 }
Jon Doron8536ec02019-05-29 09:41:44 +03002089 break;
edgar_igl7d03f822008-05-17 18:58:29 +00002090 case 'k':
2091 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002092 error_report("QEMU: Terminated via GDBstub");
Alex Bennéeb9e10c62021-01-08 22:42:45 +00002093 gdb_exit(0);
Clément Chigote2162562023-10-03 09:14:27 +02002094 gdb_qemu_exit(0);
2095 break;
edgar_igl7d03f822008-05-17 18:58:29 +00002096 case 'D':
Jon Doron3e2c1262019-05-29 09:41:30 +03002097 {
2098 static const GdbCmdParseEntry detach_cmd_desc = {
2099 .handler = handle_detach,
2100 .cmd = "D",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002101 .cmd_startswith = true,
Jon Doron3e2c1262019-05-29 09:41:30 +03002102 .schema = "?.l0"
2103 };
2104 cmd_parser = &detach_cmd_desc;
Luc Michel546f3c62019-01-07 15:23:46 +00002105 }
edgar_igl7d03f822008-05-17 18:58:29 +00002106 break;
bellard858693c2004-03-31 18:52:07 +00002107 case 's':
Jon Doron933f80d2019-05-29 09:41:43 +03002108 {
2109 static const GdbCmdParseEntry step_cmd_desc = {
2110 .handler = handle_step,
2111 .cmd = "s",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002112 .cmd_startswith = true,
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -03002113 .allow_stop_reply = true,
Jon Doron933f80d2019-05-29 09:41:43 +03002114 .schema = "L0"
2115 };
2116 cmd_parser = &step_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002117 }
Jon Doron933f80d2019-05-29 09:41:43 +03002118 break;
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03002119 case 'b':
2120 {
2121 static const GdbCmdParseEntry backward_cmd_desc = {
2122 .handler = handle_backward,
2123 .cmd = "b",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002124 .cmd_startswith = true,
Nicholas Piggin3b72d682023-06-30 19:04:14 +01002125 .allow_stop_reply = true,
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03002126 .schema = "o0"
2127 };
2128 cmd_parser = &backward_cmd_desc;
2129 }
2130 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002131 case 'F':
2132 {
Jon Doron4b20fab2019-05-29 09:41:42 +03002133 static const GdbCmdParseEntry file_io_cmd_desc = {
Alex Bennéec5660802023-03-02 18:57:57 -08002134 .handler = gdb_handle_file_io,
Jon Doron4b20fab2019-05-29 09:41:42 +03002135 .cmd = "F",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002136 .cmd_startswith = true,
Jon Doron4b20fab2019-05-29 09:41:42 +03002137 .schema = "L,L,o0"
2138 };
2139 cmd_parser = &file_io_cmd_desc;
pbrooka2d1eba2007-01-28 03:10:55 +00002140 }
2141 break;
bellard858693c2004-03-31 18:52:07 +00002142 case 'g':
Jon Doron397d1372019-05-29 09:41:41 +03002143 {
2144 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2145 .handler = handle_read_all_regs,
2146 .cmd = "g",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002147 .cmd_startswith = true
Jon Doron397d1372019-05-29 09:41:41 +03002148 };
2149 cmd_parser = &read_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002150 }
bellard858693c2004-03-31 18:52:07 +00002151 break;
2152 case 'G':
Jon Doron287ca122019-05-29 09:41:40 +03002153 {
2154 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2155 .handler = handle_write_all_regs,
2156 .cmd = "G",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002157 .cmd_startswith = true,
Jon Doron287ca122019-05-29 09:41:40 +03002158 .schema = "s0"
2159 };
2160 cmd_parser = &write_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002161 }
bellard858693c2004-03-31 18:52:07 +00002162 break;
2163 case 'm':
Jon Doronda92e232019-05-29 09:41:39 +03002164 {
2165 static const GdbCmdParseEntry read_mem_cmd_desc = {
2166 .handler = handle_read_mem,
2167 .cmd = "m",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002168 .cmd_startswith = true,
Jon Doronda92e232019-05-29 09:41:39 +03002169 .schema = "L,L0"
2170 };
2171 cmd_parser = &read_mem_cmd_desc;
bellard6f970bd2005-12-05 19:55:19 +00002172 }
bellard858693c2004-03-31 18:52:07 +00002173 break;
2174 case 'M':
Jon Doroncc0ecc72019-05-29 09:41:38 +03002175 {
2176 static const GdbCmdParseEntry write_mem_cmd_desc = {
2177 .handler = handle_write_mem,
2178 .cmd = "M",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002179 .cmd_startswith = true,
Jon Doroncc0ecc72019-05-29 09:41:38 +03002180 .schema = "L,L:s0"
2181 };
2182 cmd_parser = &write_mem_cmd_desc;
Fabien Chouteau44520db2011-09-08 12:48:16 +02002183 }
bellard858693c2004-03-31 18:52:07 +00002184 break;
pbrook56aebc82008-10-11 17:55:29 +00002185 case 'p':
Jon Doron5d0e57b2019-05-29 09:41:37 +03002186 {
2187 static const GdbCmdParseEntry get_reg_cmd_desc = {
2188 .handler = handle_get_reg,
2189 .cmd = "p",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002190 .cmd_startswith = true,
Jon Doron5d0e57b2019-05-29 09:41:37 +03002191 .schema = "L0"
2192 };
2193 cmd_parser = &get_reg_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002194 }
2195 break;
2196 case 'P':
Jon Doron62b33202019-05-29 09:41:36 +03002197 {
2198 static const GdbCmdParseEntry set_reg_cmd_desc = {
2199 .handler = handle_set_reg,
2200 .cmd = "P",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002201 .cmd_startswith = true,
Jon Doron62b33202019-05-29 09:41:36 +03002202 .schema = "L?s0"
2203 };
2204 cmd_parser = &set_reg_cmd_desc;
2205 }
pbrook56aebc82008-10-11 17:55:29 +00002206 break;
bellard858693c2004-03-31 18:52:07 +00002207 case 'Z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002208 {
2209 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2210 .handler = handle_insert_bp,
2211 .cmd = "Z",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002212 .cmd_startswith = true,
Jon Doron77f6ce52019-05-29 09:41:35 +03002213 .schema = "l?L?L0"
2214 };
2215 cmd_parser = &insert_bp_cmd_desc;
2216 }
2217 break;
bellard858693c2004-03-31 18:52:07 +00002218 case 'z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002219 {
2220 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2221 .handler = handle_remove_bp,
2222 .cmd = "z",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002223 .cmd_startswith = true,
Jon Doron77f6ce52019-05-29 09:41:35 +03002224 .schema = "l?L?L0"
2225 };
2226 cmd_parser = &remove_bp_cmd_desc;
2227 }
bellard858693c2004-03-31 18:52:07 +00002228 break;
aliguori880a7572008-11-18 20:30:24 +00002229 case 'H':
Jon Doron3a9651d2019-05-29 09:41:34 +03002230 {
2231 static const GdbCmdParseEntry set_thread_cmd_desc = {
2232 .handler = handle_set_thread,
2233 .cmd = "H",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002234 .cmd_startswith = true,
Jon Doron3a9651d2019-05-29 09:41:34 +03002235 .schema = "o.t0"
2236 };
2237 cmd_parser = &set_thread_cmd_desc;
aliguori880a7572008-11-18 20:30:24 +00002238 }
2239 break;
2240 case 'T':
Jon Doron44ffded2019-05-29 09:41:31 +03002241 {
2242 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2243 .handler = handle_thread_alive,
2244 .cmd = "T",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002245 .cmd_startswith = true,
Jon Doron44ffded2019-05-29 09:41:31 +03002246 .schema = "t0"
2247 };
2248 cmd_parser = &thread_alive_cmd_desc;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002249 }
aliguori880a7572008-11-18 20:30:24 +00002250 break;
pbrook978efd62006-06-17 18:30:42 +00002251 case 'q':
Jon Doron2704efa2019-05-29 09:41:45 +03002252 {
2253 static const GdbCmdParseEntry gen_query_cmd_desc = {
2254 .handler = handle_gen_query,
2255 .cmd = "q",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002256 .cmd_startswith = true,
Jon Doron2704efa2019-05-29 09:41:45 +03002257 .schema = "s0"
2258 };
2259 cmd_parser = &gen_query_cmd_desc;
2260 }
2261 break;
edgar_igl60897d32008-05-09 08:25:14 +00002262 case 'Q':
Jon Doron2704efa2019-05-29 09:41:45 +03002263 {
2264 static const GdbCmdParseEntry gen_set_cmd_desc = {
2265 .handler = handle_gen_set,
2266 .cmd = "Q",
Gustavo Romero3b6c27d2024-07-05 09:40:45 +01002267 .cmd_startswith = true,
Jon Doron2704efa2019-05-29 09:41:45 +03002268 .schema = "s0"
2269 };
2270 cmd_parser = &gen_set_cmd_desc;
edgar_igl60897d32008-05-09 08:25:14 +00002271 }
Jon Doron2704efa2019-05-29 09:41:45 +03002272 break;
bellard858693c2004-03-31 18:52:07 +00002273 default:
bellard858693c2004-03-31 18:52:07 +00002274 /* put empty packet */
Alex Bennée36e067b2023-03-02 18:57:45 -08002275 gdb_put_packet("");
bellard858693c2004-03-31 18:52:07 +00002276 break;
2277 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002278
Ramiro Polla2bdec392019-08-05 21:09:01 +02002279 if (cmd_parser) {
Alex Bennéea346af32020-03-16 17:21:34 +00002280 run_cmd_parser(line_buf, cmd_parser);
Ramiro Polla2bdec392019-08-05 21:09:01 +02002281 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002282
bellard858693c2004-03-31 18:52:07 +00002283 return RS_IDLE;
2284}
2285
Andreas Färber64f6b342013-05-27 02:06:09 +02002286void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00002287{
Alex Bennéea346af32020-03-16 17:21:34 +00002288 GDBProcess *p = gdb_get_cpu_process(cpu);
Luc Michel160d8582019-01-07 15:23:46 +00002289
2290 if (!p->attached) {
2291 /*
2292 * Having a stop CPU corresponding to a process that is not attached
2293 * confuses GDB. So we ignore the request.
2294 */
2295 return;
2296 }
2297
Alex Bennée8d98c442020-03-16 17:21:33 +00002298 gdbserver_state.c_cpu = cpu;
2299 gdbserver_state.g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00002300}
2301
Alex Bennée36e067b2023-03-02 18:57:45 -08002302void gdb_read_byte(uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00002303{
ths60fe76f2007-12-16 03:02:09 +00002304 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002305
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -03002306 gdbserver_state.allow_stop_reply = false;
bellard1fddef42005-04-17 19:16:13 +00002307#ifndef CONFIG_USER_ONLY
Damien Hedded116e812020-03-16 17:21:53 +00002308 if (gdbserver_state.last_packet->len) {
pbrook4046d912007-01-28 01:53:16 +00002309 /* Waiting for a response to the last packet. If we see the start
2310 of a new command then abandon the previous response. */
2311 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002312 trace_gdbstub_err_got_nack();
Alex Bennée36e067b2023-03-02 18:57:45 -08002313 gdb_put_buffer(gdbserver_state.last_packet->data,
Damien Hedded116e812020-03-16 17:21:53 +00002314 gdbserver_state.last_packet->len);
Alex Bennée118e2262017-07-12 11:52:13 +01002315 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002316 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002317 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002318 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002319 }
Alex Bennée118e2262017-07-12 11:52:13 +01002320
Damien Hedded116e812020-03-16 17:21:53 +00002321 if (ch == '+' || ch == '$') {
2322 g_byte_array_set_size(gdbserver_state.last_packet, 0);
2323 }
pbrook4046d912007-01-28 01:53:16 +00002324 if (ch != '$')
2325 return;
2326 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002327 if (runstate_is_running()) {
Nicholas Piggin108e8182023-07-11 18:59:03 +10002328 /*
2329 * When the CPU is running, we cannot do anything except stop
2330 * it when receiving a char. This is expected on a Ctrl-C in the
2331 * gdb client. Because we are in all-stop mode, gdb sends a
2332 * 0x03 byte which is not a usual packet, so we handle it specially
2333 * here, but it does expect a stop reply.
2334 */
2335 if (ch != 0x03) {
Alex Bennée3869eb72023-08-10 16:36:39 +01002336 trace_gdbstub_err_unexpected_runpkt(ch);
2337 } else {
2338 gdbserver_state.allow_stop_reply = true;
Nicholas Piggin108e8182023-07-11 18:59:03 +10002339 }
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002340 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002341 } else
bellard1fddef42005-04-17 19:16:13 +00002342#endif
bellard41625032005-04-24 10:07:11 +00002343 {
Alex Bennéea346af32020-03-16 17:21:34 +00002344 switch(gdbserver_state.state) {
bellard858693c2004-03-31 18:52:07 +00002345 case RS_IDLE:
2346 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002347 /* start of command packet */
Alex Bennéea346af32020-03-16 17:21:34 +00002348 gdbserver_state.line_buf_index = 0;
2349 gdbserver_state.line_sum = 0;
2350 gdbserver_state.state = RS_GETLINE;
Alex Bennéef1b0f892023-08-10 16:36:40 +01002351 } else if (ch == '+') {
2352 /*
2353 * do nothing, gdb may preemptively send out ACKs on
2354 * initial connection
2355 */
Doug Gale4bf43122017-05-01 12:22:10 -04002356 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002357 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00002358 }
2359 break;
bellard858693c2004-03-31 18:52:07 +00002360 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002361 if (ch == '}') {
2362 /* start escape sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002363 gdbserver_state.state = RS_GETLINE_ESC;
2364 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002365 } else if (ch == '*') {
2366 /* start run length encoding sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002367 gdbserver_state.state = RS_GETLINE_RLE;
2368 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002369 } else if (ch == '#') {
2370 /* end of command, start of checksum*/
Alex Bennéea346af32020-03-16 17:21:34 +00002371 gdbserver_state.state = RS_CHKSUM1;
2372 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002373 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002374 gdbserver_state.state = RS_IDLE;
bellard858693c2004-03-31 18:52:07 +00002375 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002376 /* unescaped command character */
Alex Bennéea346af32020-03-16 17:21:34 +00002377 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
2378 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002379 }
2380 break;
2381 case RS_GETLINE_ESC:
2382 if (ch == '#') {
2383 /* unexpected end of command in escape sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002384 gdbserver_state.state = RS_CHKSUM1;
2385 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002386 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002387 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002388 gdbserver_state.state = RS_IDLE;
Doug Gale4bf43122017-05-01 12:22:10 -04002389 } else {
2390 /* parse escaped character and leave escape state */
Alex Bennéea346af32020-03-16 17:21:34 +00002391 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
2392 gdbserver_state.line_sum += ch;
2393 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002394 }
2395 break;
2396 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02002397 /*
2398 * Run-length encoding is explained in "Debugging with GDB /
2399 * Appendix E GDB Remote Serial Protocol / Overview".
2400 */
2401 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04002402 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002403 trace_gdbstub_err_invalid_repeat(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002404 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002405 } else {
2406 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002407 int repeat = ch - ' ' + 3;
Alex Bennéea346af32020-03-16 17:21:34 +00002408 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002409 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002410 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002411 gdbserver_state.state = RS_IDLE;
2412 } else if (gdbserver_state.line_buf_index < 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002413 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002414 trace_gdbstub_err_invalid_rle();
Alex Bennéea346af32020-03-16 17:21:34 +00002415 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002416 } else {
2417 /* repeat the last character */
Alex Bennéea346af32020-03-16 17:21:34 +00002418 memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
2419 gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
2420 gdbserver_state.line_buf_index += repeat;
2421 gdbserver_state.line_sum += ch;
2422 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002423 }
bellard858693c2004-03-31 18:52:07 +00002424 }
2425 break;
2426 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002427 /* get high hex digit of checksum */
2428 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002429 trace_gdbstub_err_checksum_invalid(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002430 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002431 break;
2432 }
Alex Bennéea346af32020-03-16 17:21:34 +00002433 gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
2434 gdbserver_state.line_csum = fromhex(ch) << 4;
2435 gdbserver_state.state = RS_CHKSUM2;
bellard858693c2004-03-31 18:52:07 +00002436 break;
2437 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002438 /* get low hex digit of checksum */
2439 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002440 trace_gdbstub_err_checksum_invalid(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002441 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002442 break;
bellard858693c2004-03-31 18:52:07 +00002443 }
Alex Bennéea346af32020-03-16 17:21:34 +00002444 gdbserver_state.line_csum |= fromhex(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002445
Alex Bennéea346af32020-03-16 17:21:34 +00002446 if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
2447 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002448 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002449 reply = '-';
Alex Bennée36e067b2023-03-02 18:57:45 -08002450 gdb_put_buffer(&reply, 1);
Alex Bennéea346af32020-03-16 17:21:34 +00002451 gdbserver_state.state = RS_IDLE;
bellard858693c2004-03-31 18:52:07 +00002452 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002453 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002454 reply = '+';
Alex Bennée36e067b2023-03-02 18:57:45 -08002455 gdb_put_buffer(&reply, 1);
Alex Bennéea346af32020-03-16 17:21:34 +00002456 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
bellard858693c2004-03-31 18:52:07 +00002457 }
bellardb4608c02003-06-27 17:34:32 +00002458 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002459 default:
2460 abort();
bellardb4608c02003-06-27 17:34:32 +00002461 }
2462 }
bellard858693c2004-03-31 18:52:07 +00002463}
2464
Luc Michel8f468632019-01-07 15:23:45 +00002465/*
2466 * Create the process that will contain all the "orphan" CPUs (that are not
2467 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2468 * be attachable and thus will be invisible to the user.
2469 */
Alex Bennée36e067b2023-03-02 18:57:45 -08002470void gdb_create_default_process(GDBState *s)
Luc Michel8f468632019-01-07 15:23:45 +00002471{
2472 GDBProcess *process;
Ilya Leoshkevichdc14a7a2023-06-30 19:04:20 +01002473 int pid;
Luc Michel8f468632019-01-07 15:23:45 +00002474
Ilya Leoshkevichdc14a7a2023-06-30 19:04:20 +01002475#ifdef CONFIG_USER_ONLY
2476 assert(gdbserver_state.process_num == 0);
2477 pid = getpid();
2478#else
Alex Bennéea346af32020-03-16 17:21:34 +00002479 if (gdbserver_state.process_num) {
Ilya Leoshkevichdc14a7a2023-06-30 19:04:20 +01002480 pid = s->processes[s->process_num - 1].pid;
2481 } else {
2482 pid = 0;
Luc Michel8f468632019-01-07 15:23:45 +00002483 }
Ilya Leoshkevichdc14a7a2023-06-30 19:04:20 +01002484 /* We need an available PID slot for this process */
2485 assert(pid < UINT32_MAX);
2486 pid++;
2487#endif
Luc Michel8f468632019-01-07 15:23:45 +00002488
2489 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2490 process = &s->processes[s->process_num - 1];
Ilya Leoshkevichdc14a7a2023-06-30 19:04:20 +01002491 process->pid = pid;
Luc Michel8f468632019-01-07 15:23:45 +00002492 process->attached = false;
Alex Bennée56e534b2023-08-29 17:15:26 +01002493 process->target_xml = NULL;
Luc Michel8f468632019-01-07 15:23:45 +00002494}
2495