blob: dd22ff0fb3a0c14f6579f6ac4542d533d66494d9 [file] [log] [blame]
Alex Bennéeae7467b2022-09-29 12:42:24 +01001/*
Philippe Mathieu-Daudé25f34eb2024-03-13 21:09:38 +01002 * gdb server stub - system specific bits
Alex Bennéeae7467b2022-09-29 12:42:24 +01003 *
4 * Debug integration depends on support from the individual
5 * accelerators so most of this involves calling the ops helpers.
6 *
Alex Bennée94557622023-03-02 18:57:38 -08007 * Copyright (c) 2003-2005 Fabrice Bellard
Alex Bennéeae7467b2022-09-29 12:42:24 +01008 * Copyright (c) 2022 Linaro Ltd
9 *
Philippe Mathieu-Daudéb14d0642024-09-11 17:12:04 +020010 * SPDX-License-Identifier: LGPL-2.0-or-later
Alex Bennéeae7467b2022-09-29 12:42:24 +010011 */
12
13#include "qemu/osdep.h"
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -080014#include "qapi/error.h"
15#include "qemu/error-report.h"
16#include "qemu/cutils.h"
Alex Bennéeae7467b2022-09-29 12:42:24 +010017#include "exec/gdbstub.h"
Alex Bennéec5660802023-03-02 18:57:57 -080018#include "gdbstub/syscalls.h"
Gustavo Romero133f2022024-07-05 09:40:38 +010019#include "gdbstub/commands.h"
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -080020#include "exec/hwaddr.h"
21#include "exec/tb-flush.h"
Philippe Mathieu-Daudé0f665362025-01-23 13:39:05 +010022#include "system/accel-ops.h"
Philippe Mathieu-Daudé32cad1f2024-12-03 15:20:13 +010023#include "system/cpus.h"
24#include "system/runstate.h"
25#include "system/replay.h"
Philippe Mathieu-Daudé270dbee2025-01-23 14:09:19 +010026#include "system/tcg.h"
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -080027#include "hw/core/cpu.h"
28#include "hw/cpu/cluster.h"
29#include "hw/boards.h"
30#include "chardev/char.h"
31#include "chardev/char-fe.h"
32#include "monitor/monitor.h"
33#include "trace.h"
Alex Bennéeae7467b2022-09-29 12:42:24 +010034#include "internals.h"
35
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -080036/* System emulation specific state */
37typedef struct {
38 CharBackend chr;
39 Chardev *mon_chr;
40} GDBSystemState;
41
42GDBSystemState gdbserver_system_state;
43
44static void reset_gdbserver_state(void)
45{
46 g_free(gdbserver_state.processes);
47 gdbserver_state.processes = NULL;
48 gdbserver_state.process_num = 0;
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -030049 gdbserver_state.allow_stop_reply = false;
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -080050}
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 */
58int gdb_get_cpu_index(CPUState *cpu)
59{
60 return cpu->cpu_index + 1;
61}
62
63/*
Alex Bennéea7e0f9b2023-03-02 18:57:49 -080064 * We check the status of the last message in the chardev receive code
65 */
66bool gdb_got_immediate_ack(void)
67{
68 return true;
69}
70
71/*
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -080072 * 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
77void 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
86static 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();
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800103 break;
104 default:
105 break;
106 }
107}
108
Alex Bennée131f3872023-03-02 18:58:01 -0800109/*
Philippe Mathieu-Daudé3f7d1bd2023-10-04 11:06:22 +0200110 * In system-mode we stop the VM and wait to send the syscall packet
Alex Bennée131f3872023-03-02 18:58:01 -0800111 * 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 */
117void gdb_syscall_handling(const char *syscall_packet)
118{
119 vm_stop(RUN_STATE_DEBUG);
120 qemu_cpu_kick(gdbserver_state.c_cpu);
121}
122
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800123static 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 const char *type;
129 int ret;
130
131 if (running || gdbserver_state.state == RS_INACTIVE) {
132 return;
133 }
Alex Bennéec5660802023-03-02 18:57:57 -0800134
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800135 /* Is there a GDB syscall waiting to be sent? */
Alex Bennéec5660802023-03-02 18:57:57 -0800136 if (gdb_handled_syscall()) {
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800137 return;
138 }
139
140 if (cpu == NULL) {
141 /* No process attached */
142 return;
143 }
144
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -0300145 if (!gdbserver_state.allow_stop_reply) {
146 return;
147 }
148
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800149 gdb_append_thread_id(cpu, tid);
150
151 switch (state) {
152 case RUN_STATE_DEBUG:
153 if (cpu->watchpoint_hit) {
154 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
155 case BP_MEM_READ:
156 type = "r";
157 break;
158 case BP_MEM_ACCESS:
159 type = "a";
160 break;
161 default:
162 type = "";
163 break;
164 }
165 trace_gdbstub_hit_watchpoint(type,
166 gdb_get_cpu_index(cpu),
167 cpu->watchpoint_hit->vaddr);
168 g_string_printf(buf, "T%02xthread:%s;%swatch:%" VADDR_PRIx ";",
169 GDB_SIGNAL_TRAP, tid->str, type,
170 cpu->watchpoint_hit->vaddr);
171 cpu->watchpoint_hit = NULL;
172 goto send_packet;
173 } else {
174 trace_gdbstub_hit_break();
175 }
Philippe Mathieu-Daudé270dbee2025-01-23 14:09:19 +0100176 if (tcg_enabled()) {
177 tb_flush(cpu);
178 }
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800179 ret = GDB_SIGNAL_TRAP;
180 break;
181 case RUN_STATE_PAUSED:
182 trace_gdbstub_hit_paused();
183 ret = GDB_SIGNAL_INT;
184 break;
185 case RUN_STATE_SHUTDOWN:
186 trace_gdbstub_hit_shutdown();
187 ret = GDB_SIGNAL_QUIT;
188 break;
189 case RUN_STATE_IO_ERROR:
190 trace_gdbstub_hit_io_error();
Alex Bennée5dcf6332023-12-01 09:36:27 +0000191 ret = GDB_SIGNAL_STOP;
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800192 break;
193 case RUN_STATE_WATCHDOG:
194 trace_gdbstub_hit_watchdog();
195 ret = GDB_SIGNAL_ALRM;
196 break;
197 case RUN_STATE_INTERNAL_ERROR:
198 trace_gdbstub_hit_internal_error();
199 ret = GDB_SIGNAL_ABRT;
200 break;
201 case RUN_STATE_SAVE_VM:
202 case RUN_STATE_RESTORE_VM:
203 return;
204 case RUN_STATE_FINISH_MIGRATE:
205 ret = GDB_SIGNAL_XCPU;
206 break;
207 default:
208 trace_gdbstub_hit_unknown(state);
209 ret = GDB_SIGNAL_UNKNOWN;
210 break;
211 }
212 gdb_set_stop_cpu(cpu);
213 g_string_printf(buf, "T%02xthread:%s;", ret, tid->str);
214
215send_packet:
216 gdb_put_packet(buf->str);
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -0300217 gdbserver_state.allow_stop_reply = false;
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800218
219 /* disable single step if it was enabled */
220 cpu_single_step(cpu, 0);
221}
222
223#ifndef _WIN32
224static void gdb_sigterm_handler(int signal)
225{
226 if (runstate_is_running()) {
227 vm_stop(RUN_STATE_PAUSED);
228 }
229}
230#endif
231
232static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
233{
234 g_autoptr(GString) hex_buf = g_string_new("O");
235 gdb_memtohex(hex_buf, buf, len);
236 gdb_put_packet(hex_buf->str);
237 return len;
238}
239
240static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
241 bool *be_opened, Error **errp)
242{
243 *be_opened = false;
244}
245
246static void char_gdb_class_init(ObjectClass *oc, void *data)
247{
248 ChardevClass *cc = CHARDEV_CLASS(oc);
249
250 cc->internal = true;
251 cc->open = gdb_monitor_open;
252 cc->chr_write = gdb_monitor_write;
253}
254
255#define TYPE_CHARDEV_GDB "chardev-gdb"
256
257static const TypeInfo char_gdb_type_info = {
258 .name = TYPE_CHARDEV_GDB,
259 .parent = TYPE_CHARDEV,
260 .class_init = char_gdb_class_init,
261};
262
263static int gdb_chr_can_receive(void *opaque)
264{
265 /*
266 * We can handle an arbitrarily large amount of data.
267 * Pick the maximum packet size, which is as good as anything.
268 */
269 return MAX_PACKET_LENGTH;
270}
271
272static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
273{
274 int i;
275
276 for (i = 0; i < size; i++) {
277 gdb_read_byte(buf[i]);
278 }
279}
280
281static int find_cpu_clusters(Object *child, void *opaque)
282{
283 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
284 GDBState *s = (GDBState *) opaque;
285 CPUClusterState *cluster = CPU_CLUSTER(child);
286 GDBProcess *process;
287
288 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
289
290 process = &s->processes[s->process_num - 1];
291
292 /*
293 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
294 * runtime, we enforce here that the machine does not use a cluster ID
295 * that would lead to PID 0.
296 */
297 assert(cluster->cluster_id != UINT32_MAX);
298 process->pid = cluster->cluster_id + 1;
299 process->attached = false;
Akihiko Odakiaf2e06c2023-10-09 17:40:47 +0100300 process->target_xml = NULL;
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800301
302 return 0;
303 }
304
305 return object_child_foreach(child, find_cpu_clusters, opaque);
306}
307
308static int pid_order(const void *a, const void *b)
309{
310 GDBProcess *pa = (GDBProcess *) a;
311 GDBProcess *pb = (GDBProcess *) b;
312
313 if (pa->pid < pb->pid) {
314 return -1;
315 } else if (pa->pid > pb->pid) {
316 return 1;
317 } else {
318 return 0;
319 }
320}
321
322static void create_processes(GDBState *s)
323{
324 object_child_foreach(object_get_root(), find_cpu_clusters, s);
325
326 if (gdbserver_state.processes) {
327 /* Sort by PID */
328 qsort(gdbserver_state.processes,
329 gdbserver_state.process_num,
330 sizeof(gdbserver_state.processes[0]),
331 pid_order);
332 }
333
334 gdb_create_default_process(s);
335}
336
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000337bool gdbserver_start(const char *device, Error **errp)
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800338{
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800339 Chardev *chr = NULL;
340 Chardev *mon_chr;
Alex Bennéed2fe2262023-06-30 19:04:13 +0100341 g_autoptr(GString) cs = g_string_new(device);
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800342
343 if (!first_cpu) {
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000344 error_setg(errp, "gdbstub: meaningless to attach gdb to a "
345 "machine without any CPU.");
346 return false;
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800347 }
348
349 if (!gdb_supports_guest_debug()) {
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000350 error_setg(errp, "gdbstub: current accelerator doesn't "
351 "support guest debugging");
352 return false;
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800353 }
354
Alex Bennéed2fe2262023-06-30 19:04:13 +0100355 if (cs->len == 0) {
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000356 error_setg(errp, "gdbstub: missing connection string");
357 return false;
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800358 }
Alex Bennéed2fe2262023-06-30 19:04:13 +0100359
360 trace_gdbstub_op_start(cs->str);
361
362 if (g_strcmp0(cs->str, "none") != 0) {
363 if (g_str_has_prefix(cs->str, "tcp:")) {
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800364 /* enforce required TCP attributes */
Alex Bennéed2fe2262023-06-30 19:04:13 +0100365 g_string_append_printf(cs, ",wait=off,nodelay=on,server=on");
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800366 }
367#ifndef _WIN32
368 else if (strcmp(device, "stdio") == 0) {
369 struct sigaction act;
370
371 memset(&act, 0, sizeof(act));
372 act.sa_handler = gdb_sigterm_handler;
373 sigaction(SIGINT, &act, NULL);
374 }
375#endif
376 /*
377 * FIXME: it's a bit weird to allow using a mux chardev here
378 * and implicitly setup a monitor. We may want to break this.
379 */
Alex Bennéed2fe2262023-06-30 19:04:13 +0100380 chr = qemu_chr_new_noreplay("gdb", cs->str, true, NULL);
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800381 if (!chr) {
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000382 error_setg(errp, "gdbstub: couldn't create chardev");
383 return false;
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800384 }
385 }
386
387 if (!gdbserver_state.init) {
388 gdb_init_gdbserver_state();
389
390 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
391
392 /* Initialize a monitor terminal for gdb */
393 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
394 NULL, NULL, &error_abort);
395 monitor_init_hmp(mon_chr, false, &error_abort);
396 } else {
397 qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
398 mon_chr = gdbserver_system_state.mon_chr;
399 reset_gdbserver_state();
400 }
401
402 create_processes(&gdbserver_state);
403
404 if (chr) {
405 qemu_chr_fe_init(&gdbserver_system_state.chr, chr, &error_abort);
406 qemu_chr_fe_set_handlers(&gdbserver_system_state.chr,
407 gdb_chr_can_receive,
408 gdb_chr_receive, gdb_chr_event,
409 NULL, &gdbserver_state, NULL, true);
410 }
411 gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE;
412 gdbserver_system_state.mon_chr = mon_chr;
Alex Bennéec5660802023-03-02 18:57:57 -0800413 gdb_syscall_reset();
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800414
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000415 return true;
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800416}
417
418static void register_types(void)
419{
420 type_register_static(&char_gdb_type_info);
421}
422
423type_init(register_types);
424
425/* Tell the remote gdb that the process has exited. */
426void gdb_exit(int code)
427{
428 char buf[4];
429
430 if (!gdbserver_state.init) {
431 return;
432 }
433
434 trace_gdbstub_op_exiting((uint8_t)code);
435
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -0300436 if (gdbserver_state.allow_stop_reply) {
437 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
438 gdb_put_packet(buf);
439 gdbserver_state.allow_stop_reply = false;
440 }
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800441
442 qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
443}
444
Clément Chigote2162562023-10-03 09:14:27 +0200445void gdb_qemu_exit(int code)
446{
447 qemu_system_shutdown_request_with_code(SHUTDOWN_CAUSE_GUEST_SHUTDOWN,
448 code);
449}
450
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800451/*
Alex Bennée589a5862023-03-02 18:57:51 -0800452 * Memory access
453 */
454static int phy_memory_mode;
455
456int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
457 uint8_t *buf, int len, bool is_write)
458{
Alex Bennée589a5862023-03-02 18:57:51 -0800459 if (phy_memory_mode) {
460 if (is_write) {
461 cpu_physical_memory_write(addr, buf, len);
462 } else {
463 cpu_physical_memory_read(addr, buf, len);
464 }
465 return 0;
466 }
467
Philippe Mathieu-Daudé0368d8d2025-01-21 12:11:35 +0100468 if (cpu->cc->memory_rw_debug) {
469 return cpu->cc->memory_rw_debug(cpu, addr, buf, len, is_write);
Alex Bennée589a5862023-03-02 18:57:51 -0800470 }
471
472 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
473}
474
Alex Bennée7ea0c332023-03-02 18:57:52 -0800475/*
476 * cpu helpers
477 */
478
479unsigned int gdb_get_max_cpus(void)
480{
481 MachineState *ms = MACHINE(qdev_get_machine());
482 return ms->smp.max_cpus;
483}
Alex Bennée589a5862023-03-02 18:57:51 -0800484
Alex Bennée505601d2023-03-02 18:57:53 -0800485bool gdb_can_reverse(void)
486{
487 return replay_mode == REPLAY_MODE_PLAY;
488}
489
Alex Bennée589a5862023-03-02 18:57:51 -0800490/*
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800491 * Softmmu specific command helpers
492 */
Alex Bennée589a5862023-03-02 18:57:51 -0800493
494void gdb_handle_query_qemu_phy_mem_mode(GArray *params,
Philippe Mathieu-Daudé0eaf7fb2024-03-13 21:12:36 +0100495 void *ctx)
Alex Bennée589a5862023-03-02 18:57:51 -0800496{
497 g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
498 gdb_put_strbuf();
499}
500
Philippe Mathieu-Daudé0eaf7fb2024-03-13 21:12:36 +0100501void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx)
Alex Bennée589a5862023-03-02 18:57:51 -0800502{
503 if (!params->len) {
504 gdb_put_packet("E22");
505 return;
506 }
507
Gustavo Romero133f2022024-07-05 09:40:38 +0100508 if (!gdb_get_cmd_param(params, 0)->val_ul) {
Alex Bennée589a5862023-03-02 18:57:51 -0800509 phy_memory_mode = 0;
510 } else {
511 phy_memory_mode = 1;
512 }
513 gdb_put_packet("OK");
514}
515
Philippe Mathieu-Daudé0eaf7fb2024-03-13 21:12:36 +0100516void gdb_handle_query_rcmd(GArray *params, void *ctx)
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800517{
518 const guint8 zero = 0;
519 int len;
520
521 if (!params->len) {
522 gdb_put_packet("E22");
523 return;
524 }
525
Gustavo Romero133f2022024-07-05 09:40:38 +0100526 len = strlen(gdb_get_cmd_param(params, 0)->data);
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800527 if (len % 2) {
528 gdb_put_packet("E01");
529 return;
530 }
531
532 g_assert(gdbserver_state.mem_buf->len == 0);
533 len = len / 2;
Gustavo Romero133f2022024-07-05 09:40:38 +0100534 gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 0)->data, len);
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800535 g_byte_array_append(gdbserver_state.mem_buf, &zero, 1);
536 qemu_chr_be_write(gdbserver_system_state.mon_chr,
537 gdbserver_state.mem_buf->data,
538 gdbserver_state.mem_buf->len);
539 gdb_put_packet("OK");
540}
541
542/*
Alex Bennéed96bf492023-03-02 18:57:47 -0800543 * Execution state helpers
544 */
545
Philippe Mathieu-Daudé0eaf7fb2024-03-13 21:12:36 +0100546void gdb_handle_query_attached(GArray *params, void *ctx)
Alex Bennée8a2025b2023-03-02 18:57:50 -0800547{
548 gdb_put_packet("1");
549}
550
Alex Bennéed96bf492023-03-02 18:57:47 -0800551void gdb_continue(void)
552{
553 if (!runstate_needs_reset()) {
554 trace_gdbstub_op_continue();
555 vm_start();
556 }
557}
558
559/*
560 * Resume execution, per CPU actions.
561 */
562int gdb_continue_partial(char *newstates)
563{
564 CPUState *cpu;
565 int res = 0;
566 int flag = 0;
567
568 if (!runstate_needs_reset()) {
569 bool step_requested = false;
570 CPU_FOREACH(cpu) {
571 if (newstates[cpu->cpu_index] == 's') {
572 step_requested = true;
573 break;
574 }
575 }
576
577 if (vm_prepare_start(step_requested)) {
578 return 0;
579 }
580
581 CPU_FOREACH(cpu) {
582 switch (newstates[cpu->cpu_index]) {
583 case 0:
584 case 1:
585 break; /* nothing to do here */
586 case 's':
587 trace_gdbstub_op_stepping(cpu->cpu_index);
588 cpu_single_step(cpu, gdbserver_state.sstep_flags);
589 cpu_resume(cpu);
590 flag = 1;
591 break;
592 case 'c':
593 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
594 cpu_resume(cpu);
595 flag = 1;
596 break;
597 default:
598 res = -1;
599 break;
600 }
601 }
602 }
603 if (flag) {
604 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
605 }
606 return res;
607}
608
609/*
610 * Signal Handling - in system mode we only need SIGINT and SIGTRAP; other
611 * signals are not yet supported.
612 */
613
614enum {
615 TARGET_SIGINT = 2,
616 TARGET_SIGTRAP = 5
617};
618
Alex Bennéed96bf492023-03-02 18:57:47 -0800619int gdb_signal_to_target(int sig)
620{
Alex Bennéeccd4c7c2023-03-02 18:57:48 -0800621 switch (sig) {
622 case 2:
623 return TARGET_SIGINT;
624 case 5:
625 return TARGET_SIGTRAP;
626 default:
Alex Bennéed96bf492023-03-02 18:57:47 -0800627 return -1;
628 }
629}
630
631/*
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800632 * Break/Watch point helpers
633 */
634
Alex Bennéea48e7d92022-09-29 12:42:25 +0100635bool gdb_supports_guest_debug(void)
636{
637 const AccelOpsClass *ops = cpus_get_accel();
638 if (ops->supports_guest_debug) {
639 return ops->supports_guest_debug();
640 }
641 return false;
642}
643
Philippe Mathieu-Daudé55b5b8e2022-12-06 16:20:27 +0100644int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len)
Alex Bennéeae7467b2022-09-29 12:42:24 +0100645{
646 const AccelOpsClass *ops = cpus_get_accel();
647 if (ops->insert_breakpoint) {
648 return ops->insert_breakpoint(cs, type, addr, len);
649 }
650 return -ENOSYS;
651}
652
Philippe Mathieu-Daudé55b5b8e2022-12-06 16:20:27 +0100653int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len)
Alex Bennéeae7467b2022-09-29 12:42:24 +0100654{
655 const AccelOpsClass *ops = cpus_get_accel();
656 if (ops->remove_breakpoint) {
657 return ops->remove_breakpoint(cs, type, addr, len);
658 }
659 return -ENOSYS;
660}
661
662void gdb_breakpoint_remove_all(CPUState *cs)
663{
664 const AccelOpsClass *ops = cpus_get_accel();
665 if (ops->remove_all_breakpoints) {
666 ops->remove_all_breakpoints(cs);
667 }
668}