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