blob: 40b6c780b4f373620104a444c3acb51471f3d71e [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
10#include "lldb/Core/Disassembler.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/lldb-private.h"
17#include "lldb/Core/Error.h"
18#include "lldb/Core/DataBufferHeap.h"
19#include "lldb/Core/DataExtractor.h"
20#include "lldb/Core/Debugger.h"
Caroline Ticead379efc2011-04-05 18:46:00 +000021#include "lldb/Core/EmulateInstruction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/Module.h"
23#include "lldb/Core/PluginManager.h"
Caroline Ticede2fb9c2011-04-22 05:08:45 +000024#include "lldb/Core/RegularExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Timer.h"
Caroline Ticede2fb9c2011-04-22 05:08:45 +000026#include "lldb/Interpreter/NamedOptionValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Symbol/ObjectFile.h"
28#include "lldb/Target/ExecutionContext.h"
29#include "lldb/Target/Process.h"
30#include "lldb/Target/StackFrame.h"
31#include "lldb/Target/Target.h"
32
33#define DEFAULT_DISASM_BYTE_SIZE 32
34
35using namespace lldb;
36using namespace lldb_private;
37
38
39Disassembler*
Greg Clayton1080edbc2011-03-25 18:03:16 +000040Disassembler::FindPlugin (const ArchSpec &arch, const char *plugin_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041{
42 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Clayton1080edbc2011-03-25 18:03:16 +000043 "Disassembler::FindPlugin (arch = %s, plugin_name = %s)",
44 arch.GetArchitectureName(),
45 plugin_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
47 std::auto_ptr<Disassembler> disassembler_ap;
Greg Clayton1080edbc2011-03-25 18:03:16 +000048 DisassemblerCreateInstance create_callback = NULL;
49
50 if (plugin_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 {
Greg Clayton1080edbc2011-03-25 18:03:16 +000052 create_callback = PluginManager::GetDisassemblerCreateCallbackForPluginName (plugin_name);
53 if (create_callback)
54 {
55 disassembler_ap.reset (create_callback(arch));
56
57 if (disassembler_ap.get())
58 return disassembler_ap.release();
59 }
60 }
61 else
62 {
63 for (uint32_t idx = 0; (create_callback = PluginManager::GetDisassemblerCreateCallbackAtIndex(idx)) != NULL; ++idx)
64 {
65 disassembler_ap.reset (create_callback(arch));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
Greg Clayton1080edbc2011-03-25 18:03:16 +000067 if (disassembler_ap.get())
68 return disassembler_ap.release();
69 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070 }
71 return NULL;
72}
73
Greg Claytondda4f7b2010-06-30 23:03:03 +000074
Greg Clayton357132e2011-03-26 19:14:58 +000075static void
76ResolveAddress (const ExecutionContext &exe_ctx,
77 const Address &addr,
78 Address &resolved_addr)
79{
80 if (!addr.IsSectionOffset())
81 {
82 // If we weren't passed in a section offset address range,
83 // try and resolve it to something
84 if (exe_ctx.target)
85 {
86 if (exe_ctx.target->GetSectionLoadList().IsEmpty())
87 {
88 exe_ctx.target->GetImages().ResolveFileAddress (addr.GetOffset(), resolved_addr);
89 }
90 else
91 {
92 exe_ctx.target->GetSectionLoadList().ResolveLoadAddress (addr.GetOffset(), resolved_addr);
93 }
94 // We weren't able to resolve the address, just treat it as a
95 // raw address
96 if (resolved_addr.IsValid())
97 return;
98 }
99 }
100 resolved_addr = addr;
101}
Greg Claytondda4f7b2010-06-30 23:03:03 +0000102
103size_t
104Disassembler::Disassemble
105(
106 Debugger &debugger,
107 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000108 const char *plugin_name,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000109 const ExecutionContext &exe_ctx,
110 SymbolContextList &sc_list,
Jim Ingham37023b02011-03-22 01:48:42 +0000111 uint32_t num_instructions,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000112 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000113 uint32_t options,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000114 Stream &strm
115)
116{
117 size_t success_count = 0;
118 const size_t count = sc_list.GetSize();
119 SymbolContext sc;
120 AddressRange range;
Greg Clayton7e14f912011-04-23 02:04:55 +0000121 const uint32_t scope = eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
122 const bool use_inline_block_range = true;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000123 for (size_t i=0; i<count; ++i)
124 {
125 if (sc_list.GetContextAtIndex(i, sc) == false)
126 break;
Greg Clayton7e14f912011-04-23 02:04:55 +0000127 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 +0000128 {
Greg Clayton1080edbc2011-03-25 18:03:16 +0000129 if (Disassemble (debugger,
130 arch,
131 plugin_name,
132 exe_ctx,
133 range,
134 num_instructions,
135 num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000136 options,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000137 strm))
Greg Claytondda4f7b2010-06-30 23:03:03 +0000138 {
139 ++success_count;
140 strm.EOL();
141 }
142 }
143 }
144 return success_count;
145}
146
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147bool
148Disassembler::Disassemble
149(
Greg Clayton66111032010-06-23 01:19:29 +0000150 Debugger &debugger,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000152 const char *plugin_name,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 const ExecutionContext &exe_ctx,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000154 const ConstString &name,
155 Module *module,
Jim Ingham37023b02011-03-22 01:48:42 +0000156 uint32_t num_instructions,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000157 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000158 uint32_t options,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159 Stream &strm
160)
161{
Greg Claytondda4f7b2010-06-30 23:03:03 +0000162 SymbolContextList sc_list;
Greg Clayton931180e2011-01-27 06:44:37 +0000163 if (name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164 {
Greg Clayton931180e2011-01-27 06:44:37 +0000165 const bool include_symbols = true;
166 if (module)
167 {
168 module->FindFunctions (name,
169 eFunctionNameTypeBase |
170 eFunctionNameTypeFull |
171 eFunctionNameTypeMethod |
172 eFunctionNameTypeSelector,
173 include_symbols,
174 true,
175 sc_list);
176 }
177 else if (exe_ctx.target)
178 {
179 exe_ctx.target->GetImages().FindFunctions (name,
Greg Clayton6dbd3982010-09-15 05:51:24 +0000180 eFunctionNameTypeBase |
181 eFunctionNameTypeFull |
182 eFunctionNameTypeMethod |
183 eFunctionNameTypeSelector,
Greg Clayton931180e2011-01-27 06:44:37 +0000184 include_symbols,
Sean Callanan8ade1042010-07-27 00:55:47 +0000185 false,
Greg Clayton931180e2011-01-27 06:44:37 +0000186 sc_list);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000187 }
Greg Clayton931180e2011-01-27 06:44:37 +0000188 }
189
190 if (sc_list.GetSize ())
191 {
192 return Disassemble (debugger,
193 arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000194 plugin_name,
Greg Clayton931180e2011-01-27 06:44:37 +0000195 exe_ctx,
Jim Ingham37023b02011-03-22 01:48:42 +0000196 sc_list,
197 num_instructions,
Greg Clayton931180e2011-01-27 06:44:37 +0000198 num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000199 options,
Greg Clayton931180e2011-01-27 06:44:37 +0000200 strm);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000201 }
202 return false;
203}
204
Greg Clayton1d273162010-10-06 03:09:58 +0000205
206lldb::DisassemblerSP
207Disassembler::DisassembleRange
208(
209 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000210 const char *plugin_name,
Greg Clayton1d273162010-10-06 03:09:58 +0000211 const ExecutionContext &exe_ctx,
212 const AddressRange &range
213)
214{
215 lldb::DisassemblerSP disasm_sp;
216 if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid())
217 {
Greg Clayton1080edbc2011-03-25 18:03:16 +0000218 disasm_sp.reset (Disassembler::FindPlugin(arch, plugin_name));
Greg Clayton1d273162010-10-06 03:09:58 +0000219
220 if (disasm_sp)
221 {
Greg Clayton357132e2011-03-26 19:14:58 +0000222 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range);
Greg Clayton1d273162010-10-06 03:09:58 +0000223 if (bytes_disassembled == 0)
224 disasm_sp.reset();
225 }
226 }
227 return disasm_sp;
228}
229
230
Greg Claytondda4f7b2010-06-30 23:03:03 +0000231bool
232Disassembler::Disassemble
233(
234 Debugger &debugger,
235 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000236 const char *plugin_name,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000237 const ExecutionContext &exe_ctx,
238 const AddressRange &disasm_range,
Jim Ingham37023b02011-03-22 01:48:42 +0000239 uint32_t num_instructions,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000240 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000241 uint32_t options,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000242 Stream &strm
243)
244{
245 if (disasm_range.GetByteSize())
246 {
Greg Clayton1080edbc2011-03-25 18:03:16 +0000247 std::auto_ptr<Disassembler> disasm_ap (Disassembler::FindPlugin(arch, plugin_name));
Greg Claytondda4f7b2010-06-30 23:03:03 +0000248
Greg Clayton1d273162010-10-06 03:09:58 +0000249 if (disasm_ap.get())
Greg Claytondda4f7b2010-06-30 23:03:03 +0000250 {
Greg Clayton357132e2011-03-26 19:14:58 +0000251 AddressRange range;
252 ResolveAddress (exe_ctx, disasm_range.GetBaseAddress(), range.GetBaseAddress());
253 range.SetByteSize (disasm_range.GetByteSize());
Greg Claytondda4f7b2010-06-30 23:03:03 +0000254
Greg Clayton357132e2011-03-26 19:14:58 +0000255 size_t bytes_disassembled = disasm_ap->ParseInstructions (&exe_ctx, range);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256 if (bytes_disassembled == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257 return false;
Greg Clayton1080edbc2011-03-25 18:03:16 +0000258
259 return PrintInstructions (disasm_ap.get(),
260 debugger,
261 arch,
262 exe_ctx,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000263 num_instructions,
264 num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000265 options,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000266 strm);
Jim Ingham37023b02011-03-22 01:48:42 +0000267 }
268 }
269 return false;
270}
271
272bool
273Disassembler::Disassemble
274(
275 Debugger &debugger,
276 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000277 const char *plugin_name,
Jim Ingham37023b02011-03-22 01:48:42 +0000278 const ExecutionContext &exe_ctx,
279 const Address &start_address,
280 uint32_t num_instructions,
281 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000282 uint32_t options,
Jim Ingham37023b02011-03-22 01:48:42 +0000283 Stream &strm
284)
285{
286 if (num_instructions > 0)
287 {
Greg Clayton1080edbc2011-03-25 18:03:16 +0000288 std::auto_ptr<Disassembler> disasm_ap (Disassembler::FindPlugin(arch, plugin_name));
Jim Ingham37023b02011-03-22 01:48:42 +0000289 if (disasm_ap.get())
290 {
Greg Clayton357132e2011-03-26 19:14:58 +0000291 Address addr;
292 ResolveAddress (exe_ctx, start_address, addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293
Greg Clayton357132e2011-03-26 19:14:58 +0000294 size_t bytes_disassembled = disasm_ap->ParseInstructions (&exe_ctx, addr, num_instructions);
Jim Ingham37023b02011-03-22 01:48:42 +0000295 if (bytes_disassembled == 0)
Jim Ingham37023b02011-03-22 01:48:42 +0000296 return false;
Greg Clayton1080edbc2011-03-25 18:03:16 +0000297 return PrintInstructions (disasm_ap.get(),
298 debugger,
299 arch,
300 exe_ctx,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000301 num_instructions,
302 num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000303 options,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000304 strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306 }
307 return false;
308}
Jim Ingham37023b02011-03-22 01:48:42 +0000309
310bool
311Disassembler::PrintInstructions
312(
313 Disassembler *disasm_ptr,
Jim Ingham37023b02011-03-22 01:48:42 +0000314 Debugger &debugger,
315 const ArchSpec &arch,
316 const ExecutionContext &exe_ctx,
Jim Ingham37023b02011-03-22 01:48:42 +0000317 uint32_t num_instructions,
318 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000319 uint32_t options,
Jim Ingham37023b02011-03-22 01:48:42 +0000320 Stream &strm
321)
322{
323 // We got some things disassembled...
324 size_t num_instructions_found = disasm_ptr->GetInstructionList().GetSize();
325
326 if (num_instructions > 0 && num_instructions < num_instructions_found)
327 num_instructions_found = num_instructions;
328
Greg Clayton357132e2011-03-26 19:14:58 +0000329 const uint32_t max_opcode_byte_size = disasm_ptr->GetInstructionList().GetMaxOpcocdeByteSize ();
Jim Ingham37023b02011-03-22 01:48:42 +0000330 uint32_t offset = 0;
331 SymbolContext sc;
332 SymbolContext prev_sc;
333 AddressRange sc_range;
Greg Clayton34132752011-07-06 04:07:21 +0000334 const Address *pc_addr_ptr = NULL;
Greg Clayton7e14f912011-04-23 02:04:55 +0000335 ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope();
Greg Clayton32e0a752011-03-30 18:16:51 +0000336 if (exe_ctx.frame)
337 pc_addr_ptr = &exe_ctx.frame->GetFrameCodeAddress();
Greg Clayton7e14f912011-04-23 02:04:55 +0000338 const uint32_t scope = eSymbolContextLineEntry | eSymbolContextFunction | eSymbolContextSymbol;
339 const bool use_inline_block_range = false;
Jim Ingham37023b02011-03-22 01:48:42 +0000340 for (size_t i=0; i<num_instructions_found; ++i)
341 {
342 Instruction *inst = disasm_ptr->GetInstructionList().GetInstructionAtIndex (i).get();
343 if (inst)
344 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000345 const Address &addr = inst->GetAddress();
346 const bool inst_is_at_pc = pc_addr_ptr && addr == *pc_addr_ptr;
Jim Ingham37023b02011-03-22 01:48:42 +0000347
348 prev_sc = sc;
349
Greg Clayton32e0a752011-03-30 18:16:51 +0000350 Module *module = addr.GetModule();
351 if (module)
Jim Ingham37023b02011-03-22 01:48:42 +0000352 {
Jim Ingham37023b02011-03-22 01:48:42 +0000353 uint32_t resolved_mask = module->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, sc);
354 if (resolved_mask)
355 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000356 if (num_mixed_context_lines)
357 {
358 if (!sc_range.ContainsFileAddress (addr))
359 {
Greg Clayton7e14f912011-04-23 02:04:55 +0000360 sc.GetAddressRange (scope, 0, use_inline_block_range, sc_range);
Greg Clayton32e0a752011-03-30 18:16:51 +0000361
362 if (sc != prev_sc)
363 {
364 if (offset != 0)
365 strm.EOL();
366
367 sc.DumpStopContext(&strm, exe_ctx.process, addr, false, true, false);
368 strm.EOL();
369
370 if (sc.comp_unit && sc.line_entry.IsValid())
371 {
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000372 debugger.GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.line_entry.file,
Greg Clayton32e0a752011-03-30 18:16:51 +0000373 sc.line_entry.line,
374 num_mixed_context_lines,
375 num_mixed_context_lines,
Greg Claytonb10d72f2011-06-28 19:01:40 +0000376 ((inst_is_at_pc && (options & eOptionMarkPCSourceLine)) ? "->" : ""),
Greg Clayton32e0a752011-03-30 18:16:51 +0000377 &strm);
378 }
379 }
380 }
381 }
382 else if (!(prev_sc.function == sc.function || prev_sc.symbol == sc.symbol))
Jim Ingham37023b02011-03-22 01:48:42 +0000383 {
384 if (prev_sc.function || prev_sc.symbol)
385 strm.EOL();
386
Greg Clayton7e14f912011-04-23 02:04:55 +0000387 bool show_fullpaths = false;
388 bool show_module = true;
389 bool show_inlined_frames = true;
390 sc.DumpStopContext (&strm,
391 exe_scope,
392 addr,
393 show_fullpaths,
394 show_module,
395 show_inlined_frames);
Jim Ingham37023b02011-03-22 01:48:42 +0000396
Jim Ingham37023b02011-03-22 01:48:42 +0000397 strm << ":\n";
398 }
Jim Ingham37023b02011-03-22 01:48:42 +0000399 }
400 else
401 {
402 sc.Clear();
403 }
404 }
Jim Ingham37023b02011-03-22 01:48:42 +0000405
Greg Claytonb10d72f2011-06-28 19:01:40 +0000406 if ((options & eOptionMarkPCAddress) && pc_addr_ptr)
Greg Clayton32e0a752011-03-30 18:16:51 +0000407 {
Greg Claytonb10d72f2011-06-28 19:01:40 +0000408 strm.PutCString(inst_is_at_pc ? "-> " : " ");
Greg Clayton32e0a752011-03-30 18:16:51 +0000409 }
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000410 const bool show_bytes = (options & eOptionShowBytes) != 0;
411 const bool raw = (options & eOptionRawOuput) != 0;
Greg Clayton32e0a752011-03-30 18:16:51 +0000412 inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx, raw);
413 strm.EOL();
Jim Ingham37023b02011-03-22 01:48:42 +0000414 }
415 else
416 {
417 break;
418 }
419 }
Jim Ingham37023b02011-03-22 01:48:42 +0000420
421 return true;
422}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423
Greg Claytondda4f7b2010-06-30 23:03:03 +0000424
425bool
426Disassembler::Disassemble
427(
428 Debugger &debugger,
429 const ArchSpec &arch,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000430 const char *plugin_name,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000431 const ExecutionContext &exe_ctx,
Jim Ingham37023b02011-03-22 01:48:42 +0000432 uint32_t num_instructions,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000433 uint32_t num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000434 uint32_t options,
Greg Claytondda4f7b2010-06-30 23:03:03 +0000435 Stream &strm
436)
437{
438 AddressRange range;
439 if (exe_ctx.frame)
440 {
441 SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
442 if (sc.function)
443 {
444 range = sc.function->GetAddressRange();
445 }
446 else if (sc.symbol && sc.symbol->GetAddressRangePtr())
447 {
448 range = *sc.symbol->GetAddressRangePtr();
449 }
450 else
451 {
Greg Clayton9da7bd02010-08-24 21:05:24 +0000452 range.GetBaseAddress() = exe_ctx.frame->GetFrameCodeAddress();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000453 }
454
455 if (range.GetBaseAddress().IsValid() && range.GetByteSize() == 0)
456 range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE);
457 }
458
Greg Clayton1080edbc2011-03-25 18:03:16 +0000459 return Disassemble (debugger,
460 arch,
461 plugin_name,
462 exe_ctx,
463 range,
464 num_instructions,
465 num_mixed_context_lines,
Greg Clayton1da6f9d2011-06-22 01:39:49 +0000466 options,
Greg Clayton1080edbc2011-03-25 18:03:16 +0000467 strm);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000468}
469
Greg Clayton357132e2011-03-26 19:14:58 +0000470Instruction::Instruction(const Address &address, AddressClass addr_class) :
Greg Clayton1080edbc2011-03-25 18:03:16 +0000471 m_address (address),
Greg Clayton357132e2011-03-26 19:14:58 +0000472 m_address_class (addr_class),
Greg Clayton1080edbc2011-03-25 18:03:16 +0000473 m_opcode()
Greg Clayton0ae96272011-03-24 23:53:38 +0000474{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475}
476
Greg Clayton1d273162010-10-06 03:09:58 +0000477Instruction::~Instruction()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478{
479}
480
Greg Clayton357132e2011-03-26 19:14:58 +0000481AddressClass
482Instruction::GetAddressClass ()
483{
484 if (m_address_class == eAddressClassInvalid)
485 m_address_class = m_address.GetAddressClass();
486 return m_address_class;
487}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000489bool
490Instruction::DumpEmulation (const ArchSpec &arch)
491{
Greg Clayton2ed751b2011-04-26 04:39:08 +0000492 std::auto_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000493 if (insn_emulator_ap.get())
494 {
Greg Clayton2ed751b2011-04-26 04:39:08 +0000495 insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL);
496 return insn_emulator_ap->EvaluateInstruction (0);
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000497 }
498
499 return false;
500}
501
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000502OptionValueSP
503Instruction::ReadArray (FILE *in_file, Stream *out_stream, OptionValue::Type data_type)
504{
505 bool done = false;
506 char buffer[1024];
507
508 OptionValueSP option_value_sp (new OptionValueArray (1u << data_type));
509
510 int idx = 0;
511 while (!done)
512 {
513 if (!fgets (buffer, 1023, in_file))
514 {
515 out_stream->Printf ("Instruction::ReadArray: Erroe reading file (fgets).\n");
516 option_value_sp.reset ();
517 return option_value_sp;
518 }
519
520 std::string line (buffer);
521
522 int len = line.size();
523 if (line[len-1] == '\n')
524 {
525 line[len-1] = '\0';
526 line.resize (len-1);
527 }
528
529 if ((line.size() == 1) && line[0] == ']')
530 {
531 done = true;
532 line.clear();
533 }
534
535 if (line.size() > 0)
536 {
537 std::string value;
538 RegularExpression reg_exp ("^[ \t]*([^ \t]+)[ \t]*$");
539 bool reg_exp_success = reg_exp.Execute (line.c_str(), 1);
540 if (reg_exp_success)
541 reg_exp.GetMatchAtIndex (line.c_str(), 1, value);
542 else
543 value = line;
544
545 OptionValueSP data_value_sp;
546 switch (data_type)
547 {
548 case OptionValue::eTypeUInt64:
549 data_value_sp.reset (new OptionValueUInt64 (0, 0));
550 data_value_sp->SetValueFromCString (value.c_str());
551 break;
552 // Other types can be added later as needed.
553 default:
554 data_value_sp.reset (new OptionValueString (value.c_str(), ""));
555 break;
556 }
557
Greg Clayton84c39662011-04-27 22:04:39 +0000558 option_value_sp->GetAsArray()->InsertValue (idx, data_value_sp);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000559 ++idx;
560 }
561 }
562
563 return option_value_sp;
564}
565
566OptionValueSP
567Instruction::ReadDictionary (FILE *in_file, Stream *out_stream)
568{
569 bool done = false;
570 char buffer[1024];
571
572 OptionValueSP option_value_sp (new OptionValueDictionary());
573 static ConstString encoding_key ("data_encoding");
574 OptionValue::Type data_type = OptionValue::eTypeInvalid;
575
576
577 while (!done)
578 {
579 // Read the next line in the file
580 if (!fgets (buffer, 1023, in_file))
581 {
582 out_stream->Printf ("Instruction::ReadDictionary: Error reading file (fgets).\n");
583 option_value_sp.reset ();
584 return option_value_sp;
585 }
586
587 // Check to see if the line contains the end-of-dictionary marker ("}")
588 std::string line (buffer);
589
590 int len = line.size();
591 if (line[len-1] == '\n')
592 {
593 line[len-1] = '\0';
594 line.resize (len-1);
595 }
596
597 if ((line.size() == 1) && (line[0] == '}'))
598 {
599 done = true;
600 line.clear();
601 }
602
603 // Try to find a key-value pair in the current line and add it to the dictionary.
604 if (line.size() > 0)
605 {
606 RegularExpression reg_exp ("^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*=[ \t]*(.*)[ \t]*$");
607 bool reg_exp_success = reg_exp.Execute (line.c_str(), 2);
608 std::string key;
609 std::string value;
610 if (reg_exp_success)
611 {
612 reg_exp.GetMatchAtIndex (line.c_str(), 1, key);
613 reg_exp.GetMatchAtIndex (line.c_str(), 2, value);
614 }
615 else
616 {
617 out_stream->Printf ("Instruction::ReadDictionary: Failure executing regular expression.\n");
618 option_value_sp.reset();
619 return option_value_sp;
620 }
621
622 ConstString const_key (key.c_str());
623 // Check value to see if it's the start of an array or dictionary.
624
625 lldb::OptionValueSP value_sp;
626 assert (value.empty() == false);
627 assert (key.empty() == false);
628
629 if (value[0] == '{')
630 {
631 assert (value.size() == 1);
632 // value is a dictionary
633 value_sp = ReadDictionary (in_file, out_stream);
634 if (value_sp.get() == NULL)
635 {
636 option_value_sp.reset ();
637 return option_value_sp;
638 }
639 }
640 else if (value[0] == '[')
641 {
642 assert (value.size() == 1);
643 // value is an array
644 value_sp = ReadArray (in_file, out_stream, data_type);
645 if (value_sp.get() == NULL)
646 {
647 option_value_sp.reset ();
648 return option_value_sp;
649 }
650 // We've used the data_type to read an array; re-set the type to Invalid
651 data_type = OptionValue::eTypeInvalid;
652 }
653 else if ((value[0] == '0') && (value[1] == 'x'))
654 {
655 value_sp.reset (new OptionValueUInt64 (0, 0));
656 value_sp->SetValueFromCString (value.c_str());
657 }
658 else
659 {
660 int len = value.size();
661 if ((value[0] == '"') && (value[len-1] == '"'))
662 value = value.substr (1, len-2);
663 value_sp.reset (new OptionValueString (value.c_str(), ""));
664 }
665
666
667
668 if (const_key == encoding_key)
669 {
670 // A 'data_encoding=..." is NOT a normal key-value pair; it is meta-data indicating the
671 // data type of an upcoming array (usually the next bit of data to be read in).
672 if (strcmp (value.c_str(), "uint32_t") == 0)
673 data_type = OptionValue::eTypeUInt64;
674 }
675 else
Greg Clayton84c39662011-04-27 22:04:39 +0000676 option_value_sp->GetAsDictionary()->SetValueForKey (const_key, value_sp, false);
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000677 }
678 }
679
680 return option_value_sp;
681}
682
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000683bool
Caroline Tice3ac67112011-04-19 23:30:03 +0000684Instruction::TestEmulation (Stream *out_stream, const char *file_name)
685{
686 if (!out_stream)
687 return false;
688
689 if (!file_name)
690 {
Johnny Chenea80ba82011-04-21 20:27:45 +0000691 out_stream->Printf ("Instruction::TestEmulation: Missing file_name.");
Caroline Tice3ac67112011-04-19 23:30:03 +0000692 return false;
693 }
694
695 FILE *test_file = fopen (file_name, "r");
696 if (!test_file)
697 {
Johnny Chenea80ba82011-04-21 20:27:45 +0000698 out_stream->Printf ("Instruction::TestEmulation: Attempt to open test file failed.");
Caroline Tice3ac67112011-04-19 23:30:03 +0000699 return false;
700 }
701
Caroline Tice3ac67112011-04-19 23:30:03 +0000702 char buffer[256];
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000703 if (!fgets (buffer, 255, test_file))
Caroline Tice3ac67112011-04-19 23:30:03 +0000704 {
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000705 out_stream->Printf ("Instruction::TestEmulation: Error reading first line of test file.\n");
Caroline Tice3ac67112011-04-19 23:30:03 +0000706 fclose (test_file);
707 return false;
708 }
709
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000710 if (strncmp (buffer, "InstructionEmulationState={", 27) != 0)
711 {
712 out_stream->Printf ("Instructin::TestEmulation: Test file does not contain emulation state dictionary\n");
713 fclose (test_file);
714 return false;
715 }
716
717 // Read all the test information from the test file into an OptionValueDictionary.
718
719 OptionValueSP data_dictionary_sp (ReadDictionary (test_file, out_stream));
720 if (data_dictionary_sp.get() == NULL)
721 {
722 out_stream->Printf ("Instruction::TestEmulation: Error reading Dictionary Object.\n");
723 fclose (test_file);
724 return false;
725 }
726
727 fclose (test_file);
728
Greg Clayton84c39662011-04-27 22:04:39 +0000729 OptionValueDictionary *data_dictionary = data_dictionary_sp->GetAsDictionary();
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000730 static ConstString description_key ("assembly_string");
731 static ConstString triple_key ("triple");
732
733 OptionValueSP value_sp = data_dictionary->GetValueForKey (description_key);
734
735 if (value_sp.get() == NULL)
736 {
737 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain description string.\n");
738 return false;
739 }
740
741 SetDescription (value_sp->GetStringValue());
742
743
744 value_sp = data_dictionary->GetValueForKey (triple_key);
745 if (value_sp.get() == NULL)
746 {
747 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain triple.\n");
748 return false;
749 }
750
751 ArchSpec arch;
752 arch.SetTriple (llvm::Triple (value_sp->GetStringValue()));
Caroline Tice3ac67112011-04-19 23:30:03 +0000753
754 bool success = false;
Greg Clayton2ed751b2011-04-26 04:39:08 +0000755 std::auto_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
Caroline Tice3ac67112011-04-19 23:30:03 +0000756 if (insn_emulator_ap.get())
Caroline Ticede2fb9c2011-04-22 05:08:45 +0000757 success = insn_emulator_ap->TestEmulation (out_stream, arch, data_dictionary);
Caroline Tice3ac67112011-04-19 23:30:03 +0000758
Caroline Tice3ac67112011-04-19 23:30:03 +0000759 if (success)
Johnny Chenea80ba82011-04-21 20:27:45 +0000760 out_stream->Printf ("Emulation test succeeded.");
Caroline Tice3ac67112011-04-19 23:30:03 +0000761 else
Johnny Chenea80ba82011-04-21 20:27:45 +0000762 out_stream->Printf ("Emulation test failed.");
Caroline Tice3ac67112011-04-19 23:30:03 +0000763
764 return success;
765}
766
767bool
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000768Instruction::Emulate (const ArchSpec &arch,
Greg Clayton2ed751b2011-04-26 04:39:08 +0000769 uint32_t evaluate_options,
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000770 void *baton,
Greg Clayton7349bd92011-05-09 20:18:18 +0000771 EmulateInstruction::ReadMemoryCallback read_mem_callback,
772 EmulateInstruction::WriteMemoryCallback write_mem_callback,
773 EmulateInstruction::ReadRegisterCallback read_reg_callback,
774 EmulateInstruction::WriteRegisterCallback write_reg_callback)
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000775{
Greg Clayton2ed751b2011-04-26 04:39:08 +0000776 std::auto_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000777 if (insn_emulator_ap.get())
778 {
779 insn_emulator_ap->SetBaton (baton);
780 insn_emulator_ap->SetCallbacks (read_mem_callback, write_mem_callback, read_reg_callback, write_reg_callback);
Greg Clayton2ed751b2011-04-26 04:39:08 +0000781 insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL);
782 return insn_emulator_ap->EvaluateInstruction (evaluate_options);
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000783 }
784
785 return false;
786}
787
Greg Clayton1d273162010-10-06 03:09:58 +0000788InstructionList::InstructionList() :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789 m_instructions()
790{
791}
792
Greg Clayton1d273162010-10-06 03:09:58 +0000793InstructionList::~InstructionList()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794{
795}
796
797size_t
Greg Clayton1d273162010-10-06 03:09:58 +0000798InstructionList::GetSize() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799{
800 return m_instructions.size();
801}
802
Greg Clayton357132e2011-03-26 19:14:58 +0000803uint32_t
804InstructionList::GetMaxOpcocdeByteSize () const
805{
806 uint32_t max_inst_size = 0;
807 collection::const_iterator pos, end;
808 for (pos = m_instructions.begin(), end = m_instructions.end();
809 pos != end;
810 ++pos)
811 {
812 uint32_t inst_size = (*pos)->GetOpcode().GetByteSize();
813 if (max_inst_size < inst_size)
814 max_inst_size = inst_size;
815 }
816 return max_inst_size;
817}
818
819
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820
Greg Clayton1d273162010-10-06 03:09:58 +0000821InstructionSP
822InstructionList::GetInstructionAtIndex (uint32_t idx) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000823{
Greg Clayton1d273162010-10-06 03:09:58 +0000824 InstructionSP inst_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000825 if (idx < m_instructions.size())
Greg Clayton1d273162010-10-06 03:09:58 +0000826 inst_sp = m_instructions[idx];
827 return inst_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000828}
829
830void
Greg Clayton1d273162010-10-06 03:09:58 +0000831InstructionList::Clear()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832{
833 m_instructions.clear();
834}
835
836void
Greg Clayton1d273162010-10-06 03:09:58 +0000837InstructionList::Append (lldb::InstructionSP &inst_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838{
839 if (inst_sp)
840 m_instructions.push_back(inst_sp);
841}
842
843
844size_t
845Disassembler::ParseInstructions
846(
847 const ExecutionContext *exe_ctx,
Greg Clayton357132e2011-03-26 19:14:58 +0000848 const AddressRange &range
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849)
850{
Greg Claytondda4f7b2010-06-30 23:03:03 +0000851 Target *target = exe_ctx->target;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000852 const addr_t byte_size = range.GetByteSize();
853 if (target == NULL || byte_size == 0 || !range.GetBaseAddress().IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854 return 0;
855
Greg Claytondda4f7b2010-06-30 23:03:03 +0000856 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
857 DataBufferSP data_sp(heap_buffer);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858
859 Error error;
Greg Clayton357132e2011-03-26 19:14:58 +0000860 const bool prefer_file_cache = true;
861 const size_t bytes_read = target->ReadMemory (range.GetBaseAddress(),
862 prefer_file_cache,
863 heap_buffer->GetBytes(),
864 heap_buffer->GetByteSize(),
865 error);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000866
867 if (bytes_read > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868 {
Greg Claytondda4f7b2010-06-30 23:03:03 +0000869 if (bytes_read != heap_buffer->GetByteSize())
870 heap_buffer->SetByteSize (bytes_read);
Greg Clayton357132e2011-03-26 19:14:58 +0000871 DataExtractor data (data_sp,
872 m_arch.GetByteOrder(),
873 m_arch.GetAddressByteSize());
Jim Ingham37023b02011-03-22 01:48:42 +0000874 return DecodeInstructions (range.GetBaseAddress(), data, 0, UINT32_MAX, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000875 }
876
877 return 0;
878}
879
Jim Ingham37023b02011-03-22 01:48:42 +0000880size_t
881Disassembler::ParseInstructions
882(
883 const ExecutionContext *exe_ctx,
884 const Address &start,
Greg Clayton357132e2011-03-26 19:14:58 +0000885 uint32_t num_instructions
Jim Ingham37023b02011-03-22 01:48:42 +0000886)
887{
Greg Clayton357132e2011-03-26 19:14:58 +0000888 m_instruction_list.Clear();
889
890 if (num_instructions == 0 || !start.IsValid())
Jim Ingham37023b02011-03-22 01:48:42 +0000891 return 0;
892
893 Target *target = exe_ctx->target;
Greg Clayton357132e2011-03-26 19:14:58 +0000894 // Calculate the max buffer size we will need in order to disassemble
895 const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize();
Jim Ingham37023b02011-03-22 01:48:42 +0000896
Greg Clayton357132e2011-03-26 19:14:58 +0000897 if (target == NULL || byte_size == 0)
Jim Ingham37023b02011-03-22 01:48:42 +0000898 return 0;
899
900 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
Greg Clayton357132e2011-03-26 19:14:58 +0000901 DataBufferSP data_sp (heap_buffer);
Jim Ingham37023b02011-03-22 01:48:42 +0000902
903 Error error;
904 bool prefer_file_cache = true;
Greg Clayton357132e2011-03-26 19:14:58 +0000905 const size_t bytes_read = target->ReadMemory (start,
906 prefer_file_cache,
907 heap_buffer->GetBytes(),
908 byte_size,
909 error);
910
911 if (bytes_read == 0)
912 return 0;
913 DataExtractor data (data_sp,
914 m_arch.GetByteOrder(),
915 m_arch.GetAddressByteSize());
916
917 const bool append_instructions = true;
918 DecodeInstructions (start,
919 data,
920 0,
921 num_instructions,
922 append_instructions);
923
Jim Ingham37023b02011-03-22 01:48:42 +0000924 return m_instruction_list.GetSize();
925}
926
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927//----------------------------------------------------------------------
928// Disassembler copy constructor
929//----------------------------------------------------------------------
930Disassembler::Disassembler(const ArchSpec& arch) :
931 m_arch (arch),
932 m_instruction_list(),
933 m_base_addr(LLDB_INVALID_ADDRESS)
934{
935
936}
937
938//----------------------------------------------------------------------
939// Destructor
940//----------------------------------------------------------------------
941Disassembler::~Disassembler()
942{
943}
944
Greg Clayton1d273162010-10-06 03:09:58 +0000945InstructionList &
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946Disassembler::GetInstructionList ()
947{
948 return m_instruction_list;
949}
950
Greg Clayton1d273162010-10-06 03:09:58 +0000951const InstructionList &
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952Disassembler::GetInstructionList () const
953{
954 return m_instruction_list;
955}
Caroline Tice3ac67112011-04-19 23:30:03 +0000956
957//----------------------------------------------------------------------
958// Class PseudoInstruction
959//----------------------------------------------------------------------
960PseudoInstruction::PseudoInstruction () :
961 Instruction (Address(), eAddressClassUnknown),
962 m_description ()
963{
964}
965
966PseudoInstruction::~PseudoInstruction ()
967{
968}
969
970void
971PseudoInstruction::Dump (lldb_private::Stream *s,
972 uint32_t max_opcode_byte_size,
973 bool show_address,
974 bool show_bytes,
975 const lldb_private::ExecutionContext* exe_ctx,
976 bool raw)
977{
978 if (!s)
979 return;
980
981 if (show_bytes)
982 m_opcode.Dump (s, max_opcode_byte_size);
983
984 if (m_description.size() > 0)
985 s->Printf ("%s", m_description.c_str());
986 else
987 s->Printf ("<unknown>");
988
989}
990
991bool
992PseudoInstruction::DoesBranch () const
993{
994 // This is NOT a valid question for a pseudo instruction.
995 return false;
996}
997
998size_t
999PseudoInstruction::Decode (const lldb_private::Disassembler &disassembler,
1000 const lldb_private::DataExtractor &data,
1001 uint32_t data_offset)
1002{
1003 return m_opcode.GetByteSize();
1004}
1005
1006
1007void
1008PseudoInstruction::SetOpcode (size_t opcode_size, void *opcode_data)
1009{
1010 if (!opcode_data)
1011 return;
1012
1013 switch (opcode_size)
1014 {
1015 case 8:
1016 {
1017 uint8_t value8 = *((uint8_t *) opcode_data);
1018 m_opcode.SetOpcode8 (value8);
1019 break;
1020 }
1021 case 16:
1022 {
1023 uint16_t value16 = *((uint16_t *) opcode_data);
1024 m_opcode.SetOpcode16 (value16);
1025 break;
1026 }
1027 case 32:
1028 {
1029 uint32_t value32 = *((uint32_t *) opcode_data);
1030 m_opcode.SetOpcode32 (value32);
1031 break;
1032 }
1033 case 64:
1034 {
1035 uint64_t value64 = *((uint64_t *) opcode_data);
1036 m_opcode.SetOpcode64 (value64);
1037 break;
1038 }
1039 default:
1040 break;
1041 }
1042}
1043
1044void
1045PseudoInstruction::SetDescription (const char *description)
1046{
1047 if (description && strlen (description) > 0)
1048 m_description = description;
1049}