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