xref: /qemu/disas/disas-mon.c (revision 13af3af1)
1e22d3c48SThomas Huth /*
2e22d3c48SThomas Huth  * Functions related to disassembly from the monitor
3e22d3c48SThomas Huth  *
4e22d3c48SThomas Huth  * SPDX-License-Identifier: GPL-2.0-or-later
5e22d3c48SThomas Huth  */
6e22d3c48SThomas Huth 
7e22d3c48SThomas Huth #include "qemu/osdep.h"
8e22d3c48SThomas Huth #include "disas-internal.h"
9e22d3c48SThomas Huth #include "disas/disas.h"
10e22d3c48SThomas Huth #include "exec/memory.h"
11e22d3c48SThomas Huth #include "hw/core/cpu.h"
12e22d3c48SThomas Huth #include "monitor/monitor.h"
13e22d3c48SThomas Huth 
14e22d3c48SThomas Huth static int
physical_read_memory(bfd_vma memaddr,bfd_byte * myaddr,int length,struct disassemble_info * info)15e22d3c48SThomas Huth physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
16e22d3c48SThomas Huth                      struct disassemble_info *info)
17e22d3c48SThomas Huth {
18e22d3c48SThomas Huth     CPUDebug *s = container_of(info, CPUDebug, info);
19e22d3c48SThomas Huth     MemTxResult res;
20e22d3c48SThomas Huth 
21e22d3c48SThomas Huth     res = address_space_read(s->cpu->as, memaddr, MEMTXATTRS_UNSPECIFIED,
22e22d3c48SThomas Huth                              myaddr, length);
23e22d3c48SThomas Huth     return res == MEMTX_OK ? 0 : EIO;
24e22d3c48SThomas Huth }
25e22d3c48SThomas Huth 
26e22d3c48SThomas Huth /* Disassembler for the monitor.  */
monitor_disas(Monitor * mon,CPUState * cpu,uint64_t pc,int nb_insn,bool is_physical)27e22d3c48SThomas Huth void monitor_disas(Monitor *mon, CPUState *cpu, uint64_t pc,
28e22d3c48SThomas Huth                    int nb_insn, bool is_physical)
29e22d3c48SThomas Huth {
30e22d3c48SThomas Huth     int count, i;
31e22d3c48SThomas Huth     CPUDebug s;
32e22d3c48SThomas Huth     g_autoptr(GString) ds = g_string_new("");
33e22d3c48SThomas Huth 
34e22d3c48SThomas Huth     disas_initialize_debug_target(&s, cpu);
35e22d3c48SThomas Huth     s.info.fprintf_func = disas_gstring_printf;
36e22d3c48SThomas Huth     s.info.stream = (FILE *)ds;  /* abuse this slot */
37*13af3af1SRichard Henderson     s.info.show_opcodes = true;
38e22d3c48SThomas Huth 
39e22d3c48SThomas Huth     if (is_physical) {
40e22d3c48SThomas Huth         s.info.read_memory_func = physical_read_memory;
41e22d3c48SThomas Huth     }
42e22d3c48SThomas Huth     s.info.buffer_vma = pc;
43e22d3c48SThomas Huth 
44e22d3c48SThomas Huth     if (s.info.cap_arch >= 0 && cap_disas_monitor(&s.info, pc, nb_insn)) {
45e22d3c48SThomas Huth         monitor_puts(mon, ds->str);
46e22d3c48SThomas Huth         return;
47e22d3c48SThomas Huth     }
48e22d3c48SThomas Huth 
49e22d3c48SThomas Huth     if (!s.info.print_insn) {
50e22d3c48SThomas Huth         monitor_printf(mon, "0x%08" PRIx64
51e22d3c48SThomas Huth                        ": Asm output not supported on this arch\n", pc);
52e22d3c48SThomas Huth         return;
53e22d3c48SThomas Huth     }
54e22d3c48SThomas Huth 
55e22d3c48SThomas Huth     for (i = 0; i < nb_insn; i++) {
56e22d3c48SThomas Huth         g_string_append_printf(ds, "0x%08" PRIx64 ":  ", pc);
57e22d3c48SThomas Huth         count = s.info.print_insn(pc, &s.info);
58e22d3c48SThomas Huth         g_string_append_c(ds, '\n');
59e22d3c48SThomas Huth         if (count < 0) {
60e22d3c48SThomas Huth             break;
61e22d3c48SThomas Huth         }
62e22d3c48SThomas Huth         pc += count;
63e22d3c48SThomas Huth     }
64e22d3c48SThomas Huth 
65e22d3c48SThomas Huth     monitor_puts(mon, ds->str);
66e22d3c48SThomas Huth }
67