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 * GDB Connection management. For system emulation we do all of this
60 * via our existing Chardev infrastructure which allows us to support
61 * network and unix sockets.
64 void gdb_put_buffer(const uint8_t *buf
, int len
)
67 * XXX this blocks entire thread. Rewrite to use
68 * qemu_chr_fe_write and background I/O callbacks
70 qemu_chr_fe_write_all(&gdbserver_system_state
.chr
, buf
, len
);
73 static void gdb_chr_event(void *opaque
, QEMUChrEvent event
)
76 GDBState
*s
= (GDBState
*) opaque
;
79 case CHR_EVENT_OPENED
:
80 /* Start with first process attached, others detached */
81 for (i
= 0; i
< s
->process_num
; i
++) {
82 s
->processes
[i
].attached
= !i
;
85 s
->c_cpu
= gdb_first_attached_cpu();
88 vm_stop(RUN_STATE_PAUSED
);
89 replay_gdb_attached();
97 static void gdb_vm_state_change(void *opaque
, bool running
, RunState state
)
99 CPUState
*cpu
= gdbserver_state
.c_cpu
;
100 g_autoptr(GString
) buf
= g_string_new(NULL
);
101 g_autoptr(GString
) tid
= g_string_new(NULL
);
105 if (running
|| gdbserver_state
.state
== RS_INACTIVE
) {
108 /* Is there a GDB syscall waiting to be sent? */
109 if (gdbserver_state
.current_syscall_cb
) {
110 gdb_put_packet(gdbserver_state
.syscall_buf
);
115 /* No process attached */
119 gdb_append_thread_id(cpu
, tid
);
122 case RUN_STATE_DEBUG
:
123 if (cpu
->watchpoint_hit
) {
124 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
135 trace_gdbstub_hit_watchpoint(type
,
136 gdb_get_cpu_index(cpu
),
137 cpu
->watchpoint_hit
->vaddr
);
138 g_string_printf(buf
, "T%02xthread:%s;%swatch:%" VADDR_PRIx
";",
139 GDB_SIGNAL_TRAP
, tid
->str
, type
,
140 cpu
->watchpoint_hit
->vaddr
);
141 cpu
->watchpoint_hit
= NULL
;
144 trace_gdbstub_hit_break();
147 ret
= GDB_SIGNAL_TRAP
;
149 case RUN_STATE_PAUSED
:
150 trace_gdbstub_hit_paused();
151 ret
= GDB_SIGNAL_INT
;
153 case RUN_STATE_SHUTDOWN
:
154 trace_gdbstub_hit_shutdown();
155 ret
= GDB_SIGNAL_QUIT
;
157 case RUN_STATE_IO_ERROR
:
158 trace_gdbstub_hit_io_error();
161 case RUN_STATE_WATCHDOG
:
162 trace_gdbstub_hit_watchdog();
163 ret
= GDB_SIGNAL_ALRM
;
165 case RUN_STATE_INTERNAL_ERROR
:
166 trace_gdbstub_hit_internal_error();
167 ret
= GDB_SIGNAL_ABRT
;
169 case RUN_STATE_SAVE_VM
:
170 case RUN_STATE_RESTORE_VM
:
172 case RUN_STATE_FINISH_MIGRATE
:
173 ret
= GDB_SIGNAL_XCPU
;
176 trace_gdbstub_hit_unknown(state
);
177 ret
= GDB_SIGNAL_UNKNOWN
;
180 gdb_set_stop_cpu(cpu
);
181 g_string_printf(buf
, "T%02xthread:%s;", ret
, tid
->str
);
184 gdb_put_packet(buf
->str
);
186 /* disable single step if it was enabled */
187 cpu_single_step(cpu
, 0);
191 static void gdb_sigterm_handler(int signal
)
193 if (runstate_is_running()) {
194 vm_stop(RUN_STATE_PAUSED
);
199 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
201 g_autoptr(GString
) hex_buf
= g_string_new("O");
202 gdb_memtohex(hex_buf
, buf
, len
);
203 gdb_put_packet(hex_buf
->str
);
207 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
208 bool *be_opened
, Error
**errp
)
213 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
215 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
218 cc
->open
= gdb_monitor_open
;
219 cc
->chr_write
= gdb_monitor_write
;
222 #define TYPE_CHARDEV_GDB "chardev-gdb"
224 static const TypeInfo char_gdb_type_info
= {
225 .name
= TYPE_CHARDEV_GDB
,
226 .parent
= TYPE_CHARDEV
,
227 .class_init
= char_gdb_class_init
,
230 static int gdb_chr_can_receive(void *opaque
)
233 * We can handle an arbitrarily large amount of data.
234 * Pick the maximum packet size, which is as good as anything.
236 return MAX_PACKET_LENGTH
;
239 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
243 for (i
= 0; i
< size
; i
++) {
244 gdb_read_byte(buf
[i
]);
248 static int find_cpu_clusters(Object
*child
, void *opaque
)
250 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
251 GDBState
*s
= (GDBState
*) opaque
;
252 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
255 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
257 process
= &s
->processes
[s
->process_num
- 1];
260 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
261 * runtime, we enforce here that the machine does not use a cluster ID
262 * that would lead to PID 0.
264 assert(cluster
->cluster_id
!= UINT32_MAX
);
265 process
->pid
= cluster
->cluster_id
+ 1;
266 process
->attached
= false;
267 process
->target_xml
[0] = '\0';
272 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
275 static int pid_order(const void *a
, const void *b
)
277 GDBProcess
*pa
= (GDBProcess
*) a
;
278 GDBProcess
*pb
= (GDBProcess
*) b
;
280 if (pa
->pid
< pb
->pid
) {
282 } else if (pa
->pid
> pb
->pid
) {
289 static void create_processes(GDBState
*s
)
291 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
293 if (gdbserver_state
.processes
) {
295 qsort(gdbserver_state
.processes
,
296 gdbserver_state
.process_num
,
297 sizeof(gdbserver_state
.processes
[0]),
301 gdb_create_default_process(s
);
304 int gdbserver_start(const char *device
)
306 trace_gdbstub_op_start(device
);
308 char gdbstub_device_name
[128];
313 error_report("gdbstub: meaningless to attach gdb to a "
314 "machine without any CPU.");
318 if (!gdb_supports_guest_debug()) {
319 error_report("gdbstub: current accelerator doesn't "
320 "support guest debugging");
327 if (strcmp(device
, "none") != 0) {
328 if (strstart(device
, "tcp:", NULL
)) {
329 /* enforce required TCP attributes */
330 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
331 "%s,wait=off,nodelay=on,server=on", device
);
332 device
= gdbstub_device_name
;
335 else if (strcmp(device
, "stdio") == 0) {
336 struct sigaction act
;
338 memset(&act
, 0, sizeof(act
));
339 act
.sa_handler
= gdb_sigterm_handler
;
340 sigaction(SIGINT
, &act
, NULL
);
344 * FIXME: it's a bit weird to allow using a mux chardev here
345 * and implicitly setup a monitor. We may want to break this.
347 chr
= qemu_chr_new_noreplay("gdb", device
, true, NULL
);
353 if (!gdbserver_state
.init
) {
354 gdb_init_gdbserver_state();
356 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
358 /* Initialize a monitor terminal for gdb */
359 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
360 NULL
, NULL
, &error_abort
);
361 monitor_init_hmp(mon_chr
, false, &error_abort
);
363 qemu_chr_fe_deinit(&gdbserver_system_state
.chr
, true);
364 mon_chr
= gdbserver_system_state
.mon_chr
;
365 reset_gdbserver_state();
368 create_processes(&gdbserver_state
);
371 qemu_chr_fe_init(&gdbserver_system_state
.chr
, chr
, &error_abort
);
372 qemu_chr_fe_set_handlers(&gdbserver_system_state
.chr
,
374 gdb_chr_receive
, gdb_chr_event
,
375 NULL
, &gdbserver_state
, NULL
, true);
377 gdbserver_state
.state
= chr
? RS_IDLE
: RS_INACTIVE
;
378 gdbserver_system_state
.mon_chr
= mon_chr
;
379 gdbserver_state
.current_syscall_cb
= NULL
;
384 static void register_types(void)
386 type_register_static(&char_gdb_type_info
);
389 type_init(register_types
);
391 /* Tell the remote gdb that the process has exited. */
392 void gdb_exit(int code
)
396 if (!gdbserver_state
.init
) {
400 trace_gdbstub_op_exiting((uint8_t)code
);
402 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
405 qemu_chr_fe_deinit(&gdbserver_system_state
.chr
, true);
409 * Softmmu specific command helpers
411 void gdb_handle_query_rcmd(GArray
*params
, void *user_ctx
)
413 const guint8 zero
= 0;
417 gdb_put_packet("E22");
421 len
= strlen(get_param(params
, 0)->data
);
423 gdb_put_packet("E01");
427 g_assert(gdbserver_state
.mem_buf
->len
== 0);
429 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
430 g_byte_array_append(gdbserver_state
.mem_buf
, &zero
, 1);
431 qemu_chr_be_write(gdbserver_system_state
.mon_chr
,
432 gdbserver_state
.mem_buf
->data
,
433 gdbserver_state
.mem_buf
->len
);
434 gdb_put_packet("OK");
438 * Break/Watch point helpers
441 bool gdb_supports_guest_debug(void)
443 const AccelOpsClass
*ops
= cpus_get_accel();
444 if (ops
->supports_guest_debug
) {
445 return ops
->supports_guest_debug();
450 int gdb_breakpoint_insert(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
452 const AccelOpsClass
*ops
= cpus_get_accel();
453 if (ops
->insert_breakpoint
) {
454 return ops
->insert_breakpoint(cs
, type
, addr
, len
);
459 int gdb_breakpoint_remove(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
461 const AccelOpsClass
*ops
= cpus_get_accel();
462 if (ops
->remove_breakpoint
) {
463 return ops
->remove_breakpoint(cs
, type
, addr
, len
);
468 void gdb_breakpoint_remove_all(CPUState
*cs
)
470 const AccelOpsClass
*ops
= cpus_get_accel();
471 if (ops
->remove_all_breakpoints
) {
472 ops
->remove_all_breakpoints(cs
);