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