Fix twisted kvm_arch_get/put_registers
[qemu-kvm/fedora.git] / disas.c
blob6bf7592702a17de0a82d4d0cf179901c651ef8a6
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 "exec-all.h"
9 #include "disas.h"
11 /* Filled in by elfload.c. Simplistic, but will do for now. */
12 struct syminfo *syminfos = NULL;
14 /* Get LENGTH bytes from info's buffer, at target address memaddr.
15 Transfer them to myaddr. */
16 int
17 buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
18 struct disassemble_info *info)
20 if (memaddr < info->buffer_vma
21 || memaddr + length > info->buffer_vma + info->buffer_length)
22 /* Out of bounds. Use EIO because GDB uses it. */
23 return EIO;
24 memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length);
25 return 0;
28 /* Get LENGTH bytes from info's buffer, at target address memaddr.
29 Transfer them to myaddr. */
30 static int
31 target_read_memory (bfd_vma memaddr,
32 bfd_byte *myaddr,
33 int length,
34 struct disassemble_info *info)
36 int i;
37 for(i = 0; i < length; i++) {
38 myaddr[i] = ldub_code(memaddr + i);
40 return 0;
43 /* Print an error message. We can assume that this is in response to
44 an error return from buffer_read_memory. */
45 void
46 perror_memory (int status, bfd_vma memaddr, struct disassemble_info *info)
48 if (status != EIO)
49 /* Can't happen. */
50 (*info->fprintf_func) (info->stream, "Unknown error %d\n", status);
51 else
52 /* Actually, address between memaddr and memaddr + len was
53 out of bounds. */
54 (*info->fprintf_func) (info->stream,
55 "Address 0x%" PRIx64 " is out of bounds.\n", memaddr);
58 /* This could be in a separate file, to save miniscule amounts of space
59 in statically linked executables. */
61 /* Just print the address is hex. This is included for completeness even
62 though both GDB and objdump provide their own (to print symbolic
63 addresses). */
65 void
66 generic_print_address (bfd_vma addr, struct disassemble_info *info)
68 (*info->fprintf_func) (info->stream, "0x%" PRIx64, addr);
71 /* Just return the given address. */
73 int
74 generic_symbol_at_address (bfd_vma addr, struct disassemble_info *info)
76 return 1;
79 bfd_vma bfd_getl32 (const bfd_byte *addr)
81 unsigned long v;
83 v = (unsigned long) addr[0];
84 v |= (unsigned long) addr[1] << 8;
85 v |= (unsigned long) addr[2] << 16;
86 v |= (unsigned long) addr[3] << 24;
87 return (bfd_vma) v;
90 bfd_vma bfd_getb32 (const bfd_byte *addr)
92 unsigned long v;
94 v = (unsigned long) addr[0] << 24;
95 v |= (unsigned long) addr[1] << 16;
96 v |= (unsigned long) addr[2] << 8;
97 v |= (unsigned long) addr[3];
98 return (bfd_vma) v;
101 bfd_vma bfd_getl16 (const bfd_byte *addr)
103 unsigned long v;
105 v = (unsigned long) addr[0];
106 v |= (unsigned long) addr[1] << 8;
107 return (bfd_vma) v;
110 bfd_vma bfd_getb16 (const bfd_byte *addr)
112 unsigned long v;
114 v = (unsigned long) addr[0] << 24;
115 v |= (unsigned long) addr[1] << 16;
116 return (bfd_vma) v;
119 #ifdef TARGET_ARM
120 static int
121 print_insn_thumb1(bfd_vma pc, disassemble_info *info)
123 return print_insn_arm(pc | 1, info);
125 #endif
127 /* Disassemble this for me please... (debugging). 'flags' has the following
128 values:
129 i386 - nonzero means 16 bit code
130 arm - nonzero means thumb code
131 ppc - nonzero means little endian
132 other targets - unused
134 void target_disas(FILE *out, target_ulong code, target_ulong size, int flags)
136 target_ulong pc;
137 int count;
138 struct disassemble_info disasm_info;
139 int (*print_insn)(bfd_vma pc, disassemble_info *info);
141 INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
143 disasm_info.read_memory_func = target_read_memory;
144 disasm_info.buffer_vma = code;
145 disasm_info.buffer_length = size;
147 #ifdef TARGET_WORDS_BIGENDIAN
148 disasm_info.endian = BFD_ENDIAN_BIG;
149 #else
150 disasm_info.endian = BFD_ENDIAN_LITTLE;
151 #endif
152 #if defined(TARGET_I386)
153 if (flags == 2)
154 disasm_info.mach = bfd_mach_x86_64;
155 else if (flags == 1)
156 disasm_info.mach = bfd_mach_i386_i8086;
157 else
158 disasm_info.mach = bfd_mach_i386_i386;
159 print_insn = print_insn_i386;
160 #elif defined(TARGET_ARM)
161 if (flags)
162 print_insn = print_insn_thumb1;
163 else
164 print_insn = print_insn_arm;
165 #elif defined(TARGET_SPARC)
166 print_insn = print_insn_sparc;
167 #ifdef TARGET_SPARC64
168 disasm_info.mach = bfd_mach_sparc_v9b;
169 #endif
170 #elif defined(TARGET_PPC)
171 if (flags >> 16)
172 disasm_info.endian = BFD_ENDIAN_LITTLE;
173 if (flags & 0xFFFF) {
174 /* If we have a precise definitions of the instructions set, use it */
175 disasm_info.mach = flags & 0xFFFF;
176 } else {
177 #ifdef TARGET_PPC64
178 disasm_info.mach = bfd_mach_ppc64;
179 #else
180 disasm_info.mach = bfd_mach_ppc;
181 #endif
183 print_insn = print_insn_ppc;
184 #elif defined(TARGET_M68K)
185 print_insn = print_insn_m68k;
186 #elif defined(TARGET_MIPS)
187 #ifdef TARGET_WORDS_BIGENDIAN
188 print_insn = print_insn_big_mips;
189 #else
190 print_insn = print_insn_little_mips;
191 #endif
192 #elif defined(TARGET_SH4)
193 disasm_info.mach = bfd_mach_sh4;
194 print_insn = print_insn_sh;
195 #elif defined(TARGET_ALPHA)
196 disasm_info.mach = bfd_mach_alpha;
197 print_insn = print_insn_alpha;
198 #elif defined(TARGET_CRIS)
199 disasm_info.mach = bfd_mach_cris_v32;
200 print_insn = print_insn_crisv32;
201 #else
202 fprintf(out, "0x" TARGET_FMT_lx
203 ": Asm output not supported on this arch\n", code);
204 return;
205 #endif
207 for (pc = code; size > 0; pc += count, size -= count) {
208 fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
209 count = print_insn(pc, &disasm_info);
210 #if 0
212 int i;
213 uint8_t b;
214 fprintf(out, " {");
215 for(i = 0; i < count; i++) {
216 target_read_memory(pc + i, &b, 1, &disasm_info);
217 fprintf(out, " %02x", b);
219 fprintf(out, " }");
221 #endif
222 fprintf(out, "\n");
223 if (count < 0)
224 break;
228 /* Disassemble this for me please... (debugging). */
229 void disas(FILE *out, void *code, unsigned long size)
231 unsigned long pc;
232 int count;
233 struct disassemble_info disasm_info;
234 int (*print_insn)(bfd_vma pc, disassemble_info *info);
236 INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
238 disasm_info.buffer = code;
239 disasm_info.buffer_vma = (unsigned long)code;
240 disasm_info.buffer_length = size;
242 #ifdef WORDS_BIGENDIAN
243 disasm_info.endian = BFD_ENDIAN_BIG;
244 #else
245 disasm_info.endian = BFD_ENDIAN_LITTLE;
246 #endif
247 #if defined(__i386__)
248 disasm_info.mach = bfd_mach_i386_i386;
249 print_insn = print_insn_i386;
250 #elif defined(__x86_64__)
251 disasm_info.mach = bfd_mach_x86_64;
252 print_insn = print_insn_i386;
253 #elif defined(_ARCH_PPC)
254 print_insn = print_insn_ppc;
255 #elif defined(__alpha__)
256 print_insn = print_insn_alpha;
257 #elif defined(__sparc__)
258 print_insn = print_insn_sparc;
259 #if defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
260 disasm_info.mach = bfd_mach_sparc_v9b;
261 #endif
262 #elif defined(__arm__)
263 print_insn = print_insn_arm;
264 #elif defined(__MIPSEB__)
265 print_insn = print_insn_big_mips;
266 #elif defined(__MIPSEL__)
267 print_insn = print_insn_little_mips;
268 #elif defined(__m68k__)
269 print_insn = print_insn_m68k;
270 #elif defined(__s390__)
271 print_insn = print_insn_s390;
272 #elif defined(__hppa__)
273 print_insn = print_insn_hppa;
274 #else
275 fprintf(out, "0x%lx: Asm output not supported on this arch\n",
276 (long) code);
277 return;
278 #endif
279 for (pc = (unsigned long)code; size > 0; pc += count, size -= count) {
280 fprintf(out, "0x%08lx: ", pc);
281 #ifdef __arm__
282 /* since data is included in the code, it is better to
283 display code data too */
284 fprintf(out, "%08x ", (int)bfd_getl32((const bfd_byte *)pc));
285 #endif
286 count = print_insn(pc, &disasm_info);
287 fprintf(out, "\n");
288 if (count < 0)
289 break;
293 /* Look up symbol for debugging purpose. Returns "" if unknown. */
294 const char *lookup_symbol(target_ulong orig_addr)
296 const char *symbol = "";
297 struct syminfo *s;
299 for (s = syminfos; s; s = s->next) {
300 symbol = s->lookup_symbol(s, orig_addr);
301 if (symbol[0] != '\0') {
302 break;
306 return symbol;
309 #if !defined(CONFIG_USER_ONLY)
311 #include "monitor.h"
313 static int monitor_disas_is_physical;
314 static CPUState *monitor_disas_env;
316 static int
317 monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
318 struct disassemble_info *info)
320 if (monitor_disas_is_physical) {
321 cpu_physical_memory_rw(memaddr, myaddr, length, 0);
322 } else {
323 cpu_memory_rw_debug(monitor_disas_env, memaddr,myaddr, length, 0);
325 return 0;
328 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
330 va_list ap;
331 va_start(ap, fmt);
332 monitor_vprintf((Monitor *)stream, fmt, ap);
333 va_end(ap);
334 return 0;
337 void monitor_disas(Monitor *mon, CPUState *env,
338 target_ulong pc, int nb_insn, int is_physical, int flags)
340 int count, i;
341 struct disassemble_info disasm_info;
342 int (*print_insn)(bfd_vma pc, disassemble_info *info);
344 INIT_DISASSEMBLE_INFO(disasm_info, (FILE *)mon, monitor_fprintf);
346 monitor_disas_env = env;
347 monitor_disas_is_physical = is_physical;
348 disasm_info.read_memory_func = monitor_read_memory;
350 disasm_info.buffer_vma = pc;
352 #ifdef TARGET_WORDS_BIGENDIAN
353 disasm_info.endian = BFD_ENDIAN_BIG;
354 #else
355 disasm_info.endian = BFD_ENDIAN_LITTLE;
356 #endif
357 #if defined(TARGET_I386)
358 if (flags == 2)
359 disasm_info.mach = bfd_mach_x86_64;
360 else if (flags == 1)
361 disasm_info.mach = bfd_mach_i386_i8086;
362 else
363 disasm_info.mach = bfd_mach_i386_i386;
364 print_insn = print_insn_i386;
365 #elif defined(TARGET_ARM)
366 print_insn = print_insn_arm;
367 #elif defined(TARGET_ALPHA)
368 print_insn = print_insn_alpha;
369 #elif defined(TARGET_SPARC)
370 print_insn = print_insn_sparc;
371 #ifdef TARGET_SPARC64
372 disasm_info.mach = bfd_mach_sparc_v9b;
373 #endif
374 #elif defined(TARGET_PPC)
375 #ifdef TARGET_PPC64
376 disasm_info.mach = bfd_mach_ppc64;
377 #else
378 disasm_info.mach = bfd_mach_ppc;
379 #endif
380 print_insn = print_insn_ppc;
381 #elif defined(TARGET_M68K)
382 print_insn = print_insn_m68k;
383 #elif defined(TARGET_MIPS)
384 #ifdef TARGET_WORDS_BIGENDIAN
385 print_insn = print_insn_big_mips;
386 #else
387 print_insn = print_insn_little_mips;
388 #endif
389 #else
390 monitor_printf(mon, "0x" TARGET_FMT_lx
391 ": Asm output not supported on this arch\n", pc);
392 return;
393 #endif
395 for(i = 0; i < nb_insn; i++) {
396 monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
397 count = print_insn(pc, &disasm_info);
398 monitor_printf(mon, "\n");
399 if (count < 0)
400 break;
401 pc += count;
404 #endif