blob: 79e694483cdb8288b752276866ebd69533cbd423 [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"
Paolo Bonzini76cad712012-10-24 11:12:21 +02003#include "disas/bfd.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"
Paolo Bonzini76cad712012-10-24 11:12:21 +02008#include "disas/disas.h"
bellardc6105c02003-10-27 21:13:58 +00009
Blue Swirlf4359b92012-09-08 12:40:00 +000010typedef struct CPUDebug {
11 struct disassemble_info info;
12 CPUArchState *env;
13} CPUDebug;
14
bellardb9adb4a2003-04-29 20:41:16 +000015/* Filled in by elfload.c. Simplistic, but will do for now. */
bellarde80cfcf2004-12-19 23:18:01 +000016struct syminfo *syminfos = NULL;
bellardb9adb4a2003-04-29 20:41:16 +000017
bellardaa0aa4f2003-06-09 15:23:31 +000018/* Get LENGTH bytes from info's buffer, at target address memaddr.
19 Transfer them to myaddr. */
20int
pbrook3a742b72008-10-22 15:55:18 +000021buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
22 struct disassemble_info *info)
bellardaa0aa4f2003-06-09 15:23:31 +000023{
bellardc6105c02003-10-27 21:13:58 +000024 if (memaddr < info->buffer_vma
25 || memaddr + length > info->buffer_vma + info->buffer_length)
26 /* Out of bounds. Use EIO because GDB uses it. */
27 return EIO;
28 memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length);
29 return 0;
bellardaa0aa4f2003-06-09 15:23:31 +000030}
31
bellardc6105c02003-10-27 21:13:58 +000032/* Get LENGTH bytes from info's buffer, at target address memaddr.
33 Transfer them to myaddr. */
34static int
bellardc27004e2005-01-03 23:35:10 +000035target_read_memory (bfd_vma memaddr,
36 bfd_byte *myaddr,
37 int length,
38 struct disassemble_info *info)
bellardc6105c02003-10-27 21:13:58 +000039{
Blue Swirlf4359b92012-09-08 12:40:00 +000040 CPUDebug *s = container_of(info, CPUDebug, info);
41
Andreas Färberf17ec442013-06-29 19:40:58 +020042 cpu_memory_rw_debug(ENV_GET_CPU(s->env), memaddr, myaddr, length, 0);
bellardc6105c02003-10-27 21:13:58 +000043 return 0;
44}
bellardc6105c02003-10-27 21:13:58 +000045
bellardaa0aa4f2003-06-09 15:23:31 +000046/* Print an error message. We can assume that this is in response to
47 an error return from buffer_read_memory. */
48void
pbrook3a742b72008-10-22 15:55:18 +000049perror_memory (int status, bfd_vma memaddr, struct disassemble_info *info)
bellardaa0aa4f2003-06-09 15:23:31 +000050{
51 if (status != EIO)
52 /* Can't happen. */
53 (*info->fprintf_func) (info->stream, "Unknown error %d\n", status);
54 else
55 /* Actually, address between memaddr and memaddr + len was
56 out of bounds. */
57 (*info->fprintf_func) (info->stream,
bellard26a76462006-06-25 18:15:32 +000058 "Address 0x%" PRIx64 " is out of bounds.\n", memaddr);
bellardaa0aa4f2003-06-09 15:23:31 +000059}
60
Jim Meyeringa31f0532012-05-09 05:12:04 +000061/* This could be in a separate file, to save minuscule amounts of space
bellardaa0aa4f2003-06-09 15:23:31 +000062 in statically linked executables. */
63
64/* Just print the address is hex. This is included for completeness even
65 though both GDB and objdump provide their own (to print symbolic
66 addresses). */
67
68void
pbrook3a742b72008-10-22 15:55:18 +000069generic_print_address (bfd_vma addr, struct disassemble_info *info)
bellardaa0aa4f2003-06-09 15:23:31 +000070{
bellard26a76462006-06-25 18:15:32 +000071 (*info->fprintf_func) (info->stream, "0x%" PRIx64, addr);
bellardaa0aa4f2003-06-09 15:23:31 +000072}
73
Peter Maydell636bd282012-06-25 04:55:55 +000074/* Print address in hex, truncated to the width of a target virtual address. */
75static void
76generic_print_target_address(bfd_vma addr, struct disassemble_info *info)
77{
78 uint64_t mask = ~0ULL >> (64 - TARGET_VIRT_ADDR_SPACE_BITS);
79 generic_print_address(addr & mask, info);
80}
81
82/* Print address in hex, truncated to the width of a host virtual address. */
83static void
84generic_print_host_address(bfd_vma addr, struct disassemble_info *info)
85{
86 uint64_t mask = ~0ULL >> (64 - (sizeof(void *) * 8));
87 generic_print_address(addr & mask, info);
88}
89
bellardaa0aa4f2003-06-09 15:23:31 +000090/* Just return the given address. */
91
92int
pbrook3a742b72008-10-22 15:55:18 +000093generic_symbol_at_address (bfd_vma addr, struct disassemble_info *info)
bellardaa0aa4f2003-06-09 15:23:31 +000094{
95 return 1;
96}
97
Aurelien Jarno903ec552010-03-29 02:12:51 +020098bfd_vma bfd_getl64 (const bfd_byte *addr)
99{
100 unsigned long long v;
101
102 v = (unsigned long long) addr[0];
103 v |= (unsigned long long) addr[1] << 8;
104 v |= (unsigned long long) addr[2] << 16;
105 v |= (unsigned long long) addr[3] << 24;
106 v |= (unsigned long long) addr[4] << 32;
107 v |= (unsigned long long) addr[5] << 40;
108 v |= (unsigned long long) addr[6] << 48;
109 v |= (unsigned long long) addr[7] << 56;
110 return (bfd_vma) v;
111}
112
bellardaa0aa4f2003-06-09 15:23:31 +0000113bfd_vma bfd_getl32 (const bfd_byte *addr)
114{
115 unsigned long v;
116
117 v = (unsigned long) addr[0];
118 v |= (unsigned long) addr[1] << 8;
119 v |= (unsigned long) addr[2] << 16;
120 v |= (unsigned long) addr[3] << 24;
121 return (bfd_vma) v;
122}
123
124bfd_vma bfd_getb32 (const bfd_byte *addr)
125{
126 unsigned long v;
127
128 v = (unsigned long) addr[0] << 24;
129 v |= (unsigned long) addr[1] << 16;
130 v |= (unsigned long) addr[2] << 8;
131 v |= (unsigned long) addr[3];
132 return (bfd_vma) v;
133}
134
bellard6af0bf92005-07-02 14:58:51 +0000135bfd_vma bfd_getl16 (const bfd_byte *addr)
136{
137 unsigned long v;
138
139 v = (unsigned long) addr[0];
140 v |= (unsigned long) addr[1] << 8;
141 return (bfd_vma) v;
142}
143
144bfd_vma bfd_getb16 (const bfd_byte *addr)
145{
146 unsigned long v;
147
148 v = (unsigned long) addr[0] << 24;
149 v |= (unsigned long) addr[1] << 16;
150 return (bfd_vma) v;
151}
152
bellardc2d551f2005-04-27 20:15:00 +0000153#ifdef TARGET_ARM
154static int
155print_insn_thumb1(bfd_vma pc, disassemble_info *info)
156{
157 return print_insn_arm(pc | 1, info);
158}
159#endif
160
Richard Hendersonc46ffd52013-08-16 23:29:45 -0700161static int print_insn_objdump(bfd_vma pc, disassemble_info *info,
162 const char *prefix)
163{
164 int i, n = info->buffer_length;
165 uint8_t *buf = g_malloc(n);
166
167 info->read_memory_func(pc, buf, n, info);
168
169 for (i = 0; i < n; ++i) {
170 if (i % 32 == 0) {
171 info->fprintf_func(info->stream, "\n%s: ", prefix);
172 }
173 info->fprintf_func(info->stream, "%02x", buf[i]);
174 }
175
176 g_free(buf);
177 return n;
178}
179
180static int print_insn_od_host(bfd_vma pc, disassemble_info *info)
181{
182 return print_insn_objdump(pc, info, "OBJD-H");
183}
184
185static int print_insn_od_target(bfd_vma pc, disassemble_info *info)
186{
187 return print_insn_objdump(pc, info, "OBJD-T");
188}
189
thse91c8a72007-06-03 13:35:16 +0000190/* Disassemble this for me please... (debugging). 'flags' has the following
bellardc2d551f2005-04-27 20:15:00 +0000191 values:
Frediano Ziglioe99722f2011-08-25 09:14:38 +0200192 i386 - 1 means 16 bit code, 2 means 64 bit code
Claudio Fontana999b53e2014-02-05 17:27:28 +0000193 arm - bit 0 = thumb, bit 1 = reverse endian, bit 2 = A64
bellard6a00d602005-11-21 23:25:50 +0000194 ppc - nonzero means little endian
bellardc2d551f2005-04-27 20:15:00 +0000195 other targets - unused
196 */
Blue Swirlf4359b92012-09-08 12:40:00 +0000197void target_disas(FILE *out, CPUArchState *env, target_ulong code,
198 target_ulong size, int flags)
bellardb9adb4a2003-04-29 20:41:16 +0000199{
bellardc27004e2005-01-03 23:35:10 +0000200 target_ulong pc;
bellardb9adb4a2003-04-29 20:41:16 +0000201 int count;
Blue Swirlf4359b92012-09-08 12:40:00 +0000202 CPUDebug s;
Richard Hendersonc46ffd52013-08-16 23:29:45 -0700203 int (*print_insn)(bfd_vma pc, disassemble_info *info) = NULL;
bellardb9adb4a2003-04-29 20:41:16 +0000204
Blue Swirlf4359b92012-09-08 12:40:00 +0000205 INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
bellardb9adb4a2003-04-29 20:41:16 +0000206
Blue Swirlf4359b92012-09-08 12:40:00 +0000207 s.env = env;
208 s.info.read_memory_func = target_read_memory;
209 s.info.buffer_vma = code;
210 s.info.buffer_length = size;
211 s.info.print_address_func = generic_print_target_address;
bellardc27004e2005-01-03 23:35:10 +0000212
213#ifdef TARGET_WORDS_BIGENDIAN
Blue Swirlf4359b92012-09-08 12:40:00 +0000214 s.info.endian = BFD_ENDIAN_BIG;
bellardc27004e2005-01-03 23:35:10 +0000215#else
Blue Swirlf4359b92012-09-08 12:40:00 +0000216 s.info.endian = BFD_ENDIAN_LITTLE;
bellardc6105c02003-10-27 21:13:58 +0000217#endif
bellardc27004e2005-01-03 23:35:10 +0000218#if defined(TARGET_I386)
Blue Swirlf4359b92012-09-08 12:40:00 +0000219 if (flags == 2) {
220 s.info.mach = bfd_mach_x86_64;
221 } else if (flags == 1) {
222 s.info.mach = bfd_mach_i386_i8086;
223 } else {
224 s.info.mach = bfd_mach_i386_i386;
225 }
bellardc27004e2005-01-03 23:35:10 +0000226 print_insn = print_insn_i386;
227#elif defined(TARGET_ARM)
Claudio Fontana999b53e2014-02-05 17:27:28 +0000228 if (flags & 4) {
229 /* We might not be compiled with the A64 disassembler
230 * because it needs a C++ compiler; in that case we will
231 * fall through to the default print_insn_od case.
232 */
233#if defined(CONFIG_ARM_A64_DIS)
234 print_insn = print_insn_arm_a64;
235#endif
236 } else if (flags & 1) {
Paul Brookd8fd2952012-03-30 18:02:50 +0100237 print_insn = print_insn_thumb1;
238 } else {
239 print_insn = print_insn_arm;
240 }
241 if (flags & 2) {
242#ifdef TARGET_WORDS_BIGENDIAN
Blue Swirlf4359b92012-09-08 12:40:00 +0000243 s.info.endian = BFD_ENDIAN_LITTLE;
Paul Brookd8fd2952012-03-30 18:02:50 +0100244#else
Blue Swirlf4359b92012-09-08 12:40:00 +0000245 s.info.endian = BFD_ENDIAN_BIG;
Paul Brookd8fd2952012-03-30 18:02:50 +0100246#endif
247 }
bellardc27004e2005-01-03 23:35:10 +0000248#elif defined(TARGET_SPARC)
249 print_insn = print_insn_sparc;
bellard34751872005-07-02 14:31:34 +0000250#ifdef TARGET_SPARC64
Blue Swirlf4359b92012-09-08 12:40:00 +0000251 s.info.mach = bfd_mach_sparc_v9b;
ths3b46e622007-09-17 08:09:54 +0000252#endif
bellardc27004e2005-01-03 23:35:10 +0000253#elif defined(TARGET_PPC)
Blue Swirlf4359b92012-09-08 12:40:00 +0000254 if (flags >> 16) {
255 s.info.endian = BFD_ENDIAN_LITTLE;
256 }
j_mayer237c0af2007-09-29 12:01:46 +0000257 if (flags & 0xFFFF) {
258 /* If we have a precise definitions of the instructions set, use it */
Blue Swirlf4359b92012-09-08 12:40:00 +0000259 s.info.mach = flags & 0xFFFF;
j_mayer237c0af2007-09-29 12:01:46 +0000260 } else {
bellarda2458622005-07-23 22:39:53 +0000261#ifdef TARGET_PPC64
Blue Swirlf4359b92012-09-08 12:40:00 +0000262 s.info.mach = bfd_mach_ppc64;
bellarda2458622005-07-23 22:39:53 +0000263#else
Blue Swirlf4359b92012-09-08 12:40:00 +0000264 s.info.mach = bfd_mach_ppc;
bellarda2458622005-07-23 22:39:53 +0000265#endif
j_mayer237c0af2007-09-29 12:01:46 +0000266 }
Aurelien Jarno88770fe2013-04-20 08:56:14 +0000267 s.info.disassembler_options = (char *)"any";
bellardc27004e2005-01-03 23:35:10 +0000268 print_insn = print_insn_ppc;
pbrooke6e59062006-10-22 00:18:54 +0000269#elif defined(TARGET_M68K)
270 print_insn = print_insn_m68k;
bellard6af0bf92005-07-02 14:58:51 +0000271#elif defined(TARGET_MIPS)
bellard76b30302005-12-17 01:10:04 +0000272#ifdef TARGET_WORDS_BIGENDIAN
bellard6af0bf92005-07-02 14:58:51 +0000273 print_insn = print_insn_big_mips;
bellard76b30302005-12-17 01:10:04 +0000274#else
275 print_insn = print_insn_little_mips;
276#endif
bellardfdf9b3e2006-04-27 21:07:38 +0000277#elif defined(TARGET_SH4)
Blue Swirlf4359b92012-09-08 12:40:00 +0000278 s.info.mach = bfd_mach_sh4;
bellardfdf9b3e2006-04-27 21:07:38 +0000279 print_insn = print_insn_sh;
j_mayereddf68a2007-04-05 07:22:49 +0000280#elif defined(TARGET_ALPHA)
Blue Swirlf4359b92012-09-08 12:40:00 +0000281 s.info.mach = bfd_mach_alpha_ev6;
j_mayereddf68a2007-04-05 07:22:49 +0000282 print_insn = print_insn_alpha;
thsa25fd132007-10-08 12:46:58 +0000283#elif defined(TARGET_CRIS)
Edgar E. Iglesiasb09cd072011-01-10 22:31:09 +0100284 if (flags != 32) {
Blue Swirlf4359b92012-09-08 12:40:00 +0000285 s.info.mach = bfd_mach_cris_v0_v10;
Edgar E. Iglesiasb09cd072011-01-10 22:31:09 +0100286 print_insn = print_insn_crisv10;
287 } else {
Blue Swirlf4359b92012-09-08 12:40:00 +0000288 s.info.mach = bfd_mach_cris_v32;
Edgar E. Iglesiasb09cd072011-01-10 22:31:09 +0100289 print_insn = print_insn_crisv32;
290 }
Ulrich Hechtdb500602011-03-29 15:29:32 +0200291#elif defined(TARGET_S390X)
Blue Swirlf4359b92012-09-08 12:40:00 +0000292 s.info.mach = bfd_mach_s390_64;
Ulrich Hechtdb500602011-03-29 15:29:32 +0200293 print_insn = print_insn_s390;
Edgar E. Iglesiase90e3902009-05-20 20:07:38 +0200294#elif defined(TARGET_MICROBLAZE)
Blue Swirlf4359b92012-09-08 12:40:00 +0000295 s.info.mach = bfd_arch_microblaze;
Edgar E. Iglesiase90e3902009-05-20 20:07:38 +0200296 print_insn = print_insn_microblaze;
Anthony Greenbd86a882013-03-18 15:49:23 -0400297#elif defined(TARGET_MOXIE)
298 s.info.mach = bfd_arch_moxie;
299 print_insn = print_insn_moxie;
Michael Walle79368f42012-03-31 19:54:20 +0200300#elif defined(TARGET_LM32)
Blue Swirlf4359b92012-09-08 12:40:00 +0000301 s.info.mach = bfd_mach_lm32;
Michael Walle79368f42012-03-31 19:54:20 +0200302 print_insn = print_insn_lm32;
bellardc27004e2005-01-03 23:35:10 +0000303#endif
Richard Hendersonc46ffd52013-08-16 23:29:45 -0700304 if (print_insn == NULL) {
305 print_insn = print_insn_od_target;
306 }
bellardc27004e2005-01-03 23:35:10 +0000307
blueswir17e000c22009-02-13 21:44:41 +0000308 for (pc = code; size > 0; pc += count, size -= count) {
bellardfa15e032005-01-31 23:32:31 +0000309 fprintf(out, "0x" TARGET_FMT_lx ": ", pc);
Blue Swirlf4359b92012-09-08 12:40:00 +0000310 count = print_insn(pc, &s.info);
bellardc27004e2005-01-03 23:35:10 +0000311#if 0
312 {
313 int i;
314 uint8_t b;
315 fprintf(out, " {");
316 for(i = 0; i < count; i++) {
Blue Swirlf4359b92012-09-08 12:40:00 +0000317 target_read_memory(pc + i, &b, 1, &s.info);
bellardc27004e2005-01-03 23:35:10 +0000318 fprintf(out, " %02x", b);
319 }
320 fprintf(out, " }");
321 }
322#endif
323 fprintf(out, "\n");
324 if (count < 0)
325 break;
malc754d00a2009-04-21 22:26:22 +0000326 if (size < count) {
327 fprintf(out,
328 "Disassembler disagrees with translator over instruction "
329 "decoding\n"
330 "Please report this to qemu-devel@nongnu.org\n");
331 break;
332 }
bellardc27004e2005-01-03 23:35:10 +0000333 }
334}
335
336/* Disassemble this for me please... (debugging). */
337void disas(FILE *out, void *code, unsigned long size)
338{
Stefan Weilb0b0f1c2012-04-12 15:44:35 +0200339 uintptr_t pc;
bellardc27004e2005-01-03 23:35:10 +0000340 int count;
Blue Swirlf4359b92012-09-08 12:40:00 +0000341 CPUDebug s;
Richard Hendersonc46ffd52013-08-16 23:29:45 -0700342 int (*print_insn)(bfd_vma pc, disassemble_info *info) = NULL;
bellardc27004e2005-01-03 23:35:10 +0000343
Blue Swirlf4359b92012-09-08 12:40:00 +0000344 INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
345 s.info.print_address_func = generic_print_host_address;
bellardc6105c02003-10-27 21:13:58 +0000346
Blue Swirlf4359b92012-09-08 12:40:00 +0000347 s.info.buffer = code;
348 s.info.buffer_vma = (uintptr_t)code;
349 s.info.buffer_length = size;
bellardb9adb4a2003-04-29 20:41:16 +0000350
Juan Quintelae2542fe2009-07-27 16:13:06 +0200351#ifdef HOST_WORDS_BIGENDIAN
Blue Swirlf4359b92012-09-08 12:40:00 +0000352 s.info.endian = BFD_ENDIAN_BIG;
bellardb9adb4a2003-04-29 20:41:16 +0000353#else
Blue Swirlf4359b92012-09-08 12:40:00 +0000354 s.info.endian = BFD_ENDIAN_LITTLE;
bellardb9adb4a2003-04-29 20:41:16 +0000355#endif
Stefan Weil5826e512011-10-05 20:03:53 +0200356#if defined(CONFIG_TCG_INTERPRETER)
357 print_insn = print_insn_tci;
358#elif defined(__i386__)
Blue Swirlf4359b92012-09-08 12:40:00 +0000359 s.info.mach = bfd_mach_i386_i386;
bellardc27004e2005-01-03 23:35:10 +0000360 print_insn = print_insn_i386;
bellardbc51c5c2004-03-17 23:46:04 +0000361#elif defined(__x86_64__)
Blue Swirlf4359b92012-09-08 12:40:00 +0000362 s.info.mach = bfd_mach_x86_64;
bellardc27004e2005-01-03 23:35:10 +0000363 print_insn = print_insn_i386;
malce58ffeb2009-01-14 18:39:49 +0000364#elif defined(_ARCH_PPC)
Richard Henderson66d4f6a2013-01-31 11:16:21 -0800365 s.info.disassembler_options = (char *)"any";
bellardc27004e2005-01-03 23:35:10 +0000366 print_insn = print_insn_ppc;
Claudio Fontana999b53e2014-02-05 17:27:28 +0000367#elif defined(__aarch64__) && defined(CONFIG_ARM_A64_DIS)
368 print_insn = print_insn_arm_a64;
bellarda993ba82003-05-11 12:25:45 +0000369#elif defined(__alpha__)
bellardc27004e2005-01-03 23:35:10 +0000370 print_insn = print_insn_alpha;
bellardaa0aa4f2003-06-09 15:23:31 +0000371#elif defined(__sparc__)
bellardc27004e2005-01-03 23:35:10 +0000372 print_insn = print_insn_sparc;
Blue Swirlf4359b92012-09-08 12:40:00 +0000373 s.info.mach = bfd_mach_sparc_v9b;
ths5fafdf22007-09-16 21:08:06 +0000374#elif defined(__arm__)
bellardc27004e2005-01-03 23:35:10 +0000375 print_insn = print_insn_arm;
bellard6af0bf92005-07-02 14:58:51 +0000376#elif defined(__MIPSEB__)
377 print_insn = print_insn_big_mips;
378#elif defined(__MIPSEL__)
379 print_insn = print_insn_little_mips;
bellard48024e42005-11-06 16:52:11 +0000380#elif defined(__m68k__)
381 print_insn = print_insn_m68k;
ths8f860bb2007-07-31 23:44:21 +0000382#elif defined(__s390__)
383 print_insn = print_insn_s390;
aurel32f54b3f92008-04-12 20:14:54 +0000384#elif defined(__hppa__)
385 print_insn = print_insn_hppa;
Aurelien Jarno903ec552010-03-29 02:12:51 +0200386#elif defined(__ia64__)
387 print_insn = print_insn_ia64;
bellardb9adb4a2003-04-29 20:41:16 +0000388#endif
Richard Hendersonc46ffd52013-08-16 23:29:45 -0700389 if (print_insn == NULL) {
390 print_insn = print_insn_od_host;
391 }
Stefan Weilb0b0f1c2012-04-12 15:44:35 +0200392 for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) {
393 fprintf(out, "0x%08" PRIxPTR ": ", pc);
Blue Swirlf4359b92012-09-08 12:40:00 +0000394 count = print_insn(pc, &s.info);
bellardb9adb4a2003-04-29 20:41:16 +0000395 fprintf(out, "\n");
396 if (count < 0)
397 break;
398 }
399}
400
401/* Look up symbol for debugging purpose. Returns "" if unknown. */
bellardc27004e2005-01-03 23:35:10 +0000402const char *lookup_symbol(target_ulong orig_addr)
bellardb9adb4a2003-04-29 20:41:16 +0000403{
pbrook49918a72008-10-22 15:11:31 +0000404 const char *symbol = "";
bellarde80cfcf2004-12-19 23:18:01 +0000405 struct syminfo *s;
ths3b46e622007-09-17 08:09:54 +0000406
bellarde80cfcf2004-12-19 23:18:01 +0000407 for (s = syminfos; s; s = s->next) {
pbrook49918a72008-10-22 15:11:31 +0000408 symbol = s->lookup_symbol(s, orig_addr);
409 if (symbol[0] != '\0') {
410 break;
411 }
bellardb9adb4a2003-04-29 20:41:16 +0000412 }
pbrook49918a72008-10-22 15:11:31 +0000413
414 return symbol;
bellardb9adb4a2003-04-29 20:41:16 +0000415}
bellard9307c4c2004-04-04 12:57:25 +0000416
417#if !defined(CONFIG_USER_ONLY)
418
Paolo Bonzini83c90892012-12-17 18:19:49 +0100419#include "monitor/monitor.h"
bellard3d2cfdf2004-08-01 21:49:07 +0000420
bellard9307c4c2004-04-04 12:57:25 +0000421static int monitor_disas_is_physical;
422
423static int
blueswir1a5f1b962008-08-17 20:21:51 +0000424monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
425 struct disassemble_info *info)
bellard9307c4c2004-04-04 12:57:25 +0000426{
Blue Swirlf4359b92012-09-08 12:40:00 +0000427 CPUDebug *s = container_of(info, CPUDebug, info);
428
bellard9307c4c2004-04-04 12:57:25 +0000429 if (monitor_disas_is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +0200430 cpu_physical_memory_read(memaddr, myaddr, length);
bellard9307c4c2004-04-04 12:57:25 +0000431 } else {
Andreas Färberf17ec442013-06-29 19:40:58 +0200432 cpu_memory_rw_debug(ENV_GET_CPU(s->env), memaddr, myaddr, length, 0);
bellard9307c4c2004-04-04 12:57:25 +0000433 }
434 return 0;
435}
436
Stefan Weil8b7968f2010-09-23 21:28:05 +0200437static int GCC_FMT_ATTR(2, 3)
438monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard3d2cfdf2004-08-01 21:49:07 +0000439{
440 va_list ap;
441 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000442 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard3d2cfdf2004-08-01 21:49:07 +0000443 va_end(ap);
444 return 0;
445}
446
Andreas Färber9349b4f2012-03-14 01:38:32 +0100447void monitor_disas(Monitor *mon, CPUArchState *env,
bellard6a00d602005-11-21 23:25:50 +0000448 target_ulong pc, int nb_insn, int is_physical, int flags)
bellard9307c4c2004-04-04 12:57:25 +0000449{
bellard9307c4c2004-04-04 12:57:25 +0000450 int count, i;
Blue Swirlf4359b92012-09-08 12:40:00 +0000451 CPUDebug s;
bellard9307c4c2004-04-04 12:57:25 +0000452 int (*print_insn)(bfd_vma pc, disassemble_info *info);
453
Blue Swirlf4359b92012-09-08 12:40:00 +0000454 INIT_DISASSEMBLE_INFO(s.info, (FILE *)mon, monitor_fprintf);
bellard9307c4c2004-04-04 12:57:25 +0000455
Blue Swirlf4359b92012-09-08 12:40:00 +0000456 s.env = env;
bellard9307c4c2004-04-04 12:57:25 +0000457 monitor_disas_is_physical = is_physical;
Blue Swirlf4359b92012-09-08 12:40:00 +0000458 s.info.read_memory_func = monitor_read_memory;
459 s.info.print_address_func = generic_print_target_address;
bellard9307c4c2004-04-04 12:57:25 +0000460
Blue Swirlf4359b92012-09-08 12:40:00 +0000461 s.info.buffer_vma = pc;
bellard9307c4c2004-04-04 12:57:25 +0000462
463#ifdef TARGET_WORDS_BIGENDIAN
Blue Swirlf4359b92012-09-08 12:40:00 +0000464 s.info.endian = BFD_ENDIAN_BIG;
bellard9307c4c2004-04-04 12:57:25 +0000465#else
Blue Swirlf4359b92012-09-08 12:40:00 +0000466 s.info.endian = BFD_ENDIAN_LITTLE;
bellard9307c4c2004-04-04 12:57:25 +0000467#endif
468#if defined(TARGET_I386)
Blue Swirlf4359b92012-09-08 12:40:00 +0000469 if (flags == 2) {
470 s.info.mach = bfd_mach_x86_64;
471 } else if (flags == 1) {
472 s.info.mach = bfd_mach_i386_i8086;
473 } else {
474 s.info.mach = bfd_mach_i386_i386;
475 }
bellard9307c4c2004-04-04 12:57:25 +0000476 print_insn = print_insn_i386;
477#elif defined(TARGET_ARM)
478 print_insn = print_insn_arm;
thscbd669d2007-12-25 00:26:36 +0000479#elif defined(TARGET_ALPHA)
480 print_insn = print_insn_alpha;
bellard9307c4c2004-04-04 12:57:25 +0000481#elif defined(TARGET_SPARC)
482 print_insn = print_insn_sparc;
blueswir1682c4f12007-04-09 15:14:57 +0000483#ifdef TARGET_SPARC64
Blue Swirlf4359b92012-09-08 12:40:00 +0000484 s.info.mach = bfd_mach_sparc_v9b;
blueswir1682c4f12007-04-09 15:14:57 +0000485#endif
bellard9307c4c2004-04-04 12:57:25 +0000486#elif defined(TARGET_PPC)
bellarda2458622005-07-23 22:39:53 +0000487#ifdef TARGET_PPC64
Blue Swirlf4359b92012-09-08 12:40:00 +0000488 s.info.mach = bfd_mach_ppc64;
bellarda2458622005-07-23 22:39:53 +0000489#else
Blue Swirlf4359b92012-09-08 12:40:00 +0000490 s.info.mach = bfd_mach_ppc;
bellarda2458622005-07-23 22:39:53 +0000491#endif
bellard9307c4c2004-04-04 12:57:25 +0000492 print_insn = print_insn_ppc;
pbrooke6e59062006-10-22 00:18:54 +0000493#elif defined(TARGET_M68K)
494 print_insn = print_insn_m68k;
bellard6af0bf92005-07-02 14:58:51 +0000495#elif defined(TARGET_MIPS)
bellard76b30302005-12-17 01:10:04 +0000496#ifdef TARGET_WORDS_BIGENDIAN
bellard6af0bf92005-07-02 14:58:51 +0000497 print_insn = print_insn_big_mips;
bellard76b30302005-12-17 01:10:04 +0000498#else
499 print_insn = print_insn_little_mips;
500#endif
Magnus Dammb4e1f072009-11-13 18:54:22 +0900501#elif defined(TARGET_SH4)
Blue Swirlf4359b92012-09-08 12:40:00 +0000502 s.info.mach = bfd_mach_sh4;
Magnus Dammb4e1f072009-11-13 18:54:22 +0900503 print_insn = print_insn_sh;
Ulrich Hechtdb500602011-03-29 15:29:32 +0200504#elif defined(TARGET_S390X)
Blue Swirlf4359b92012-09-08 12:40:00 +0000505 s.info.mach = bfd_mach_s390_64;
Ulrich Hechtdb500602011-03-29 15:29:32 +0200506 print_insn = print_insn_s390;
Anthony Greenbd86a882013-03-18 15:49:23 -0400507#elif defined(TARGET_MOXIE)
508 s.info.mach = bfd_arch_moxie;
509 print_insn = print_insn_moxie;
Michael Walle79368f42012-03-31 19:54:20 +0200510#elif defined(TARGET_LM32)
Blue Swirlf4359b92012-09-08 12:40:00 +0000511 s.info.mach = bfd_mach_lm32;
Michael Walle79368f42012-03-31 19:54:20 +0200512 print_insn = print_insn_lm32;
bellard9307c4c2004-04-04 12:57:25 +0000513#else
aliguori376253e2009-03-05 23:01:23 +0000514 monitor_printf(mon, "0x" TARGET_FMT_lx
515 ": Asm output not supported on this arch\n", pc);
bellard9307c4c2004-04-04 12:57:25 +0000516 return;
517#endif
518
519 for(i = 0; i < nb_insn; i++) {
aliguori376253e2009-03-05 23:01:23 +0000520 monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc);
Blue Swirlf4359b92012-09-08 12:40:00 +0000521 count = print_insn(pc, &s.info);
aliguori376253e2009-03-05 23:01:23 +0000522 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000523 if (count < 0)
524 break;
525 pc += count;
526 }
527}
528#endif