blob: e80e92c91b51efc1d9c580f07775ab35b5aea21c [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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 Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#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 Ticead379efc2011-04-05 18:46:00 +000023#include "lldb/Core/EmulateInstruction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/Module.h"
25#include "lldb/Core/PluginManager.h"
Caroline Ticede2fb9c2011-04-22 05:08:45 +000026#include "lldb/Core/RegularExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/Timer.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000028#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 Callananb6d70eb2011-10-12 02:08:07 +000033#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Clayton1f746072012-08-29 21:13:06 +000034#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#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
43using namespace lldb;
44using namespace lldb_private;
45
46
Sean Callanan7e6d4e52012-08-01 18:50:59 +000047DisassemblerSP
Jim Ingham0f063ba2013-03-02 00:26:47 +000048Disassembler::FindPlugin (const ArchSpec &arch, const char *flavor, const char *plugin_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049{
50 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Clayton1080edbc2011-03-25 18:03:16 +000051 "Disassembler::FindPlugin (arch = %s, plugin_name = %s)",
52 arch.GetArchitectureName(),
53 plugin_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
Greg Clayton1080edbc2011-03-25 18:03:16 +000055 DisassemblerCreateInstance create_callback = NULL;
56
57 if (plugin_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058 {
Greg Clayton57abc5d2013-05-10 21:47:16 +000059 ConstString const_plugin_name (plugin_name);
60 create_callback = PluginManager::GetDisassemblerCreateCallbackForPluginName (const_plugin_name);
Greg Clayton1080edbc2011-03-25 18:03:16 +000061 if (create_callback)
62 {
Jim Ingham0f063ba2013-03-02 00:26:47 +000063 DisassemblerSP disassembler_sp(create_callback(arch, flavor));
Greg Clayton1080edbc2011-03-25 18:03:16 +000064
Sean Callanan7e6d4e52012-08-01 18:50:59 +000065 if (disassembler_sp.get())
66 return disassembler_sp;
Greg Clayton1080edbc2011-03-25 18:03:16 +000067 }
68 }
69 else
70 {
71 for (uint32_t idx = 0; (create_callback = PluginManager::GetDisassemblerCreateCallbackAtIndex(idx)) != NULL; ++idx)
72 {
Jim Ingham0f063ba2013-03-02 00:26:47 +000073 DisassemblerSP disassembler_sp(create_callback(arch, flavor));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
Sean Callanan7e6d4e52012-08-01 18:50:59 +000075 if (disassembler_sp.get())
76 return disassembler_sp;
Greg Clayton1080edbc2011-03-25 18:03:16 +000077 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078 }
Sean Callanan7e6d4e52012-08-01 18:50:59 +000079 return DisassemblerSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080}
81
Jim Ingham0f063ba2013-03-02 00:26:47 +000082DisassemblerSP
83Disassembler::FindPluginForTarget(const TargetSP target_sp, const ArchSpec &arch, const char *flavor, const char *plugin_name)
84{
85 if (target_sp && flavor == NULL)
86 {
87 // FIXME - we don't have the mechanism in place to do per-architecture settings. But since we know that for now
88 // we only support flavors on x86 & x86_64,
89 if (arch.GetTriple().getArch() == llvm::Triple::x86
90 || arch.GetTriple().getArch() == llvm::Triple::x86_64)
91 flavor = target_sp->GetDisassemblyFlavor();
92 }
93 return FindPlugin(arch, flavor, plugin_name);
94}
95
Greg Claytondda4f7b2010-06-30 23:03:03 +000096
Greg Clayton357132e2011-03-26 19:14:58 +000097static void
98ResolveAddress (const ExecutionContext &exe_ctx,
99 const Address &addr,
100 Address &resolved_addr)
101{
102 if (!addr.IsSectionOffset())
103 {
104 // If we weren't passed in a section offset address range,
105 // try and resolve it to something
Greg Claytonc14ee322011-09-22 04:58:26 +0000106 Target *target = exe_ctx.GetTargetPtr();
107 if (target)
Greg Clayton357132e2011-03-26 19:14:58 +0000108 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000109 if (target->GetSectionLoadList().IsEmpty())
Greg Clayton357132e2011-03-26 19:14:58 +0000110 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000111 target->GetImages().ResolveFileAddress (addr.GetOffset(), resolved_addr);
Greg Clayton357132e2011-03-26 19:14:58 +0000112 }
113 else
114 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000115 target->GetSectionLoadList().ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Clayton357132e2011-03-26 19:14:58 +0000116 }
117 // We weren't able to resolve the address, just treat it as a
118 // raw address
119 if (resolved_addr.IsValid())
120 return;
121 }
122 }
123 resolved_addr = addr;
124}
Greg Claytondda4f7b2010-06-30 23:03:03 +0000125
126size_t
127Disassembler::Disassemble
128(
129 Debugger &debugger,
130 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000131 const char *plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000132 const char *flavor,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000133 const ExecutionContext &exe_ctx,
134 SymbolContextList &sc_list,
Jim Ingham37023b02011-03-22 01:48:42 +0000135 uint32_t num_instructions,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000136 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000137 uint32_t options,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000138 Stream &strm
139)
140{
141 size_t success_count = 0;
142 const size_t count = sc_list.GetSize();
143 SymbolContext sc;
144 AddressRange range;
Greg Clayton7e14f912011-04-23 02:04:55 +0000145 const uint32_t scope = eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
146 const bool use_inline_block_range = true;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000147 for (size_t i=0; i<count; ++i)
148 {
149 if (sc_list.GetContextAtIndex(i, sc) == false)
150 break;
Greg Clayton7e14f912011-04-23 02:04:55 +0000151 for (uint32_t range_idx = 0; sc.GetAddressRange(scope, range_idx, use_inline_block_range, range); ++range_idx)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000152 {
Greg Clayton1080edbc2011-03-25 18:03:16 +0000153 if (Disassemble (debugger,
154 arch,
155 plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000156 flavor,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000157 exe_ctx,
158 range,
159 num_instructions,
160 num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000161 options,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000162 strm))
Greg Claytondda4f7b2010-06-30 23:03:03 +0000163 {
164 ++success_count;
165 strm.EOL();
166 }
167 }
168 }
169 return success_count;
170}
171
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172bool
173Disassembler::Disassemble
174(
Greg Clayton66111032010-06-23 01:19:29 +0000175 Debugger &debugger,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000177 const char *plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000178 const char *flavor,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 const ExecutionContext &exe_ctx,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000180 const ConstString &name,
181 Module *module,
Jim Ingham37023b02011-03-22 01:48:42 +0000182 uint32_t num_instructions,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000183 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000184 uint32_t options,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185 Stream &strm
186)
187{
Greg Claytondda4f7b2010-06-30 23:03:03 +0000188 SymbolContextList sc_list;
Greg Clayton931180e2011-01-27 06:44:37 +0000189 if (name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190 {
Greg Clayton931180e2011-01-27 06:44:37 +0000191 const bool include_symbols = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +0000192 const bool include_inlines = true;
Greg Clayton931180e2011-01-27 06:44:37 +0000193 if (module)
194 {
Sean Callananb6d70eb2011-10-12 02:08:07 +0000195 module->FindFunctions (name,
196 NULL,
Greg Clayton6ecb2322013-05-18 00:11:21 +0000197 eFunctionNameTypeAuto,
Greg Clayton931180e2011-01-27 06:44:37 +0000198 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000199 include_inlines,
Greg Clayton931180e2011-01-27 06:44:37 +0000200 true,
201 sc_list);
202 }
Greg Claytonc14ee322011-09-22 04:58:26 +0000203 else if (exe_ctx.GetTargetPtr())
Greg Clayton931180e2011-01-27 06:44:37 +0000204 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000205 exe_ctx.GetTargetPtr()->GetImages().FindFunctions (name,
Greg Clayton6ecb2322013-05-18 00:11:21 +0000206 eFunctionNameTypeAuto,
Sean Callanan9df05fb2012-02-10 22:52:19 +0000207 include_symbols,
208 include_inlines,
Greg Claytonc14ee322011-09-22 04:58:26 +0000209 false,
210 sc_list);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000211 }
Greg Clayton931180e2011-01-27 06:44:37 +0000212 }
213
214 if (sc_list.GetSize ())
215 {
216 return Disassemble (debugger,
217 arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000218 plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000219 flavor,
Greg Clayton931180e2011-01-27 06:44:37 +0000220 exe_ctx,
Jim Ingham37023b02011-03-22 01:48:42 +0000221 sc_list,
222 num_instructions,
Greg Clayton931180e2011-01-27 06:44:37 +0000223 num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000224 options,
Greg Clayton931180e2011-01-27 06:44:37 +0000225 strm);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000226 }
227 return false;
228}
229
Greg Clayton1d273162010-10-06 03:09:58 +0000230
231lldb::DisassemblerSP
232Disassembler::DisassembleRange
233(
234 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000235 const char *plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000236 const char *flavor,
Greg Clayton1d273162010-10-06 03:09:58 +0000237 const ExecutionContext &exe_ctx,
238 const AddressRange &range
239)
240{
241 lldb::DisassemblerSP disasm_sp;
242 if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid())
243 {
Jim Ingham0f063ba2013-03-02 00:26:47 +0000244 disasm_sp = Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch, flavor, plugin_name);
Greg Clayton1d273162010-10-06 03:09:58 +0000245
246 if (disasm_sp)
247 {
Greg Clayton3faf47c2013-03-28 23:42:53 +0000248 const bool prefer_file_cache = false;
249 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range, NULL, prefer_file_cache);
Greg Clayton1d273162010-10-06 03:09:58 +0000250 if (bytes_disassembled == 0)
251 disasm_sp.reset();
252 }
253 }
254 return disasm_sp;
255}
256
Sean Callanan50952e92011-12-14 23:49:37 +0000257lldb::DisassemblerSP
Greg Clayton3faf47c2013-03-28 23:42:53 +0000258Disassembler::DisassembleBytes (const ArchSpec &arch,
259 const char *plugin_name,
260 const char *flavor,
261 const Address &start,
262 const void *src,
263 size_t src_len,
264 uint32_t num_instructions,
265 bool data_from_file)
Sean Callanan50952e92011-12-14 23:49:37 +0000266{
267 lldb::DisassemblerSP disasm_sp;
268
Greg Clayton3faf47c2013-03-28 23:42:53 +0000269 if (src)
Sean Callanan50952e92011-12-14 23:49:37 +0000270 {
Jim Ingham0f063ba2013-03-02 00:26:47 +0000271 disasm_sp = Disassembler::FindPlugin(arch, flavor, plugin_name);
Sean Callanan50952e92011-12-14 23:49:37 +0000272
273 if (disasm_sp)
274 {
Greg Clayton3faf47c2013-03-28 23:42:53 +0000275 DataExtractor data(src, src_len, arch.GetByteOrder(), arch.GetAddressByteSize());
Sean Callanan50952e92011-12-14 23:49:37 +0000276
277 (void)disasm_sp->DecodeInstructions (start,
278 data,
279 0,
Greg Clayton9c766112012-03-06 22:24:44 +0000280 num_instructions,
Greg Clayton3faf47c2013-03-28 23:42:53 +0000281 false,
282 data_from_file);
Sean Callanan50952e92011-12-14 23:49:37 +0000283 }
284 }
285
286 return disasm_sp;
287}
288
Greg Clayton1d273162010-10-06 03:09:58 +0000289
Greg Claytondda4f7b2010-06-30 23:03:03 +0000290bool
291Disassembler::Disassemble
292(
293 Debugger &debugger,
294 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000295 const char *plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000296 const char *flavor,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000297 const ExecutionContext &exe_ctx,
298 const AddressRange &disasm_range,
Jim Ingham37023b02011-03-22 01:48:42 +0000299 uint32_t num_instructions,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000300 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000301 uint32_t options,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000302 Stream &strm
303)
304{
305 if (disasm_range.GetByteSize())
306 {
Jim Ingham0f063ba2013-03-02 00:26:47 +0000307 lldb::DisassemblerSP disasm_sp (Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch, flavor, plugin_name));
Greg Claytondda4f7b2010-06-30 23:03:03 +0000308
Sean Callanan7e6d4e52012-08-01 18:50:59 +0000309 if (disasm_sp.get())
Greg Claytondda4f7b2010-06-30 23:03:03 +0000310 {
Greg Clayton357132e2011-03-26 19:14:58 +0000311 AddressRange range;
312 ResolveAddress (exe_ctx, disasm_range.GetBaseAddress(), range.GetBaseAddress());
313 range.SetByteSize (disasm_range.GetByteSize());
Greg Clayton3faf47c2013-03-28 23:42:53 +0000314 const bool prefer_file_cache = false;
315 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range, &strm, prefer_file_cache);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316 if (bytes_disassembled == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 return false;
Greg Clayton1080edbc2011-03-25 18:03:16 +0000318
Jim Ingham56d40422013-07-31 02:19:15 +0000319 bool result = PrintInstructions (disasm_sp.get(),
320 debugger,
321 arch,
322 exe_ctx,
323 num_instructions,
324 num_mixed_context_lines,
325 options,
326 strm);
327
328 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
329 // I'll fix that but for now, just clear the list and it will go away nicely.
330 disasm_sp->GetInstructionList().Clear();
331 return result;
Jim Ingham37023b02011-03-22 01:48:42 +0000332 }
333 }
334 return false;
335}
336
337bool
338Disassembler::Disassemble
339(
340 Debugger &debugger,
341 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000342 const char *plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000343 const char *flavor,
Jim Ingham37023b02011-03-22 01:48:42 +0000344 const ExecutionContext &exe_ctx,
345 const Address &start_address,
346 uint32_t num_instructions,
347 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000348 uint32_t options,
Jim Ingham37023b02011-03-22 01:48:42 +0000349 Stream &strm
350)
351{
352 if (num_instructions > 0)
353 {
Greg Clayton3faf47c2013-03-28 23:42:53 +0000354 lldb::DisassemblerSP disasm_sp (Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(),
355 arch,
356 flavor,
357 plugin_name));
Sean Callanan7e6d4e52012-08-01 18:50:59 +0000358 if (disasm_sp.get())
Jim Ingham37023b02011-03-22 01:48:42 +0000359 {
Greg Clayton357132e2011-03-26 19:14:58 +0000360 Address addr;
361 ResolveAddress (exe_ctx, start_address, addr);
Greg Clayton3faf47c2013-03-28 23:42:53 +0000362 const bool prefer_file_cache = false;
363 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx,
364 addr,
365 num_instructions,
366 prefer_file_cache);
Jim Ingham37023b02011-03-22 01:48:42 +0000367 if (bytes_disassembled == 0)
Jim Ingham37023b02011-03-22 01:48:42 +0000368 return false;
Jim Ingham56d40422013-07-31 02:19:15 +0000369 bool result = PrintInstructions (disasm_sp.get(),
370 debugger,
371 arch,
372 exe_ctx,
373 num_instructions,
374 num_mixed_context_lines,
375 options,
376 strm);
377
378 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
379 // I'll fix that but for now, just clear the list and it will go away nicely.
380 disasm_sp->GetInstructionList().Clear();
381 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383 }
384 return false;
385}
Jim Ingham37023b02011-03-22 01:48:42 +0000386
387bool
388Disassembler::PrintInstructions
389(
390 Disassembler *disasm_ptr,
Jim Ingham37023b02011-03-22 01:48:42 +0000391 Debugger &debugger,
392 const ArchSpec &arch,
393 const ExecutionContext &exe_ctx,
Jim Ingham37023b02011-03-22 01:48:42 +0000394 uint32_t num_instructions,
395 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000396 uint32_t options,
Jim Ingham37023b02011-03-22 01:48:42 +0000397 Stream &strm
398)
399{
400 // We got some things disassembled...
401 size_t num_instructions_found = disasm_ptr->GetInstructionList().GetSize();
402
403 if (num_instructions > 0 && num_instructions < num_instructions_found)
404 num_instructions_found = num_instructions;
405
Greg Clayton357132e2011-03-26 19:14:58 +0000406 const uint32_t max_opcode_byte_size = disasm_ptr->GetInstructionList().GetMaxOpcocdeByteSize ();
Jim Ingham37023b02011-03-22 01:48:42 +0000407 uint32_t offset = 0;
408 SymbolContext sc;
409 SymbolContext prev_sc;
410 AddressRange sc_range;
Greg Clayton34132752011-07-06 04:07:21 +0000411 const Address *pc_addr_ptr = NULL;
Greg Clayton7e14f912011-04-23 02:04:55 +0000412 ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope();
Greg Claytonc14ee322011-09-22 04:58:26 +0000413 StackFrame *frame = exe_ctx.GetFramePtr();
414
Michael Sartain4b2967f2013-07-08 17:56:02 +0000415 TargetSP target_sp (exe_ctx.GetTargetSP());
416 SourceManager &source_manager = target_sp ? target_sp->GetSourceManager() : debugger.GetSourceManager();
417
Greg Claytonc14ee322011-09-22 04:58:26 +0000418 if (frame)
419 pc_addr_ptr = &frame->GetFrameCodeAddress();
Greg Clayton7e14f912011-04-23 02:04:55 +0000420 const uint32_t scope = eSymbolContextLineEntry | eSymbolContextFunction | eSymbolContextSymbol;
421 const bool use_inline_block_range = false;
Jim Ingham37023b02011-03-22 01:48:42 +0000422 for (size_t i=0; i<num_instructions_found; ++i)
423 {
424 Instruction *inst = disasm_ptr->GetInstructionList().GetInstructionAtIndex (i).get();
425 if (inst)
426 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000427 const Address &addr = inst->GetAddress();
428 const bool inst_is_at_pc = pc_addr_ptr && addr == *pc_addr_ptr;
Jim Ingham37023b02011-03-22 01:48:42 +0000429
430 prev_sc = sc;
431
Greg Claytone72dfb32012-02-24 01:59:29 +0000432 ModuleSP module_sp (addr.GetModule());
433 if (module_sp)
Jim Ingham37023b02011-03-22 01:48:42 +0000434 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000435 uint32_t resolved_mask = module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, sc);
Jim Ingham37023b02011-03-22 01:48:42 +0000436 if (resolved_mask)
437 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000438 if (num_mixed_context_lines)
439 {
440 if (!sc_range.ContainsFileAddress (addr))
441 {
Greg Clayton7e14f912011-04-23 02:04:55 +0000442 sc.GetAddressRange (scope, 0, use_inline_block_range, sc_range);
Greg Clayton32e0a752011-03-30 18:16:51 +0000443
444 if (sc != prev_sc)
445 {
446 if (offset != 0)
447 strm.EOL();
448
Greg Claytonc14ee322011-09-22 04:58:26 +0000449 sc.DumpStopContext(&strm, exe_ctx.GetProcessPtr(), addr, false, true, false);
Greg Clayton32e0a752011-03-30 18:16:51 +0000450 strm.EOL();
451
452 if (sc.comp_unit && sc.line_entry.IsValid())
453 {
Michael Sartain4b2967f2013-07-08 17:56:02 +0000454 source_manager.DisplaySourceLinesWithLineNumbers (sc.line_entry.file,
455 sc.line_entry.line,
456 num_mixed_context_lines,
457 num_mixed_context_lines,
458 ((inst_is_at_pc && (options & eOptionMarkPCSourceLine)) ? "->" : ""),
459 &strm);
Greg Clayton32e0a752011-03-30 18:16:51 +0000460 }
461 }
462 }
463 }
Greg Clayton1bba2be2011-11-30 19:36:42 +0000464 else if ((sc.function || sc.symbol) && (sc.function != prev_sc.function || sc.symbol != prev_sc.symbol))
Jim Ingham37023b02011-03-22 01:48:42 +0000465 {
466 if (prev_sc.function || prev_sc.symbol)
467 strm.EOL();
468
Greg Clayton7e14f912011-04-23 02:04:55 +0000469 bool show_fullpaths = false;
470 bool show_module = true;
471 bool show_inlined_frames = true;
472 sc.DumpStopContext (&strm,
473 exe_scope,
474 addr,
475 show_fullpaths,
476 show_module,
477 show_inlined_frames);
Jim Ingham37023b02011-03-22 01:48:42 +0000478
Jim Ingham37023b02011-03-22 01:48:42 +0000479 strm << ":\n";
480 }
Jim Ingham37023b02011-03-22 01:48:42 +0000481 }
482 else
483 {
Greg Clayton72310352013-02-23 04:12:47 +0000484 sc.Clear(true);
Jim Ingham37023b02011-03-22 01:48:42 +0000485 }
486 }
Jim Ingham37023b02011-03-22 01:48:42 +0000487
Greg Claytonb10d72f2011-06-28 19:01:40 +0000488 if ((options & eOptionMarkPCAddress) && pc_addr_ptr)
Greg Clayton32e0a752011-03-30 18:16:51 +0000489 {
Greg Claytonb10d72f2011-06-28 19:01:40 +0000490 strm.PutCString(inst_is_at_pc ? "-> " : " ");
Greg Clayton32e0a752011-03-30 18:16:51 +0000491 }
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000492 const bool show_bytes = (options & eOptionShowBytes) != 0;
Greg Claytonba812f42012-05-10 02:52:23 +0000493 inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx);
Greg Clayton32e0a752011-03-30 18:16:51 +0000494 strm.EOL();
Jim Ingham37023b02011-03-22 01:48:42 +0000495 }
496 else
497 {
498 break;
499 }
500 }
Jim Ingham37023b02011-03-22 01:48:42 +0000501
502 return true;
503}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504
Greg Claytondda4f7b2010-06-30 23:03:03 +0000505
506bool
507Disassembler::Disassemble
508(
509 Debugger &debugger,
510 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000511 const char *plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000512 const char *flavor,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000513 const ExecutionContext &exe_ctx,
Jim Ingham37023b02011-03-22 01:48:42 +0000514 uint32_t num_instructions,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000515 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000516 uint32_t options,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000517 Stream &strm
518)
519{
520 AddressRange range;
Greg Claytonc14ee322011-09-22 04:58:26 +0000521 StackFrame *frame = exe_ctx.GetFramePtr();
522 if (frame)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000523 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000524 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
Greg Claytondda4f7b2010-06-30 23:03:03 +0000525 if (sc.function)
526 {
527 range = sc.function->GetAddressRange();
528 }
Greg Claytone7612132012-03-07 21:03:09 +0000529 else if (sc.symbol && sc.symbol->ValueIsAddress())
Greg Claytondda4f7b2010-06-30 23:03:03 +0000530 {
Greg Claytone7612132012-03-07 21:03:09 +0000531 range.GetBaseAddress() = sc.symbol->GetAddress();
532 range.SetByteSize (sc.symbol->GetByteSize());
Greg Claytondda4f7b2010-06-30 23:03:03 +0000533 }
534 else
535 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000536 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000537 }
538
539 if (range.GetBaseAddress().IsValid() && range.GetByteSize() == 0)
540 range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE);
541 }
542
Greg Clayton1080edbc2011-03-25 18:03:16 +0000543 return Disassemble (debugger,
544 arch,
545 plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000546 flavor,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000547 exe_ctx,
548 range,
549 num_instructions,
550 num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000551 options,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000552 strm);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000553}
554
Greg Clayton357132e2011-03-26 19:14:58 +0000555Instruction::Instruction(const Address &address, AddressClass addr_class) :
Greg Clayton1080edbc2011-03-25 18:03:16 +0000556 m_address (address),
Greg Clayton357132e2011-03-26 19:14:58 +0000557 m_address_class (addr_class),
Sean Callanana97aa922012-02-14 00:22:51 +0000558 m_opcode(),
559 m_calculated_strings(false)
Greg Clayton0ae96272011-03-24 23:53:38 +0000560{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561}
562
Greg Clayton1d273162010-10-06 03:09:58 +0000563Instruction::~Instruction()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564{
565}
566
Greg Clayton357132e2011-03-26 19:14:58 +0000567AddressClass
568Instruction::GetAddressClass ()
569{
570 if (m_address_class == eAddressClassInvalid)
571 m_address_class = m_address.GetAddressClass();
572 return m_address_class;
573}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574
Greg Claytonba812f42012-05-10 02:52:23 +0000575void
576Instruction::Dump (lldb_private::Stream *s,
577 uint32_t max_opcode_byte_size,
578 bool show_address,
579 bool show_bytes,
580 const ExecutionContext* exe_ctx)
581{
Jason Molenda7a37c1e2013-01-04 23:52:35 +0000582 size_t opcode_column_width = 7;
Greg Claytonba812f42012-05-10 02:52:23 +0000583 const size_t operand_column_width = 25;
584
585 CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx);
586
587 StreamString ss;
588
589 if (show_address)
590 {
591 m_address.Dump(&ss,
592 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL,
593 Address::DumpStyleLoadAddress,
594 Address::DumpStyleModuleWithFileAddress,
595 0);
596
597 ss.PutCString(": ");
598 }
599
600 if (show_bytes)
601 {
602 if (m_opcode.GetType() == Opcode::eTypeBytes)
603 {
604 // x86_64 and i386 are the only ones that use bytes right now so
605 // pad out the byte dump to be able to always show 15 bytes (3 chars each)
606 // plus a space
607 if (max_opcode_byte_size > 0)
608 m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
609 else
610 m_opcode.Dump (&ss, 15 * 3 + 1);
611 }
612 else
613 {
614 // Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces)
615 // plus two for padding...
616 if (max_opcode_byte_size > 0)
617 m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
618 else
619 m_opcode.Dump (&ss, 12);
620 }
621 }
622
623 const size_t opcode_pos = ss.GetSize();
624
Jason Molenda7a37c1e2013-01-04 23:52:35 +0000625 // The default opcode size of 7 characters is plenty for most architectures
626 // but some like arm can pull out the occasional vqrshrun.s16. We won't get
627 // consistent column spacing in these cases, unfortunately.
628 if (m_opcode_name.length() >= opcode_column_width)
629 {
630 opcode_column_width = m_opcode_name.length() + 1;
631 }
632
Greg Claytonba812f42012-05-10 02:52:23 +0000633 ss.PutCString (m_opcode_name.c_str());
634 ss.FillLastLineToColumn (opcode_pos + opcode_column_width, ' ');
Jim Ingham0f063ba2013-03-02 00:26:47 +0000635 ss.PutCString (m_mnemonics.c_str());
Greg Claytonba812f42012-05-10 02:52:23 +0000636
637 if (!m_comment.empty())
638 {
639 ss.FillLastLineToColumn (opcode_pos + opcode_column_width + operand_column_width, ' ');
640 ss.PutCString (" ; ");
641 ss.PutCString (m_comment.c_str());
642 }
643 s->Write (ss.GetData(), ss.GetSize());
644}
645
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000646bool
647Instruction::DumpEmulation (const ArchSpec &arch)
648{
Greg Clayton7b0992d2013-04-18 22:45:39 +0000649 std::unique_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000650 if (insn_emulator_ap.get())
651 {
Greg Clayton2ed751b2011-04-26 04:39:08 +0000652 insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL);
653 return insn_emulator_ap->EvaluateInstruction (0);
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000654 }
655
656 return false;
657}
658
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000659OptionValueSP
660Instruction::ReadArray (FILE *in_file, Stream *out_stream, OptionValue::Type data_type)
661{
662 bool done = false;
663 char buffer[1024];
664
665 OptionValueSP option_value_sp (new OptionValueArray (1u << data_type));
666
667 int idx = 0;
668 while (!done)
669 {
670 if (!fgets (buffer, 1023, in_file))
671 {
Greg Clayton762f7132011-09-18 18:59:15 +0000672 out_stream->Printf ("Instruction::ReadArray: Error reading file (fgets).\n");
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000673 option_value_sp.reset ();
674 return option_value_sp;
675 }
676
677 std::string line (buffer);
678
Greg Claytonc7bece562013-01-25 18:06:21 +0000679 size_t len = line.size();
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000680 if (line[len-1] == '\n')
681 {
682 line[len-1] = '\0';
683 line.resize (len-1);
684 }
685
686 if ((line.size() == 1) && line[0] == ']')
687 {
688 done = true;
689 line.clear();
690 }
691
692 if (line.size() > 0)
693 {
694 std::string value;
Greg Claytonbc43cab2013-04-03 21:37:16 +0000695 static RegularExpression g_reg_exp ("^[ \t]*([^ \t]+)[ \t]*$");
696 RegularExpression::Match regex_match(1);
697 bool reg_exp_success = g_reg_exp.Execute (line.c_str(), &regex_match);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000698 if (reg_exp_success)
Greg Claytonbc43cab2013-04-03 21:37:16 +0000699 regex_match.GetMatchAtIndex (line.c_str(), 1, value);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000700 else
701 value = line;
702
703 OptionValueSP data_value_sp;
704 switch (data_type)
705 {
706 case OptionValue::eTypeUInt64:
707 data_value_sp.reset (new OptionValueUInt64 (0, 0));
708 data_value_sp->SetValueFromCString (value.c_str());
709 break;
710 // Other types can be added later as needed.
711 default:
712 data_value_sp.reset (new OptionValueString (value.c_str(), ""));
713 break;
714 }
715
Greg Clayton84c39662011-04-27 22:04:39 +0000716 option_value_sp->GetAsArray()->InsertValue (idx, data_value_sp);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000717 ++idx;
718 }
719 }
720
721 return option_value_sp;
722}
723
724OptionValueSP
725Instruction::ReadDictionary (FILE *in_file, Stream *out_stream)
726{
727 bool done = false;
728 char buffer[1024];
729
730 OptionValueSP option_value_sp (new OptionValueDictionary());
731 static ConstString encoding_key ("data_encoding");
732 OptionValue::Type data_type = OptionValue::eTypeInvalid;
733
734
735 while (!done)
736 {
737 // Read the next line in the file
738 if (!fgets (buffer, 1023, in_file))
739 {
740 out_stream->Printf ("Instruction::ReadDictionary: Error reading file (fgets).\n");
741 option_value_sp.reset ();
742 return option_value_sp;
743 }
744
745 // Check to see if the line contains the end-of-dictionary marker ("}")
746 std::string line (buffer);
747
Greg Claytonc7bece562013-01-25 18:06:21 +0000748 size_t len = line.size();
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000749 if (line[len-1] == '\n')
750 {
751 line[len-1] = '\0';
752 line.resize (len-1);
753 }
754
755 if ((line.size() == 1) && (line[0] == '}'))
756 {
757 done = true;
758 line.clear();
759 }
760
761 // Try to find a key-value pair in the current line and add it to the dictionary.
762 if (line.size() > 0)
763 {
Greg Claytonbc43cab2013-04-03 21:37:16 +0000764 static RegularExpression g_reg_exp ("^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*=[ \t]*(.*)[ \t]*$");
765 RegularExpression::Match regex_match(2);
766
767 bool reg_exp_success = g_reg_exp.Execute (line.c_str(), &regex_match);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000768 std::string key;
769 std::string value;
770 if (reg_exp_success)
771 {
Greg Claytonbc43cab2013-04-03 21:37:16 +0000772 regex_match.GetMatchAtIndex (line.c_str(), 1, key);
773 regex_match.GetMatchAtIndex (line.c_str(), 2, value);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000774 }
775 else
776 {
777 out_stream->Printf ("Instruction::ReadDictionary: Failure executing regular expression.\n");
778 option_value_sp.reset();
779 return option_value_sp;
780 }
781
782 ConstString const_key (key.c_str());
783 // Check value to see if it's the start of an array or dictionary.
784
785 lldb::OptionValueSP value_sp;
786 assert (value.empty() == false);
787 assert (key.empty() == false);
788
789 if (value[0] == '{')
790 {
791 assert (value.size() == 1);
792 // value is a dictionary
793 value_sp = ReadDictionary (in_file, out_stream);
794 if (value_sp.get() == NULL)
795 {
796 option_value_sp.reset ();
797 return option_value_sp;
798 }
799 }
800 else if (value[0] == '[')
801 {
802 assert (value.size() == 1);
803 // value is an array
804 value_sp = ReadArray (in_file, out_stream, data_type);
805 if (value_sp.get() == NULL)
806 {
807 option_value_sp.reset ();
808 return option_value_sp;
809 }
810 // We've used the data_type to read an array; re-set the type to Invalid
811 data_type = OptionValue::eTypeInvalid;
812 }
813 else if ((value[0] == '0') && (value[1] == 'x'))
814 {
815 value_sp.reset (new OptionValueUInt64 (0, 0));
816 value_sp->SetValueFromCString (value.c_str());
817 }
818 else
819 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000820 size_t len = value.size();
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000821 if ((value[0] == '"') && (value[len-1] == '"'))
822 value = value.substr (1, len-2);
823 value_sp.reset (new OptionValueString (value.c_str(), ""));
824 }
825
826
827
828 if (const_key == encoding_key)
829 {
830 // A 'data_encoding=..." is NOT a normal key-value pair; it is meta-data indicating the
831 // data type of an upcoming array (usually the next bit of data to be read in).
832 if (strcmp (value.c_str(), "uint32_t") == 0)
833 data_type = OptionValue::eTypeUInt64;
834 }
835 else
Greg Clayton84c39662011-04-27 22:04:39 +0000836 option_value_sp->GetAsDictionary()->SetValueForKey (const_key, value_sp, false);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000837 }
838 }
839
840 return option_value_sp;
841}
842
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000843bool
Caroline Tice3ac67112011-04-19 23:30:03 +0000844Instruction::TestEmulation (Stream *out_stream, const char *file_name)
845{
846 if (!out_stream)
847 return false;
848
849 if (!file_name)
850 {
Johnny Chenea80ba82011-04-21 20:27:45 +0000851 out_stream->Printf ("Instruction::TestEmulation: Missing file_name.");
Caroline Tice3ac67112011-04-19 23:30:03 +0000852 return false;
853 }
854
855 FILE *test_file = fopen (file_name, "r");
856 if (!test_file)
857 {
Johnny Chenea80ba82011-04-21 20:27:45 +0000858 out_stream->Printf ("Instruction::TestEmulation: Attempt to open test file failed.");
Caroline Tice3ac67112011-04-19 23:30:03 +0000859 return false;
860 }
861
Caroline Tice3ac67112011-04-19 23:30:03 +0000862 char buffer[256];
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000863 if (!fgets (buffer, 255, test_file))
Caroline Tice3ac67112011-04-19 23:30:03 +0000864 {
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000865 out_stream->Printf ("Instruction::TestEmulation: Error reading first line of test file.\n");
Caroline Tice3ac67112011-04-19 23:30:03 +0000866 fclose (test_file);
867 return false;
868 }
869
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000870 if (strncmp (buffer, "InstructionEmulationState={", 27) != 0)
871 {
872 out_stream->Printf ("Instructin::TestEmulation: Test file does not contain emulation state dictionary\n");
873 fclose (test_file);
874 return false;
875 }
876
877 // Read all the test information from the test file into an OptionValueDictionary.
878
879 OptionValueSP data_dictionary_sp (ReadDictionary (test_file, out_stream));
880 if (data_dictionary_sp.get() == NULL)
881 {
882 out_stream->Printf ("Instruction::TestEmulation: Error reading Dictionary Object.\n");
883 fclose (test_file);
884 return false;
885 }
886
887 fclose (test_file);
888
Greg Clayton84c39662011-04-27 22:04:39 +0000889 OptionValueDictionary *data_dictionary = data_dictionary_sp->GetAsDictionary();
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000890 static ConstString description_key ("assembly_string");
891 static ConstString triple_key ("triple");
892
893 OptionValueSP value_sp = data_dictionary->GetValueForKey (description_key);
894
895 if (value_sp.get() == NULL)
896 {
897 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain description string.\n");
898 return false;
899 }
900
901 SetDescription (value_sp->GetStringValue());
902
903
904 value_sp = data_dictionary->GetValueForKey (triple_key);
905 if (value_sp.get() == NULL)
906 {
907 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain triple.\n");
908 return false;
909 }
910
911 ArchSpec arch;
912 arch.SetTriple (llvm::Triple (value_sp->GetStringValue()));
Caroline Tice3ac67112011-04-19 23:30:03 +0000913
914 bool success = false;
Greg Clayton7b0992d2013-04-18 22:45:39 +0000915 std::unique_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
Caroline Tice3ac67112011-04-19 23:30:03 +0000916 if (insn_emulator_ap.get())
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000917 success = insn_emulator_ap->TestEmulation (out_stream, arch, data_dictionary);
Caroline Tice3ac67112011-04-19 23:30:03 +0000918
Caroline Tice3ac67112011-04-19 23:30:03 +0000919 if (success)
Johnny Chenea80ba82011-04-21 20:27:45 +0000920 out_stream->Printf ("Emulation test succeeded.");
Caroline Tice3ac67112011-04-19 23:30:03 +0000921 else
Johnny Chenea80ba82011-04-21 20:27:45 +0000922 out_stream->Printf ("Emulation test failed.");
Caroline Tice3ac67112011-04-19 23:30:03 +0000923
924 return success;
925}
926
927bool
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000928Instruction::Emulate (const ArchSpec &arch,
Greg Clayton2ed751b2011-04-26 04:39:08 +0000929 uint32_t evaluate_options,
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000930 void *baton,
Greg Clayton7349bd92011-05-09 20:18:18 +0000931 EmulateInstruction::ReadMemoryCallback read_mem_callback,
932 EmulateInstruction::WriteMemoryCallback write_mem_callback,
933 EmulateInstruction::ReadRegisterCallback read_reg_callback,
934 EmulateInstruction::WriteRegisterCallback write_reg_callback)
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000935{
Greg Clayton7b0992d2013-04-18 22:45:39 +0000936 std::unique_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000937 if (insn_emulator_ap.get())
938 {
939 insn_emulator_ap->SetBaton (baton);
940 insn_emulator_ap->SetCallbacks (read_mem_callback, write_mem_callback, read_reg_callback, write_reg_callback);
Greg Clayton2ed751b2011-04-26 04:39:08 +0000941 insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL);
942 return insn_emulator_ap->EvaluateInstruction (evaluate_options);
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000943 }
944
945 return false;
946}
947
Greg Claytonba812f42012-05-10 02:52:23 +0000948
949uint32_t
950Instruction::GetData (DataExtractor &data)
951{
Sean Callanancd4ae1a2012-08-07 01:44:58 +0000952 return m_opcode.GetData(data);
Greg Claytonba812f42012-05-10 02:52:23 +0000953}
954
Greg Clayton1d273162010-10-06 03:09:58 +0000955InstructionList::InstructionList() :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956 m_instructions()
957{
958}
959
Greg Clayton1d273162010-10-06 03:09:58 +0000960InstructionList::~InstructionList()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961{
962}
963
964size_t
Greg Clayton1d273162010-10-06 03:09:58 +0000965InstructionList::GetSize() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966{
967 return m_instructions.size();
968}
969
Greg Clayton357132e2011-03-26 19:14:58 +0000970uint32_t
971InstructionList::GetMaxOpcocdeByteSize () const
972{
973 uint32_t max_inst_size = 0;
974 collection::const_iterator pos, end;
975 for (pos = m_instructions.begin(), end = m_instructions.end();
976 pos != end;
977 ++pos)
978 {
979 uint32_t inst_size = (*pos)->GetOpcode().GetByteSize();
980 if (max_inst_size < inst_size)
981 max_inst_size = inst_size;
982 }
983 return max_inst_size;
984}
985
986
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987
Greg Clayton1d273162010-10-06 03:09:58 +0000988InstructionSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000989InstructionList::GetInstructionAtIndex (size_t idx) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000990{
Greg Clayton1d273162010-10-06 03:09:58 +0000991 InstructionSP inst_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992 if (idx < m_instructions.size())
Greg Clayton1d273162010-10-06 03:09:58 +0000993 inst_sp = m_instructions[idx];
994 return inst_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995}
996
997void
Greg Clayton5009f9d2011-10-27 17:55:14 +0000998InstructionList::Dump (Stream *s,
999 bool show_address,
1000 bool show_bytes,
1001 const ExecutionContext* exe_ctx)
1002{
1003 const uint32_t max_opcode_byte_size = GetMaxOpcocdeByteSize();
1004 collection::const_iterator pos, begin, end;
1005 for (begin = m_instructions.begin(), end = m_instructions.end(), pos = begin;
1006 pos != end;
1007 ++pos)
1008 {
1009 if (pos != begin)
1010 s->EOL();
Greg Claytonba812f42012-05-10 02:52:23 +00001011 (*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx);
Greg Clayton5009f9d2011-10-27 17:55:14 +00001012 }
1013}
1014
1015
1016void
Greg Clayton1d273162010-10-06 03:09:58 +00001017InstructionList::Clear()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018{
1019 m_instructions.clear();
1020}
1021
1022void
Greg Clayton1d273162010-10-06 03:09:58 +00001023InstructionList::Append (lldb::InstructionSP &inst_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001024{
1025 if (inst_sp)
1026 m_instructions.push_back(inst_sp);
1027}
1028
Jim Ingham564d8bc22012-03-09 04:10:47 +00001029uint32_t
1030InstructionList::GetIndexOfNextBranchInstruction(uint32_t start) const
1031{
1032 size_t num_instructions = m_instructions.size();
1033
1034 uint32_t next_branch = UINT32_MAX;
1035 for (size_t i = start; i < num_instructions; i++)
1036 {
1037 if (m_instructions[i]->DoesBranch())
1038 {
1039 next_branch = i;
1040 break;
1041 }
1042 }
1043 return next_branch;
1044}
1045
1046uint32_t
1047InstructionList::GetIndexOfInstructionAtLoadAddress (lldb::addr_t load_addr, Target &target)
1048{
1049 Address address;
1050 address.SetLoadAddress(load_addr, &target);
Greg Claytonc7bece562013-01-25 18:06:21 +00001051 size_t num_instructions = m_instructions.size();
Jim Ingham564d8bc22012-03-09 04:10:47 +00001052 uint32_t index = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001053 for (size_t i = 0; i < num_instructions; i++)
Jim Ingham564d8bc22012-03-09 04:10:47 +00001054 {
1055 if (m_instructions[i]->GetAddress() == address)
1056 {
1057 index = i;
1058 break;
1059 }
1060 }
1061 return index;
1062}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063
1064size_t
Greg Clayton3faf47c2013-03-28 23:42:53 +00001065Disassembler::ParseInstructions (const ExecutionContext *exe_ctx,
1066 const AddressRange &range,
1067 Stream *error_strm_ptr,
1068 bool prefer_file_cache)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001069{
Greg Claytonc14ee322011-09-22 04:58:26 +00001070 if (exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001071 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001072 Target *target = exe_ctx->GetTargetPtr();
1073 const addr_t byte_size = range.GetByteSize();
1074 if (target == NULL || byte_size == 0 || !range.GetBaseAddress().IsValid())
1075 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076
Greg Claytonc14ee322011-09-22 04:58:26 +00001077 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
1078 DataBufferSP data_sp(heap_buffer);
1079
1080 Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00001081 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1082 const size_t bytes_read = target->ReadMemory (range.GetBaseAddress(),
Greg Claytonc14ee322011-09-22 04:58:26 +00001083 prefer_file_cache,
1084 heap_buffer->GetBytes(),
1085 heap_buffer->GetByteSize(),
Greg Clayton3faf47c2013-03-28 23:42:53 +00001086 error,
1087 &load_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001088
1089 if (bytes_read > 0)
1090 {
1091 if (bytes_read != heap_buffer->GetByteSize())
1092 heap_buffer->SetByteSize (bytes_read);
1093 DataExtractor data (data_sp,
1094 m_arch.GetByteOrder(),
1095 m_arch.GetAddressByteSize());
Greg Clayton3faf47c2013-03-28 23:42:53 +00001096 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1097 return DecodeInstructions (range.GetBaseAddress(), data, 0, UINT32_MAX, false, data_from_file);
Greg Claytonc14ee322011-09-22 04:58:26 +00001098 }
Greg Clayton57f06302012-05-25 17:05:55 +00001099 else if (error_strm_ptr)
1100 {
1101 const char *error_cstr = error.AsCString();
1102 if (error_cstr)
1103 {
1104 error_strm_ptr->Printf("error: %s\n", error_cstr);
1105 }
1106 }
1107 }
1108 else if (error_strm_ptr)
1109 {
1110 error_strm_ptr->PutCString("error: invalid execution context\n");
Greg Claytonc14ee322011-09-22 04:58:26 +00001111 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001112 return 0;
1113}
1114
Jim Ingham37023b02011-03-22 01:48:42 +00001115size_t
Greg Clayton3faf47c2013-03-28 23:42:53 +00001116Disassembler::ParseInstructions (const ExecutionContext *exe_ctx,
1117 const Address &start,
1118 uint32_t num_instructions,
1119 bool prefer_file_cache)
Jim Ingham37023b02011-03-22 01:48:42 +00001120{
Greg Clayton357132e2011-03-26 19:14:58 +00001121 m_instruction_list.Clear();
1122
Greg Claytonc14ee322011-09-22 04:58:26 +00001123 if (exe_ctx == NULL || num_instructions == 0 || !start.IsValid())
Jim Ingham37023b02011-03-22 01:48:42 +00001124 return 0;
1125
Greg Claytonc14ee322011-09-22 04:58:26 +00001126 Target *target = exe_ctx->GetTargetPtr();
Greg Clayton357132e2011-03-26 19:14:58 +00001127 // Calculate the max buffer size we will need in order to disassemble
1128 const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize();
Jim Ingham37023b02011-03-22 01:48:42 +00001129
Greg Clayton357132e2011-03-26 19:14:58 +00001130 if (target == NULL || byte_size == 0)
Jim Ingham37023b02011-03-22 01:48:42 +00001131 return 0;
1132
1133 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
Greg Clayton357132e2011-03-26 19:14:58 +00001134 DataBufferSP data_sp (heap_buffer);
Jim Ingham37023b02011-03-22 01:48:42 +00001135
1136 Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00001137 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1138 const size_t bytes_read = target->ReadMemory (start,
Greg Clayton357132e2011-03-26 19:14:58 +00001139 prefer_file_cache,
1140 heap_buffer->GetBytes(),
1141 byte_size,
Greg Clayton3faf47c2013-03-28 23:42:53 +00001142 error,
1143 &load_addr);
1144
1145 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001146
1147 if (bytes_read == 0)
1148 return 0;
1149 DataExtractor data (data_sp,
1150 m_arch.GetByteOrder(),
1151 m_arch.GetAddressByteSize());
1152
1153 const bool append_instructions = true;
1154 DecodeInstructions (start,
1155 data,
1156 0,
1157 num_instructions,
Greg Clayton3faf47c2013-03-28 23:42:53 +00001158 append_instructions,
1159 data_from_file);
Greg Clayton357132e2011-03-26 19:14:58 +00001160
Jim Ingham37023b02011-03-22 01:48:42 +00001161 return m_instruction_list.GetSize();
1162}
1163
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001164//----------------------------------------------------------------------
1165// Disassembler copy constructor
1166//----------------------------------------------------------------------
Jim Ingham0f063ba2013-03-02 00:26:47 +00001167Disassembler::Disassembler(const ArchSpec& arch, const char *flavor) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001168 m_arch (arch),
1169 m_instruction_list(),
Jim Ingham0f063ba2013-03-02 00:26:47 +00001170 m_base_addr(LLDB_INVALID_ADDRESS),
1171 m_flavor ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001172{
Jim Ingham0f063ba2013-03-02 00:26:47 +00001173 if (flavor == NULL)
1174 m_flavor.assign("default");
1175 else
1176 m_flavor.assign(flavor);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177}
1178
1179//----------------------------------------------------------------------
1180// Destructor
1181//----------------------------------------------------------------------
1182Disassembler::~Disassembler()
1183{
1184}
1185
Greg Clayton1d273162010-10-06 03:09:58 +00001186InstructionList &
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187Disassembler::GetInstructionList ()
1188{
1189 return m_instruction_list;
1190}
1191
Greg Clayton1d273162010-10-06 03:09:58 +00001192const InstructionList &
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001193Disassembler::GetInstructionList () const
1194{
1195 return m_instruction_list;
1196}
Caroline Tice3ac67112011-04-19 23:30:03 +00001197
1198//----------------------------------------------------------------------
1199// Class PseudoInstruction
1200//----------------------------------------------------------------------
1201PseudoInstruction::PseudoInstruction () :
1202 Instruction (Address(), eAddressClassUnknown),
1203 m_description ()
1204{
1205}
1206
1207PseudoInstruction::~PseudoInstruction ()
1208{
1209}
1210
Caroline Tice3ac67112011-04-19 23:30:03 +00001211bool
Jim Ingham32ce20c2013-03-13 01:55:16 +00001212PseudoInstruction::DoesBranch ()
Caroline Tice3ac67112011-04-19 23:30:03 +00001213{
1214 // This is NOT a valid question for a pseudo instruction.
1215 return false;
1216}
1217
1218size_t
1219PseudoInstruction::Decode (const lldb_private::Disassembler &disassembler,
1220 const lldb_private::DataExtractor &data,
Greg Claytonc7bece562013-01-25 18:06:21 +00001221 lldb::offset_t data_offset)
Caroline Tice3ac67112011-04-19 23:30:03 +00001222{
1223 return m_opcode.GetByteSize();
1224}
1225
1226
1227void
1228PseudoInstruction::SetOpcode (size_t opcode_size, void *opcode_data)
1229{
1230 if (!opcode_data)
1231 return;
1232
1233 switch (opcode_size)
1234 {
1235 case 8:
1236 {
1237 uint8_t value8 = *((uint8_t *) opcode_data);
1238 m_opcode.SetOpcode8 (value8);
1239 break;
1240 }
1241 case 16:
1242 {
1243 uint16_t value16 = *((uint16_t *) opcode_data);
1244 m_opcode.SetOpcode16 (value16);
1245 break;
1246 }
1247 case 32:
1248 {
1249 uint32_t value32 = *((uint32_t *) opcode_data);
1250 m_opcode.SetOpcode32 (value32);
1251 break;
1252 }
1253 case 64:
1254 {
1255 uint64_t value64 = *((uint64_t *) opcode_data);
1256 m_opcode.SetOpcode64 (value64);
1257 break;
1258 }
1259 default:
1260 break;
1261 }
1262}
1263
1264void
1265PseudoInstruction::SetDescription (const char *description)
1266{
1267 if (description && strlen (description) > 0)
1268 m_description = description;
1269}