1 /* General "disassemble this chunk" code. Used for debugging. */
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. */
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. */
23 memcpy (myaddr
, info
->buffer
+ (memaddr
- info
->buffer_vma
), length
);
27 /* Get LENGTH bytes from info's buffer, at target address memaddr.
28 Transfer them to myaddr. */
30 target_read_memory (bfd_vma memaddr
,
33 struct disassemble_info
*info
)
35 cpu_memory_rw_debug(cpu_single_env
, memaddr
, myaddr
, length
, 0);
39 /* Print an error message. We can assume that this is in response to
40 an error return from buffer_read_memory. */
42 perror_memory (int status
, bfd_vma memaddr
, struct disassemble_info
*info
)
46 (*info
->fprintf_func
) (info
->stream
, "Unknown error %d\n", status
);
48 /* Actually, address between memaddr and memaddr + len was
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 minuscule 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
62 generic_print_address (bfd_vma addr
, struct disassemble_info
*info
)
64 (*info
->fprintf_func
) (info
->stream
, "0x%" PRIx64
, addr
);
67 /* Print address in hex, truncated to the width of a target virtual address. */
69 generic_print_target_address(bfd_vma addr
, struct disassemble_info
*info
)
71 uint64_t mask
= ~0ULL >> (64 - TARGET_VIRT_ADDR_SPACE_BITS
);
72 generic_print_address(addr
& mask
, info
);
75 /* Print address in hex, truncated to the width of a host virtual address. */
77 generic_print_host_address(bfd_vma addr
, struct disassemble_info
*info
)
79 uint64_t mask
= ~0ULL >> (64 - (sizeof(void *) * 8));
80 generic_print_address(addr
& mask
, info
);
83 /* Just return the given address. */
86 generic_symbol_at_address (bfd_vma addr
, struct disassemble_info
*info
)
91 bfd_vma
bfd_getl64 (const bfd_byte
*addr
)
95 v
= (unsigned long long) addr
[0];
96 v
|= (unsigned long long) addr
[1] << 8;
97 v
|= (unsigned long long) addr
[2] << 16;
98 v
|= (unsigned long long) addr
[3] << 24;
99 v
|= (unsigned long long) addr
[4] << 32;
100 v
|= (unsigned long long) addr
[5] << 40;
101 v
|= (unsigned long long) addr
[6] << 48;
102 v
|= (unsigned long long) addr
[7] << 56;
106 bfd_vma
bfd_getl32 (const bfd_byte
*addr
)
110 v
= (unsigned long) addr
[0];
111 v
|= (unsigned long) addr
[1] << 8;
112 v
|= (unsigned long) addr
[2] << 16;
113 v
|= (unsigned long) addr
[3] << 24;
117 bfd_vma
bfd_getb32 (const bfd_byte
*addr
)
121 v
= (unsigned long) addr
[0] << 24;
122 v
|= (unsigned long) addr
[1] << 16;
123 v
|= (unsigned long) addr
[2] << 8;
124 v
|= (unsigned long) addr
[3];
128 bfd_vma
bfd_getl16 (const bfd_byte
*addr
)
132 v
= (unsigned long) addr
[0];
133 v
|= (unsigned long) addr
[1] << 8;
137 bfd_vma
bfd_getb16 (const bfd_byte
*addr
)
141 v
= (unsigned long) addr
[0] << 24;
142 v
|= (unsigned long) addr
[1] << 16;
148 print_insn_thumb1(bfd_vma pc
, disassemble_info
*info
)
150 return print_insn_arm(pc
| 1, info
);
154 /* Disassemble this for me please... (debugging). 'flags' has the following
156 i386 - 1 means 16 bit code, 2 means 64 bit code
157 arm - bit 0 = thumb, bit 1 = reverse endian
158 ppc - nonzero means little endian
159 other targets - unused
161 void target_disas(FILE *out
, target_ulong code
, target_ulong size
, int flags
)
165 struct disassemble_info disasm_info
;
166 int (*print_insn
)(bfd_vma pc
, disassemble_info
*info
);
168 INIT_DISASSEMBLE_INFO(disasm_info
, out
, fprintf
);
170 disasm_info
.read_memory_func
= target_read_memory
;
171 disasm_info
.buffer_vma
= code
;
172 disasm_info
.buffer_length
= size
;
173 disasm_info
.print_address_func
= generic_print_target_address
;
175 #ifdef TARGET_WORDS_BIGENDIAN
176 disasm_info
.endian
= BFD_ENDIAN_BIG
;
178 disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
180 #if defined(TARGET_I386)
182 disasm_info
.mach
= bfd_mach_x86_64
;
184 disasm_info
.mach
= bfd_mach_i386_i8086
;
186 disasm_info
.mach
= bfd_mach_i386_i386
;
187 print_insn
= print_insn_i386
;
188 #elif defined(TARGET_ARM)
190 print_insn
= print_insn_thumb1
;
192 print_insn
= print_insn_arm
;
195 #ifdef TARGET_WORDS_BIGENDIAN
196 disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
198 disasm_info
.endian
= BFD_ENDIAN_BIG
;
201 #elif defined(TARGET_SPARC)
202 print_insn
= print_insn_sparc
;
203 #ifdef TARGET_SPARC64
204 disasm_info
.mach
= bfd_mach_sparc_v9b
;
206 #elif defined(TARGET_PPC)
208 disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
209 if (flags
& 0xFFFF) {
210 /* If we have a precise definitions of the instructions set, use it */
211 disasm_info
.mach
= flags
& 0xFFFF;
214 disasm_info
.mach
= bfd_mach_ppc64
;
216 disasm_info
.mach
= bfd_mach_ppc
;
219 print_insn
= print_insn_ppc
;
220 #elif defined(TARGET_M68K)
221 print_insn
= print_insn_m68k
;
222 #elif defined(TARGET_MIPS)
223 #ifdef TARGET_WORDS_BIGENDIAN
224 print_insn
= print_insn_big_mips
;
226 print_insn
= print_insn_little_mips
;
228 #elif defined(TARGET_SH4)
229 disasm_info
.mach
= bfd_mach_sh4
;
230 print_insn
= print_insn_sh
;
231 #elif defined(TARGET_ALPHA)
232 disasm_info
.mach
= bfd_mach_alpha_ev6
;
233 print_insn
= print_insn_alpha
;
234 #elif defined(TARGET_CRIS)
236 disasm_info
.mach
= bfd_mach_cris_v0_v10
;
237 print_insn
= print_insn_crisv10
;
239 disasm_info
.mach
= bfd_mach_cris_v32
;
240 print_insn
= print_insn_crisv32
;
242 #elif defined(TARGET_S390X)
243 disasm_info
.mach
= bfd_mach_s390_64
;
244 print_insn
= print_insn_s390
;
245 #elif defined(TARGET_MICROBLAZE)
246 disasm_info
.mach
= bfd_arch_microblaze
;
247 print_insn
= print_insn_microblaze
;
248 #elif defined(TARGET_LM32)
249 disasm_info
.mach
= bfd_mach_lm32
;
250 print_insn
= print_insn_lm32
;
252 fprintf(out
, "0x" TARGET_FMT_lx
253 ": Asm output not supported on this arch\n", code
);
257 for (pc
= code
; size
> 0; pc
+= count
, size
-= count
) {
258 fprintf(out
, "0x" TARGET_FMT_lx
": ", pc
);
259 count
= print_insn(pc
, &disasm_info
);
265 for(i
= 0; i
< count
; i
++) {
266 target_read_memory(pc
+ i
, &b
, 1, &disasm_info
);
267 fprintf(out
, " %02x", b
);
277 "Disassembler disagrees with translator over instruction "
279 "Please report this to qemu-devel@nongnu.org\n");
285 /* Disassemble this for me please... (debugging). */
286 void disas(FILE *out
, void *code
, unsigned long size
)
290 struct disassemble_info disasm_info
;
291 int (*print_insn
)(bfd_vma pc
, disassemble_info
*info
);
293 INIT_DISASSEMBLE_INFO(disasm_info
, out
, fprintf
);
294 disasm_info
.print_address_func
= generic_print_host_address
;
296 disasm_info
.buffer
= code
;
297 disasm_info
.buffer_vma
= (uintptr_t)code
;
298 disasm_info
.buffer_length
= size
;
300 #ifdef HOST_WORDS_BIGENDIAN
301 disasm_info
.endian
= BFD_ENDIAN_BIG
;
303 disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
305 #if defined(CONFIG_TCG_INTERPRETER)
306 print_insn
= print_insn_tci
;
307 #elif defined(__i386__)
308 disasm_info
.mach
= bfd_mach_i386_i386
;
309 print_insn
= print_insn_i386
;
310 #elif defined(__x86_64__)
311 disasm_info
.mach
= bfd_mach_x86_64
;
312 print_insn
= print_insn_i386
;
313 #elif defined(_ARCH_PPC)
314 print_insn
= print_insn_ppc
;
315 #elif defined(__alpha__)
316 print_insn
= print_insn_alpha
;
317 #elif defined(__sparc__)
318 print_insn
= print_insn_sparc
;
319 #if defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
320 disasm_info
.mach
= bfd_mach_sparc_v9b
;
322 #elif defined(__arm__)
323 print_insn
= print_insn_arm
;
324 #elif defined(__MIPSEB__)
325 print_insn
= print_insn_big_mips
;
326 #elif defined(__MIPSEL__)
327 print_insn
= print_insn_little_mips
;
328 #elif defined(__m68k__)
329 print_insn
= print_insn_m68k
;
330 #elif defined(__s390__)
331 print_insn
= print_insn_s390
;
332 #elif defined(__hppa__)
333 print_insn
= print_insn_hppa
;
334 #elif defined(__ia64__)
335 print_insn
= print_insn_ia64
;
337 fprintf(out
, "0x%lx: Asm output not supported on this arch\n",
341 for (pc
= (uintptr_t)code
; size
> 0; pc
+= count
, size
-= count
) {
342 fprintf(out
, "0x%08" PRIxPTR
": ", pc
);
343 count
= print_insn(pc
, &disasm_info
);
350 /* Look up symbol for debugging purpose. Returns "" if unknown. */
351 const char *lookup_symbol(target_ulong orig_addr
)
353 const char *symbol
= "";
356 for (s
= syminfos
; s
; s
= s
->next
) {
357 symbol
= s
->lookup_symbol(s
, orig_addr
);
358 if (symbol
[0] != '\0') {
366 #if !defined(CONFIG_USER_ONLY)
370 static int monitor_disas_is_physical
;
371 static CPUArchState
*monitor_disas_env
;
374 monitor_read_memory (bfd_vma memaddr
, bfd_byte
*myaddr
, int length
,
375 struct disassemble_info
*info
)
377 if (monitor_disas_is_physical
) {
378 cpu_physical_memory_read(memaddr
, myaddr
, length
);
380 cpu_memory_rw_debug(monitor_disas_env
, memaddr
,myaddr
, length
, 0);
385 static int GCC_FMT_ATTR(2, 3)
386 monitor_fprintf(FILE *stream
, const char *fmt
, ...)
390 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
395 void monitor_disas(Monitor
*mon
, CPUArchState
*env
,
396 target_ulong pc
, int nb_insn
, int is_physical
, int flags
)
399 struct disassemble_info disasm_info
;
400 int (*print_insn
)(bfd_vma pc
, disassemble_info
*info
);
402 INIT_DISASSEMBLE_INFO(disasm_info
, (FILE *)mon
, monitor_fprintf
);
404 monitor_disas_env
= env
;
405 monitor_disas_is_physical
= is_physical
;
406 disasm_info
.read_memory_func
= monitor_read_memory
;
407 disasm_info
.print_address_func
= generic_print_target_address
;
409 disasm_info
.buffer_vma
= pc
;
411 #ifdef TARGET_WORDS_BIGENDIAN
412 disasm_info
.endian
= BFD_ENDIAN_BIG
;
414 disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
416 #if defined(TARGET_I386)
418 disasm_info
.mach
= bfd_mach_x86_64
;
420 disasm_info
.mach
= bfd_mach_i386_i8086
;
422 disasm_info
.mach
= bfd_mach_i386_i386
;
423 print_insn
= print_insn_i386
;
424 #elif defined(TARGET_ARM)
425 print_insn
= print_insn_arm
;
426 #elif defined(TARGET_ALPHA)
427 print_insn
= print_insn_alpha
;
428 #elif defined(TARGET_SPARC)
429 print_insn
= print_insn_sparc
;
430 #ifdef TARGET_SPARC64
431 disasm_info
.mach
= bfd_mach_sparc_v9b
;
433 #elif defined(TARGET_PPC)
435 disasm_info
.mach
= bfd_mach_ppc64
;
437 disasm_info
.mach
= bfd_mach_ppc
;
439 print_insn
= print_insn_ppc
;
440 #elif defined(TARGET_M68K)
441 print_insn
= print_insn_m68k
;
442 #elif defined(TARGET_MIPS)
443 #ifdef TARGET_WORDS_BIGENDIAN
444 print_insn
= print_insn_big_mips
;
446 print_insn
= print_insn_little_mips
;
448 #elif defined(TARGET_SH4)
449 disasm_info
.mach
= bfd_mach_sh4
;
450 print_insn
= print_insn_sh
;
451 #elif defined(TARGET_S390X)
452 disasm_info
.mach
= bfd_mach_s390_64
;
453 print_insn
= print_insn_s390
;
454 #elif defined(TARGET_LM32)
455 disasm_info
.mach
= bfd_mach_lm32
;
456 print_insn
= print_insn_lm32
;
458 monitor_printf(mon
, "0x" TARGET_FMT_lx
459 ": Asm output not supported on this arch\n", pc
);
463 for(i
= 0; i
< nb_insn
; i
++) {
464 monitor_printf(mon
, "0x" TARGET_FMT_lx
": ", pc
);
465 count
= print_insn(pc
, &disasm_info
);
466 monitor_printf(mon
, "\n");