master
c 82 lines 2.22 KB
Raw
1 /*
2 * Functions related to disassembly from the monitor
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "qemu/osdep.h"
8 #include "disas-internal.h"
9 #include "disas/disas.h"
10 #include "system/memory.h"
11 #include "hw/core/cpu.h"
12 #include "monitor/monitor.h"
13 #include "monitor/hmp.h"
14
15 /*
16 * Get LENGTH bytes from info's buffer, at target address memaddr.
17 * Transfer them to myaddr.
18 */
19 static int
20 virtual_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
21 struct disassemble_info *info)
22 {
23 CPUDebug *s = container_of(info, CPUDebug, info);
24 int r = cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
25 return r ? EIO : 0;
26 }
27
28 static int
29 physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
30 struct disassemble_info *info)
31 {
32 CPUDebug *s = container_of(info, CPUDebug, info);
33 MemTxResult res;
34
35 res = address_space_read(s->cpu->as, memaddr, MEMTXATTRS_UNSPECIFIED,
36 myaddr, length);
37 return res == MEMTX_OK ? 0 : EIO;
38 }
39
40 /* Disassembler for the monitor. */
41 void monitor_disas(MonitorHMP *hmp, CPUState *cpu, uint64_t pc,
42 int nb_insn, bool is_physical)
43 {
44 int count, i;
45 CPUDebug s;
46 g_autoptr(GString) ds = g_string_new("");
47
48 disas_initialize_debug_target(&s, cpu);
49 s.info.fprintf_func = disas_gstring_printf;
50 s.info.stream = (FILE *)ds; /* abuse this slot */
51 s.info.show_opcodes = true;
52
53 if (is_physical) {
54 s.info.read_memory_func = physical_read_memory;
55 } else {
56 s.info.read_memory_func = virtual_read_memory;
57 }
58 s.info.buffer_vma = pc;
59
60 if (s.info.cap_arch >= 0 && cap_disas_monitor(&s.info, pc, nb_insn)) {
61 monitor_puts(MONITOR(hmp), ds->str);
62 return;
63 }
64
65 if (!s.info.print_insn) {
66 monitor_hmp_printf(hmp, "0x%08" PRIx64
67 ": Asm output not supported on this arch\n", pc);
68 return;
69 }
70
71 for (i = 0; i < nb_insn; i++) {
72 g_string_append_printf(ds, "0x%08" PRIx64 ": ", pc);
73 count = s.info.print_insn(pc, &s.info);
74 g_string_append_c(ds, '\n');
75 if (count < 0) {
76 break;
77 }
78 pc += count;
79 }
80
81 monitor_puts(MONITOR(hmp), ds->str);
82 }