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