blob: f7bcce748ce57d2470c99bbaf026cd66485d2e39 [file] [log] [blame]
bellardb9adb4a2003-04-29 20:41:16 +00001/* General "disassemble this chunk" code. Used for debugging. */
bellard5bbe9292003-06-09 19:38:38 +00002#include "config.h"
bellardb9adb4a2003-04-29 20:41:16 +00003#include "dis-asm.h"
bellardb9adb4a2003-04-29 20:41:16 +00004#include "elf.h"
bellardaa0aa4f2003-06-09 15:23:31 +00005#include <errno.h>
bellardb9adb4a2003-04-29 20:41:16 +00006
bellardc6105c02003-10-27 21:13:58 +00007#include "cpu.h"
8#include "exec-all.h"
bellard9307c4c2004-04-04 12:57:25 +00009#include "disas.h"
bellardc6105c02003-10-27 21:13:58 +000010
bellardb9adb4a2003-04-29 20:41:16 +000011/* Filled in by elfload.c. Simplistic, but will do for now. */
bellarde80cfcf2004-12-19 23:18:01 +000012struct syminfo *syminfos = NULL;
bellardb9adb4a2003-04-29 20:41:16 +000013
bellardaa0aa4f2003-06-09 15:23:31 +000014/* Get LENGTH bytes from info's buffer, at target address memaddr.
15 Transfer them to myaddr. */
16int
pbrook3a742b72008-10-22 15:55:18 +000017buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
18 struct disassemble_info *info)
bellardaa0aa4f2003-06-09 15:23:31 +000019{
bellardc6105c02003-10-27 21:13:58 +000020 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;
bellardaa0aa4f2003-06-09 15:23:31 +000026}
27
bellardc6105c02003-10-27 21:13:58 +000028/* Get LENGTH bytes from info's buffer, at target address memaddr.
29 Transfer them to myaddr. */
30static int
bellardc27004e2005-01-03 23:35:10 +000031target_read_memory (bfd_vma memaddr,
32 bfd_byte *myaddr,
33 int length,
34 struct disassemble_info *info)
bellardc6105c02003-10-27 21:13:58 +000035{
Blue Swirle612a1f2009-05-07 17:14:07 +000036 cpu_memory_rw_debug(cpu_single_env, memaddr, myaddr, length, 0);
bellardc6105c02003-10-27 21:13:58 +000037 return 0;
38}
bellardc6105c02003-10-27 21:13:58 +000039
bellardaa0aa4f2003-06-09 15:23:31 +000040/* Print an error message. We can assume that this is in response to
41 an error return from buffer_read_memory. */
42void
pbrook3a742b72008-10-22 15:55:18 +000043perror_memory (int status, bfd_vma memaddr, struct disassemble_info *info)
bellardaa0aa4f2003-06-09 15:23:31 +000044{
45 if (status != EIO)
46 /* Can't happen. */
47 (*info->fprintf_func) (info->stream, "Unknown error %d\n", status);
48 else
49 /* Actually, address between memaddr and memaddr + len was
50 out of bounds. */
51 (*info->fprintf_func) (info->stream,
bellard26a76462006-06-25 18:15:32 +000052 "Address 0x%" PRIx64 " is out of bounds.\n", memaddr);
bellardaa0aa4f2003-06-09 15:23:31 +000053}
54
55/* This could be in a separate file, to save miniscule amounts of space
56 in statically linked executables. */
57
58/* Just print the address is hex. This is included for completeness even
59 though both GDB and objdump provide their own (to print symbolic
60 addresses). */
61
62void
pbrook3a742b72008-10-22 15:55:18 +000063generic_print_address (bfd_vma addr, struct disassemble_info *info)
bellardaa0aa4f2003-06-09 15:23:31 +000064{
bellard26a76462006-06-25 18:15:32 +000065 (*info->fprintf_func) (info->stream, "0x%" PRIx64, addr);
bellardaa0aa4f2003-06-09 15:23:31 +000066}
67
68/* Just return the given address. */
69
70int
pbrook3a742b72008-10-22 15:55:18 +000071generic_symbol_at_address (bfd_vma addr, struct disassemble_info *info)
bellardaa0aa4f2003-06-09 15:23:31 +000072{
73 return 1;
74}
75
76bfd_vma bfd_getl32 (const bfd_byte *addr)
77{
78 unsigned long v;
79
80 v = (unsigned long) addr[0];
81 v |= (unsigned long) addr[1] << 8;
82 v |= (unsigned long) addr[2] << 16;
83 v |= (unsigned long) addr[3] << 24;
84 return (bfd_vma) v;
85}
86
87bfd_vma bfd_getb32 (const bfd_byte *addr)
88{
89 unsigned long v;
90
91 v = (unsigned long) addr[0] << 24;
92 v |= (unsigned long) addr[1] << 16;
93 v |= (unsigned long) addr[2] << 8;
94 v |= (unsigned long) addr[3];
95 return (bfd_vma) v;
96}
97
bellard6af0bf92005-07-02 14:58:51 +000098bfd_vma bfd_getl16 (const bfd_byte *addr)
99{
100 unsigned long v;
101
102 v = (unsigned long) addr[0];
103 v |= (unsigned long) addr[1] << 8;
104 return (bfd_vma) v;
105}
106
107bfd_vma bfd_getb16 (const bfd_byte *addr)
108{
109 unsigned long v;
110
111 v = (unsigned long) addr[0] << 24;
112 v |= (unsigned long) addr[1] << 16;
113 return (bfd_vma) v;
114}
115
bellardc2d551f2005-04-27 20:15:00 +0000116#ifdef TARGET_ARM
117static int
118print_insn_thumb1(bfd_vma pc, disassemble_info *info)
119{
120 return print_insn_arm(pc | 1, info);
121}
122#endif
123
thse91c8a72007-06-03 13:35:16 +0000124/* Disassemble this for me please... (debugging). 'flags' has the following
bellardc2d551f2005-04-27 20:15:00 +0000125 values:
126 i386 - nonzero means 16 bit code
ths5fafdf22007-09-16 21:08:06 +0000127 arm - nonzero means thumb code
bellard6a00d602005-11-21 23:25:50 +0000128 ppc - nonzero means little endian
bellardc2d551f2005-04-27 20:15:00 +0000129 other targets - unused
130 */
bellard83b34f82005-01-23 20:26:30 +0000131void target_disas(FILE *out, target_ulong code, target_ulong size, int flags)
bellardb9adb4a2003-04-29 20:41:16 +0000132{
bellardc27004e2005-01-03 23:35:10 +0000133 target_ulong pc;
bellardb9adb4a2003-04-29 20:41:16 +0000134 int count;
135 struct disassemble_info disasm_info;
136 int (*print_insn)(bfd_vma pc, disassemble_info *info);
137
138 INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
139
bellardc27004e2005-01-03 23:35:10 +0000140 disasm_info.read_memory_func = target_read_memory;
141 disasm_info.buffer_vma = code;
142 disasm_info.buffer_length = size;
143
144#ifdef TARGET_WORDS_BIGENDIAN
145 disasm_info.endian = BFD_ENDIAN_BIG;
146#else
147 disasm_info.endian = BFD_ENDIAN_LITTLE;
bellardc6105c02003-10-27 21:13:58 +0000148#endif
bellardc27004e2005-01-03 23:35:10 +0000149#if defined(TARGET_I386)
150 if (flags == 2)
151 disasm_info.mach = bfd_mach_x86_64;
ths5fafdf22007-09-16 21:08:06 +0000152 else if (flags == 1)
bellardc27004e2005-01-03 23:35:10 +0000153 disasm_info.mach = bfd_mach_i386_i8086;
154 else
155 disasm_info.mach = bfd_mach_i386_i386;
156 print_insn = print_insn_i386;
157#elif defined(TARGET_ARM)
bellardc2d551f2005-04-27 20:15:00 +0000158 if (flags)
159 print_insn = print_insn_thumb1;
160 else
161 print_insn = print_insn_arm;
bellardc27004e2005-01-03 23:35:10 +0000162#elif defined(TARGET_SPARC)
163 print_insn = print_insn_sparc;
bellard34751872005-07-02 14:31:34 +0000164#ifdef TARGET_SPARC64
165 disasm_info.mach = bfd_mach_sparc_v9b;
ths3b46e622007-09-17 08:09:54 +0000166#endif
bellardc27004e2005-01-03 23:35:10 +0000167#elif defined(TARGET_PPC)
j_mayer237c0af2007-09-29 12:01:46 +0000168 if (flags >> 16)
bellard111bfab2005-04-23 18:16:07 +0000169 disasm_info.endian = BFD_ENDIAN_LITTLE;
j_mayer237c0af2007-09-29 12:01:46 +0000170 if (flags & 0xFFFF) {
171 /* If we have a precise definitions of the instructions set, use it */
172 disasm_info.mach = flags & 0xFFFF;
173 } else {
bellarda2458622005-07-23 22:39:53 +0000174#ifdef TARGET_PPC64
j_mayer237c0af2007-09-29 12:01:46 +0000175 disasm_info.mach = bfd_mach_ppc64;
bellarda2458622005-07-23 22:39:53 +0000176#else
j_mayer237c0af2007-09-29 12:01:46 +0000177 disasm_info.mach = bfd_mach_ppc;
bellarda2458622005-07-23 22:39:53 +0000178#endif
j_mayer237c0af2007-09-29 12:01:46 +0000179 }
bellardc27004e2005-01-03 23:35:10 +0000180 print_insn = print_insn_ppc;
pbrooke6e59062006-10-22 00:18:54 +0000181#elif defined(TARGET_M68K)
182 print_insn = print_insn_m68k;
bellard6af0bf92005-07-02 14:58:51 +0000183#elif defined(TARGET_MIPS)
bellard76b30302005-12-17 01:10:04 +0000184#ifdef TARGET_WORDS_BIGENDIAN
bellard6af0bf92005-07-02 14:58:51 +0000185 print_insn = print_insn_big_mips;
bellard76b30302005-12-17 01:10:04 +0000186#else
187 print_insn = print_insn_little_mips;
188#endif
bellardfdf9b3e2006-04-27 21:07:38 +0000189#elif defined(TARGET_SH4)
190 disasm_info.mach = bfd_mach_sh4;
191 print_insn = print_insn_sh;
j_mayereddf68a2007-04-05 07:22:49 +0000192#elif defined(TARGET_ALPHA)
193 disasm_info.mach = bfd_mach_alpha;
194 print_insn = print_insn_alpha;
thsa25fd132007-10-08 12:46:58 +0000195#elif defined(TARGET_CRIS)
196 disasm_info.mach = bfd_mach_cris_v32;
197 print_insn = print_insn_crisv32;
Edgar E. Iglesiase90e3902009-05-20 20:07:38 +0200198#elif defined(TARGET_MICROBLAZE)
199 disasm_info.mach = bfd_arch_microblaze;
200 print_insn = print_insn_microblaze;
bellardc27004e2005-01-03 23:35:10 +0000201#else
bellardb8076a72005-04-07 22:20:31 +0000202 fprintf(out, "0x" TARGET_FMT_lx
203 ": Asm output not supported on this arch\n", code);
bellardc27004e2005-01-03 23:35:10 +0000204 return;
205#endif
206
blueswir17e000c22009-02-13 21:44:41 +0000207 for (pc = code; size > 0; pc += count, size -= count) {
bellardfa15e032005-01-31 23:32:31 +0000208 fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
bellardc27004e2005-01-03 23:35:10 +0000209 count = print_insn(pc, &disasm_info);
210#if 0
211 {
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);
218 }
219 fprintf(out, " }");
220 }
221#endif
222 fprintf(out, "\n");
223 if (count < 0)
224 break;
malc754d00a2009-04-21 22:26:22 +0000225 if (size < count) {
226 fprintf(out,
227 "Disassembler disagrees with translator over instruction "
228 "decoding\n"
229 "Please report this to qemu-devel@nongnu.org\n");
230 break;
231 }
bellardc27004e2005-01-03 23:35:10 +0000232 }
233}
234
235/* Disassemble this for me please... (debugging). */
236void disas(FILE *out, void *code, unsigned long size)
237{
238 unsigned long pc;
239 int count;
240 struct disassemble_info disasm_info;
241 int (*print_insn)(bfd_vma pc, disassemble_info *info);
242
243 INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf);
bellardc6105c02003-10-27 21:13:58 +0000244
bellardb9adb4a2003-04-29 20:41:16 +0000245 disasm_info.buffer = code;
246 disasm_info.buffer_vma = (unsigned long)code;
247 disasm_info.buffer_length = size;
248
Juan Quintelae2542fe2009-07-27 16:13:06 +0200249#ifdef HOST_WORDS_BIGENDIAN
bellardc27004e2005-01-03 23:35:10 +0000250 disasm_info.endian = BFD_ENDIAN_BIG;
bellardb9adb4a2003-04-29 20:41:16 +0000251#else
bellardc27004e2005-01-03 23:35:10 +0000252 disasm_info.endian = BFD_ENDIAN_LITTLE;
bellardb9adb4a2003-04-29 20:41:16 +0000253#endif
bellardbc51c5c2004-03-17 23:46:04 +0000254#if defined(__i386__)
bellardc27004e2005-01-03 23:35:10 +0000255 disasm_info.mach = bfd_mach_i386_i386;
256 print_insn = print_insn_i386;
bellardbc51c5c2004-03-17 23:46:04 +0000257#elif defined(__x86_64__)
bellardc27004e2005-01-03 23:35:10 +0000258 disasm_info.mach = bfd_mach_x86_64;
259 print_insn = print_insn_i386;
malce58ffeb2009-01-14 18:39:49 +0000260#elif defined(_ARCH_PPC)
bellardc27004e2005-01-03 23:35:10 +0000261 print_insn = print_insn_ppc;
bellarda993ba82003-05-11 12:25:45 +0000262#elif defined(__alpha__)
bellardc27004e2005-01-03 23:35:10 +0000263 print_insn = print_insn_alpha;
bellardaa0aa4f2003-06-09 15:23:31 +0000264#elif defined(__sparc__)
bellardc27004e2005-01-03 23:35:10 +0000265 print_insn = print_insn_sparc;
blueswir16ecd4532007-04-08 11:22:29 +0000266#if defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
267 disasm_info.mach = bfd_mach_sparc_v9b;
268#endif
ths5fafdf22007-09-16 21:08:06 +0000269#elif defined(__arm__)
bellardc27004e2005-01-03 23:35:10 +0000270 print_insn = print_insn_arm;
bellard6af0bf92005-07-02 14:58:51 +0000271#elif defined(__MIPSEB__)
272 print_insn = print_insn_big_mips;
273#elif defined(__MIPSEL__)
274 print_insn = print_insn_little_mips;
bellard48024e42005-11-06 16:52:11 +0000275#elif defined(__m68k__)
276 print_insn = print_insn_m68k;
ths8f860bb2007-07-31 23:44:21 +0000277#elif defined(__s390__)
278 print_insn = print_insn_s390;
aurel32f54b3f92008-04-12 20:14:54 +0000279#elif defined(__hppa__)
280 print_insn = print_insn_hppa;
bellardb9adb4a2003-04-29 20:41:16 +0000281#else
bellardb8076a72005-04-07 22:20:31 +0000282 fprintf(out, "0x%lx: Asm output not supported on this arch\n",
283 (long) code);
bellardc27004e2005-01-03 23:35:10 +0000284 return;
bellardb9adb4a2003-04-29 20:41:16 +0000285#endif
blueswir17e000c22009-02-13 21:44:41 +0000286 for (pc = (unsigned long)code; size > 0; pc += count, size -= count) {
bellardc27004e2005-01-03 23:35:10 +0000287 fprintf(out, "0x%08lx: ", pc);
bellardaa0aa4f2003-06-09 15:23:31 +0000288#ifdef __arm__
pbrook46152182006-07-30 19:16:29 +0000289 /* since data is included in the code, it is better to
bellardaa0aa4f2003-06-09 15:23:31 +0000290 display code data too */
pbrook46152182006-07-30 19:16:29 +0000291 fprintf(out, "%08x ", (int)bfd_getl32((const bfd_byte *)pc));
bellardaa0aa4f2003-06-09 15:23:31 +0000292#endif
bellardc27004e2005-01-03 23:35:10 +0000293 count = print_insn(pc, &disasm_info);
bellardb9adb4a2003-04-29 20:41:16 +0000294 fprintf(out, "\n");
295 if (count < 0)
296 break;
297 }
298}
299
300/* Look up symbol for debugging purpose. Returns "" if unknown. */
bellardc27004e2005-01-03 23:35:10 +0000301const char *lookup_symbol(target_ulong orig_addr)
bellardb9adb4a2003-04-29 20:41:16 +0000302{
pbrook49918a72008-10-22 15:11:31 +0000303 const char *symbol = "";
bellarde80cfcf2004-12-19 23:18:01 +0000304 struct syminfo *s;
ths3b46e622007-09-17 08:09:54 +0000305
bellarde80cfcf2004-12-19 23:18:01 +0000306 for (s = syminfos; s; s = s->next) {
pbrook49918a72008-10-22 15:11:31 +0000307 symbol = s->lookup_symbol(s, orig_addr);
308 if (symbol[0] != '\0') {
309 break;
310 }
bellardb9adb4a2003-04-29 20:41:16 +0000311 }
pbrook49918a72008-10-22 15:11:31 +0000312
313 return symbol;
bellardb9adb4a2003-04-29 20:41:16 +0000314}
bellard9307c4c2004-04-04 12:57:25 +0000315
316#if !defined(CONFIG_USER_ONLY)
317
aliguori376253e2009-03-05 23:01:23 +0000318#include "monitor.h"
bellard3d2cfdf2004-08-01 21:49:07 +0000319
bellard9307c4c2004-04-04 12:57:25 +0000320static int monitor_disas_is_physical;
bellard6a00d602005-11-21 23:25:50 +0000321static CPUState *monitor_disas_env;
bellard9307c4c2004-04-04 12:57:25 +0000322
323static int
blueswir1a5f1b962008-08-17 20:21:51 +0000324monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
325 struct disassemble_info *info)
bellard9307c4c2004-04-04 12:57:25 +0000326{
327 if (monitor_disas_is_physical) {
328 cpu_physical_memory_rw(memaddr, myaddr, length, 0);
329 } else {
bellard6a00d602005-11-21 23:25:50 +0000330 cpu_memory_rw_debug(monitor_disas_env, memaddr,myaddr, length, 0);
bellard9307c4c2004-04-04 12:57:25 +0000331 }
332 return 0;
333}
334
bellard3d2cfdf2004-08-01 21:49:07 +0000335static int monitor_fprintf(FILE *stream, const char *fmt, ...)
336{
337 va_list ap;
338 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000339 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard3d2cfdf2004-08-01 21:49:07 +0000340 va_end(ap);
341 return 0;
342}
343
aliguori376253e2009-03-05 23:01:23 +0000344void monitor_disas(Monitor *mon, CPUState *env,
bellard6a00d602005-11-21 23:25:50 +0000345 target_ulong pc, int nb_insn, int is_physical, int flags)
bellard9307c4c2004-04-04 12:57:25 +0000346{
bellard9307c4c2004-04-04 12:57:25 +0000347 int count, i;
348 struct disassemble_info disasm_info;
349 int (*print_insn)(bfd_vma pc, disassemble_info *info);
350
aliguori376253e2009-03-05 23:01:23 +0000351 INIT_DISASSEMBLE_INFO(disasm_info, (FILE *)mon, monitor_fprintf);
bellard9307c4c2004-04-04 12:57:25 +0000352
bellard6a00d602005-11-21 23:25:50 +0000353 monitor_disas_env = env;
bellard9307c4c2004-04-04 12:57:25 +0000354 monitor_disas_is_physical = is_physical;
355 disasm_info.read_memory_func = monitor_read_memory;
356
357 disasm_info.buffer_vma = pc;
358
359#ifdef TARGET_WORDS_BIGENDIAN
360 disasm_info.endian = BFD_ENDIAN_BIG;
361#else
362 disasm_info.endian = BFD_ENDIAN_LITTLE;
363#endif
364#if defined(TARGET_I386)
bellardfa15e032005-01-31 23:32:31 +0000365 if (flags == 2)
366 disasm_info.mach = bfd_mach_x86_64;
ths5fafdf22007-09-16 21:08:06 +0000367 else if (flags == 1)
bellard9307c4c2004-04-04 12:57:25 +0000368 disasm_info.mach = bfd_mach_i386_i8086;
bellardfa15e032005-01-31 23:32:31 +0000369 else
370 disasm_info.mach = bfd_mach_i386_i386;
bellard9307c4c2004-04-04 12:57:25 +0000371 print_insn = print_insn_i386;
372#elif defined(TARGET_ARM)
373 print_insn = print_insn_arm;
thscbd669d2007-12-25 00:26:36 +0000374#elif defined(TARGET_ALPHA)
375 print_insn = print_insn_alpha;
bellard9307c4c2004-04-04 12:57:25 +0000376#elif defined(TARGET_SPARC)
377 print_insn = print_insn_sparc;
blueswir1682c4f12007-04-09 15:14:57 +0000378#ifdef TARGET_SPARC64
379 disasm_info.mach = bfd_mach_sparc_v9b;
380#endif
bellard9307c4c2004-04-04 12:57:25 +0000381#elif defined(TARGET_PPC)
bellarda2458622005-07-23 22:39:53 +0000382#ifdef TARGET_PPC64
383 disasm_info.mach = bfd_mach_ppc64;
384#else
385 disasm_info.mach = bfd_mach_ppc;
386#endif
bellard9307c4c2004-04-04 12:57:25 +0000387 print_insn = print_insn_ppc;
pbrooke6e59062006-10-22 00:18:54 +0000388#elif defined(TARGET_M68K)
389 print_insn = print_insn_m68k;
bellard6af0bf92005-07-02 14:58:51 +0000390#elif defined(TARGET_MIPS)
bellard76b30302005-12-17 01:10:04 +0000391#ifdef TARGET_WORDS_BIGENDIAN
bellard6af0bf92005-07-02 14:58:51 +0000392 print_insn = print_insn_big_mips;
bellard76b30302005-12-17 01:10:04 +0000393#else
394 print_insn = print_insn_little_mips;
395#endif
Magnus Dammb4e1f072009-11-13 18:54:22 +0900396#elif defined(TARGET_SH4)
397 disasm_info.mach = bfd_mach_sh4;
398 print_insn = print_insn_sh;
bellard9307c4c2004-04-04 12:57:25 +0000399#else
aliguori376253e2009-03-05 23:01:23 +0000400 monitor_printf(mon, "0x" TARGET_FMT_lx
401 ": Asm output not supported on this arch\n", pc);
bellard9307c4c2004-04-04 12:57:25 +0000402 return;
403#endif
404
405 for(i = 0; i < nb_insn; i++) {
aliguori376253e2009-03-05 23:01:23 +0000406 monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
bellard9307c4c2004-04-04 12:57:25 +0000407 count = print_insn(pc, &disasm_info);
aliguori376253e2009-03-05 23:01:23 +0000408 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000409 if (count < 0)
410 break;
411 pc += count;
412 }
413}
414#endif