Update version for 1.1-rc0 release
[qemu.git] / disas.c
blob27071c3bc1240aef5020b28d38f711979ceb5208
1 /* General "disassemble this chunk" code. Used for debugging. */
2 #include "config.h"
3 #include "dis-asm.h"
4 #include "elf.h"
5 #include <errno.h>
7 #include "cpu.h"
8 #include "disas.h"
10 /* Filled in by elfload.c. Simplistic, but will do for now. */
11 struct syminfo *syminfos = NULL;
13 /* Get LENGTH bytes from info's buffer, at target address memaddr.
14 Transfer them to myaddr. */
15 int
16 buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
17 struct disassemble_info *info)
19 if (memaddr < info->buffer_vma
20 || memaddr + length > info->buffer_vma + info->buffer_length)
21 /* Out of bounds. Use EIO because GDB uses it. */
22 return EIO;
23 memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length);
24 return 0;
27 /* Get LENGTH bytes from info's buffer, at target address memaddr.
28 Transfer them to myaddr. */
29 static int
30 target_read_memory (bfd_vma memaddr,
31 bfd_byte *myaddr,
32 int length,
33 struct disassemble_info *info)
35 cpu_memory_rw_debug(cpu_single_env, memaddr, myaddr, length, 0);
36 return 0;
39 /* Print an error message. We can assume that this is in response to
40 an error return from buffer_read_memory. */
41 void
42 perror_memory (int status, bfd_vma memaddr, struct disassemble_info *info)
44 if (status != EIO)
45 /* Can't happen. */
46 (*info->fprintf_func) (info->stream, "Unknown error %d\n", status);
47 else
48 /* Actually, address between memaddr and memaddr + len was
49 out of bounds. */
50 (*info->fprintf_func) (info->stream,
51 "Address 0x%" PRIx64 " is out of bounds.\n", memaddr);
54 /* This could be in a separate file, to save miniscule amounts of space
55 in statically linked executables. */
57 /* Just print the address is hex. This is included for completeness even
58 though both GDB and objdump provide their own (to print symbolic
59 addresses). */
61 void
62 generic_print_address (bfd_vma addr, struct disassemble_info *info)
64 (*info->fprintf_func) (info->stream, "0x%" PRIx64, addr);
67 /* Just return the given address. */
69 int
70 generic_symbol_at_address (bfd_vma addr, struct disassemble_info *info)
72 return 1;
75 bfd_vma bfd_getl64 (const bfd_byte *addr)
77 unsigned long long v;
79 v = (unsigned long long) addr[0];
80 v |= (unsigned long long) addr[1] << 8;
81 v |= (unsigned long long) addr[2] << 16;
82 v |= (unsigned long long) addr[3] << 24;
83 v |= (unsigned long long) addr[4] << 32;
84 v |= (unsigned long long) addr[5] << 40;
85 v |= (unsigned long long) addr[6] << 48;
86 v |= (unsigned long long) addr[7] << 56;
87 return (bfd_vma) v;
90 bfd_vma bfd_getl32 (const bfd_byte *addr)
92 unsigned long v;
94 v = (unsigned long) addr[0];
95 v |= (unsigned long) addr[1] << 8;
96 v |= (unsigned long) addr[2] << 16;
97 v |= (unsigned long) addr[3] << 24;
98 return (bfd_vma) v;
101 bfd_vma bfd_getb32 (const bfd_byte *addr)
103 unsigned long v;
105 v = (unsigned long) addr[0] << 24;
106 v |= (unsigned long) addr[1] << 16;
107 v |= (unsigned long) addr[2] << 8;
108 v |= (unsigned long) addr[3];
109 return (bfd_vma) v;
112 bfd_vma bfd_getl16 (const bfd_byte *addr)
114 unsigned long v;
116 v = (unsigned long) addr[0];
117 v |= (unsigned long) addr[1] << 8;
118 return (bfd_vma) v;
121 bfd_vma bfd_getb16 (const bfd_byte *addr)
123 unsigned long v;
125 v = (unsigned long) addr[0] << 24;
126 v |= (unsigned long) addr[1] << 16;
127 return (bfd_vma) v;
130 #ifdef TARGET_ARM
131 static int
132 print_insn_thumb1(bfd_vma pc, disassemble_info *info)
134 return print_insn_arm(pc | 1, info);
136 #endif
138 /* Disassemble this for me please... (debugging). 'flags' has the following
139 values:
140 i386 - 1 means 16 bit code, 2 means 64 bit code
141 arm - bit 0 = thumb, bit 1 = reverse endian
142 ppc - nonzero means little endian
143 other targets - unused
145 void target_disas(FILE *out, target_ulong code, target_ulong size, int flags)
147 target_ulong pc;
148 int count;
149 struct disassemble_info disasm_info;
150 int (*print_insn)(bfd_vma pc, disassemble_info *info);
152 INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
154 disasm_info.read_memory_func = target_read_memory;
155 disasm_info.buffer_vma = code;
156 disasm_info.buffer_length = size;
158 #ifdef TARGET_WORDS_BIGENDIAN
159 disasm_info.endian = BFD_ENDIAN_BIG;
160 #else
161 disasm_info.endian = BFD_ENDIAN_LITTLE;
162 #endif
163 #if defined(TARGET_I386)
164 if (flags == 2)
165 disasm_info.mach = bfd_mach_x86_64;
166 else if (flags == 1)
167 disasm_info.mach = bfd_mach_i386_i8086;
168 else
169 disasm_info.mach = bfd_mach_i386_i386;
170 print_insn = print_insn_i386;
171 #elif defined(TARGET_ARM)
172 if (flags & 1) {
173 print_insn = print_insn_thumb1;
174 } else {
175 print_insn = print_insn_arm;
177 if (flags & 2) {
178 #ifdef TARGET_WORDS_BIGENDIAN
179 disasm_info.endian = BFD_ENDIAN_LITTLE;
180 #else
181 disasm_info.endian = BFD_ENDIAN_BIG;
182 #endif
184 #elif defined(TARGET_SPARC)
185 print_insn = print_insn_sparc;
186 #ifdef TARGET_SPARC64
187 disasm_info.mach = bfd_mach_sparc_v9b;
188 #endif
189 #elif defined(TARGET_PPC)
190 if (flags >> 16)
191 disasm_info.endian = BFD_ENDIAN_LITTLE;
192 if (flags & 0xFFFF) {
193 /* If we have a precise definitions of the instructions set, use it */
194 disasm_info.mach = flags & 0xFFFF;
195 } else {
196 #ifdef TARGET_PPC64
197 disasm_info.mach = bfd_mach_ppc64;
198 #else
199 disasm_info.mach = bfd_mach_ppc;
200 #endif
202 print_insn = print_insn_ppc;
203 #elif defined(TARGET_M68K)
204 print_insn = print_insn_m68k;
205 #elif defined(TARGET_MIPS)
206 #ifdef TARGET_WORDS_BIGENDIAN
207 print_insn = print_insn_big_mips;
208 #else
209 print_insn = print_insn_little_mips;
210 #endif
211 #elif defined(TARGET_SH4)
212 disasm_info.mach = bfd_mach_sh4;
213 print_insn = print_insn_sh;
214 #elif defined(TARGET_ALPHA)
215 disasm_info.mach = bfd_mach_alpha_ev6;
216 print_insn = print_insn_alpha;
217 #elif defined(TARGET_CRIS)
218 if (flags != 32) {
219 disasm_info.mach = bfd_mach_cris_v0_v10;
220 print_insn = print_insn_crisv10;
221 } else {
222 disasm_info.mach = bfd_mach_cris_v32;
223 print_insn = print_insn_crisv32;
225 #elif defined(TARGET_S390X)
226 disasm_info.mach = bfd_mach_s390_64;
227 print_insn = print_insn_s390;
228 #elif defined(TARGET_MICROBLAZE)
229 disasm_info.mach = bfd_arch_microblaze;
230 print_insn = print_insn_microblaze;
231 #elif defined(TARGET_LM32)
232 disasm_info.mach = bfd_mach_lm32;
233 print_insn = print_insn_lm32;
234 #else
235 fprintf(out, "0x" TARGET_FMT_lx
236 ": Asm output not supported on this arch\n", code);
237 return;
238 #endif
240 for (pc = code; size > 0; pc += count, size -= count) {
241 fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
242 count = print_insn(pc, &disasm_info);
243 #if 0
245 int i;
246 uint8_t b;
247 fprintf(out, " {");
248 for(i = 0; i < count; i++) {
249 target_read_memory(pc + i, &b, 1, &disasm_info);
250 fprintf(out, " %02x", b);
252 fprintf(out, " }");
254 #endif
255 fprintf(out, "\n");
256 if (count < 0)
257 break;
258 if (size < count) {
259 fprintf(out,
260 "Disassembler disagrees with translator over instruction "
261 "decoding\n"
262 "Please report this to qemu-devel@nongnu.org\n");
263 break;
268 /* Disassemble this for me please... (debugging). */
269 void disas(FILE *out, void *code, unsigned long size)
271 uintptr_t pc;
272 int count;
273 struct disassemble_info disasm_info;
274 int (*print_insn)(bfd_vma pc, disassemble_info *info);
276 INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
278 disasm_info.buffer = code;
279 disasm_info.buffer_vma = (uintptr_t)code;
280 disasm_info.buffer_length = size;
282 #ifdef HOST_WORDS_BIGENDIAN
283 disasm_info.endian = BFD_ENDIAN_BIG;
284 #else
285 disasm_info.endian = BFD_ENDIAN_LITTLE;
286 #endif
287 #if defined(CONFIG_TCG_INTERPRETER)
288 print_insn = print_insn_tci;
289 #elif defined(__i386__)
290 disasm_info.mach = bfd_mach_i386_i386;
291 print_insn = print_insn_i386;
292 #elif defined(__x86_64__)
293 disasm_info.mach = bfd_mach_x86_64;
294 print_insn = print_insn_i386;
295 #elif defined(_ARCH_PPC)
296 print_insn = print_insn_ppc;
297 #elif defined(__alpha__)
298 print_insn = print_insn_alpha;
299 #elif defined(__sparc__)
300 print_insn = print_insn_sparc;
301 #if defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
302 disasm_info.mach = bfd_mach_sparc_v9b;
303 #endif
304 #elif defined(__arm__)
305 print_insn = print_insn_arm;
306 #elif defined(__MIPSEB__)
307 print_insn = print_insn_big_mips;
308 #elif defined(__MIPSEL__)
309 print_insn = print_insn_little_mips;
310 #elif defined(__m68k__)
311 print_insn = print_insn_m68k;
312 #elif defined(__s390__)
313 print_insn = print_insn_s390;
314 #elif defined(__hppa__)
315 print_insn = print_insn_hppa;
316 #elif defined(__ia64__)
317 print_insn = print_insn_ia64;
318 #else
319 fprintf(out, "0x%lx: Asm output not supported on this arch\n",
320 (long) code);
321 return;
322 #endif
323 for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) {
324 fprintf(out, "0x%08" PRIxPTR ": ", pc);
325 count = print_insn(pc, &disasm_info);
326 fprintf(out, "\n");
327 if (count < 0)
328 break;
332 /* Look up symbol for debugging purpose. Returns "" if unknown. */
333 const char *lookup_symbol(target_ulong orig_addr)
335 const char *symbol = "";
336 struct syminfo *s;
338 for (s = syminfos; s; s = s->next) {
339 symbol = s->lookup_symbol(s, orig_addr);
340 if (symbol[0] != '\0') {
341 break;
345 return symbol;
348 #if !defined(CONFIG_USER_ONLY)
350 #include "monitor.h"
352 static int monitor_disas_is_physical;
353 static CPUArchState *monitor_disas_env;
355 static int
356 monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
357 struct disassemble_info *info)
359 if (monitor_disas_is_physical) {
360 cpu_physical_memory_read(memaddr, myaddr, length);
361 } else {
362 cpu_memory_rw_debug(monitor_disas_env, memaddr,myaddr, length, 0);
364 return 0;
367 static int GCC_FMT_ATTR(2, 3)
368 monitor_fprintf(FILE *stream, const char *fmt, ...)
370 va_list ap;
371 va_start(ap, fmt);
372 monitor_vprintf((Monitor *)stream, fmt, ap);
373 va_end(ap);
374 return 0;
377 void monitor_disas(Monitor *mon, CPUArchState *env,
378 target_ulong pc, int nb_insn, int is_physical, int flags)
380 int count, i;
381 struct disassemble_info disasm_info;
382 int (*print_insn)(bfd_vma pc, disassemble_info *info);
384 INIT_DISASSEMBLE_INFO(disasm_info, (FILE *)mon, monitor_fprintf);
386 monitor_disas_env = env;
387 monitor_disas_is_physical = is_physical;
388 disasm_info.read_memory_func = monitor_read_memory;
390 disasm_info.buffer_vma = pc;
392 #ifdef TARGET_WORDS_BIGENDIAN
393 disasm_info.endian = BFD_ENDIAN_BIG;
394 #else
395 disasm_info.endian = BFD_ENDIAN_LITTLE;
396 #endif
397 #if defined(TARGET_I386)
398 if (flags == 2)
399 disasm_info.mach = bfd_mach_x86_64;
400 else if (flags == 1)
401 disasm_info.mach = bfd_mach_i386_i8086;
402 else
403 disasm_info.mach = bfd_mach_i386_i386;
404 print_insn = print_insn_i386;
405 #elif defined(TARGET_ARM)
406 print_insn = print_insn_arm;
407 #elif defined(TARGET_ALPHA)
408 print_insn = print_insn_alpha;
409 #elif defined(TARGET_SPARC)
410 print_insn = print_insn_sparc;
411 #ifdef TARGET_SPARC64
412 disasm_info.mach = bfd_mach_sparc_v9b;
413 #endif
414 #elif defined(TARGET_PPC)
415 #ifdef TARGET_PPC64
416 disasm_info.mach = bfd_mach_ppc64;
417 #else
418 disasm_info.mach = bfd_mach_ppc;
419 #endif
420 print_insn = print_insn_ppc;
421 #elif defined(TARGET_M68K)
422 print_insn = print_insn_m68k;
423 #elif defined(TARGET_MIPS)
424 #ifdef TARGET_WORDS_BIGENDIAN
425 print_insn = print_insn_big_mips;
426 #else
427 print_insn = print_insn_little_mips;
428 #endif
429 #elif defined(TARGET_SH4)
430 disasm_info.mach = bfd_mach_sh4;
431 print_insn = print_insn_sh;
432 #elif defined(TARGET_S390X)
433 disasm_info.mach = bfd_mach_s390_64;
434 print_insn = print_insn_s390;
435 #elif defined(TARGET_LM32)
436 disasm_info.mach = bfd_mach_lm32;
437 print_insn = print_insn_lm32;
438 #else
439 monitor_printf(mon, "0x" TARGET_FMT_lx
440 ": Asm output not supported on this arch\n", pc);
441 return;
442 #endif
444 for(i = 0; i < nb_insn; i++) {
445 monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
446 count = print_insn(pc, &disasm_info);
447 monitor_printf(mon, "\n");
448 if (count < 0)
449 break;
450 pc += count;
453 #endif