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