OSDN Git Service

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