blob: 0f5ed872745f33a9902643dfcde3b964b1c67e7e [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
Sean Callanan7e6d4e52012-08-01 18:50:59 +0000319 return PrintInstructions (disasm_sp.get(),
Greg Clayton1080edbc2011-03-25 18:03:16 +0000320 debugger,
321 arch,
322 exe_ctx,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000323 num_instructions,
324 num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000325 options,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000326 strm);
Jim Ingham37023b02011-03-22 01:48:42 +0000327 }
328 }
329 return false;
330}
331
332bool
333Disassembler::Disassemble
334(
335 Debugger &debugger,
336 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000337 const char *plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000338 const char *flavor,
Jim Ingham37023b02011-03-22 01:48:42 +0000339 const ExecutionContext &exe_ctx,
340 const Address &start_address,
341 uint32_t num_instructions,
342 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000343 uint32_t options,
Jim Ingham37023b02011-03-22 01:48:42 +0000344 Stream &strm
345)
346{
347 if (num_instructions > 0)
348 {
Greg Clayton3faf47c2013-03-28 23:42:53 +0000349 lldb::DisassemblerSP disasm_sp (Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(),
350 arch,
351 flavor,
352 plugin_name));
Sean Callanan7e6d4e52012-08-01 18:50:59 +0000353 if (disasm_sp.get())
Jim Ingham37023b02011-03-22 01:48:42 +0000354 {
Greg Clayton357132e2011-03-26 19:14:58 +0000355 Address addr;
356 ResolveAddress (exe_ctx, start_address, addr);
Greg Clayton3faf47c2013-03-28 23:42:53 +0000357 const bool prefer_file_cache = false;
358 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx,
359 addr,
360 num_instructions,
361 prefer_file_cache);
Jim Ingham37023b02011-03-22 01:48:42 +0000362 if (bytes_disassembled == 0)
Jim Ingham37023b02011-03-22 01:48:42 +0000363 return false;
Sean Callanan7e6d4e52012-08-01 18:50:59 +0000364 return PrintInstructions (disasm_sp.get(),
Greg Clayton1080edbc2011-03-25 18:03:16 +0000365 debugger,
366 arch,
367 exe_ctx,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000368 num_instructions,
369 num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000370 options,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000371 strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 }
374 return false;
375}
Jim Ingham37023b02011-03-22 01:48:42 +0000376
377bool
378Disassembler::PrintInstructions
379(
380 Disassembler *disasm_ptr,
Jim Ingham37023b02011-03-22 01:48:42 +0000381 Debugger &debugger,
382 const ArchSpec &arch,
383 const ExecutionContext &exe_ctx,
Jim Ingham37023b02011-03-22 01:48:42 +0000384 uint32_t num_instructions,
385 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000386 uint32_t options,
Jim Ingham37023b02011-03-22 01:48:42 +0000387 Stream &strm
388)
389{
390 // We got some things disassembled...
391 size_t num_instructions_found = disasm_ptr->GetInstructionList().GetSize();
392
393 if (num_instructions > 0 && num_instructions < num_instructions_found)
394 num_instructions_found = num_instructions;
395
Greg Clayton357132e2011-03-26 19:14:58 +0000396 const uint32_t max_opcode_byte_size = disasm_ptr->GetInstructionList().GetMaxOpcocdeByteSize ();
Jim Ingham37023b02011-03-22 01:48:42 +0000397 uint32_t offset = 0;
398 SymbolContext sc;
399 SymbolContext prev_sc;
400 AddressRange sc_range;
Greg Clayton34132752011-07-06 04:07:21 +0000401 const Address *pc_addr_ptr = NULL;
Greg Clayton7e14f912011-04-23 02:04:55 +0000402 ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope();
Greg Claytonc14ee322011-09-22 04:58:26 +0000403 StackFrame *frame = exe_ctx.GetFramePtr();
404
405 if (frame)
406 pc_addr_ptr = &frame->GetFrameCodeAddress();
Greg Clayton7e14f912011-04-23 02:04:55 +0000407 const uint32_t scope = eSymbolContextLineEntry | eSymbolContextFunction | eSymbolContextSymbol;
408 const bool use_inline_block_range = false;
Jim Ingham37023b02011-03-22 01:48:42 +0000409 for (size_t i=0; i<num_instructions_found; ++i)
410 {
411 Instruction *inst = disasm_ptr->GetInstructionList().GetInstructionAtIndex (i).get();
412 if (inst)
413 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000414 const Address &addr = inst->GetAddress();
415 const bool inst_is_at_pc = pc_addr_ptr && addr == *pc_addr_ptr;
Jim Ingham37023b02011-03-22 01:48:42 +0000416
417 prev_sc = sc;
418
Greg Claytone72dfb32012-02-24 01:59:29 +0000419 ModuleSP module_sp (addr.GetModule());
420 if (module_sp)
Jim Ingham37023b02011-03-22 01:48:42 +0000421 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000422 uint32_t resolved_mask = module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, sc);
Jim Ingham37023b02011-03-22 01:48:42 +0000423 if (resolved_mask)
424 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000425 if (num_mixed_context_lines)
426 {
427 if (!sc_range.ContainsFileAddress (addr))
428 {
Greg Clayton7e14f912011-04-23 02:04:55 +0000429 sc.GetAddressRange (scope, 0, use_inline_block_range, sc_range);
Greg Clayton32e0a752011-03-30 18:16:51 +0000430
431 if (sc != prev_sc)
432 {
433 if (offset != 0)
434 strm.EOL();
435
Greg Claytonc14ee322011-09-22 04:58:26 +0000436 sc.DumpStopContext(&strm, exe_ctx.GetProcessPtr(), addr, false, true, false);
Greg Clayton32e0a752011-03-30 18:16:51 +0000437 strm.EOL();
438
439 if (sc.comp_unit && sc.line_entry.IsValid())
440 {
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000441 debugger.GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.line_entry.file,
Greg Clayton32e0a752011-03-30 18:16:51 +0000442 sc.line_entry.line,
443 num_mixed_context_lines,
444 num_mixed_context_lines,
Greg Claytonb10d72f2011-06-28 19:01:40 +0000445 ((inst_is_at_pc && (options & eOptionMarkPCSourceLine)) ? "->" : ""),
Greg Clayton32e0a752011-03-30 18:16:51 +0000446 &strm);
447 }
448 }
449 }
450 }
Greg Clayton1bba2be2011-11-30 19:36:42 +0000451 else if ((sc.function || sc.symbol) && (sc.function != prev_sc.function || sc.symbol != prev_sc.symbol))
Jim Ingham37023b02011-03-22 01:48:42 +0000452 {
453 if (prev_sc.function || prev_sc.symbol)
454 strm.EOL();
455
Greg Clayton7e14f912011-04-23 02:04:55 +0000456 bool show_fullpaths = false;
457 bool show_module = true;
458 bool show_inlined_frames = true;
459 sc.DumpStopContext (&strm,
460 exe_scope,
461 addr,
462 show_fullpaths,
463 show_module,
464 show_inlined_frames);
Jim Ingham37023b02011-03-22 01:48:42 +0000465
Jim Ingham37023b02011-03-22 01:48:42 +0000466 strm << ":\n";
467 }
Jim Ingham37023b02011-03-22 01:48:42 +0000468 }
469 else
470 {
Greg Clayton72310352013-02-23 04:12:47 +0000471 sc.Clear(true);
Jim Ingham37023b02011-03-22 01:48:42 +0000472 }
473 }
Jim Ingham37023b02011-03-22 01:48:42 +0000474
Greg Claytonb10d72f2011-06-28 19:01:40 +0000475 if ((options & eOptionMarkPCAddress) && pc_addr_ptr)
Greg Clayton32e0a752011-03-30 18:16:51 +0000476 {
Greg Claytonb10d72f2011-06-28 19:01:40 +0000477 strm.PutCString(inst_is_at_pc ? "-> " : " ");
Greg Clayton32e0a752011-03-30 18:16:51 +0000478 }
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000479 const bool show_bytes = (options & eOptionShowBytes) != 0;
Greg Claytonba812f42012-05-10 02:52:23 +0000480 inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx);
Greg Clayton32e0a752011-03-30 18:16:51 +0000481 strm.EOL();
Jim Ingham37023b02011-03-22 01:48:42 +0000482 }
483 else
484 {
485 break;
486 }
487 }
Jim Ingham37023b02011-03-22 01:48:42 +0000488
489 return true;
490}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491
Greg Claytondda4f7b2010-06-30 23:03:03 +0000492
493bool
494Disassembler::Disassemble
495(
496 Debugger &debugger,
497 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000498 const char *plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000499 const char *flavor,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000500 const ExecutionContext &exe_ctx,
Jim Ingham37023b02011-03-22 01:48:42 +0000501 uint32_t num_instructions,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000502 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000503 uint32_t options,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000504 Stream &strm
505)
506{
507 AddressRange range;
Greg Claytonc14ee322011-09-22 04:58:26 +0000508 StackFrame *frame = exe_ctx.GetFramePtr();
509 if (frame)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000510 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000511 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
Greg Claytondda4f7b2010-06-30 23:03:03 +0000512 if (sc.function)
513 {
514 range = sc.function->GetAddressRange();
515 }
Greg Claytone7612132012-03-07 21:03:09 +0000516 else if (sc.symbol && sc.symbol->ValueIsAddress())
Greg Claytondda4f7b2010-06-30 23:03:03 +0000517 {
Greg Claytone7612132012-03-07 21:03:09 +0000518 range.GetBaseAddress() = sc.symbol->GetAddress();
519 range.SetByteSize (sc.symbol->GetByteSize());
Greg Claytondda4f7b2010-06-30 23:03:03 +0000520 }
521 else
522 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000523 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000524 }
525
526 if (range.GetBaseAddress().IsValid() && range.GetByteSize() == 0)
527 range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE);
528 }
529
Greg Clayton1080edbc2011-03-25 18:03:16 +0000530 return Disassemble (debugger,
531 arch,
532 plugin_name,
Jim Ingham0f063ba2013-03-02 00:26:47 +0000533 flavor,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000534 exe_ctx,
535 range,
536 num_instructions,
537 num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000538 options,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000539 strm);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000540}
541
Greg Clayton357132e2011-03-26 19:14:58 +0000542Instruction::Instruction(const Address &address, AddressClass addr_class) :
Greg Clayton1080edbc2011-03-25 18:03:16 +0000543 m_address (address),
Greg Clayton357132e2011-03-26 19:14:58 +0000544 m_address_class (addr_class),
Sean Callanana97aa922012-02-14 00:22:51 +0000545 m_opcode(),
546 m_calculated_strings(false)
Greg Clayton0ae96272011-03-24 23:53:38 +0000547{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548}
549
Greg Clayton1d273162010-10-06 03:09:58 +0000550Instruction::~Instruction()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551{
552}
553
Greg Clayton357132e2011-03-26 19:14:58 +0000554AddressClass
555Instruction::GetAddressClass ()
556{
557 if (m_address_class == eAddressClassInvalid)
558 m_address_class = m_address.GetAddressClass();
559 return m_address_class;
560}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561
Greg Claytonba812f42012-05-10 02:52:23 +0000562void
563Instruction::Dump (lldb_private::Stream *s,
564 uint32_t max_opcode_byte_size,
565 bool show_address,
566 bool show_bytes,
567 const ExecutionContext* exe_ctx)
568{
Jason Molenda7a37c1e2013-01-04 23:52:35 +0000569 size_t opcode_column_width = 7;
Greg Claytonba812f42012-05-10 02:52:23 +0000570 const size_t operand_column_width = 25;
571
572 CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx);
573
574 StreamString ss;
575
576 if (show_address)
577 {
578 m_address.Dump(&ss,
579 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL,
580 Address::DumpStyleLoadAddress,
581 Address::DumpStyleModuleWithFileAddress,
582 0);
583
584 ss.PutCString(": ");
585 }
586
587 if (show_bytes)
588 {
589 if (m_opcode.GetType() == Opcode::eTypeBytes)
590 {
591 // x86_64 and i386 are the only ones that use bytes right now so
592 // pad out the byte dump to be able to always show 15 bytes (3 chars each)
593 // plus a space
594 if (max_opcode_byte_size > 0)
595 m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
596 else
597 m_opcode.Dump (&ss, 15 * 3 + 1);
598 }
599 else
600 {
601 // Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces)
602 // plus two for padding...
603 if (max_opcode_byte_size > 0)
604 m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
605 else
606 m_opcode.Dump (&ss, 12);
607 }
608 }
609
610 const size_t opcode_pos = ss.GetSize();
611
Jason Molenda7a37c1e2013-01-04 23:52:35 +0000612 // The default opcode size of 7 characters is plenty for most architectures
613 // but some like arm can pull out the occasional vqrshrun.s16. We won't get
614 // consistent column spacing in these cases, unfortunately.
615 if (m_opcode_name.length() >= opcode_column_width)
616 {
617 opcode_column_width = m_opcode_name.length() + 1;
618 }
619
Greg Claytonba812f42012-05-10 02:52:23 +0000620 ss.PutCString (m_opcode_name.c_str());
621 ss.FillLastLineToColumn (opcode_pos + opcode_column_width, ' ');
Jim Ingham0f063ba2013-03-02 00:26:47 +0000622 ss.PutCString (m_mnemonics.c_str());
Greg Claytonba812f42012-05-10 02:52:23 +0000623
624 if (!m_comment.empty())
625 {
626 ss.FillLastLineToColumn (opcode_pos + opcode_column_width + operand_column_width, ' ');
627 ss.PutCString (" ; ");
628 ss.PutCString (m_comment.c_str());
629 }
630 s->Write (ss.GetData(), ss.GetSize());
631}
632
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000633bool
634Instruction::DumpEmulation (const ArchSpec &arch)
635{
Greg Clayton7b0992d2013-04-18 22:45:39 +0000636 std::unique_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000637 if (insn_emulator_ap.get())
638 {
Greg Clayton2ed751b2011-04-26 04:39:08 +0000639 insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL);
640 return insn_emulator_ap->EvaluateInstruction (0);
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000641 }
642
643 return false;
644}
645
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000646OptionValueSP
647Instruction::ReadArray (FILE *in_file, Stream *out_stream, OptionValue::Type data_type)
648{
649 bool done = false;
650 char buffer[1024];
651
652 OptionValueSP option_value_sp (new OptionValueArray (1u << data_type));
653
654 int idx = 0;
655 while (!done)
656 {
657 if (!fgets (buffer, 1023, in_file))
658 {
Greg Clayton762f7132011-09-18 18:59:15 +0000659 out_stream->Printf ("Instruction::ReadArray: Error reading file (fgets).\n");
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000660 option_value_sp.reset ();
661 return option_value_sp;
662 }
663
664 std::string line (buffer);
665
Greg Claytonc7bece562013-01-25 18:06:21 +0000666 size_t len = line.size();
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000667 if (line[len-1] == '\n')
668 {
669 line[len-1] = '\0';
670 line.resize (len-1);
671 }
672
673 if ((line.size() == 1) && line[0] == ']')
674 {
675 done = true;
676 line.clear();
677 }
678
679 if (line.size() > 0)
680 {
681 std::string value;
Greg Claytonbc43cab2013-04-03 21:37:16 +0000682 static RegularExpression g_reg_exp ("^[ \t]*([^ \t]+)[ \t]*$");
683 RegularExpression::Match regex_match(1);
684 bool reg_exp_success = g_reg_exp.Execute (line.c_str(), &regex_match);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000685 if (reg_exp_success)
Greg Claytonbc43cab2013-04-03 21:37:16 +0000686 regex_match.GetMatchAtIndex (line.c_str(), 1, value);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000687 else
688 value = line;
689
690 OptionValueSP data_value_sp;
691 switch (data_type)
692 {
693 case OptionValue::eTypeUInt64:
694 data_value_sp.reset (new OptionValueUInt64 (0, 0));
695 data_value_sp->SetValueFromCString (value.c_str());
696 break;
697 // Other types can be added later as needed.
698 default:
699 data_value_sp.reset (new OptionValueString (value.c_str(), ""));
700 break;
701 }
702
Greg Clayton84c39662011-04-27 22:04:39 +0000703 option_value_sp->GetAsArray()->InsertValue (idx, data_value_sp);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000704 ++idx;
705 }
706 }
707
708 return option_value_sp;
709}
710
711OptionValueSP
712Instruction::ReadDictionary (FILE *in_file, Stream *out_stream)
713{
714 bool done = false;
715 char buffer[1024];
716
717 OptionValueSP option_value_sp (new OptionValueDictionary());
718 static ConstString encoding_key ("data_encoding");
719 OptionValue::Type data_type = OptionValue::eTypeInvalid;
720
721
722 while (!done)
723 {
724 // Read the next line in the file
725 if (!fgets (buffer, 1023, in_file))
726 {
727 out_stream->Printf ("Instruction::ReadDictionary: Error reading file (fgets).\n");
728 option_value_sp.reset ();
729 return option_value_sp;
730 }
731
732 // Check to see if the line contains the end-of-dictionary marker ("}")
733 std::string line (buffer);
734
Greg Claytonc7bece562013-01-25 18:06:21 +0000735 size_t len = line.size();
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000736 if (line[len-1] == '\n')
737 {
738 line[len-1] = '\0';
739 line.resize (len-1);
740 }
741
742 if ((line.size() == 1) && (line[0] == '}'))
743 {
744 done = true;
745 line.clear();
746 }
747
748 // Try to find a key-value pair in the current line and add it to the dictionary.
749 if (line.size() > 0)
750 {
Greg Claytonbc43cab2013-04-03 21:37:16 +0000751 static RegularExpression g_reg_exp ("^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*=[ \t]*(.*)[ \t]*$");
752 RegularExpression::Match regex_match(2);
753
754 bool reg_exp_success = g_reg_exp.Execute (line.c_str(), &regex_match);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000755 std::string key;
756 std::string value;
757 if (reg_exp_success)
758 {
Greg Claytonbc43cab2013-04-03 21:37:16 +0000759 regex_match.GetMatchAtIndex (line.c_str(), 1, key);
760 regex_match.GetMatchAtIndex (line.c_str(), 2, value);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000761 }
762 else
763 {
764 out_stream->Printf ("Instruction::ReadDictionary: Failure executing regular expression.\n");
765 option_value_sp.reset();
766 return option_value_sp;
767 }
768
769 ConstString const_key (key.c_str());
770 // Check value to see if it's the start of an array or dictionary.
771
772 lldb::OptionValueSP value_sp;
773 assert (value.empty() == false);
774 assert (key.empty() == false);
775
776 if (value[0] == '{')
777 {
778 assert (value.size() == 1);
779 // value is a dictionary
780 value_sp = ReadDictionary (in_file, out_stream);
781 if (value_sp.get() == NULL)
782 {
783 option_value_sp.reset ();
784 return option_value_sp;
785 }
786 }
787 else if (value[0] == '[')
788 {
789 assert (value.size() == 1);
790 // value is an array
791 value_sp = ReadArray (in_file, out_stream, data_type);
792 if (value_sp.get() == NULL)
793 {
794 option_value_sp.reset ();
795 return option_value_sp;
796 }
797 // We've used the data_type to read an array; re-set the type to Invalid
798 data_type = OptionValue::eTypeInvalid;
799 }
800 else if ((value[0] == '0') && (value[1] == 'x'))
801 {
802 value_sp.reset (new OptionValueUInt64 (0, 0));
803 value_sp->SetValueFromCString (value.c_str());
804 }
805 else
806 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000807 size_t len = value.size();
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000808 if ((value[0] == '"') && (value[len-1] == '"'))
809 value = value.substr (1, len-2);
810 value_sp.reset (new OptionValueString (value.c_str(), ""));
811 }
812
813
814
815 if (const_key == encoding_key)
816 {
817 // A 'data_encoding=..." is NOT a normal key-value pair; it is meta-data indicating the
818 // data type of an upcoming array (usually the next bit of data to be read in).
819 if (strcmp (value.c_str(), "uint32_t") == 0)
820 data_type = OptionValue::eTypeUInt64;
821 }
822 else
Greg Clayton84c39662011-04-27 22:04:39 +0000823 option_value_sp->GetAsDictionary()->SetValueForKey (const_key, value_sp, false);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000824 }
825 }
826
827 return option_value_sp;
828}
829
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000830bool
Caroline Tice3ac67112011-04-19 23:30:03 +0000831Instruction::TestEmulation (Stream *out_stream, const char *file_name)
832{
833 if (!out_stream)
834 return false;
835
836 if (!file_name)
837 {
Johnny Chenea80ba82011-04-21 20:27:45 +0000838 out_stream->Printf ("Instruction::TestEmulation: Missing file_name.");
Caroline Tice3ac67112011-04-19 23:30:03 +0000839 return false;
840 }
841
842 FILE *test_file = fopen (file_name, "r");
843 if (!test_file)
844 {
Johnny Chenea80ba82011-04-21 20:27:45 +0000845 out_stream->Printf ("Instruction::TestEmulation: Attempt to open test file failed.");
Caroline Tice3ac67112011-04-19 23:30:03 +0000846 return false;
847 }
848
Caroline Tice3ac67112011-04-19 23:30:03 +0000849 char buffer[256];
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000850 if (!fgets (buffer, 255, test_file))
Caroline Tice3ac67112011-04-19 23:30:03 +0000851 {
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000852 out_stream->Printf ("Instruction::TestEmulation: Error reading first line of test file.\n");
Caroline Tice3ac67112011-04-19 23:30:03 +0000853 fclose (test_file);
854 return false;
855 }
856
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000857 if (strncmp (buffer, "InstructionEmulationState={", 27) != 0)
858 {
859 out_stream->Printf ("Instructin::TestEmulation: Test file does not contain emulation state dictionary\n");
860 fclose (test_file);
861 return false;
862 }
863
864 // Read all the test information from the test file into an OptionValueDictionary.
865
866 OptionValueSP data_dictionary_sp (ReadDictionary (test_file, out_stream));
867 if (data_dictionary_sp.get() == NULL)
868 {
869 out_stream->Printf ("Instruction::TestEmulation: Error reading Dictionary Object.\n");
870 fclose (test_file);
871 return false;
872 }
873
874 fclose (test_file);
875
Greg Clayton84c39662011-04-27 22:04:39 +0000876 OptionValueDictionary *data_dictionary = data_dictionary_sp->GetAsDictionary();
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000877 static ConstString description_key ("assembly_string");
878 static ConstString triple_key ("triple");
879
880 OptionValueSP value_sp = data_dictionary->GetValueForKey (description_key);
881
882 if (value_sp.get() == NULL)
883 {
884 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain description string.\n");
885 return false;
886 }
887
888 SetDescription (value_sp->GetStringValue());
889
890
891 value_sp = data_dictionary->GetValueForKey (triple_key);
892 if (value_sp.get() == NULL)
893 {
894 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain triple.\n");
895 return false;
896 }
897
898 ArchSpec arch;
899 arch.SetTriple (llvm::Triple (value_sp->GetStringValue()));
Caroline Tice3ac67112011-04-19 23:30:03 +0000900
901 bool success = false;
Greg Clayton7b0992d2013-04-18 22:45:39 +0000902 std::unique_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
Caroline Tice3ac67112011-04-19 23:30:03 +0000903 if (insn_emulator_ap.get())
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000904 success = insn_emulator_ap->TestEmulation (out_stream, arch, data_dictionary);
Caroline Tice3ac67112011-04-19 23:30:03 +0000905
Caroline Tice3ac67112011-04-19 23:30:03 +0000906 if (success)
Johnny Chenea80ba82011-04-21 20:27:45 +0000907 out_stream->Printf ("Emulation test succeeded.");
Caroline Tice3ac67112011-04-19 23:30:03 +0000908 else
Johnny Chenea80ba82011-04-21 20:27:45 +0000909 out_stream->Printf ("Emulation test failed.");
Caroline Tice3ac67112011-04-19 23:30:03 +0000910
911 return success;
912}
913
914bool
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000915Instruction::Emulate (const ArchSpec &arch,
Greg Clayton2ed751b2011-04-26 04:39:08 +0000916 uint32_t evaluate_options,
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000917 void *baton,
Greg Clayton7349bd92011-05-09 20:18:18 +0000918 EmulateInstruction::ReadMemoryCallback read_mem_callback,
919 EmulateInstruction::WriteMemoryCallback write_mem_callback,
920 EmulateInstruction::ReadRegisterCallback read_reg_callback,
921 EmulateInstruction::WriteRegisterCallback write_reg_callback)
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000922{
Greg Clayton7b0992d2013-04-18 22:45:39 +0000923 std::unique_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000924 if (insn_emulator_ap.get())
925 {
926 insn_emulator_ap->SetBaton (baton);
927 insn_emulator_ap->SetCallbacks (read_mem_callback, write_mem_callback, read_reg_callback, write_reg_callback);
Greg Clayton2ed751b2011-04-26 04:39:08 +0000928 insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL);
929 return insn_emulator_ap->EvaluateInstruction (evaluate_options);
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000930 }
931
932 return false;
933}
934
Greg Claytonba812f42012-05-10 02:52:23 +0000935
936uint32_t
937Instruction::GetData (DataExtractor &data)
938{
Sean Callanancd4ae1a2012-08-07 01:44:58 +0000939 return m_opcode.GetData(data);
Greg Claytonba812f42012-05-10 02:52:23 +0000940}
941
Greg Clayton1d273162010-10-06 03:09:58 +0000942InstructionList::InstructionList() :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000943 m_instructions()
944{
945}
946
Greg Clayton1d273162010-10-06 03:09:58 +0000947InstructionList::~InstructionList()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000948{
949}
950
951size_t
Greg Clayton1d273162010-10-06 03:09:58 +0000952InstructionList::GetSize() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000953{
954 return m_instructions.size();
955}
956
Greg Clayton357132e2011-03-26 19:14:58 +0000957uint32_t
958InstructionList::GetMaxOpcocdeByteSize () const
959{
960 uint32_t max_inst_size = 0;
961 collection::const_iterator pos, end;
962 for (pos = m_instructions.begin(), end = m_instructions.end();
963 pos != end;
964 ++pos)
965 {
966 uint32_t inst_size = (*pos)->GetOpcode().GetByteSize();
967 if (max_inst_size < inst_size)
968 max_inst_size = inst_size;
969 }
970 return max_inst_size;
971}
972
973
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000974
Greg Clayton1d273162010-10-06 03:09:58 +0000975InstructionSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000976InstructionList::GetInstructionAtIndex (size_t idx) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977{
Greg Clayton1d273162010-10-06 03:09:58 +0000978 InstructionSP inst_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000979 if (idx < m_instructions.size())
Greg Clayton1d273162010-10-06 03:09:58 +0000980 inst_sp = m_instructions[idx];
981 return inst_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000982}
983
984void
Greg Clayton5009f9d2011-10-27 17:55:14 +0000985InstructionList::Dump (Stream *s,
986 bool show_address,
987 bool show_bytes,
988 const ExecutionContext* exe_ctx)
989{
990 const uint32_t max_opcode_byte_size = GetMaxOpcocdeByteSize();
991 collection::const_iterator pos, begin, end;
992 for (begin = m_instructions.begin(), end = m_instructions.end(), pos = begin;
993 pos != end;
994 ++pos)
995 {
996 if (pos != begin)
997 s->EOL();
Greg Claytonba812f42012-05-10 02:52:23 +0000998 (*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx);
Greg Clayton5009f9d2011-10-27 17:55:14 +0000999 }
1000}
1001
1002
1003void
Greg Clayton1d273162010-10-06 03:09:58 +00001004InstructionList::Clear()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005{
1006 m_instructions.clear();
1007}
1008
1009void
Greg Clayton1d273162010-10-06 03:09:58 +00001010InstructionList::Append (lldb::InstructionSP &inst_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011{
1012 if (inst_sp)
1013 m_instructions.push_back(inst_sp);
1014}
1015
Jim Ingham564d8bc22012-03-09 04:10:47 +00001016uint32_t
1017InstructionList::GetIndexOfNextBranchInstruction(uint32_t start) const
1018{
1019 size_t num_instructions = m_instructions.size();
1020
1021 uint32_t next_branch = UINT32_MAX;
1022 for (size_t i = start; i < num_instructions; i++)
1023 {
1024 if (m_instructions[i]->DoesBranch())
1025 {
1026 next_branch = i;
1027 break;
1028 }
1029 }
1030 return next_branch;
1031}
1032
1033uint32_t
1034InstructionList::GetIndexOfInstructionAtLoadAddress (lldb::addr_t load_addr, Target &target)
1035{
1036 Address address;
1037 address.SetLoadAddress(load_addr, &target);
Greg Claytonc7bece562013-01-25 18:06:21 +00001038 size_t num_instructions = m_instructions.size();
Jim Ingham564d8bc22012-03-09 04:10:47 +00001039 uint32_t index = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001040 for (size_t i = 0; i < num_instructions; i++)
Jim Ingham564d8bc22012-03-09 04:10:47 +00001041 {
1042 if (m_instructions[i]->GetAddress() == address)
1043 {
1044 index = i;
1045 break;
1046 }
1047 }
1048 return index;
1049}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050
1051size_t
Greg Clayton3faf47c2013-03-28 23:42:53 +00001052Disassembler::ParseInstructions (const ExecutionContext *exe_ctx,
1053 const AddressRange &range,
1054 Stream *error_strm_ptr,
1055 bool prefer_file_cache)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001056{
Greg Claytonc14ee322011-09-22 04:58:26 +00001057 if (exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001059 Target *target = exe_ctx->GetTargetPtr();
1060 const addr_t byte_size = range.GetByteSize();
1061 if (target == NULL || byte_size == 0 || !range.GetBaseAddress().IsValid())
1062 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063
Greg Claytonc14ee322011-09-22 04:58:26 +00001064 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
1065 DataBufferSP data_sp(heap_buffer);
1066
1067 Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00001068 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1069 const size_t bytes_read = target->ReadMemory (range.GetBaseAddress(),
Greg Claytonc14ee322011-09-22 04:58:26 +00001070 prefer_file_cache,
1071 heap_buffer->GetBytes(),
1072 heap_buffer->GetByteSize(),
Greg Clayton3faf47c2013-03-28 23:42:53 +00001073 error,
1074 &load_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001075
1076 if (bytes_read > 0)
1077 {
1078 if (bytes_read != heap_buffer->GetByteSize())
1079 heap_buffer->SetByteSize (bytes_read);
1080 DataExtractor data (data_sp,
1081 m_arch.GetByteOrder(),
1082 m_arch.GetAddressByteSize());
Greg Clayton3faf47c2013-03-28 23:42:53 +00001083 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1084 return DecodeInstructions (range.GetBaseAddress(), data, 0, UINT32_MAX, false, data_from_file);
Greg Claytonc14ee322011-09-22 04:58:26 +00001085 }
Greg Clayton57f06302012-05-25 17:05:55 +00001086 else if (error_strm_ptr)
1087 {
1088 const char *error_cstr = error.AsCString();
1089 if (error_cstr)
1090 {
1091 error_strm_ptr->Printf("error: %s\n", error_cstr);
1092 }
1093 }
1094 }
1095 else if (error_strm_ptr)
1096 {
1097 error_strm_ptr->PutCString("error: invalid execution context\n");
Greg Claytonc14ee322011-09-22 04:58:26 +00001098 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099 return 0;
1100}
1101
Jim Ingham37023b02011-03-22 01:48:42 +00001102size_t
Greg Clayton3faf47c2013-03-28 23:42:53 +00001103Disassembler::ParseInstructions (const ExecutionContext *exe_ctx,
1104 const Address &start,
1105 uint32_t num_instructions,
1106 bool prefer_file_cache)
Jim Ingham37023b02011-03-22 01:48:42 +00001107{
Greg Clayton357132e2011-03-26 19:14:58 +00001108 m_instruction_list.Clear();
1109
Greg Claytonc14ee322011-09-22 04:58:26 +00001110 if (exe_ctx == NULL || num_instructions == 0 || !start.IsValid())
Jim Ingham37023b02011-03-22 01:48:42 +00001111 return 0;
1112
Greg Claytonc14ee322011-09-22 04:58:26 +00001113 Target *target = exe_ctx->GetTargetPtr();
Greg Clayton357132e2011-03-26 19:14:58 +00001114 // Calculate the max buffer size we will need in order to disassemble
1115 const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize();
Jim Ingham37023b02011-03-22 01:48:42 +00001116
Greg Clayton357132e2011-03-26 19:14:58 +00001117 if (target == NULL || byte_size == 0)
Jim Ingham37023b02011-03-22 01:48:42 +00001118 return 0;
1119
1120 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
Greg Clayton357132e2011-03-26 19:14:58 +00001121 DataBufferSP data_sp (heap_buffer);
Jim Ingham37023b02011-03-22 01:48:42 +00001122
1123 Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00001124 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1125 const size_t bytes_read = target->ReadMemory (start,
Greg Clayton357132e2011-03-26 19:14:58 +00001126 prefer_file_cache,
1127 heap_buffer->GetBytes(),
1128 byte_size,
Greg Clayton3faf47c2013-03-28 23:42:53 +00001129 error,
1130 &load_addr);
1131
1132 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001133
1134 if (bytes_read == 0)
1135 return 0;
1136 DataExtractor data (data_sp,
1137 m_arch.GetByteOrder(),
1138 m_arch.GetAddressByteSize());
1139
1140 const bool append_instructions = true;
1141 DecodeInstructions (start,
1142 data,
1143 0,
1144 num_instructions,
Greg Clayton3faf47c2013-03-28 23:42:53 +00001145 append_instructions,
1146 data_from_file);
Greg Clayton357132e2011-03-26 19:14:58 +00001147
Jim Ingham37023b02011-03-22 01:48:42 +00001148 return m_instruction_list.GetSize();
1149}
1150
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001151//----------------------------------------------------------------------
1152// Disassembler copy constructor
1153//----------------------------------------------------------------------
Jim Ingham0f063ba2013-03-02 00:26:47 +00001154Disassembler::Disassembler(const ArchSpec& arch, const char *flavor) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155 m_arch (arch),
1156 m_instruction_list(),
Jim Ingham0f063ba2013-03-02 00:26:47 +00001157 m_base_addr(LLDB_INVALID_ADDRESS),
1158 m_flavor ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159{
Jim Ingham0f063ba2013-03-02 00:26:47 +00001160 if (flavor == NULL)
1161 m_flavor.assign("default");
1162 else
1163 m_flavor.assign(flavor);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001164}
1165
1166//----------------------------------------------------------------------
1167// Destructor
1168//----------------------------------------------------------------------
1169Disassembler::~Disassembler()
1170{
1171}
1172
Greg Clayton1d273162010-10-06 03:09:58 +00001173InstructionList &
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174Disassembler::GetInstructionList ()
1175{
1176 return m_instruction_list;
1177}
1178
Greg Clayton1d273162010-10-06 03:09:58 +00001179const InstructionList &
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180Disassembler::GetInstructionList () const
1181{
1182 return m_instruction_list;
1183}
Caroline Tice3ac67112011-04-19 23:30:03 +00001184
1185//----------------------------------------------------------------------
1186// Class PseudoInstruction
1187//----------------------------------------------------------------------
1188PseudoInstruction::PseudoInstruction () :
1189 Instruction (Address(), eAddressClassUnknown),
1190 m_description ()
1191{
1192}
1193
1194PseudoInstruction::~PseudoInstruction ()
1195{
1196}
1197
Caroline Tice3ac67112011-04-19 23:30:03 +00001198bool
Jim Ingham32ce20c2013-03-13 01:55:16 +00001199PseudoInstruction::DoesBranch ()
Caroline Tice3ac67112011-04-19 23:30:03 +00001200{
1201 // This is NOT a valid question for a pseudo instruction.
1202 return false;
1203}
1204
1205size_t
1206PseudoInstruction::Decode (const lldb_private::Disassembler &disassembler,
1207 const lldb_private::DataExtractor &data,
Greg Claytonc7bece562013-01-25 18:06:21 +00001208 lldb::offset_t data_offset)
Caroline Tice3ac67112011-04-19 23:30:03 +00001209{
1210 return m_opcode.GetByteSize();
1211}
1212
1213
1214void
1215PseudoInstruction::SetOpcode (size_t opcode_size, void *opcode_data)
1216{
1217 if (!opcode_data)
1218 return;
1219
1220 switch (opcode_size)
1221 {
1222 case 8:
1223 {
1224 uint8_t value8 = *((uint8_t *) opcode_data);
1225 m_opcode.SetOpcode8 (value8);
1226 break;
1227 }
1228 case 16:
1229 {
1230 uint16_t value16 = *((uint16_t *) opcode_data);
1231 m_opcode.SetOpcode16 (value16);
1232 break;
1233 }
1234 case 32:
1235 {
1236 uint32_t value32 = *((uint32_t *) opcode_data);
1237 m_opcode.SetOpcode32 (value32);
1238 break;
1239 }
1240 case 64:
1241 {
1242 uint64_t value64 = *((uint64_t *) opcode_data);
1243 m_opcode.SetOpcode64 (value64);
1244 break;
1245 }
1246 default:
1247 break;
1248 }
1249}
1250
1251void
1252PseudoInstruction::SetDescription (const char *description)
1253{
1254 if (description && strlen (description) > 0)
1255 m_description = description;
1256}