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 #include <limits>
17 
18 // Other libraries and framework includes
19 // Project includes
20 #include "lldb/lldb-private.h"
21 #include "lldb/Core/Error.h"
22 #include "lldb/Core/DataBufferHeap.h"
23 #include "lldb/Core/DataExtractor.h"
24 #include "lldb/Core/Debugger.h"
25 #include "lldb/Core/EmulateInstruction.h"
26 #include "lldb/Core/Module.h"
27 #include "lldb/Core/PluginManager.h"
28 #include "lldb/Core/RegularExpression.h"
29 #include "lldb/Core/Timer.h"
30 #include "lldb/Interpreter/OptionValue.h"
31 #include "lldb/Interpreter/OptionValueArray.h"
32 #include "lldb/Interpreter/OptionValueDictionary.h"
33 #include "lldb/Interpreter/OptionValueString.h"
34 #include "lldb/Interpreter/OptionValueUInt64.h"
35 #include "lldb/Symbol/Function.h"
36 #include "lldb/Symbol/ObjectFile.h"
37 #include "lldb/Target/ExecutionContext.h"
38 #include "lldb/Target/Process.h"
39 #include "lldb/Target/SectionLoadList.h"
40 #include "lldb/Target/StackFrame.h"
41 #include "lldb/Target/Target.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 
854     FILE *test_file = fopen (file_name, "r");
855     if (!test_file)
856     {
857         out_stream->Printf ("Instruction::TestEmulation: Attempt to open test file failed.");
858         return false;
859     }
860 
861     char buffer[256];
862     if (!fgets (buffer, 255, test_file))
863     {
864         out_stream->Printf ("Instruction::TestEmulation: Error reading first line of test file.\n");
865         fclose (test_file);
866         return false;
867     }
868 
869     if (strncmp (buffer, "InstructionEmulationState={", 27) != 0)
870     {
871         out_stream->Printf ("Instructin::TestEmulation: Test file does not contain emulation state dictionary\n");
872         fclose (test_file);
873         return false;
874     }
875 
876     // Read all the test information from the test file into an OptionValueDictionary.
877 
878     OptionValueSP data_dictionary_sp (ReadDictionary (test_file, out_stream));
879     if (!data_dictionary_sp)
880     {
881         out_stream->Printf ("Instruction::TestEmulation:  Error reading Dictionary Object.\n");
882         fclose (test_file);
883         return false;
884     }
885 
886     fclose (test_file);
887 
888     OptionValueDictionary *data_dictionary = data_dictionary_sp->GetAsDictionary();
889     static ConstString description_key ("assembly_string");
890     static ConstString triple_key ("triple");
891 
892     OptionValueSP value_sp = data_dictionary->GetValueForKey (description_key);
893 
894     if (!value_sp)
895     {
896         out_stream->Printf ("Instruction::TestEmulation:  Test file does not contain description string.\n");
897         return false;
898     }
899 
900     SetDescription (value_sp->GetStringValue());
901 
902     value_sp = data_dictionary->GetValueForKey (triple_key);
903     if (!value_sp)
904     {
905         out_stream->Printf ("Instruction::TestEmulation: Test file does not contain triple.\n");
906         return false;
907     }
908 
909     ArchSpec arch;
910     arch.SetTriple (llvm::Triple (value_sp->GetStringValue()));
911 
912     bool success = false;
913     std::unique_ptr<EmulateInstruction> insn_emulator_ap(EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr));
914     if (insn_emulator_ap)
915         success = insn_emulator_ap->TestEmulation (out_stream, arch, data_dictionary);
916 
917     if (success)
918         out_stream->Printf ("Emulation test succeeded.");
919     else
920         out_stream->Printf ("Emulation test failed.");
921 
922     return success;
923 }
924 
925 bool
926 Instruction::Emulate (const ArchSpec &arch,
927                       uint32_t evaluate_options,
928                       void *baton,
929                       EmulateInstruction::ReadMemoryCallback read_mem_callback,
930                       EmulateInstruction::WriteMemoryCallback write_mem_callback,
931                       EmulateInstruction::ReadRegisterCallback read_reg_callback,
932                       EmulateInstruction::WriteRegisterCallback write_reg_callback)
933 {
934     std::unique_ptr<EmulateInstruction> insn_emulator_ap(EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr));
935     if (insn_emulator_ap)
936     {
937         insn_emulator_ap->SetBaton(baton);
938         insn_emulator_ap->SetCallbacks(read_mem_callback, write_mem_callback, read_reg_callback, write_reg_callback);
939         insn_emulator_ap->SetInstruction(GetOpcode(), GetAddress(), nullptr);
940         return insn_emulator_ap->EvaluateInstruction(evaluate_options);
941     }
942 
943     return false;
944 }
945 
946 uint32_t
947 Instruction::GetData (DataExtractor &data)
948 {
949     return m_opcode.GetData(data);
950 }
951 
952 InstructionList::InstructionList() :
953     m_instructions()
954 {
955 }
956 
957 InstructionList::~InstructionList() = default;
958 
959 size_t
960 InstructionList::GetSize() const
961 {
962     return m_instructions.size();
963 }
964 
965 uint32_t
966 InstructionList::GetMaxOpcocdeByteSize () const
967 {
968     uint32_t max_inst_size = 0;
969     collection::const_iterator pos, end;
970     for (pos = m_instructions.begin(), end = m_instructions.end();
971          pos != end;
972          ++pos)
973     {
974         uint32_t inst_size = (*pos)->GetOpcode().GetByteSize();
975         if (max_inst_size < inst_size)
976             max_inst_size = inst_size;
977     }
978     return max_inst_size;
979 }
980 
981 InstructionSP
982 InstructionList::GetInstructionAtIndex (size_t idx) const
983 {
984     InstructionSP inst_sp;
985     if (idx < m_instructions.size())
986         inst_sp = m_instructions[idx];
987     return inst_sp;
988 }
989 
990 void
991 InstructionList::Dump (Stream *s,
992                        bool show_address,
993                        bool show_bytes,
994                        const ExecutionContext* exe_ctx)
995 {
996     const uint32_t max_opcode_byte_size = GetMaxOpcocdeByteSize();
997     collection::const_iterator pos, begin, end;
998 
999     const FormatEntity::Entry *disassembly_format = nullptr;
1000     FormatEntity::Entry format;
1001     if (exe_ctx && exe_ctx->HasTargetScope())
1002     {
1003         disassembly_format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat ();
1004     }
1005     else
1006     {
1007         FormatEntity::Parse("${addr}: ", format);
1008         disassembly_format = &format;
1009     }
1010 
1011     for (begin = m_instructions.begin(), end = m_instructions.end(), pos = begin;
1012          pos != end;
1013          ++pos)
1014     {
1015         if (pos != begin)
1016             s->EOL();
1017         (*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx, nullptr, nullptr, disassembly_format, 0);
1018     }
1019 }
1020 
1021 void
1022 InstructionList::Clear()
1023 {
1024     m_instructions.clear();
1025 }
1026 
1027 void
1028 InstructionList::Append (lldb::InstructionSP &inst_sp)
1029 {
1030     if (inst_sp)
1031         m_instructions.push_back(inst_sp);
1032 }
1033 
1034 uint32_t
1035 InstructionList::GetIndexOfNextBranchInstruction(uint32_t start, Target &target) const
1036 {
1037     size_t num_instructions = m_instructions.size();
1038 
1039     uint32_t next_branch = std::numeric_limits<uint32_t>::max();
1040     size_t i;
1041     for (i = start; i < num_instructions; i++)
1042     {
1043         if (m_instructions[i]->DoesBranch())
1044         {
1045             next_branch = i;
1046             break;
1047         }
1048     }
1049 
1050     // Hexagon needs the first instruction of the packet with the branch.
1051     // Go backwards until we find an instruction marked end-of-packet, or
1052     // until we hit start.
1053     if (target.GetArchitecture().GetTriple().getArch() == llvm::Triple::hexagon)
1054     {
1055         // If we didn't find a branch, find the last packet start.
1056         if (next_branch == std::numeric_limits<uint32_t>::max())
1057         {
1058             i = num_instructions - 1;
1059         }
1060 
1061         while (i > start)
1062         {
1063             --i;
1064 
1065             Error error;
1066             uint32_t inst_bytes;
1067             bool prefer_file_cache = false; // Read from process if process is running
1068             lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1069             target.ReadMemory(m_instructions[i]->GetAddress(),
1070                               prefer_file_cache,
1071                               &inst_bytes,
1072                               sizeof(inst_bytes),
1073                               error,
1074                               &load_addr);
1075             // If we have an error reading memory, return start
1076             if (!error.Success())
1077                 return start;
1078             // check if this is the last instruction in a packet
1079             // bits 15:14 will be 11b or 00b for a duplex
1080             if (((inst_bytes & 0xC000) == 0xC000) ||
1081                 ((inst_bytes & 0xC000) == 0x0000))
1082             {
1083                 // instruction after this should be the start of next packet
1084                 next_branch = i + 1;
1085                 break;
1086             }
1087         }
1088 
1089         if (next_branch == std::numeric_limits<uint32_t>::max())
1090         {
1091             // We couldn't find the previous packet, so return start
1092             next_branch = start;
1093         }
1094     }
1095     return next_branch;
1096 }
1097 
1098 uint32_t
1099 InstructionList::GetIndexOfInstructionAtAddress (const Address &address)
1100 {
1101     size_t num_instructions = m_instructions.size();
1102     uint32_t index = std::numeric_limits<uint32_t>::max();
1103     for (size_t i = 0; i < num_instructions; i++)
1104     {
1105         if (m_instructions[i]->GetAddress() == address)
1106         {
1107             index = i;
1108             break;
1109         }
1110     }
1111     return index;
1112 }
1113 
1114 uint32_t
1115 InstructionList::GetIndexOfInstructionAtLoadAddress (lldb::addr_t load_addr, Target &target)
1116 {
1117     Address address;
1118     address.SetLoadAddress(load_addr, &target);
1119     return GetIndexOfInstructionAtAddress(address);
1120 }
1121 
1122 size_t
1123 Disassembler::ParseInstructions (const ExecutionContext *exe_ctx,
1124                                  const AddressRange &range,
1125                                  Stream *error_strm_ptr,
1126                                  bool prefer_file_cache)
1127 {
1128     if (exe_ctx)
1129     {
1130         Target *target = exe_ctx->GetTargetPtr();
1131         const addr_t byte_size = range.GetByteSize();
1132         if (target == nullptr || byte_size == 0 || !range.GetBaseAddress().IsValid())
1133             return 0;
1134 
1135         DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
1136         DataBufferSP data_sp(heap_buffer);
1137 
1138         Error error;
1139         lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1140         const size_t bytes_read = target->ReadMemory (range.GetBaseAddress(),
1141                                                       prefer_file_cache,
1142                                                       heap_buffer->GetBytes(),
1143                                                       heap_buffer->GetByteSize(),
1144                                                       error,
1145                                                       &load_addr);
1146 
1147         if (bytes_read > 0)
1148         {
1149             if (bytes_read != heap_buffer->GetByteSize())
1150                 heap_buffer->SetByteSize (bytes_read);
1151             DataExtractor data (data_sp,
1152                                 m_arch.GetByteOrder(),
1153                                 m_arch.GetAddressByteSize());
1154             const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1155             return DecodeInstructions(range.GetBaseAddress(), data, 0, std::numeric_limits<uint32_t>::max(), false,
1156                                       data_from_file);
1157         }
1158         else if (error_strm_ptr)
1159         {
1160             const char *error_cstr = error.AsCString();
1161             if (error_cstr)
1162             {
1163                 error_strm_ptr->Printf("error: %s\n", error_cstr);
1164             }
1165         }
1166     }
1167     else if (error_strm_ptr)
1168     {
1169         error_strm_ptr->PutCString("error: invalid execution context\n");
1170     }
1171     return 0;
1172 }
1173 
1174 size_t
1175 Disassembler::ParseInstructions (const ExecutionContext *exe_ctx,
1176                                  const Address &start,
1177                                  uint32_t num_instructions,
1178                                  bool prefer_file_cache)
1179 {
1180     m_instruction_list.Clear();
1181 
1182     if (exe_ctx == nullptr || num_instructions == 0 || !start.IsValid())
1183         return 0;
1184 
1185     Target *target = exe_ctx->GetTargetPtr();
1186     // Calculate the max buffer size we will need in order to disassemble
1187     const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize();
1188 
1189     if (target == nullptr || byte_size == 0)
1190         return 0;
1191 
1192     DataBufferHeap *heap_buffer = new DataBufferHeap (byte_size, '\0');
1193     DataBufferSP data_sp (heap_buffer);
1194 
1195     Error error;
1196     lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1197     const size_t bytes_read = target->ReadMemory (start,
1198                                                   prefer_file_cache,
1199                                                   heap_buffer->GetBytes(),
1200                                                   byte_size,
1201                                                   error,
1202                                                   &load_addr);
1203 
1204     const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1205 
1206     if (bytes_read == 0)
1207         return 0;
1208     DataExtractor data (data_sp,
1209                         m_arch.GetByteOrder(),
1210                         m_arch.GetAddressByteSize());
1211 
1212     const bool append_instructions = true;
1213     DecodeInstructions (start,
1214                         data,
1215                         0,
1216                         num_instructions,
1217                         append_instructions,
1218                         data_from_file);
1219 
1220     return m_instruction_list.GetSize();
1221 }
1222 
1223 //----------------------------------------------------------------------
1224 // Disassembler copy constructor
1225 //----------------------------------------------------------------------
1226 Disassembler::Disassembler(const ArchSpec& arch, const char *flavor) :
1227     m_arch (arch),
1228     m_instruction_list(),
1229     m_base_addr(LLDB_INVALID_ADDRESS),
1230     m_flavor ()
1231 {
1232     if (flavor == nullptr)
1233         m_flavor.assign("default");
1234     else
1235         m_flavor.assign(flavor);
1236 
1237     // If this is an arm variant that can only include thumb (T16, T32)
1238     // instructions, force the arch triple to be "thumbv.." instead of
1239     // "armv..."
1240     if ((arch.GetTriple().getArch() == llvm::Triple::arm || arch.GetTriple().getArch() == llvm::Triple::thumb)
1241         && (arch.GetCore() == ArchSpec::Core::eCore_arm_armv7m
1242             || arch.GetCore() == ArchSpec::Core::eCore_arm_armv7em
1243             || arch.GetCore() == ArchSpec::Core::eCore_arm_armv6m))
1244     {
1245         std::string thumb_arch_name (arch.GetTriple().getArchName().str());
1246         // Replace "arm" with "thumb" so we get all thumb variants correct
1247         if (thumb_arch_name.size() > 3)
1248         {
1249             thumb_arch_name.erase(0, 3);
1250             thumb_arch_name.insert(0, "thumb");
1251         }
1252         m_arch.SetTriple (thumb_arch_name.c_str());
1253     }
1254 }
1255 
1256 Disassembler::~Disassembler() = default;
1257 
1258 InstructionList &
1259 Disassembler::GetInstructionList ()
1260 {
1261     return m_instruction_list;
1262 }
1263 
1264 const InstructionList &
1265 Disassembler::GetInstructionList () const
1266 {
1267     return m_instruction_list;
1268 }
1269 
1270 //----------------------------------------------------------------------
1271 // Class PseudoInstruction
1272 //----------------------------------------------------------------------
1273 
1274 PseudoInstruction::PseudoInstruction () :
1275     Instruction (Address(), eAddressClassUnknown),
1276     m_description ()
1277 {
1278 }
1279 
1280 PseudoInstruction::~PseudoInstruction() = default;
1281 
1282 bool
1283 PseudoInstruction::DoesBranch ()
1284 {
1285     // This is NOT a valid question for a pseudo instruction.
1286     return false;
1287 }
1288 
1289 bool
1290 PseudoInstruction::HasDelaySlot ()
1291 {
1292     // This is NOT a valid question for a pseudo instruction.
1293     return false;
1294 }
1295 
1296 size_t
1297 PseudoInstruction::Decode (const lldb_private::Disassembler &disassembler,
1298                            const lldb_private::DataExtractor &data,
1299                            lldb::offset_t data_offset)
1300 {
1301     return m_opcode.GetByteSize();
1302 }
1303 
1304 void
1305 PseudoInstruction::SetOpcode (size_t opcode_size, void *opcode_data)
1306 {
1307     if (!opcode_data)
1308         return;
1309 
1310     switch (opcode_size)
1311     {
1312         case 8:
1313         {
1314             uint8_t value8 = *((uint8_t *) opcode_data);
1315             m_opcode.SetOpcode8 (value8, eByteOrderInvalid);
1316             break;
1317          }
1318         case 16:
1319         {
1320             uint16_t value16 = *((uint16_t *) opcode_data);
1321             m_opcode.SetOpcode16 (value16, eByteOrderInvalid);
1322             break;
1323          }
1324         case 32:
1325         {
1326             uint32_t value32 = *((uint32_t *) opcode_data);
1327             m_opcode.SetOpcode32 (value32, eByteOrderInvalid);
1328             break;
1329          }
1330         case 64:
1331         {
1332             uint64_t value64 = *((uint64_t *) opcode_data);
1333             m_opcode.SetOpcode64 (value64, eByteOrderInvalid);
1334             break;
1335          }
1336         default:
1337             break;
1338     }
1339 }
1340 
1341 void
1342 PseudoInstruction::SetDescription (const char *description)
1343 {
1344     if (description && strlen (description) > 0)
1345         m_description = description;
1346 }
1347