2 * gdb server stub - softmmu specific bits
4 * Debug integration depends on support from the individual
5 * accelerators so most of this involves calling the ops helpers.
7 * Copyright (c) 2003-2005 Fabrice Bellard
8 * Copyright (c) 2022 Linaro Ltd
10 * SPDX-License-Identifier: LGPL-2.0+
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 "exec/hwaddr.h"
19 #include "exec/tb-flush.h"
20 #include "sysemu/cpus.h"
21 #include "sysemu/runstate.h"
22 #include "sysemu/replay.h"
23 #include "hw/core/cpu.h"
24 #include "hw/cpu/cluster.h"
25 #include "hw/boards.h"
26 #include "chardev/char.h"
27 #include "chardev/char-fe.h"
28 #include "monitor/monitor.h"
30 #include "internals.h"
32 /* System emulation specific state */
38 GDBSystemState gdbserver_system_state
;
40 static void reset_gdbserver_state(void)
42 g_free(gdbserver_state
.processes
);
43 gdbserver_state
.processes
= NULL
;
44 gdbserver_state
.process_num
= 0;
48 * Return the GDB index for a given vCPU state.
50 * In system mode GDB numbers CPUs from 1 as 0 is reserved as an "any
53 int gdb_get_cpu_index(CPUState
*cpu
)
55 return cpu
->cpu_index
+ 1;
59 * We check the status of the last message in the chardev receive code
61 bool gdb_got_immediate_ack(void)
67 * GDB Connection management. For system emulation we do all of this
68 * via our existing Chardev infrastructure which allows us to support
69 * network and unix sockets.
72 void gdb_put_buffer(const uint8_t *buf
, int len
)
75 * XXX this blocks entire thread. Rewrite to use
76 * qemu_chr_fe_write and background I/O callbacks
78 qemu_chr_fe_write_all(&gdbserver_system_state
.chr
, buf
, len
);
81 static void gdb_chr_event(void *opaque
, QEMUChrEvent event
)
84 GDBState
*s
= (GDBState
*) opaque
;
87 case CHR_EVENT_OPENED
:
88 /* Start with first process attached, others detached */
89 for (i
= 0; i
< s
->process_num
; i
++) {
90 s
->processes
[i
].attached
= !i
;
93 s
->c_cpu
= gdb_first_attached_cpu();
96 vm_stop(RUN_STATE_PAUSED
);
97 replay_gdb_attached();
105 static void gdb_vm_state_change(void *opaque
, bool running
, RunState state
)
107 CPUState
*cpu
= gdbserver_state
.c_cpu
;
108 g_autoptr(GString
) buf
= g_string_new(NULL
);
109 g_autoptr(GString
) tid
= g_string_new(NULL
);
113 if (running
|| gdbserver_state
.state
== RS_INACTIVE
) {
116 /* Is there a GDB syscall waiting to be sent? */
117 if (gdbserver_state
.current_syscall_cb
) {
118 gdb_put_packet(gdbserver_state
.syscall_buf
);
123 /* No process attached */
127 gdb_append_thread_id(cpu
, tid
);
130 case RUN_STATE_DEBUG
:
131 if (cpu
->watchpoint_hit
) {
132 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
143 trace_gdbstub_hit_watchpoint(type
,
144 gdb_get_cpu_index(cpu
),
145 cpu
->watchpoint_hit
->vaddr
);
146 g_string_printf(buf
, "T%02xthread:%s;%swatch:%" VADDR_PRIx
";",
147 GDB_SIGNAL_TRAP
, tid
->str
, type
,
148 cpu
->watchpoint_hit
->vaddr
);
149 cpu
->watchpoint_hit
= NULL
;
152 trace_gdbstub_hit_break();
155 ret
= GDB_SIGNAL_TRAP
;
157 case RUN_STATE_PAUSED
:
158 trace_gdbstub_hit_paused();
159 ret
= GDB_SIGNAL_INT
;
161 case RUN_STATE_SHUTDOWN
:
162 trace_gdbstub_hit_shutdown();
163 ret
= GDB_SIGNAL_QUIT
;
165 case RUN_STATE_IO_ERROR
:
166 trace_gdbstub_hit_io_error();
169 case RUN_STATE_WATCHDOG
:
170 trace_gdbstub_hit_watchdog();
171 ret
= GDB_SIGNAL_ALRM
;
173 case RUN_STATE_INTERNAL_ERROR
:
174 trace_gdbstub_hit_internal_error();
175 ret
= GDB_SIGNAL_ABRT
;
177 case RUN_STATE_SAVE_VM
:
178 case RUN_STATE_RESTORE_VM
:
180 case RUN_STATE_FINISH_MIGRATE
:
181 ret
= GDB_SIGNAL_XCPU
;
184 trace_gdbstub_hit_unknown(state
);
185 ret
= GDB_SIGNAL_UNKNOWN
;
188 gdb_set_stop_cpu(cpu
);
189 g_string_printf(buf
, "T%02xthread:%s;", ret
, tid
->str
);
192 gdb_put_packet(buf
->str
);
194 /* disable single step if it was enabled */
195 cpu_single_step(cpu
, 0);
199 static void gdb_sigterm_handler(int signal
)
201 if (runstate_is_running()) {
202 vm_stop(RUN_STATE_PAUSED
);
207 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
209 g_autoptr(GString
) hex_buf
= g_string_new("O");
210 gdb_memtohex(hex_buf
, buf
, len
);
211 gdb_put_packet(hex_buf
->str
);
215 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
216 bool *be_opened
, Error
**errp
)
221 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
223 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
226 cc
->open
= gdb_monitor_open
;
227 cc
->chr_write
= gdb_monitor_write
;
230 #define TYPE_CHARDEV_GDB "chardev-gdb"
232 static const TypeInfo char_gdb_type_info
= {
233 .name
= TYPE_CHARDEV_GDB
,
234 .parent
= TYPE_CHARDEV
,
235 .class_init
= char_gdb_class_init
,
238 static int gdb_chr_can_receive(void *opaque
)
241 * We can handle an arbitrarily large amount of data.
242 * Pick the maximum packet size, which is as good as anything.
244 return MAX_PACKET_LENGTH
;
247 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
251 for (i
= 0; i
< size
; i
++) {
252 gdb_read_byte(buf
[i
]);
256 static int find_cpu_clusters(Object
*child
, void *opaque
)
258 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
259 GDBState
*s
= (GDBState
*) opaque
;
260 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
263 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
265 process
= &s
->processes
[s
->process_num
- 1];
268 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
269 * runtime, we enforce here that the machine does not use a cluster ID
270 * that would lead to PID 0.
272 assert(cluster
->cluster_id
!= UINT32_MAX
);
273 process
->pid
= cluster
->cluster_id
+ 1;
274 process
->attached
= false;
275 process
->target_xml
[0] = '\0';
280 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
283 static int pid_order(const void *a
, const void *b
)
285 GDBProcess
*pa
= (GDBProcess
*) a
;
286 GDBProcess
*pb
= (GDBProcess
*) b
;
288 if (pa
->pid
< pb
->pid
) {
290 } else if (pa
->pid
> pb
->pid
) {
297 static void create_processes(GDBState
*s
)
299 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
301 if (gdbserver_state
.processes
) {
303 qsort(gdbserver_state
.processes
,
304 gdbserver_state
.process_num
,
305 sizeof(gdbserver_state
.processes
[0]),
309 gdb_create_default_process(s
);
312 int gdbserver_start(const char *device
)
314 trace_gdbstub_op_start(device
);
316 char gdbstub_device_name
[128];
321 error_report("gdbstub: meaningless to attach gdb to a "
322 "machine without any CPU.");
326 if (!gdb_supports_guest_debug()) {
327 error_report("gdbstub: current accelerator doesn't "
328 "support guest debugging");
335 if (strcmp(device
, "none") != 0) {
336 if (strstart(device
, "tcp:", NULL
)) {
337 /* enforce required TCP attributes */
338 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
339 "%s,wait=off,nodelay=on,server=on", device
);
340 device
= gdbstub_device_name
;
343 else if (strcmp(device
, "stdio") == 0) {
344 struct sigaction act
;
346 memset(&act
, 0, sizeof(act
));
347 act
.sa_handler
= gdb_sigterm_handler
;
348 sigaction(SIGINT
, &act
, NULL
);
352 * FIXME: it's a bit weird to allow using a mux chardev here
353 * and implicitly setup a monitor. We may want to break this.
355 chr
= qemu_chr_new_noreplay("gdb", device
, true, NULL
);
361 if (!gdbserver_state
.init
) {
362 gdb_init_gdbserver_state();
364 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
366 /* Initialize a monitor terminal for gdb */
367 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
368 NULL
, NULL
, &error_abort
);
369 monitor_init_hmp(mon_chr
, false, &error_abort
);
371 qemu_chr_fe_deinit(&gdbserver_system_state
.chr
, true);
372 mon_chr
= gdbserver_system_state
.mon_chr
;
373 reset_gdbserver_state();
376 create_processes(&gdbserver_state
);
379 qemu_chr_fe_init(&gdbserver_system_state
.chr
, chr
, &error_abort
);
380 qemu_chr_fe_set_handlers(&gdbserver_system_state
.chr
,
382 gdb_chr_receive
, gdb_chr_event
,
383 NULL
, &gdbserver_state
, NULL
, true);
385 gdbserver_state
.state
= chr
? RS_IDLE
: RS_INACTIVE
;
386 gdbserver_system_state
.mon_chr
= mon_chr
;
387 gdbserver_state
.current_syscall_cb
= NULL
;
392 static void register_types(void)
394 type_register_static(&char_gdb_type_info
);
397 type_init(register_types
);
399 /* Tell the remote gdb that the process has exited. */
400 void gdb_exit(int code
)
404 if (!gdbserver_state
.init
) {
408 trace_gdbstub_op_exiting((uint8_t)code
);
410 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
413 qemu_chr_fe_deinit(&gdbserver_system_state
.chr
, true);
419 static int phy_memory_mode
;
421 int gdb_target_memory_rw_debug(CPUState
*cpu
, hwaddr addr
,
422 uint8_t *buf
, int len
, bool is_write
)
426 if (phy_memory_mode
) {
428 cpu_physical_memory_write(addr
, buf
, len
);
430 cpu_physical_memory_read(addr
, buf
, len
);
435 cc
= CPU_GET_CLASS(cpu
);
436 if (cc
->memory_rw_debug
) {
437 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
440 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
445 * Softmmu specific command helpers
448 void gdb_handle_query_qemu_phy_mem_mode(GArray
*params
,
451 g_string_printf(gdbserver_state
.str_buf
, "%d", phy_memory_mode
);
455 void gdb_handle_set_qemu_phy_mem_mode(GArray
*params
, void *user_ctx
)
458 gdb_put_packet("E22");
462 if (!get_param(params
, 0)->val_ul
) {
467 gdb_put_packet("OK");
470 void gdb_handle_query_rcmd(GArray
*params
, void *user_ctx
)
472 const guint8 zero
= 0;
476 gdb_put_packet("E22");
480 len
= strlen(get_param(params
, 0)->data
);
482 gdb_put_packet("E01");
486 g_assert(gdbserver_state
.mem_buf
->len
== 0);
488 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
489 g_byte_array_append(gdbserver_state
.mem_buf
, &zero
, 1);
490 qemu_chr_be_write(gdbserver_system_state
.mon_chr
,
491 gdbserver_state
.mem_buf
->data
,
492 gdbserver_state
.mem_buf
->len
);
493 gdb_put_packet("OK");
497 * Execution state helpers
500 void gdb_handle_query_attached(GArray
*params
, void *user_ctx
)
505 void gdb_continue(void)
507 if (!runstate_needs_reset()) {
508 trace_gdbstub_op_continue();
514 * Resume execution, per CPU actions.
516 int gdb_continue_partial(char *newstates
)
522 if (!runstate_needs_reset()) {
523 bool step_requested
= false;
525 if (newstates
[cpu
->cpu_index
] == 's') {
526 step_requested
= true;
531 if (vm_prepare_start(step_requested
)) {
536 switch (newstates
[cpu
->cpu_index
]) {
539 break; /* nothing to do here */
541 trace_gdbstub_op_stepping(cpu
->cpu_index
);
542 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
547 trace_gdbstub_op_continue_cpu(cpu
->cpu_index
);
558 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
564 * Signal Handling - in system mode we only need SIGINT and SIGTRAP; other
565 * signals are not yet supported.
573 int gdb_signal_to_target(int sig
)
577 return TARGET_SIGINT
;
579 return TARGET_SIGTRAP
;
586 * Break/Watch point helpers
589 bool gdb_supports_guest_debug(void)
591 const AccelOpsClass
*ops
= cpus_get_accel();
592 if (ops
->supports_guest_debug
) {
593 return ops
->supports_guest_debug();
598 int gdb_breakpoint_insert(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
600 const AccelOpsClass
*ops
= cpus_get_accel();
601 if (ops
->insert_breakpoint
) {
602 return ops
->insert_breakpoint(cs
, type
, addr
, len
);
607 int gdb_breakpoint_remove(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
609 const AccelOpsClass
*ops
= cpus_get_accel();
610 if (ops
->remove_breakpoint
) {
611 return ops
->remove_breakpoint(cs
, type
, addr
, len
);
616 void gdb_breakpoint_remove_all(CPUState
*cs
)
618 const AccelOpsClass
*ops
= cpus_get_accel();
619 if (ops
->remove_all_breakpoints
) {
620 ops
->remove_all_breakpoints(cs
);