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