bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 1 | /* General "disassemble this chunk" code. Used for debugging. */ |
bellard | 5bbe929 | 2003-06-09 19:38:38 +0000 | [diff] [blame] | 2 | #include "config.h" |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 3 | #include "dis-asm.h" |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 4 | #include "elf.h" |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 5 | #include <errno.h> |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 6 | |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 7 | #include "cpu.h" |
| 8 | #include "exec-all.h" |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 9 | #include "disas.h" |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 10 | |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 11 | /* Filled in by elfload.c. Simplistic, but will do for now. */ |
| 12 | unsigned int disas_num_syms; |
| 13 | void *disas_symtab; |
| 14 | const char *disas_strtab; |
| 15 | |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 16 | /* Get LENGTH bytes from info's buffer, at target address memaddr. |
| 17 | Transfer them to myaddr. */ |
| 18 | int |
| 19 | buffer_read_memory (memaddr, myaddr, length, info) |
| 20 | bfd_vma memaddr; |
| 21 | bfd_byte *myaddr; |
| 22 | int length; |
| 23 | struct disassemble_info *info; |
| 24 | { |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 25 | if (memaddr < info->buffer_vma |
| 26 | || memaddr + length > info->buffer_vma + info->buffer_length) |
| 27 | /* Out of bounds. Use EIO because GDB uses it. */ |
| 28 | return EIO; |
| 29 | memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length); |
| 30 | return 0; |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 31 | } |
| 32 | |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 33 | #if !defined(CONFIG_USER_ONLY) |
| 34 | /* Get LENGTH bytes from info's buffer, at target address memaddr. |
| 35 | Transfer them to myaddr. */ |
| 36 | static int |
| 37 | target_read_memory (memaddr, myaddr, length, info) |
| 38 | bfd_vma memaddr; |
| 39 | bfd_byte *myaddr; |
| 40 | int length; |
| 41 | struct disassemble_info *info; |
| 42 | { |
| 43 | int i; |
| 44 | for(i = 0; i < length; i++) { |
bellard | baf8ebf | 2003-10-27 23:57:40 +0000 | [diff] [blame] | 45 | myaddr[i] = ldub_code((void *)((long)memaddr + i)); |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 46 | } |
| 47 | return 0; |
| 48 | } |
| 49 | #endif |
| 50 | |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 51 | /* Print an error message. We can assume that this is in response to |
| 52 | an error return from buffer_read_memory. */ |
| 53 | void |
| 54 | perror_memory (status, memaddr, info) |
| 55 | int status; |
| 56 | bfd_vma memaddr; |
| 57 | struct disassemble_info *info; |
| 58 | { |
| 59 | if (status != EIO) |
| 60 | /* Can't happen. */ |
| 61 | (*info->fprintf_func) (info->stream, "Unknown error %d\n", status); |
| 62 | else |
| 63 | /* Actually, address between memaddr and memaddr + len was |
| 64 | out of bounds. */ |
| 65 | (*info->fprintf_func) (info->stream, |
bellard | d44b29c | 2003-07-13 17:29:55 +0000 | [diff] [blame] | 66 | "Address 0x%llx is out of bounds.\n", memaddr); |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* This could be in a separate file, to save miniscule amounts of space |
| 70 | in statically linked executables. */ |
| 71 | |
| 72 | /* Just print the address is hex. This is included for completeness even |
| 73 | though both GDB and objdump provide their own (to print symbolic |
| 74 | addresses). */ |
| 75 | |
| 76 | void |
| 77 | generic_print_address (addr, info) |
| 78 | bfd_vma addr; |
| 79 | struct disassemble_info *info; |
| 80 | { |
bellard | d44b29c | 2003-07-13 17:29:55 +0000 | [diff] [blame] | 81 | (*info->fprintf_func) (info->stream, "0x%llx", addr); |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* Just return the given address. */ |
| 85 | |
| 86 | int |
| 87 | generic_symbol_at_address (addr, info) |
| 88 | bfd_vma addr; |
| 89 | struct disassemble_info * info; |
| 90 | { |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | bfd_vma bfd_getl32 (const bfd_byte *addr) |
| 95 | { |
| 96 | unsigned long v; |
| 97 | |
| 98 | v = (unsigned long) addr[0]; |
| 99 | v |= (unsigned long) addr[1] << 8; |
| 100 | v |= (unsigned long) addr[2] << 16; |
| 101 | v |= (unsigned long) addr[3] << 24; |
| 102 | return (bfd_vma) v; |
| 103 | } |
| 104 | |
| 105 | bfd_vma bfd_getb32 (const bfd_byte *addr) |
| 106 | { |
| 107 | unsigned long v; |
| 108 | |
| 109 | v = (unsigned long) addr[0] << 24; |
| 110 | v |= (unsigned long) addr[1] << 16; |
| 111 | v |= (unsigned long) addr[2] << 8; |
| 112 | v |= (unsigned long) addr[3]; |
| 113 | return (bfd_vma) v; |
| 114 | } |
| 115 | |
bellard | 95cbfc6 | 2003-06-15 19:44:10 +0000 | [diff] [blame] | 116 | /* Disassemble this for me please... (debugging). 'flags' is only used |
| 117 | for i386: non zero means 16 bit code */ |
| 118 | void disas(FILE *out, void *code, unsigned long size, int is_host, int flags) |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 119 | { |
| 120 | uint8_t *pc; |
| 121 | int count; |
| 122 | struct disassemble_info disasm_info; |
| 123 | int (*print_insn)(bfd_vma pc, disassemble_info *info); |
| 124 | |
| 125 | INIT_DISASSEMBLE_INFO(disasm_info, out, fprintf); |
| 126 | |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 127 | #if !defined(CONFIG_USER_ONLY) |
| 128 | if (!is_host) { |
| 129 | disasm_info.read_memory_func = target_read_memory; |
| 130 | } |
| 131 | #endif |
| 132 | |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 133 | disasm_info.buffer = code; |
| 134 | disasm_info.buffer_vma = (unsigned long)code; |
| 135 | disasm_info.buffer_length = size; |
| 136 | |
bellard | 95cbfc6 | 2003-06-15 19:44:10 +0000 | [diff] [blame] | 137 | if (is_host) { |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 138 | #ifdef WORDS_BIGENDIAN |
| 139 | disasm_info.endian = BFD_ENDIAN_BIG; |
| 140 | #else |
| 141 | disasm_info.endian = BFD_ENDIAN_LITTLE; |
| 142 | #endif |
bellard | bc51c5c | 2004-03-17 23:46:04 +0000 | [diff] [blame] | 143 | #if defined(__i386__) |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 144 | disasm_info.mach = bfd_mach_i386_i386; |
| 145 | print_insn = print_insn_i386; |
bellard | bc51c5c | 2004-03-17 23:46:04 +0000 | [diff] [blame] | 146 | #elif defined(__x86_64__) |
| 147 | disasm_info.mach = bfd_mach_x86_64; |
| 148 | print_insn = print_insn_i386; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 149 | #elif defined(__powerpc__) |
| 150 | print_insn = print_insn_ppc; |
bellard | a993ba8 | 2003-05-11 12:25:45 +0000 | [diff] [blame] | 151 | #elif defined(__alpha__) |
| 152 | print_insn = print_insn_alpha; |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 153 | #elif defined(__sparc__) |
| 154 | print_insn = print_insn_sparc; |
| 155 | #elif defined(__arm__) |
| 156 | print_insn = print_insn_arm; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 157 | #else |
| 158 | fprintf(out, "Asm output not supported on this arch\n"); |
| 159 | return; |
| 160 | #endif |
| 161 | } else { |
bellard | 95cbfc6 | 2003-06-15 19:44:10 +0000 | [diff] [blame] | 162 | #ifdef TARGET_WORDS_BIGENDIAN |
| 163 | disasm_info.endian = BFD_ENDIAN_BIG; |
| 164 | #else |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 165 | disasm_info.endian = BFD_ENDIAN_LITTLE; |
bellard | 95cbfc6 | 2003-06-15 19:44:10 +0000 | [diff] [blame] | 166 | #endif |
| 167 | #if defined(TARGET_I386) |
| 168 | if (!flags) |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 169 | disasm_info.mach = bfd_mach_i386_i386; |
| 170 | else |
| 171 | disasm_info.mach = bfd_mach_i386_i8086; |
| 172 | print_insn = print_insn_i386; |
bellard | 95cbfc6 | 2003-06-15 19:44:10 +0000 | [diff] [blame] | 173 | #elif defined(TARGET_ARM) |
| 174 | print_insn = print_insn_arm; |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 175 | #elif defined(TARGET_SPARC) |
| 176 | print_insn = print_insn_sparc; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 177 | #elif defined(TARGET_PPC) |
| 178 | print_insn = print_insn_ppc; |
bellard | 95cbfc6 | 2003-06-15 19:44:10 +0000 | [diff] [blame] | 179 | #else |
| 180 | fprintf(out, "Asm output not supported on this arch\n"); |
| 181 | return; |
| 182 | #endif |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | for (pc = code; pc < (uint8_t *)code + size; pc += count) { |
| 186 | fprintf(out, "0x%08lx: ", (long)pc); |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 187 | #ifdef __arm__ |
| 188 | /* since data are included in the code, it is better to |
| 189 | display code data too */ |
bellard | 95cbfc6 | 2003-06-15 19:44:10 +0000 | [diff] [blame] | 190 | if (is_host) { |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 191 | fprintf(out, "%08x ", (int)bfd_getl32((const bfd_byte *)pc)); |
| 192 | } |
| 193 | #endif |
bellard | 08351fb | 2003-05-25 16:42:20 +0000 | [diff] [blame] | 194 | count = print_insn((unsigned long)pc, &disasm_info); |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 195 | fprintf(out, "\n"); |
| 196 | if (count < 0) |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* Look up symbol for debugging purpose. Returns "" if unknown. */ |
| 202 | const char *lookup_symbol(void *orig_addr) |
| 203 | { |
| 204 | unsigned int i; |
| 205 | /* Hack, because we know this is x86. */ |
| 206 | Elf32_Sym *sym = disas_symtab; |
| 207 | |
| 208 | for (i = 0; i < disas_num_syms; i++) { |
| 209 | if (sym[i].st_shndx == SHN_UNDEF |
| 210 | || sym[i].st_shndx >= SHN_LORESERVE) |
| 211 | continue; |
| 212 | |
| 213 | if (ELF_ST_TYPE(sym[i].st_info) != STT_FUNC) |
| 214 | continue; |
| 215 | |
| 216 | if ((long)orig_addr >= sym[i].st_value |
| 217 | && (long)orig_addr < sym[i].st_value + sym[i].st_size) |
| 218 | return disas_strtab + sym[i].st_name; |
| 219 | } |
| 220 | return ""; |
| 221 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 222 | |
| 223 | #if !defined(CONFIG_USER_ONLY) |
| 224 | |
bellard | 3d2cfdf | 2004-08-01 21:49:07 +0000 | [diff] [blame] | 225 | void term_vprintf(const char *fmt, va_list ap); |
| 226 | void term_printf(const char *fmt, ...); |
| 227 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 228 | static int monitor_disas_is_physical; |
| 229 | |
| 230 | static int |
| 231 | monitor_read_memory (memaddr, myaddr, length, info) |
| 232 | bfd_vma memaddr; |
| 233 | bfd_byte *myaddr; |
| 234 | int length; |
| 235 | struct disassemble_info *info; |
| 236 | { |
| 237 | if (monitor_disas_is_physical) { |
| 238 | cpu_physical_memory_rw(memaddr, myaddr, length, 0); |
| 239 | } else { |
| 240 | cpu_memory_rw_debug(cpu_single_env, memaddr,myaddr, length, 0); |
| 241 | } |
| 242 | return 0; |
| 243 | } |
| 244 | |
bellard | 3d2cfdf | 2004-08-01 21:49:07 +0000 | [diff] [blame] | 245 | static int monitor_fprintf(FILE *stream, const char *fmt, ...) |
| 246 | { |
| 247 | va_list ap; |
| 248 | va_start(ap, fmt); |
| 249 | term_vprintf(fmt, ap); |
| 250 | va_end(ap); |
| 251 | return 0; |
| 252 | } |
| 253 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 254 | void monitor_disas(target_ulong pc, int nb_insn, int is_physical, int flags) |
| 255 | { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 256 | int count, i; |
| 257 | struct disassemble_info disasm_info; |
| 258 | int (*print_insn)(bfd_vma pc, disassemble_info *info); |
| 259 | |
bellard | 3d2cfdf | 2004-08-01 21:49:07 +0000 | [diff] [blame] | 260 | INIT_DISASSEMBLE_INFO(disasm_info, NULL, monitor_fprintf); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 261 | |
| 262 | monitor_disas_is_physical = is_physical; |
| 263 | disasm_info.read_memory_func = monitor_read_memory; |
| 264 | |
| 265 | disasm_info.buffer_vma = pc; |
| 266 | |
| 267 | #ifdef TARGET_WORDS_BIGENDIAN |
| 268 | disasm_info.endian = BFD_ENDIAN_BIG; |
| 269 | #else |
| 270 | disasm_info.endian = BFD_ENDIAN_LITTLE; |
| 271 | #endif |
| 272 | #if defined(TARGET_I386) |
| 273 | if (!flags) |
| 274 | disasm_info.mach = bfd_mach_i386_i386; |
| 275 | else |
| 276 | disasm_info.mach = bfd_mach_i386_i8086; |
| 277 | print_insn = print_insn_i386; |
| 278 | #elif defined(TARGET_ARM) |
| 279 | print_insn = print_insn_arm; |
| 280 | #elif defined(TARGET_SPARC) |
| 281 | print_insn = print_insn_sparc; |
| 282 | #elif defined(TARGET_PPC) |
| 283 | print_insn = print_insn_ppc; |
| 284 | #else |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 285 | term_printf("Asm output not supported on this arch\n"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 286 | return; |
| 287 | #endif |
| 288 | |
| 289 | for(i = 0; i < nb_insn; i++) { |
bellard | 3d2cfdf | 2004-08-01 21:49:07 +0000 | [diff] [blame] | 290 | term_printf("0x%08lx: ", (unsigned long)pc); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 291 | count = print_insn(pc, &disasm_info); |
bellard | 3d2cfdf | 2004-08-01 21:49:07 +0000 | [diff] [blame] | 292 | term_printf("\n"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 293 | if (count < 0) |
| 294 | break; |
| 295 | pc += count; |
| 296 | } |
| 297 | } |
| 298 | #endif |