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