system/physmem: use return value of ram_block_discard_require() as errno
[qemu/armbru.git] / gdbstub / gdbstub.c
blobb9ad0a063ed65909c82d624d57a8ea9006200a48
1 /*
2 * gdb server stub
4 * This implements a subset of the remote protocol as described in:
6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
8 * Copyright (c) 2003-2005 Fabrice Bellard
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.
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.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 * SPDX-License-Identifier: LGPL-2.0+
26 #include "qemu/osdep.h"
27 #include "qemu/ctype.h"
28 #include "qemu/cutils.h"
29 #include "qemu/module.h"
30 #include "qemu/error-report.h"
31 #include "trace.h"
32 #include "exec/gdbstub.h"
33 #include "gdbstub/commands.h"
34 #include "gdbstub/syscalls.h"
35 #ifdef CONFIG_USER_ONLY
36 #include "accel/tcg/vcpu-state.h"
37 #include "gdbstub/user.h"
38 #else
39 #include "hw/cpu/cluster.h"
40 #include "hw/boards.h"
41 #endif
42 #include "hw/core/cpu.h"
44 #include "sysemu/hw_accel.h"
45 #include "sysemu/runstate.h"
46 #include "exec/replay-core.h"
47 #include "exec/hwaddr.h"
49 #include "internals.h"
51 typedef struct GDBRegisterState {
52 int base_reg;
53 gdb_get_reg_cb get_reg;
54 gdb_set_reg_cb set_reg;
55 const GDBFeature *feature;
56 } GDBRegisterState;
58 GDBState gdbserver_state;
60 void gdb_init_gdbserver_state(void)
62 g_assert(!gdbserver_state.init);
63 memset(&gdbserver_state, 0, sizeof(GDBState));
64 gdbserver_state.init = true;
65 gdbserver_state.str_buf = g_string_new(NULL);
66 gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
67 gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4);
70 * 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.
74 gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags();
75 gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
76 gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags;
79 /* writes 2*len+1 bytes in buf */
80 void gdb_memtohex(GString *buf, const uint8_t *mem, int len)
82 int i, c;
83 for(i = 0; i < len; i++) {
84 c = mem[i];
85 g_string_append_c(buf, tohex(c >> 4));
86 g_string_append_c(buf, tohex(c & 0xf));
88 g_string_append_c(buf, '\0');
91 void gdb_hextomem(GByteArray *mem, const char *buf, int len)
93 int i;
95 for(i = 0; i < len; i++) {
96 guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
97 g_byte_array_append(mem, &byte, 1);
98 buf += 2;
102 static void hexdump(const char *buf, int len,
103 void (*trace_fn)(size_t ofs, char const *text))
105 char line_buffer[3 * 16 + 4 + 16 + 1];
107 size_t i;
108 for (i = 0; i < len || (i & 0xF); ++i) {
109 size_t byte_ofs = i & 15;
111 if (byte_ofs == 0) {
112 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
113 line_buffer[3 * 16 + 4 + 16] = 0;
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;
120 if (i < len) {
121 char value = buf[i];
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 : '.';
130 if (byte_ofs == 0xF)
131 trace_fn(i & -16, line_buffer);
135 /* return -1 if error, 0 if OK */
136 int gdb_put_packet_binary(const char *buf, int len, bool dump)
138 int csum, i;
139 uint8_t footer[3];
141 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
142 hexdump(buf, len, trace_gdbstub_io_binaryreply);
145 for(;;) {
146 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);
151 csum = 0;
152 for(i = 0; i < len; i++) {
153 csum += buf[i];
155 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);
160 gdb_put_buffer(gdbserver_state.last_packet->data,
161 gdbserver_state.last_packet->len);
163 if (gdb_got_immediate_ack()) {
164 break;
167 return 0;
170 /* return -1 if error, 0 if OK */
171 int gdb_put_packet(const char *buf)
173 trace_gdbstub_io_reply(buf);
175 return gdb_put_packet_binary(buf, strlen(buf), false);
178 void gdb_put_strbuf(void)
180 gdb_put_packet(gdbserver_state.str_buf->str);
183 /* Encode data using the encoding for 'x' packets. */
184 void gdb_memtox(GString *buf, const char *mem, int len)
186 char c;
188 while (len--) {
189 c = *(mem++);
190 switch (c) {
191 case '#': case '$': case '*': case '}':
192 g_string_append_c(buf, '}');
193 g_string_append_c(buf, c ^ 0x20);
194 break;
195 default:
196 g_string_append_c(buf, c);
197 break;
202 static uint32_t gdb_get_cpu_pid(CPUState *cpu)
204 #ifdef CONFIG_USER_ONLY
205 return getpid();
206 #else
207 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
208 /* Return the default process' PID */
209 int index = gdbserver_state.process_num - 1;
210 return gdbserver_state.processes[index].pid;
212 return cpu->cluster_index + 1;
213 #endif
216 GDBProcess *gdb_get_process(uint32_t pid)
218 int i;
220 if (!pid) {
221 /* 0 means any process, we take the first one */
222 return &gdbserver_state.processes[0];
225 for (i = 0; i < gdbserver_state.process_num; i++) {
226 if (gdbserver_state.processes[i].pid == pid) {
227 return &gdbserver_state.processes[i];
231 return NULL;
234 static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
236 return gdb_get_process(gdb_get_cpu_pid(cpu));
239 static CPUState *find_cpu(uint32_t thread_id)
241 CPUState *cpu;
243 CPU_FOREACH(cpu) {
244 if (gdb_get_cpu_index(cpu) == thread_id) {
245 return cpu;
249 return NULL;
252 CPUState *gdb_get_first_cpu_in_process(GDBProcess *process)
254 CPUState *cpu;
256 CPU_FOREACH(cpu) {
257 if (gdb_get_cpu_pid(cpu) == process->pid) {
258 return cpu;
262 return NULL;
265 static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
267 uint32_t pid = gdb_get_cpu_pid(cpu);
268 cpu = CPU_NEXT(cpu);
270 while (cpu) {
271 if (gdb_get_cpu_pid(cpu) == pid) {
272 break;
275 cpu = CPU_NEXT(cpu);
278 return cpu;
281 /* Return the cpu following @cpu, while ignoring unattached processes. */
282 static CPUState *gdb_next_attached_cpu(CPUState *cpu)
284 cpu = CPU_NEXT(cpu);
286 while (cpu) {
287 if (gdb_get_cpu_process(cpu)->attached) {
288 break;
291 cpu = CPU_NEXT(cpu);
294 return cpu;
297 /* Return the first attached cpu */
298 CPUState *gdb_first_attached_cpu(void)
300 CPUState *cpu = first_cpu;
301 GDBProcess *process = gdb_get_cpu_process(cpu);
303 if (!process->attached) {
304 return gdb_next_attached_cpu(cpu);
307 return cpu;
310 static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
312 GDBProcess *process;
313 CPUState *cpu;
315 if (!pid && !tid) {
316 /* 0 means any process/thread, we take the first attached one */
317 return gdb_first_attached_cpu();
318 } else if (pid && !tid) {
319 /* any thread in a specific process */
320 process = gdb_get_process(pid);
322 if (process == NULL) {
323 return NULL;
326 if (!process->attached) {
327 return NULL;
330 return gdb_get_first_cpu_in_process(process);
331 } else {
332 /* a specific thread */
333 cpu = find_cpu(tid);
335 if (cpu == NULL) {
336 return NULL;
339 process = gdb_get_cpu_process(cpu);
341 if (pid && process->pid != pid) {
342 return NULL;
345 if (!process->attached) {
346 return NULL;
349 return cpu;
353 static const char *get_feature_xml(const char *p, const char **newp,
354 GDBProcess *process)
356 CPUState *cpu = gdb_get_first_cpu_in_process(process);
357 CPUClass *cc = CPU_GET_CLASS(cpu);
358 GDBRegisterState *r;
359 size_t len;
362 * qXfer:features:read:ANNEX:OFFSET,LENGTH'
363 * ^p ^newp
365 char *term = strchr(p, ':');
366 *newp = term + 1;
367 len = term - p;
369 /* Is it the main target xml? */
370 if (strncmp(p, "target.xml", len) == 0) {
371 if (!process->target_xml) {
372 g_autoptr(GPtrArray) xml = g_ptr_array_new_with_free_func(g_free);
374 g_ptr_array_add(
375 xml,
376 g_strdup("<?xml version=\"1.0\"?>"
377 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
378 "<target>"));
380 if (cc->gdb_arch_name) {
381 g_ptr_array_add(
382 xml,
383 g_markup_printf_escaped("<architecture>%s</architecture>",
384 cc->gdb_arch_name(cpu)));
386 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));
393 g_ptr_array_add(xml, g_strdup("</target>"));
394 g_ptr_array_add(xml, NULL);
396 process->target_xml = g_strjoinv(NULL, (void *)xml->pdata);
398 return process->target_xml;
400 /* 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;
408 /* failed */
409 return NULL;
412 void gdb_feature_builder_init(GDBFeatureBuilder *builder, GDBFeature *feature,
413 const char *name, const char *xmlname,
414 int base_reg)
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);
422 builder->feature = feature;
423 builder->xml = g_ptr_array_new();
424 g_ptr_array_add(builder->xml, header);
425 builder->regs = g_ptr_array_new();
426 builder->base_reg = base_reg;
427 feature->xmlname = xmlname;
428 feature->name = name;
431 void gdb_feature_builder_append_tag(const GDBFeatureBuilder *builder,
432 const char *format, ...)
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);
440 void 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)
447 if (builder->regs->len <= regnum) {
448 g_ptr_array_set_size(builder->regs, regnum + 1);
451 builder->regs->pdata[regnum] = (gpointer *)name;
453 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);
466 void gdb_feature_builder_end(const GDBFeatureBuilder *builder)
468 g_ptr_array_add(builder->xml, (void *)"</feature>");
469 g_ptr_array_add(builder->xml, NULL);
471 builder->feature->xml = g_strjoinv(NULL, (void *)builder->xml->pdata);
473 for (guint i = 0; i < builder->xml->len - 2; i++) {
474 g_free(g_ptr_array_index(builder->xml, i));
477 g_ptr_array_free(builder->xml, TRUE);
479 builder->feature->num_regs = builder->regs->len;
480 builder->feature->regs = (void *)g_ptr_array_free(builder->regs, FALSE);
483 const GDBFeature *gdb_find_static_feature(const char *xmlname)
485 const GDBFeature *feature;
487 for (feature = gdb_static_features; feature->xmlname; feature++) {
488 if (!strcmp(feature->xmlname, xmlname)) {
489 return feature;
493 g_assert_not_reached();
496 GArray *gdb_get_register_list(CPUState *cpu)
498 GArray *results = g_array_new(true, true, sizeof(GDBRegDesc));
500 /* registers are only available once the CPU is initialised */
501 if (!cpu->gdb_regs) {
502 return results;
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
514 g_array_append_val(results, desc);
518 return results;
521 int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
523 CPUClass *cc = CPU_GET_CLASS(cpu);
524 GDBRegisterState *r;
526 if (reg < cc->gdb_num_core_regs) {
527 return cc->gdb_read_register(cpu, buf, reg);
530 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);
536 return 0;
539 static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
541 CPUClass *cc = CPU_GET_CLASS(cpu);
542 GDBRegisterState *r;
544 if (reg < cc->gdb_num_core_regs) {
545 return cc->gdb_write_register(cpu, mem_buf, reg);
548 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);
554 return 0;
557 static 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)
561 GDBRegisterState s = {
562 .base_reg = base_reg,
563 .get_reg = get_reg,
564 .set_reg = set_reg,
565 .feature = feature
568 g_array_append_val(cpu->gdb_regs, s);
571 void gdb_init_cpu(CPUState *cpu)
573 CPUClass *cc = CPU_GET_CLASS(cpu);
574 const GDBFeature *feature;
576 cpu->gdb_regs = g_array_new(false, false, sizeof(GDBRegisterState));
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);
583 cpu->gdb_num_regs = cpu->gdb_num_g_regs = feature->num_regs;
586 if (cc->gdb_num_core_regs) {
587 cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs;
591 void gdb_register_coprocessor(CPUState *cpu,
592 gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
593 const GDBFeature *feature, int g_pos)
595 GDBRegisterState *s;
596 guint i;
597 int base_reg = cpu->gdb_num_regs;
599 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;
607 gdb_register_feature(cpu, base_reg, get_reg, set_reg, feature);
609 /* Add to end of list. */
610 cpu->gdb_num_regs += feature->num_regs;
611 if (g_pos) {
612 if (g_pos != base_reg) {
613 error_report("Error: Bad gdb register numbering for '%s', "
614 "expected %d got %d", feature->xml, g_pos, base_reg);
615 } else {
616 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
621 static void gdb_process_breakpoint_remove_all(GDBProcess *p)
623 CPUState *cpu = gdb_get_first_cpu_in_process(p);
625 while (cpu) {
626 gdb_breakpoint_remove_all(cpu);
627 cpu = gdb_next_cpu_in_process(cpu);
632 static void gdb_set_cpu_pc(vaddr pc)
634 CPUState *cpu = gdbserver_state.c_cpu;
636 cpu_synchronize_state(cpu);
637 cpu_set_pc(cpu, pc);
640 void gdb_append_thread_id(CPUState *cpu, GString *buf)
642 if (gdbserver_state.multiprocess) {
643 g_string_append_printf(buf, "p%02x.%02x",
644 gdb_get_cpu_pid(cpu), gdb_get_cpu_index(cpu));
645 } else {
646 g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu));
650 static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
651 uint32_t *pid, uint32_t *tid)
653 unsigned long p, t;
654 int ret;
656 if (*buf == 'p') {
657 buf++;
658 ret = qemu_strtoul(buf, &buf, 16, &p);
660 if (ret) {
661 return GDB_READ_THREAD_ERR;
664 /* Skip '.' */
665 buf++;
666 } else {
667 p = 0;
670 ret = qemu_strtoul(buf, &buf, 16, &t);
672 if (ret) {
673 return GDB_READ_THREAD_ERR;
676 *end_buf = buf;
678 if (p == -1) {
679 return GDB_ALL_PROCESSES;
682 if (pid) {
683 *pid = p;
686 if (t == -1) {
687 return GDB_ALL_THREADS;
690 if (tid) {
691 *tid = t;
694 return GDB_ONE_THREAD;
698 * gdb_handle_vcont - Parses and handles a vCont packet.
699 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
700 * a format error, 0 on success.
702 static int gdb_handle_vcont(const char *p)
704 int res, signal = 0;
705 char cur_action;
706 unsigned long tmp;
707 uint32_t pid, tid;
708 GDBProcess *process;
709 CPUState *cpu;
710 GDBThreadIdKind kind;
711 unsigned int max_cpus = gdb_get_max_cpus();
712 /* uninitialised CPUs stay 0 */
713 g_autofree char *newstates = g_new0(char, max_cpus);
715 /* mark valid CPUs with 1 */
716 CPU_FOREACH(cpu) {
717 newstates[cpu->cpu_index] = 1;
721 * res keeps track of what error we are returning, with -ENOTSUP meaning
722 * that the command is unknown or unsupported, thus returning an empty
723 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
724 * or incorrect parameters passed.
726 res = 0;
729 * target_count and last_target keep track of how many CPUs we are going to
730 * step or resume, and a pointer to the state structure of one of them,
731 * respectively
733 int target_count = 0;
734 CPUState *last_target = NULL;
736 while (*p) {
737 if (*p++ != ';') {
738 return -ENOTSUP;
741 cur_action = *p++;
742 if (cur_action == 'C' || cur_action == 'S') {
743 cur_action = qemu_tolower(cur_action);
744 res = qemu_strtoul(p, &p, 16, &tmp);
745 if (res) {
746 return res;
748 signal = gdb_signal_to_target(tmp);
749 } else if (cur_action != 'c' && cur_action != 's') {
750 /* unknown/invalid/unsupported command */
751 return -ENOTSUP;
754 if (*p == '\0' || *p == ';') {
756 * No thread specifier, action is on "all threads". The
757 * specification is unclear regarding the process to act on. We
758 * choose all processes.
760 kind = GDB_ALL_PROCESSES;
761 } else if (*p++ == ':') {
762 kind = read_thread_id(p, &p, &pid, &tid);
763 } else {
764 return -ENOTSUP;
767 switch (kind) {
768 case GDB_READ_THREAD_ERR:
769 return -EINVAL;
771 case GDB_ALL_PROCESSES:
772 cpu = gdb_first_attached_cpu();
773 while (cpu) {
774 if (newstates[cpu->cpu_index] == 1) {
775 newstates[cpu->cpu_index] = cur_action;
777 target_count++;
778 last_target = cpu;
781 cpu = gdb_next_attached_cpu(cpu);
783 break;
785 case GDB_ALL_THREADS:
786 process = gdb_get_process(pid);
788 if (!process->attached) {
789 return -EINVAL;
792 cpu = gdb_get_first_cpu_in_process(process);
793 while (cpu) {
794 if (newstates[cpu->cpu_index] == 1) {
795 newstates[cpu->cpu_index] = cur_action;
797 target_count++;
798 last_target = cpu;
801 cpu = gdb_next_cpu_in_process(cpu);
803 break;
805 case GDB_ONE_THREAD:
806 cpu = gdb_get_cpu(pid, tid);
808 /* invalid CPU/thread specified */
809 if (!cpu) {
810 return -EINVAL;
813 /* only use if no previous match occourred */
814 if (newstates[cpu->cpu_index] == 1) {
815 newstates[cpu->cpu_index] = cur_action;
817 target_count++;
818 last_target = cpu;
820 break;
825 * if we're about to resume a specific set of CPUs/threads, make it so that
826 * in case execution gets interrupted, we can send GDB a stop reply with a
827 * correct value. it doesn't really matter which CPU we tell GDB the signal
828 * happened in (VM pauses stop all of them anyway), so long as it is one of
829 * the ones we resumed/single stepped here.
831 if (target_count > 0) {
832 gdbserver_state.c_cpu = last_target;
835 gdbserver_state.signal = signal;
836 gdb_continue_partial(newstates);
837 return res;
840 static const char *cmd_next_param(const char *param, const char delimiter)
842 static const char all_delimiters[] = ",;:=";
843 char curr_delimiters[2] = {0};
844 const char *delimiters;
846 if (delimiter == '?') {
847 delimiters = all_delimiters;
848 } else if (delimiter == '0') {
849 return strchr(param, '\0');
850 } else if (delimiter == '.' && *param) {
851 return param + 1;
852 } else {
853 curr_delimiters[0] = delimiter;
854 delimiters = curr_delimiters;
857 param += strcspn(param, delimiters);
858 if (*param) {
859 param++;
861 return param;
864 static int cmd_parse_params(const char *data, const char *schema,
865 GArray *params)
867 const char *curr_schema, *curr_data;
869 g_assert(schema);
870 g_assert(params->len == 0);
872 curr_schema = schema;
873 curr_data = data;
874 while (curr_schema[0] && curr_schema[1] && *curr_data) {
875 GdbCmdVariant this_param;
877 switch (curr_schema[0]) {
878 case 'l':
879 if (qemu_strtoul(curr_data, &curr_data, 16,
880 &this_param.val_ul)) {
881 return -EINVAL;
883 curr_data = cmd_next_param(curr_data, curr_schema[1]);
884 g_array_append_val(params, this_param);
885 break;
886 case 'L':
887 if (qemu_strtou64(curr_data, &curr_data, 16,
888 (uint64_t *)&this_param.val_ull)) {
889 return -EINVAL;
891 curr_data = cmd_next_param(curr_data, curr_schema[1]);
892 g_array_append_val(params, this_param);
893 break;
894 case 's':
895 this_param.data = curr_data;
896 curr_data = cmd_next_param(curr_data, curr_schema[1]);
897 g_array_append_val(params, this_param);
898 break;
899 case 'o':
900 this_param.opcode = *(uint8_t *)curr_data;
901 curr_data = cmd_next_param(curr_data, curr_schema[1]);
902 g_array_append_val(params, this_param);
903 break;
904 case 't':
905 this_param.thread_id.kind =
906 read_thread_id(curr_data, &curr_data,
907 &this_param.thread_id.pid,
908 &this_param.thread_id.tid);
909 curr_data = cmd_next_param(curr_data, curr_schema[1]);
910 g_array_append_val(params, this_param);
911 break;
912 case '?':
913 curr_data = cmd_next_param(curr_data, curr_schema[1]);
914 break;
915 default:
916 return -EINVAL;
918 curr_schema += 2;
921 return 0;
924 static inline int startswith(const char *string, const char *pattern)
926 return !strncmp(string, pattern, strlen(pattern));
929 static bool process_string_cmd(const char *data,
930 const GdbCmdParseEntry *cmds, int num_cmds)
932 int i;
933 g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant));
935 if (!cmds) {
936 return false;
939 for (i = 0; i < num_cmds; i++) {
940 const GdbCmdParseEntry *cmd = &cmds[i];
941 void *user_ctx = NULL;
942 g_assert(cmd->handler && cmd->cmd);
944 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
945 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
946 continue;
949 if (cmd->schema) {
950 if (cmd_parse_params(&data[strlen(cmd->cmd)],
951 cmd->schema, params)) {
952 return false;
956 if (cmd->need_cpu_context) {
957 user_ctx = (void *)gdbserver_state.g_cpu;
960 gdbserver_state.allow_stop_reply = cmd->allow_stop_reply;
961 cmd->handler(params, user_ctx);
962 return true;
965 return false;
968 static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
970 if (!data) {
971 return;
974 g_string_set_size(gdbserver_state.str_buf, 0);
975 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
977 /* In case there was an error during the command parsing we must
978 * send a NULL packet to indicate the command is not supported */
979 if (!process_string_cmd(data, cmd, 1)) {
980 gdb_put_packet("");
984 static void handle_detach(GArray *params, void *user_ctx)
986 GDBProcess *process;
987 uint32_t pid = 1;
989 if (gdbserver_state.multiprocess) {
990 if (!params->len) {
991 gdb_put_packet("E22");
992 return;
995 pid = gdb_get_cmd_param(params, 0)->val_ul;
998 #ifdef CONFIG_USER_ONLY
999 if (gdb_handle_detach_user(pid)) {
1000 return;
1002 #endif
1004 process = gdb_get_process(pid);
1005 gdb_process_breakpoint_remove_all(process);
1006 process->attached = false;
1008 if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
1009 gdbserver_state.c_cpu = gdb_first_attached_cpu();
1012 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
1013 gdbserver_state.g_cpu = gdb_first_attached_cpu();
1016 if (!gdbserver_state.c_cpu) {
1017 /* No more process attached */
1018 gdb_disable_syscalls();
1019 gdb_continue();
1021 gdb_put_packet("OK");
1024 static void handle_thread_alive(GArray *params, void *user_ctx)
1026 CPUState *cpu;
1028 if (!params->len) {
1029 gdb_put_packet("E22");
1030 return;
1033 if (gdb_get_cmd_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
1034 gdb_put_packet("E22");
1035 return;
1038 cpu = gdb_get_cpu(gdb_get_cmd_param(params, 0)->thread_id.pid,
1039 gdb_get_cmd_param(params, 0)->thread_id.tid);
1040 if (!cpu) {
1041 gdb_put_packet("E22");
1042 return;
1045 gdb_put_packet("OK");
1048 static void handle_continue(GArray *params, void *user_ctx)
1050 if (params->len) {
1051 gdb_set_cpu_pc(gdb_get_cmd_param(params, 0)->val_ull);
1054 gdbserver_state.signal = 0;
1055 gdb_continue();
1058 static void handle_cont_with_sig(GArray *params, void *user_ctx)
1060 unsigned long signal = 0;
1063 * Note: C sig;[addr] is currently unsupported and we simply
1064 * omit the addr parameter
1066 if (params->len) {
1067 signal = gdb_get_cmd_param(params, 0)->val_ul;
1070 gdbserver_state.signal = gdb_signal_to_target(signal);
1071 if (gdbserver_state.signal == -1) {
1072 gdbserver_state.signal = 0;
1074 gdb_continue();
1077 static void handle_set_thread(GArray *params, void *user_ctx)
1079 uint32_t pid, tid;
1080 CPUState *cpu;
1082 if (params->len != 2) {
1083 gdb_put_packet("E22");
1084 return;
1087 if (gdb_get_cmd_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
1088 gdb_put_packet("E22");
1089 return;
1092 if (gdb_get_cmd_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
1093 gdb_put_packet("OK");
1094 return;
1097 pid = gdb_get_cmd_param(params, 1)->thread_id.pid;
1098 tid = gdb_get_cmd_param(params, 1)->thread_id.tid;
1099 #ifdef CONFIG_USER_ONLY
1100 if (gdb_handle_set_thread_user(pid, tid)) {
1101 return;
1103 #endif
1104 cpu = gdb_get_cpu(pid, tid);
1105 if (!cpu) {
1106 gdb_put_packet("E22");
1107 return;
1111 * Note: This command is deprecated and modern gdb's will be using the
1112 * vCont command instead.
1114 switch (gdb_get_cmd_param(params, 0)->opcode) {
1115 case 'c':
1116 gdbserver_state.c_cpu = cpu;
1117 gdb_put_packet("OK");
1118 break;
1119 case 'g':
1120 gdbserver_state.g_cpu = cpu;
1121 gdb_put_packet("OK");
1122 break;
1123 default:
1124 gdb_put_packet("E22");
1125 break;
1129 static void handle_insert_bp(GArray *params, void *user_ctx)
1131 int res;
1133 if (params->len != 3) {
1134 gdb_put_packet("E22");
1135 return;
1138 res = gdb_breakpoint_insert(gdbserver_state.c_cpu,
1139 gdb_get_cmd_param(params, 0)->val_ul,
1140 gdb_get_cmd_param(params, 1)->val_ull,
1141 gdb_get_cmd_param(params, 2)->val_ull);
1142 if (res >= 0) {
1143 gdb_put_packet("OK");
1144 return;
1145 } else if (res == -ENOSYS) {
1146 gdb_put_packet("");
1147 return;
1150 gdb_put_packet("E22");
1153 static void handle_remove_bp(GArray *params, void *user_ctx)
1155 int res;
1157 if (params->len != 3) {
1158 gdb_put_packet("E22");
1159 return;
1162 res = gdb_breakpoint_remove(gdbserver_state.c_cpu,
1163 gdb_get_cmd_param(params, 0)->val_ul,
1164 gdb_get_cmd_param(params, 1)->val_ull,
1165 gdb_get_cmd_param(params, 2)->val_ull);
1166 if (res >= 0) {
1167 gdb_put_packet("OK");
1168 return;
1169 } else if (res == -ENOSYS) {
1170 gdb_put_packet("");
1171 return;
1174 gdb_put_packet("E22");
1178 * handle_set/get_reg
1180 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1181 * This works, but can be very slow. Anything new enough to understand
1182 * XML also knows how to use this properly. However to use this we
1183 * need to define a local XML file as well as be talking to a
1184 * reasonably modern gdb. Responding with an empty packet will cause
1185 * the remote gdb to fallback to older methods.
1188 static void handle_set_reg(GArray *params, void *user_ctx)
1190 int reg_size;
1192 if (params->len != 2) {
1193 gdb_put_packet("E22");
1194 return;
1197 reg_size = strlen(gdb_get_cmd_param(params, 1)->data) / 2;
1198 gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 1)->data, reg_size);
1199 gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
1200 gdb_get_cmd_param(params, 0)->val_ull);
1201 gdb_put_packet("OK");
1204 static void handle_get_reg(GArray *params, void *user_ctx)
1206 int reg_size;
1208 if (!params->len) {
1209 gdb_put_packet("E14");
1210 return;
1213 reg_size = gdb_read_register(gdbserver_state.g_cpu,
1214 gdbserver_state.mem_buf,
1215 gdb_get_cmd_param(params, 0)->val_ull);
1216 if (!reg_size) {
1217 gdb_put_packet("E14");
1218 return;
1219 } else {
1220 g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
1223 gdb_memtohex(gdbserver_state.str_buf,
1224 gdbserver_state.mem_buf->data, reg_size);
1225 gdb_put_strbuf();
1228 static void handle_write_mem(GArray *params, void *user_ctx)
1230 if (params->len != 3) {
1231 gdb_put_packet("E22");
1232 return;
1235 /* gdb_hextomem() reads 2*len bytes */
1236 if (gdb_get_cmd_param(params, 1)->val_ull >
1237 strlen(gdb_get_cmd_param(params, 2)->data) / 2) {
1238 gdb_put_packet("E22");
1239 return;
1242 gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 2)->data,
1243 gdb_get_cmd_param(params, 1)->val_ull);
1244 if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
1245 gdb_get_cmd_param(params, 0)->val_ull,
1246 gdbserver_state.mem_buf->data,
1247 gdbserver_state.mem_buf->len, true)) {
1248 gdb_put_packet("E14");
1249 return;
1252 gdb_put_packet("OK");
1255 static void handle_read_mem(GArray *params, void *user_ctx)
1257 if (params->len != 2) {
1258 gdb_put_packet("E22");
1259 return;
1262 /* gdb_memtohex() doubles the required space */
1263 if (gdb_get_cmd_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) {
1264 gdb_put_packet("E22");
1265 return;
1268 g_byte_array_set_size(gdbserver_state.mem_buf,
1269 gdb_get_cmd_param(params, 1)->val_ull);
1271 if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
1272 gdb_get_cmd_param(params, 0)->val_ull,
1273 gdbserver_state.mem_buf->data,
1274 gdbserver_state.mem_buf->len, false)) {
1275 gdb_put_packet("E14");
1276 return;
1279 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
1280 gdbserver_state.mem_buf->len);
1281 gdb_put_strbuf();
1284 static void handle_write_all_regs(GArray *params, void *user_ctx)
1286 int reg_id;
1287 size_t len;
1288 uint8_t *registers;
1289 int reg_size;
1291 if (!params->len) {
1292 return;
1295 cpu_synchronize_state(gdbserver_state.g_cpu);
1296 len = strlen(gdb_get_cmd_param(params, 0)->data) / 2;
1297 gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 0)->data, len);
1298 registers = gdbserver_state.mem_buf->data;
1299 for (reg_id = 0;
1300 reg_id < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
1301 reg_id++) {
1302 reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, reg_id);
1303 len -= reg_size;
1304 registers += reg_size;
1306 gdb_put_packet("OK");
1309 static void handle_read_all_regs(GArray *params, void *user_ctx)
1311 int reg_id;
1312 size_t len;
1314 cpu_synchronize_state(gdbserver_state.g_cpu);
1315 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
1316 len = 0;
1317 for (reg_id = 0; reg_id < gdbserver_state.g_cpu->gdb_num_g_regs; reg_id++) {
1318 len += gdb_read_register(gdbserver_state.g_cpu,
1319 gdbserver_state.mem_buf,
1320 reg_id);
1322 g_assert(len == gdbserver_state.mem_buf->len);
1324 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
1325 gdb_put_strbuf();
1329 static void handle_step(GArray *params, void *user_ctx)
1331 if (params->len) {
1332 gdb_set_cpu_pc(gdb_get_cmd_param(params, 0)->val_ull);
1335 cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
1336 gdb_continue();
1339 static void handle_backward(GArray *params, void *user_ctx)
1341 if (!gdb_can_reverse()) {
1342 gdb_put_packet("E22");
1344 if (params->len == 1) {
1345 switch (gdb_get_cmd_param(params, 0)->opcode) {
1346 case 's':
1347 if (replay_reverse_step()) {
1348 gdb_continue();
1349 } else {
1350 gdb_put_packet("E14");
1352 return;
1353 case 'c':
1354 if (replay_reverse_continue()) {
1355 gdb_continue();
1356 } else {
1357 gdb_put_packet("E14");
1359 return;
1363 /* Default invalid command */
1364 gdb_put_packet("");
1367 static void handle_v_cont_query(GArray *params, void *user_ctx)
1369 gdb_put_packet("vCont;c;C;s;S");
1372 static void handle_v_cont(GArray *params, void *user_ctx)
1374 int res;
1376 if (!params->len) {
1377 return;
1380 res = gdb_handle_vcont(gdb_get_cmd_param(params, 0)->data);
1381 if ((res == -EINVAL) || (res == -ERANGE)) {
1382 gdb_put_packet("E22");
1383 } else if (res) {
1384 gdb_put_packet("");
1388 static void handle_v_attach(GArray *params, void *user_ctx)
1390 GDBProcess *process;
1391 CPUState *cpu;
1393 g_string_assign(gdbserver_state.str_buf, "E22");
1394 if (!params->len) {
1395 goto cleanup;
1398 process = gdb_get_process(gdb_get_cmd_param(params, 0)->val_ul);
1399 if (!process) {
1400 goto cleanup;
1403 cpu = gdb_get_first_cpu_in_process(process);
1404 if (!cpu) {
1405 goto cleanup;
1408 process->attached = true;
1409 gdbserver_state.g_cpu = cpu;
1410 gdbserver_state.c_cpu = cpu;
1412 if (gdbserver_state.allow_stop_reply) {
1413 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1414 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1415 g_string_append_c(gdbserver_state.str_buf, ';');
1416 gdbserver_state.allow_stop_reply = false;
1417 cleanup:
1418 gdb_put_strbuf();
1422 static void handle_v_kill(GArray *params, void *user_ctx)
1424 /* Kill the target */
1425 gdb_put_packet("OK");
1426 error_report("QEMU: Terminated via GDBstub");
1427 gdb_exit(0);
1428 gdb_qemu_exit(0);
1431 static const GdbCmdParseEntry gdb_v_commands_table[] = {
1432 /* Order is important if has same prefix */
1434 .handler = handle_v_cont_query,
1435 .cmd = "Cont?",
1436 .cmd_startswith = true
1439 .handler = handle_v_cont,
1440 .cmd = "Cont",
1441 .cmd_startswith = true,
1442 .allow_stop_reply = true,
1443 .schema = "s0"
1446 .handler = handle_v_attach,
1447 .cmd = "Attach;",
1448 .cmd_startswith = true,
1449 .allow_stop_reply = true,
1450 .schema = "l0"
1453 .handler = handle_v_kill,
1454 .cmd = "Kill;",
1455 .cmd_startswith = true
1457 #ifdef CONFIG_USER_ONLY
1459 * Host I/O Packets. See [1] for details.
1460 * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html
1463 .handler = gdb_handle_v_file_open,
1464 .cmd = "File:open:",
1465 .cmd_startswith = true,
1466 .schema = "s,L,L0"
1469 .handler = gdb_handle_v_file_close,
1470 .cmd = "File:close:",
1471 .cmd_startswith = true,
1472 .schema = "l0"
1475 .handler = gdb_handle_v_file_pread,
1476 .cmd = "File:pread:",
1477 .cmd_startswith = true,
1478 .schema = "l,L,L0"
1481 .handler = gdb_handle_v_file_readlink,
1482 .cmd = "File:readlink:",
1483 .cmd_startswith = true,
1484 .schema = "s0"
1486 #endif
1489 static void handle_v_commands(GArray *params, void *user_ctx)
1491 if (!params->len) {
1492 return;
1495 if (!process_string_cmd(gdb_get_cmd_param(params, 0)->data,
1496 gdb_v_commands_table,
1497 ARRAY_SIZE(gdb_v_commands_table))) {
1498 gdb_put_packet("");
1502 static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx)
1504 g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE);
1506 if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) {
1507 g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x",
1508 SSTEP_NOIRQ);
1511 if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) {
1512 g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x",
1513 SSTEP_NOTIMER);
1516 gdb_put_strbuf();
1519 static void handle_set_qemu_sstep(GArray *params, void *user_ctx)
1521 int new_sstep_flags;
1523 if (!params->len) {
1524 return;
1527 new_sstep_flags = gdb_get_cmd_param(params, 0)->val_ul;
1529 if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) {
1530 gdb_put_packet("E22");
1531 return;
1534 gdbserver_state.sstep_flags = new_sstep_flags;
1535 gdb_put_packet("OK");
1538 static void handle_query_qemu_sstep(GArray *params, void *user_ctx)
1540 g_string_printf(gdbserver_state.str_buf, "0x%x",
1541 gdbserver_state.sstep_flags);
1542 gdb_put_strbuf();
1545 static void handle_query_curr_tid(GArray *params, void *user_ctx)
1547 CPUState *cpu;
1548 GDBProcess *process;
1551 * "Current thread" remains vague in the spec, so always return
1552 * the first thread of the current process (gdb returns the
1553 * first thread).
1555 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
1556 cpu = gdb_get_first_cpu_in_process(process);
1557 g_string_assign(gdbserver_state.str_buf, "QC");
1558 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1559 gdb_put_strbuf();
1562 static void handle_query_threads(GArray *params, void *user_ctx)
1564 if (!gdbserver_state.query_cpu) {
1565 gdb_put_packet("l");
1566 return;
1569 g_string_assign(gdbserver_state.str_buf, "m");
1570 gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
1571 gdb_put_strbuf();
1572 gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
1575 static void handle_query_first_threads(GArray *params, void *user_ctx)
1577 gdbserver_state.query_cpu = gdb_first_attached_cpu();
1578 handle_query_threads(params, user_ctx);
1581 static void handle_query_thread_extra(GArray *params, void *user_ctx)
1583 g_autoptr(GString) rs = g_string_new(NULL);
1584 CPUState *cpu;
1586 if (!params->len ||
1587 gdb_get_cmd_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
1588 gdb_put_packet("E22");
1589 return;
1592 cpu = gdb_get_cpu(gdb_get_cmd_param(params, 0)->thread_id.pid,
1593 gdb_get_cmd_param(params, 0)->thread_id.tid);
1594 if (!cpu) {
1595 return;
1598 cpu_synchronize_state(cpu);
1600 if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
1601 /* Print the CPU model and name in multiprocess mode */
1602 ObjectClass *oc = object_get_class(OBJECT(cpu));
1603 const char *cpu_model = object_class_get_name(oc);
1604 const char *cpu_name =
1605 object_get_canonical_path_component(OBJECT(cpu));
1606 g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
1607 cpu->halted ? "halted " : "running");
1608 } else {
1609 g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
1610 cpu->halted ? "halted " : "running");
1612 trace_gdbstub_op_extra_info(rs->str);
1613 gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
1614 gdb_put_strbuf();
1617 static char *extended_qsupported_features;
1618 void gdb_extend_qsupported_features(char *qsupported_features)
1621 * We don't support different sets of CPU gdb features on different CPUs yet
1622 * so assert the feature strings are the same on all CPUs, or is set only
1623 * once (1 CPU).
1625 g_assert(extended_qsupported_features == NULL ||
1626 g_strcmp0(extended_qsupported_features, qsupported_features) == 0);
1628 extended_qsupported_features = qsupported_features;
1631 static void handle_query_supported(GArray *params, void *user_ctx)
1633 CPUClass *cc;
1635 g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
1636 cc = CPU_GET_CLASS(first_cpu);
1637 if (cc->gdb_core_xml_file) {
1638 g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
1641 if (gdb_can_reverse()) {
1642 g_string_append(gdbserver_state.str_buf,
1643 ";ReverseStep+;ReverseContinue+");
1646 #if defined(CONFIG_USER_ONLY)
1647 #if defined(CONFIG_LINUX)
1648 if (get_task_state(gdbserver_state.c_cpu)) {
1649 g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
1651 g_string_append(gdbserver_state.str_buf, ";QCatchSyscalls+");
1653 g_string_append(gdbserver_state.str_buf, ";qXfer:siginfo:read+");
1654 #endif
1655 g_string_append(gdbserver_state.str_buf, ";qXfer:exec-file:read+");
1656 #endif
1658 if (params->len) {
1659 const char *gdb_supported = gdb_get_cmd_param(params, 0)->data;
1661 if (strstr(gdb_supported, "multiprocess+")) {
1662 gdbserver_state.multiprocess = true;
1664 #if defined(CONFIG_USER_ONLY)
1665 gdb_handle_query_supported_user(gdb_supported);
1666 #endif
1669 g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
1671 if (extended_qsupported_features) {
1672 g_string_append(gdbserver_state.str_buf, extended_qsupported_features);
1675 gdb_put_strbuf();
1678 static void handle_query_xfer_features(GArray *params, void *user_ctx)
1680 GDBProcess *process;
1681 CPUClass *cc;
1682 unsigned long len, total_len, addr;
1683 const char *xml;
1684 const char *p;
1686 if (params->len < 3) {
1687 gdb_put_packet("E22");
1688 return;
1691 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
1692 cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
1693 if (!cc->gdb_core_xml_file) {
1694 gdb_put_packet("");
1695 return;
1698 p = gdb_get_cmd_param(params, 0)->data;
1699 xml = get_feature_xml(p, &p, process);
1700 if (!xml) {
1701 gdb_put_packet("E00");
1702 return;
1705 addr = gdb_get_cmd_param(params, 1)->val_ul;
1706 len = gdb_get_cmd_param(params, 2)->val_ul;
1707 total_len = strlen(xml);
1708 if (addr > total_len) {
1709 gdb_put_packet("E00");
1710 return;
1713 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
1714 len = (MAX_PACKET_LENGTH - 5) / 2;
1717 if (len < total_len - addr) {
1718 g_string_assign(gdbserver_state.str_buf, "m");
1719 gdb_memtox(gdbserver_state.str_buf, xml + addr, len);
1720 } else {
1721 g_string_assign(gdbserver_state.str_buf, "l");
1722 gdb_memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
1725 gdb_put_packet_binary(gdbserver_state.str_buf->str,
1726 gdbserver_state.str_buf->len, true);
1729 static void handle_query_qemu_supported(GArray *params, void *user_ctx)
1731 g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
1732 #ifndef CONFIG_USER_ONLY
1733 g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
1734 #endif
1735 gdb_put_strbuf();
1738 static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
1739 /* Order is important if has same prefix */
1741 .handler = handle_query_qemu_sstepbits,
1742 .cmd = "qemu.sstepbits",
1745 .handler = handle_query_qemu_sstep,
1746 .cmd = "qemu.sstep",
1749 .handler = handle_set_qemu_sstep,
1750 .cmd = "qemu.sstep=",
1751 .cmd_startswith = true,
1752 .schema = "l0"
1756 /* Compares if a set of command parsers is equal to another set of parsers. */
1757 static bool cmp_cmds(GdbCmdParseEntry *c, GdbCmdParseEntry *d, int size)
1759 for (int i = 0; i < size; i++) {
1760 if (!(c[i].handler == d[i].handler &&
1761 g_strcmp0(c[i].cmd, d[i].cmd) == 0 &&
1762 c[i].cmd_startswith == d[i].cmd_startswith &&
1763 g_strcmp0(c[i].schema, d[i].schema) == 0)) {
1765 /* Sets are different. */
1766 return false;
1770 /* Sets are equal, i.e. contain the same command parsers. */
1771 return true;
1774 static GdbCmdParseEntry *extended_query_table;
1775 static int extended_query_table_size;
1776 void gdb_extend_query_table(GdbCmdParseEntry *table, int size)
1779 * We don't support different sets of CPU gdb features on different CPUs yet
1780 * so assert query table is the same on all CPUs, or is set only once
1781 * (1 CPU).
1783 g_assert(extended_query_table == NULL ||
1784 (extended_query_table_size == size &&
1785 cmp_cmds(extended_query_table, table, size)));
1787 extended_query_table = table;
1788 extended_query_table_size = size;
1791 static const GdbCmdParseEntry gdb_gen_query_table[] = {
1793 .handler = handle_query_curr_tid,
1794 .cmd = "C",
1797 .handler = handle_query_threads,
1798 .cmd = "sThreadInfo",
1801 .handler = handle_query_first_threads,
1802 .cmd = "fThreadInfo",
1805 .handler = handle_query_thread_extra,
1806 .cmd = "ThreadExtraInfo,",
1807 .cmd_startswith = true,
1808 .schema = "t0"
1810 #ifdef CONFIG_USER_ONLY
1812 .handler = gdb_handle_query_offsets,
1813 .cmd = "Offsets",
1815 #else
1817 .handler = gdb_handle_query_rcmd,
1818 .cmd = "Rcmd,",
1819 .cmd_startswith = true,
1820 .schema = "s0"
1822 #endif
1824 .handler = handle_query_supported,
1825 .cmd = "Supported:",
1826 .cmd_startswith = true,
1827 .schema = "s0"
1830 .handler = handle_query_supported,
1831 .cmd = "Supported",
1832 .schema = "s0"
1835 .handler = handle_query_xfer_features,
1836 .cmd = "Xfer:features:read:",
1837 .cmd_startswith = true,
1838 .schema = "s:l,l0"
1840 #if defined(CONFIG_USER_ONLY)
1841 #if defined(CONFIG_LINUX)
1843 .handler = gdb_handle_query_xfer_auxv,
1844 .cmd = "Xfer:auxv:read::",
1845 .cmd_startswith = true,
1846 .schema = "l,l0"
1849 .handler = gdb_handle_query_xfer_siginfo,
1850 .cmd = "Xfer:siginfo:read::",
1851 .cmd_startswith = true,
1852 .schema = "l,l0"
1854 #endif
1856 .handler = gdb_handle_query_xfer_exec_file,
1857 .cmd = "Xfer:exec-file:read:",
1858 .cmd_startswith = true,
1859 .schema = "l:l,l0"
1861 #endif
1863 .handler = gdb_handle_query_attached,
1864 .cmd = "Attached:",
1865 .cmd_startswith = true
1868 .handler = gdb_handle_query_attached,
1869 .cmd = "Attached",
1872 .handler = handle_query_qemu_supported,
1873 .cmd = "qemu.Supported",
1875 #ifndef CONFIG_USER_ONLY
1877 .handler = gdb_handle_query_qemu_phy_mem_mode,
1878 .cmd = "qemu.PhyMemMode",
1880 #endif
1883 static GdbCmdParseEntry *extended_set_table;
1884 static int extended_set_table_size;
1885 void gdb_extend_set_table(GdbCmdParseEntry *table, int size)
1888 * We don't support different sets of CPU gdb features on different CPUs yet
1889 * so assert set table is the same on all CPUs, or is set only once (1 CPU).
1891 g_assert(extended_set_table == NULL ||
1892 (extended_set_table_size == size &&
1893 cmp_cmds(extended_set_table, table, size)));
1895 extended_set_table = table;
1896 extended_set_table_size = size;
1899 static const GdbCmdParseEntry gdb_gen_set_table[] = {
1900 /* Order is important if has same prefix */
1902 .handler = handle_set_qemu_sstep,
1903 .cmd = "qemu.sstep:",
1904 .cmd_startswith = true,
1905 .schema = "l0"
1907 #ifndef CONFIG_USER_ONLY
1909 .handler = gdb_handle_set_qemu_phy_mem_mode,
1910 .cmd = "qemu.PhyMemMode:",
1911 .cmd_startswith = true,
1912 .schema = "l0"
1914 #endif
1915 #if defined(CONFIG_USER_ONLY)
1917 .handler = gdb_handle_set_catch_syscalls,
1918 .cmd = "CatchSyscalls:",
1919 .cmd_startswith = true,
1920 .schema = "s0",
1922 #endif
1925 static void handle_gen_query(GArray *params, void *user_ctx)
1927 if (!params->len) {
1928 return;
1931 if (process_string_cmd(gdb_get_cmd_param(params, 0)->data,
1932 gdb_gen_query_set_common_table,
1933 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
1934 return;
1937 if (process_string_cmd(gdb_get_cmd_param(params, 0)->data,
1938 gdb_gen_query_table,
1939 ARRAY_SIZE(gdb_gen_query_table))) {
1940 return;
1943 if (extended_query_table &&
1944 process_string_cmd(gdb_get_cmd_param(params, 0)->data,
1945 extended_query_table,
1946 extended_query_table_size)) {
1947 return;
1950 /* Can't handle query, return Empty response. */
1951 gdb_put_packet("");
1954 static void handle_gen_set(GArray *params, void *user_ctx)
1956 if (!params->len) {
1957 return;
1960 if (process_string_cmd(gdb_get_cmd_param(params, 0)->data,
1961 gdb_gen_query_set_common_table,
1962 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
1963 return;
1966 if (process_string_cmd(gdb_get_cmd_param(params, 0)->data,
1967 gdb_gen_set_table,
1968 ARRAY_SIZE(gdb_gen_set_table))) {
1969 return;
1972 if (extended_set_table &&
1973 process_string_cmd(gdb_get_cmd_param(params, 0)->data,
1974 extended_set_table,
1975 extended_set_table_size)) {
1976 return;
1979 /* Can't handle set, return Empty response. */
1980 gdb_put_packet("");
1983 static void handle_target_halt(GArray *params, void *user_ctx)
1985 if (gdbserver_state.allow_stop_reply) {
1986 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1987 gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
1988 g_string_append_c(gdbserver_state.str_buf, ';');
1989 gdb_put_strbuf();
1990 gdbserver_state.allow_stop_reply = false;
1993 * Remove all the breakpoints when this query is issued,
1994 * because gdb is doing an initial connect and the state
1995 * should be cleaned up.
1997 gdb_breakpoint_remove_all(gdbserver_state.c_cpu);
2000 static int gdb_handle_packet(const char *line_buf)
2002 const GdbCmdParseEntry *cmd_parser = NULL;
2004 trace_gdbstub_io_command(line_buf);
2006 switch (line_buf[0]) {
2007 case '!':
2008 gdb_put_packet("OK");
2009 break;
2010 case '?':
2012 static const GdbCmdParseEntry target_halted_cmd_desc = {
2013 .handler = handle_target_halt,
2014 .cmd = "?",
2015 .cmd_startswith = true,
2016 .allow_stop_reply = true,
2018 cmd_parser = &target_halted_cmd_desc;
2020 break;
2021 case 'c':
2023 static const GdbCmdParseEntry continue_cmd_desc = {
2024 .handler = handle_continue,
2025 .cmd = "c",
2026 .cmd_startswith = true,
2027 .allow_stop_reply = true,
2028 .schema = "L0"
2030 cmd_parser = &continue_cmd_desc;
2032 break;
2033 case 'C':
2035 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2036 .handler = handle_cont_with_sig,
2037 .cmd = "C",
2038 .cmd_startswith = true,
2039 .allow_stop_reply = true,
2040 .schema = "l0"
2042 cmd_parser = &cont_with_sig_cmd_desc;
2044 break;
2045 case 'v':
2047 static const GdbCmdParseEntry v_cmd_desc = {
2048 .handler = handle_v_commands,
2049 .cmd = "v",
2050 .cmd_startswith = true,
2051 .schema = "s0"
2053 cmd_parser = &v_cmd_desc;
2055 break;
2056 case 'k':
2057 /* Kill the target */
2058 error_report("QEMU: Terminated via GDBstub");
2059 gdb_exit(0);
2060 gdb_qemu_exit(0);
2061 break;
2062 case 'D':
2064 static const GdbCmdParseEntry detach_cmd_desc = {
2065 .handler = handle_detach,
2066 .cmd = "D",
2067 .cmd_startswith = true,
2068 .schema = "?.l0"
2070 cmd_parser = &detach_cmd_desc;
2072 break;
2073 case 's':
2075 static const GdbCmdParseEntry step_cmd_desc = {
2076 .handler = handle_step,
2077 .cmd = "s",
2078 .cmd_startswith = true,
2079 .allow_stop_reply = true,
2080 .schema = "L0"
2082 cmd_parser = &step_cmd_desc;
2084 break;
2085 case 'b':
2087 static const GdbCmdParseEntry backward_cmd_desc = {
2088 .handler = handle_backward,
2089 .cmd = "b",
2090 .cmd_startswith = true,
2091 .allow_stop_reply = true,
2092 .schema = "o0"
2094 cmd_parser = &backward_cmd_desc;
2096 break;
2097 case 'F':
2099 static const GdbCmdParseEntry file_io_cmd_desc = {
2100 .handler = gdb_handle_file_io,
2101 .cmd = "F",
2102 .cmd_startswith = true,
2103 .schema = "L,L,o0"
2105 cmd_parser = &file_io_cmd_desc;
2107 break;
2108 case 'g':
2110 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2111 .handler = handle_read_all_regs,
2112 .cmd = "g",
2113 .cmd_startswith = true
2115 cmd_parser = &read_all_regs_cmd_desc;
2117 break;
2118 case 'G':
2120 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2121 .handler = handle_write_all_regs,
2122 .cmd = "G",
2123 .cmd_startswith = true,
2124 .schema = "s0"
2126 cmd_parser = &write_all_regs_cmd_desc;
2128 break;
2129 case 'm':
2131 static const GdbCmdParseEntry read_mem_cmd_desc = {
2132 .handler = handle_read_mem,
2133 .cmd = "m",
2134 .cmd_startswith = true,
2135 .schema = "L,L0"
2137 cmd_parser = &read_mem_cmd_desc;
2139 break;
2140 case 'M':
2142 static const GdbCmdParseEntry write_mem_cmd_desc = {
2143 .handler = handle_write_mem,
2144 .cmd = "M",
2145 .cmd_startswith = true,
2146 .schema = "L,L:s0"
2148 cmd_parser = &write_mem_cmd_desc;
2150 break;
2151 case 'p':
2153 static const GdbCmdParseEntry get_reg_cmd_desc = {
2154 .handler = handle_get_reg,
2155 .cmd = "p",
2156 .cmd_startswith = true,
2157 .schema = "L0"
2159 cmd_parser = &get_reg_cmd_desc;
2161 break;
2162 case 'P':
2164 static const GdbCmdParseEntry set_reg_cmd_desc = {
2165 .handler = handle_set_reg,
2166 .cmd = "P",
2167 .cmd_startswith = true,
2168 .schema = "L?s0"
2170 cmd_parser = &set_reg_cmd_desc;
2172 break;
2173 case 'Z':
2175 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2176 .handler = handle_insert_bp,
2177 .cmd = "Z",
2178 .cmd_startswith = true,
2179 .schema = "l?L?L0"
2181 cmd_parser = &insert_bp_cmd_desc;
2183 break;
2184 case 'z':
2186 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2187 .handler = handle_remove_bp,
2188 .cmd = "z",
2189 .cmd_startswith = true,
2190 .schema = "l?L?L0"
2192 cmd_parser = &remove_bp_cmd_desc;
2194 break;
2195 case 'H':
2197 static const GdbCmdParseEntry set_thread_cmd_desc = {
2198 .handler = handle_set_thread,
2199 .cmd = "H",
2200 .cmd_startswith = true,
2201 .schema = "o.t0"
2203 cmd_parser = &set_thread_cmd_desc;
2205 break;
2206 case 'T':
2208 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2209 .handler = handle_thread_alive,
2210 .cmd = "T",
2211 .cmd_startswith = true,
2212 .schema = "t0"
2214 cmd_parser = &thread_alive_cmd_desc;
2216 break;
2217 case 'q':
2219 static const GdbCmdParseEntry gen_query_cmd_desc = {
2220 .handler = handle_gen_query,
2221 .cmd = "q",
2222 .cmd_startswith = true,
2223 .schema = "s0"
2225 cmd_parser = &gen_query_cmd_desc;
2227 break;
2228 case 'Q':
2230 static const GdbCmdParseEntry gen_set_cmd_desc = {
2231 .handler = handle_gen_set,
2232 .cmd = "Q",
2233 .cmd_startswith = true,
2234 .schema = "s0"
2236 cmd_parser = &gen_set_cmd_desc;
2238 break;
2239 default:
2240 /* put empty packet */
2241 gdb_put_packet("");
2242 break;
2245 if (cmd_parser) {
2246 run_cmd_parser(line_buf, cmd_parser);
2249 return RS_IDLE;
2252 void gdb_set_stop_cpu(CPUState *cpu)
2254 GDBProcess *p = gdb_get_cpu_process(cpu);
2256 if (!p->attached) {
2258 * Having a stop CPU corresponding to a process that is not attached
2259 * confuses GDB. So we ignore the request.
2261 return;
2264 gdbserver_state.c_cpu = cpu;
2265 gdbserver_state.g_cpu = cpu;
2268 void gdb_read_byte(uint8_t ch)
2270 uint8_t reply;
2272 gdbserver_state.allow_stop_reply = false;
2273 #ifndef CONFIG_USER_ONLY
2274 if (gdbserver_state.last_packet->len) {
2275 /* Waiting for a response to the last packet. If we see the start
2276 of a new command then abandon the previous response. */
2277 if (ch == '-') {
2278 trace_gdbstub_err_got_nack();
2279 gdb_put_buffer(gdbserver_state.last_packet->data,
2280 gdbserver_state.last_packet->len);
2281 } else if (ch == '+') {
2282 trace_gdbstub_io_got_ack();
2283 } else {
2284 trace_gdbstub_io_got_unexpected(ch);
2287 if (ch == '+' || ch == '$') {
2288 g_byte_array_set_size(gdbserver_state.last_packet, 0);
2290 if (ch != '$')
2291 return;
2293 if (runstate_is_running()) {
2295 * When the CPU is running, we cannot do anything except stop
2296 * it when receiving a char. This is expected on a Ctrl-C in the
2297 * gdb client. Because we are in all-stop mode, gdb sends a
2298 * 0x03 byte which is not a usual packet, so we handle it specially
2299 * here, but it does expect a stop reply.
2301 if (ch != 0x03) {
2302 trace_gdbstub_err_unexpected_runpkt(ch);
2303 } else {
2304 gdbserver_state.allow_stop_reply = true;
2306 vm_stop(RUN_STATE_PAUSED);
2307 } else
2308 #endif
2310 switch(gdbserver_state.state) {
2311 case RS_IDLE:
2312 if (ch == '$') {
2313 /* start of command packet */
2314 gdbserver_state.line_buf_index = 0;
2315 gdbserver_state.line_sum = 0;
2316 gdbserver_state.state = RS_GETLINE;
2317 } else if (ch == '+') {
2319 * do nothing, gdb may preemptively send out ACKs on
2320 * initial connection
2322 } else {
2323 trace_gdbstub_err_garbage(ch);
2325 break;
2326 case RS_GETLINE:
2327 if (ch == '}') {
2328 /* start escape sequence */
2329 gdbserver_state.state = RS_GETLINE_ESC;
2330 gdbserver_state.line_sum += ch;
2331 } else if (ch == '*') {
2332 /* start run length encoding sequence */
2333 gdbserver_state.state = RS_GETLINE_RLE;
2334 gdbserver_state.line_sum += ch;
2335 } else if (ch == '#') {
2336 /* end of command, start of checksum*/
2337 gdbserver_state.state = RS_CHKSUM1;
2338 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
2339 trace_gdbstub_err_overrun();
2340 gdbserver_state.state = RS_IDLE;
2341 } else {
2342 /* unescaped command character */
2343 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
2344 gdbserver_state.line_sum += ch;
2346 break;
2347 case RS_GETLINE_ESC:
2348 if (ch == '#') {
2349 /* unexpected end of command in escape sequence */
2350 gdbserver_state.state = RS_CHKSUM1;
2351 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
2352 /* command buffer overrun */
2353 trace_gdbstub_err_overrun();
2354 gdbserver_state.state = RS_IDLE;
2355 } else {
2356 /* parse escaped character and leave escape state */
2357 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
2358 gdbserver_state.line_sum += ch;
2359 gdbserver_state.state = RS_GETLINE;
2361 break;
2362 case RS_GETLINE_RLE:
2364 * Run-length encoding is explained in "Debugging with GDB /
2365 * Appendix E GDB Remote Serial Protocol / Overview".
2367 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
2368 /* invalid RLE count encoding */
2369 trace_gdbstub_err_invalid_repeat(ch);
2370 gdbserver_state.state = RS_GETLINE;
2371 } else {
2372 /* decode repeat length */
2373 int repeat = ch - ' ' + 3;
2374 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
2375 /* that many repeats would overrun the command buffer */
2376 trace_gdbstub_err_overrun();
2377 gdbserver_state.state = RS_IDLE;
2378 } else if (gdbserver_state.line_buf_index < 1) {
2379 /* got a repeat but we have nothing to repeat */
2380 trace_gdbstub_err_invalid_rle();
2381 gdbserver_state.state = RS_GETLINE;
2382 } else {
2383 /* repeat the last character */
2384 memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
2385 gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
2386 gdbserver_state.line_buf_index += repeat;
2387 gdbserver_state.line_sum += ch;
2388 gdbserver_state.state = RS_GETLINE;
2391 break;
2392 case RS_CHKSUM1:
2393 /* get high hex digit of checksum */
2394 if (!isxdigit(ch)) {
2395 trace_gdbstub_err_checksum_invalid(ch);
2396 gdbserver_state.state = RS_GETLINE;
2397 break;
2399 gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
2400 gdbserver_state.line_csum = fromhex(ch) << 4;
2401 gdbserver_state.state = RS_CHKSUM2;
2402 break;
2403 case RS_CHKSUM2:
2404 /* get low hex digit of checksum */
2405 if (!isxdigit(ch)) {
2406 trace_gdbstub_err_checksum_invalid(ch);
2407 gdbserver_state.state = RS_GETLINE;
2408 break;
2410 gdbserver_state.line_csum |= fromhex(ch);
2412 if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
2413 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
2414 /* send NAK reply */
2415 reply = '-';
2416 gdb_put_buffer(&reply, 1);
2417 gdbserver_state.state = RS_IDLE;
2418 } else {
2419 /* send ACK reply */
2420 reply = '+';
2421 gdb_put_buffer(&reply, 1);
2422 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
2424 break;
2425 default:
2426 abort();
2432 * Create the process that will contain all the "orphan" CPUs (that are not
2433 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2434 * be attachable and thus will be invisible to the user.
2436 void gdb_create_default_process(GDBState *s)
2438 GDBProcess *process;
2439 int pid;
2441 #ifdef CONFIG_USER_ONLY
2442 assert(gdbserver_state.process_num == 0);
2443 pid = getpid();
2444 #else
2445 if (gdbserver_state.process_num) {
2446 pid = s->processes[s->process_num - 1].pid;
2447 } else {
2448 pid = 0;
2450 /* We need an available PID slot for this process */
2451 assert(pid < UINT32_MAX);
2452 pid++;
2453 #endif
2455 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2456 process = &s->processes[s->process_num - 1];
2457 process->pid = pid;
2458 process->attached = false;
2459 process->target_xml = NULL;