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 | |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Disassembler.h" |
| 13 | |
| 14 | // C Includes |
| 15 | // C++ Includes |
| 16 | // Other libraries and framework includes |
| 17 | // Project includes |
| 18 | #include "lldb/lldb-private.h" |
| 19 | #include "lldb/Core/Error.h" |
| 20 | #include "lldb/Core/DataBufferHeap.h" |
| 21 | #include "lldb/Core/DataExtractor.h" |
| 22 | #include "lldb/Core/Debugger.h" |
Caroline Tice | ad379efc | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 23 | #include "lldb/Core/EmulateInstruction.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | #include "lldb/Core/Module.h" |
| 25 | #include "lldb/Core/PluginManager.h" |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 26 | #include "lldb/Core/RegularExpression.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "lldb/Core/Timer.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 28 | #include "lldb/Interpreter/OptionValue.h" |
| 29 | #include "lldb/Interpreter/OptionValueArray.h" |
| 30 | #include "lldb/Interpreter/OptionValueDictionary.h" |
| 31 | #include "lldb/Interpreter/OptionValueString.h" |
| 32 | #include "lldb/Interpreter/OptionValueUInt64.h" |
Sean Callanan | b6d70eb | 2011-10-12 02:08:07 +0000 | [diff] [blame] | 33 | #include "lldb/Symbol/ClangNamespaceDecl.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 34 | #include "lldb/Symbol/Function.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | #include "lldb/Symbol/ObjectFile.h" |
| 36 | #include "lldb/Target/ExecutionContext.h" |
| 37 | #include "lldb/Target/Process.h" |
| 38 | #include "lldb/Target/StackFrame.h" |
| 39 | #include "lldb/Target/Target.h" |
| 40 | |
| 41 | #define DEFAULT_DISASM_BYTE_SIZE 32 |
| 42 | |
| 43 | using namespace lldb; |
| 44 | using namespace lldb_private; |
| 45 | |
| 46 | |
Sean Callanan | 7e6d4e5 | 2012-08-01 18:50:59 +0000 | [diff] [blame] | 47 | DisassemblerSP |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 48 | Disassembler::FindPlugin (const ArchSpec &arch, const char *flavor, const char *plugin_name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | { |
| 50 | Timer scoped_timer (__PRETTY_FUNCTION__, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 51 | "Disassembler::FindPlugin (arch = %s, plugin_name = %s)", |
| 52 | arch.GetArchitectureName(), |
| 53 | plugin_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 55 | DisassemblerCreateInstance create_callback = NULL; |
| 56 | |
| 57 | if (plugin_name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | { |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 59 | create_callback = PluginManager::GetDisassemblerCreateCallbackForPluginName (plugin_name); |
| 60 | if (create_callback) |
| 61 | { |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 62 | DisassemblerSP disassembler_sp(create_callback(arch, flavor)); |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 63 | |
Sean Callanan | 7e6d4e5 | 2012-08-01 18:50:59 +0000 | [diff] [blame] | 64 | if (disassembler_sp.get()) |
| 65 | return disassembler_sp; |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | for (uint32_t idx = 0; (create_callback = PluginManager::GetDisassemblerCreateCallbackAtIndex(idx)) != NULL; ++idx) |
| 71 | { |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 72 | DisassemblerSP disassembler_sp(create_callback(arch, flavor)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | |
Sean Callanan | 7e6d4e5 | 2012-08-01 18:50:59 +0000 | [diff] [blame] | 74 | if (disassembler_sp.get()) |
| 75 | return disassembler_sp; |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 76 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | } |
Sean Callanan | 7e6d4e5 | 2012-08-01 18:50:59 +0000 | [diff] [blame] | 78 | return DisassemblerSP(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 81 | DisassemblerSP |
| 82 | Disassembler::FindPluginForTarget(const TargetSP target_sp, const ArchSpec &arch, const char *flavor, const char *plugin_name) |
| 83 | { |
| 84 | if (target_sp && flavor == NULL) |
| 85 | { |
| 86 | // FIXME - we don't have the mechanism in place to do per-architecture settings. But since we know that for now |
| 87 | // we only support flavors on x86 & x86_64, |
| 88 | if (arch.GetTriple().getArch() == llvm::Triple::x86 |
| 89 | || arch.GetTriple().getArch() == llvm::Triple::x86_64) |
| 90 | flavor = target_sp->GetDisassemblyFlavor(); |
| 91 | } |
| 92 | return FindPlugin(arch, flavor, plugin_name); |
| 93 | } |
| 94 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 95 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 96 | static void |
| 97 | ResolveAddress (const ExecutionContext &exe_ctx, |
| 98 | const Address &addr, |
| 99 | Address &resolved_addr) |
| 100 | { |
| 101 | if (!addr.IsSectionOffset()) |
| 102 | { |
| 103 | // If we weren't passed in a section offset address range, |
| 104 | // try and resolve it to something |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 105 | Target *target = exe_ctx.GetTargetPtr(); |
| 106 | if (target) |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 107 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 108 | if (target->GetSectionLoadList().IsEmpty()) |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 109 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 110 | target->GetImages().ResolveFileAddress (addr.GetOffset(), resolved_addr); |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 114 | target->GetSectionLoadList().ResolveLoadAddress (addr.GetOffset(), resolved_addr); |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 115 | } |
| 116 | // We weren't able to resolve the address, just treat it as a |
| 117 | // raw address |
| 118 | if (resolved_addr.IsValid()) |
| 119 | return; |
| 120 | } |
| 121 | } |
| 122 | resolved_addr = addr; |
| 123 | } |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 124 | |
| 125 | size_t |
| 126 | Disassembler::Disassemble |
| 127 | ( |
| 128 | Debugger &debugger, |
| 129 | const ArchSpec &arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 130 | const char *plugin_name, |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 131 | const char *flavor, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 132 | const ExecutionContext &exe_ctx, |
| 133 | SymbolContextList &sc_list, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 134 | uint32_t num_instructions, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 135 | uint32_t num_mixed_context_lines, |
Greg Clayton | 1da6f9d | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 136 | uint32_t options, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 137 | Stream &strm |
| 138 | ) |
| 139 | { |
| 140 | size_t success_count = 0; |
| 141 | const size_t count = sc_list.GetSize(); |
| 142 | SymbolContext sc; |
| 143 | AddressRange range; |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 144 | const uint32_t scope = eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol; |
| 145 | const bool use_inline_block_range = true; |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 146 | for (size_t i=0; i<count; ++i) |
| 147 | { |
| 148 | if (sc_list.GetContextAtIndex(i, sc) == false) |
| 149 | break; |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 150 | 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] | 151 | { |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 152 | if (Disassemble (debugger, |
| 153 | arch, |
| 154 | plugin_name, |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 155 | flavor, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 156 | exe_ctx, |
| 157 | range, |
| 158 | num_instructions, |
| 159 | num_mixed_context_lines, |
Greg Clayton | 1da6f9d | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 160 | options, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 161 | strm)) |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 162 | { |
| 163 | ++success_count; |
| 164 | strm.EOL(); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | return success_count; |
| 169 | } |
| 170 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 171 | bool |
| 172 | Disassembler::Disassemble |
| 173 | ( |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 174 | Debugger &debugger, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 175 | const ArchSpec &arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 176 | const char *plugin_name, |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 177 | const char *flavor, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 178 | const ExecutionContext &exe_ctx, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 179 | const ConstString &name, |
| 180 | Module *module, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 181 | uint32_t num_instructions, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 182 | uint32_t num_mixed_context_lines, |
Greg Clayton | 1da6f9d | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 183 | uint32_t options, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 184 | Stream &strm |
| 185 | ) |
| 186 | { |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 187 | SymbolContextList sc_list; |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 188 | if (name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 189 | { |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 190 | const bool include_symbols = true; |
Sean Callanan | 9df05fb | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 191 | const bool include_inlines = true; |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 192 | if (module) |
| 193 | { |
Sean Callanan | b6d70eb | 2011-10-12 02:08:07 +0000 | [diff] [blame] | 194 | module->FindFunctions (name, |
| 195 | NULL, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 196 | eFunctionNameTypeBase | |
| 197 | eFunctionNameTypeFull | |
| 198 | eFunctionNameTypeMethod | |
| 199 | eFunctionNameTypeSelector, |
| 200 | include_symbols, |
Sean Callanan | 9df05fb | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 201 | include_inlines, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 202 | true, |
| 203 | sc_list); |
| 204 | } |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 205 | else if (exe_ctx.GetTargetPtr()) |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 206 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 207 | exe_ctx.GetTargetPtr()->GetImages().FindFunctions (name, |
| 208 | eFunctionNameTypeBase | |
| 209 | eFunctionNameTypeFull | |
| 210 | eFunctionNameTypeMethod | |
| 211 | eFunctionNameTypeSelector, |
Sean Callanan | 9df05fb | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 212 | include_symbols, |
| 213 | include_inlines, |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 214 | false, |
| 215 | sc_list); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 216 | } |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (sc_list.GetSize ()) |
| 220 | { |
| 221 | return Disassemble (debugger, |
| 222 | arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 223 | plugin_name, |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 224 | flavor, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 225 | exe_ctx, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 226 | sc_list, |
| 227 | num_instructions, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 228 | num_mixed_context_lines, |
Greg Clayton | 1da6f9d | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 229 | options, |
Greg Clayton | 931180e | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 230 | strm); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 235 | |
| 236 | lldb::DisassemblerSP |
| 237 | Disassembler::DisassembleRange |
| 238 | ( |
| 239 | const ArchSpec &arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 240 | const char *plugin_name, |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 241 | const char *flavor, |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 242 | const ExecutionContext &exe_ctx, |
| 243 | const AddressRange &range |
| 244 | ) |
| 245 | { |
| 246 | lldb::DisassemblerSP disasm_sp; |
| 247 | if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid()) |
| 248 | { |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 249 | disasm_sp = Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch, flavor, plugin_name); |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 250 | |
| 251 | if (disasm_sp) |
| 252 | { |
Greg Clayton | 57f0630 | 2012-05-25 17:05:55 +0000 | [diff] [blame] | 253 | size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range, NULL); |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 254 | if (bytes_disassembled == 0) |
| 255 | disasm_sp.reset(); |
| 256 | } |
| 257 | } |
| 258 | return disasm_sp; |
| 259 | } |
| 260 | |
Sean Callanan | 50952e9 | 2011-12-14 23:49:37 +0000 | [diff] [blame] | 261 | lldb::DisassemblerSP |
| 262 | Disassembler::DisassembleBytes |
| 263 | ( |
| 264 | const ArchSpec &arch, |
| 265 | const char *plugin_name, |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 266 | const char *flavor, |
Sean Callanan | 50952e9 | 2011-12-14 23:49:37 +0000 | [diff] [blame] | 267 | const Address &start, |
| 268 | const void *bytes, |
Greg Clayton | 9c76611 | 2012-03-06 22:24:44 +0000 | [diff] [blame] | 269 | size_t length, |
| 270 | uint32_t num_instructions |
Sean Callanan | 50952e9 | 2011-12-14 23:49:37 +0000 | [diff] [blame] | 271 | ) |
| 272 | { |
| 273 | lldb::DisassemblerSP disasm_sp; |
| 274 | |
| 275 | if (bytes) |
| 276 | { |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 277 | disasm_sp = Disassembler::FindPlugin(arch, flavor, plugin_name); |
Sean Callanan | 50952e9 | 2011-12-14 23:49:37 +0000 | [diff] [blame] | 278 | |
| 279 | if (disasm_sp) |
| 280 | { |
| 281 | DataExtractor data(bytes, length, arch.GetByteOrder(), arch.GetAddressByteSize()); |
| 282 | |
| 283 | (void)disasm_sp->DecodeInstructions (start, |
| 284 | data, |
| 285 | 0, |
Greg Clayton | 9c76611 | 2012-03-06 22:24:44 +0000 | [diff] [blame] | 286 | num_instructions, |
Sean Callanan | 50952e9 | 2011-12-14 23:49:37 +0000 | [diff] [blame] | 287 | false); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return disasm_sp; |
| 292 | } |
| 293 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 294 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 295 | bool |
| 296 | Disassembler::Disassemble |
| 297 | ( |
| 298 | Debugger &debugger, |
| 299 | const ArchSpec &arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 300 | const char *plugin_name, |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 301 | const char *flavor, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 302 | const ExecutionContext &exe_ctx, |
| 303 | const AddressRange &disasm_range, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 304 | uint32_t num_instructions, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 305 | uint32_t num_mixed_context_lines, |
Greg Clayton | 1da6f9d | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 306 | uint32_t options, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 307 | Stream &strm |
| 308 | ) |
| 309 | { |
| 310 | if (disasm_range.GetByteSize()) |
| 311 | { |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 312 | lldb::DisassemblerSP disasm_sp (Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch, flavor, plugin_name)); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 313 | |
Sean Callanan | 7e6d4e5 | 2012-08-01 18:50:59 +0000 | [diff] [blame] | 314 | if (disasm_sp.get()) |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 315 | { |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 316 | AddressRange range; |
| 317 | ResolveAddress (exe_ctx, disasm_range.GetBaseAddress(), range.GetBaseAddress()); |
| 318 | range.SetByteSize (disasm_range.GetByteSize()); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 319 | |
Sean Callanan | 7e6d4e5 | 2012-08-01 18:50:59 +0000 | [diff] [blame] | 320 | size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range, &strm); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | if (bytes_disassembled == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 322 | return false; |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 323 | |
Sean Callanan | 7e6d4e5 | 2012-08-01 18:50:59 +0000 | [diff] [blame] | 324 | return PrintInstructions (disasm_sp.get(), |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 325 | debugger, |
| 326 | arch, |
| 327 | exe_ctx, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 328 | num_instructions, |
| 329 | num_mixed_context_lines, |
Greg Clayton | 1da6f9d | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 330 | options, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 331 | strm); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | bool |
| 338 | Disassembler::Disassemble |
| 339 | ( |
| 340 | Debugger &debugger, |
| 341 | const ArchSpec &arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 342 | const char *plugin_name, |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 343 | const char *flavor, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 344 | const ExecutionContext &exe_ctx, |
| 345 | const Address &start_address, |
| 346 | uint32_t num_instructions, |
| 347 | uint32_t num_mixed_context_lines, |
Greg Clayton | 1da6f9d | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 348 | uint32_t options, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 349 | Stream &strm |
| 350 | ) |
| 351 | { |
| 352 | if (num_instructions > 0) |
| 353 | { |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 354 | lldb::DisassemblerSP disasm_sp (Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch, flavor, plugin_name)); |
Sean Callanan | 7e6d4e5 | 2012-08-01 18:50:59 +0000 | [diff] [blame] | 355 | if (disasm_sp.get()) |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 356 | { |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 357 | Address addr; |
| 358 | ResolveAddress (exe_ctx, start_address, addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 359 | |
Sean Callanan | 7e6d4e5 | 2012-08-01 18:50:59 +0000 | [diff] [blame] | 360 | size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, addr, num_instructions); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 361 | if (bytes_disassembled == 0) |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 362 | return false; |
Sean Callanan | 7e6d4e5 | 2012-08-01 18:50:59 +0000 | [diff] [blame] | 363 | return PrintInstructions (disasm_sp.get(), |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 364 | debugger, |
| 365 | arch, |
| 366 | exe_ctx, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 367 | num_instructions, |
| 368 | num_mixed_context_lines, |
Greg Clayton | 1da6f9d | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 369 | options, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 370 | strm); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 371 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 372 | } |
| 373 | return false; |
| 374 | } |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 375 | |
| 376 | bool |
| 377 | Disassembler::PrintInstructions |
| 378 | ( |
| 379 | Disassembler *disasm_ptr, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 380 | Debugger &debugger, |
| 381 | const ArchSpec &arch, |
| 382 | const ExecutionContext &exe_ctx, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 383 | uint32_t num_instructions, |
| 384 | uint32_t num_mixed_context_lines, |
Greg Clayton | 1da6f9d | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 385 | uint32_t options, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 386 | Stream &strm |
| 387 | ) |
| 388 | { |
| 389 | // We got some things disassembled... |
| 390 | size_t num_instructions_found = disasm_ptr->GetInstructionList().GetSize(); |
| 391 | |
| 392 | if (num_instructions > 0 && num_instructions < num_instructions_found) |
| 393 | num_instructions_found = num_instructions; |
| 394 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 395 | const uint32_t max_opcode_byte_size = disasm_ptr->GetInstructionList().GetMaxOpcocdeByteSize (); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 396 | uint32_t offset = 0; |
| 397 | SymbolContext sc; |
| 398 | SymbolContext prev_sc; |
| 399 | AddressRange sc_range; |
Greg Clayton | 3413275 | 2011-07-06 04:07:21 +0000 | [diff] [blame] | 400 | const Address *pc_addr_ptr = NULL; |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 401 | ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope(); |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 402 | StackFrame *frame = exe_ctx.GetFramePtr(); |
| 403 | |
| 404 | if (frame) |
| 405 | pc_addr_ptr = &frame->GetFrameCodeAddress(); |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 406 | const uint32_t scope = eSymbolContextLineEntry | eSymbolContextFunction | eSymbolContextSymbol; |
| 407 | const bool use_inline_block_range = false; |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 408 | for (size_t i=0; i<num_instructions_found; ++i) |
| 409 | { |
| 410 | Instruction *inst = disasm_ptr->GetInstructionList().GetInstructionAtIndex (i).get(); |
| 411 | if (inst) |
| 412 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 413 | const Address &addr = inst->GetAddress(); |
| 414 | 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] | 415 | |
| 416 | prev_sc = sc; |
| 417 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 418 | ModuleSP module_sp (addr.GetModule()); |
| 419 | if (module_sp) |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 420 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 421 | uint32_t resolved_mask = module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, sc); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 422 | if (resolved_mask) |
| 423 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 424 | if (num_mixed_context_lines) |
| 425 | { |
| 426 | if (!sc_range.ContainsFileAddress (addr)) |
| 427 | { |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 428 | sc.GetAddressRange (scope, 0, use_inline_block_range, sc_range); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 429 | |
| 430 | if (sc != prev_sc) |
| 431 | { |
| 432 | if (offset != 0) |
| 433 | strm.EOL(); |
| 434 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 435 | sc.DumpStopContext(&strm, exe_ctx.GetProcessPtr(), addr, false, true, false); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 436 | strm.EOL(); |
| 437 | |
| 438 | if (sc.comp_unit && sc.line_entry.IsValid()) |
| 439 | { |
Jim Ingham | b7f6b2f | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 440 | debugger.GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.line_entry.file, |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 441 | sc.line_entry.line, |
| 442 | num_mixed_context_lines, |
| 443 | num_mixed_context_lines, |
Greg Clayton | b10d72f | 2011-06-28 19:01:40 +0000 | [diff] [blame] | 444 | ((inst_is_at_pc && (options & eOptionMarkPCSourceLine)) ? "->" : ""), |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 445 | &strm); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | } |
Greg Clayton | 1bba2be | 2011-11-30 19:36:42 +0000 | [diff] [blame] | 450 | else if ((sc.function || sc.symbol) && (sc.function != prev_sc.function || sc.symbol != prev_sc.symbol)) |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 451 | { |
| 452 | if (prev_sc.function || prev_sc.symbol) |
| 453 | strm.EOL(); |
| 454 | |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 455 | bool show_fullpaths = false; |
| 456 | bool show_module = true; |
| 457 | bool show_inlined_frames = true; |
| 458 | sc.DumpStopContext (&strm, |
| 459 | exe_scope, |
| 460 | addr, |
| 461 | show_fullpaths, |
| 462 | show_module, |
| 463 | show_inlined_frames); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 464 | |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 465 | strm << ":\n"; |
| 466 | } |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 467 | } |
| 468 | else |
| 469 | { |
Greg Clayton | 7231035 | 2013-02-23 04:12:47 +0000 | [diff] [blame] | 470 | sc.Clear(true); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 473 | |
Greg Clayton | b10d72f | 2011-06-28 19:01:40 +0000 | [diff] [blame] | 474 | if ((options & eOptionMarkPCAddress) && pc_addr_ptr) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 475 | { |
Greg Clayton | b10d72f | 2011-06-28 19:01:40 +0000 | [diff] [blame] | 476 | strm.PutCString(inst_is_at_pc ? "-> " : " "); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 477 | } |
Greg Clayton | 1da6f9d | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 478 | const bool show_bytes = (options & eOptionShowBytes) != 0; |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 479 | inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 480 | strm.EOL(); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 481 | } |
| 482 | else |
| 483 | { |
| 484 | break; |
| 485 | } |
| 486 | } |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 487 | |
| 488 | return true; |
| 489 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 490 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 491 | |
| 492 | bool |
| 493 | Disassembler::Disassemble |
| 494 | ( |
| 495 | Debugger &debugger, |
| 496 | const ArchSpec &arch, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 497 | const char *plugin_name, |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 498 | const char *flavor, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 499 | const ExecutionContext &exe_ctx, |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 500 | uint32_t num_instructions, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 501 | uint32_t num_mixed_context_lines, |
Greg Clayton | 1da6f9d | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 502 | uint32_t options, |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 503 | Stream &strm |
| 504 | ) |
| 505 | { |
| 506 | AddressRange range; |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 507 | StackFrame *frame = exe_ctx.GetFramePtr(); |
| 508 | if (frame) |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 509 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 510 | SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol)); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 511 | if (sc.function) |
| 512 | { |
| 513 | range = sc.function->GetAddressRange(); |
| 514 | } |
Greg Clayton | e761213 | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 515 | else if (sc.symbol && sc.symbol->ValueIsAddress()) |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 516 | { |
Greg Clayton | e761213 | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 517 | range.GetBaseAddress() = sc.symbol->GetAddress(); |
| 518 | range.SetByteSize (sc.symbol->GetByteSize()); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 519 | } |
| 520 | else |
| 521 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 522 | range.GetBaseAddress() = frame->GetFrameCodeAddress(); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | if (range.GetBaseAddress().IsValid() && range.GetByteSize() == 0) |
| 526 | range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE); |
| 527 | } |
| 528 | |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 529 | return Disassemble (debugger, |
| 530 | arch, |
| 531 | plugin_name, |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 532 | flavor, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 533 | exe_ctx, |
| 534 | range, |
| 535 | num_instructions, |
| 536 | num_mixed_context_lines, |
Greg Clayton | 1da6f9d | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 537 | options, |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 538 | strm); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 541 | Instruction::Instruction(const Address &address, AddressClass addr_class) : |
Greg Clayton | 1080edbc | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 542 | m_address (address), |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 543 | m_address_class (addr_class), |
Sean Callanan | a97aa92 | 2012-02-14 00:22:51 +0000 | [diff] [blame] | 544 | m_opcode(), |
| 545 | m_calculated_strings(false) |
Greg Clayton | 0ae9627 | 2011-03-24 23:53:38 +0000 | [diff] [blame] | 546 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 549 | Instruction::~Instruction() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 550 | { |
| 551 | } |
| 552 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 553 | AddressClass |
| 554 | Instruction::GetAddressClass () |
| 555 | { |
| 556 | if (m_address_class == eAddressClassInvalid) |
| 557 | m_address_class = m_address.GetAddressClass(); |
| 558 | return m_address_class; |
| 559 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 560 | |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 561 | void |
| 562 | Instruction::Dump (lldb_private::Stream *s, |
| 563 | uint32_t max_opcode_byte_size, |
| 564 | bool show_address, |
| 565 | bool show_bytes, |
| 566 | const ExecutionContext* exe_ctx) |
| 567 | { |
Jason Molenda | 7a37c1e | 2013-01-04 23:52:35 +0000 | [diff] [blame] | 568 | size_t opcode_column_width = 7; |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 569 | const size_t operand_column_width = 25; |
| 570 | |
| 571 | CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx); |
| 572 | |
| 573 | StreamString ss; |
| 574 | |
| 575 | if (show_address) |
| 576 | { |
| 577 | m_address.Dump(&ss, |
| 578 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL, |
| 579 | Address::DumpStyleLoadAddress, |
| 580 | Address::DumpStyleModuleWithFileAddress, |
| 581 | 0); |
| 582 | |
| 583 | ss.PutCString(": "); |
| 584 | } |
| 585 | |
| 586 | if (show_bytes) |
| 587 | { |
| 588 | if (m_opcode.GetType() == Opcode::eTypeBytes) |
| 589 | { |
| 590 | // x86_64 and i386 are the only ones that use bytes right now so |
| 591 | // pad out the byte dump to be able to always show 15 bytes (3 chars each) |
| 592 | // plus a space |
| 593 | if (max_opcode_byte_size > 0) |
| 594 | m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1); |
| 595 | else |
| 596 | m_opcode.Dump (&ss, 15 * 3 + 1); |
| 597 | } |
| 598 | else |
| 599 | { |
| 600 | // Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces) |
| 601 | // plus two for padding... |
| 602 | if (max_opcode_byte_size > 0) |
| 603 | m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1); |
| 604 | else |
| 605 | m_opcode.Dump (&ss, 12); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | const size_t opcode_pos = ss.GetSize(); |
| 610 | |
Jason Molenda | 7a37c1e | 2013-01-04 23:52:35 +0000 | [diff] [blame] | 611 | // The default opcode size of 7 characters is plenty for most architectures |
| 612 | // but some like arm can pull out the occasional vqrshrun.s16. We won't get |
| 613 | // consistent column spacing in these cases, unfortunately. |
| 614 | if (m_opcode_name.length() >= opcode_column_width) |
| 615 | { |
| 616 | opcode_column_width = m_opcode_name.length() + 1; |
| 617 | } |
| 618 | |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 619 | ss.PutCString (m_opcode_name.c_str()); |
| 620 | ss.FillLastLineToColumn (opcode_pos + opcode_column_width, ' '); |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 621 | ss.PutCString (m_mnemonics.c_str()); |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 622 | |
| 623 | if (!m_comment.empty()) |
| 624 | { |
| 625 | ss.FillLastLineToColumn (opcode_pos + opcode_column_width + operand_column_width, ' '); |
| 626 | ss.PutCString (" ; "); |
| 627 | ss.PutCString (m_comment.c_str()); |
| 628 | } |
| 629 | s->Write (ss.GetData(), ss.GetSize()); |
| 630 | } |
| 631 | |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 632 | bool |
| 633 | Instruction::DumpEmulation (const ArchSpec &arch) |
| 634 | { |
Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 635 | std::auto_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL)); |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 636 | if (insn_emulator_ap.get()) |
| 637 | { |
Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 638 | insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL); |
| 639 | return insn_emulator_ap->EvaluateInstruction (0); |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | return false; |
| 643 | } |
| 644 | |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 645 | OptionValueSP |
| 646 | Instruction::ReadArray (FILE *in_file, Stream *out_stream, OptionValue::Type data_type) |
| 647 | { |
| 648 | bool done = false; |
| 649 | char buffer[1024]; |
| 650 | |
| 651 | OptionValueSP option_value_sp (new OptionValueArray (1u << data_type)); |
| 652 | |
| 653 | int idx = 0; |
| 654 | while (!done) |
| 655 | { |
| 656 | if (!fgets (buffer, 1023, in_file)) |
| 657 | { |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 658 | out_stream->Printf ("Instruction::ReadArray: Error reading file (fgets).\n"); |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 659 | option_value_sp.reset (); |
| 660 | return option_value_sp; |
| 661 | } |
| 662 | |
| 663 | std::string line (buffer); |
| 664 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 665 | size_t len = line.size(); |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 666 | if (line[len-1] == '\n') |
| 667 | { |
| 668 | line[len-1] = '\0'; |
| 669 | line.resize (len-1); |
| 670 | } |
| 671 | |
| 672 | if ((line.size() == 1) && line[0] == ']') |
| 673 | { |
| 674 | done = true; |
| 675 | line.clear(); |
| 676 | } |
| 677 | |
| 678 | if (line.size() > 0) |
| 679 | { |
| 680 | std::string value; |
| 681 | RegularExpression reg_exp ("^[ \t]*([^ \t]+)[ \t]*$"); |
| 682 | bool reg_exp_success = reg_exp.Execute (line.c_str(), 1); |
| 683 | if (reg_exp_success) |
| 684 | reg_exp.GetMatchAtIndex (line.c_str(), 1, value); |
| 685 | else |
| 686 | value = line; |
| 687 | |
| 688 | OptionValueSP data_value_sp; |
| 689 | switch (data_type) |
| 690 | { |
| 691 | case OptionValue::eTypeUInt64: |
| 692 | data_value_sp.reset (new OptionValueUInt64 (0, 0)); |
| 693 | data_value_sp->SetValueFromCString (value.c_str()); |
| 694 | break; |
| 695 | // Other types can be added later as needed. |
| 696 | default: |
| 697 | data_value_sp.reset (new OptionValueString (value.c_str(), "")); |
| 698 | break; |
| 699 | } |
| 700 | |
Greg Clayton | 84c3966 | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 701 | option_value_sp->GetAsArray()->InsertValue (idx, data_value_sp); |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 702 | ++idx; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return option_value_sp; |
| 707 | } |
| 708 | |
| 709 | OptionValueSP |
| 710 | Instruction::ReadDictionary (FILE *in_file, Stream *out_stream) |
| 711 | { |
| 712 | bool done = false; |
| 713 | char buffer[1024]; |
| 714 | |
| 715 | OptionValueSP option_value_sp (new OptionValueDictionary()); |
| 716 | static ConstString encoding_key ("data_encoding"); |
| 717 | OptionValue::Type data_type = OptionValue::eTypeInvalid; |
| 718 | |
| 719 | |
| 720 | while (!done) |
| 721 | { |
| 722 | // Read the next line in the file |
| 723 | if (!fgets (buffer, 1023, in_file)) |
| 724 | { |
| 725 | out_stream->Printf ("Instruction::ReadDictionary: Error reading file (fgets).\n"); |
| 726 | option_value_sp.reset (); |
| 727 | return option_value_sp; |
| 728 | } |
| 729 | |
| 730 | // Check to see if the line contains the end-of-dictionary marker ("}") |
| 731 | std::string line (buffer); |
| 732 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 733 | size_t len = line.size(); |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 734 | if (line[len-1] == '\n') |
| 735 | { |
| 736 | line[len-1] = '\0'; |
| 737 | line.resize (len-1); |
| 738 | } |
| 739 | |
| 740 | if ((line.size() == 1) && (line[0] == '}')) |
| 741 | { |
| 742 | done = true; |
| 743 | line.clear(); |
| 744 | } |
| 745 | |
| 746 | // Try to find a key-value pair in the current line and add it to the dictionary. |
| 747 | if (line.size() > 0) |
| 748 | { |
| 749 | RegularExpression reg_exp ("^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*=[ \t]*(.*)[ \t]*$"); |
| 750 | bool reg_exp_success = reg_exp.Execute (line.c_str(), 2); |
| 751 | std::string key; |
| 752 | std::string value; |
| 753 | if (reg_exp_success) |
| 754 | { |
| 755 | reg_exp.GetMatchAtIndex (line.c_str(), 1, key); |
| 756 | reg_exp.GetMatchAtIndex (line.c_str(), 2, value); |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | out_stream->Printf ("Instruction::ReadDictionary: Failure executing regular expression.\n"); |
| 761 | option_value_sp.reset(); |
| 762 | return option_value_sp; |
| 763 | } |
| 764 | |
| 765 | ConstString const_key (key.c_str()); |
| 766 | // Check value to see if it's the start of an array or dictionary. |
| 767 | |
| 768 | lldb::OptionValueSP value_sp; |
| 769 | assert (value.empty() == false); |
| 770 | assert (key.empty() == false); |
| 771 | |
| 772 | if (value[0] == '{') |
| 773 | { |
| 774 | assert (value.size() == 1); |
| 775 | // value is a dictionary |
| 776 | value_sp = ReadDictionary (in_file, out_stream); |
| 777 | if (value_sp.get() == NULL) |
| 778 | { |
| 779 | option_value_sp.reset (); |
| 780 | return option_value_sp; |
| 781 | } |
| 782 | } |
| 783 | else if (value[0] == '[') |
| 784 | { |
| 785 | assert (value.size() == 1); |
| 786 | // value is an array |
| 787 | value_sp = ReadArray (in_file, out_stream, data_type); |
| 788 | if (value_sp.get() == NULL) |
| 789 | { |
| 790 | option_value_sp.reset (); |
| 791 | return option_value_sp; |
| 792 | } |
| 793 | // We've used the data_type to read an array; re-set the type to Invalid |
| 794 | data_type = OptionValue::eTypeInvalid; |
| 795 | } |
| 796 | else if ((value[0] == '0') && (value[1] == 'x')) |
| 797 | { |
| 798 | value_sp.reset (new OptionValueUInt64 (0, 0)); |
| 799 | value_sp->SetValueFromCString (value.c_str()); |
| 800 | } |
| 801 | else |
| 802 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 803 | size_t len = value.size(); |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 804 | if ((value[0] == '"') && (value[len-1] == '"')) |
| 805 | value = value.substr (1, len-2); |
| 806 | value_sp.reset (new OptionValueString (value.c_str(), "")); |
| 807 | } |
| 808 | |
| 809 | |
| 810 | |
| 811 | if (const_key == encoding_key) |
| 812 | { |
| 813 | // A 'data_encoding=..." is NOT a normal key-value pair; it is meta-data indicating the |
| 814 | // data type of an upcoming array (usually the next bit of data to be read in). |
| 815 | if (strcmp (value.c_str(), "uint32_t") == 0) |
| 816 | data_type = OptionValue::eTypeUInt64; |
| 817 | } |
| 818 | else |
Greg Clayton | 84c3966 | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 819 | option_value_sp->GetAsDictionary()->SetValueForKey (const_key, value_sp, false); |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
| 823 | return option_value_sp; |
| 824 | } |
| 825 | |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 826 | bool |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 827 | Instruction::TestEmulation (Stream *out_stream, const char *file_name) |
| 828 | { |
| 829 | if (!out_stream) |
| 830 | return false; |
| 831 | |
| 832 | if (!file_name) |
| 833 | { |
Johnny Chen | ea80ba8 | 2011-04-21 20:27:45 +0000 | [diff] [blame] | 834 | out_stream->Printf ("Instruction::TestEmulation: Missing file_name."); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 835 | return false; |
| 836 | } |
| 837 | |
| 838 | FILE *test_file = fopen (file_name, "r"); |
| 839 | if (!test_file) |
| 840 | { |
Johnny Chen | ea80ba8 | 2011-04-21 20:27:45 +0000 | [diff] [blame] | 841 | out_stream->Printf ("Instruction::TestEmulation: Attempt to open test file failed."); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 842 | return false; |
| 843 | } |
| 844 | |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 845 | char buffer[256]; |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 846 | if (!fgets (buffer, 255, test_file)) |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 847 | { |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 848 | 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] | 849 | fclose (test_file); |
| 850 | return false; |
| 851 | } |
| 852 | |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 853 | if (strncmp (buffer, "InstructionEmulationState={", 27) != 0) |
| 854 | { |
| 855 | out_stream->Printf ("Instructin::TestEmulation: Test file does not contain emulation state dictionary\n"); |
| 856 | fclose (test_file); |
| 857 | return false; |
| 858 | } |
| 859 | |
| 860 | // Read all the test information from the test file into an OptionValueDictionary. |
| 861 | |
| 862 | OptionValueSP data_dictionary_sp (ReadDictionary (test_file, out_stream)); |
| 863 | if (data_dictionary_sp.get() == NULL) |
| 864 | { |
| 865 | out_stream->Printf ("Instruction::TestEmulation: Error reading Dictionary Object.\n"); |
| 866 | fclose (test_file); |
| 867 | return false; |
| 868 | } |
| 869 | |
| 870 | fclose (test_file); |
| 871 | |
Greg Clayton | 84c3966 | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 872 | OptionValueDictionary *data_dictionary = data_dictionary_sp->GetAsDictionary(); |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 873 | static ConstString description_key ("assembly_string"); |
| 874 | static ConstString triple_key ("triple"); |
| 875 | |
| 876 | OptionValueSP value_sp = data_dictionary->GetValueForKey (description_key); |
| 877 | |
| 878 | if (value_sp.get() == NULL) |
| 879 | { |
| 880 | out_stream->Printf ("Instruction::TestEmulation: Test file does not contain description string.\n"); |
| 881 | return false; |
| 882 | } |
| 883 | |
| 884 | SetDescription (value_sp->GetStringValue()); |
| 885 | |
| 886 | |
| 887 | value_sp = data_dictionary->GetValueForKey (triple_key); |
| 888 | if (value_sp.get() == NULL) |
| 889 | { |
| 890 | out_stream->Printf ("Instruction::TestEmulation: Test file does not contain triple.\n"); |
| 891 | return false; |
| 892 | } |
| 893 | |
| 894 | ArchSpec arch; |
| 895 | arch.SetTriple (llvm::Triple (value_sp->GetStringValue())); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 896 | |
| 897 | bool success = false; |
Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 898 | std::auto_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL)); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 899 | if (insn_emulator_ap.get()) |
Caroline Tice | de2fb9c | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 900 | success = insn_emulator_ap->TestEmulation (out_stream, arch, data_dictionary); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 901 | |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 902 | if (success) |
Johnny Chen | ea80ba8 | 2011-04-21 20:27:45 +0000 | [diff] [blame] | 903 | out_stream->Printf ("Emulation test succeeded."); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 904 | else |
Johnny Chen | ea80ba8 | 2011-04-21 20:27:45 +0000 | [diff] [blame] | 905 | out_stream->Printf ("Emulation test failed."); |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 906 | |
| 907 | return success; |
| 908 | } |
| 909 | |
| 910 | bool |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 911 | Instruction::Emulate (const ArchSpec &arch, |
Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 912 | uint32_t evaluate_options, |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 913 | void *baton, |
Greg Clayton | 7349bd9 | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 914 | EmulateInstruction::ReadMemoryCallback read_mem_callback, |
| 915 | EmulateInstruction::WriteMemoryCallback write_mem_callback, |
| 916 | EmulateInstruction::ReadRegisterCallback read_reg_callback, |
| 917 | EmulateInstruction::WriteRegisterCallback write_reg_callback) |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 918 | { |
Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 919 | std::auto_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL)); |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 920 | if (insn_emulator_ap.get()) |
| 921 | { |
| 922 | insn_emulator_ap->SetBaton (baton); |
| 923 | insn_emulator_ap->SetCallbacks (read_mem_callback, write_mem_callback, read_reg_callback, write_reg_callback); |
Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 924 | insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL); |
| 925 | return insn_emulator_ap->EvaluateInstruction (evaluate_options); |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | return false; |
| 929 | } |
| 930 | |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 931 | |
| 932 | uint32_t |
| 933 | Instruction::GetData (DataExtractor &data) |
| 934 | { |
Sean Callanan | cd4ae1a | 2012-08-07 01:44:58 +0000 | [diff] [blame] | 935 | return m_opcode.GetData(data); |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 938 | InstructionList::InstructionList() : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 939 | m_instructions() |
| 940 | { |
| 941 | } |
| 942 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 943 | InstructionList::~InstructionList() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 944 | { |
| 945 | } |
| 946 | |
| 947 | size_t |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 948 | InstructionList::GetSize() const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 949 | { |
| 950 | return m_instructions.size(); |
| 951 | } |
| 952 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 953 | uint32_t |
| 954 | InstructionList::GetMaxOpcocdeByteSize () const |
| 955 | { |
| 956 | uint32_t max_inst_size = 0; |
| 957 | collection::const_iterator pos, end; |
| 958 | for (pos = m_instructions.begin(), end = m_instructions.end(); |
| 959 | pos != end; |
| 960 | ++pos) |
| 961 | { |
| 962 | uint32_t inst_size = (*pos)->GetOpcode().GetByteSize(); |
| 963 | if (max_inst_size < inst_size) |
| 964 | max_inst_size = inst_size; |
| 965 | } |
| 966 | return max_inst_size; |
| 967 | } |
| 968 | |
| 969 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 970 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 971 | InstructionSP |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 972 | InstructionList::GetInstructionAtIndex (size_t idx) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 973 | { |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 974 | InstructionSP inst_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 975 | if (idx < m_instructions.size()) |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 976 | inst_sp = m_instructions[idx]; |
| 977 | return inst_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | void |
Greg Clayton | 5009f9d | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 981 | InstructionList::Dump (Stream *s, |
| 982 | bool show_address, |
| 983 | bool show_bytes, |
| 984 | const ExecutionContext* exe_ctx) |
| 985 | { |
| 986 | const uint32_t max_opcode_byte_size = GetMaxOpcocdeByteSize(); |
| 987 | collection::const_iterator pos, begin, end; |
| 988 | for (begin = m_instructions.begin(), end = m_instructions.end(), pos = begin; |
| 989 | pos != end; |
| 990 | ++pos) |
| 991 | { |
| 992 | if (pos != begin) |
| 993 | s->EOL(); |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 994 | (*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx); |
Greg Clayton | 5009f9d | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
| 998 | |
| 999 | void |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 1000 | InstructionList::Clear() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1001 | { |
| 1002 | m_instructions.clear(); |
| 1003 | } |
| 1004 | |
| 1005 | void |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 1006 | InstructionList::Append (lldb::InstructionSP &inst_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1007 | { |
| 1008 | if (inst_sp) |
| 1009 | m_instructions.push_back(inst_sp); |
| 1010 | } |
| 1011 | |
Jim Ingham | 564d8bc2 | 2012-03-09 04:10:47 +0000 | [diff] [blame] | 1012 | uint32_t |
| 1013 | InstructionList::GetIndexOfNextBranchInstruction(uint32_t start) const |
| 1014 | { |
| 1015 | size_t num_instructions = m_instructions.size(); |
| 1016 | |
| 1017 | uint32_t next_branch = UINT32_MAX; |
| 1018 | for (size_t i = start; i < num_instructions; i++) |
| 1019 | { |
| 1020 | if (m_instructions[i]->DoesBranch()) |
| 1021 | { |
| 1022 | next_branch = i; |
| 1023 | break; |
| 1024 | } |
| 1025 | } |
| 1026 | return next_branch; |
| 1027 | } |
| 1028 | |
| 1029 | uint32_t |
| 1030 | InstructionList::GetIndexOfInstructionAtLoadAddress (lldb::addr_t load_addr, Target &target) |
| 1031 | { |
| 1032 | Address address; |
| 1033 | address.SetLoadAddress(load_addr, &target); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1034 | size_t num_instructions = m_instructions.size(); |
Jim Ingham | 564d8bc2 | 2012-03-09 04:10:47 +0000 | [diff] [blame] | 1035 | uint32_t index = UINT32_MAX; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1036 | for (size_t i = 0; i < num_instructions; i++) |
Jim Ingham | 564d8bc2 | 2012-03-09 04:10:47 +0000 | [diff] [blame] | 1037 | { |
| 1038 | if (m_instructions[i]->GetAddress() == address) |
| 1039 | { |
| 1040 | index = i; |
| 1041 | break; |
| 1042 | } |
| 1043 | } |
| 1044 | return index; |
| 1045 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1046 | |
| 1047 | size_t |
| 1048 | Disassembler::ParseInstructions |
| 1049 | ( |
| 1050 | const ExecutionContext *exe_ctx, |
Greg Clayton | 57f0630 | 2012-05-25 17:05:55 +0000 | [diff] [blame] | 1051 | const AddressRange &range, |
| 1052 | Stream *error_strm_ptr |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1053 | ) |
| 1054 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1055 | if (exe_ctx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1056 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1057 | Target *target = exe_ctx->GetTargetPtr(); |
| 1058 | const addr_t byte_size = range.GetByteSize(); |
| 1059 | if (target == NULL || byte_size == 0 || !range.GetBaseAddress().IsValid()) |
| 1060 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1061 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1062 | DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0'); |
| 1063 | DataBufferSP data_sp(heap_buffer); |
| 1064 | |
| 1065 | Error error; |
| 1066 | const bool prefer_file_cache = true; |
| 1067 | const size_t bytes_read = target->ReadMemory (range.GetBaseAddress(), |
| 1068 | prefer_file_cache, |
| 1069 | heap_buffer->GetBytes(), |
| 1070 | heap_buffer->GetByteSize(), |
| 1071 | error); |
| 1072 | |
| 1073 | if (bytes_read > 0) |
| 1074 | { |
| 1075 | if (bytes_read != heap_buffer->GetByteSize()) |
| 1076 | heap_buffer->SetByteSize (bytes_read); |
| 1077 | DataExtractor data (data_sp, |
| 1078 | m_arch.GetByteOrder(), |
| 1079 | m_arch.GetAddressByteSize()); |
| 1080 | return DecodeInstructions (range.GetBaseAddress(), data, 0, UINT32_MAX, false); |
| 1081 | } |
Greg Clayton | 57f0630 | 2012-05-25 17:05:55 +0000 | [diff] [blame] | 1082 | else if (error_strm_ptr) |
| 1083 | { |
| 1084 | const char *error_cstr = error.AsCString(); |
| 1085 | if (error_cstr) |
| 1086 | { |
| 1087 | error_strm_ptr->Printf("error: %s\n", error_cstr); |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | else if (error_strm_ptr) |
| 1092 | { |
| 1093 | error_strm_ptr->PutCString("error: invalid execution context\n"); |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1094 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1095 | return 0; |
| 1096 | } |
| 1097 | |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 1098 | size_t |
| 1099 | Disassembler::ParseInstructions |
| 1100 | ( |
| 1101 | const ExecutionContext *exe_ctx, |
| 1102 | const Address &start, |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 1103 | uint32_t num_instructions |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 1104 | ) |
| 1105 | { |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 1106 | m_instruction_list.Clear(); |
| 1107 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1108 | if (exe_ctx == NULL || num_instructions == 0 || !start.IsValid()) |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 1109 | return 0; |
| 1110 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1111 | Target *target = exe_ctx->GetTargetPtr(); |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 1112 | // Calculate the max buffer size we will need in order to disassemble |
| 1113 | const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize(); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 1114 | |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 1115 | if (target == NULL || byte_size == 0) |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 1116 | return 0; |
| 1117 | |
| 1118 | DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0'); |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 1119 | DataBufferSP data_sp (heap_buffer); |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 1120 | |
| 1121 | Error error; |
| 1122 | bool prefer_file_cache = true; |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 1123 | const size_t bytes_read = target->ReadMemory (start, |
| 1124 | prefer_file_cache, |
| 1125 | heap_buffer->GetBytes(), |
| 1126 | byte_size, |
| 1127 | error); |
| 1128 | |
| 1129 | if (bytes_read == 0) |
| 1130 | return 0; |
| 1131 | DataExtractor data (data_sp, |
| 1132 | m_arch.GetByteOrder(), |
| 1133 | m_arch.GetAddressByteSize()); |
| 1134 | |
| 1135 | const bool append_instructions = true; |
| 1136 | DecodeInstructions (start, |
| 1137 | data, |
| 1138 | 0, |
| 1139 | num_instructions, |
| 1140 | append_instructions); |
| 1141 | |
Jim Ingham | 37023b0 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 1142 | return m_instruction_list.GetSize(); |
| 1143 | } |
| 1144 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1145 | //---------------------------------------------------------------------- |
| 1146 | // Disassembler copy constructor |
| 1147 | //---------------------------------------------------------------------- |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 1148 | Disassembler::Disassembler(const ArchSpec& arch, const char *flavor) : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1149 | m_arch (arch), |
| 1150 | m_instruction_list(), |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 1151 | m_base_addr(LLDB_INVALID_ADDRESS), |
| 1152 | m_flavor () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1153 | { |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 1154 | if (flavor == NULL) |
| 1155 | m_flavor.assign("default"); |
| 1156 | else |
| 1157 | m_flavor.assign(flavor); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | //---------------------------------------------------------------------- |
| 1161 | // Destructor |
| 1162 | //---------------------------------------------------------------------- |
| 1163 | Disassembler::~Disassembler() |
| 1164 | { |
| 1165 | } |
| 1166 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 1167 | InstructionList & |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1168 | Disassembler::GetInstructionList () |
| 1169 | { |
| 1170 | return m_instruction_list; |
| 1171 | } |
| 1172 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 1173 | const InstructionList & |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1174 | Disassembler::GetInstructionList () const |
| 1175 | { |
| 1176 | return m_instruction_list; |
| 1177 | } |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 1178 | |
| 1179 | //---------------------------------------------------------------------- |
| 1180 | // Class PseudoInstruction |
| 1181 | //---------------------------------------------------------------------- |
| 1182 | PseudoInstruction::PseudoInstruction () : |
| 1183 | Instruction (Address(), eAddressClassUnknown), |
| 1184 | m_description () |
| 1185 | { |
| 1186 | } |
| 1187 | |
| 1188 | PseudoInstruction::~PseudoInstruction () |
| 1189 | { |
| 1190 | } |
| 1191 | |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 1192 | bool |
| 1193 | PseudoInstruction::DoesBranch () const |
| 1194 | { |
| 1195 | // This is NOT a valid question for a pseudo instruction. |
| 1196 | return false; |
| 1197 | } |
| 1198 | |
| 1199 | size_t |
| 1200 | PseudoInstruction::Decode (const lldb_private::Disassembler &disassembler, |
| 1201 | const lldb_private::DataExtractor &data, |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1202 | lldb::offset_t data_offset) |
Caroline Tice | 3ac6711 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 1203 | { |
| 1204 | return m_opcode.GetByteSize(); |
| 1205 | } |
| 1206 | |
| 1207 | |
| 1208 | void |
| 1209 | PseudoInstruction::SetOpcode (size_t opcode_size, void *opcode_data) |
| 1210 | { |
| 1211 | if (!opcode_data) |
| 1212 | return; |
| 1213 | |
| 1214 | switch (opcode_size) |
| 1215 | { |
| 1216 | case 8: |
| 1217 | { |
| 1218 | uint8_t value8 = *((uint8_t *) opcode_data); |
| 1219 | m_opcode.SetOpcode8 (value8); |
| 1220 | break; |
| 1221 | } |
| 1222 | case 16: |
| 1223 | { |
| 1224 | uint16_t value16 = *((uint16_t *) opcode_data); |
| 1225 | m_opcode.SetOpcode16 (value16); |
| 1226 | break; |
| 1227 | } |
| 1228 | case 32: |
| 1229 | { |
| 1230 | uint32_t value32 = *((uint32_t *) opcode_data); |
| 1231 | m_opcode.SetOpcode32 (value32); |
| 1232 | break; |
| 1233 | } |
| 1234 | case 64: |
| 1235 | { |
| 1236 | uint64_t value64 = *((uint64_t *) opcode_data); |
| 1237 | m_opcode.SetOpcode64 (value64); |
| 1238 | break; |
| 1239 | } |
| 1240 | default: |
| 1241 | break; |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | void |
| 1246 | PseudoInstruction::SetDescription (const char *description) |
| 1247 | { |
| 1248 | if (description && strlen (description) > 0) |
| 1249 | m_description = description; |
| 1250 | } |