qdev: add children before qdev_init
[qemu/opensuse.git] / disas.c
blob94858241dee0e97f452ae61926f338399e640a95
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 - nonzero means thumb code
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)
173 print_insn = print_insn_thumb1;
174 else
175 print_insn = print_insn_arm;
176 #elif defined(TARGET_SPARC)
177 print_insn = print_insn_sparc;
178 #ifdef TARGET_SPARC64
179 disasm_info.mach = bfd_mach_sparc_v9b;
180 #endif
181 #elif defined(TARGET_PPC)
182 if (flags >> 16)
183 disasm_info.endian = BFD_ENDIAN_LITTLE;
184 if (flags & 0xFFFF) {
185 /* If we have a precise definitions of the instructions set, use it */
186 disasm_info.mach = flags & 0xFFFF;
187 } else {
188 #ifdef TARGET_PPC64
189 disasm_info.mach = bfd_mach_ppc64;
190 #else
191 disasm_info.mach = bfd_mach_ppc;
192 #endif
194 print_insn = print_insn_ppc;
195 #elif defined(TARGET_M68K)
196 print_insn = print_insn_m68k;
197 #elif defined(TARGET_MIPS)
198 #ifdef TARGET_WORDS_BIGENDIAN
199 print_insn = print_insn_big_mips;
200 #else
201 print_insn = print_insn_little_mips;
202 #endif
203 #elif defined(TARGET_SH4)
204 disasm_info.mach = bfd_mach_sh4;
205 print_insn = print_insn_sh;
206 #elif defined(TARGET_ALPHA)
207 disasm_info.mach = bfd_mach_alpha_ev6;
208 print_insn = print_insn_alpha;
209 #elif defined(TARGET_CRIS)
210 if (flags != 32) {
211 disasm_info.mach = bfd_mach_cris_v0_v10;
212 print_insn = print_insn_crisv10;
213 } else {
214 disasm_info.mach = bfd_mach_cris_v32;
215 print_insn = print_insn_crisv32;
217 #elif defined(TARGET_S390X)
218 disasm_info.mach = bfd_mach_s390_64;
219 print_insn = print_insn_s390;
220 #elif defined(TARGET_MICROBLAZE)
221 disasm_info.mach = bfd_arch_microblaze;
222 print_insn = print_insn_microblaze;
223 #elif defined(TARGET_LM32)
224 disasm_info.mach = bfd_mach_lm32;
225 print_insn = print_insn_lm32;
226 #else
227 fprintf(out, "0x" TARGET_FMT_lx
228 ": Asm output not supported on this arch\n", code);
229 return;
230 #endif
232 for (pc = code; size > 0; pc += count, size -= count) {
233 fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
234 count = print_insn(pc, &disasm_info);
235 #if 0
237 int i;
238 uint8_t b;
239 fprintf(out, " {");
240 for(i = 0; i < count; i++) {
241 target_read_memory(pc + i, &b, 1, &disasm_info);
242 fprintf(out, " %02x", b);
244 fprintf(out, " }");
246 #endif
247 fprintf(out, "\n");
248 if (count < 0)
249 break;
250 if (size < count) {
251 fprintf(out,
252 "Disassembler disagrees with translator over instruction "
253 "decoding\n"
254 "Please report this to qemu-devel@nongnu.org\n");
255 break;
260 /* Disassemble this for me please... (debugging). */
261 void disas(FILE *out, void *code, unsigned long size)
263 unsigned long pc;
264 int count;
265 struct disassemble_info disasm_info;
266 int (*print_insn)(bfd_vma pc, disassemble_info *info);
268 INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
270 disasm_info.buffer = code;
271 disasm_info.buffer_vma = (unsigned long)code;
272 disasm_info.buffer_length = size;
274 #ifdef HOST_WORDS_BIGENDIAN
275 disasm_info.endian = BFD_ENDIAN_BIG;
276 #else
277 disasm_info.endian = BFD_ENDIAN_LITTLE;
278 #endif
279 #if defined(CONFIG_TCG_INTERPRETER)
280 print_insn = print_insn_tci;
281 #elif defined(__i386__)
282 disasm_info.mach = bfd_mach_i386_i386;
283 print_insn = print_insn_i386;
284 #elif defined(__x86_64__)
285 disasm_info.mach = bfd_mach_x86_64;
286 print_insn = print_insn_i386;
287 #elif defined(_ARCH_PPC)
288 print_insn = print_insn_ppc;
289 #elif defined(__alpha__)
290 print_insn = print_insn_alpha;
291 #elif defined(__sparc__)
292 print_insn = print_insn_sparc;
293 #if defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
294 disasm_info.mach = bfd_mach_sparc_v9b;
295 #endif
296 #elif defined(__arm__)
297 print_insn = print_insn_arm;
298 #elif defined(__MIPSEB__)
299 print_insn = print_insn_big_mips;
300 #elif defined(__MIPSEL__)
301 print_insn = print_insn_little_mips;
302 #elif defined(__m68k__)
303 print_insn = print_insn_m68k;
304 #elif defined(__s390__)
305 print_insn = print_insn_s390;
306 #elif defined(__hppa__)
307 print_insn = print_insn_hppa;
308 #elif defined(__ia64__)
309 print_insn = print_insn_ia64;
310 #else
311 fprintf(out, "0x%lx: Asm output not supported on this arch\n",
312 (long) code);
313 return;
314 #endif
315 for (pc = (unsigned long)code; size > 0; pc += count, size -= count) {
316 fprintf(out, "0x%08lx: ", pc);
317 count = print_insn(pc, &disasm_info);
318 fprintf(out, "\n");
319 if (count < 0)
320 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.h"
344 static int monitor_disas_is_physical;
345 static CPUArchState *monitor_disas_env;
347 static int
348 monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
349 struct disassemble_info *info)
351 if (monitor_disas_is_physical) {
352 cpu_physical_memory_read(memaddr, myaddr, length);
353 } else {
354 cpu_memory_rw_debug(monitor_disas_env, memaddr,myaddr, length, 0);
356 return 0;
359 static int GCC_FMT_ATTR(2, 3)
360 monitor_fprintf(FILE *stream, const char *fmt, ...)
362 va_list ap;
363 va_start(ap, fmt);
364 monitor_vprintf((Monitor *)stream, fmt, ap);
365 va_end(ap);
366 return 0;
369 void monitor_disas(Monitor *mon, CPUArchState *env,
370 target_ulong pc, int nb_insn, int is_physical, int flags)
372 int count, i;
373 struct disassemble_info disasm_info;
374 int (*print_insn)(bfd_vma pc, disassemble_info *info);
376 INIT_DISASSEMBLE_INFO(disasm_info, (FILE *)mon, monitor_fprintf);
378 monitor_disas_env = env;
379 monitor_disas_is_physical = is_physical;
380 disasm_info.read_memory_func = monitor_read_memory;
382 disasm_info.buffer_vma = pc;
384 #ifdef TARGET_WORDS_BIGENDIAN
385 disasm_info.endian = BFD_ENDIAN_BIG;
386 #else
387 disasm_info.endian = BFD_ENDIAN_LITTLE;
388 #endif
389 #if defined(TARGET_I386)
390 if (flags == 2)
391 disasm_info.mach = bfd_mach_x86_64;
392 else if (flags == 1)
393 disasm_info.mach = bfd_mach_i386_i8086;
394 else
395 disasm_info.mach = bfd_mach_i386_i386;
396 print_insn = print_insn_i386;
397 #elif defined(TARGET_ARM)
398 print_insn = print_insn_arm;
399 #elif defined(TARGET_ALPHA)
400 print_insn = print_insn_alpha;
401 #elif defined(TARGET_SPARC)
402 print_insn = print_insn_sparc;
403 #ifdef TARGET_SPARC64
404 disasm_info.mach = bfd_mach_sparc_v9b;
405 #endif
406 #elif defined(TARGET_PPC)
407 #ifdef TARGET_PPC64
408 disasm_info.mach = bfd_mach_ppc64;
409 #else
410 disasm_info.mach = bfd_mach_ppc;
411 #endif
412 print_insn = print_insn_ppc;
413 #elif defined(TARGET_M68K)
414 print_insn = print_insn_m68k;
415 #elif defined(TARGET_MIPS)
416 #ifdef TARGET_WORDS_BIGENDIAN
417 print_insn = print_insn_big_mips;
418 #else
419 print_insn = print_insn_little_mips;
420 #endif
421 #elif defined(TARGET_SH4)
422 disasm_info.mach = bfd_mach_sh4;
423 print_insn = print_insn_sh;
424 #elif defined(TARGET_S390X)
425 disasm_info.mach = bfd_mach_s390_64;
426 print_insn = print_insn_s390;
427 #elif defined(TARGET_LM32)
428 disasm_info.mach = bfd_mach_lm32;
429 print_insn = print_insn_lm32;
430 #else
431 monitor_printf(mon, "0x" TARGET_FMT_lx
432 ": Asm output not supported on this arch\n", pc);
433 return;
434 #endif
436 for(i = 0; i < nb_insn; i++) {
437 monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
438 count = print_insn(pc, &disasm_info);
439 monitor_printf(mon, "\n");
440 if (count < 0)
441 break;
442 pc += count;
445 #endif