master
c 671 lines 16.5 KB
Raw
1 /*
2 * gdb server stub - system specific bits
3 *
4 * Debug integration depends on support from the individual
5 * accelerators so most of this involves calling the ops helpers.
6 *
7 * Copyright (c) 2003-2005 Fabrice Bellard
8 * Copyright (c) 2022 Linaro Ltd
9 *
10 * SPDX-License-Identifier: LGPL-2.0-or-later
11 */
12
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/error-report.h"
16 #include "qemu/cutils.h"
17 #include "exec/gdbstub.h"
18 #include "gdbstub/syscalls.h"
19 #include "gdbstub/commands.h"
20 #include "exec/hwaddr.h"
21 #include "accel/accel-ops.h"
22 #include "accel/accel-cpu-ops.h"
23 #include "system/address-spaces.h"
24 #include "system/cpus.h"
25 #include "system/runstate.h"
26 #include "system/replay.h"
27 #include "hw/core/cpu.h"
28 #include "hw/cpu/cluster.h"
29 #include "hw/core/boards.h"
30 #include "chardev/char.h"
31 #include "chardev/char-fe.h"
32 #include "monitor/hmp.h"
33 #include "trace.h"
34 #include "internals.h"
35
36 /* System emulation specific state */
37 typedef struct {
38 CharFrontend chr;
39 Chardev *mon_chr;
40 } GDBSystemState;
41
42 GDBSystemState gdbserver_system_state;
43
44 static void reset_gdbserver_state(void)
45 {
46 g_free(gdbserver_state.processes);
47 gdbserver_state.processes = NULL;
48 gdbserver_state.process_num = 0;
49 gdbserver_state.allow_stop_reply = false;
50 }
51
52 /*
53 * Return the GDB index for a given vCPU state.
54 *
55 * In system mode GDB numbers CPUs from 1 as 0 is reserved as an "any
56 * cpu" index.
57 */
58 int gdb_get_cpu_index(CPUState *cpu)
59 {
60 return cpu->cpu_index + 1;
61 }
62
63 /*
64 * We check the status of the last message in the chardev receive code
65 */
66 bool gdb_got_immediate_ack(void)
67 {
68 return true;
69 }
70
71 /*
72 * GDB Connection management. For system emulation we do all of this
73 * via our existing Chardev infrastructure which allows us to support
74 * network and unix sockets.
75 */
76
77 void gdb_put_buffer(const uint8_t *buf, int len)
78 {
79 /*
80 * XXX this blocks entire thread. Rewrite to use
81 * qemu_chr_fe_write and background I/O callbacks
82 */
83 qemu_chr_fe_write_all(&gdbserver_system_state.chr, buf, len);
84 }
85
86 static void gdb_chr_event(void *opaque, QEMUChrEvent event)
87 {
88 int i;
89 GDBState *s = (GDBState *) opaque;
90
91 switch (event) {
92 case CHR_EVENT_OPENED:
93 /* Start with first process attached, others detached */
94 for (i = 0; i < s->process_num; i++) {
95 s->processes[i].attached = !i;
96 }
97
98 s->c_cpu = gdb_first_attached_cpu();
99 s->g_cpu = s->c_cpu;
100
101 vm_stop(RUN_STATE_PAUSED);
102 replay_gdb_attached();
103 break;
104 default:
105 break;
106 }
107 }
108
109 /*
110 * In system-mode we stop the VM and wait to send the syscall packet
111 * until notification that the CPU has stopped. This must be done
112 * because if the packet is sent now the reply from the syscall
113 * request could be received while the CPU is still in the running
114 * state, which can cause packets to be dropped and state transition
115 * 'T' packets to be sent while the syscall is still being processed.
116 */
117 void gdb_syscall_handling(const char *syscall_packet)
118 {
119 vm_stop(RUN_STATE_DEBUG);
120 qemu_cpu_kick(gdbserver_state.c_cpu);
121 }
122
123 static void gdb_vm_state_change(void *opaque, bool running, RunState state)
124 {
125 CPUState *cpu = gdbserver_state.c_cpu;
126 g_autoptr(GString) buf = g_string_new(NULL);
127 g_autoptr(GString) tid = g_string_new(NULL);
128 int ret;
129
130 if (running || gdbserver_state.state == RS_INACTIVE) {
131 return;
132 }
133
134 /* Is there a GDB syscall waiting to be sent? */
135 if (gdb_handled_syscall()) {
136 return;
137 }
138
139 if (cpu == NULL) {
140 /* No process attached */
141 return;
142 }
143
144 if (!gdbserver_state.allow_stop_reply) {
145 return;
146 }
147
148 gdb_append_thread_id(cpu, tid);
149
150 switch (state) {
151 case RUN_STATE_DEBUG:
152 if (cpu->watchpoint_hit) {
153 const char *type;
154
155 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
156 case BP_MEM_READ:
157 type = "r";
158 break;
159 case BP_MEM_ACCESS:
160 type = "a";
161 break;
162 default:
163 type = "";
164 break;
165 }
166 trace_gdbstub_hit_watchpoint(type,
167 gdb_get_cpu_index(cpu),
168 cpu->watchpoint_hit->vaddr);
169 g_string_printf(buf, "T%02xthread:%s;%swatch:%" VADDR_PRIx ";",
170 GDB_SIGNAL_TRAP, tid->str, type,
171 cpu->watchpoint_hit->vaddr);
172 cpu->watchpoint_hit = NULL;
173 goto send_packet;
174 } else {
175 trace_gdbstub_hit_break();
176 }
177 ret = GDB_SIGNAL_TRAP;
178 break;
179 case RUN_STATE_PAUSED:
180 trace_gdbstub_hit_paused();
181 ret = GDB_SIGNAL_INT;
182 break;
183 case RUN_STATE_SHUTDOWN:
184 trace_gdbstub_hit_shutdown();
185 ret = GDB_SIGNAL_QUIT;
186 break;
187 case RUN_STATE_IO_ERROR:
188 trace_gdbstub_hit_io_error();
189 ret = GDB_SIGNAL_STOP;
190 break;
191 case RUN_STATE_WATCHDOG:
192 trace_gdbstub_hit_watchdog();
193 ret = GDB_SIGNAL_ALRM;
194 break;
195 case RUN_STATE_INTERNAL_ERROR:
196 trace_gdbstub_hit_internal_error();
197 ret = GDB_SIGNAL_ABRT;
198 break;
199 case RUN_STATE_SAVE_VM:
200 case RUN_STATE_RESTORE_VM:
201 return;
202 case RUN_STATE_FINISH_MIGRATE:
203 ret = GDB_SIGNAL_XCPU;
204 break;
205 default:
206 trace_gdbstub_hit_unknown(state);
207 ret = GDB_SIGNAL_UNKNOWN;
208 break;
209 }
210 gdb_set_stop_cpu(cpu);
211 g_string_printf(buf, "T%02xthread:%s;", ret, tid->str);
212
213 send_packet:
214 gdb_put_packet(buf->str);
215 gdbserver_state.allow_stop_reply = false;
216
217 /* disable single step if it was enabled */
218 cpu_single_step(cpu, 0);
219 }
220
221 #ifndef _WIN32
222 static void gdb_sigterm_handler(int signal)
223 {
224 if (runstate_is_running()) {
225 vm_stop(RUN_STATE_PAUSED);
226 }
227 }
228 #endif
229
230 static int gdb_chr_write(Chardev *chr, const uint8_t *buf, int len)
231 {
232 g_autoptr(GString) hex_buf = g_string_new("O");
233 gdb_memtohex(hex_buf, buf, len);
234 gdb_put_packet(hex_buf->str);
235 return len;
236 }
237
238 static bool gdb_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp)
239 {
240 /* Never send CHR_EVENT_OPENED */
241 return true;
242 }
243
244 static void char_gdb_class_init(ObjectClass *oc, const void *data)
245 {
246 ChardevClass *cc = CHARDEV_CLASS(oc);
247
248 cc->internal = true;
249 cc->chr_open = gdb_chr_open;
250 cc->chr_write = gdb_chr_write;
251 }
252
253 #define TYPE_CHARDEV_GDB "chardev-gdb"
254
255 static const TypeInfo char_gdb_type_info = {
256 .name = TYPE_CHARDEV_GDB,
257 .parent = TYPE_CHARDEV,
258 .class_init = char_gdb_class_init,
259 };
260
261 static int gdb_chr_can_receive(void *opaque)
262 {
263 /*
264 * We can handle an arbitrarily large amount of data.
265 * Pick the maximum packet size, which is as good as anything.
266 */
267 return MAX_PACKET_LENGTH;
268 }
269
270 static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
271 {
272 int i;
273
274 for (i = 0; i < size; i++) {
275 gdb_read_byte(buf[i]);
276 }
277 }
278
279 static int find_cpu_clusters(Object *child, void *opaque)
280 {
281 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
282 GDBState *s = (GDBState *) opaque;
283 CPUClusterState *cluster = CPU_CLUSTER(child);
284 GDBProcess *process;
285
286 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
287
288 process = &s->processes[s->process_num - 1];
289
290 /*
291 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
292 * runtime, we enforce here that the machine does not use a cluster ID
293 * that would lead to PID 0.
294 */
295 assert(cluster->cluster_id != UINT32_MAX);
296 process->pid = cluster->cluster_id + 1;
297 process->attached = false;
298 process->target_xml = NULL;
299
300 return 0;
301 }
302
303 return object_child_foreach(child, find_cpu_clusters, opaque);
304 }
305
306 static int pid_order(const void *a, const void *b)
307 {
308 GDBProcess *pa = (GDBProcess *) a;
309 GDBProcess *pb = (GDBProcess *) b;
310
311 if (pa->pid < pb->pid) {
312 return -1;
313 } else if (pa->pid > pb->pid) {
314 return 1;
315 } else {
316 return 0;
317 }
318 }
319
320 static void create_processes(GDBState *s)
321 {
322 object_child_foreach(object_get_root(), find_cpu_clusters, s);
323
324 if (gdbserver_state.processes) {
325 /* Sort by PID */
326 qsort(gdbserver_state.processes,
327 gdbserver_state.process_num,
328 sizeof(gdbserver_state.processes[0]),
329 pid_order);
330 }
331
332 gdb_create_default_process(s);
333 }
334
335 bool gdbserver_start(const char *device, Error **errp)
336 {
337 Chardev *chr = NULL;
338 Chardev *mon_chr;
339 g_autoptr(GString) cs = g_string_new(device);
340
341 if (!first_cpu) {
342 error_setg(errp, "gdbstub: meaningless to attach gdb to a "
343 "machine without any CPU.");
344 return false;
345 }
346
347 if (!accel_supports_guest_debug(current_accel())) {
348 error_setg(errp, "gdbstub: current accelerator doesn't "
349 "support guest debugging");
350 return false;
351 }
352
353 if (cs->len == 0) {
354 error_setg(errp, "gdbstub: missing connection string");
355 return false;
356 }
357
358 trace_gdbstub_op_start(cs->str);
359
360 if (g_strcmp0(cs->str, "none") != 0) {
361 if (g_str_has_prefix(cs->str, "tcp:")) {
362 /* enforce required TCP attributes */
363 g_string_append_printf(cs, ",wait=off,nodelay=on,server=on");
364 }
365 #ifndef _WIN32
366 else if (strcmp(device, "stdio") == 0) {
367 struct sigaction act;
368
369 memset(&act, 0, sizeof(act));
370 act.sa_handler = gdb_sigterm_handler;
371 sigaction(SIGINT, &act, NULL);
372 }
373 #endif
374 /*
375 * FIXME: it's a bit weird to allow using a mux chardev here
376 * and implicitly setup a monitor. We may want to break this.
377 */
378 chr = qemu_chr_new_noreplay("gdb", cs->str, true, NULL);
379 if (!chr) {
380 error_setg(errp, "gdbstub: couldn't create chardev");
381 return false;
382 }
383 }
384
385 if (!gdbserver_state.init) {
386 gdb_init_gdbserver_state();
387
388 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
389
390 #ifdef CONFIG_HMP
391 /* Initialize a monitor terminal for gdb */
392 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
393 NULL, NULL, &error_abort);
394 monitor_new_hmp(NULL, mon_chr->label, false, &error_abort);
395 #else
396 mon_chr = NULL;
397 #endif
398 } else {
399 qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
400 mon_chr = gdbserver_system_state.mon_chr;
401 reset_gdbserver_state();
402 }
403
404 create_processes(&gdbserver_state);
405
406 if (chr) {
407 qemu_chr_fe_init(&gdbserver_system_state.chr, chr, &error_abort);
408 qemu_chr_fe_set_handlers(&gdbserver_system_state.chr,
409 gdb_chr_can_receive,
410 gdb_chr_receive, gdb_chr_event,
411 NULL, &gdbserver_state, NULL, true);
412 }
413 gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE;
414 gdbserver_system_state.mon_chr = mon_chr;
415 gdb_syscall_reset();
416
417 return true;
418 }
419
420 static void register_types(void)
421 {
422 type_register_static(&char_gdb_type_info);
423 }
424
425 type_init(register_types);
426
427 /* Tell the remote gdb that the process has exited. */
428 void gdb_exit(int code)
429 {
430 char buf[4];
431
432 if (!gdbserver_state.init) {
433 return;
434 }
435
436 trace_gdbstub_op_exiting((uint8_t)code);
437
438 if (gdbserver_state.allow_stop_reply) {
439 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
440 gdb_put_packet(buf);
441 gdbserver_state.allow_stop_reply = false;
442 }
443
444 qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
445 }
446
447 void gdb_qemu_exit(int code)
448 {
449 qemu_system_shutdown_request_with_code(SHUTDOWN_CAUSE_GUEST_SHUTDOWN,
450 code);
451 }
452
453 /*
454 * Memory access
455 */
456 static int phy_memory_mode;
457
458 int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
459 uint8_t *buf, int len, bool is_write)
460 {
461 if (phy_memory_mode) {
462 MemTxResult res = address_space_rw(&address_space_memory, addr,
463 MEMTXATTRS_UNSPECIFIED, buf, len,
464 is_write);
465 return res == MEMTX_OK ? 0 : -1;
466 }
467
468 if (cpu->cc->memory_rw_debug) {
469 return cpu->cc->memory_rw_debug(cpu, addr, buf, len, is_write);
470 }
471
472 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
473 }
474
475 /*
476 * cpu helpers
477 */
478
479 unsigned int gdb_get_max_cpus(void)
480 {
481 MachineState *ms = MACHINE(qdev_get_machine());
482 return ms->smp.max_cpus;
483 }
484
485 /*
486 * Softmmu specific command helpers
487 */
488
489 void gdb_handle_query_qemu_phy_mem_mode(GArray *params,
490 void *ctx)
491 {
492 g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
493 gdb_put_strbuf();
494 }
495
496 void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx)
497 {
498 if (!params->len) {
499 gdb_put_packet("E22");
500 return;
501 }
502
503 if (!gdb_get_cmd_param(params, 0)->val_ul) {
504 phy_memory_mode = 0;
505 } else {
506 phy_memory_mode = 1;
507 }
508 gdb_put_packet("OK");
509 }
510
511 void gdb_handle_query_rcmd(GArray *params, void *ctx)
512 {
513 const guint8 zero = 0;
514 int len;
515
516 if (!params->len) {
517 gdb_put_packet("E22");
518 return;
519 }
520
521 len = strlen(gdb_get_cmd_param(params, 0)->data);
522 if (len % 2) {
523 gdb_put_packet("E01");
524 return;
525 }
526
527 g_assert(gdbserver_state.mem_buf->len == 0);
528 len = len / 2;
529 gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 0)->data, len);
530 g_byte_array_append(gdbserver_state.mem_buf, &zero, 1);
531 #ifdef CONFIG_HMP
532 qemu_chr_be_write(gdbserver_system_state.mon_chr,
533 gdbserver_state.mem_buf->data,
534 gdbserver_state.mem_buf->len);
535 gdb_put_packet("OK");
536 #else
537 gdb_put_packet("E01");
538 #endif
539 }
540
541 /*
542 * Execution state helpers
543 */
544
545 void gdb_handle_query_attached(GArray *params, void *ctx)
546 {
547 gdb_put_packet("1");
548 }
549
550 void gdb_continue(void)
551 {
552 if (!runstate_needs_reset()) {
553 trace_gdbstub_op_continue();
554 vm_start();
555 }
556 }
557
558 /*
559 * Resume execution, per CPU actions.
560 */
561 int gdb_continue_partial(char *newstates)
562 {
563 CPUState *cpu;
564 int res = 0;
565 int flag = 0;
566
567 if (!runstate_needs_reset()) {
568 bool step_requested = false;
569 CPU_FOREACH(cpu) {
570 if (newstates[cpu->cpu_index] == 's') {
571 step_requested = true;
572 break;
573 }
574 }
575
576 if (vm_prepare_start(step_requested)) {
577 return 0;
578 }
579
580 CPU_FOREACH(cpu) {
581 switch (newstates[cpu->cpu_index]) {
582 case 0:
583 case 1:
584 break; /* nothing to do here */
585 case 's':
586 trace_gdbstub_op_stepping(cpu->cpu_index);
587 cpu_single_step(cpu, gdbserver_state.sstep_flags);
588 cpu_resume(cpu);
589 flag = 1;
590 break;
591 case 'c':
592 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
593 cpu_resume(cpu);
594 flag = 1;
595 break;
596 default:
597 res = -1;
598 break;
599 }
600 }
601 }
602 if (flag) {
603 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
604 }
605 return res;
606 }
607
608 /*
609 * Signal Handling - in system mode we only need SIGINT and SIGTRAP; other
610 * signals are not yet supported.
611 */
612
613 enum {
614 TARGET_SIGINT = 2,
615 TARGET_SIGTRAP = 5
616 };
617
618 int gdb_signal_to_target(int sig)
619 {
620 switch (sig) {
621 case 2:
622 return TARGET_SIGINT;
623 case 5:
624 return TARGET_SIGTRAP;
625 default:
626 return -1;
627 }
628 }
629
630 /*
631 * Break/Watch point helpers
632 */
633
634 int gdb_breakpoint_insert(CPUState *cs, GdbBreakpointType type,
635 vaddr addr, vaddr len)
636 {
637 const AccelOpsClass *ops = cpus_get_accel();
638 if (ops->insert_gdbstub_breakpoint) {
639 return ops->insert_gdbstub_breakpoint(cs, type, addr, len);
640 }
641 return -ENOSYS;
642 }
643
644 int gdb_breakpoint_remove(CPUState *cs, GdbBreakpointType type,
645 vaddr addr, vaddr len)
646 {
647 const AccelOpsClass *ops = cpus_get_accel();
648 if (ops->remove_gdbstub_breakpoint) {
649 return ops->remove_gdbstub_breakpoint(cs, type, addr, len);
650 }
651 return -ENOSYS;
652 }
653
654 void gdb_breakpoint_remove_all(CPUState *cs)
655 {
656 const AccelOpsClass *ops = cpus_get_accel();
657 if (ops->remove_all_gdbstub_breakpoints) {
658 ops->remove_all_gdbstub_breakpoints(cs);
659 }
660 }
661
662 /*
663 * The minimal system-mode stop reply packet is:
664 * T05core:{id};
665 */
666
667 void gdb_build_stop_packet(GString *buf, CPUState *cs)
668 {
669 g_string_printf(buf,
670 "T%02xcore:%02x;", GDB_SIGNAL_TRAP, gdb_get_cpu_index(cs));
671 }