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