qemu-iotests: Test qemu-img bitmap/commit exit code on error
[qemu.git] / disas.c
blob3b31315f40388ad8012252db7faf30ffea1e8215
1 /* General "disassemble this chunk" code. Used for debugging. */
2 #include "qemu/osdep.h"
3 #include "disas/dis-asm.h"
4 #include "elf.h"
5 #include "qemu/qemu-print.h"
7 #include "disas/disas.h"
8 #include "disas/capstone.h"
10 typedef struct CPUDebug {
11 struct disassemble_info info;
12 CPUState *cpu;
13 } CPUDebug;
15 /* Filled in by elfload.c. Simplistic, but will do for now. */
16 struct syminfo *syminfos = NULL;
19 * Get LENGTH bytes from info's buffer, at host address memaddr.
20 * Transfer them to myaddr.
22 static int host_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
23 struct disassemble_info *info)
25 if (memaddr < info->buffer_vma
26 || memaddr + length > info->buffer_vma + info->buffer_length) {
27 /* Out of bounds. Use EIO because GDB uses it. */
28 return EIO;
30 memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length);
31 return 0;
35 * Get LENGTH bytes from info's buffer, at target address memaddr.
36 * Transfer them to myaddr.
38 static int target_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
39 struct disassemble_info *info)
41 CPUDebug *s = container_of(info, CPUDebug, info);
42 int r = cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
43 return r ? EIO : 0;
47 * Print an error message. We can assume that this is in response to
48 * an error return from {host,target}_read_memory.
50 static void perror_memory(int status, bfd_vma memaddr,
51 struct disassemble_info *info)
53 if (status != EIO) {
54 /* Can't happen. */
55 info->fprintf_func(info->stream, "Unknown error %d\n", status);
56 } else {
57 /* Address between memaddr and memaddr + len was out of bounds. */
58 info->fprintf_func(info->stream,
59 "Address 0x%" PRIx64 " is out of bounds.\n",
60 memaddr);
64 /* Print address in hex. */
65 static void print_address(bfd_vma addr, struct disassemble_info *info)
67 info->fprintf_func(info->stream, "0x%" PRIx64, addr);
70 /* Print address in hex, truncated to the width of a host virtual address. */
71 static void host_print_address(bfd_vma addr, struct disassemble_info *info)
73 print_address((uintptr_t)addr, info);
76 /* Stub prevents some fruitless earching in optabs disassemblers. */
77 static int symbol_at_address(bfd_vma addr, struct disassemble_info *info)
79 return 1;
82 static int print_insn_objdump(bfd_vma pc, disassemble_info *info,
83 const char *prefix)
85 int i, n = info->buffer_length;
86 g_autofree uint8_t *buf = g_malloc(n);
88 if (info->read_memory_func(pc, buf, n, info) == 0) {
89 for (i = 0; i < n; ++i) {
90 if (i % 32 == 0) {
91 info->fprintf_func(info->stream, "\n%s: ", prefix);
93 info->fprintf_func(info->stream, "%02x", buf[i]);
95 } else {
96 info->fprintf_func(info->stream, "unable to read memory");
98 return n;
101 static int print_insn_od_host(bfd_vma pc, disassemble_info *info)
103 return print_insn_objdump(pc, info, "OBJD-H");
106 static int print_insn_od_target(bfd_vma pc, disassemble_info *info)
108 return print_insn_objdump(pc, info, "OBJD-T");
111 static void initialize_debug(CPUDebug *s)
113 memset(s, 0, sizeof(*s));
114 s->info.arch = bfd_arch_unknown;
115 s->info.cap_arch = -1;
116 s->info.cap_insn_unit = 4;
117 s->info.cap_insn_split = 4;
118 s->info.memory_error_func = perror_memory;
119 s->info.symbol_at_address_func = symbol_at_address;
122 static void initialize_debug_target(CPUDebug *s, CPUState *cpu)
124 initialize_debug(s);
126 s->cpu = cpu;
127 s->info.read_memory_func = target_read_memory;
128 s->info.print_address_func = print_address;
129 #if TARGET_BIG_ENDIAN
130 s->info.endian = BFD_ENDIAN_BIG;
131 #else
132 s->info.endian = BFD_ENDIAN_LITTLE;
133 #endif
135 CPUClass *cc = CPU_GET_CLASS(cpu);
136 if (cc->disas_set_info) {
137 cc->disas_set_info(cpu, &s->info);
141 static void initialize_debug_host(CPUDebug *s)
143 initialize_debug(s);
145 s->info.read_memory_func = host_read_memory;
146 s->info.print_address_func = host_print_address;
147 #if HOST_BIG_ENDIAN
148 s->info.endian = BFD_ENDIAN_BIG;
149 #else
150 s->info.endian = BFD_ENDIAN_LITTLE;
151 #endif
152 #if defined(CONFIG_TCG_INTERPRETER)
153 s->info.print_insn = print_insn_tci;
154 #elif defined(__i386__)
155 s->info.mach = bfd_mach_i386_i386;
156 s->info.cap_arch = CS_ARCH_X86;
157 s->info.cap_mode = CS_MODE_32;
158 s->info.cap_insn_unit = 1;
159 s->info.cap_insn_split = 8;
160 #elif defined(__x86_64__)
161 s->info.mach = bfd_mach_x86_64;
162 s->info.cap_arch = CS_ARCH_X86;
163 s->info.cap_mode = CS_MODE_64;
164 s->info.cap_insn_unit = 1;
165 s->info.cap_insn_split = 8;
166 #elif defined(_ARCH_PPC)
167 s->info.cap_arch = CS_ARCH_PPC;
168 # ifdef _ARCH_PPC64
169 s->info.cap_mode = CS_MODE_64;
170 # endif
171 #elif defined(__riscv) && defined(CONFIG_RISCV_DIS)
172 #if defined(_ILP32) || (__riscv_xlen == 32)
173 s->info.print_insn = print_insn_riscv32;
174 #elif defined(_LP64)
175 s->info.print_insn = print_insn_riscv64;
176 #else
177 #error unsupported RISC-V ABI
178 #endif
179 #elif defined(__aarch64__)
180 s->info.cap_arch = CS_ARCH_ARM64;
181 #elif defined(__alpha__)
182 s->info.print_insn = print_insn_alpha;
183 #elif defined(__sparc__)
184 s->info.print_insn = print_insn_sparc;
185 s->info.mach = bfd_mach_sparc_v9b;
186 #elif defined(__arm__)
187 /* TCG only generates code for arm mode. */
188 s->info.cap_arch = CS_ARCH_ARM;
189 #elif defined(__MIPSEB__)
190 s->info.print_insn = print_insn_big_mips;
191 #elif defined(__MIPSEL__)
192 s->info.print_insn = print_insn_little_mips;
193 #elif defined(__m68k__)
194 s->info.print_insn = print_insn_m68k;
195 #elif defined(__s390__)
196 s->info.cap_arch = CS_ARCH_SYSZ;
197 s->info.cap_insn_unit = 2;
198 s->info.cap_insn_split = 6;
199 #elif defined(__hppa__)
200 s->info.print_insn = print_insn_hppa;
201 #endif
204 /* Disassemble this for me please... (debugging). */
205 void target_disas(FILE *out, CPUState *cpu, target_ulong code,
206 target_ulong size)
208 target_ulong pc;
209 int count;
210 CPUDebug s;
212 initialize_debug_target(&s, cpu);
213 s.info.fprintf_func = fprintf;
214 s.info.stream = out;
215 s.info.buffer_vma = code;
216 s.info.buffer_length = size;
218 if (s.info.cap_arch >= 0 && cap_disas_target(&s.info, code, size)) {
219 return;
222 if (s.info.print_insn == NULL) {
223 s.info.print_insn = print_insn_od_target;
226 for (pc = code; size > 0; pc += count, size -= count) {
227 fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
228 count = s.info.print_insn(pc, &s.info);
229 fprintf(out, "\n");
230 if (count < 0)
231 break;
232 if (size < count) {
233 fprintf(out,
234 "Disassembler disagrees with translator over instruction "
235 "decoding\n"
236 "Please report this to qemu-devel@nongnu.org\n");
237 break;
242 static int G_GNUC_PRINTF(2, 3)
243 gstring_printf(FILE *stream, const char *fmt, ...)
245 /* We abuse the FILE parameter to pass a GString. */
246 GString *s = (GString *)stream;
247 int initial_len = s->len;
248 va_list va;
250 va_start(va, fmt);
251 g_string_append_vprintf(s, fmt, va);
252 va_end(va);
254 return s->len - initial_len;
257 static void plugin_print_address(bfd_vma addr, struct disassemble_info *info)
259 /* does nothing */
264 * We should only be dissembling one instruction at a time here. If
265 * there is left over it usually indicates the front end has read more
266 * bytes than it needed.
268 char *plugin_disas(CPUState *cpu, uint64_t addr, size_t size)
270 CPUDebug s;
271 GString *ds = g_string_new(NULL);
273 initialize_debug_target(&s, cpu);
274 s.info.fprintf_func = gstring_printf;
275 s.info.stream = (FILE *)ds; /* abuse this slot */
276 s.info.buffer_vma = addr;
277 s.info.buffer_length = size;
278 s.info.print_address_func = plugin_print_address;
280 if (s.info.cap_arch >= 0 && cap_disas_plugin(&s.info, addr, size)) {
281 ; /* done */
282 } else if (s.info.print_insn) {
283 s.info.print_insn(addr, &s.info);
284 } else {
285 ; /* cannot disassemble -- return empty string */
288 /* Return the buffer, freeing the GString container. */
289 return g_string_free(ds, false);
292 /* Disassemble this for me please... (debugging). */
293 void disas(FILE *out, const void *code, unsigned long size)
295 uintptr_t pc;
296 int count;
297 CPUDebug s;
299 initialize_debug_host(&s);
300 s.info.fprintf_func = fprintf;
301 s.info.stream = out;
302 s.info.buffer = code;
303 s.info.buffer_vma = (uintptr_t)code;
304 s.info.buffer_length = size;
306 if (s.info.cap_arch >= 0 && cap_disas_host(&s.info, code, size)) {
307 return;
310 if (s.info.print_insn == NULL) {
311 s.info.print_insn = print_insn_od_host;
313 for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) {
314 fprintf(out, "0x%08" PRIxPTR ": ", pc);
315 count = s.info.print_insn(pc, &s.info);
316 fprintf(out, "\n");
317 if (count < 0) {
318 break;
324 /* Look up symbol for debugging purpose. Returns "" if unknown. */
325 const char *lookup_symbol(target_ulong orig_addr)
327 const char *symbol = "";
328 struct syminfo *s;
330 for (s = syminfos; s; s = s->next) {
331 symbol = s->lookup_symbol(s, orig_addr);
332 if (symbol[0] != '\0') {
333 break;
337 return symbol;
340 #if !defined(CONFIG_USER_ONLY)
342 #include "monitor/monitor.h"
344 static int
345 physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
346 struct disassemble_info *info)
348 CPUDebug *s = container_of(info, CPUDebug, info);
349 MemTxResult res;
351 res = address_space_read(s->cpu->as, memaddr, MEMTXATTRS_UNSPECIFIED,
352 myaddr, length);
353 return res == MEMTX_OK ? 0 : EIO;
356 /* Disassembler for the monitor. */
357 void monitor_disas(Monitor *mon, CPUState *cpu,
358 target_ulong pc, int nb_insn, int is_physical)
360 int count, i;
361 CPUDebug s;
362 g_autoptr(GString) ds = g_string_new("");
364 initialize_debug_target(&s, cpu);
365 s.info.fprintf_func = gstring_printf;
366 s.info.stream = (FILE *)ds; /* abuse this slot */
368 if (is_physical) {
369 s.info.read_memory_func = physical_read_memory;
371 s.info.buffer_vma = pc;
373 if (s.info.cap_arch >= 0 && cap_disas_monitor(&s.info, pc, nb_insn)) {
374 monitor_puts(mon, ds->str);
375 return;
378 if (!s.info.print_insn) {
379 monitor_printf(mon, "0x" TARGET_FMT_lx
380 ": Asm output not supported on this arch\n", pc);
381 return;
384 for (i = 0; i < nb_insn; i++) {
385 g_string_append_printf(ds, "0x" TARGET_FMT_lx ": ", pc);
386 count = s.info.print_insn(pc, &s.info);
387 g_string_append_c(ds, '\n');
388 if (count < 0) {
389 break;
391 pc += count;
394 monitor_puts(mon, ds->str);
396 #endif