bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 1 | /* General "disassemble this chunk" code. Used for debugging. */ |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 2 | #include "qemu/osdep.h" |
Markus Armbruster | 3979fca | 2019-04-17 21:18:04 +0200 | [diff] [blame] | 3 | #include "disas/dis-asm.h" |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 4 | #include "elf.h" |
Markus Armbruster | 30cc983 | 2019-04-17 21:18:03 +0200 | [diff] [blame] | 5 | #include "qemu/qemu-print.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" |
Paolo Bonzini | 76cad71 | 2012-10-24 11:12:21 +0200 | [diff] [blame] | 8 | #include "disas/disas.h" |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 9 | #include "disas/capstone.h" |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 10 | |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 11 | typedef struct CPUDebug { |
| 12 | struct disassemble_info info; |
Peter Crosthwaite | d49190c | 2015-05-24 14:20:41 -0700 | [diff] [blame] | 13 | CPUState *cpu; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 14 | } CPUDebug; |
| 15 | |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 16 | /* Filled in by elfload.c. Simplistic, but will do for now. */ |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 17 | struct syminfo *syminfos = NULL; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 18 | |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 19 | /* |
| 20 | * Get LENGTH bytes from info's buffer, at host address memaddr. |
| 21 | * Transfer them to myaddr. |
| 22 | */ |
| 23 | static int host_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length, |
| 24 | struct disassemble_info *info) |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 25 | { |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 26 | if (memaddr < info->buffer_vma |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 27 | || memaddr + length > info->buffer_vma + info->buffer_length) { |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 28 | /* Out of bounds. Use EIO because GDB uses it. */ |
| 29 | return EIO; |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 30 | } |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 31 | memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length); |
| 32 | return 0; |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 35 | /* |
| 36 | * Get LENGTH bytes from info's buffer, at target address memaddr. |
| 37 | * Transfer them to myaddr. |
| 38 | */ |
| 39 | static int target_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length, |
| 40 | struct disassemble_info *info) |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 41 | { |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 42 | CPUDebug *s = container_of(info, CPUDebug, info); |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 43 | int r = cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0); |
Philippe Mathieu-Daudé | 6766ba5 | 2020-05-18 17:53:04 +0200 | [diff] [blame] | 44 | return r ? EIO : 0; |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 45 | } |
bellard | c6105c0 | 2003-10-27 21:13:58 +0000 | [diff] [blame] | 46 | |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Print an error message. We can assume that this is in response to |
| 49 | * an error return from {host,target}_read_memory. |
| 50 | */ |
| 51 | static void perror_memory(int status, bfd_vma memaddr, |
| 52 | struct disassemble_info *info) |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 53 | { |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 54 | if (status != EIO) { |
| 55 | /* Can't happen. */ |
| 56 | info->fprintf_func(info->stream, "Unknown error %d\n", status); |
| 57 | } else { |
| 58 | /* Address between memaddr and memaddr + len was out of bounds. */ |
| 59 | info->fprintf_func(info->stream, |
| 60 | "Address 0x%" PRIx64 " is out of bounds.\n", |
| 61 | memaddr); |
| 62 | } |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 65 | /* Print address in hex. */ |
| 66 | static void print_address(bfd_vma addr, struct disassemble_info *info) |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 67 | { |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 68 | info->fprintf_func(info->stream, "0x%" PRIx64, addr); |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Peter Maydell | 636bd28 | 2012-06-25 04:55:55 +0000 | [diff] [blame] | 71 | /* Print address in hex, truncated to the width of a host virtual address. */ |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 72 | static void host_print_address(bfd_vma addr, struct disassemble_info *info) |
Peter Maydell | 636bd28 | 2012-06-25 04:55:55 +0000 | [diff] [blame] | 73 | { |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 74 | print_address((uintptr_t)addr, info); |
Peter Maydell | 636bd28 | 2012-06-25 04:55:55 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 77 | /* Stub prevents some fruitless earching in optabs disassemblers. */ |
| 78 | static int symbol_at_address(bfd_vma addr, struct disassemble_info *info) |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 79 | { |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 80 | return 1; |
bellard | aa0aa4f | 2003-06-09 15:23:31 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Richard Henderson | c46ffd5 | 2013-08-16 23:29:45 -0700 | [diff] [blame] | 83 | static int print_insn_objdump(bfd_vma pc, disassemble_info *info, |
| 84 | const char *prefix) |
| 85 | { |
| 86 | int i, n = info->buffer_length; |
| 87 | uint8_t *buf = g_malloc(n); |
| 88 | |
| 89 | info->read_memory_func(pc, buf, n, info); |
| 90 | |
| 91 | for (i = 0; i < n; ++i) { |
| 92 | if (i % 32 == 0) { |
| 93 | info->fprintf_func(info->stream, "\n%s: ", prefix); |
| 94 | } |
| 95 | info->fprintf_func(info->stream, "%02x", buf[i]); |
| 96 | } |
| 97 | |
| 98 | g_free(buf); |
| 99 | return n; |
| 100 | } |
| 101 | |
| 102 | static int print_insn_od_host(bfd_vma pc, disassemble_info *info) |
| 103 | { |
| 104 | return print_insn_objdump(pc, info, "OBJD-H"); |
| 105 | } |
| 106 | |
| 107 | static int print_insn_od_target(bfd_vma pc, disassemble_info *info) |
| 108 | { |
| 109 | return print_insn_objdump(pc, info, "OBJD-T"); |
| 110 | } |
| 111 | |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 112 | static void initialize_debug(CPUDebug *s) |
| 113 | { |
| 114 | memset(s, 0, sizeof(*s)); |
| 115 | s->info.arch = bfd_arch_unknown; |
| 116 | s->info.cap_arch = -1; |
| 117 | s->info.cap_insn_unit = 4; |
| 118 | s->info.cap_insn_split = 4; |
| 119 | s->info.memory_error_func = perror_memory; |
| 120 | s->info.symbol_at_address_func = symbol_at_address; |
| 121 | } |
| 122 | |
| 123 | static void initialize_debug_target(CPUDebug *s, CPUState *cpu) |
| 124 | { |
| 125 | initialize_debug(s); |
| 126 | |
| 127 | s->cpu = cpu; |
| 128 | s->info.read_memory_func = target_read_memory; |
| 129 | s->info.print_address_func = print_address; |
| 130 | #ifdef TARGET_WORDS_BIGENDIAN |
| 131 | s->info.endian = BFD_ENDIAN_BIG; |
| 132 | #else |
| 133 | s->info.endian = BFD_ENDIAN_LITTLE; |
| 134 | #endif |
| 135 | |
| 136 | CPUClass *cc = CPU_GET_CLASS(cpu); |
| 137 | if (cc->disas_set_info) { |
| 138 | cc->disas_set_info(cpu, &s->info); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static void initialize_debug_host(CPUDebug *s) |
| 143 | { |
| 144 | initialize_debug(s); |
| 145 | |
| 146 | s->info.read_memory_func = host_read_memory; |
| 147 | s->info.print_address_func = host_print_address; |
| 148 | #ifdef HOST_WORDS_BIGENDIAN |
| 149 | s->info.endian = BFD_ENDIAN_BIG; |
| 150 | #else |
| 151 | s->info.endian = BFD_ENDIAN_LITTLE; |
| 152 | #endif |
| 153 | #if defined(CONFIG_TCG_INTERPRETER) |
| 154 | s->info.print_insn = print_insn_tci; |
| 155 | #elif defined(__i386__) |
| 156 | s->info.mach = bfd_mach_i386_i386; |
| 157 | s->info.print_insn = print_insn_i386; |
| 158 | s->info.cap_arch = CS_ARCH_X86; |
| 159 | s->info.cap_mode = CS_MODE_32; |
| 160 | s->info.cap_insn_unit = 1; |
| 161 | s->info.cap_insn_split = 8; |
| 162 | #elif defined(__x86_64__) |
| 163 | s->info.mach = bfd_mach_x86_64; |
| 164 | s->info.print_insn = print_insn_i386; |
| 165 | s->info.cap_arch = CS_ARCH_X86; |
| 166 | s->info.cap_mode = CS_MODE_64; |
| 167 | s->info.cap_insn_unit = 1; |
| 168 | s->info.cap_insn_split = 8; |
| 169 | #elif defined(_ARCH_PPC) |
| 170 | s->info.disassembler_options = (char *)"any"; |
| 171 | s->info.print_insn = print_insn_ppc; |
| 172 | s->info.cap_arch = CS_ARCH_PPC; |
| 173 | # ifdef _ARCH_PPC64 |
| 174 | s->info.cap_mode = CS_MODE_64; |
| 175 | # endif |
| 176 | #elif defined(__riscv) && defined(CONFIG_RISCV_DIS) |
| 177 | #if defined(_ILP32) || (__riscv_xlen == 32) |
| 178 | s->info.print_insn = print_insn_riscv32; |
| 179 | #elif defined(_LP64) |
| 180 | s->info.print_insn = print_insn_riscv64; |
| 181 | #else |
| 182 | #error unsupported RISC-V ABI |
| 183 | #endif |
Richard Henderson | a4038a0 | 2020-09-11 17:59:01 -0700 | [diff] [blame] | 184 | #elif defined(__aarch64__) |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 185 | s->info.cap_arch = CS_ARCH_ARM64; |
Richard Henderson | a4038a0 | 2020-09-11 17:59:01 -0700 | [diff] [blame] | 186 | # ifdef CONFIG_ARM_A64_DIS |
| 187 | s->info.print_insn = print_insn_arm_a64; |
| 188 | # endif |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 189 | #elif defined(__alpha__) |
| 190 | s->info.print_insn = print_insn_alpha; |
| 191 | #elif defined(__sparc__) |
| 192 | s->info.print_insn = print_insn_sparc; |
| 193 | s->info.mach = bfd_mach_sparc_v9b; |
| 194 | #elif defined(__arm__) |
| 195 | /* TCG only generates code for arm mode. */ |
| 196 | s->info.print_insn = print_insn_arm; |
| 197 | s->info.cap_arch = CS_ARCH_ARM; |
| 198 | #elif defined(__MIPSEB__) |
| 199 | s->info.print_insn = print_insn_big_mips; |
| 200 | #elif defined(__MIPSEL__) |
| 201 | s->info.print_insn = print_insn_little_mips; |
| 202 | #elif defined(__m68k__) |
| 203 | s->info.print_insn = print_insn_m68k; |
| 204 | #elif defined(__s390__) |
| 205 | s->info.print_insn = print_insn_s390; |
Richard Henderson | 3d56284 | 2020-01-04 07:24:59 +1000 | [diff] [blame^] | 206 | s->info.cap_arch = CS_ARCH_SYSZ; |
| 207 | s->info.cap_insn_unit = 2; |
| 208 | s->info.cap_insn_split = 6; |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 209 | #elif defined(__hppa__) |
| 210 | s->info.print_insn = print_insn_hppa; |
| 211 | #endif |
| 212 | } |
| 213 | |
Richard Henderson | 1d48474 | 2017-09-14 08:38:35 -0700 | [diff] [blame] | 214 | /* Disassemble this for me please... (debugging). */ |
Peter Crosthwaite | d49190c | 2015-05-24 14:20:41 -0700 | [diff] [blame] | 215 | void target_disas(FILE *out, CPUState *cpu, target_ulong code, |
Richard Henderson | 1d48474 | 2017-09-14 08:38:35 -0700 | [diff] [blame] | 216 | target_ulong size) |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 217 | { |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 218 | target_ulong pc; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 219 | int count; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 220 | CPUDebug s; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 221 | |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 222 | initialize_debug_target(&s, cpu); |
| 223 | s.info.fprintf_func = fprintf; |
| 224 | s.info.stream = out; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 225 | s.info.buffer_vma = code; |
| 226 | s.info.buffer_length = size; |
Peter Crosthwaite | 37b9de4 | 2015-06-23 20:57:33 -0700 | [diff] [blame] | 227 | |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 228 | if (s.info.cap_arch >= 0 && cap_disas_target(&s.info, code, size)) { |
| 229 | return; |
| 230 | } |
| 231 | |
Peter Crosthwaite | 2de295c | 2015-06-23 20:57:32 -0700 | [diff] [blame] | 232 | if (s.info.print_insn == NULL) { |
| 233 | s.info.print_insn = print_insn_od_target; |
Richard Henderson | c46ffd5 | 2013-08-16 23:29:45 -0700 | [diff] [blame] | 234 | } |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 235 | |
blueswir1 | 7e000c2 | 2009-02-13 21:44:41 +0000 | [diff] [blame] | 236 | for (pc = code; size > 0; pc += count, size -= count) { |
bellard | fa15e03 | 2005-01-31 23:32:31 +0000 | [diff] [blame] | 237 | fprintf(out, "0x" TARGET_FMT_lx ": ", pc); |
Peter Crosthwaite | 2de295c | 2015-06-23 20:57:32 -0700 | [diff] [blame] | 238 | count = s.info.print_insn(pc, &s.info); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 239 | fprintf(out, "\n"); |
| 240 | if (count < 0) |
| 241 | break; |
malc | 754d00a | 2009-04-21 22:26:22 +0000 | [diff] [blame] | 242 | if (size < count) { |
| 243 | fprintf(out, |
| 244 | "Disassembler disagrees with translator over instruction " |
| 245 | "decoding\n" |
| 246 | "Please report this to qemu-devel@nongnu.org\n"); |
| 247 | break; |
| 248 | } |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Alex Bennée | cbafa23 | 2019-05-22 10:27:14 +0100 | [diff] [blame] | 252 | static int plugin_printf(FILE *stream, const char *fmt, ...) |
| 253 | { |
Richard Henderson | b71f3a6 | 2020-09-10 15:13:38 -0700 | [diff] [blame] | 254 | /* We abuse the FILE parameter to pass a GString. */ |
| 255 | GString *s = (GString *)stream; |
Alex Bennée | cbafa23 | 2019-05-22 10:27:14 +0100 | [diff] [blame] | 256 | int initial_len = s->len; |
Richard Henderson | b71f3a6 | 2020-09-10 15:13:38 -0700 | [diff] [blame] | 257 | va_list va; |
Alex Bennée | cbafa23 | 2019-05-22 10:27:14 +0100 | [diff] [blame] | 258 | |
| 259 | va_start(va, fmt); |
| 260 | g_string_append_vprintf(s, fmt, va); |
| 261 | va_end(va); |
| 262 | |
| 263 | return s->len - initial_len; |
| 264 | } |
| 265 | |
| 266 | static void plugin_print_address(bfd_vma addr, struct disassemble_info *info) |
| 267 | { |
| 268 | /* does nothing */ |
| 269 | } |
| 270 | |
| 271 | |
Alex Bennée | cbafa23 | 2019-05-22 10:27:14 +0100 | [diff] [blame] | 272 | /* |
| 273 | * We should only be dissembling one instruction at a time here. If |
| 274 | * there is left over it usually indicates the front end has read more |
| 275 | * bytes than it needed. |
| 276 | */ |
| 277 | char *plugin_disas(CPUState *cpu, uint64_t addr, size_t size) |
| 278 | { |
Alex Bennée | cbafa23 | 2019-05-22 10:27:14 +0100 | [diff] [blame] | 279 | CPUDebug s; |
Richard Henderson | b71f3a6 | 2020-09-10 15:13:38 -0700 | [diff] [blame] | 280 | GString *ds = g_string_new(NULL); |
Alex Bennée | cbafa23 | 2019-05-22 10:27:14 +0100 | [diff] [blame] | 281 | |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 282 | initialize_debug_target(&s, cpu); |
| 283 | s.info.fprintf_func = plugin_printf; |
Richard Henderson | b71f3a6 | 2020-09-10 15:13:38 -0700 | [diff] [blame] | 284 | s.info.stream = (FILE *)ds; /* abuse this slot */ |
Alex Bennée | cbafa23 | 2019-05-22 10:27:14 +0100 | [diff] [blame] | 285 | s.info.buffer_vma = addr; |
| 286 | s.info.buffer_length = size; |
| 287 | s.info.print_address_func = plugin_print_address; |
Alex Bennée | cbafa23 | 2019-05-22 10:27:14 +0100 | [diff] [blame] | 288 | |
| 289 | if (s.info.cap_arch >= 0 && cap_disas_plugin(&s.info, addr, size)) { |
Richard Henderson | b71f3a6 | 2020-09-10 15:13:38 -0700 | [diff] [blame] | 290 | ; /* done */ |
| 291 | } else if (s.info.print_insn) { |
| 292 | s.info.print_insn(addr, &s.info); |
| 293 | } else { |
| 294 | ; /* cannot disassemble -- return empty string */ |
Alex Bennée | cbafa23 | 2019-05-22 10:27:14 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Richard Henderson | b71f3a6 | 2020-09-10 15:13:38 -0700 | [diff] [blame] | 297 | /* Return the buffer, freeing the GString container. */ |
| 298 | return g_string_free(ds, false); |
Alex Bennée | cbafa23 | 2019-05-22 10:27:14 +0100 | [diff] [blame] | 299 | } |
| 300 | |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 301 | /* Disassemble this for me please... (debugging). */ |
Richard Henderson | 4c389f6 | 2020-09-10 12:15:04 -0700 | [diff] [blame] | 302 | void disas(FILE *out, void *code, unsigned long size) |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 303 | { |
Stefan Weil | b0b0f1c | 2012-04-12 15:44:35 +0200 | [diff] [blame] | 304 | uintptr_t pc; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 305 | int count; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 306 | CPUDebug s; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 307 | |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 308 | initialize_debug_host(&s); |
| 309 | s.info.fprintf_func = fprintf; |
| 310 | s.info.stream = out; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 311 | s.info.buffer = code; |
| 312 | s.info.buffer_vma = (uintptr_t)code; |
| 313 | s.info.buffer_length = size; |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 314 | |
Richard Henderson | 4c389f6 | 2020-09-10 12:15:04 -0700 | [diff] [blame] | 315 | if (s.info.cap_arch >= 0 && cap_disas_host(&s.info, code, size)) { |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 319 | if (s.info.print_insn == NULL) { |
| 320 | s.info.print_insn = print_insn_od_host; |
Richard Henderson | c46ffd5 | 2013-08-16 23:29:45 -0700 | [diff] [blame] | 321 | } |
Stefan Weil | b0b0f1c | 2012-04-12 15:44:35 +0200 | [diff] [blame] | 322 | for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) { |
| 323 | fprintf(out, "0x%08" PRIxPTR ": ", pc); |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 324 | count = s.info.print_insn(pc, &s.info); |
Alex Bennée | e5ef4ec | 2020-05-13 18:51:32 +0100 | [diff] [blame] | 325 | fprintf(out, "\n"); |
| 326 | if (count < 0) { |
| 327 | break; |
| 328 | } |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 329 | } |
Alex Bennée | e5ef4ec | 2020-05-13 18:51:32 +0100 | [diff] [blame] | 330 | |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* Look up symbol for debugging purpose. Returns "" if unknown. */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 334 | const char *lookup_symbol(target_ulong orig_addr) |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 335 | { |
pbrook | 49918a7 | 2008-10-22 15:11:31 +0000 | [diff] [blame] | 336 | const char *symbol = ""; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 337 | struct syminfo *s; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 338 | |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 339 | for (s = syminfos; s; s = s->next) { |
pbrook | 49918a7 | 2008-10-22 15:11:31 +0000 | [diff] [blame] | 340 | symbol = s->lookup_symbol(s, orig_addr); |
| 341 | if (symbol[0] != '\0') { |
| 342 | break; |
| 343 | } |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 344 | } |
pbrook | 49918a7 | 2008-10-22 15:11:31 +0000 | [diff] [blame] | 345 | |
| 346 | return symbol; |
bellard | b9adb4a | 2003-04-29 20:41:16 +0000 | [diff] [blame] | 347 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 348 | |
| 349 | #if !defined(CONFIG_USER_ONLY) |
| 350 | |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 351 | #include "monitor/monitor.h" |
bellard | 3d2cfdf | 2004-08-01 21:49:07 +0000 | [diff] [blame] | 352 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 353 | static int |
Richard Henderson | b8d8720 | 2017-09-19 09:40:40 -0500 | [diff] [blame] | 354 | physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length, |
blueswir1 | a5f1b96 | 2008-08-17 20:21:51 +0000 | [diff] [blame] | 355 | struct disassemble_info *info) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 356 | { |
Peter Maydell | f2c6abc | 2018-12-14 13:30:49 +0000 | [diff] [blame] | 357 | CPUDebug *s = container_of(info, CPUDebug, info); |
Philippe Mathieu-Daudé | 6766ba5 | 2020-05-18 17:53:04 +0200 | [diff] [blame] | 358 | MemTxResult res; |
Peter Maydell | f2c6abc | 2018-12-14 13:30:49 +0000 | [diff] [blame] | 359 | |
Philippe Mathieu-Daudé | 6766ba5 | 2020-05-18 17:53:04 +0200 | [diff] [blame] | 360 | res = address_space_read(s->cpu->as, memaddr, MEMTXATTRS_UNSPECIFIED, |
| 361 | myaddr, length); |
| 362 | return res == MEMTX_OK ? 0 : EIO; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Richard Henderson | 1d48474 | 2017-09-14 08:38:35 -0700 | [diff] [blame] | 365 | /* Disassembler for the monitor. */ |
Peter Crosthwaite | d49190c | 2015-05-24 14:20:41 -0700 | [diff] [blame] | 366 | void monitor_disas(Monitor *mon, CPUState *cpu, |
Richard Henderson | 1d48474 | 2017-09-14 08:38:35 -0700 | [diff] [blame] | 367 | target_ulong pc, int nb_insn, int is_physical) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 368 | { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 369 | int count, i; |
Blue Swirl | f4359b9 | 2012-09-08 12:40:00 +0000 | [diff] [blame] | 370 | CPUDebug s; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 371 | |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 372 | initialize_debug_target(&s, cpu); |
| 373 | s.info.fprintf_func = qemu_fprintf; |
| 374 | if (is_physical) { |
| 375 | s.info.read_memory_func = physical_read_memory; |
Peter Crosthwaite | 37b9de4 | 2015-06-23 20:57:33 -0700 | [diff] [blame] | 376 | } |
Richard Henderson | 12b6e9b | 2020-09-10 14:40:54 -0700 | [diff] [blame] | 377 | s.info.buffer_vma = pc; |
Peter Crosthwaite | 37b9de4 | 2015-06-23 20:57:33 -0700 | [diff] [blame] | 378 | |
Richard Henderson | 8ca8076 | 2017-09-14 09:41:12 -0700 | [diff] [blame] | 379 | if (s.info.cap_arch >= 0 && cap_disas_monitor(&s.info, pc, nb_insn)) { |
| 380 | return; |
| 381 | } |
| 382 | |
Peter Crosthwaite | 37b9de4 | 2015-06-23 20:57:33 -0700 | [diff] [blame] | 383 | if (!s.info.print_insn) { |
| 384 | monitor_printf(mon, "0x" TARGET_FMT_lx |
| 385 | ": Asm output not supported on this arch\n", pc); |
| 386 | return; |
| 387 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 388 | |
| 389 | for(i = 0; i < nb_insn; i++) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 390 | monitor_printf(mon, "0x" TARGET_FMT_lx ": ", pc); |
Peter Crosthwaite | 2de295c | 2015-06-23 20:57:32 -0700 | [diff] [blame] | 391 | count = s.info.print_insn(pc, &s.info); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 392 | monitor_printf(mon, "\n"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 393 | if (count < 0) |
| 394 | break; |
| 395 | pc += count; |
| 396 | } |
| 397 | } |
| 398 | #endif |