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