OSDN Git Service

62608a5389e12f04f79859c2383c311b913af18d
[qmiga/qemu.git] / gdbstub / gdbstub.c
1 /*
2  * gdb server stub
3  *
4  * This implements a subset of the remote protocol as described in:
5  *
6  *   https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
7  *
8  * Copyright (c) 2003-2005 Fabrice Bellard
9  *
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
21  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22  *
23  * SPDX-License-Identifier: LGPL-2.0+
24  */
25
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/syscalls.h"
34 #ifdef CONFIG_USER_ONLY
35 #include "gdbstub/user.h"
36 #else
37 #include "hw/cpu/cluster.h"
38 #include "hw/boards.h"
39 #endif
40
41 #include "sysemu/hw_accel.h"
42 #include "sysemu/runstate.h"
43 #include "exec/replay-core.h"
44 #include "exec/hwaddr.h"
45
46 #include "internals.h"
47
48 typedef struct GDBRegisterState {
49     int base_reg;
50     int num_regs;
51     gdb_get_reg_cb get_reg;
52     gdb_set_reg_cb set_reg;
53     const char *xml;
54     struct GDBRegisterState *next;
55 } GDBRegisterState;
56
57 GDBState gdbserver_state;
58
59 void gdb_init_gdbserver_state(void)
60 {
61     g_assert(!gdbserver_state.init);
62     memset(&gdbserver_state, 0, sizeof(GDBState));
63     gdbserver_state.init = true;
64     gdbserver_state.str_buf = g_string_new(NULL);
65     gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
66     gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4);
67
68     /*
69      * What single-step modes are supported is accelerator dependent.
70      * By default try to use no IRQs and no timers while single
71      * stepping so as to make single stepping like a typical ICE HW step.
72      */
73     gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags();
74     gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
75     gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags;
76 }
77
78 /* writes 2*len+1 bytes in buf */
79 void gdb_memtohex(GString *buf, const uint8_t *mem, int len)
80 {
81     int i, c;
82     for(i = 0; i < len; i++) {
83         c = mem[i];
84         g_string_append_c(buf, tohex(c >> 4));
85         g_string_append_c(buf, tohex(c & 0xf));
86     }
87     g_string_append_c(buf, '\0');
88 }
89
90 void gdb_hextomem(GByteArray *mem, const char *buf, int len)
91 {
92     int i;
93
94     for(i = 0; i < len; i++) {
95         guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
96         g_byte_array_append(mem, &byte, 1);
97         buf += 2;
98     }
99 }
100
101 static void hexdump(const char *buf, int len,
102                     void (*trace_fn)(size_t ofs, char const *text))
103 {
104     char line_buffer[3 * 16 + 4 + 16 + 1];
105
106     size_t i;
107     for (i = 0; i < len || (i & 0xF); ++i) {
108         size_t byte_ofs = i & 15;
109
110         if (byte_ofs == 0) {
111             memset(line_buffer, ' ', 3 * 16 + 4 + 16);
112             line_buffer[3 * 16 + 4 + 16] = 0;
113         }
114
115         size_t col_group = (i >> 2) & 3;
116         size_t hex_col = byte_ofs * 3 + col_group;
117         size_t txt_col = 3 * 16 + 4 + byte_ofs;
118
119         if (i < len) {
120             char value = buf[i];
121
122             line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
123             line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
124             line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
125                     ? value
126                     : '.';
127         }
128
129         if (byte_ofs == 0xF)
130             trace_fn(i & -16, line_buffer);
131     }
132 }
133
134 /* return -1 if error, 0 if OK */
135 int gdb_put_packet_binary(const char *buf, int len, bool dump)
136 {
137     int csum, i;
138     uint8_t footer[3];
139
140     if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
141         hexdump(buf, len, trace_gdbstub_io_binaryreply);
142     }
143
144     for(;;) {
145         g_byte_array_set_size(gdbserver_state.last_packet, 0);
146         g_byte_array_append(gdbserver_state.last_packet,
147                             (const uint8_t *) "$", 1);
148         g_byte_array_append(gdbserver_state.last_packet,
149                             (const uint8_t *) buf, len);
150         csum = 0;
151         for(i = 0; i < len; i++) {
152             csum += buf[i];
153         }
154         footer[0] = '#';
155         footer[1] = tohex((csum >> 4) & 0xf);
156         footer[2] = tohex((csum) & 0xf);
157         g_byte_array_append(gdbserver_state.last_packet, footer, 3);
158
159         gdb_put_buffer(gdbserver_state.last_packet->data,
160                    gdbserver_state.last_packet->len);
161
162         if (gdb_got_immediate_ack()) {
163             break;
164         }
165     }
166     return 0;
167 }
168
169 /* return -1 if error, 0 if OK */
170 int gdb_put_packet(const char *buf)
171 {
172     trace_gdbstub_io_reply(buf);
173
174     return gdb_put_packet_binary(buf, strlen(buf), false);
175 }
176
177 void gdb_put_strbuf(void)
178 {
179     gdb_put_packet(gdbserver_state.str_buf->str);
180 }
181
182 /* Encode data using the encoding for 'x' packets.  */
183 void gdb_memtox(GString *buf, const char *mem, int len)
184 {
185     char c;
186
187     while (len--) {
188         c = *(mem++);
189         switch (c) {
190         case '#': case '$': case '*': case '}':
191             g_string_append_c(buf, '}');
192             g_string_append_c(buf, c ^ 0x20);
193             break;
194         default:
195             g_string_append_c(buf, c);
196             break;
197         }
198     }
199 }
200
201 static uint32_t gdb_get_cpu_pid(CPUState *cpu)
202 {
203 #ifdef CONFIG_USER_ONLY
204     return getpid();
205 #else
206     if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
207         /* Return the default process' PID */
208         int index = gdbserver_state.process_num - 1;
209         return gdbserver_state.processes[index].pid;
210     }
211     return cpu->cluster_index + 1;
212 #endif
213 }
214
215 GDBProcess *gdb_get_process(uint32_t pid)
216 {
217     int i;
218
219     if (!pid) {
220         /* 0 means any process, we take the first one */
221         return &gdbserver_state.processes[0];
222     }
223
224     for (i = 0; i < gdbserver_state.process_num; i++) {
225         if (gdbserver_state.processes[i].pid == pid) {
226             return &gdbserver_state.processes[i];
227         }
228     }
229
230     return NULL;
231 }
232
233 static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
234 {
235     return gdb_get_process(gdb_get_cpu_pid(cpu));
236 }
237
238 static CPUState *find_cpu(uint32_t thread_id)
239 {
240     CPUState *cpu;
241
242     CPU_FOREACH(cpu) {
243         if (gdb_get_cpu_index(cpu) == thread_id) {
244             return cpu;
245         }
246     }
247
248     return NULL;
249 }
250
251 CPUState *gdb_get_first_cpu_in_process(GDBProcess *process)
252 {
253     CPUState *cpu;
254
255     CPU_FOREACH(cpu) {
256         if (gdb_get_cpu_pid(cpu) == process->pid) {
257             return cpu;
258         }
259     }
260
261     return NULL;
262 }
263
264 static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
265 {
266     uint32_t pid = gdb_get_cpu_pid(cpu);
267     cpu = CPU_NEXT(cpu);
268
269     while (cpu) {
270         if (gdb_get_cpu_pid(cpu) == pid) {
271             break;
272         }
273
274         cpu = CPU_NEXT(cpu);
275     }
276
277     return cpu;
278 }
279
280 /* Return the cpu following @cpu, while ignoring unattached processes. */
281 static CPUState *gdb_next_attached_cpu(CPUState *cpu)
282 {
283     cpu = CPU_NEXT(cpu);
284
285     while (cpu) {
286         if (gdb_get_cpu_process(cpu)->attached) {
287             break;
288         }
289
290         cpu = CPU_NEXT(cpu);
291     }
292
293     return cpu;
294 }
295
296 /* Return the first attached cpu */
297 CPUState *gdb_first_attached_cpu(void)
298 {
299     CPUState *cpu = first_cpu;
300     GDBProcess *process = gdb_get_cpu_process(cpu);
301
302     if (!process->attached) {
303         return gdb_next_attached_cpu(cpu);
304     }
305
306     return cpu;
307 }
308
309 static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
310 {
311     GDBProcess *process;
312     CPUState *cpu;
313
314     if (!pid && !tid) {
315         /* 0 means any process/thread, we take the first attached one */
316         return gdb_first_attached_cpu();
317     } else if (pid && !tid) {
318         /* any thread in a specific process */
319         process = gdb_get_process(pid);
320
321         if (process == NULL) {
322             return NULL;
323         }
324
325         if (!process->attached) {
326             return NULL;
327         }
328
329         return gdb_get_first_cpu_in_process(process);
330     } else {
331         /* a specific thread */
332         cpu = find_cpu(tid);
333
334         if (cpu == NULL) {
335             return NULL;
336         }
337
338         process = gdb_get_cpu_process(cpu);
339
340         if (pid && process->pid != pid) {
341             return NULL;
342         }
343
344         if (!process->attached) {
345             return NULL;
346         }
347
348         return cpu;
349     }
350 }
351
352 static const char *get_feature_xml(const char *p, const char **newp,
353                                    GDBProcess *process)
354 {
355     CPUState *cpu = gdb_get_first_cpu_in_process(process);
356     CPUClass *cc = CPU_GET_CLASS(cpu);
357     size_t len;
358
359     /*
360      * qXfer:features:read:ANNEX:OFFSET,LENGTH'
361      *                     ^p    ^newp
362      */
363     char *term = strchr(p, ':');
364     *newp = term + 1;
365     len = term - p;
366
367     /* Is it the main target xml? */
368     if (strncmp(p, "target.xml", len) == 0) {
369         if (!process->target_xml) {
370             GDBRegisterState *r;
371             g_autoptr(GPtrArray) xml = g_ptr_array_new_with_free_func(g_free);
372
373             g_ptr_array_add(
374                 xml,
375                 g_strdup("<?xml version=\"1.0\"?>"
376                          "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
377                          "<target>"));
378
379             if (cc->gdb_arch_name) {
380                 g_ptr_array_add(
381                     xml,
382                     g_markup_printf_escaped("<architecture>%s</architecture>",
383                                             cc->gdb_arch_name(cpu)));
384             }
385             g_ptr_array_add(
386                 xml,
387                 g_markup_printf_escaped("<xi:include href=\"%s\"/>",
388                                         cc->gdb_core_xml_file));
389             for (r = cpu->gdb_regs; r; r = r->next) {
390                 g_ptr_array_add(
391                     xml,
392                     g_markup_printf_escaped("<xi:include href=\"%s\"/>",
393                                             r->xml));
394             }
395             g_ptr_array_add(xml, g_strdup("</target>"));
396             g_ptr_array_add(xml, NULL);
397
398             process->target_xml = g_strjoinv(NULL, (void *)xml->pdata);
399         }
400         return process->target_xml;
401     }
402     /* Is it dynamically generated by the target? */
403     if (cc->gdb_get_dynamic_xml) {
404         g_autofree char *xmlname = g_strndup(p, len);
405         const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
406         if (xml) {
407             return xml;
408         }
409     }
410     /* Is it one of the encoded gdb-xml/ files? */
411     for (int i = 0; gdb_static_features[i].xmlname; i++) {
412         const char *name = gdb_static_features[i].xmlname;
413         if ((strncmp(name, p, len) == 0) &&
414             strlen(name) == len) {
415             return gdb_static_features[i].xml;
416         }
417     }
418
419     /* failed */
420     return NULL;
421 }
422
423 static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
424 {
425     CPUClass *cc = CPU_GET_CLASS(cpu);
426     CPUArchState *env = cpu_env(cpu);
427     GDBRegisterState *r;
428
429     if (reg < cc->gdb_num_core_regs) {
430         return cc->gdb_read_register(cpu, buf, reg);
431     }
432
433     for (r = cpu->gdb_regs; r; r = r->next) {
434         if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
435             return r->get_reg(env, buf, reg - r->base_reg);
436         }
437     }
438     return 0;
439 }
440
441 static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
442 {
443     CPUClass *cc = CPU_GET_CLASS(cpu);
444     CPUArchState *env = cpu_env(cpu);
445     GDBRegisterState *r;
446
447     if (reg < cc->gdb_num_core_regs) {
448         return cc->gdb_write_register(cpu, mem_buf, reg);
449     }
450
451     for (r = cpu->gdb_regs; r; r = r->next) {
452         if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
453             return r->set_reg(env, mem_buf, reg - r->base_reg);
454         }
455     }
456     return 0;
457 }
458
459 void gdb_register_coprocessor(CPUState *cpu,
460                               gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
461                               int num_regs, const char *xml, int g_pos)
462 {
463     GDBRegisterState *s;
464     GDBRegisterState **p;
465
466     p = &cpu->gdb_regs;
467     while (*p) {
468         /* Check for duplicates.  */
469         if (strcmp((*p)->xml, xml) == 0)
470             return;
471         p = &(*p)->next;
472     }
473
474     s = g_new0(GDBRegisterState, 1);
475     s->base_reg = cpu->gdb_num_regs;
476     s->num_regs = num_regs;
477     s->get_reg = get_reg;
478     s->set_reg = set_reg;
479     s->xml = xml;
480
481     /* Add to end of list.  */
482     cpu->gdb_num_regs += num_regs;
483     *p = s;
484     if (g_pos) {
485         if (g_pos != s->base_reg) {
486             error_report("Error: Bad gdb register numbering for '%s', "
487                          "expected %d got %d", xml, g_pos, s->base_reg);
488         } else {
489             cpu->gdb_num_g_regs = cpu->gdb_num_regs;
490         }
491     }
492 }
493
494 static void gdb_process_breakpoint_remove_all(GDBProcess *p)
495 {
496     CPUState *cpu = gdb_get_first_cpu_in_process(p);
497
498     while (cpu) {
499         gdb_breakpoint_remove_all(cpu);
500         cpu = gdb_next_cpu_in_process(cpu);
501     }
502 }
503
504
505 static void gdb_set_cpu_pc(vaddr pc)
506 {
507     CPUState *cpu = gdbserver_state.c_cpu;
508
509     cpu_synchronize_state(cpu);
510     cpu_set_pc(cpu, pc);
511 }
512
513 void gdb_append_thread_id(CPUState *cpu, GString *buf)
514 {
515     if (gdbserver_state.multiprocess) {
516         g_string_append_printf(buf, "p%02x.%02x",
517                                gdb_get_cpu_pid(cpu), gdb_get_cpu_index(cpu));
518     } else {
519         g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu));
520     }
521 }
522
523 static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
524                                       uint32_t *pid, uint32_t *tid)
525 {
526     unsigned long p, t;
527     int ret;
528
529     if (*buf == 'p') {
530         buf++;
531         ret = qemu_strtoul(buf, &buf, 16, &p);
532
533         if (ret) {
534             return GDB_READ_THREAD_ERR;
535         }
536
537         /* Skip '.' */
538         buf++;
539     } else {
540         p = 0;
541     }
542
543     ret = qemu_strtoul(buf, &buf, 16, &t);
544
545     if (ret) {
546         return GDB_READ_THREAD_ERR;
547     }
548
549     *end_buf = buf;
550
551     if (p == -1) {
552         return GDB_ALL_PROCESSES;
553     }
554
555     if (pid) {
556         *pid = p;
557     }
558
559     if (t == -1) {
560         return GDB_ALL_THREADS;
561     }
562
563     if (tid) {
564         *tid = t;
565     }
566
567     return GDB_ONE_THREAD;
568 }
569
570 /**
571  * gdb_handle_vcont - Parses and handles a vCont packet.
572  * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
573  *         a format error, 0 on success.
574  */
575 static int gdb_handle_vcont(const char *p)
576 {
577     int res, signal = 0;
578     char cur_action;
579     unsigned long tmp;
580     uint32_t pid, tid;
581     GDBProcess *process;
582     CPUState *cpu;
583     GDBThreadIdKind kind;
584     unsigned int max_cpus = gdb_get_max_cpus();
585     /* uninitialised CPUs stay 0 */
586     g_autofree char *newstates = g_new0(char, max_cpus);
587
588     /* mark valid CPUs with 1 */
589     CPU_FOREACH(cpu) {
590         newstates[cpu->cpu_index] = 1;
591     }
592
593     /*
594      * res keeps track of what error we are returning, with -ENOTSUP meaning
595      * that the command is unknown or unsupported, thus returning an empty
596      * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
597      *  or incorrect parameters passed.
598      */
599     res = 0;
600
601     /*
602      * target_count and last_target keep track of how many CPUs we are going to
603      * step or resume, and a pointer to the state structure of one of them,
604      * respectivelly
605      */
606     int target_count = 0;
607     CPUState *last_target = NULL;
608
609     while (*p) {
610         if (*p++ != ';') {
611             return -ENOTSUP;
612         }
613
614         cur_action = *p++;
615         if (cur_action == 'C' || cur_action == 'S') {
616             cur_action = qemu_tolower(cur_action);
617             res = qemu_strtoul(p, &p, 16, &tmp);
618             if (res) {
619                 return res;
620             }
621             signal = gdb_signal_to_target(tmp);
622         } else if (cur_action != 'c' && cur_action != 's') {
623             /* unknown/invalid/unsupported command */
624             return -ENOTSUP;
625         }
626
627         if (*p == '\0' || *p == ';') {
628             /*
629              * No thread specifier, action is on "all threads". The
630              * specification is unclear regarding the process to act on. We
631              * choose all processes.
632              */
633             kind = GDB_ALL_PROCESSES;
634         } else if (*p++ == ':') {
635             kind = read_thread_id(p, &p, &pid, &tid);
636         } else {
637             return -ENOTSUP;
638         }
639
640         switch (kind) {
641         case GDB_READ_THREAD_ERR:
642             return -EINVAL;
643
644         case GDB_ALL_PROCESSES:
645             cpu = gdb_first_attached_cpu();
646             while (cpu) {
647                 if (newstates[cpu->cpu_index] == 1) {
648                     newstates[cpu->cpu_index] = cur_action;
649
650                     target_count++;
651                     last_target = cpu;
652                 }
653
654                 cpu = gdb_next_attached_cpu(cpu);
655             }
656             break;
657
658         case GDB_ALL_THREADS:
659             process = gdb_get_process(pid);
660
661             if (!process->attached) {
662                 return -EINVAL;
663             }
664
665             cpu = gdb_get_first_cpu_in_process(process);
666             while (cpu) {
667                 if (newstates[cpu->cpu_index] == 1) {
668                     newstates[cpu->cpu_index] = cur_action;
669
670                     target_count++;
671                     last_target = cpu;
672                 }
673
674                 cpu = gdb_next_cpu_in_process(cpu);
675             }
676             break;
677
678         case GDB_ONE_THREAD:
679             cpu = gdb_get_cpu(pid, tid);
680
681             /* invalid CPU/thread specified */
682             if (!cpu) {
683                 return -EINVAL;
684             }
685
686             /* only use if no previous match occourred */
687             if (newstates[cpu->cpu_index] == 1) {
688                 newstates[cpu->cpu_index] = cur_action;
689
690                 target_count++;
691                 last_target = cpu;
692             }
693             break;
694         }
695     }
696
697     /*
698      * if we're about to resume a specific set of CPUs/threads, make it so that
699      * in case execution gets interrupted, we can send GDB a stop reply with a
700      * correct value. it doesn't really matter which CPU we tell GDB the signal
701      * happened in (VM pauses stop all of them anyway), so long as it is one of
702      * the ones we resumed/single stepped here.
703      */
704     if (target_count > 0) {
705         gdbserver_state.c_cpu = last_target;
706     }
707
708     gdbserver_state.signal = signal;
709     gdb_continue_partial(newstates);
710     return res;
711 }
712
713 static const char *cmd_next_param(const char *param, const char delimiter)
714 {
715     static const char all_delimiters[] = ",;:=";
716     char curr_delimiters[2] = {0};
717     const char *delimiters;
718
719     if (delimiter == '?') {
720         delimiters = all_delimiters;
721     } else if (delimiter == '0') {
722         return strchr(param, '\0');
723     } else if (delimiter == '.' && *param) {
724         return param + 1;
725     } else {
726         curr_delimiters[0] = delimiter;
727         delimiters = curr_delimiters;
728     }
729
730     param += strcspn(param, delimiters);
731     if (*param) {
732         param++;
733     }
734     return param;
735 }
736
737 static int cmd_parse_params(const char *data, const char *schema,
738                             GArray *params)
739 {
740     const char *curr_schema, *curr_data;
741
742     g_assert(schema);
743     g_assert(params->len == 0);
744
745     curr_schema = schema;
746     curr_data = data;
747     while (curr_schema[0] && curr_schema[1] && *curr_data) {
748         GdbCmdVariant this_param;
749
750         switch (curr_schema[0]) {
751         case 'l':
752             if (qemu_strtoul(curr_data, &curr_data, 16,
753                              &this_param.val_ul)) {
754                 return -EINVAL;
755             }
756             curr_data = cmd_next_param(curr_data, curr_schema[1]);
757             g_array_append_val(params, this_param);
758             break;
759         case 'L':
760             if (qemu_strtou64(curr_data, &curr_data, 16,
761                               (uint64_t *)&this_param.val_ull)) {
762                 return -EINVAL;
763             }
764             curr_data = cmd_next_param(curr_data, curr_schema[1]);
765             g_array_append_val(params, this_param);
766             break;
767         case 's':
768             this_param.data = curr_data;
769             curr_data = cmd_next_param(curr_data, curr_schema[1]);
770             g_array_append_val(params, this_param);
771             break;
772         case 'o':
773             this_param.opcode = *(uint8_t *)curr_data;
774             curr_data = cmd_next_param(curr_data, curr_schema[1]);
775             g_array_append_val(params, this_param);
776             break;
777         case 't':
778             this_param.thread_id.kind =
779                 read_thread_id(curr_data, &curr_data,
780                                &this_param.thread_id.pid,
781                                &this_param.thread_id.tid);
782             curr_data = cmd_next_param(curr_data, curr_schema[1]);
783             g_array_append_val(params, this_param);
784             break;
785         case '?':
786             curr_data = cmd_next_param(curr_data, curr_schema[1]);
787             break;
788         default:
789             return -EINVAL;
790         }
791         curr_schema += 2;
792     }
793
794     return 0;
795 }
796
797 typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx);
798
799 /*
800  * cmd_startswith -> cmd is compared using startswith
801  *
802  * allow_stop_reply -> true iff the gdbstub can respond to this command with a
803  *   "stop reply" packet. The list of commands that accept such response is
804  *   defined at the GDB Remote Serial Protocol documentation. see:
805  *   https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.
806  *
807  * schema definitions:
808  * Each schema parameter entry consists of 2 chars,
809  * the first char represents the parameter type handling
810  * the second char represents the delimiter for the next parameter
811  *
812  * Currently supported schema types:
813  * 'l' -> unsigned long (stored in .val_ul)
814  * 'L' -> unsigned long long (stored in .val_ull)
815  * 's' -> string (stored in .data)
816  * 'o' -> single char (stored in .opcode)
817  * 't' -> thread id (stored in .thread_id)
818  * '?' -> skip according to delimiter
819  *
820  * Currently supported delimiters:
821  * '?' -> Stop at any delimiter (",;:=\0")
822  * '0' -> Stop at "\0"
823  * '.' -> Skip 1 char unless reached "\0"
824  * Any other value is treated as the delimiter value itself
825  */
826 typedef struct GdbCmdParseEntry {
827     GdbCmdHandler handler;
828     const char *cmd;
829     bool cmd_startswith;
830     const char *schema;
831     bool allow_stop_reply;
832 } GdbCmdParseEntry;
833
834 static inline int startswith(const char *string, const char *pattern)
835 {
836   return !strncmp(string, pattern, strlen(pattern));
837 }
838
839 static int process_string_cmd(const char *data,
840                               const GdbCmdParseEntry *cmds, int num_cmds)
841 {
842     int i;
843     g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant));
844
845     if (!cmds) {
846         return -1;
847     }
848
849     for (i = 0; i < num_cmds; i++) {
850         const GdbCmdParseEntry *cmd = &cmds[i];
851         g_assert(cmd->handler && cmd->cmd);
852
853         if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
854             (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
855             continue;
856         }
857
858         if (cmd->schema) {
859             if (cmd_parse_params(&data[strlen(cmd->cmd)],
860                                  cmd->schema, params)) {
861                 return -1;
862             }
863         }
864
865         gdbserver_state.allow_stop_reply = cmd->allow_stop_reply;
866         cmd->handler(params, NULL);
867         return 0;
868     }
869
870     return -1;
871 }
872
873 static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
874 {
875     if (!data) {
876         return;
877     }
878
879     g_string_set_size(gdbserver_state.str_buf, 0);
880     g_byte_array_set_size(gdbserver_state.mem_buf, 0);
881
882     /* In case there was an error during the command parsing we must
883     * send a NULL packet to indicate the command is not supported */
884     if (process_string_cmd(data, cmd, 1)) {
885         gdb_put_packet("");
886     }
887 }
888
889 static void handle_detach(GArray *params, void *user_ctx)
890 {
891     GDBProcess *process;
892     uint32_t pid = 1;
893
894     if (gdbserver_state.multiprocess) {
895         if (!params->len) {
896             gdb_put_packet("E22");
897             return;
898         }
899
900         pid = get_param(params, 0)->val_ul;
901     }
902
903     process = gdb_get_process(pid);
904     gdb_process_breakpoint_remove_all(process);
905     process->attached = false;
906
907     if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
908         gdbserver_state.c_cpu = gdb_first_attached_cpu();
909     }
910
911     if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
912         gdbserver_state.g_cpu = gdb_first_attached_cpu();
913     }
914
915     if (!gdbserver_state.c_cpu) {
916         /* No more process attached */
917         gdb_disable_syscalls();
918         gdb_continue();
919     }
920     gdb_put_packet("OK");
921 }
922
923 static void handle_thread_alive(GArray *params, void *user_ctx)
924 {
925     CPUState *cpu;
926
927     if (!params->len) {
928         gdb_put_packet("E22");
929         return;
930     }
931
932     if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
933         gdb_put_packet("E22");
934         return;
935     }
936
937     cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
938                       get_param(params, 0)->thread_id.tid);
939     if (!cpu) {
940         gdb_put_packet("E22");
941         return;
942     }
943
944     gdb_put_packet("OK");
945 }
946
947 static void handle_continue(GArray *params, void *user_ctx)
948 {
949     if (params->len) {
950         gdb_set_cpu_pc(get_param(params, 0)->val_ull);
951     }
952
953     gdbserver_state.signal = 0;
954     gdb_continue();
955 }
956
957 static void handle_cont_with_sig(GArray *params, void *user_ctx)
958 {
959     unsigned long signal = 0;
960
961     /*
962      * Note: C sig;[addr] is currently unsupported and we simply
963      *       omit the addr parameter
964      */
965     if (params->len) {
966         signal = get_param(params, 0)->val_ul;
967     }
968
969     gdbserver_state.signal = gdb_signal_to_target(signal);
970     if (gdbserver_state.signal == -1) {
971         gdbserver_state.signal = 0;
972     }
973     gdb_continue();
974 }
975
976 static void handle_set_thread(GArray *params, void *user_ctx)
977 {
978     CPUState *cpu;
979
980     if (params->len != 2) {
981         gdb_put_packet("E22");
982         return;
983     }
984
985     if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
986         gdb_put_packet("E22");
987         return;
988     }
989
990     if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
991         gdb_put_packet("OK");
992         return;
993     }
994
995     cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid,
996                       get_param(params, 1)->thread_id.tid);
997     if (!cpu) {
998         gdb_put_packet("E22");
999         return;
1000     }
1001
1002     /*
1003      * Note: This command is deprecated and modern gdb's will be using the
1004      *       vCont command instead.
1005      */
1006     switch (get_param(params, 0)->opcode) {
1007     case 'c':
1008         gdbserver_state.c_cpu = cpu;
1009         gdb_put_packet("OK");
1010         break;
1011     case 'g':
1012         gdbserver_state.g_cpu = cpu;
1013         gdb_put_packet("OK");
1014         break;
1015     default:
1016         gdb_put_packet("E22");
1017         break;
1018     }
1019 }
1020
1021 static void handle_insert_bp(GArray *params, void *user_ctx)
1022 {
1023     int res;
1024
1025     if (params->len != 3) {
1026         gdb_put_packet("E22");
1027         return;
1028     }
1029
1030     res = gdb_breakpoint_insert(gdbserver_state.c_cpu,
1031                                 get_param(params, 0)->val_ul,
1032                                 get_param(params, 1)->val_ull,
1033                                 get_param(params, 2)->val_ull);
1034     if (res >= 0) {
1035         gdb_put_packet("OK");
1036         return;
1037     } else if (res == -ENOSYS) {
1038         gdb_put_packet("");
1039         return;
1040     }
1041
1042     gdb_put_packet("E22");
1043 }
1044
1045 static void handle_remove_bp(GArray *params, void *user_ctx)
1046 {
1047     int res;
1048
1049     if (params->len != 3) {
1050         gdb_put_packet("E22");
1051         return;
1052     }
1053
1054     res = gdb_breakpoint_remove(gdbserver_state.c_cpu,
1055                                 get_param(params, 0)->val_ul,
1056                                 get_param(params, 1)->val_ull,
1057                                 get_param(params, 2)->val_ull);
1058     if (res >= 0) {
1059         gdb_put_packet("OK");
1060         return;
1061     } else if (res == -ENOSYS) {
1062         gdb_put_packet("");
1063         return;
1064     }
1065
1066     gdb_put_packet("E22");
1067 }
1068
1069 /*
1070  * handle_set/get_reg
1071  *
1072  * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1073  * This works, but can be very slow. Anything new enough to understand
1074  * XML also knows how to use this properly. However to use this we
1075  * need to define a local XML file as well as be talking to a
1076  * reasonably modern gdb. Responding with an empty packet will cause
1077  * the remote gdb to fallback to older methods.
1078  */
1079
1080 static void handle_set_reg(GArray *params, void *user_ctx)
1081 {
1082     int reg_size;
1083
1084     if (params->len != 2) {
1085         gdb_put_packet("E22");
1086         return;
1087     }
1088
1089     reg_size = strlen(get_param(params, 1)->data) / 2;
1090     gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size);
1091     gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
1092                        get_param(params, 0)->val_ull);
1093     gdb_put_packet("OK");
1094 }
1095
1096 static void handle_get_reg(GArray *params, void *user_ctx)
1097 {
1098     int reg_size;
1099
1100     if (!params->len) {
1101         gdb_put_packet("E14");
1102         return;
1103     }
1104
1105     reg_size = gdb_read_register(gdbserver_state.g_cpu,
1106                                  gdbserver_state.mem_buf,
1107                                  get_param(params, 0)->val_ull);
1108     if (!reg_size) {
1109         gdb_put_packet("E14");
1110         return;
1111     } else {
1112         g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
1113     }
1114
1115     gdb_memtohex(gdbserver_state.str_buf,
1116                  gdbserver_state.mem_buf->data, reg_size);
1117     gdb_put_strbuf();
1118 }
1119
1120 static void handle_write_mem(GArray *params, void *user_ctx)
1121 {
1122     if (params->len != 3) {
1123         gdb_put_packet("E22");
1124         return;
1125     }
1126
1127     /* gdb_hextomem() reads 2*len bytes */
1128     if (get_param(params, 1)->val_ull >
1129         strlen(get_param(params, 2)->data) / 2) {
1130         gdb_put_packet("E22");
1131         return;
1132     }
1133
1134     gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data,
1135                  get_param(params, 1)->val_ull);
1136     if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
1137                                    get_param(params, 0)->val_ull,
1138                                    gdbserver_state.mem_buf->data,
1139                                    gdbserver_state.mem_buf->len, true)) {
1140         gdb_put_packet("E14");
1141         return;
1142     }
1143
1144     gdb_put_packet("OK");
1145 }
1146
1147 static void handle_read_mem(GArray *params, void *user_ctx)
1148 {
1149     if (params->len != 2) {
1150         gdb_put_packet("E22");
1151         return;
1152     }
1153
1154     /* gdb_memtohex() doubles the required space */
1155     if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) {
1156         gdb_put_packet("E22");
1157         return;
1158     }
1159
1160     g_byte_array_set_size(gdbserver_state.mem_buf,
1161                           get_param(params, 1)->val_ull);
1162
1163     if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
1164                                    get_param(params, 0)->val_ull,
1165                                    gdbserver_state.mem_buf->data,
1166                                    gdbserver_state.mem_buf->len, false)) {
1167         gdb_put_packet("E14");
1168         return;
1169     }
1170
1171     gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
1172              gdbserver_state.mem_buf->len);
1173     gdb_put_strbuf();
1174 }
1175
1176 static void handle_write_all_regs(GArray *params, void *user_ctx)
1177 {
1178     int reg_id;
1179     size_t len;
1180     uint8_t *registers;
1181     int reg_size;
1182
1183     if (!params->len) {
1184         return;
1185     }
1186
1187     cpu_synchronize_state(gdbserver_state.g_cpu);
1188     len = strlen(get_param(params, 0)->data) / 2;
1189     gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
1190     registers = gdbserver_state.mem_buf->data;
1191     for (reg_id = 0;
1192          reg_id < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
1193          reg_id++) {
1194         reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, reg_id);
1195         len -= reg_size;
1196         registers += reg_size;
1197     }
1198     gdb_put_packet("OK");
1199 }
1200
1201 static void handle_read_all_regs(GArray *params, void *user_ctx)
1202 {
1203     int reg_id;
1204     size_t len;
1205
1206     cpu_synchronize_state(gdbserver_state.g_cpu);
1207     g_byte_array_set_size(gdbserver_state.mem_buf, 0);
1208     len = 0;
1209     for (reg_id = 0; reg_id < gdbserver_state.g_cpu->gdb_num_g_regs; reg_id++) {
1210         len += gdb_read_register(gdbserver_state.g_cpu,
1211                                  gdbserver_state.mem_buf,
1212                                  reg_id);
1213     }
1214     g_assert(len == gdbserver_state.mem_buf->len);
1215
1216     gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
1217     gdb_put_strbuf();
1218 }
1219
1220
1221 static void handle_step(GArray *params, void *user_ctx)
1222 {
1223     if (params->len) {
1224         gdb_set_cpu_pc(get_param(params, 0)->val_ull);
1225     }
1226
1227     cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
1228     gdb_continue();
1229 }
1230
1231 static void handle_backward(GArray *params, void *user_ctx)
1232 {
1233     if (!gdb_can_reverse()) {
1234         gdb_put_packet("E22");
1235     }
1236     if (params->len == 1) {
1237         switch (get_param(params, 0)->opcode) {
1238         case 's':
1239             if (replay_reverse_step()) {
1240                 gdb_continue();
1241             } else {
1242                 gdb_put_packet("E14");
1243             }
1244             return;
1245         case 'c':
1246             if (replay_reverse_continue()) {
1247                 gdb_continue();
1248             } else {
1249                 gdb_put_packet("E14");
1250             }
1251             return;
1252         }
1253     }
1254
1255     /* Default invalid command */
1256     gdb_put_packet("");
1257 }
1258
1259 static void handle_v_cont_query(GArray *params, void *user_ctx)
1260 {
1261     gdb_put_packet("vCont;c;C;s;S");
1262 }
1263
1264 static void handle_v_cont(GArray *params, void *user_ctx)
1265 {
1266     int res;
1267
1268     if (!params->len) {
1269         return;
1270     }
1271
1272     res = gdb_handle_vcont(get_param(params, 0)->data);
1273     if ((res == -EINVAL) || (res == -ERANGE)) {
1274         gdb_put_packet("E22");
1275     } else if (res) {
1276         gdb_put_packet("");
1277     }
1278 }
1279
1280 static void handle_v_attach(GArray *params, void *user_ctx)
1281 {
1282     GDBProcess *process;
1283     CPUState *cpu;
1284
1285     g_string_assign(gdbserver_state.str_buf, "E22");
1286     if (!params->len) {
1287         goto cleanup;
1288     }
1289
1290     process = gdb_get_process(get_param(params, 0)->val_ul);
1291     if (!process) {
1292         goto cleanup;
1293     }
1294
1295     cpu = gdb_get_first_cpu_in_process(process);
1296     if (!cpu) {
1297         goto cleanup;
1298     }
1299
1300     process->attached = true;
1301     gdbserver_state.g_cpu = cpu;
1302     gdbserver_state.c_cpu = cpu;
1303
1304     if (gdbserver_state.allow_stop_reply) {
1305         g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1306         gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1307         g_string_append_c(gdbserver_state.str_buf, ';');
1308         gdbserver_state.allow_stop_reply = false;
1309 cleanup:
1310         gdb_put_strbuf();
1311     }
1312 }
1313
1314 static void handle_v_kill(GArray *params, void *user_ctx)
1315 {
1316     /* Kill the target */
1317     gdb_put_packet("OK");
1318     error_report("QEMU: Terminated via GDBstub");
1319     gdb_exit(0);
1320     exit(0);
1321 }
1322
1323 static const GdbCmdParseEntry gdb_v_commands_table[] = {
1324     /* Order is important if has same prefix */
1325     {
1326         .handler = handle_v_cont_query,
1327         .cmd = "Cont?",
1328         .cmd_startswith = 1
1329     },
1330     {
1331         .handler = handle_v_cont,
1332         .cmd = "Cont",
1333         .cmd_startswith = 1,
1334         .allow_stop_reply = true,
1335         .schema = "s0"
1336     },
1337     {
1338         .handler = handle_v_attach,
1339         .cmd = "Attach;",
1340         .cmd_startswith = 1,
1341         .allow_stop_reply = true,
1342         .schema = "l0"
1343     },
1344     {
1345         .handler = handle_v_kill,
1346         .cmd = "Kill;",
1347         .cmd_startswith = 1
1348     },
1349 #ifdef CONFIG_USER_ONLY
1350     /*
1351      * Host I/O Packets. See [1] for details.
1352      * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html
1353      */
1354     {
1355         .handler = gdb_handle_v_file_open,
1356         .cmd = "File:open:",
1357         .cmd_startswith = 1,
1358         .schema = "s,L,L0"
1359     },
1360     {
1361         .handler = gdb_handle_v_file_close,
1362         .cmd = "File:close:",
1363         .cmd_startswith = 1,
1364         .schema = "l0"
1365     },
1366     {
1367         .handler = gdb_handle_v_file_pread,
1368         .cmd = "File:pread:",
1369         .cmd_startswith = 1,
1370         .schema = "l,L,L0"
1371     },
1372     {
1373         .handler = gdb_handle_v_file_readlink,
1374         .cmd = "File:readlink:",
1375         .cmd_startswith = 1,
1376         .schema = "s0"
1377     },
1378 #endif
1379 };
1380
1381 static void handle_v_commands(GArray *params, void *user_ctx)
1382 {
1383     if (!params->len) {
1384         return;
1385     }
1386
1387     if (process_string_cmd(get_param(params, 0)->data,
1388                            gdb_v_commands_table,
1389                            ARRAY_SIZE(gdb_v_commands_table))) {
1390         gdb_put_packet("");
1391     }
1392 }
1393
1394 static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx)
1395 {
1396     g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE);
1397
1398     if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) {
1399         g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x",
1400                                SSTEP_NOIRQ);
1401     }
1402
1403     if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) {
1404         g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x",
1405                                SSTEP_NOTIMER);
1406     }
1407
1408     gdb_put_strbuf();
1409 }
1410
1411 static void handle_set_qemu_sstep(GArray *params, void *user_ctx)
1412 {
1413     int new_sstep_flags;
1414
1415     if (!params->len) {
1416         return;
1417     }
1418
1419     new_sstep_flags = get_param(params, 0)->val_ul;
1420
1421     if (new_sstep_flags  & ~gdbserver_state.supported_sstep_flags) {
1422         gdb_put_packet("E22");
1423         return;
1424     }
1425
1426     gdbserver_state.sstep_flags = new_sstep_flags;
1427     gdb_put_packet("OK");
1428 }
1429
1430 static void handle_query_qemu_sstep(GArray *params, void *user_ctx)
1431 {
1432     g_string_printf(gdbserver_state.str_buf, "0x%x",
1433                     gdbserver_state.sstep_flags);
1434     gdb_put_strbuf();
1435 }
1436
1437 static void handle_query_curr_tid(GArray *params, void *user_ctx)
1438 {
1439     CPUState *cpu;
1440     GDBProcess *process;
1441
1442     /*
1443      * "Current thread" remains vague in the spec, so always return
1444      * the first thread of the current process (gdb returns the
1445      * first thread).
1446      */
1447     process = gdb_get_cpu_process(gdbserver_state.g_cpu);
1448     cpu = gdb_get_first_cpu_in_process(process);
1449     g_string_assign(gdbserver_state.str_buf, "QC");
1450     gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1451     gdb_put_strbuf();
1452 }
1453
1454 static void handle_query_threads(GArray *params, void *user_ctx)
1455 {
1456     if (!gdbserver_state.query_cpu) {
1457         gdb_put_packet("l");
1458         return;
1459     }
1460
1461     g_string_assign(gdbserver_state.str_buf, "m");
1462     gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
1463     gdb_put_strbuf();
1464     gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
1465 }
1466
1467 static void handle_query_first_threads(GArray *params, void *user_ctx)
1468 {
1469     gdbserver_state.query_cpu = gdb_first_attached_cpu();
1470     handle_query_threads(params, user_ctx);
1471 }
1472
1473 static void handle_query_thread_extra(GArray *params, void *user_ctx)
1474 {
1475     g_autoptr(GString) rs = g_string_new(NULL);
1476     CPUState *cpu;
1477
1478     if (!params->len ||
1479         get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
1480         gdb_put_packet("E22");
1481         return;
1482     }
1483
1484     cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
1485                       get_param(params, 0)->thread_id.tid);
1486     if (!cpu) {
1487         return;
1488     }
1489
1490     cpu_synchronize_state(cpu);
1491
1492     if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
1493         /* Print the CPU model and name in multiprocess mode */
1494         ObjectClass *oc = object_get_class(OBJECT(cpu));
1495         const char *cpu_model = object_class_get_name(oc);
1496         const char *cpu_name =
1497             object_get_canonical_path_component(OBJECT(cpu));
1498         g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
1499                         cpu->halted ? "halted " : "running");
1500     } else {
1501         g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
1502                         cpu->halted ? "halted " : "running");
1503     }
1504     trace_gdbstub_op_extra_info(rs->str);
1505     gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
1506     gdb_put_strbuf();
1507 }
1508
1509 static void handle_query_supported(GArray *params, void *user_ctx)
1510 {
1511     CPUClass *cc;
1512
1513     g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
1514     cc = CPU_GET_CLASS(first_cpu);
1515     if (cc->gdb_core_xml_file) {
1516         g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
1517     }
1518
1519     if (gdb_can_reverse()) {
1520         g_string_append(gdbserver_state.str_buf,
1521             ";ReverseStep+;ReverseContinue+");
1522     }
1523
1524 #if defined(CONFIG_USER_ONLY)
1525 #if defined(CONFIG_LINUX)
1526     if (gdbserver_state.c_cpu->opaque) {
1527         g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
1528     }
1529 #endif
1530     g_string_append(gdbserver_state.str_buf, ";qXfer:exec-file:read+");
1531 #endif
1532
1533     if (params->len &&
1534         strstr(get_param(params, 0)->data, "multiprocess+")) {
1535         gdbserver_state.multiprocess = true;
1536     }
1537
1538     g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
1539     gdb_put_strbuf();
1540 }
1541
1542 static void handle_query_xfer_features(GArray *params, void *user_ctx)
1543 {
1544     GDBProcess *process;
1545     CPUClass *cc;
1546     unsigned long len, total_len, addr;
1547     const char *xml;
1548     const char *p;
1549
1550     if (params->len < 3) {
1551         gdb_put_packet("E22");
1552         return;
1553     }
1554
1555     process = gdb_get_cpu_process(gdbserver_state.g_cpu);
1556     cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
1557     if (!cc->gdb_core_xml_file) {
1558         gdb_put_packet("");
1559         return;
1560     }
1561
1562     p = get_param(params, 0)->data;
1563     xml = get_feature_xml(p, &p, process);
1564     if (!xml) {
1565         gdb_put_packet("E00");
1566         return;
1567     }
1568
1569     addr = get_param(params, 1)->val_ul;
1570     len = get_param(params, 2)->val_ul;
1571     total_len = strlen(xml);
1572     if (addr > total_len) {
1573         gdb_put_packet("E00");
1574         return;
1575     }
1576
1577     if (len > (MAX_PACKET_LENGTH - 5) / 2) {
1578         len = (MAX_PACKET_LENGTH - 5) / 2;
1579     }
1580
1581     if (len < total_len - addr) {
1582         g_string_assign(gdbserver_state.str_buf, "m");
1583         gdb_memtox(gdbserver_state.str_buf, xml + addr, len);
1584     } else {
1585         g_string_assign(gdbserver_state.str_buf, "l");
1586         gdb_memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
1587     }
1588
1589     gdb_put_packet_binary(gdbserver_state.str_buf->str,
1590                       gdbserver_state.str_buf->len, true);
1591 }
1592
1593 static void handle_query_qemu_supported(GArray *params, void *user_ctx)
1594 {
1595     g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
1596 #ifndef CONFIG_USER_ONLY
1597     g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
1598 #endif
1599     gdb_put_strbuf();
1600 }
1601
1602 static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
1603     /* Order is important if has same prefix */
1604     {
1605         .handler = handle_query_qemu_sstepbits,
1606         .cmd = "qemu.sstepbits",
1607     },
1608     {
1609         .handler = handle_query_qemu_sstep,
1610         .cmd = "qemu.sstep",
1611     },
1612     {
1613         .handler = handle_set_qemu_sstep,
1614         .cmd = "qemu.sstep=",
1615         .cmd_startswith = 1,
1616         .schema = "l0"
1617     },
1618 };
1619
1620 static const GdbCmdParseEntry gdb_gen_query_table[] = {
1621     {
1622         .handler = handle_query_curr_tid,
1623         .cmd = "C",
1624     },
1625     {
1626         .handler = handle_query_threads,
1627         .cmd = "sThreadInfo",
1628     },
1629     {
1630         .handler = handle_query_first_threads,
1631         .cmd = "fThreadInfo",
1632     },
1633     {
1634         .handler = handle_query_thread_extra,
1635         .cmd = "ThreadExtraInfo,",
1636         .cmd_startswith = 1,
1637         .schema = "t0"
1638     },
1639 #ifdef CONFIG_USER_ONLY
1640     {
1641         .handler = gdb_handle_query_offsets,
1642         .cmd = "Offsets",
1643     },
1644 #else
1645     {
1646         .handler = gdb_handle_query_rcmd,
1647         .cmd = "Rcmd,",
1648         .cmd_startswith = 1,
1649         .schema = "s0"
1650     },
1651 #endif
1652     {
1653         .handler = handle_query_supported,
1654         .cmd = "Supported:",
1655         .cmd_startswith = 1,
1656         .schema = "s0"
1657     },
1658     {
1659         .handler = handle_query_supported,
1660         .cmd = "Supported",
1661         .schema = "s0"
1662     },
1663     {
1664         .handler = handle_query_xfer_features,
1665         .cmd = "Xfer:features:read:",
1666         .cmd_startswith = 1,
1667         .schema = "s:l,l0"
1668     },
1669 #if defined(CONFIG_USER_ONLY)
1670 #if defined(CONFIG_LINUX)
1671     {
1672         .handler = gdb_handle_query_xfer_auxv,
1673         .cmd = "Xfer:auxv:read::",
1674         .cmd_startswith = 1,
1675         .schema = "l,l0"
1676     },
1677 #endif
1678     {
1679         .handler = gdb_handle_query_xfer_exec_file,
1680         .cmd = "Xfer:exec-file:read:",
1681         .cmd_startswith = 1,
1682         .schema = "l:l,l0"
1683     },
1684 #endif
1685     {
1686         .handler = gdb_handle_query_attached,
1687         .cmd = "Attached:",
1688         .cmd_startswith = 1
1689     },
1690     {
1691         .handler = gdb_handle_query_attached,
1692         .cmd = "Attached",
1693     },
1694     {
1695         .handler = handle_query_qemu_supported,
1696         .cmd = "qemu.Supported",
1697     },
1698 #ifndef CONFIG_USER_ONLY
1699     {
1700         .handler = gdb_handle_query_qemu_phy_mem_mode,
1701         .cmd = "qemu.PhyMemMode",
1702     },
1703 #endif
1704 };
1705
1706 static const GdbCmdParseEntry gdb_gen_set_table[] = {
1707     /* Order is important if has same prefix */
1708     {
1709         .handler = handle_set_qemu_sstep,
1710         .cmd = "qemu.sstep:",
1711         .cmd_startswith = 1,
1712         .schema = "l0"
1713     },
1714 #ifndef CONFIG_USER_ONLY
1715     {
1716         .handler = gdb_handle_set_qemu_phy_mem_mode,
1717         .cmd = "qemu.PhyMemMode:",
1718         .cmd_startswith = 1,
1719         .schema = "l0"
1720     },
1721 #endif
1722 };
1723
1724 static void handle_gen_query(GArray *params, void *user_ctx)
1725 {
1726     if (!params->len) {
1727         return;
1728     }
1729
1730     if (!process_string_cmd(get_param(params, 0)->data,
1731                             gdb_gen_query_set_common_table,
1732                             ARRAY_SIZE(gdb_gen_query_set_common_table))) {
1733         return;
1734     }
1735
1736     if (process_string_cmd(get_param(params, 0)->data,
1737                            gdb_gen_query_table,
1738                            ARRAY_SIZE(gdb_gen_query_table))) {
1739         gdb_put_packet("");
1740     }
1741 }
1742
1743 static void handle_gen_set(GArray *params, void *user_ctx)
1744 {
1745     if (!params->len) {
1746         return;
1747     }
1748
1749     if (!process_string_cmd(get_param(params, 0)->data,
1750                             gdb_gen_query_set_common_table,
1751                             ARRAY_SIZE(gdb_gen_query_set_common_table))) {
1752         return;
1753     }
1754
1755     if (process_string_cmd(get_param(params, 0)->data,
1756                            gdb_gen_set_table,
1757                            ARRAY_SIZE(gdb_gen_set_table))) {
1758         gdb_put_packet("");
1759     }
1760 }
1761
1762 static void handle_target_halt(GArray *params, void *user_ctx)
1763 {
1764     if (gdbserver_state.allow_stop_reply) {
1765         g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1766         gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
1767         g_string_append_c(gdbserver_state.str_buf, ';');
1768         gdb_put_strbuf();
1769         gdbserver_state.allow_stop_reply = false;
1770     }
1771     /*
1772      * Remove all the breakpoints when this query is issued,
1773      * because gdb is doing an initial connect and the state
1774      * should be cleaned up.
1775      */
1776     gdb_breakpoint_remove_all(gdbserver_state.c_cpu);
1777 }
1778
1779 static int gdb_handle_packet(const char *line_buf)
1780 {
1781     const GdbCmdParseEntry *cmd_parser = NULL;
1782
1783     trace_gdbstub_io_command(line_buf);
1784
1785     switch (line_buf[0]) {
1786     case '!':
1787         gdb_put_packet("OK");
1788         break;
1789     case '?':
1790         {
1791             static const GdbCmdParseEntry target_halted_cmd_desc = {
1792                 .handler = handle_target_halt,
1793                 .cmd = "?",
1794                 .cmd_startswith = 1,
1795                 .allow_stop_reply = true,
1796             };
1797             cmd_parser = &target_halted_cmd_desc;
1798         }
1799         break;
1800     case 'c':
1801         {
1802             static const GdbCmdParseEntry continue_cmd_desc = {
1803                 .handler = handle_continue,
1804                 .cmd = "c",
1805                 .cmd_startswith = 1,
1806                 .allow_stop_reply = true,
1807                 .schema = "L0"
1808             };
1809             cmd_parser = &continue_cmd_desc;
1810         }
1811         break;
1812     case 'C':
1813         {
1814             static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
1815                 .handler = handle_cont_with_sig,
1816                 .cmd = "C",
1817                 .cmd_startswith = 1,
1818                 .allow_stop_reply = true,
1819                 .schema = "l0"
1820             };
1821             cmd_parser = &cont_with_sig_cmd_desc;
1822         }
1823         break;
1824     case 'v':
1825         {
1826             static const GdbCmdParseEntry v_cmd_desc = {
1827                 .handler = handle_v_commands,
1828                 .cmd = "v",
1829                 .cmd_startswith = 1,
1830                 .schema = "s0"
1831             };
1832             cmd_parser = &v_cmd_desc;
1833         }
1834         break;
1835     case 'k':
1836         /* Kill the target */
1837         error_report("QEMU: Terminated via GDBstub");
1838         gdb_exit(0);
1839         exit(0);
1840     case 'D':
1841         {
1842             static const GdbCmdParseEntry detach_cmd_desc = {
1843                 .handler = handle_detach,
1844                 .cmd = "D",
1845                 .cmd_startswith = 1,
1846                 .schema = "?.l0"
1847             };
1848             cmd_parser = &detach_cmd_desc;
1849         }
1850         break;
1851     case 's':
1852         {
1853             static const GdbCmdParseEntry step_cmd_desc = {
1854                 .handler = handle_step,
1855                 .cmd = "s",
1856                 .cmd_startswith = 1,
1857                 .allow_stop_reply = true,
1858                 .schema = "L0"
1859             };
1860             cmd_parser = &step_cmd_desc;
1861         }
1862         break;
1863     case 'b':
1864         {
1865             static const GdbCmdParseEntry backward_cmd_desc = {
1866                 .handler = handle_backward,
1867                 .cmd = "b",
1868                 .cmd_startswith = 1,
1869                 .allow_stop_reply = true,
1870                 .schema = "o0"
1871             };
1872             cmd_parser = &backward_cmd_desc;
1873         }
1874         break;
1875     case 'F':
1876         {
1877             static const GdbCmdParseEntry file_io_cmd_desc = {
1878                 .handler = gdb_handle_file_io,
1879                 .cmd = "F",
1880                 .cmd_startswith = 1,
1881                 .schema = "L,L,o0"
1882             };
1883             cmd_parser = &file_io_cmd_desc;
1884         }
1885         break;
1886     case 'g':
1887         {
1888             static const GdbCmdParseEntry read_all_regs_cmd_desc = {
1889                 .handler = handle_read_all_regs,
1890                 .cmd = "g",
1891                 .cmd_startswith = 1
1892             };
1893             cmd_parser = &read_all_regs_cmd_desc;
1894         }
1895         break;
1896     case 'G':
1897         {
1898             static const GdbCmdParseEntry write_all_regs_cmd_desc = {
1899                 .handler = handle_write_all_regs,
1900                 .cmd = "G",
1901                 .cmd_startswith = 1,
1902                 .schema = "s0"
1903             };
1904             cmd_parser = &write_all_regs_cmd_desc;
1905         }
1906         break;
1907     case 'm':
1908         {
1909             static const GdbCmdParseEntry read_mem_cmd_desc = {
1910                 .handler = handle_read_mem,
1911                 .cmd = "m",
1912                 .cmd_startswith = 1,
1913                 .schema = "L,L0"
1914             };
1915             cmd_parser = &read_mem_cmd_desc;
1916         }
1917         break;
1918     case 'M':
1919         {
1920             static const GdbCmdParseEntry write_mem_cmd_desc = {
1921                 .handler = handle_write_mem,
1922                 .cmd = "M",
1923                 .cmd_startswith = 1,
1924                 .schema = "L,L:s0"
1925             };
1926             cmd_parser = &write_mem_cmd_desc;
1927         }
1928         break;
1929     case 'p':
1930         {
1931             static const GdbCmdParseEntry get_reg_cmd_desc = {
1932                 .handler = handle_get_reg,
1933                 .cmd = "p",
1934                 .cmd_startswith = 1,
1935                 .schema = "L0"
1936             };
1937             cmd_parser = &get_reg_cmd_desc;
1938         }
1939         break;
1940     case 'P':
1941         {
1942             static const GdbCmdParseEntry set_reg_cmd_desc = {
1943                 .handler = handle_set_reg,
1944                 .cmd = "P",
1945                 .cmd_startswith = 1,
1946                 .schema = "L?s0"
1947             };
1948             cmd_parser = &set_reg_cmd_desc;
1949         }
1950         break;
1951     case 'Z':
1952         {
1953             static const GdbCmdParseEntry insert_bp_cmd_desc = {
1954                 .handler = handle_insert_bp,
1955                 .cmd = "Z",
1956                 .cmd_startswith = 1,
1957                 .schema = "l?L?L0"
1958             };
1959             cmd_parser = &insert_bp_cmd_desc;
1960         }
1961         break;
1962     case 'z':
1963         {
1964             static const GdbCmdParseEntry remove_bp_cmd_desc = {
1965                 .handler = handle_remove_bp,
1966                 .cmd = "z",
1967                 .cmd_startswith = 1,
1968                 .schema = "l?L?L0"
1969             };
1970             cmd_parser = &remove_bp_cmd_desc;
1971         }
1972         break;
1973     case 'H':
1974         {
1975             static const GdbCmdParseEntry set_thread_cmd_desc = {
1976                 .handler = handle_set_thread,
1977                 .cmd = "H",
1978                 .cmd_startswith = 1,
1979                 .schema = "o.t0"
1980             };
1981             cmd_parser = &set_thread_cmd_desc;
1982         }
1983         break;
1984     case 'T':
1985         {
1986             static const GdbCmdParseEntry thread_alive_cmd_desc = {
1987                 .handler = handle_thread_alive,
1988                 .cmd = "T",
1989                 .cmd_startswith = 1,
1990                 .schema = "t0"
1991             };
1992             cmd_parser = &thread_alive_cmd_desc;
1993         }
1994         break;
1995     case 'q':
1996         {
1997             static const GdbCmdParseEntry gen_query_cmd_desc = {
1998                 .handler = handle_gen_query,
1999                 .cmd = "q",
2000                 .cmd_startswith = 1,
2001                 .schema = "s0"
2002             };
2003             cmd_parser = &gen_query_cmd_desc;
2004         }
2005         break;
2006     case 'Q':
2007         {
2008             static const GdbCmdParseEntry gen_set_cmd_desc = {
2009                 .handler = handle_gen_set,
2010                 .cmd = "Q",
2011                 .cmd_startswith = 1,
2012                 .schema = "s0"
2013             };
2014             cmd_parser = &gen_set_cmd_desc;
2015         }
2016         break;
2017     default:
2018         /* put empty packet */
2019         gdb_put_packet("");
2020         break;
2021     }
2022
2023     if (cmd_parser) {
2024         run_cmd_parser(line_buf, cmd_parser);
2025     }
2026
2027     return RS_IDLE;
2028 }
2029
2030 void gdb_set_stop_cpu(CPUState *cpu)
2031 {
2032     GDBProcess *p = gdb_get_cpu_process(cpu);
2033
2034     if (!p->attached) {
2035         /*
2036          * Having a stop CPU corresponding to a process that is not attached
2037          * confuses GDB. So we ignore the request.
2038          */
2039         return;
2040     }
2041
2042     gdbserver_state.c_cpu = cpu;
2043     gdbserver_state.g_cpu = cpu;
2044 }
2045
2046 void gdb_read_byte(uint8_t ch)
2047 {
2048     uint8_t reply;
2049
2050     gdbserver_state.allow_stop_reply = false;
2051 #ifndef CONFIG_USER_ONLY
2052     if (gdbserver_state.last_packet->len) {
2053         /* Waiting for a response to the last packet.  If we see the start
2054            of a new command then abandon the previous response.  */
2055         if (ch == '-') {
2056             trace_gdbstub_err_got_nack();
2057             gdb_put_buffer(gdbserver_state.last_packet->data,
2058                        gdbserver_state.last_packet->len);
2059         } else if (ch == '+') {
2060             trace_gdbstub_io_got_ack();
2061         } else {
2062             trace_gdbstub_io_got_unexpected(ch);
2063         }
2064
2065         if (ch == '+' || ch == '$') {
2066             g_byte_array_set_size(gdbserver_state.last_packet, 0);
2067         }
2068         if (ch != '$')
2069             return;
2070     }
2071     if (runstate_is_running()) {
2072         /*
2073          * When the CPU is running, we cannot do anything except stop
2074          * it when receiving a char. This is expected on a Ctrl-C in the
2075          * gdb client. Because we are in all-stop mode, gdb sends a
2076          * 0x03 byte which is not a usual packet, so we handle it specially
2077          * here, but it does expect a stop reply.
2078          */
2079         if (ch != 0x03) {
2080             trace_gdbstub_err_unexpected_runpkt(ch);
2081         } else {
2082             gdbserver_state.allow_stop_reply = true;
2083         }
2084         vm_stop(RUN_STATE_PAUSED);
2085     } else
2086 #endif
2087     {
2088         switch(gdbserver_state.state) {
2089         case RS_IDLE:
2090             if (ch == '$') {
2091                 /* start of command packet */
2092                 gdbserver_state.line_buf_index = 0;
2093                 gdbserver_state.line_sum = 0;
2094                 gdbserver_state.state = RS_GETLINE;
2095             } else if (ch == '+') {
2096                 /*
2097                  * do nothing, gdb may preemptively send out ACKs on
2098                  * initial connection
2099                  */
2100             } else {
2101                 trace_gdbstub_err_garbage(ch);
2102             }
2103             break;
2104         case RS_GETLINE:
2105             if (ch == '}') {
2106                 /* start escape sequence */
2107                 gdbserver_state.state = RS_GETLINE_ESC;
2108                 gdbserver_state.line_sum += ch;
2109             } else if (ch == '*') {
2110                 /* start run length encoding sequence */
2111                 gdbserver_state.state = RS_GETLINE_RLE;
2112                 gdbserver_state.line_sum += ch;
2113             } else if (ch == '#') {
2114                 /* end of command, start of checksum*/
2115                 gdbserver_state.state = RS_CHKSUM1;
2116             } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
2117                 trace_gdbstub_err_overrun();
2118                 gdbserver_state.state = RS_IDLE;
2119             } else {
2120                 /* unescaped command character */
2121                 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
2122                 gdbserver_state.line_sum += ch;
2123             }
2124             break;
2125         case RS_GETLINE_ESC:
2126             if (ch == '#') {
2127                 /* unexpected end of command in escape sequence */
2128                 gdbserver_state.state = RS_CHKSUM1;
2129             } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
2130                 /* command buffer overrun */
2131                 trace_gdbstub_err_overrun();
2132                 gdbserver_state.state = RS_IDLE;
2133             } else {
2134                 /* parse escaped character and leave escape state */
2135                 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
2136                 gdbserver_state.line_sum += ch;
2137                 gdbserver_state.state = RS_GETLINE;
2138             }
2139             break;
2140         case RS_GETLINE_RLE:
2141             /*
2142              * Run-length encoding is explained in "Debugging with GDB /
2143              * Appendix E GDB Remote Serial Protocol / Overview".
2144              */
2145             if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
2146                 /* invalid RLE count encoding */
2147                 trace_gdbstub_err_invalid_repeat(ch);
2148                 gdbserver_state.state = RS_GETLINE;
2149             } else {
2150                 /* decode repeat length */
2151                 int repeat = ch - ' ' + 3;
2152                 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
2153                     /* that many repeats would overrun the command buffer */
2154                     trace_gdbstub_err_overrun();
2155                     gdbserver_state.state = RS_IDLE;
2156                 } else if (gdbserver_state.line_buf_index < 1) {
2157                     /* got a repeat but we have nothing to repeat */
2158                     trace_gdbstub_err_invalid_rle();
2159                     gdbserver_state.state = RS_GETLINE;
2160                 } else {
2161                     /* repeat the last character */
2162                     memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
2163                            gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
2164                     gdbserver_state.line_buf_index += repeat;
2165                     gdbserver_state.line_sum += ch;
2166                     gdbserver_state.state = RS_GETLINE;
2167                 }
2168             }
2169             break;
2170         case RS_CHKSUM1:
2171             /* get high hex digit of checksum */
2172             if (!isxdigit(ch)) {
2173                 trace_gdbstub_err_checksum_invalid(ch);
2174                 gdbserver_state.state = RS_GETLINE;
2175                 break;
2176             }
2177             gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
2178             gdbserver_state.line_csum = fromhex(ch) << 4;
2179             gdbserver_state.state = RS_CHKSUM2;
2180             break;
2181         case RS_CHKSUM2:
2182             /* get low hex digit of checksum */
2183             if (!isxdigit(ch)) {
2184                 trace_gdbstub_err_checksum_invalid(ch);
2185                 gdbserver_state.state = RS_GETLINE;
2186                 break;
2187             }
2188             gdbserver_state.line_csum |= fromhex(ch);
2189
2190             if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
2191                 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
2192                 /* send NAK reply */
2193                 reply = '-';
2194                 gdb_put_buffer(&reply, 1);
2195                 gdbserver_state.state = RS_IDLE;
2196             } else {
2197                 /* send ACK reply */
2198                 reply = '+';
2199                 gdb_put_buffer(&reply, 1);
2200                 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
2201             }
2202             break;
2203         default:
2204             abort();
2205         }
2206     }
2207 }
2208
2209 /*
2210  * Create the process that will contain all the "orphan" CPUs (that are not
2211  * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2212  * be attachable and thus will be invisible to the user.
2213  */
2214 void gdb_create_default_process(GDBState *s)
2215 {
2216     GDBProcess *process;
2217     int pid;
2218
2219 #ifdef CONFIG_USER_ONLY
2220     assert(gdbserver_state.process_num == 0);
2221     pid = getpid();
2222 #else
2223     if (gdbserver_state.process_num) {
2224         pid = s->processes[s->process_num - 1].pid;
2225     } else {
2226         pid = 0;
2227     }
2228     /* We need an available PID slot for this process */
2229     assert(pid < UINT32_MAX);
2230     pid++;
2231 #endif
2232
2233     s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2234     process = &s->processes[s->process_num - 1];
2235     process->pid = pid;
2236     process->attached = false;
2237     process->target_xml = NULL;
2238 }
2239