Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Disassembler.cpp ----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Core/Disassembler.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/lldb-private.h" |
| 17 | #include "lldb/Core/Error.h" |
| 18 | #include "lldb/Core/DataBufferHeap.h" |
| 19 | #include "lldb/Core/DataExtractor.h" |
| 20 | #include "lldb/Core/Debugger.h" |
Caroline Tice | ad379efc | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 21 | #include "lldb/Core/EmulateInstruction.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Core/Module.h" |
| 23 | #include "lldb/Core/PluginManager.h" |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 24 | #include "lldb/Core/RegularExpression.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | #include "lldb/Core/Timer.h" |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 26 | #include "lldb/Interpreter/NamedOptionValue.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "lldb/Symbol/ObjectFile.h" |
| 28 | #include "lldb/Target/ExecutionContext.h" |
| 29 | #include "lldb/Target/Process.h" |
| 30 | #include "lldb/Target/StackFrame.h" |
| 31 | #include "lldb/Target/Target.h" |
| 32 | |
| 33 | #define DEFAULT_DISASM_BYTE_SIZE 32 |
| 34 | |
| 35 | using namespace lldb; |
| 36 | using namespace lldb_private; |
| 37 | |
| 38 | |
| 39 | Disassembler* |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 40 | Disassembler::FindPlugin (const ArchSpec &arch, const char *plugin_name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | { |
| 42 | Timer scoped_timer (__PRETTY_FUNCTION__, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 43 | "Disassembler::FindPlugin (arch = %s, plugin_name = %s)", |
| 44 | arch.GetArchitectureName(), |
| 45 | plugin_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | |
| 47 | std::auto_ptr<Disassembler> disassembler_ap; |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 48 | DisassemblerCreateInstance create_callback = NULL; |
| 49 | |
| 50 | if (plugin_name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | { |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 52 | create_callback = PluginManager::GetDisassemblerCreateCallbackForPluginName (plugin_name); |
| 53 | if (create_callback) |
| 54 | { |
| 55 | disassembler_ap.reset (create_callback(arch)); |
| 56 | |
| 57 | if (disassembler_ap.get()) |
| 58 | return disassembler_ap.release(); |
| 59 | } |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | for (uint32_t idx = 0; (create_callback = PluginManager::GetDisassemblerCreateCallbackAtIndex(idx)) != NULL; ++idx) |
| 64 | { |
| 65 | disassembler_ap.reset (create_callback(arch)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 66 | |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 67 | if (disassembler_ap.get()) |
| 68 | return disassembler_ap.release(); |
| 69 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | } |
| 71 | return NULL; |
| 72 | } |
| 73 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 74 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 75 | static void |
| 76 | ResolveAddress (const ExecutionContext &exe_ctx, |
| 77 | const Address &addr, |
| 78 | Address &resolved_addr) |
| 79 | { |
| 80 | if (!addr.IsSectionOffset()) |
| 81 | { |
| 82 | // If we weren't passed in a section offset address range, |
| 83 | // try and resolve it to something |
| 84 | if (exe_ctx.target) |
| 85 | { |
| 86 | if (exe_ctx.target->GetSectionLoadList().IsEmpty()) |
| 87 | { |
| 88 | exe_ctx.target->GetImages().ResolveFileAddress (addr.GetOffset(), resolved_addr); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | exe_ctx.target->GetSectionLoadList().ResolveLoadAddress (addr.GetOffset(), resolved_addr); |
| 93 | } |
| 94 | // We weren't able to resolve the address, just treat it as a |
| 95 | // raw address |
| 96 | if (resolved_addr.IsValid()) |
| 97 | return; |
| 98 | } |
| 99 | } |
| 100 | resolved_addr = addr; |
| 101 | } |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 102 | |
| 103 | size_t |
| 104 | Disassembler::Disassemble |
| 105 | ( |
| 106 | Debugger &debugger, |
| 107 | const ArchSpec &arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 108 | const char *plugin_name, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 109 | const ExecutionContext &exe_ctx, |
| 110 | SymbolContextList &sc_list, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 111 | uint32_t num_instructions, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 112 | uint32_t num_mixed_context_lines, |
| 113 | bool show_bytes, |
Sean Callanan | b3396b2 | 2011-03-10 23:35:12 +0000 | [diff] [blame] | 114 | bool raw, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 115 | Stream &strm |
| 116 | ) |
| 117 | { |
| 118 | size_t success_count = 0; |
| 119 | const size_t count = sc_list.GetSize(); |
| 120 | SymbolContext sc; |
| 121 | AddressRange range; |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 122 | const uint32_t scope = eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol; |
| 123 | const bool use_inline_block_range = true; |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 124 | for (size_t i=0; i<count; ++i) |
| 125 | { |
| 126 | if (sc_list.GetContextAtIndex(i, sc) == false) |
| 127 | break; |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 128 | for (uint32_t range_idx = 0; sc.GetAddressRange(scope, range_idx, use_inline_block_range, range); ++range_idx) |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 129 | { |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 130 | if (Disassemble (debugger, |
| 131 | arch, |
| 132 | plugin_name, |
| 133 | exe_ctx, |
| 134 | range, |
| 135 | num_instructions, |
| 136 | num_mixed_context_lines, |
| 137 | show_bytes, |
| 138 | raw, |
| 139 | strm)) |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 140 | { |
| 141 | ++success_count; |
| 142 | strm.EOL(); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | return success_count; |
| 147 | } |
| 148 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | bool |
| 150 | Disassembler::Disassemble |
| 151 | ( |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 152 | Debugger &debugger, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | const ArchSpec &arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 154 | const char *plugin_name, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | const ExecutionContext &exe_ctx, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 156 | const ConstString &name, |
| 157 | Module *module, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 158 | uint32_t num_instructions, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 159 | uint32_t num_mixed_context_lines, |
| 160 | bool show_bytes, |
Sean Callanan | b3396b2 | 2011-03-10 23:35:12 +0000 | [diff] [blame] | 161 | bool raw, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | Stream &strm |
| 163 | ) |
| 164 | { |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 165 | SymbolContextList sc_list; |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 166 | if (name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 167 | { |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 168 | const bool include_symbols = true; |
| 169 | if (module) |
| 170 | { |
| 171 | module->FindFunctions (name, |
| 172 | eFunctionNameTypeBase | |
| 173 | eFunctionNameTypeFull | |
| 174 | eFunctionNameTypeMethod | |
| 175 | eFunctionNameTypeSelector, |
| 176 | include_symbols, |
| 177 | true, |
| 178 | sc_list); |
| 179 | } |
| 180 | else if (exe_ctx.target) |
| 181 | { |
| 182 | exe_ctx.target->GetImages().FindFunctions (name, |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 183 | eFunctionNameTypeBase | |
| 184 | eFunctionNameTypeFull | |
| 185 | eFunctionNameTypeMethod | |
| 186 | eFunctionNameTypeSelector, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 187 | include_symbols, |
Sean Callanan | 8ade104 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 188 | false, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 189 | sc_list); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 190 | } |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | if (sc_list.GetSize ()) |
| 194 | { |
| 195 | return Disassemble (debugger, |
| 196 | arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 197 | plugin_name, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 198 | exe_ctx, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 199 | sc_list, |
| 200 | num_instructions, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 201 | num_mixed_context_lines, |
Sean Callanan | b3396b2 | 2011-03-10 23:35:12 +0000 | [diff] [blame] | 202 | show_bytes, |
| 203 | raw, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 204 | strm); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 209 | |
| 210 | lldb::DisassemblerSP |
| 211 | Disassembler::DisassembleRange |
| 212 | ( |
| 213 | const ArchSpec &arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 214 | const char *plugin_name, |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 215 | const ExecutionContext &exe_ctx, |
| 216 | const AddressRange &range |
| 217 | ) |
| 218 | { |
| 219 | lldb::DisassemblerSP disasm_sp; |
| 220 | if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid()) |
| 221 | { |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 222 | disasm_sp.reset (Disassembler::FindPlugin(arch, plugin_name)); |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 223 | |
| 224 | if (disasm_sp) |
| 225 | { |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 226 | size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range); |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 227 | if (bytes_disassembled == 0) |
| 228 | disasm_sp.reset(); |
| 229 | } |
| 230 | } |
| 231 | return disasm_sp; |
| 232 | } |
| 233 | |
| 234 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 235 | bool |
| 236 | Disassembler::Disassemble |
| 237 | ( |
| 238 | Debugger &debugger, |
| 239 | const ArchSpec &arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 240 | const char *plugin_name, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 241 | const ExecutionContext &exe_ctx, |
| 242 | const AddressRange &disasm_range, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 243 | uint32_t num_instructions, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 244 | uint32_t num_mixed_context_lines, |
| 245 | bool show_bytes, |
Sean Callanan | b3396b2 | 2011-03-10 23:35:12 +0000 | [diff] [blame] | 246 | bool raw, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 247 | Stream &strm |
| 248 | ) |
| 249 | { |
| 250 | if (disasm_range.GetByteSize()) |
| 251 | { |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 252 | std::auto_ptr<Disassembler> disasm_ap (Disassembler::FindPlugin(arch, plugin_name)); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 253 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 254 | if (disasm_ap.get()) |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 255 | { |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 256 | AddressRange range; |
| 257 | ResolveAddress (exe_ctx, disasm_range.GetBaseAddress(), range.GetBaseAddress()); |
| 258 | range.SetByteSize (disasm_range.GetByteSize()); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 259 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 260 | size_t bytes_disassembled = disasm_ap->ParseInstructions (&exe_ctx, range); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 261 | if (bytes_disassembled == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 262 | return false; |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 263 | |
| 264 | return PrintInstructions (disasm_ap.get(), |
| 265 | debugger, |
| 266 | arch, |
| 267 | exe_ctx, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 268 | num_instructions, |
| 269 | num_mixed_context_lines, |
| 270 | show_bytes, |
| 271 | raw, |
| 272 | strm); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | bool |
| 279 | Disassembler::Disassemble |
| 280 | ( |
| 281 | Debugger &debugger, |
| 282 | const ArchSpec &arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 283 | const char *plugin_name, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 284 | const ExecutionContext &exe_ctx, |
| 285 | const Address &start_address, |
| 286 | uint32_t num_instructions, |
| 287 | uint32_t num_mixed_context_lines, |
| 288 | bool show_bytes, |
| 289 | bool raw, |
| 290 | Stream &strm |
| 291 | ) |
| 292 | { |
| 293 | if (num_instructions > 0) |
| 294 | { |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 295 | std::auto_ptr<Disassembler> disasm_ap (Disassembler::FindPlugin(arch, plugin_name)); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 296 | if (disasm_ap.get()) |
| 297 | { |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 298 | Address addr; |
| 299 | ResolveAddress (exe_ctx, start_address, addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 300 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 301 | size_t bytes_disassembled = disasm_ap->ParseInstructions (&exe_ctx, addr, num_instructions); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 302 | if (bytes_disassembled == 0) |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 303 | return false; |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 304 | return PrintInstructions (disasm_ap.get(), |
| 305 | debugger, |
| 306 | arch, |
| 307 | exe_ctx, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 308 | num_instructions, |
| 309 | num_mixed_context_lines, |
| 310 | show_bytes, |
| 311 | raw, |
| 312 | strm); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 314 | } |
| 315 | return false; |
| 316 | } |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 317 | |
| 318 | bool |
| 319 | Disassembler::PrintInstructions |
| 320 | ( |
| 321 | Disassembler *disasm_ptr, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 322 | Debugger &debugger, |
| 323 | const ArchSpec &arch, |
| 324 | const ExecutionContext &exe_ctx, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 325 | uint32_t num_instructions, |
| 326 | uint32_t num_mixed_context_lines, |
| 327 | bool show_bytes, |
| 328 | bool raw, |
| 329 | Stream &strm |
| 330 | ) |
| 331 | { |
| 332 | // We got some things disassembled... |
| 333 | size_t num_instructions_found = disasm_ptr->GetInstructionList().GetSize(); |
| 334 | |
| 335 | if (num_instructions > 0 && num_instructions < num_instructions_found) |
| 336 | num_instructions_found = num_instructions; |
| 337 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 338 | const uint32_t max_opcode_byte_size = disasm_ptr->GetInstructionList().GetMaxOpcocdeByteSize (); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 339 | uint32_t offset = 0; |
| 340 | SymbolContext sc; |
| 341 | SymbolContext prev_sc; |
| 342 | AddressRange sc_range; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 343 | Address *pc_addr_ptr = NULL; |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 344 | ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope(); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 345 | if (exe_ctx.frame) |
| 346 | pc_addr_ptr = &exe_ctx.frame->GetFrameCodeAddress(); |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 347 | const uint32_t scope = eSymbolContextLineEntry | eSymbolContextFunction | eSymbolContextSymbol; |
| 348 | const bool use_inline_block_range = false; |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 349 | |
| 350 | for (size_t i=0; i<num_instructions_found; ++i) |
| 351 | { |
| 352 | Instruction *inst = disasm_ptr->GetInstructionList().GetInstructionAtIndex (i).get(); |
| 353 | if (inst) |
| 354 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 355 | const Address &addr = inst->GetAddress(); |
| 356 | const bool inst_is_at_pc = pc_addr_ptr && addr == *pc_addr_ptr; |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 357 | |
| 358 | prev_sc = sc; |
| 359 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 360 | Module *module = addr.GetModule(); |
| 361 | if (module) |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 362 | { |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 363 | uint32_t resolved_mask = module->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, sc); |
| 364 | if (resolved_mask) |
| 365 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 366 | if (num_mixed_context_lines) |
| 367 | { |
| 368 | if (!sc_range.ContainsFileAddress (addr)) |
| 369 | { |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 370 | sc.GetAddressRange (scope, 0, use_inline_block_range, sc_range); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 371 | |
| 372 | if (sc != prev_sc) |
| 373 | { |
| 374 | if (offset != 0) |
| 375 | strm.EOL(); |
| 376 | |
| 377 | sc.DumpStopContext(&strm, exe_ctx.process, addr, false, true, false); |
| 378 | strm.EOL(); |
| 379 | |
| 380 | if (sc.comp_unit && sc.line_entry.IsValid()) |
| 381 | { |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 382 | debugger.GetSourceManager().DisplaySourceLinesWithLineNumbers (debugger.GetTargetList().GetSelectedTarget().get(), |
| 383 | sc.line_entry.file, |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 384 | sc.line_entry.line, |
| 385 | num_mixed_context_lines, |
| 386 | num_mixed_context_lines, |
| 387 | num_mixed_context_lines ? "->" : "", |
| 388 | &strm); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | else if (!(prev_sc.function == sc.function || prev_sc.symbol == sc.symbol)) |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 394 | { |
| 395 | if (prev_sc.function || prev_sc.symbol) |
| 396 | strm.EOL(); |
| 397 | |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 398 | bool show_fullpaths = false; |
| 399 | bool show_module = true; |
| 400 | bool show_inlined_frames = true; |
| 401 | sc.DumpStopContext (&strm, |
| 402 | exe_scope, |
| 403 | addr, |
| 404 | show_fullpaths, |
| 405 | show_module, |
| 406 | show_inlined_frames); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 407 | |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 408 | strm << ":\n"; |
| 409 | } |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 410 | } |
| 411 | else |
| 412 | { |
| 413 | sc.Clear(); |
| 414 | } |
| 415 | } |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 416 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 417 | if (pc_addr_ptr) |
| 418 | { |
| 419 | if (inst_is_at_pc) |
| 420 | strm.PutCString("-> "); |
| 421 | else |
| 422 | strm.PutCString(" "); |
| 423 | } |
| 424 | inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx, raw); |
| 425 | strm.EOL(); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 426 | } |
| 427 | else |
| 428 | { |
| 429 | break; |
| 430 | } |
| 431 | } |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 432 | |
| 433 | return true; |
| 434 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 435 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 436 | |
| 437 | bool |
| 438 | Disassembler::Disassemble |
| 439 | ( |
| 440 | Debugger &debugger, |
| 441 | const ArchSpec &arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 442 | const char *plugin_name, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 443 | const ExecutionContext &exe_ctx, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 444 | uint32_t num_instructions, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 445 | uint32_t num_mixed_context_lines, |
| 446 | bool show_bytes, |
Sean Callanan | b3396b2 | 2011-03-10 23:35:12 +0000 | [diff] [blame] | 447 | bool raw, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 448 | Stream &strm |
| 449 | ) |
| 450 | { |
| 451 | AddressRange range; |
| 452 | if (exe_ctx.frame) |
| 453 | { |
| 454 | SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol)); |
| 455 | if (sc.function) |
| 456 | { |
| 457 | range = sc.function->GetAddressRange(); |
| 458 | } |
| 459 | else if (sc.symbol && sc.symbol->GetAddressRangePtr()) |
| 460 | { |
| 461 | range = *sc.symbol->GetAddressRangePtr(); |
| 462 | } |
| 463 | else |
| 464 | { |
Greg Clayton | 9da7bd0 | 2010-08-24 21:05:24 +0000 | [diff] [blame] | 465 | range.GetBaseAddress() = exe_ctx.frame->GetFrameCodeAddress(); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | if (range.GetBaseAddress().IsValid() && range.GetByteSize() == 0) |
| 469 | range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE); |
| 470 | } |
| 471 | |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 472 | return Disassemble (debugger, |
| 473 | arch, |
| 474 | plugin_name, |
| 475 | exe_ctx, |
| 476 | range, |
| 477 | num_instructions, |
| 478 | num_mixed_context_lines, |
| 479 | show_bytes, |
| 480 | raw, |
| 481 | strm); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 484 | Instruction::Instruction(const Address &address, AddressClass addr_class) : |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 485 | m_address (address), |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 486 | m_address_class (addr_class), |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 487 | m_opcode() |
Greg Clayton | 0ae9627 | 2011-03-24 23:53:38 +0000 | [diff] [blame] | 488 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 491 | Instruction::~Instruction() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 492 | { |
| 493 | } |
| 494 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 495 | AddressClass |
| 496 | Instruction::GetAddressClass () |
| 497 | { |
| 498 | if (m_address_class == eAddressClassInvalid) |
| 499 | m_address_class = m_address.GetAddressClass(); |
| 500 | return m_address_class; |
| 501 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 502 | |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 503 | bool |
| 504 | Instruction::DumpEmulation (const ArchSpec &arch) |
| 505 | { |
| 506 | std::auto_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, NULL)); |
| 507 | if (insn_emulator_ap.get()) |
| 508 | { |
| 509 | insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress()); |
| 510 | return insn_emulator_ap->EvaluateInstruction (); |
| 511 | } |
| 512 | |
| 513 | return false; |
| 514 | } |
| 515 | |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 516 | OptionValueSP |
| 517 | Instruction::ReadArray (FILE *in_file, Stream *out_stream, OptionValue::Type data_type) |
| 518 | { |
| 519 | bool done = false; |
| 520 | char buffer[1024]; |
| 521 | |
| 522 | OptionValueSP option_value_sp (new OptionValueArray (1u << data_type)); |
| 523 | |
| 524 | int idx = 0; |
| 525 | while (!done) |
| 526 | { |
| 527 | if (!fgets (buffer, 1023, in_file)) |
| 528 | { |
| 529 | out_stream->Printf ("Instruction::ReadArray: Erroe reading file (fgets).\n"); |
| 530 | option_value_sp.reset (); |
| 531 | return option_value_sp; |
| 532 | } |
| 533 | |
| 534 | std::string line (buffer); |
| 535 | |
| 536 | int len = line.size(); |
| 537 | if (line[len-1] == '\n') |
| 538 | { |
| 539 | line[len-1] = '\0'; |
| 540 | line.resize (len-1); |
| 541 | } |
| 542 | |
| 543 | if ((line.size() == 1) && line[0] == ']') |
| 544 | { |
| 545 | done = true; |
| 546 | line.clear(); |
| 547 | } |
| 548 | |
| 549 | if (line.size() > 0) |
| 550 | { |
| 551 | std::string value; |
| 552 | RegularExpression reg_exp ("^[ \t]*([^ \t]+)[ \t]*$"); |
| 553 | bool reg_exp_success = reg_exp.Execute (line.c_str(), 1); |
| 554 | if (reg_exp_success) |
| 555 | reg_exp.GetMatchAtIndex (line.c_str(), 1, value); |
| 556 | else |
| 557 | value = line; |
| 558 | |
| 559 | OptionValueSP data_value_sp; |
| 560 | switch (data_type) |
| 561 | { |
| 562 | case OptionValue::eTypeUInt64: |
| 563 | data_value_sp.reset (new OptionValueUInt64 (0, 0)); |
| 564 | data_value_sp->SetValueFromCString (value.c_str()); |
| 565 | break; |
| 566 | // Other types can be added later as needed. |
| 567 | default: |
| 568 | data_value_sp.reset (new OptionValueString (value.c_str(), "")); |
| 569 | break; |
| 570 | } |
| 571 | |
| 572 | option_value_sp->GetAsArrayValue()->InsertValue (idx, data_value_sp); |
| 573 | ++idx; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | return option_value_sp; |
| 578 | } |
| 579 | |
| 580 | OptionValueSP |
| 581 | Instruction::ReadDictionary (FILE *in_file, Stream *out_stream) |
| 582 | { |
| 583 | bool done = false; |
| 584 | char buffer[1024]; |
| 585 | |
| 586 | OptionValueSP option_value_sp (new OptionValueDictionary()); |
| 587 | static ConstString encoding_key ("data_encoding"); |
| 588 | OptionValue::Type data_type = OptionValue::eTypeInvalid; |
| 589 | |
| 590 | |
| 591 | while (!done) |
| 592 | { |
| 593 | // Read the next line in the file |
| 594 | if (!fgets (buffer, 1023, in_file)) |
| 595 | { |
| 596 | out_stream->Printf ("Instruction::ReadDictionary: Error reading file (fgets).\n"); |
| 597 | option_value_sp.reset (); |
| 598 | return option_value_sp; |
| 599 | } |
| 600 | |
| 601 | // Check to see if the line contains the end-of-dictionary marker ("}") |
| 602 | std::string line (buffer); |
| 603 | |
| 604 | int len = line.size(); |
| 605 | if (line[len-1] == '\n') |
| 606 | { |
| 607 | line[len-1] = '\0'; |
| 608 | line.resize (len-1); |
| 609 | } |
| 610 | |
| 611 | if ((line.size() == 1) && (line[0] == '}')) |
| 612 | { |
| 613 | done = true; |
| 614 | line.clear(); |
| 615 | } |
| 616 | |
| 617 | // Try to find a key-value pair in the current line and add it to the dictionary. |
| 618 | if (line.size() > 0) |
| 619 | { |
| 620 | RegularExpression reg_exp ("^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*=[ \t]*(.*)[ \t]*$"); |
| 621 | bool reg_exp_success = reg_exp.Execute (line.c_str(), 2); |
| 622 | std::string key; |
| 623 | std::string value; |
| 624 | if (reg_exp_success) |
| 625 | { |
| 626 | reg_exp.GetMatchAtIndex (line.c_str(), 1, key); |
| 627 | reg_exp.GetMatchAtIndex (line.c_str(), 2, value); |
| 628 | } |
| 629 | else |
| 630 | { |
| 631 | out_stream->Printf ("Instruction::ReadDictionary: Failure executing regular expression.\n"); |
| 632 | option_value_sp.reset(); |
| 633 | return option_value_sp; |
| 634 | } |
| 635 | |
| 636 | ConstString const_key (key.c_str()); |
| 637 | // Check value to see if it's the start of an array or dictionary. |
| 638 | |
| 639 | lldb::OptionValueSP value_sp; |
| 640 | assert (value.empty() == false); |
| 641 | assert (key.empty() == false); |
| 642 | |
| 643 | if (value[0] == '{') |
| 644 | { |
| 645 | assert (value.size() == 1); |
| 646 | // value is a dictionary |
| 647 | value_sp = ReadDictionary (in_file, out_stream); |
| 648 | if (value_sp.get() == NULL) |
| 649 | { |
| 650 | option_value_sp.reset (); |
| 651 | return option_value_sp; |
| 652 | } |
| 653 | } |
| 654 | else if (value[0] == '[') |
| 655 | { |
| 656 | assert (value.size() == 1); |
| 657 | // value is an array |
| 658 | value_sp = ReadArray (in_file, out_stream, data_type); |
| 659 | if (value_sp.get() == NULL) |
| 660 | { |
| 661 | option_value_sp.reset (); |
| 662 | return option_value_sp; |
| 663 | } |
| 664 | // We've used the data_type to read an array; re-set the type to Invalid |
| 665 | data_type = OptionValue::eTypeInvalid; |
| 666 | } |
| 667 | else if ((value[0] == '0') && (value[1] == 'x')) |
| 668 | { |
| 669 | value_sp.reset (new OptionValueUInt64 (0, 0)); |
| 670 | value_sp->SetValueFromCString (value.c_str()); |
| 671 | } |
| 672 | else |
| 673 | { |
| 674 | int len = value.size(); |
| 675 | if ((value[0] == '"') && (value[len-1] == '"')) |
| 676 | value = value.substr (1, len-2); |
| 677 | value_sp.reset (new OptionValueString (value.c_str(), "")); |
| 678 | } |
| 679 | |
| 680 | |
| 681 | |
| 682 | if (const_key == encoding_key) |
| 683 | { |
| 684 | // A 'data_encoding=..." is NOT a normal key-value pair; it is meta-data indicating the |
| 685 | // data type of an upcoming array (usually the next bit of data to be read in). |
| 686 | if (strcmp (value.c_str(), "uint32_t") == 0) |
| 687 | data_type = OptionValue::eTypeUInt64; |
| 688 | } |
| 689 | else |
| 690 | option_value_sp->GetAsDictionaryValue()->SetValueForKey (const_key, value_sp, false); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | return option_value_sp; |
| 695 | } |
| 696 | |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 697 | bool |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 698 | Instruction::TestEmulation (Stream *out_stream, const char *file_name) |
| 699 | { |
| 700 | if (!out_stream) |
| 701 | return false; |
| 702 | |
| 703 | if (!file_name) |
| 704 | { |
Johnny Chen | ea80ba8 | 2011-04-21 20:27:45 +0000 | [diff] [blame] | 705 | out_stream->Printf ("Instruction::TestEmulation: Missing file_name."); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 706 | return false; |
| 707 | } |
| 708 | |
| 709 | FILE *test_file = fopen (file_name, "r"); |
| 710 | if (!test_file) |
| 711 | { |
Johnny Chen | ea80ba8 | 2011-04-21 20:27:45 +0000 | [diff] [blame] | 712 | out_stream->Printf ("Instruction::TestEmulation: Attempt to open test file failed."); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 713 | return false; |
| 714 | } |
| 715 | |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 716 | char buffer[256]; |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 717 | if (!fgets (buffer, 255, test_file)) |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 718 | { |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 719 | out_stream->Printf ("Instruction::TestEmulation: Error reading first line of test file.\n"); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 720 | fclose (test_file); |
| 721 | return false; |
| 722 | } |
| 723 | |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 724 | if (strncmp (buffer, "InstructionEmulationState={", 27) != 0) |
| 725 | { |
| 726 | out_stream->Printf ("Instructin::TestEmulation: Test file does not contain emulation state dictionary\n"); |
| 727 | fclose (test_file); |
| 728 | return false; |
| 729 | } |
| 730 | |
| 731 | // Read all the test information from the test file into an OptionValueDictionary. |
| 732 | |
| 733 | OptionValueSP data_dictionary_sp (ReadDictionary (test_file, out_stream)); |
| 734 | if (data_dictionary_sp.get() == NULL) |
| 735 | { |
| 736 | out_stream->Printf ("Instruction::TestEmulation: Error reading Dictionary Object.\n"); |
| 737 | fclose (test_file); |
| 738 | return false; |
| 739 | } |
| 740 | |
| 741 | fclose (test_file); |
| 742 | |
| 743 | OptionValueDictionary *data_dictionary = data_dictionary_sp->GetAsDictionaryValue(); |
| 744 | static ConstString description_key ("assembly_string"); |
| 745 | static ConstString triple_key ("triple"); |
| 746 | |
| 747 | OptionValueSP value_sp = data_dictionary->GetValueForKey (description_key); |
| 748 | |
| 749 | if (value_sp.get() == NULL) |
| 750 | { |
| 751 | out_stream->Printf ("Instruction::TestEmulation: Test file does not contain description string.\n"); |
| 752 | return false; |
| 753 | } |
| 754 | |
| 755 | SetDescription (value_sp->GetStringValue()); |
| 756 | |
| 757 | |
| 758 | value_sp = data_dictionary->GetValueForKey (triple_key); |
| 759 | if (value_sp.get() == NULL) |
| 760 | { |
| 761 | out_stream->Printf ("Instruction::TestEmulation: Test file does not contain triple.\n"); |
| 762 | return false; |
| 763 | } |
| 764 | |
| 765 | ArchSpec arch; |
| 766 | arch.SetTriple (llvm::Triple (value_sp->GetStringValue())); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 767 | |
| 768 | bool success = false; |
| 769 | std::auto_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, NULL)); |
| 770 | if (insn_emulator_ap.get()) |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 771 | success = insn_emulator_ap->TestEmulation (out_stream, arch, data_dictionary); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 772 | |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 773 | if (success) |
Johnny Chen | ea80ba8 | 2011-04-21 20:27:45 +0000 | [diff] [blame] | 774 | out_stream->Printf ("Emulation test succeeded."); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 775 | else |
Johnny Chen | ea80ba8 | 2011-04-21 20:27:45 +0000 | [diff] [blame] | 776 | out_stream->Printf ("Emulation test failed."); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 777 | |
| 778 | return success; |
| 779 | } |
| 780 | |
| 781 | bool |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 782 | Instruction::Emulate (const ArchSpec &arch, |
Caroline Tice | 25d61ac | 2011-04-08 23:33:06 +0000 | [diff] [blame] | 783 | bool auto_advance_pc, |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 784 | void *baton, |
| 785 | EmulateInstruction::ReadMemory read_mem_callback, |
| 786 | EmulateInstruction::WriteMemory write_mem_callback, |
| 787 | EmulateInstruction::ReadRegister read_reg_callback, |
| 788 | EmulateInstruction::WriteRegister write_reg_callback) |
| 789 | { |
| 790 | std::auto_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, NULL)); |
| 791 | if (insn_emulator_ap.get()) |
| 792 | { |
| 793 | insn_emulator_ap->SetBaton (baton); |
| 794 | insn_emulator_ap->SetCallbacks (read_mem_callback, write_mem_callback, read_reg_callback, write_reg_callback); |
| 795 | insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress()); |
Caroline Tice | 25d61ac | 2011-04-08 23:33:06 +0000 | [diff] [blame] | 796 | insn_emulator_ap->SetAdvancePC (auto_advance_pc); |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 797 | return insn_emulator_ap->EvaluateInstruction (); |
| 798 | } |
| 799 | |
| 800 | return false; |
| 801 | } |
| 802 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 803 | InstructionList::InstructionList() : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 804 | m_instructions() |
| 805 | { |
| 806 | } |
| 807 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 808 | InstructionList::~InstructionList() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 809 | { |
| 810 | } |
| 811 | |
| 812 | size_t |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 813 | InstructionList::GetSize() const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 814 | { |
| 815 | return m_instructions.size(); |
| 816 | } |
| 817 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 818 | uint32_t |
| 819 | InstructionList::GetMaxOpcocdeByteSize () const |
| 820 | { |
| 821 | uint32_t max_inst_size = 0; |
| 822 | collection::const_iterator pos, end; |
| 823 | for (pos = m_instructions.begin(), end = m_instructions.end(); |
| 824 | pos != end; |
| 825 | ++pos) |
| 826 | { |
| 827 | uint32_t inst_size = (*pos)->GetOpcode().GetByteSize(); |
| 828 | if (max_inst_size < inst_size) |
| 829 | max_inst_size = inst_size; |
| 830 | } |
| 831 | return max_inst_size; |
| 832 | } |
| 833 | |
| 834 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 835 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 836 | InstructionSP |
| 837 | InstructionList::GetInstructionAtIndex (uint32_t idx) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 838 | { |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 839 | InstructionSP inst_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 840 | if (idx < m_instructions.size()) |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 841 | inst_sp = m_instructions[idx]; |
| 842 | return inst_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | void |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 846 | InstructionList::Clear() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 847 | { |
| 848 | m_instructions.clear(); |
| 849 | } |
| 850 | |
| 851 | void |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 852 | InstructionList::Append (lldb::InstructionSP &inst_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 853 | { |
| 854 | if (inst_sp) |
| 855 | m_instructions.push_back(inst_sp); |
| 856 | } |
| 857 | |
| 858 | |
| 859 | size_t |
| 860 | Disassembler::ParseInstructions |
| 861 | ( |
| 862 | const ExecutionContext *exe_ctx, |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 863 | const AddressRange &range |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 864 | ) |
| 865 | { |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 866 | Target *target = exe_ctx->target; |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 867 | const addr_t byte_size = range.GetByteSize(); |
| 868 | if (target == NULL || byte_size == 0 || !range.GetBaseAddress().IsValid()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 869 | return 0; |
| 870 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 871 | DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0'); |
| 872 | DataBufferSP data_sp(heap_buffer); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 873 | |
| 874 | Error error; |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 875 | const bool prefer_file_cache = true; |
| 876 | const size_t bytes_read = target->ReadMemory (range.GetBaseAddress(), |
| 877 | prefer_file_cache, |
| 878 | heap_buffer->GetBytes(), |
| 879 | heap_buffer->GetByteSize(), |
| 880 | error); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 881 | |
| 882 | if (bytes_read > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 883 | { |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 884 | if (bytes_read != heap_buffer->GetByteSize()) |
| 885 | heap_buffer->SetByteSize (bytes_read); |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 886 | DataExtractor data (data_sp, |
| 887 | m_arch.GetByteOrder(), |
| 888 | m_arch.GetAddressByteSize()); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 889 | return DecodeInstructions (range.GetBaseAddress(), data, 0, UINT32_MAX, false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 895 | size_t |
| 896 | Disassembler::ParseInstructions |
| 897 | ( |
| 898 | const ExecutionContext *exe_ctx, |
| 899 | const Address &start, |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 900 | uint32_t num_instructions |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 901 | ) |
| 902 | { |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 903 | m_instruction_list.Clear(); |
| 904 | |
| 905 | if (num_instructions == 0 || !start.IsValid()) |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 906 | return 0; |
| 907 | |
| 908 | Target *target = exe_ctx->target; |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 909 | // Calculate the max buffer size we will need in order to disassemble |
| 910 | const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize(); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 911 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 912 | if (target == NULL || byte_size == 0) |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 913 | return 0; |
| 914 | |
| 915 | DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0'); |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 916 | DataBufferSP data_sp (heap_buffer); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 917 | |
| 918 | Error error; |
| 919 | bool prefer_file_cache = true; |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 920 | const size_t bytes_read = target->ReadMemory (start, |
| 921 | prefer_file_cache, |
| 922 | heap_buffer->GetBytes(), |
| 923 | byte_size, |
| 924 | error); |
| 925 | |
| 926 | if (bytes_read == 0) |
| 927 | return 0; |
| 928 | DataExtractor data (data_sp, |
| 929 | m_arch.GetByteOrder(), |
| 930 | m_arch.GetAddressByteSize()); |
| 931 | |
| 932 | const bool append_instructions = true; |
| 933 | DecodeInstructions (start, |
| 934 | data, |
| 935 | 0, |
| 936 | num_instructions, |
| 937 | append_instructions); |
| 938 | |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 939 | return m_instruction_list.GetSize(); |
| 940 | } |
| 941 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 942 | //---------------------------------------------------------------------- |
| 943 | // Disassembler copy constructor |
| 944 | //---------------------------------------------------------------------- |
| 945 | Disassembler::Disassembler(const ArchSpec& arch) : |
| 946 | m_arch (arch), |
| 947 | m_instruction_list(), |
| 948 | m_base_addr(LLDB_INVALID_ADDRESS) |
| 949 | { |
| 950 | |
| 951 | } |
| 952 | |
| 953 | //---------------------------------------------------------------------- |
| 954 | // Destructor |
| 955 | //---------------------------------------------------------------------- |
| 956 | Disassembler::~Disassembler() |
| 957 | { |
| 958 | } |
| 959 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 960 | InstructionList & |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 961 | Disassembler::GetInstructionList () |
| 962 | { |
| 963 | return m_instruction_list; |
| 964 | } |
| 965 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 966 | const InstructionList & |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 967 | Disassembler::GetInstructionList () const |
| 968 | { |
| 969 | return m_instruction_list; |
| 970 | } |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 971 | |
| 972 | //---------------------------------------------------------------------- |
| 973 | // Class PseudoInstruction |
| 974 | //---------------------------------------------------------------------- |
| 975 | PseudoInstruction::PseudoInstruction () : |
| 976 | Instruction (Address(), eAddressClassUnknown), |
| 977 | m_description () |
| 978 | { |
| 979 | } |
| 980 | |
| 981 | PseudoInstruction::~PseudoInstruction () |
| 982 | { |
| 983 | } |
| 984 | |
| 985 | void |
| 986 | PseudoInstruction::Dump (lldb_private::Stream *s, |
| 987 | uint32_t max_opcode_byte_size, |
| 988 | bool show_address, |
| 989 | bool show_bytes, |
| 990 | const lldb_private::ExecutionContext* exe_ctx, |
| 991 | bool raw) |
| 992 | { |
| 993 | if (!s) |
| 994 | return; |
| 995 | |
| 996 | if (show_bytes) |
| 997 | m_opcode.Dump (s, max_opcode_byte_size); |
| 998 | |
| 999 | if (m_description.size() > 0) |
| 1000 | s->Printf ("%s", m_description.c_str()); |
| 1001 | else |
| 1002 | s->Printf ("<unknown>"); |
| 1003 | |
| 1004 | } |
| 1005 | |
| 1006 | bool |
| 1007 | PseudoInstruction::DoesBranch () const |
| 1008 | { |
| 1009 | // This is NOT a valid question for a pseudo instruction. |
| 1010 | return false; |
| 1011 | } |
| 1012 | |
| 1013 | size_t |
| 1014 | PseudoInstruction::Decode (const lldb_private::Disassembler &disassembler, |
| 1015 | const lldb_private::DataExtractor &data, |
| 1016 | uint32_t data_offset) |
| 1017 | { |
| 1018 | return m_opcode.GetByteSize(); |
| 1019 | } |
| 1020 | |
| 1021 | |
| 1022 | void |
| 1023 | PseudoInstruction::SetOpcode (size_t opcode_size, void *opcode_data) |
| 1024 | { |
| 1025 | if (!opcode_data) |
| 1026 | return; |
| 1027 | |
| 1028 | switch (opcode_size) |
| 1029 | { |
| 1030 | case 8: |
| 1031 | { |
| 1032 | uint8_t value8 = *((uint8_t *) opcode_data); |
| 1033 | m_opcode.SetOpcode8 (value8); |
| 1034 | break; |
| 1035 | } |
| 1036 | case 16: |
| 1037 | { |
| 1038 | uint16_t value16 = *((uint16_t *) opcode_data); |
| 1039 | m_opcode.SetOpcode16 (value16); |
| 1040 | break; |
| 1041 | } |
| 1042 | case 32: |
| 1043 | { |
| 1044 | uint32_t value32 = *((uint32_t *) opcode_data); |
| 1045 | m_opcode.SetOpcode32 (value32); |
| 1046 | break; |
| 1047 | } |
| 1048 | case 64: |
| 1049 | { |
| 1050 | uint64_t value64 = *((uint64_t *) opcode_data); |
| 1051 | m_opcode.SetOpcode64 (value64); |
| 1052 | break; |
| 1053 | } |
| 1054 | default: |
| 1055 | break; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | void |
| 1060 | PseudoInstruction::SetDescription (const char *description) |
| 1061 | { |
| 1062 | if (description && strlen (description) > 0) |
| 1063 | m_description = description; |
| 1064 | } |