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