blob: 6da90c275a8bb4f4c7550ab2c15c55374c3846c4 [file] [log] [blame]
Chris Lattner24943d22010-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 Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-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 Tice080bf612011-04-05 18:46:00 +000023#include "lldb/Core/EmulateInstruction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Core/Module.h"
25#include "lldb/Core/PluginManager.h"
Caroline Ticedfb2e202011-04-22 05:08:45 +000026#include "lldb/Core/RegularExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Core/Timer.h"
Greg Clayton73844aa2012-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 Callanan3e80cd92011-10-12 02:08:07 +000033#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000034#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-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 Callanan4f28c312012-08-01 18:50:59 +000047DisassemblerSP
Greg Clayton149731c2011-03-25 18:03:16 +000048Disassembler::FindPlugin (const ArchSpec &arch, const char *plugin_name)
Chris Lattner24943d22010-06-08 16:52:24 +000049{
50 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Clayton149731c2011-03-25 18:03:16 +000051 "Disassembler::FindPlugin (arch = %s, plugin_name = %s)",
52 arch.GetArchitectureName(),
53 plugin_name);
Chris Lattner24943d22010-06-08 16:52:24 +000054
Greg Clayton149731c2011-03-25 18:03:16 +000055 DisassemblerCreateInstance create_callback = NULL;
56
57 if (plugin_name)
Chris Lattner24943d22010-06-08 16:52:24 +000058 {
Greg Clayton149731c2011-03-25 18:03:16 +000059 create_callback = PluginManager::GetDisassemblerCreateCallbackForPluginName (plugin_name);
60 if (create_callback)
61 {
Sean Callanan4f28c312012-08-01 18:50:59 +000062 DisassemblerSP disassembler_sp(create_callback(arch));
Greg Clayton149731c2011-03-25 18:03:16 +000063
Sean Callanan4f28c312012-08-01 18:50:59 +000064 if (disassembler_sp.get())
65 return disassembler_sp;
Greg Clayton149731c2011-03-25 18:03:16 +000066 }
67 }
68 else
69 {
70 for (uint32_t idx = 0; (create_callback = PluginManager::GetDisassemblerCreateCallbackAtIndex(idx)) != NULL; ++idx)
71 {
Sean Callanan4f28c312012-08-01 18:50:59 +000072 DisassemblerSP disassembler_sp(create_callback(arch));
Chris Lattner24943d22010-06-08 16:52:24 +000073
Sean Callanan4f28c312012-08-01 18:50:59 +000074 if (disassembler_sp.get())
75 return disassembler_sp;
Greg Clayton149731c2011-03-25 18:03:16 +000076 }
Chris Lattner24943d22010-06-08 16:52:24 +000077 }
Sean Callanan4f28c312012-08-01 18:50:59 +000078 return DisassemblerSP();
Chris Lattner24943d22010-06-08 16:52:24 +000079}
80
Greg Clayton70436352010-06-30 23:03:03 +000081
Greg Clayton889fbd02011-03-26 19:14:58 +000082static void
83ResolveAddress (const ExecutionContext &exe_ctx,
84 const Address &addr,
85 Address &resolved_addr)
86{
87 if (!addr.IsSectionOffset())
88 {
89 // If we weren't passed in a section offset address range,
90 // try and resolve it to something
Greg Clayton567e7f32011-09-22 04:58:26 +000091 Target *target = exe_ctx.GetTargetPtr();
92 if (target)
Greg Clayton889fbd02011-03-26 19:14:58 +000093 {
Greg Clayton567e7f32011-09-22 04:58:26 +000094 if (target->GetSectionLoadList().IsEmpty())
Greg Clayton889fbd02011-03-26 19:14:58 +000095 {
Greg Clayton567e7f32011-09-22 04:58:26 +000096 target->GetImages().ResolveFileAddress (addr.GetOffset(), resolved_addr);
Greg Clayton889fbd02011-03-26 19:14:58 +000097 }
98 else
99 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000100 target->GetSectionLoadList().ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Clayton889fbd02011-03-26 19:14:58 +0000101 }
102 // We weren't able to resolve the address, just treat it as a
103 // raw address
104 if (resolved_addr.IsValid())
105 return;
106 }
107 }
108 resolved_addr = addr;
109}
Greg Clayton70436352010-06-30 23:03:03 +0000110
111size_t
112Disassembler::Disassemble
113(
114 Debugger &debugger,
115 const ArchSpec &arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000116 const char *plugin_name,
Greg Clayton70436352010-06-30 23:03:03 +0000117 const ExecutionContext &exe_ctx,
118 SymbolContextList &sc_list,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000119 uint32_t num_instructions,
Greg Clayton70436352010-06-30 23:03:03 +0000120 uint32_t num_mixed_context_lines,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000121 uint32_t options,
Greg Clayton70436352010-06-30 23:03:03 +0000122 Stream &strm
123)
124{
125 size_t success_count = 0;
126 const size_t count = sc_list.GetSize();
127 SymbolContext sc;
128 AddressRange range;
Greg Claytonff44ab42011-04-23 02:04:55 +0000129 const uint32_t scope = eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
130 const bool use_inline_block_range = true;
Greg Clayton70436352010-06-30 23:03:03 +0000131 for (size_t i=0; i<count; ++i)
132 {
133 if (sc_list.GetContextAtIndex(i, sc) == false)
134 break;
Greg Claytonff44ab42011-04-23 02:04:55 +0000135 for (uint32_t range_idx = 0; sc.GetAddressRange(scope, range_idx, use_inline_block_range, range); ++range_idx)
Greg Clayton70436352010-06-30 23:03:03 +0000136 {
Greg Clayton149731c2011-03-25 18:03:16 +0000137 if (Disassemble (debugger,
138 arch,
139 plugin_name,
140 exe_ctx,
141 range,
142 num_instructions,
143 num_mixed_context_lines,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000144 options,
Greg Clayton149731c2011-03-25 18:03:16 +0000145 strm))
Greg Clayton70436352010-06-30 23:03:03 +0000146 {
147 ++success_count;
148 strm.EOL();
149 }
150 }
151 }
152 return success_count;
153}
154
Chris Lattner24943d22010-06-08 16:52:24 +0000155bool
156Disassembler::Disassemble
157(
Greg Clayton63094e02010-06-23 01:19:29 +0000158 Debugger &debugger,
Chris Lattner24943d22010-06-08 16:52:24 +0000159 const ArchSpec &arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000160 const char *plugin_name,
Chris Lattner24943d22010-06-08 16:52:24 +0000161 const ExecutionContext &exe_ctx,
Greg Clayton70436352010-06-30 23:03:03 +0000162 const ConstString &name,
163 Module *module,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000164 uint32_t num_instructions,
Greg Clayton70436352010-06-30 23:03:03 +0000165 uint32_t num_mixed_context_lines,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000166 uint32_t options,
Chris Lattner24943d22010-06-08 16:52:24 +0000167 Stream &strm
168)
169{
Greg Clayton70436352010-06-30 23:03:03 +0000170 SymbolContextList sc_list;
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000171 if (name)
Chris Lattner24943d22010-06-08 16:52:24 +0000172 {
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000173 const bool include_symbols = true;
Sean Callanan302d78c2012-02-10 22:52:19 +0000174 const bool include_inlines = true;
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000175 if (module)
176 {
Sean Callanan3e80cd92011-10-12 02:08:07 +0000177 module->FindFunctions (name,
178 NULL,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000179 eFunctionNameTypeBase |
180 eFunctionNameTypeFull |
181 eFunctionNameTypeMethod |
182 eFunctionNameTypeSelector,
183 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +0000184 include_inlines,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000185 true,
186 sc_list);
187 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000188 else if (exe_ctx.GetTargetPtr())
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000189 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000190 exe_ctx.GetTargetPtr()->GetImages().FindFunctions (name,
191 eFunctionNameTypeBase |
192 eFunctionNameTypeFull |
193 eFunctionNameTypeMethod |
194 eFunctionNameTypeSelector,
Sean Callanan302d78c2012-02-10 22:52:19 +0000195 include_symbols,
196 include_inlines,
Greg Clayton567e7f32011-09-22 04:58:26 +0000197 false,
198 sc_list);
Greg Clayton70436352010-06-30 23:03:03 +0000199 }
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000200 }
201
202 if (sc_list.GetSize ())
203 {
204 return Disassemble (debugger,
205 arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000206 plugin_name,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000207 exe_ctx,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000208 sc_list,
209 num_instructions,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000210 num_mixed_context_lines,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000211 options,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000212 strm);
Greg Clayton70436352010-06-30 23:03:03 +0000213 }
214 return false;
215}
216
Greg Clayton5c4c7462010-10-06 03:09:58 +0000217
218lldb::DisassemblerSP
219Disassembler::DisassembleRange
220(
221 const ArchSpec &arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000222 const char *plugin_name,
Greg Clayton5c4c7462010-10-06 03:09:58 +0000223 const ExecutionContext &exe_ctx,
224 const AddressRange &range
225)
226{
227 lldb::DisassemblerSP disasm_sp;
228 if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid())
229 {
Sean Callanan4f28c312012-08-01 18:50:59 +0000230 disasm_sp = Disassembler::FindPlugin(arch, plugin_name);
Greg Clayton5c4c7462010-10-06 03:09:58 +0000231
232 if (disasm_sp)
233 {
Greg Clayton04e6ada2012-05-25 17:05:55 +0000234 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range, NULL);
Greg Clayton5c4c7462010-10-06 03:09:58 +0000235 if (bytes_disassembled == 0)
236 disasm_sp.reset();
237 }
238 }
239 return disasm_sp;
240}
241
Sean Callananef1f6902011-12-14 23:49:37 +0000242lldb::DisassemblerSP
243Disassembler::DisassembleBytes
244(
245 const ArchSpec &arch,
246 const char *plugin_name,
247 const Address &start,
248 const void *bytes,
Greg Claytona9893072012-03-06 22:24:44 +0000249 size_t length,
250 uint32_t num_instructions
Sean Callananef1f6902011-12-14 23:49:37 +0000251)
252{
253 lldb::DisassemblerSP disasm_sp;
254
255 if (bytes)
256 {
Sean Callanan4f28c312012-08-01 18:50:59 +0000257 disasm_sp = Disassembler::FindPlugin(arch, plugin_name);
Sean Callananef1f6902011-12-14 23:49:37 +0000258
259 if (disasm_sp)
260 {
261 DataExtractor data(bytes, length, arch.GetByteOrder(), arch.GetAddressByteSize());
262
263 (void)disasm_sp->DecodeInstructions (start,
264 data,
265 0,
Greg Claytona9893072012-03-06 22:24:44 +0000266 num_instructions,
Sean Callananef1f6902011-12-14 23:49:37 +0000267 false);
268 }
269 }
270
271 return disasm_sp;
272}
273
Greg Clayton5c4c7462010-10-06 03:09:58 +0000274
Greg Clayton70436352010-06-30 23:03:03 +0000275bool
276Disassembler::Disassemble
277(
278 Debugger &debugger,
279 const ArchSpec &arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000280 const char *plugin_name,
Greg Clayton70436352010-06-30 23:03:03 +0000281 const ExecutionContext &exe_ctx,
282 const AddressRange &disasm_range,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000283 uint32_t num_instructions,
Greg Clayton70436352010-06-30 23:03:03 +0000284 uint32_t num_mixed_context_lines,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000285 uint32_t options,
Greg Clayton70436352010-06-30 23:03:03 +0000286 Stream &strm
287)
288{
289 if (disasm_range.GetByteSize())
290 {
Sean Callanan4f28c312012-08-01 18:50:59 +0000291 lldb::DisassemblerSP disasm_sp (Disassembler::FindPlugin(arch, plugin_name));
Greg Clayton70436352010-06-30 23:03:03 +0000292
Sean Callanan4f28c312012-08-01 18:50:59 +0000293 if (disasm_sp.get())
Greg Clayton70436352010-06-30 23:03:03 +0000294 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000295 AddressRange range;
296 ResolveAddress (exe_ctx, disasm_range.GetBaseAddress(), range.GetBaseAddress());
297 range.SetByteSize (disasm_range.GetByteSize());
Greg Clayton70436352010-06-30 23:03:03 +0000298
Sean Callanan4f28c312012-08-01 18:50:59 +0000299 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, range, &strm);
Chris Lattner24943d22010-06-08 16:52:24 +0000300 if (bytes_disassembled == 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000301 return false;
Greg Clayton149731c2011-03-25 18:03:16 +0000302
Sean Callanan4f28c312012-08-01 18:50:59 +0000303 return PrintInstructions (disasm_sp.get(),
Greg Clayton149731c2011-03-25 18:03:16 +0000304 debugger,
305 arch,
306 exe_ctx,
Greg Clayton149731c2011-03-25 18:03:16 +0000307 num_instructions,
308 num_mixed_context_lines,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000309 options,
Greg Clayton149731c2011-03-25 18:03:16 +0000310 strm);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000311 }
312 }
313 return false;
314}
315
316bool
317Disassembler::Disassemble
318(
319 Debugger &debugger,
320 const ArchSpec &arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000321 const char *plugin_name,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000322 const ExecutionContext &exe_ctx,
323 const Address &start_address,
324 uint32_t num_instructions,
325 uint32_t num_mixed_context_lines,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000326 uint32_t options,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000327 Stream &strm
328)
329{
330 if (num_instructions > 0)
331 {
Sean Callanan4f28c312012-08-01 18:50:59 +0000332 lldb::DisassemblerSP disasm_sp (Disassembler::FindPlugin(arch, plugin_name));
333 if (disasm_sp.get())
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000334 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000335 Address addr;
336 ResolveAddress (exe_ctx, start_address, addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000337
Sean Callanan4f28c312012-08-01 18:50:59 +0000338 size_t bytes_disassembled = disasm_sp->ParseInstructions (&exe_ctx, addr, num_instructions);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000339 if (bytes_disassembled == 0)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000340 return false;
Sean Callanan4f28c312012-08-01 18:50:59 +0000341 return PrintInstructions (disasm_sp.get(),
Greg Clayton149731c2011-03-25 18:03:16 +0000342 debugger,
343 arch,
344 exe_ctx,
Greg Clayton149731c2011-03-25 18:03:16 +0000345 num_instructions,
346 num_mixed_context_lines,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000347 options,
Greg Clayton149731c2011-03-25 18:03:16 +0000348 strm);
Chris Lattner24943d22010-06-08 16:52:24 +0000349 }
Chris Lattner24943d22010-06-08 16:52:24 +0000350 }
351 return false;
352}
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000353
354bool
355Disassembler::PrintInstructions
356(
357 Disassembler *disasm_ptr,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000358 Debugger &debugger,
359 const ArchSpec &arch,
360 const ExecutionContext &exe_ctx,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000361 uint32_t num_instructions,
362 uint32_t num_mixed_context_lines,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000363 uint32_t options,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000364 Stream &strm
365)
366{
367 // We got some things disassembled...
368 size_t num_instructions_found = disasm_ptr->GetInstructionList().GetSize();
369
370 if (num_instructions > 0 && num_instructions < num_instructions_found)
371 num_instructions_found = num_instructions;
372
Greg Clayton889fbd02011-03-26 19:14:58 +0000373 const uint32_t max_opcode_byte_size = disasm_ptr->GetInstructionList().GetMaxOpcocdeByteSize ();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000374 uint32_t offset = 0;
375 SymbolContext sc;
376 SymbolContext prev_sc;
377 AddressRange sc_range;
Greg Clayton107e53d2011-07-06 04:07:21 +0000378 const Address *pc_addr_ptr = NULL;
Greg Claytonff44ab42011-04-23 02:04:55 +0000379 ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope();
Greg Clayton567e7f32011-09-22 04:58:26 +0000380 StackFrame *frame = exe_ctx.GetFramePtr();
381
382 if (frame)
383 pc_addr_ptr = &frame->GetFrameCodeAddress();
Greg Claytonff44ab42011-04-23 02:04:55 +0000384 const uint32_t scope = eSymbolContextLineEntry | eSymbolContextFunction | eSymbolContextSymbol;
385 const bool use_inline_block_range = false;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000386 for (size_t i=0; i<num_instructions_found; ++i)
387 {
388 Instruction *inst = disasm_ptr->GetInstructionList().GetInstructionAtIndex (i).get();
389 if (inst)
390 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000391 const Address &addr = inst->GetAddress();
392 const bool inst_is_at_pc = pc_addr_ptr && addr == *pc_addr_ptr;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000393
394 prev_sc = sc;
395
Greg Clayton3508c382012-02-24 01:59:29 +0000396 ModuleSP module_sp (addr.GetModule());
397 if (module_sp)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000398 {
Greg Clayton3508c382012-02-24 01:59:29 +0000399 uint32_t resolved_mask = module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, sc);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000400 if (resolved_mask)
401 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000402 if (num_mixed_context_lines)
403 {
404 if (!sc_range.ContainsFileAddress (addr))
405 {
Greg Claytonff44ab42011-04-23 02:04:55 +0000406 sc.GetAddressRange (scope, 0, use_inline_block_range, sc_range);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000407
408 if (sc != prev_sc)
409 {
410 if (offset != 0)
411 strm.EOL();
412
Greg Clayton567e7f32011-09-22 04:58:26 +0000413 sc.DumpStopContext(&strm, exe_ctx.GetProcessPtr(), addr, false, true, false);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000414 strm.EOL();
415
416 if (sc.comp_unit && sc.line_entry.IsValid())
417 {
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000418 debugger.GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.line_entry.file,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000419 sc.line_entry.line,
420 num_mixed_context_lines,
421 num_mixed_context_lines,
Greg Clayton6377f5c2011-06-28 19:01:40 +0000422 ((inst_is_at_pc && (options & eOptionMarkPCSourceLine)) ? "->" : ""),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000423 &strm);
424 }
425 }
426 }
427 }
Greg Claytone9570392011-11-30 19:36:42 +0000428 else if ((sc.function || sc.symbol) && (sc.function != prev_sc.function || sc.symbol != prev_sc.symbol))
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000429 {
430 if (prev_sc.function || prev_sc.symbol)
431 strm.EOL();
432
Greg Claytonff44ab42011-04-23 02:04:55 +0000433 bool show_fullpaths = false;
434 bool show_module = true;
435 bool show_inlined_frames = true;
436 sc.DumpStopContext (&strm,
437 exe_scope,
438 addr,
439 show_fullpaths,
440 show_module,
441 show_inlined_frames);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000442
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000443 strm << ":\n";
444 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000445 }
446 else
447 {
448 sc.Clear();
449 }
450 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000451
Greg Clayton6377f5c2011-06-28 19:01:40 +0000452 if ((options & eOptionMarkPCAddress) && pc_addr_ptr)
Greg Clayton24bc5d92011-03-30 18:16:51 +0000453 {
Greg Clayton6377f5c2011-06-28 19:01:40 +0000454 strm.PutCString(inst_is_at_pc ? "-> " : " ");
Greg Clayton24bc5d92011-03-30 18:16:51 +0000455 }
Greg Clayton4bb0f192011-06-22 01:39:49 +0000456 const bool show_bytes = (options & eOptionShowBytes) != 0;
Greg Clayton0fef9682012-05-10 02:52:23 +0000457 inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000458 strm.EOL();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000459 }
460 else
461 {
462 break;
463 }
464 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000465
466 return true;
467}
Chris Lattner24943d22010-06-08 16:52:24 +0000468
Greg Clayton70436352010-06-30 23:03:03 +0000469
470bool
471Disassembler::Disassemble
472(
473 Debugger &debugger,
474 const ArchSpec &arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000475 const char *plugin_name,
Greg Clayton70436352010-06-30 23:03:03 +0000476 const ExecutionContext &exe_ctx,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000477 uint32_t num_instructions,
Greg Clayton70436352010-06-30 23:03:03 +0000478 uint32_t num_mixed_context_lines,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000479 uint32_t options,
Greg Clayton70436352010-06-30 23:03:03 +0000480 Stream &strm
481)
482{
483 AddressRange range;
Greg Clayton567e7f32011-09-22 04:58:26 +0000484 StackFrame *frame = exe_ctx.GetFramePtr();
485 if (frame)
Greg Clayton70436352010-06-30 23:03:03 +0000486 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000487 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
Greg Clayton70436352010-06-30 23:03:03 +0000488 if (sc.function)
489 {
490 range = sc.function->GetAddressRange();
491 }
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000492 else if (sc.symbol && sc.symbol->ValueIsAddress())
Greg Clayton70436352010-06-30 23:03:03 +0000493 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000494 range.GetBaseAddress() = sc.symbol->GetAddress();
495 range.SetByteSize (sc.symbol->GetByteSize());
Greg Clayton70436352010-06-30 23:03:03 +0000496 }
497 else
498 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000499 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Greg Clayton70436352010-06-30 23:03:03 +0000500 }
501
502 if (range.GetBaseAddress().IsValid() && range.GetByteSize() == 0)
503 range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE);
504 }
505
Greg Clayton149731c2011-03-25 18:03:16 +0000506 return Disassemble (debugger,
507 arch,
508 plugin_name,
509 exe_ctx,
510 range,
511 num_instructions,
512 num_mixed_context_lines,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000513 options,
Greg Clayton149731c2011-03-25 18:03:16 +0000514 strm);
Greg Clayton70436352010-06-30 23:03:03 +0000515}
516
Greg Clayton889fbd02011-03-26 19:14:58 +0000517Instruction::Instruction(const Address &address, AddressClass addr_class) :
Greg Clayton149731c2011-03-25 18:03:16 +0000518 m_address (address),
Greg Clayton889fbd02011-03-26 19:14:58 +0000519 m_address_class (addr_class),
Sean Callanan8da54892012-02-14 00:22:51 +0000520 m_opcode(),
521 m_calculated_strings(false)
Greg Clayton7bc39082011-03-24 23:53:38 +0000522{
Chris Lattner24943d22010-06-08 16:52:24 +0000523}
524
Greg Clayton5c4c7462010-10-06 03:09:58 +0000525Instruction::~Instruction()
Chris Lattner24943d22010-06-08 16:52:24 +0000526{
527}
528
Greg Clayton889fbd02011-03-26 19:14:58 +0000529AddressClass
530Instruction::GetAddressClass ()
531{
532 if (m_address_class == eAddressClassInvalid)
533 m_address_class = m_address.GetAddressClass();
534 return m_address_class;
535}
Chris Lattner24943d22010-06-08 16:52:24 +0000536
Greg Clayton0fef9682012-05-10 02:52:23 +0000537void
538Instruction::Dump (lldb_private::Stream *s,
539 uint32_t max_opcode_byte_size,
540 bool show_address,
541 bool show_bytes,
542 const ExecutionContext* exe_ctx)
543{
544 const size_t opcode_column_width = 7;
545 const size_t operand_column_width = 25;
546
547 CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx);
548
549 StreamString ss;
550
551 if (show_address)
552 {
553 m_address.Dump(&ss,
554 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL,
555 Address::DumpStyleLoadAddress,
556 Address::DumpStyleModuleWithFileAddress,
557 0);
558
559 ss.PutCString(": ");
560 }
561
562 if (show_bytes)
563 {
564 if (m_opcode.GetType() == Opcode::eTypeBytes)
565 {
566 // x86_64 and i386 are the only ones that use bytes right now so
567 // pad out the byte dump to be able to always show 15 bytes (3 chars each)
568 // plus a space
569 if (max_opcode_byte_size > 0)
570 m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
571 else
572 m_opcode.Dump (&ss, 15 * 3 + 1);
573 }
574 else
575 {
576 // Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces)
577 // plus two for padding...
578 if (max_opcode_byte_size > 0)
579 m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
580 else
581 m_opcode.Dump (&ss, 12);
582 }
583 }
584
585 const size_t opcode_pos = ss.GetSize();
586
587 ss.PutCString (m_opcode_name.c_str());
588 ss.FillLastLineToColumn (opcode_pos + opcode_column_width, ' ');
589 ss.PutCString (m_mnemocics.c_str());
590
591 if (!m_comment.empty())
592 {
593 ss.FillLastLineToColumn (opcode_pos + opcode_column_width + operand_column_width, ' ');
594 ss.PutCString (" ; ");
595 ss.PutCString (m_comment.c_str());
596 }
597 s->Write (ss.GetData(), ss.GetSize());
598}
599
Caroline Ticeaf591802011-04-05 23:22:54 +0000600bool
601Instruction::DumpEmulation (const ArchSpec &arch)
602{
Greg Clayton888a7332011-04-26 04:39:08 +0000603 std::auto_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
Caroline Ticeaf591802011-04-05 23:22:54 +0000604 if (insn_emulator_ap.get())
605 {
Greg Clayton888a7332011-04-26 04:39:08 +0000606 insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL);
607 return insn_emulator_ap->EvaluateInstruction (0);
Caroline Ticeaf591802011-04-05 23:22:54 +0000608 }
609
610 return false;
611}
612
Caroline Ticedfb2e202011-04-22 05:08:45 +0000613OptionValueSP
614Instruction::ReadArray (FILE *in_file, Stream *out_stream, OptionValue::Type data_type)
615{
616 bool done = false;
617 char buffer[1024];
618
619 OptionValueSP option_value_sp (new OptionValueArray (1u << data_type));
620
621 int idx = 0;
622 while (!done)
623 {
624 if (!fgets (buffer, 1023, in_file))
625 {
Greg Claytone40b6422011-09-18 18:59:15 +0000626 out_stream->Printf ("Instruction::ReadArray: Error reading file (fgets).\n");
Caroline Ticedfb2e202011-04-22 05:08:45 +0000627 option_value_sp.reset ();
628 return option_value_sp;
629 }
630
631 std::string line (buffer);
632
633 int len = line.size();
634 if (line[len-1] == '\n')
635 {
636 line[len-1] = '\0';
637 line.resize (len-1);
638 }
639
640 if ((line.size() == 1) && line[0] == ']')
641 {
642 done = true;
643 line.clear();
644 }
645
646 if (line.size() > 0)
647 {
648 std::string value;
649 RegularExpression reg_exp ("^[ \t]*([^ \t]+)[ \t]*$");
650 bool reg_exp_success = reg_exp.Execute (line.c_str(), 1);
651 if (reg_exp_success)
652 reg_exp.GetMatchAtIndex (line.c_str(), 1, value);
653 else
654 value = line;
655
656 OptionValueSP data_value_sp;
657 switch (data_type)
658 {
659 case OptionValue::eTypeUInt64:
660 data_value_sp.reset (new OptionValueUInt64 (0, 0));
661 data_value_sp->SetValueFromCString (value.c_str());
662 break;
663 // Other types can be added later as needed.
664 default:
665 data_value_sp.reset (new OptionValueString (value.c_str(), ""));
666 break;
667 }
668
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000669 option_value_sp->GetAsArray()->InsertValue (idx, data_value_sp);
Caroline Ticedfb2e202011-04-22 05:08:45 +0000670 ++idx;
671 }
672 }
673
674 return option_value_sp;
675}
676
677OptionValueSP
678Instruction::ReadDictionary (FILE *in_file, Stream *out_stream)
679{
680 bool done = false;
681 char buffer[1024];
682
683 OptionValueSP option_value_sp (new OptionValueDictionary());
684 static ConstString encoding_key ("data_encoding");
685 OptionValue::Type data_type = OptionValue::eTypeInvalid;
686
687
688 while (!done)
689 {
690 // Read the next line in the file
691 if (!fgets (buffer, 1023, in_file))
692 {
693 out_stream->Printf ("Instruction::ReadDictionary: Error reading file (fgets).\n");
694 option_value_sp.reset ();
695 return option_value_sp;
696 }
697
698 // Check to see if the line contains the end-of-dictionary marker ("}")
699 std::string line (buffer);
700
701 int len = line.size();
702 if (line[len-1] == '\n')
703 {
704 line[len-1] = '\0';
705 line.resize (len-1);
706 }
707
708 if ((line.size() == 1) && (line[0] == '}'))
709 {
710 done = true;
711 line.clear();
712 }
713
714 // Try to find a key-value pair in the current line and add it to the dictionary.
715 if (line.size() > 0)
716 {
717 RegularExpression reg_exp ("^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*=[ \t]*(.*)[ \t]*$");
718 bool reg_exp_success = reg_exp.Execute (line.c_str(), 2);
719 std::string key;
720 std::string value;
721 if (reg_exp_success)
722 {
723 reg_exp.GetMatchAtIndex (line.c_str(), 1, key);
724 reg_exp.GetMatchAtIndex (line.c_str(), 2, value);
725 }
726 else
727 {
728 out_stream->Printf ("Instruction::ReadDictionary: Failure executing regular expression.\n");
729 option_value_sp.reset();
730 return option_value_sp;
731 }
732
733 ConstString const_key (key.c_str());
734 // Check value to see if it's the start of an array or dictionary.
735
736 lldb::OptionValueSP value_sp;
737 assert (value.empty() == false);
738 assert (key.empty() == false);
739
740 if (value[0] == '{')
741 {
742 assert (value.size() == 1);
743 // value is a dictionary
744 value_sp = ReadDictionary (in_file, out_stream);
745 if (value_sp.get() == NULL)
746 {
747 option_value_sp.reset ();
748 return option_value_sp;
749 }
750 }
751 else if (value[0] == '[')
752 {
753 assert (value.size() == 1);
754 // value is an array
755 value_sp = ReadArray (in_file, out_stream, data_type);
756 if (value_sp.get() == NULL)
757 {
758 option_value_sp.reset ();
759 return option_value_sp;
760 }
761 // We've used the data_type to read an array; re-set the type to Invalid
762 data_type = OptionValue::eTypeInvalid;
763 }
764 else if ((value[0] == '0') && (value[1] == 'x'))
765 {
766 value_sp.reset (new OptionValueUInt64 (0, 0));
767 value_sp->SetValueFromCString (value.c_str());
768 }
769 else
770 {
771 int len = value.size();
772 if ((value[0] == '"') && (value[len-1] == '"'))
773 value = value.substr (1, len-2);
774 value_sp.reset (new OptionValueString (value.c_str(), ""));
775 }
776
777
778
779 if (const_key == encoding_key)
780 {
781 // A 'data_encoding=..." is NOT a normal key-value pair; it is meta-data indicating the
782 // data type of an upcoming array (usually the next bit of data to be read in).
783 if (strcmp (value.c_str(), "uint32_t") == 0)
784 data_type = OptionValue::eTypeUInt64;
785 }
786 else
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000787 option_value_sp->GetAsDictionary()->SetValueForKey (const_key, value_sp, false);
Caroline Ticedfb2e202011-04-22 05:08:45 +0000788 }
789 }
790
791 return option_value_sp;
792}
793
Caroline Ticeaf591802011-04-05 23:22:54 +0000794bool
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000795Instruction::TestEmulation (Stream *out_stream, const char *file_name)
796{
797 if (!out_stream)
798 return false;
799
800 if (!file_name)
801 {
Johnny Chen09008d02011-04-21 20:27:45 +0000802 out_stream->Printf ("Instruction::TestEmulation: Missing file_name.");
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000803 return false;
804 }
805
806 FILE *test_file = fopen (file_name, "r");
807 if (!test_file)
808 {
Johnny Chen09008d02011-04-21 20:27:45 +0000809 out_stream->Printf ("Instruction::TestEmulation: Attempt to open test file failed.");
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000810 return false;
811 }
812
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000813 char buffer[256];
Caroline Ticedfb2e202011-04-22 05:08:45 +0000814 if (!fgets (buffer, 255, test_file))
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000815 {
Caroline Ticedfb2e202011-04-22 05:08:45 +0000816 out_stream->Printf ("Instruction::TestEmulation: Error reading first line of test file.\n");
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000817 fclose (test_file);
818 return false;
819 }
820
Caroline Ticedfb2e202011-04-22 05:08:45 +0000821 if (strncmp (buffer, "InstructionEmulationState={", 27) != 0)
822 {
823 out_stream->Printf ("Instructin::TestEmulation: Test file does not contain emulation state dictionary\n");
824 fclose (test_file);
825 return false;
826 }
827
828 // Read all the test information from the test file into an OptionValueDictionary.
829
830 OptionValueSP data_dictionary_sp (ReadDictionary (test_file, out_stream));
831 if (data_dictionary_sp.get() == NULL)
832 {
833 out_stream->Printf ("Instruction::TestEmulation: Error reading Dictionary Object.\n");
834 fclose (test_file);
835 return false;
836 }
837
838 fclose (test_file);
839
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000840 OptionValueDictionary *data_dictionary = data_dictionary_sp->GetAsDictionary();
Caroline Ticedfb2e202011-04-22 05:08:45 +0000841 static ConstString description_key ("assembly_string");
842 static ConstString triple_key ("triple");
843
844 OptionValueSP value_sp = data_dictionary->GetValueForKey (description_key);
845
846 if (value_sp.get() == NULL)
847 {
848 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain description string.\n");
849 return false;
850 }
851
852 SetDescription (value_sp->GetStringValue());
853
854
855 value_sp = data_dictionary->GetValueForKey (triple_key);
856 if (value_sp.get() == NULL)
857 {
858 out_stream->Printf ("Instruction::TestEmulation: Test file does not contain triple.\n");
859 return false;
860 }
861
862 ArchSpec arch;
863 arch.SetTriple (llvm::Triple (value_sp->GetStringValue()));
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000864
865 bool success = false;
Greg Clayton888a7332011-04-26 04:39:08 +0000866 std::auto_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000867 if (insn_emulator_ap.get())
Caroline Ticedfb2e202011-04-22 05:08:45 +0000868 success = insn_emulator_ap->TestEmulation (out_stream, arch, data_dictionary);
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000869
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000870 if (success)
Johnny Chen09008d02011-04-21 20:27:45 +0000871 out_stream->Printf ("Emulation test succeeded.");
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000872 else
Johnny Chen09008d02011-04-21 20:27:45 +0000873 out_stream->Printf ("Emulation test failed.");
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000874
875 return success;
876}
877
878bool
Caroline Ticeaf591802011-04-05 23:22:54 +0000879Instruction::Emulate (const ArchSpec &arch,
Greg Clayton888a7332011-04-26 04:39:08 +0000880 uint32_t evaluate_options,
Caroline Ticeaf591802011-04-05 23:22:54 +0000881 void *baton,
Greg Clayton061b79d2011-05-09 20:18:18 +0000882 EmulateInstruction::ReadMemoryCallback read_mem_callback,
883 EmulateInstruction::WriteMemoryCallback write_mem_callback,
884 EmulateInstruction::ReadRegisterCallback read_reg_callback,
885 EmulateInstruction::WriteRegisterCallback write_reg_callback)
Caroline Ticeaf591802011-04-05 23:22:54 +0000886{
Greg Clayton888a7332011-04-26 04:39:08 +0000887 std::auto_ptr<EmulateInstruction> insn_emulator_ap (EmulateInstruction::FindPlugin (arch, eInstructionTypeAny, NULL));
Caroline Ticeaf591802011-04-05 23:22:54 +0000888 if (insn_emulator_ap.get())
889 {
890 insn_emulator_ap->SetBaton (baton);
891 insn_emulator_ap->SetCallbacks (read_mem_callback, write_mem_callback, read_reg_callback, write_reg_callback);
Greg Clayton888a7332011-04-26 04:39:08 +0000892 insn_emulator_ap->SetInstruction (GetOpcode(), GetAddress(), NULL);
893 return insn_emulator_ap->EvaluateInstruction (evaluate_options);
Caroline Ticeaf591802011-04-05 23:22:54 +0000894 }
895
896 return false;
897}
898
Greg Clayton0fef9682012-05-10 02:52:23 +0000899
900uint32_t
901Instruction::GetData (DataExtractor &data)
902{
Sean Callananb42f1c82012-08-07 01:44:58 +0000903 return m_opcode.GetData(data);
Greg Clayton0fef9682012-05-10 02:52:23 +0000904}
905
Greg Clayton5c4c7462010-10-06 03:09:58 +0000906InstructionList::InstructionList() :
Chris Lattner24943d22010-06-08 16:52:24 +0000907 m_instructions()
908{
909}
910
Greg Clayton5c4c7462010-10-06 03:09:58 +0000911InstructionList::~InstructionList()
Chris Lattner24943d22010-06-08 16:52:24 +0000912{
913}
914
915size_t
Greg Clayton5c4c7462010-10-06 03:09:58 +0000916InstructionList::GetSize() const
Chris Lattner24943d22010-06-08 16:52:24 +0000917{
918 return m_instructions.size();
919}
920
Greg Clayton889fbd02011-03-26 19:14:58 +0000921uint32_t
922InstructionList::GetMaxOpcocdeByteSize () const
923{
924 uint32_t max_inst_size = 0;
925 collection::const_iterator pos, end;
926 for (pos = m_instructions.begin(), end = m_instructions.end();
927 pos != end;
928 ++pos)
929 {
930 uint32_t inst_size = (*pos)->GetOpcode().GetByteSize();
931 if (max_inst_size < inst_size)
932 max_inst_size = inst_size;
933 }
934 return max_inst_size;
935}
936
937
Chris Lattner24943d22010-06-08 16:52:24 +0000938
Greg Clayton5c4c7462010-10-06 03:09:58 +0000939InstructionSP
940InstructionList::GetInstructionAtIndex (uint32_t idx) const
Chris Lattner24943d22010-06-08 16:52:24 +0000941{
Greg Clayton5c4c7462010-10-06 03:09:58 +0000942 InstructionSP inst_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000943 if (idx < m_instructions.size())
Greg Clayton5c4c7462010-10-06 03:09:58 +0000944 inst_sp = m_instructions[idx];
945 return inst_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000946}
947
948void
Greg Clayton24a6bd92011-10-27 17:55:14 +0000949InstructionList::Dump (Stream *s,
950 bool show_address,
951 bool show_bytes,
952 const ExecutionContext* exe_ctx)
953{
954 const uint32_t max_opcode_byte_size = GetMaxOpcocdeByteSize();
955 collection::const_iterator pos, begin, end;
956 for (begin = m_instructions.begin(), end = m_instructions.end(), pos = begin;
957 pos != end;
958 ++pos)
959 {
960 if (pos != begin)
961 s->EOL();
Greg Clayton0fef9682012-05-10 02:52:23 +0000962 (*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx);
Greg Clayton24a6bd92011-10-27 17:55:14 +0000963 }
964}
965
966
967void
Greg Clayton5c4c7462010-10-06 03:09:58 +0000968InstructionList::Clear()
Chris Lattner24943d22010-06-08 16:52:24 +0000969{
970 m_instructions.clear();
971}
972
973void
Greg Clayton5c4c7462010-10-06 03:09:58 +0000974InstructionList::Append (lldb::InstructionSP &inst_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000975{
976 if (inst_sp)
977 m_instructions.push_back(inst_sp);
978}
979
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000980uint32_t
981InstructionList::GetIndexOfNextBranchInstruction(uint32_t start) const
982{
983 size_t num_instructions = m_instructions.size();
984
985 uint32_t next_branch = UINT32_MAX;
986 for (size_t i = start; i < num_instructions; i++)
987 {
988 if (m_instructions[i]->DoesBranch())
989 {
990 next_branch = i;
991 break;
992 }
993 }
994 return next_branch;
995}
996
997uint32_t
998InstructionList::GetIndexOfInstructionAtLoadAddress (lldb::addr_t load_addr, Target &target)
999{
1000 Address address;
1001 address.SetLoadAddress(load_addr, &target);
1002 uint32_t num_instructions = m_instructions.size();
1003 uint32_t index = UINT32_MAX;
1004 for (int i = 0; i < num_instructions; i++)
1005 {
1006 if (m_instructions[i]->GetAddress() == address)
1007 {
1008 index = i;
1009 break;
1010 }
1011 }
1012 return index;
1013}
Chris Lattner24943d22010-06-08 16:52:24 +00001014
1015size_t
1016Disassembler::ParseInstructions
1017(
1018 const ExecutionContext *exe_ctx,
Greg Clayton04e6ada2012-05-25 17:05:55 +00001019 const AddressRange &range,
1020 Stream *error_strm_ptr
Chris Lattner24943d22010-06-08 16:52:24 +00001021)
1022{
Greg Clayton567e7f32011-09-22 04:58:26 +00001023 if (exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001024 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001025 Target *target = exe_ctx->GetTargetPtr();
1026 const addr_t byte_size = range.GetByteSize();
1027 if (target == NULL || byte_size == 0 || !range.GetBaseAddress().IsValid())
1028 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001029
Greg Clayton567e7f32011-09-22 04:58:26 +00001030 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
1031 DataBufferSP data_sp(heap_buffer);
1032
1033 Error error;
1034 const bool prefer_file_cache = true;
1035 const size_t bytes_read = target->ReadMemory (range.GetBaseAddress(),
1036 prefer_file_cache,
1037 heap_buffer->GetBytes(),
1038 heap_buffer->GetByteSize(),
1039 error);
1040
1041 if (bytes_read > 0)
1042 {
1043 if (bytes_read != heap_buffer->GetByteSize())
1044 heap_buffer->SetByteSize (bytes_read);
1045 DataExtractor data (data_sp,
1046 m_arch.GetByteOrder(),
1047 m_arch.GetAddressByteSize());
1048 return DecodeInstructions (range.GetBaseAddress(), data, 0, UINT32_MAX, false);
1049 }
Greg Clayton04e6ada2012-05-25 17:05:55 +00001050 else if (error_strm_ptr)
1051 {
1052 const char *error_cstr = error.AsCString();
1053 if (error_cstr)
1054 {
1055 error_strm_ptr->Printf("error: %s\n", error_cstr);
1056 }
1057 }
1058 }
1059 else if (error_strm_ptr)
1060 {
1061 error_strm_ptr->PutCString("error: invalid execution context\n");
Greg Clayton567e7f32011-09-22 04:58:26 +00001062 }
Chris Lattner24943d22010-06-08 16:52:24 +00001063 return 0;
1064}
1065
Jim Inghamaa3e3e12011-03-22 01:48:42 +00001066size_t
1067Disassembler::ParseInstructions
1068(
1069 const ExecutionContext *exe_ctx,
1070 const Address &start,
Greg Clayton889fbd02011-03-26 19:14:58 +00001071 uint32_t num_instructions
Jim Inghamaa3e3e12011-03-22 01:48:42 +00001072)
1073{
Greg Clayton889fbd02011-03-26 19:14:58 +00001074 m_instruction_list.Clear();
1075
Greg Clayton567e7f32011-09-22 04:58:26 +00001076 if (exe_ctx == NULL || num_instructions == 0 || !start.IsValid())
Jim Inghamaa3e3e12011-03-22 01:48:42 +00001077 return 0;
1078
Greg Clayton567e7f32011-09-22 04:58:26 +00001079 Target *target = exe_ctx->GetTargetPtr();
Greg Clayton889fbd02011-03-26 19:14:58 +00001080 // Calculate the max buffer size we will need in order to disassemble
1081 const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize();
Jim Inghamaa3e3e12011-03-22 01:48:42 +00001082
Greg Clayton889fbd02011-03-26 19:14:58 +00001083 if (target == NULL || byte_size == 0)
Jim Inghamaa3e3e12011-03-22 01:48:42 +00001084 return 0;
1085
1086 DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
Greg Clayton889fbd02011-03-26 19:14:58 +00001087 DataBufferSP data_sp (heap_buffer);
Jim Inghamaa3e3e12011-03-22 01:48:42 +00001088
1089 Error error;
1090 bool prefer_file_cache = true;
Greg Clayton889fbd02011-03-26 19:14:58 +00001091 const size_t bytes_read = target->ReadMemory (start,
1092 prefer_file_cache,
1093 heap_buffer->GetBytes(),
1094 byte_size,
1095 error);
1096
1097 if (bytes_read == 0)
1098 return 0;
1099 DataExtractor data (data_sp,
1100 m_arch.GetByteOrder(),
1101 m_arch.GetAddressByteSize());
1102
1103 const bool append_instructions = true;
1104 DecodeInstructions (start,
1105 data,
1106 0,
1107 num_instructions,
1108 append_instructions);
1109
Jim Inghamaa3e3e12011-03-22 01:48:42 +00001110 return m_instruction_list.GetSize();
1111}
1112
Chris Lattner24943d22010-06-08 16:52:24 +00001113//----------------------------------------------------------------------
1114// Disassembler copy constructor
1115//----------------------------------------------------------------------
1116Disassembler::Disassembler(const ArchSpec& arch) :
1117 m_arch (arch),
1118 m_instruction_list(),
1119 m_base_addr(LLDB_INVALID_ADDRESS)
1120{
1121
1122}
1123
1124//----------------------------------------------------------------------
1125// Destructor
1126//----------------------------------------------------------------------
1127Disassembler::~Disassembler()
1128{
1129}
1130
Greg Clayton5c4c7462010-10-06 03:09:58 +00001131InstructionList &
Chris Lattner24943d22010-06-08 16:52:24 +00001132Disassembler::GetInstructionList ()
1133{
1134 return m_instruction_list;
1135}
1136
Greg Clayton5c4c7462010-10-06 03:09:58 +00001137const InstructionList &
Chris Lattner24943d22010-06-08 16:52:24 +00001138Disassembler::GetInstructionList () const
1139{
1140 return m_instruction_list;
1141}
Caroline Tice6b8d3b52011-04-19 23:30:03 +00001142
1143//----------------------------------------------------------------------
1144// Class PseudoInstruction
1145//----------------------------------------------------------------------
1146PseudoInstruction::PseudoInstruction () :
1147 Instruction (Address(), eAddressClassUnknown),
1148 m_description ()
1149{
1150}
1151
1152PseudoInstruction::~PseudoInstruction ()
1153{
1154}
1155
Caroline Tice6b8d3b52011-04-19 23:30:03 +00001156bool
1157PseudoInstruction::DoesBranch () const
1158{
1159 // This is NOT a valid question for a pseudo instruction.
1160 return false;
1161}
1162
1163size_t
1164PseudoInstruction::Decode (const lldb_private::Disassembler &disassembler,
1165 const lldb_private::DataExtractor &data,
1166 uint32_t data_offset)
1167{
1168 return m_opcode.GetByteSize();
1169}
1170
1171
1172void
1173PseudoInstruction::SetOpcode (size_t opcode_size, void *opcode_data)
1174{
1175 if (!opcode_data)
1176 return;
1177
1178 switch (opcode_size)
1179 {
1180 case 8:
1181 {
1182 uint8_t value8 = *((uint8_t *) opcode_data);
1183 m_opcode.SetOpcode8 (value8);
1184 break;
1185 }
1186 case 16:
1187 {
1188 uint16_t value16 = *((uint16_t *) opcode_data);
1189 m_opcode.SetOpcode16 (value16);
1190 break;
1191 }
1192 case 32:
1193 {
1194 uint32_t value32 = *((uint32_t *) opcode_data);
1195 m_opcode.SetOpcode32 (value32);
1196 break;
1197 }
1198 case 64:
1199 {
1200 uint64_t value64 = *((uint64_t *) opcode_data);
1201 m_opcode.SetOpcode64 (value64);
1202 break;
1203 }
1204 default:
1205 break;
1206 }
1207}
1208
1209void
1210PseudoInstruction::SetDescription (const char *description)
1211{
1212 if (description && strlen (description) > 0)
1213 m_description = description;
1214}