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