1ac7ddfbfSEd Maste //===-- Disassembler.cpp ----------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste // The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste
10ac7ddfbfSEd Maste #include "lldb/Core/Disassembler.h"
11ac7ddfbfSEd Maste
12*b5893f02SDimitry Andric #include "lldb/Core/AddressRange.h"
13ac7ddfbfSEd Maste #include "lldb/Core/Debugger.h"
14ac7ddfbfSEd Maste #include "lldb/Core/EmulateInstruction.h"
15*b5893f02SDimitry Andric #include "lldb/Core/Mangled.h"
16ac7ddfbfSEd Maste #include "lldb/Core/Module.h"
17*b5893f02SDimitry Andric #include "lldb/Core/ModuleList.h"
18ac7ddfbfSEd Maste #include "lldb/Core/PluginManager.h"
19*b5893f02SDimitry Andric #include "lldb/Core/SourceManager.h"
204bb0738eSEd Maste #include "lldb/Host/FileSystem.h"
21ac7ddfbfSEd Maste #include "lldb/Interpreter/OptionValue.h"
22ac7ddfbfSEd Maste #include "lldb/Interpreter/OptionValueArray.h"
23ac7ddfbfSEd Maste #include "lldb/Interpreter/OptionValueDictionary.h"
24435933ddSDimitry Andric #include "lldb/Interpreter/OptionValueRegex.h"
25ac7ddfbfSEd Maste #include "lldb/Interpreter/OptionValueString.h"
26ac7ddfbfSEd Maste #include "lldb/Interpreter/OptionValueUInt64.h"
27ac7ddfbfSEd Maste #include "lldb/Symbol/Function.h"
28*b5893f02SDimitry Andric #include "lldb/Symbol/Symbol.h"
29*b5893f02SDimitry Andric #include "lldb/Symbol/SymbolContext.h"
30ac7ddfbfSEd Maste #include "lldb/Target/ExecutionContext.h"
3112b93ac6SEd Maste #include "lldb/Target/SectionLoadList.h"
32ac7ddfbfSEd Maste #include "lldb/Target/StackFrame.h"
33ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
34*b5893f02SDimitry Andric #include "lldb/Target/Thread.h"
35f678e45dSDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
36f678e45dSDimitry Andric #include "lldb/Utility/DataExtractor.h"
37f678e45dSDimitry Andric #include "lldb/Utility/RegularExpression.h"
385517e702SDimitry Andric #include "lldb/Utility/Status.h"
39*b5893f02SDimitry Andric #include "lldb/Utility/Stream.h"
40*b5893f02SDimitry Andric #include "lldb/Utility/StreamString.h"
41a580b014SDimitry Andric #include "lldb/Utility/Timer.h"
42*b5893f02SDimitry Andric #include "lldb/lldb-private-enumerations.h"
43*b5893f02SDimitry Andric #include "lldb/lldb-private-interfaces.h"
44*b5893f02SDimitry Andric #include "lldb/lldb-private-types.h"
45*b5893f02SDimitry Andric #include "llvm/ADT/Triple.h"
46*b5893f02SDimitry Andric #include "llvm/Support/Compiler.h"
47f678e45dSDimitry Andric
48*b5893f02SDimitry Andric #include <cstdint>
49f678e45dSDimitry Andric #include <cstring>
50*b5893f02SDimitry Andric #include <utility>
51f678e45dSDimitry Andric
52*b5893f02SDimitry Andric #include <assert.h>
53ac7ddfbfSEd Maste
54ac7ddfbfSEd Maste #define DEFAULT_DISASM_BYTE_SIZE 32
55ac7ddfbfSEd Maste
56ac7ddfbfSEd Maste using namespace lldb;
57ac7ddfbfSEd Maste using namespace lldb_private;
58ac7ddfbfSEd Maste
FindPlugin(const ArchSpec & arch,const char * flavor,const char * plugin_name)59435933ddSDimitry Andric DisassemblerSP Disassembler::FindPlugin(const ArchSpec &arch,
60435933ddSDimitry Andric const char *flavor,
61435933ddSDimitry Andric const char *plugin_name) {
625517e702SDimitry Andric static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
635517e702SDimitry Andric Timer scoped_timer(func_cat,
64ac7ddfbfSEd Maste "Disassembler::FindPlugin (arch = %s, plugin_name = %s)",
65435933ddSDimitry Andric arch.GetArchitectureName(), plugin_name);
66ac7ddfbfSEd Maste
674bb0738eSEd Maste DisassemblerCreateInstance create_callback = nullptr;
68ac7ddfbfSEd Maste
69435933ddSDimitry Andric if (plugin_name) {
70ac7ddfbfSEd Maste ConstString const_plugin_name(plugin_name);
71435933ddSDimitry Andric create_callback = PluginManager::GetDisassemblerCreateCallbackForPluginName(
72435933ddSDimitry Andric const_plugin_name);
73435933ddSDimitry Andric if (create_callback) {
74ac7ddfbfSEd Maste DisassemblerSP disassembler_sp(create_callback(arch, flavor));
75ac7ddfbfSEd Maste
764bb0738eSEd Maste if (disassembler_sp)
77ac7ddfbfSEd Maste return disassembler_sp;
78ac7ddfbfSEd Maste }
79435933ddSDimitry Andric } else {
80435933ddSDimitry Andric for (uint32_t idx = 0;
81435933ddSDimitry Andric (create_callback = PluginManager::GetDisassemblerCreateCallbackAtIndex(
82435933ddSDimitry Andric idx)) != nullptr;
83435933ddSDimitry Andric ++idx) {
84ac7ddfbfSEd Maste DisassemblerSP disassembler_sp(create_callback(arch, flavor));
85ac7ddfbfSEd Maste
864bb0738eSEd Maste if (disassembler_sp)
87ac7ddfbfSEd Maste return disassembler_sp;
88ac7ddfbfSEd Maste }
89ac7ddfbfSEd Maste }
90ac7ddfbfSEd Maste return DisassemblerSP();
91ac7ddfbfSEd Maste }
92ac7ddfbfSEd Maste
FindPluginForTarget(const TargetSP target_sp,const ArchSpec & arch,const char * flavor,const char * plugin_name)93435933ddSDimitry Andric DisassemblerSP Disassembler::FindPluginForTarget(const TargetSP target_sp,
94435933ddSDimitry Andric const ArchSpec &arch,
95435933ddSDimitry Andric const char *flavor,
96435933ddSDimitry Andric const char *plugin_name) {
97435933ddSDimitry Andric if (target_sp && flavor == nullptr) {
98435933ddSDimitry Andric // FIXME - we don't have the mechanism in place to do per-architecture
994ba319b5SDimitry Andric // settings. But since we know that for now we only support flavors on x86
1004ba319b5SDimitry Andric // & x86_64,
101435933ddSDimitry Andric if (arch.GetTriple().getArch() == llvm::Triple::x86 ||
102435933ddSDimitry Andric arch.GetTriple().getArch() == llvm::Triple::x86_64)
103ac7ddfbfSEd Maste flavor = target_sp->GetDisassemblyFlavor();
104ac7ddfbfSEd Maste }
105ac7ddfbfSEd Maste return FindPlugin(arch, flavor, plugin_name);
106ac7ddfbfSEd Maste }
107ac7ddfbfSEd Maste
ResolveAddress(const ExecutionContext & exe_ctx,const Address & addr,Address & resolved_addr)108435933ddSDimitry Andric static void ResolveAddress(const ExecutionContext &exe_ctx, const Address &addr,
109435933ddSDimitry Andric Address &resolved_addr) {
110435933ddSDimitry Andric if (!addr.IsSectionOffset()) {
1114ba319b5SDimitry Andric // If we weren't passed in a section offset address range, try and resolve
1124ba319b5SDimitry Andric // it to something
113ac7ddfbfSEd Maste Target *target = exe_ctx.GetTargetPtr();
114435933ddSDimitry Andric if (target) {
1154ba319b5SDimitry Andric bool is_resolved =
1164ba319b5SDimitry Andric target->GetSectionLoadList().IsEmpty() ?
1174ba319b5SDimitry Andric target->GetImages().ResolveFileAddress(addr.GetOffset(),
1184ba319b5SDimitry Andric resolved_addr) :
119435933ddSDimitry Andric target->GetSectionLoadList().ResolveLoadAddress(addr.GetOffset(),
120435933ddSDimitry Andric resolved_addr);
1214ba319b5SDimitry Andric
1224ba319b5SDimitry Andric // We weren't able to resolve the address, just treat it as a raw address
1234ba319b5SDimitry Andric if (is_resolved && resolved_addr.IsValid())
124ac7ddfbfSEd Maste return;
125ac7ddfbfSEd Maste }
126ac7ddfbfSEd Maste }
127ac7ddfbfSEd Maste resolved_addr = addr;
128ac7ddfbfSEd Maste }
129ac7ddfbfSEd Maste
Disassemble(Debugger & debugger,const ArchSpec & arch,const char * plugin_name,const char * flavor,const ExecutionContext & exe_ctx,SymbolContextList & sc_list,uint32_t num_instructions,bool mixed_source_and_assembly,uint32_t num_mixed_context_lines,uint32_t options,Stream & strm)130435933ddSDimitry Andric size_t Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
131435933ddSDimitry Andric const char *plugin_name, const char *flavor,
132ac7ddfbfSEd Maste const ExecutionContext &exe_ctx,
133ac7ddfbfSEd Maste SymbolContextList &sc_list,
134ac7ddfbfSEd Maste uint32_t num_instructions,
135435933ddSDimitry Andric bool mixed_source_and_assembly,
136ac7ddfbfSEd Maste uint32_t num_mixed_context_lines,
137435933ddSDimitry Andric uint32_t options, Stream &strm) {
138ac7ddfbfSEd Maste size_t success_count = 0;
139ac7ddfbfSEd Maste const size_t count = sc_list.GetSize();
140ac7ddfbfSEd Maste SymbolContext sc;
141ac7ddfbfSEd Maste AddressRange range;
142435933ddSDimitry Andric const uint32_t scope =
143435933ddSDimitry Andric eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
144ac7ddfbfSEd Maste const bool use_inline_block_range = true;
145435933ddSDimitry Andric for (size_t i = 0; i < count; ++i) {
1464bb0738eSEd Maste if (!sc_list.GetContextAtIndex(i, sc))
147ac7ddfbfSEd Maste break;
148435933ddSDimitry Andric for (uint32_t range_idx = 0;
149435933ddSDimitry Andric sc.GetAddressRange(scope, range_idx, use_inline_block_range, range);
150435933ddSDimitry Andric ++range_idx) {
151435933ddSDimitry Andric if (Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, range,
152435933ddSDimitry Andric num_instructions, mixed_source_and_assembly,
153435933ddSDimitry Andric num_mixed_context_lines, options, strm)) {
154ac7ddfbfSEd Maste ++success_count;
155ac7ddfbfSEd Maste strm.EOL();
156ac7ddfbfSEd Maste }
157ac7ddfbfSEd Maste }
158ac7ddfbfSEd Maste }
159ac7ddfbfSEd Maste return success_count;
160ac7ddfbfSEd Maste }
161ac7ddfbfSEd Maste
Disassemble(Debugger & debugger,const ArchSpec & arch,const char * plugin_name,const char * flavor,const ExecutionContext & exe_ctx,const ConstString & name,Module * module,uint32_t num_instructions,bool mixed_source_and_assembly,uint32_t num_mixed_context_lines,uint32_t options,Stream & strm)162435933ddSDimitry Andric bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
163435933ddSDimitry Andric const char *plugin_name, const char *flavor,
164ac7ddfbfSEd Maste const ExecutionContext &exe_ctx,
165435933ddSDimitry Andric const ConstString &name, Module *module,
166ac7ddfbfSEd Maste uint32_t num_instructions,
167435933ddSDimitry Andric bool mixed_source_and_assembly,
168ac7ddfbfSEd Maste uint32_t num_mixed_context_lines,
169435933ddSDimitry Andric uint32_t options, Stream &strm) {
170ac7ddfbfSEd Maste SymbolContextList sc_list;
171435933ddSDimitry Andric if (name) {
172ac7ddfbfSEd Maste const bool include_symbols = true;
173ac7ddfbfSEd Maste const bool include_inlines = true;
174435933ddSDimitry Andric if (module) {
175435933ddSDimitry Andric module->FindFunctions(name, nullptr, eFunctionNameTypeAuto,
176435933ddSDimitry Andric include_symbols, include_inlines, true, sc_list);
177435933ddSDimitry Andric } else if (exe_ctx.GetTargetPtr()) {
178435933ddSDimitry Andric exe_ctx.GetTargetPtr()->GetImages().FindFunctions(
179435933ddSDimitry Andric name, eFunctionNameTypeAuto, include_symbols, include_inlines, false,
180ac7ddfbfSEd Maste sc_list);
181ac7ddfbfSEd Maste }
182ac7ddfbfSEd Maste }
183ac7ddfbfSEd Maste
184435933ddSDimitry Andric if (sc_list.GetSize()) {
185435933ddSDimitry Andric return Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, sc_list,
186435933ddSDimitry Andric num_instructions, mixed_source_and_assembly,
187435933ddSDimitry Andric num_mixed_context_lines, options, strm);
188ac7ddfbfSEd Maste }
189ac7ddfbfSEd Maste return false;
190ac7ddfbfSEd Maste }
191ac7ddfbfSEd Maste
DisassembleRange(const ArchSpec & arch,const char * plugin_name,const char * flavor,const ExecutionContext & exe_ctx,const AddressRange & range,bool prefer_file_cache)192435933ddSDimitry Andric lldb::DisassemblerSP Disassembler::DisassembleRange(
193435933ddSDimitry Andric const ArchSpec &arch, const char *plugin_name, const char *flavor,
194435933ddSDimitry Andric const ExecutionContext &exe_ctx, const AddressRange &range,
195435933ddSDimitry Andric bool prefer_file_cache) {
196ac7ddfbfSEd Maste lldb::DisassemblerSP disasm_sp;
197435933ddSDimitry Andric if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid()) {
198435933ddSDimitry Andric disasm_sp = Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch,
199435933ddSDimitry Andric flavor, plugin_name);
200ac7ddfbfSEd Maste
201435933ddSDimitry Andric if (disasm_sp) {
202435933ddSDimitry Andric size_t bytes_disassembled = disasm_sp->ParseInstructions(
203435933ddSDimitry Andric &exe_ctx, range, nullptr, prefer_file_cache);
204ac7ddfbfSEd Maste if (bytes_disassembled == 0)
205ac7ddfbfSEd Maste disasm_sp.reset();
206ac7ddfbfSEd Maste }
207ac7ddfbfSEd Maste }
208ac7ddfbfSEd Maste return disasm_sp;
209ac7ddfbfSEd Maste }
210ac7ddfbfSEd Maste
211ac7ddfbfSEd Maste lldb::DisassemblerSP
DisassembleBytes(const ArchSpec & arch,const char * plugin_name,const char * flavor,const Address & start,const void * src,size_t src_len,uint32_t num_instructions,bool data_from_file)212435933ddSDimitry Andric Disassembler::DisassembleBytes(const ArchSpec &arch, const char *plugin_name,
213435933ddSDimitry Andric const char *flavor, const Address &start,
214435933ddSDimitry Andric const void *src, size_t src_len,
215435933ddSDimitry Andric uint32_t num_instructions, bool data_from_file) {
216ac7ddfbfSEd Maste lldb::DisassemblerSP disasm_sp;
217ac7ddfbfSEd Maste
218435933ddSDimitry Andric if (src) {
219ac7ddfbfSEd Maste disasm_sp = Disassembler::FindPlugin(arch, flavor, plugin_name);
220ac7ddfbfSEd Maste
221435933ddSDimitry Andric if (disasm_sp) {
222435933ddSDimitry Andric DataExtractor data(src, src_len, arch.GetByteOrder(),
223435933ddSDimitry Andric arch.GetAddressByteSize());
224ac7ddfbfSEd Maste
225435933ddSDimitry Andric (void)disasm_sp->DecodeInstructions(start, data, 0, num_instructions,
226435933ddSDimitry Andric false, data_from_file);
227ac7ddfbfSEd Maste }
228ac7ddfbfSEd Maste }
229ac7ddfbfSEd Maste
230ac7ddfbfSEd Maste return disasm_sp;
231ac7ddfbfSEd Maste }
232ac7ddfbfSEd Maste
Disassemble(Debugger & debugger,const ArchSpec & arch,const char * plugin_name,const char * flavor,const ExecutionContext & exe_ctx,const AddressRange & disasm_range,uint32_t num_instructions,bool mixed_source_and_assembly,uint32_t num_mixed_context_lines,uint32_t options,Stream & strm)233435933ddSDimitry Andric bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
234435933ddSDimitry Andric const char *plugin_name, const char *flavor,
235ac7ddfbfSEd Maste const ExecutionContext &exe_ctx,
236ac7ddfbfSEd Maste const AddressRange &disasm_range,
237ac7ddfbfSEd Maste uint32_t num_instructions,
238435933ddSDimitry Andric bool mixed_source_and_assembly,
239ac7ddfbfSEd Maste uint32_t num_mixed_context_lines,
240435933ddSDimitry Andric uint32_t options, Stream &strm) {
241435933ddSDimitry Andric if (disasm_range.GetByteSize()) {
242435933ddSDimitry Andric lldb::DisassemblerSP disasm_sp(Disassembler::FindPluginForTarget(
243435933ddSDimitry Andric exe_ctx.GetTargetSP(), arch, flavor, plugin_name));
244ac7ddfbfSEd Maste
245435933ddSDimitry Andric if (disasm_sp) {
246ac7ddfbfSEd Maste AddressRange range;
247435933ddSDimitry Andric ResolveAddress(exe_ctx, disasm_range.GetBaseAddress(),
248435933ddSDimitry Andric range.GetBaseAddress());
249ac7ddfbfSEd Maste range.SetByteSize(disasm_range.GetByteSize());
250ac7ddfbfSEd Maste const bool prefer_file_cache = false;
251435933ddSDimitry Andric size_t bytes_disassembled = disasm_sp->ParseInstructions(
252435933ddSDimitry Andric &exe_ctx, range, &strm, prefer_file_cache);
253ac7ddfbfSEd Maste if (bytes_disassembled == 0)
254ac7ddfbfSEd Maste return false;
255ac7ddfbfSEd Maste
256435933ddSDimitry Andric return PrintInstructions(disasm_sp.get(), debugger, arch, exe_ctx,
257435933ddSDimitry Andric num_instructions, mixed_source_and_assembly,
2584bb0738eSEd Maste num_mixed_context_lines, options, strm);
259ac7ddfbfSEd Maste }
260ac7ddfbfSEd Maste }
261ac7ddfbfSEd Maste return false;
262ac7ddfbfSEd Maste }
263ac7ddfbfSEd Maste
Disassemble(Debugger & debugger,const ArchSpec & arch,const char * plugin_name,const char * flavor,const ExecutionContext & exe_ctx,const Address & start_address,uint32_t num_instructions,bool mixed_source_and_assembly,uint32_t num_mixed_context_lines,uint32_t options,Stream & strm)264435933ddSDimitry Andric bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
265435933ddSDimitry Andric const char *plugin_name, const char *flavor,
266ac7ddfbfSEd Maste const ExecutionContext &exe_ctx,
267ac7ddfbfSEd Maste const Address &start_address,
268ac7ddfbfSEd Maste uint32_t num_instructions,
269435933ddSDimitry Andric bool mixed_source_and_assembly,
270ac7ddfbfSEd Maste uint32_t num_mixed_context_lines,
271435933ddSDimitry Andric uint32_t options, Stream &strm) {
272435933ddSDimitry Andric if (num_instructions > 0) {
273435933ddSDimitry Andric lldb::DisassemblerSP disasm_sp(Disassembler::FindPluginForTarget(
274435933ddSDimitry Andric exe_ctx.GetTargetSP(), arch, flavor, plugin_name));
275435933ddSDimitry Andric if (disasm_sp) {
276ac7ddfbfSEd Maste Address addr;
277ac7ddfbfSEd Maste ResolveAddress(exe_ctx, start_address, addr);
278ac7ddfbfSEd Maste const bool prefer_file_cache = false;
279435933ddSDimitry Andric size_t bytes_disassembled = disasm_sp->ParseInstructions(
280435933ddSDimitry Andric &exe_ctx, addr, num_instructions, prefer_file_cache);
281ac7ddfbfSEd Maste if (bytes_disassembled == 0)
282ac7ddfbfSEd Maste return false;
283435933ddSDimitry Andric return PrintInstructions(disasm_sp.get(), debugger, arch, exe_ctx,
284435933ddSDimitry Andric num_instructions, mixed_source_and_assembly,
2854bb0738eSEd Maste num_mixed_context_lines, options, strm);
286ac7ddfbfSEd Maste }
287ac7ddfbfSEd Maste }
288ac7ddfbfSEd Maste return false;
289ac7ddfbfSEd Maste }
290ac7ddfbfSEd Maste
291435933ddSDimitry Andric Disassembler::SourceLine
GetFunctionDeclLineEntry(const SymbolContext & sc)292435933ddSDimitry Andric Disassembler::GetFunctionDeclLineEntry(const SymbolContext &sc) {
293435933ddSDimitry Andric SourceLine decl_line;
294435933ddSDimitry Andric if (sc.function && sc.line_entry.IsValid()) {
295435933ddSDimitry Andric LineEntry prologue_end_line = sc.line_entry;
296435933ddSDimitry Andric FileSpec func_decl_file;
297435933ddSDimitry Andric uint32_t func_decl_line;
298435933ddSDimitry Andric sc.function->GetStartLineSourceInfo(func_decl_file, func_decl_line);
299435933ddSDimitry Andric if (func_decl_file == prologue_end_line.file ||
300435933ddSDimitry Andric func_decl_file == prologue_end_line.original_file) {
301435933ddSDimitry Andric decl_line.file = func_decl_file;
302435933ddSDimitry Andric decl_line.line = func_decl_line;
303435933ddSDimitry Andric // TODO do we care about column on these entries? If so, we need to
304435933ddSDimitry Andric // plumb that through GetStartLineSourceInfo.
305435933ddSDimitry Andric decl_line.column = 0;
306435933ddSDimitry Andric }
307435933ddSDimitry Andric }
308435933ddSDimitry Andric return decl_line;
309435933ddSDimitry Andric }
310435933ddSDimitry Andric
AddLineToSourceLineTables(SourceLine & line,std::map<FileSpec,std::set<uint32_t>> & source_lines_seen)311435933ddSDimitry Andric void Disassembler::AddLineToSourceLineTables(
312435933ddSDimitry Andric SourceLine &line,
313435933ddSDimitry Andric std::map<FileSpec, std::set<uint32_t>> &source_lines_seen) {
314435933ddSDimitry Andric if (line.IsValid()) {
315435933ddSDimitry Andric auto source_lines_seen_pos = source_lines_seen.find(line.file);
316435933ddSDimitry Andric if (source_lines_seen_pos == source_lines_seen.end()) {
317435933ddSDimitry Andric std::set<uint32_t> lines;
318435933ddSDimitry Andric lines.insert(line.line);
319435933ddSDimitry Andric source_lines_seen.emplace(line.file, lines);
320435933ddSDimitry Andric } else {
321435933ddSDimitry Andric source_lines_seen_pos->second.insert(line.line);
322435933ddSDimitry Andric }
323435933ddSDimitry Andric }
324435933ddSDimitry Andric }
325435933ddSDimitry Andric
ElideMixedSourceAndDisassemblyLine(const ExecutionContext & exe_ctx,const SymbolContext & sc,SourceLine & line)326435933ddSDimitry Andric bool Disassembler::ElideMixedSourceAndDisassemblyLine(
327435933ddSDimitry Andric const ExecutionContext &exe_ctx, const SymbolContext &sc,
328435933ddSDimitry Andric SourceLine &line) {
329435933ddSDimitry Andric
330435933ddSDimitry Andric // TODO: should we also check target.process.thread.step-avoid-libraries ?
331435933ddSDimitry Andric
332435933ddSDimitry Andric const RegularExpression *avoid_regex = nullptr;
333435933ddSDimitry Andric
334435933ddSDimitry Andric // Skip any line #0 entries - they are implementation details
335435933ddSDimitry Andric if (line.line == 0)
336435933ddSDimitry Andric return false;
337435933ddSDimitry Andric
338435933ddSDimitry Andric ThreadSP thread_sp = exe_ctx.GetThreadSP();
339435933ddSDimitry Andric if (thread_sp) {
340435933ddSDimitry Andric avoid_regex = thread_sp->GetSymbolsToAvoidRegexp();
341435933ddSDimitry Andric } else {
342435933ddSDimitry Andric TargetSP target_sp = exe_ctx.GetTargetSP();
343435933ddSDimitry Andric if (target_sp) {
3445517e702SDimitry Andric Status error;
345435933ddSDimitry Andric OptionValueSP value_sp = target_sp->GetDebugger().GetPropertyValue(
346435933ddSDimitry Andric &exe_ctx, "target.process.thread.step-avoid-regexp", false, error);
347435933ddSDimitry Andric if (value_sp && value_sp->GetType() == OptionValue::eTypeRegex) {
348435933ddSDimitry Andric OptionValueRegex *re = value_sp->GetAsRegex();
349435933ddSDimitry Andric if (re) {
350435933ddSDimitry Andric avoid_regex = re->GetCurrentValue();
351435933ddSDimitry Andric }
352435933ddSDimitry Andric }
353435933ddSDimitry Andric }
354435933ddSDimitry Andric }
355435933ddSDimitry Andric if (avoid_regex && sc.symbol != nullptr) {
356435933ddSDimitry Andric const char *function_name =
357435933ddSDimitry Andric sc.GetFunctionName(Mangled::ePreferDemangledWithoutArguments)
358435933ddSDimitry Andric .GetCString();
359435933ddSDimitry Andric if (function_name) {
360435933ddSDimitry Andric RegularExpression::Match regex_match(1);
361435933ddSDimitry Andric if (avoid_regex->Execute(function_name, ®ex_match)) {
362435933ddSDimitry Andric // skip this source line
363435933ddSDimitry Andric return true;
364435933ddSDimitry Andric }
365435933ddSDimitry Andric }
366435933ddSDimitry Andric }
367435933ddSDimitry Andric // don't skip this source line
368435933ddSDimitry Andric return false;
369435933ddSDimitry Andric }
370435933ddSDimitry Andric
PrintInstructions(Disassembler * disasm_ptr,Debugger & debugger,const ArchSpec & arch,const ExecutionContext & exe_ctx,uint32_t num_instructions,bool mixed_source_and_assembly,uint32_t num_mixed_context_lines,uint32_t options,Stream & strm)371435933ddSDimitry Andric bool Disassembler::PrintInstructions(Disassembler *disasm_ptr,
372435933ddSDimitry Andric Debugger &debugger, const ArchSpec &arch,
373435933ddSDimitry Andric const ExecutionContext &exe_ctx,
374435933ddSDimitry Andric uint32_t num_instructions,
375435933ddSDimitry Andric bool mixed_source_and_assembly,
376435933ddSDimitry Andric uint32_t num_mixed_context_lines,
377435933ddSDimitry Andric uint32_t options, Stream &strm) {
378ac7ddfbfSEd Maste // We got some things disassembled...
379ac7ddfbfSEd Maste size_t num_instructions_found = disasm_ptr->GetInstructionList().GetSize();
380ac7ddfbfSEd Maste
381ac7ddfbfSEd Maste if (num_instructions > 0 && num_instructions < num_instructions_found)
382ac7ddfbfSEd Maste num_instructions_found = num_instructions;
383ac7ddfbfSEd Maste
384435933ddSDimitry Andric const uint32_t max_opcode_byte_size =
385435933ddSDimitry Andric disasm_ptr->GetInstructionList().GetMaxOpcocdeByteSize();
386ac7ddfbfSEd Maste SymbolContext sc;
387ac7ddfbfSEd Maste SymbolContext prev_sc;
388435933ddSDimitry Andric AddressRange current_source_line_range;
3894bb0738eSEd Maste const Address *pc_addr_ptr = nullptr;
390ac7ddfbfSEd Maste StackFrame *frame = exe_ctx.GetFramePtr();
391ac7ddfbfSEd Maste
392ac7ddfbfSEd Maste TargetSP target_sp(exe_ctx.GetTargetSP());
393435933ddSDimitry Andric SourceManager &source_manager =
394435933ddSDimitry Andric target_sp ? target_sp->GetSourceManager() : debugger.GetSourceManager();
395ac7ddfbfSEd Maste
396435933ddSDimitry Andric if (frame) {
397ac7ddfbfSEd Maste pc_addr_ptr = &frame->GetFrameCodeAddress();
3987aa51b79SEd Maste }
399435933ddSDimitry Andric const uint32_t scope =
400435933ddSDimitry Andric eSymbolContextLineEntry | eSymbolContextFunction | eSymbolContextSymbol;
401ac7ddfbfSEd Maste const bool use_inline_block_range = false;
4021c3bbb01SEd Maste
4034bb0738eSEd Maste const FormatEntity::Entry *disassembly_format = nullptr;
4041c3bbb01SEd Maste FormatEntity::Entry format;
405435933ddSDimitry Andric if (exe_ctx.HasTargetScope()) {
406435933ddSDimitry Andric disassembly_format =
407435933ddSDimitry Andric exe_ctx.GetTargetRef().GetDebugger().GetDisassemblyFormat();
408435933ddSDimitry Andric } else {
4091c3bbb01SEd Maste FormatEntity::Parse("${addr}: ", format);
4101c3bbb01SEd Maste disassembly_format = &format;
4111c3bbb01SEd Maste }
4121c3bbb01SEd Maste
4134ba319b5SDimitry Andric // First pass: step through the list of instructions, find how long the
4144ba319b5SDimitry Andric // initial addresses strings are, insert padding in the second pass so the
4154ba319b5SDimitry Andric // opcodes all line up nicely.
416435933ddSDimitry Andric
417435933ddSDimitry Andric // Also build up the source line mapping if this is mixed source & assembly
4184ba319b5SDimitry Andric // mode. Calculate the source line for each assembly instruction (eliding
4194ba319b5SDimitry Andric // inlined functions which the user wants to skip).
420435933ddSDimitry Andric
421435933ddSDimitry Andric std::map<FileSpec, std::set<uint32_t>> source_lines_seen;
422435933ddSDimitry Andric Symbol *previous_symbol = nullptr;
423435933ddSDimitry Andric
4241c3bbb01SEd Maste size_t address_text_size = 0;
425435933ddSDimitry Andric for (size_t i = 0; i < num_instructions_found; ++i) {
426435933ddSDimitry Andric Instruction *inst =
427435933ddSDimitry Andric disasm_ptr->GetInstructionList().GetInstructionAtIndex(i).get();
428435933ddSDimitry Andric if (inst) {
4291c3bbb01SEd Maste const Address &addr = inst->GetAddress();
4301c3bbb01SEd Maste ModuleSP module_sp(addr.GetModule());
431435933ddSDimitry Andric if (module_sp) {
432*b5893f02SDimitry Andric const SymbolContextItem resolve_mask = eSymbolContextFunction |
433435933ddSDimitry Andric eSymbolContextSymbol |
434435933ddSDimitry Andric eSymbolContextLineEntry;
435435933ddSDimitry Andric uint32_t resolved_mask =
436435933ddSDimitry Andric module_sp->ResolveSymbolContextForAddress(addr, resolve_mask, sc);
437435933ddSDimitry Andric if (resolved_mask) {
4381c3bbb01SEd Maste StreamString strmstr;
439435933ddSDimitry Andric Debugger::FormatDisassemblerAddress(disassembly_format, &sc, nullptr,
440435933ddSDimitry Andric &exe_ctx, &addr, strmstr);
4411c3bbb01SEd Maste size_t cur_line = strmstr.GetSizeOfLastLine();
4421c3bbb01SEd Maste if (cur_line > address_text_size)
4431c3bbb01SEd Maste address_text_size = cur_line;
444435933ddSDimitry Andric
445435933ddSDimitry Andric // Add entries to our "source_lines_seen" map+set which list which
446435933ddSDimitry Andric // sources lines occur in this disassembly session. We will print
447435933ddSDimitry Andric // lines of context around a source line, but we don't want to print
448435933ddSDimitry Andric // a source line that has a line table entry of its own - we'll leave
449435933ddSDimitry Andric // that source line to be printed when it actually occurs in the
450435933ddSDimitry Andric // disassembly.
451435933ddSDimitry Andric
452435933ddSDimitry Andric if (mixed_source_and_assembly && sc.line_entry.IsValid()) {
453435933ddSDimitry Andric if (sc.symbol != previous_symbol) {
454435933ddSDimitry Andric SourceLine decl_line = GetFunctionDeclLineEntry(sc);
455*b5893f02SDimitry Andric if (!ElideMixedSourceAndDisassemblyLine(exe_ctx, sc, decl_line))
456435933ddSDimitry Andric AddLineToSourceLineTables(decl_line, source_lines_seen);
457435933ddSDimitry Andric }
458435933ddSDimitry Andric if (sc.line_entry.IsValid()) {
459435933ddSDimitry Andric SourceLine this_line;
460435933ddSDimitry Andric this_line.file = sc.line_entry.file;
461435933ddSDimitry Andric this_line.line = sc.line_entry.line;
462435933ddSDimitry Andric this_line.column = sc.line_entry.column;
463*b5893f02SDimitry Andric if (!ElideMixedSourceAndDisassemblyLine(exe_ctx, sc, this_line))
464435933ddSDimitry Andric AddLineToSourceLineTables(this_line, source_lines_seen);
465435933ddSDimitry Andric }
466435933ddSDimitry Andric }
4671c3bbb01SEd Maste }
4681c3bbb01SEd Maste sc.Clear(false);
4691c3bbb01SEd Maste }
4701c3bbb01SEd Maste }
4711c3bbb01SEd Maste }
4721c3bbb01SEd Maste
473435933ddSDimitry Andric previous_symbol = nullptr;
474435933ddSDimitry Andric SourceLine previous_line;
475435933ddSDimitry Andric for (size_t i = 0; i < num_instructions_found; ++i) {
476435933ddSDimitry Andric Instruction *inst =
477435933ddSDimitry Andric disasm_ptr->GetInstructionList().GetInstructionAtIndex(i).get();
478435933ddSDimitry Andric
479435933ddSDimitry Andric if (inst) {
480ac7ddfbfSEd Maste const Address &addr = inst->GetAddress();
481ac7ddfbfSEd Maste const bool inst_is_at_pc = pc_addr_ptr && addr == *pc_addr_ptr;
482435933ddSDimitry Andric SourceLinesToDisplay source_lines_to_display;
483ac7ddfbfSEd Maste
484ac7ddfbfSEd Maste prev_sc = sc;
485ac7ddfbfSEd Maste
486ac7ddfbfSEd Maste ModuleSP module_sp(addr.GetModule());
487435933ddSDimitry Andric if (module_sp) {
488435933ddSDimitry Andric uint32_t resolved_mask = module_sp->ResolveSymbolContextForAddress(
489435933ddSDimitry Andric addr, eSymbolContextEverything, sc);
490435933ddSDimitry Andric if (resolved_mask) {
491435933ddSDimitry Andric if (mixed_source_and_assembly) {
492ac7ddfbfSEd Maste
493435933ddSDimitry Andric // If we've started a new function (non-inlined), print all of the
4944ba319b5SDimitry Andric // source lines from the function declaration until the first line
4954ba319b5SDimitry Andric // table entry - typically the opening curly brace of the function.
496435933ddSDimitry Andric if (previous_symbol != sc.symbol) {
4974ba319b5SDimitry Andric // The default disassembly format puts an extra blank line
4984ba319b5SDimitry Andric // between functions - so when we're displaying the source
4994ba319b5SDimitry Andric // context for a function, we don't want to add a blank line
5004ba319b5SDimitry Andric // after the source context or we'll end up with two of them.
501435933ddSDimitry Andric if (previous_symbol != nullptr)
502435933ddSDimitry Andric source_lines_to_display.print_source_context_end_eol = false;
503ac7ddfbfSEd Maste
504435933ddSDimitry Andric previous_symbol = sc.symbol;
505435933ddSDimitry Andric if (sc.function && sc.line_entry.IsValid()) {
506435933ddSDimitry Andric LineEntry prologue_end_line = sc.line_entry;
507*b5893f02SDimitry Andric if (!ElideMixedSourceAndDisassemblyLine(exe_ctx, sc,
508*b5893f02SDimitry Andric prologue_end_line)) {
509435933ddSDimitry Andric FileSpec func_decl_file;
510435933ddSDimitry Andric uint32_t func_decl_line;
511435933ddSDimitry Andric sc.function->GetStartLineSourceInfo(func_decl_file,
512435933ddSDimitry Andric func_decl_line);
513435933ddSDimitry Andric if (func_decl_file == prologue_end_line.file ||
514435933ddSDimitry Andric func_decl_file == prologue_end_line.original_file) {
5154ba319b5SDimitry Andric // Add all the lines between the function declaration and
5164ba319b5SDimitry Andric // the first non-prologue source line to the list of lines
5174ba319b5SDimitry Andric // to print.
518435933ddSDimitry Andric for (uint32_t lineno = func_decl_line;
519435933ddSDimitry Andric lineno <= prologue_end_line.line; lineno++) {
520435933ddSDimitry Andric SourceLine this_line;
521435933ddSDimitry Andric this_line.file = func_decl_file;
522435933ddSDimitry Andric this_line.line = lineno;
523435933ddSDimitry Andric source_lines_to_display.lines.push_back(this_line);
524435933ddSDimitry Andric }
5254ba319b5SDimitry Andric // Mark the last line as the "current" one. Usually this
5264ba319b5SDimitry Andric // is the open curly brace.
527435933ddSDimitry Andric if (source_lines_to_display.lines.size() > 0)
528435933ddSDimitry Andric source_lines_to_display.current_source_line =
529435933ddSDimitry Andric source_lines_to_display.lines.size() - 1;
530435933ddSDimitry Andric }
531435933ddSDimitry Andric }
532435933ddSDimitry Andric }
533435933ddSDimitry Andric sc.GetAddressRange(scope, 0, use_inline_block_range,
534435933ddSDimitry Andric current_source_line_range);
535435933ddSDimitry Andric }
536ac7ddfbfSEd Maste
5374ba319b5SDimitry Andric // If we've left a previous source line's address range, print a
5384ba319b5SDimitry Andric // new source line
539435933ddSDimitry Andric if (!current_source_line_range.ContainsFileAddress(addr)) {
540435933ddSDimitry Andric sc.GetAddressRange(scope, 0, use_inline_block_range,
541435933ddSDimitry Andric current_source_line_range);
542435933ddSDimitry Andric
543435933ddSDimitry Andric if (sc != prev_sc && sc.comp_unit && sc.line_entry.IsValid()) {
544435933ddSDimitry Andric SourceLine this_line;
545435933ddSDimitry Andric this_line.file = sc.line_entry.file;
546435933ddSDimitry Andric this_line.line = sc.line_entry.line;
547435933ddSDimitry Andric
548*b5893f02SDimitry Andric if (!ElideMixedSourceAndDisassemblyLine(exe_ctx, sc,
549*b5893f02SDimitry Andric this_line)) {
550435933ddSDimitry Andric // Only print this source line if it is different from the
551435933ddSDimitry Andric // last source line we printed. There may have been inlined
552435933ddSDimitry Andric // functions between these lines that we elided, resulting in
5534ba319b5SDimitry Andric // the same line being printed twice in a row for a
5544ba319b5SDimitry Andric // contiguous block of assembly instructions.
555435933ddSDimitry Andric if (this_line != previous_line) {
556435933ddSDimitry Andric
557435933ddSDimitry Andric std::vector<uint32_t> previous_lines;
558435933ddSDimitry Andric for (uint32_t i = 0;
559435933ddSDimitry Andric i < num_mixed_context_lines &&
560435933ddSDimitry Andric (this_line.line - num_mixed_context_lines) > 0;
561435933ddSDimitry Andric i++) {
562435933ddSDimitry Andric uint32_t line =
563435933ddSDimitry Andric this_line.line - num_mixed_context_lines + i;
564435933ddSDimitry Andric auto pos = source_lines_seen.find(this_line.file);
565435933ddSDimitry Andric if (pos != source_lines_seen.end()) {
566435933ddSDimitry Andric if (pos->second.count(line) == 1) {
567435933ddSDimitry Andric previous_lines.clear();
568435933ddSDimitry Andric } else {
569435933ddSDimitry Andric previous_lines.push_back(line);
570435933ddSDimitry Andric }
571435933ddSDimitry Andric }
572435933ddSDimitry Andric }
573435933ddSDimitry Andric for (size_t i = 0; i < previous_lines.size(); i++) {
574435933ddSDimitry Andric SourceLine previous_line;
575435933ddSDimitry Andric previous_line.file = this_line.file;
576435933ddSDimitry Andric previous_line.line = previous_lines[i];
577435933ddSDimitry Andric auto pos = source_lines_seen.find(previous_line.file);
578435933ddSDimitry Andric if (pos != source_lines_seen.end()) {
579435933ddSDimitry Andric pos->second.insert(previous_line.line);
580435933ddSDimitry Andric }
581435933ddSDimitry Andric source_lines_to_display.lines.push_back(previous_line);
582435933ddSDimitry Andric }
583435933ddSDimitry Andric
584435933ddSDimitry Andric source_lines_to_display.lines.push_back(this_line);
585435933ddSDimitry Andric source_lines_to_display.current_source_line =
586435933ddSDimitry Andric source_lines_to_display.lines.size() - 1;
587435933ddSDimitry Andric
588435933ddSDimitry Andric for (uint32_t i = 0; i < num_mixed_context_lines; i++) {
589435933ddSDimitry Andric SourceLine next_line;
590435933ddSDimitry Andric next_line.file = this_line.file;
591435933ddSDimitry Andric next_line.line = this_line.line + i + 1;
592435933ddSDimitry Andric auto pos = source_lines_seen.find(next_line.file);
593435933ddSDimitry Andric if (pos != source_lines_seen.end()) {
594435933ddSDimitry Andric if (pos->second.count(next_line.line) == 1)
595435933ddSDimitry Andric break;
596435933ddSDimitry Andric pos->second.insert(next_line.line);
597435933ddSDimitry Andric }
598435933ddSDimitry Andric source_lines_to_display.lines.push_back(next_line);
599435933ddSDimitry Andric }
600435933ddSDimitry Andric }
601435933ddSDimitry Andric previous_line = this_line;
602ac7ddfbfSEd Maste }
603ac7ddfbfSEd Maste }
604ac7ddfbfSEd Maste }
605ac7ddfbfSEd Maste }
606435933ddSDimitry Andric } else {
607ac7ddfbfSEd Maste sc.Clear(true);
608ac7ddfbfSEd Maste }
609ac7ddfbfSEd Maste }
610ac7ddfbfSEd Maste
611435933ddSDimitry Andric if (source_lines_to_display.lines.size() > 0) {
612435933ddSDimitry Andric strm.EOL();
613435933ddSDimitry Andric for (size_t idx = 0; idx < source_lines_to_display.lines.size();
614435933ddSDimitry Andric idx++) {
615435933ddSDimitry Andric SourceLine ln = source_lines_to_display.lines[idx];
616435933ddSDimitry Andric const char *line_highlight = "";
617435933ddSDimitry Andric if (inst_is_at_pc && (options & eOptionMarkPCSourceLine)) {
618435933ddSDimitry Andric line_highlight = "->";
619435933ddSDimitry Andric } else if (idx == source_lines_to_display.current_source_line) {
620435933ddSDimitry Andric line_highlight = "**";
621435933ddSDimitry Andric }
622435933ddSDimitry Andric source_manager.DisplaySourceLinesWithLineNumbers(
623435933ddSDimitry Andric ln.file, ln.line, ln.column, 0, 0, line_highlight, &strm);
624435933ddSDimitry Andric }
625435933ddSDimitry Andric if (source_lines_to_display.print_source_context_end_eol)
626ac7ddfbfSEd Maste strm.EOL();
627ac7ddfbfSEd Maste }
628435933ddSDimitry Andric
629435933ddSDimitry Andric const bool show_bytes = (options & eOptionShowBytes) != 0;
630435933ddSDimitry Andric inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx, &sc,
631435933ddSDimitry Andric &prev_sc, nullptr, address_text_size);
632435933ddSDimitry Andric strm.EOL();
633435933ddSDimitry Andric } else {
634ac7ddfbfSEd Maste break;
635ac7ddfbfSEd Maste }
636ac7ddfbfSEd Maste }
637ac7ddfbfSEd Maste
638ac7ddfbfSEd Maste return true;
639ac7ddfbfSEd Maste }
640ac7ddfbfSEd Maste
Disassemble(Debugger & debugger,const ArchSpec & arch,const char * plugin_name,const char * flavor,const ExecutionContext & exe_ctx,uint32_t num_instructions,bool mixed_source_and_assembly,uint32_t num_mixed_context_lines,uint32_t options,Stream & strm)641435933ddSDimitry Andric bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
642435933ddSDimitry Andric const char *plugin_name, const char *flavor,
643ac7ddfbfSEd Maste const ExecutionContext &exe_ctx,
644ac7ddfbfSEd Maste uint32_t num_instructions,
645435933ddSDimitry Andric bool mixed_source_and_assembly,
646ac7ddfbfSEd Maste uint32_t num_mixed_context_lines,
647435933ddSDimitry Andric uint32_t options, Stream &strm) {
648ac7ddfbfSEd Maste AddressRange range;
649ac7ddfbfSEd Maste StackFrame *frame = exe_ctx.GetFramePtr();
650435933ddSDimitry Andric if (frame) {
651435933ddSDimitry Andric SymbolContext sc(
652435933ddSDimitry Andric frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
653435933ddSDimitry Andric if (sc.function) {
654ac7ddfbfSEd Maste range = sc.function->GetAddressRange();
655435933ddSDimitry Andric } else if (sc.symbol && sc.symbol->ValueIsAddress()) {
6561c3bbb01SEd Maste range.GetBaseAddress() = sc.symbol->GetAddressRef();
657ac7ddfbfSEd Maste range.SetByteSize(sc.symbol->GetByteSize());
658435933ddSDimitry Andric } else {
659ac7ddfbfSEd Maste range.GetBaseAddress() = frame->GetFrameCodeAddress();
660ac7ddfbfSEd Maste }
661ac7ddfbfSEd Maste
662ac7ddfbfSEd Maste if (range.GetBaseAddress().IsValid() && range.GetByteSize() == 0)
663ac7ddfbfSEd Maste range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
664ac7ddfbfSEd Maste }
665ac7ddfbfSEd Maste
666435933ddSDimitry Andric return Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, range,
667435933ddSDimitry Andric num_instructions, mixed_source_and_assembly,
668435933ddSDimitry Andric num_mixed_context_lines, options, strm);
669ac7ddfbfSEd Maste }
670ac7ddfbfSEd Maste
Instruction(const Address & address,AddressClass addr_class)671435933ddSDimitry Andric Instruction::Instruction(const Address &address, AddressClass addr_class)
672435933ddSDimitry Andric : m_address(address), m_address_class(addr_class), m_opcode(),
673435933ddSDimitry Andric m_calculated_strings(false) {}
674ac7ddfbfSEd Maste
6754bb0738eSEd Maste Instruction::~Instruction() = default;
676ac7ddfbfSEd Maste
GetAddressClass()677435933ddSDimitry Andric AddressClass Instruction::GetAddressClass() {
6784ba319b5SDimitry Andric if (m_address_class == AddressClass::eInvalid)
679ac7ddfbfSEd Maste m_address_class = m_address.GetAddressClass();
680ac7ddfbfSEd Maste return m_address_class;
681ac7ddfbfSEd Maste }
682ac7ddfbfSEd Maste
Dump(lldb_private::Stream * s,uint32_t max_opcode_byte_size,bool show_address,bool show_bytes,const ExecutionContext * exe_ctx,const SymbolContext * sym_ctx,const SymbolContext * prev_sym_ctx,const FormatEntity::Entry * disassembly_addr_format,size_t max_address_text_size)683435933ddSDimitry Andric void Instruction::Dump(lldb_private::Stream *s, uint32_t max_opcode_byte_size,
684435933ddSDimitry Andric bool show_address, bool show_bytes,
6857aa51b79SEd Maste const ExecutionContext *exe_ctx,
6867aa51b79SEd Maste const SymbolContext *sym_ctx,
6877aa51b79SEd Maste const SymbolContext *prev_sym_ctx,
6881c3bbb01SEd Maste const FormatEntity::Entry *disassembly_addr_format,
689435933ddSDimitry Andric size_t max_address_text_size) {
690ac7ddfbfSEd Maste size_t opcode_column_width = 7;
691ac7ddfbfSEd Maste const size_t operand_column_width = 25;
692ac7ddfbfSEd Maste
693ac7ddfbfSEd Maste CalculateMnemonicOperandsAndCommentIfNeeded(exe_ctx);
694ac7ddfbfSEd Maste
695ac7ddfbfSEd Maste StreamString ss;
696ac7ddfbfSEd Maste
697435933ddSDimitry Andric if (show_address) {
698435933ddSDimitry Andric Debugger::FormatDisassemblerAddress(disassembly_addr_format, sym_ctx,
699435933ddSDimitry Andric prev_sym_ctx, exe_ctx, &m_address, ss);
7001c3bbb01SEd Maste ss.FillLastLineToColumn(max_address_text_size, ' ');
701ac7ddfbfSEd Maste }
702ac7ddfbfSEd Maste
703435933ddSDimitry Andric if (show_bytes) {
704435933ddSDimitry Andric if (m_opcode.GetType() == Opcode::eTypeBytes) {
7054ba319b5SDimitry Andric // x86_64 and i386 are the only ones that use bytes right now so pad out
7064ba319b5SDimitry Andric // the byte dump to be able to always show 15 bytes (3 chars each) plus a
7074ba319b5SDimitry Andric // space
708ac7ddfbfSEd Maste if (max_opcode_byte_size > 0)
709ac7ddfbfSEd Maste m_opcode.Dump(&ss, max_opcode_byte_size * 3 + 1);
710ac7ddfbfSEd Maste else
711ac7ddfbfSEd Maste m_opcode.Dump(&ss, 15 * 3 + 1);
712435933ddSDimitry Andric } else {
7134ba319b5SDimitry Andric // Else, we have ARM or MIPS which can show up to a uint32_t 0x00000000
7144ba319b5SDimitry Andric // (10 spaces) plus two for padding...
715ac7ddfbfSEd Maste if (max_opcode_byte_size > 0)
716ac7ddfbfSEd Maste m_opcode.Dump(&ss, max_opcode_byte_size * 3 + 1);
717ac7ddfbfSEd Maste else
718ac7ddfbfSEd Maste m_opcode.Dump(&ss, 12);
719ac7ddfbfSEd Maste }
720ac7ddfbfSEd Maste }
721ac7ddfbfSEd Maste
7227aa51b79SEd Maste const size_t opcode_pos = ss.GetSizeOfLastLine();
723ac7ddfbfSEd Maste
724ac7ddfbfSEd Maste // The default opcode size of 7 characters is plenty for most architectures
725ac7ddfbfSEd Maste // but some like arm can pull out the occasional vqrshrun.s16. We won't get
726ac7ddfbfSEd Maste // consistent column spacing in these cases, unfortunately.
727435933ddSDimitry Andric if (m_opcode_name.length() >= opcode_column_width) {
728ac7ddfbfSEd Maste opcode_column_width = m_opcode_name.length() + 1;
729ac7ddfbfSEd Maste }
730ac7ddfbfSEd Maste
731435933ddSDimitry Andric ss.PutCString(m_opcode_name);
732ac7ddfbfSEd Maste ss.FillLastLineToColumn(opcode_pos + opcode_column_width, ' ');
733435933ddSDimitry Andric ss.PutCString(m_mnemonics);
734ac7ddfbfSEd Maste
735435933ddSDimitry Andric if (!m_comment.empty()) {
736435933ddSDimitry Andric ss.FillLastLineToColumn(
737435933ddSDimitry Andric opcode_pos + opcode_column_width + operand_column_width, ' ');
738ac7ddfbfSEd Maste ss.PutCString(" ; ");
739435933ddSDimitry Andric ss.PutCString(m_comment);
740ac7ddfbfSEd Maste }
741435933ddSDimitry Andric s->PutCString(ss.GetString());
742ac7ddfbfSEd Maste }
743ac7ddfbfSEd Maste
DumpEmulation(const ArchSpec & arch)744435933ddSDimitry Andric bool Instruction::DumpEmulation(const ArchSpec &arch) {
745435933ddSDimitry Andric std::unique_ptr<EmulateInstruction> insn_emulator_ap(
746435933ddSDimitry Andric EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr));
747435933ddSDimitry Andric if (insn_emulator_ap) {
7484bb0738eSEd Maste insn_emulator_ap->SetInstruction(GetOpcode(), GetAddress(), nullptr);
749ac7ddfbfSEd Maste return insn_emulator_ap->EvaluateInstruction(0);
750ac7ddfbfSEd Maste }
751ac7ddfbfSEd Maste
752ac7ddfbfSEd Maste return false;
753ac7ddfbfSEd Maste }
754ac7ddfbfSEd Maste
CanSetBreakpoint()7550f5676f4SDimitry Andric bool Instruction::CanSetBreakpoint () {
7560f5676f4SDimitry Andric return !HasDelaySlot();
7570f5676f4SDimitry Andric }
7580f5676f4SDimitry Andric
HasDelaySlot()759435933ddSDimitry Andric bool Instruction::HasDelaySlot() {
7609f2f44ceSEd Maste // Default is false.
7619f2f44ceSEd Maste return false;
7629f2f44ceSEd Maste }
7639f2f44ceSEd Maste
ReadArray(FILE * in_file,Stream * out_stream,OptionValue::Type data_type)764435933ddSDimitry Andric OptionValueSP Instruction::ReadArray(FILE *in_file, Stream *out_stream,
765435933ddSDimitry Andric OptionValue::Type data_type) {
766ac7ddfbfSEd Maste bool done = false;
767ac7ddfbfSEd Maste char buffer[1024];
768ac7ddfbfSEd Maste
769f678e45dSDimitry Andric auto option_value_sp = std::make_shared<OptionValueArray>(1u << data_type);
770ac7ddfbfSEd Maste
771ac7ddfbfSEd Maste int idx = 0;
772435933ddSDimitry Andric while (!done) {
773435933ddSDimitry Andric if (!fgets(buffer, 1023, in_file)) {
774435933ddSDimitry Andric out_stream->Printf(
775435933ddSDimitry Andric "Instruction::ReadArray: Error reading file (fgets).\n");
776ac7ddfbfSEd Maste option_value_sp.reset();
777ac7ddfbfSEd Maste return option_value_sp;
778ac7ddfbfSEd Maste }
779ac7ddfbfSEd Maste
780ac7ddfbfSEd Maste std::string line(buffer);
781ac7ddfbfSEd Maste
782ac7ddfbfSEd Maste size_t len = line.size();
783435933ddSDimitry Andric if (line[len - 1] == '\n') {
784ac7ddfbfSEd Maste line[len - 1] = '\0';
785ac7ddfbfSEd Maste line.resize(len - 1);
786ac7ddfbfSEd Maste }
787ac7ddfbfSEd Maste
788435933ddSDimitry Andric if ((line.size() == 1) && line[0] == ']') {
789ac7ddfbfSEd Maste done = true;
790ac7ddfbfSEd Maste line.clear();
791ac7ddfbfSEd Maste }
792ac7ddfbfSEd Maste
793435933ddSDimitry Andric if (!line.empty()) {
794ac7ddfbfSEd Maste std::string value;
795435933ddSDimitry Andric static RegularExpression g_reg_exp(
796435933ddSDimitry Andric llvm::StringRef("^[ \t]*([^ \t]+)[ \t]*$"));
797ac7ddfbfSEd Maste RegularExpression::Match regex_match(1);
798435933ddSDimitry Andric bool reg_exp_success = g_reg_exp.Execute(line, ®ex_match);
799ac7ddfbfSEd Maste if (reg_exp_success)
800ac7ddfbfSEd Maste regex_match.GetMatchAtIndex(line.c_str(), 1, value);
801ac7ddfbfSEd Maste else
802ac7ddfbfSEd Maste value = line;
803ac7ddfbfSEd Maste
804ac7ddfbfSEd Maste OptionValueSP data_value_sp;
805435933ddSDimitry Andric switch (data_type) {
806ac7ddfbfSEd Maste case OptionValue::eTypeUInt64:
807f678e45dSDimitry Andric data_value_sp = std::make_shared<OptionValueUInt64>(0, 0);
8081c3bbb01SEd Maste data_value_sp->SetValueFromString(value);
809ac7ddfbfSEd Maste break;
810ac7ddfbfSEd Maste // Other types can be added later as needed.
811ac7ddfbfSEd Maste default:
812f678e45dSDimitry Andric data_value_sp = std::make_shared<OptionValueString>(value.c_str(), "");
813ac7ddfbfSEd Maste break;
814ac7ddfbfSEd Maste }
815ac7ddfbfSEd Maste
816ac7ddfbfSEd Maste option_value_sp->GetAsArray()->InsertValue(idx, data_value_sp);
817ac7ddfbfSEd Maste ++idx;
818ac7ddfbfSEd Maste }
819ac7ddfbfSEd Maste }
820ac7ddfbfSEd Maste
821ac7ddfbfSEd Maste return option_value_sp;
822ac7ddfbfSEd Maste }
823ac7ddfbfSEd Maste
ReadDictionary(FILE * in_file,Stream * out_stream)824435933ddSDimitry Andric OptionValueSP Instruction::ReadDictionary(FILE *in_file, Stream *out_stream) {
825ac7ddfbfSEd Maste bool done = false;
826ac7ddfbfSEd Maste char buffer[1024];
827ac7ddfbfSEd Maste
828f678e45dSDimitry Andric auto option_value_sp = std::make_shared<OptionValueDictionary>();
829ac7ddfbfSEd Maste static ConstString encoding_key("data_encoding");
830ac7ddfbfSEd Maste OptionValue::Type data_type = OptionValue::eTypeInvalid;
831ac7ddfbfSEd Maste
832435933ddSDimitry Andric while (!done) {
833ac7ddfbfSEd Maste // Read the next line in the file
834435933ddSDimitry Andric if (!fgets(buffer, 1023, in_file)) {
835435933ddSDimitry Andric out_stream->Printf(
836435933ddSDimitry Andric "Instruction::ReadDictionary: Error reading file (fgets).\n");
837ac7ddfbfSEd Maste option_value_sp.reset();
838ac7ddfbfSEd Maste return option_value_sp;
839ac7ddfbfSEd Maste }
840ac7ddfbfSEd Maste
841ac7ddfbfSEd Maste // Check to see if the line contains the end-of-dictionary marker ("}")
842ac7ddfbfSEd Maste std::string line(buffer);
843ac7ddfbfSEd Maste
844ac7ddfbfSEd Maste size_t len = line.size();
845435933ddSDimitry Andric if (line[len - 1] == '\n') {
846ac7ddfbfSEd Maste line[len - 1] = '\0';
847ac7ddfbfSEd Maste line.resize(len - 1);
848ac7ddfbfSEd Maste }
849ac7ddfbfSEd Maste
850435933ddSDimitry Andric if ((line.size() == 1) && (line[0] == '}')) {
851ac7ddfbfSEd Maste done = true;
852ac7ddfbfSEd Maste line.clear();
853ac7ddfbfSEd Maste }
854ac7ddfbfSEd Maste
855435933ddSDimitry Andric // Try to find a key-value pair in the current line and add it to the
856435933ddSDimitry Andric // dictionary.
857435933ddSDimitry Andric if (!line.empty()) {
858435933ddSDimitry Andric static RegularExpression g_reg_exp(llvm::StringRef(
859435933ddSDimitry Andric "^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*=[ \t]*(.*)[ \t]*$"));
860ac7ddfbfSEd Maste RegularExpression::Match regex_match(2);
861ac7ddfbfSEd Maste
862435933ddSDimitry Andric bool reg_exp_success = g_reg_exp.Execute(line, ®ex_match);
863ac7ddfbfSEd Maste std::string key;
864ac7ddfbfSEd Maste std::string value;
865435933ddSDimitry Andric if (reg_exp_success) {
866ac7ddfbfSEd Maste regex_match.GetMatchAtIndex(line.c_str(), 1, key);
867ac7ddfbfSEd Maste regex_match.GetMatchAtIndex(line.c_str(), 2, value);
868435933ddSDimitry Andric } else {
869435933ddSDimitry Andric out_stream->Printf("Instruction::ReadDictionary: Failure executing "
870435933ddSDimitry Andric "regular expression.\n");
871ac7ddfbfSEd Maste option_value_sp.reset();
872ac7ddfbfSEd Maste return option_value_sp;
873ac7ddfbfSEd Maste }
874ac7ddfbfSEd Maste
875ac7ddfbfSEd Maste ConstString const_key(key.c_str());
876ac7ddfbfSEd Maste // Check value to see if it's the start of an array or dictionary.
877ac7ddfbfSEd Maste
878ac7ddfbfSEd Maste lldb::OptionValueSP value_sp;
879ac7ddfbfSEd Maste assert(value.empty() == false);
880ac7ddfbfSEd Maste assert(key.empty() == false);
881ac7ddfbfSEd Maste
882435933ddSDimitry Andric if (value[0] == '{') {
883ac7ddfbfSEd Maste assert(value.size() == 1);
884ac7ddfbfSEd Maste // value is a dictionary
885ac7ddfbfSEd Maste value_sp = ReadDictionary(in_file, out_stream);
886435933ddSDimitry Andric if (!value_sp) {
887ac7ddfbfSEd Maste option_value_sp.reset();
888ac7ddfbfSEd Maste return option_value_sp;
889ac7ddfbfSEd Maste }
890435933ddSDimitry Andric } else if (value[0] == '[') {
891ac7ddfbfSEd Maste assert(value.size() == 1);
892ac7ddfbfSEd Maste // value is an array
893ac7ddfbfSEd Maste value_sp = ReadArray(in_file, out_stream, data_type);
894435933ddSDimitry Andric if (!value_sp) {
895ac7ddfbfSEd Maste option_value_sp.reset();
896ac7ddfbfSEd Maste return option_value_sp;
897ac7ddfbfSEd Maste }
8984ba319b5SDimitry Andric // We've used the data_type to read an array; re-set the type to
8994ba319b5SDimitry Andric // Invalid
900ac7ddfbfSEd Maste data_type = OptionValue::eTypeInvalid;
901435933ddSDimitry Andric } else if ((value[0] == '0') && (value[1] == 'x')) {
902f678e45dSDimitry Andric value_sp = std::make_shared<OptionValueUInt64>(0, 0);
9031c3bbb01SEd Maste value_sp->SetValueFromString(value);
904435933ddSDimitry Andric } else {
905ac7ddfbfSEd Maste size_t len = value.size();
906ac7ddfbfSEd Maste if ((value[0] == '"') && (value[len - 1] == '"'))
907ac7ddfbfSEd Maste value = value.substr(1, len - 2);
908f678e45dSDimitry Andric value_sp = std::make_shared<OptionValueString>(value.c_str(), "");
909ac7ddfbfSEd Maste }
910ac7ddfbfSEd Maste
911435933ddSDimitry Andric if (const_key == encoding_key) {
912435933ddSDimitry Andric // A 'data_encoding=..." is NOT a normal key-value pair; it is meta-data
913435933ddSDimitry Andric // indicating the
914435933ddSDimitry Andric // data type of an upcoming array (usually the next bit of data to be
915435933ddSDimitry Andric // read in).
916ac7ddfbfSEd Maste if (strcmp(value.c_str(), "uint32_t") == 0)
917ac7ddfbfSEd Maste data_type = OptionValue::eTypeUInt64;
918435933ddSDimitry Andric } else
919435933ddSDimitry Andric option_value_sp->GetAsDictionary()->SetValueForKey(const_key, value_sp,
920435933ddSDimitry Andric false);
921ac7ddfbfSEd Maste }
922ac7ddfbfSEd Maste }
923ac7ddfbfSEd Maste
924ac7ddfbfSEd Maste return option_value_sp;
925ac7ddfbfSEd Maste }
926ac7ddfbfSEd Maste
TestEmulation(Stream * out_stream,const char * file_name)927435933ddSDimitry Andric bool Instruction::TestEmulation(Stream *out_stream, const char *file_name) {
928ac7ddfbfSEd Maste if (!out_stream)
929ac7ddfbfSEd Maste return false;
930ac7ddfbfSEd Maste
931435933ddSDimitry Andric if (!file_name) {
932ac7ddfbfSEd Maste out_stream->Printf("Instruction::TestEmulation: Missing file_name.");
933ac7ddfbfSEd Maste return false;
934ac7ddfbfSEd Maste }
935*b5893f02SDimitry Andric FILE *test_file = FileSystem::Instance().Fopen(file_name, "r");
936435933ddSDimitry Andric if (!test_file) {
937435933ddSDimitry Andric out_stream->Printf(
938435933ddSDimitry Andric "Instruction::TestEmulation: Attempt to open test file failed.");
939ac7ddfbfSEd Maste return false;
940ac7ddfbfSEd Maste }
941ac7ddfbfSEd Maste
942ac7ddfbfSEd Maste char buffer[256];
943435933ddSDimitry Andric if (!fgets(buffer, 255, test_file)) {
944435933ddSDimitry Andric out_stream->Printf(
945435933ddSDimitry Andric "Instruction::TestEmulation: Error reading first line of test file.\n");
946ac7ddfbfSEd Maste fclose(test_file);
947ac7ddfbfSEd Maste return false;
948ac7ddfbfSEd Maste }
949ac7ddfbfSEd Maste
950435933ddSDimitry Andric if (strncmp(buffer, "InstructionEmulationState={", 27) != 0) {
951435933ddSDimitry Andric out_stream->Printf("Instructin::TestEmulation: Test file does not contain "
952435933ddSDimitry Andric "emulation state dictionary\n");
953ac7ddfbfSEd Maste fclose(test_file);
954ac7ddfbfSEd Maste return false;
955ac7ddfbfSEd Maste }
956ac7ddfbfSEd Maste
957435933ddSDimitry Andric // Read all the test information from the test file into an
958435933ddSDimitry Andric // OptionValueDictionary.
959ac7ddfbfSEd Maste
960ac7ddfbfSEd Maste OptionValueSP data_dictionary_sp(ReadDictionary(test_file, out_stream));
961435933ddSDimitry Andric if (!data_dictionary_sp) {
962435933ddSDimitry Andric out_stream->Printf(
963435933ddSDimitry Andric "Instruction::TestEmulation: Error reading Dictionary Object.\n");
964ac7ddfbfSEd Maste fclose(test_file);
965ac7ddfbfSEd Maste return false;
966ac7ddfbfSEd Maste }
967ac7ddfbfSEd Maste
968ac7ddfbfSEd Maste fclose(test_file);
969ac7ddfbfSEd Maste
970435933ddSDimitry Andric OptionValueDictionary *data_dictionary =
971435933ddSDimitry Andric data_dictionary_sp->GetAsDictionary();
972ac7ddfbfSEd Maste static ConstString description_key("assembly_string");
973ac7ddfbfSEd Maste static ConstString triple_key("triple");
974ac7ddfbfSEd Maste
975ac7ddfbfSEd Maste OptionValueSP value_sp = data_dictionary->GetValueForKey(description_key);
976ac7ddfbfSEd Maste
977435933ddSDimitry Andric if (!value_sp) {
978435933ddSDimitry Andric out_stream->Printf("Instruction::TestEmulation: Test file does not "
979435933ddSDimitry Andric "contain description string.\n");
980ac7ddfbfSEd Maste return false;
981ac7ddfbfSEd Maste }
982ac7ddfbfSEd Maste
983ac7ddfbfSEd Maste SetDescription(value_sp->GetStringValue());
984ac7ddfbfSEd Maste
985ac7ddfbfSEd Maste value_sp = data_dictionary->GetValueForKey(triple_key);
986435933ddSDimitry Andric if (!value_sp) {
987435933ddSDimitry Andric out_stream->Printf(
988435933ddSDimitry Andric "Instruction::TestEmulation: Test file does not contain triple.\n");
989ac7ddfbfSEd Maste return false;
990ac7ddfbfSEd Maste }
991ac7ddfbfSEd Maste
992ac7ddfbfSEd Maste ArchSpec arch;
993ac7ddfbfSEd Maste arch.SetTriple(llvm::Triple(value_sp->GetStringValue()));
994ac7ddfbfSEd Maste
995ac7ddfbfSEd Maste bool success = false;
996435933ddSDimitry Andric std::unique_ptr<EmulateInstruction> insn_emulator_ap(
997435933ddSDimitry Andric EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr));
9984bb0738eSEd Maste if (insn_emulator_ap)
999435933ddSDimitry Andric success =
1000435933ddSDimitry Andric insn_emulator_ap->TestEmulation(out_stream, arch, data_dictionary);
1001ac7ddfbfSEd Maste
1002ac7ddfbfSEd Maste if (success)
1003ac7ddfbfSEd Maste out_stream->Printf("Emulation test succeeded.");
1004ac7ddfbfSEd Maste else
1005ac7ddfbfSEd Maste out_stream->Printf("Emulation test failed.");
1006ac7ddfbfSEd Maste
1007ac7ddfbfSEd Maste return success;
1008ac7ddfbfSEd Maste }
1009ac7ddfbfSEd Maste
Emulate(const ArchSpec & arch,uint32_t evaluate_options,void * baton,EmulateInstruction::ReadMemoryCallback read_mem_callback,EmulateInstruction::WriteMemoryCallback write_mem_callback,EmulateInstruction::ReadRegisterCallback read_reg_callback,EmulateInstruction::WriteRegisterCallback write_reg_callback)1010435933ddSDimitry Andric bool Instruction::Emulate(
1011435933ddSDimitry Andric const ArchSpec &arch, uint32_t evaluate_options, void *baton,
1012ac7ddfbfSEd Maste EmulateInstruction::ReadMemoryCallback read_mem_callback,
1013ac7ddfbfSEd Maste EmulateInstruction::WriteMemoryCallback write_mem_callback,
1014ac7ddfbfSEd Maste EmulateInstruction::ReadRegisterCallback read_reg_callback,
1015435933ddSDimitry Andric EmulateInstruction::WriteRegisterCallback write_reg_callback) {
1016435933ddSDimitry Andric std::unique_ptr<EmulateInstruction> insn_emulator_ap(
1017435933ddSDimitry Andric EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr));
1018435933ddSDimitry Andric if (insn_emulator_ap) {
1019ac7ddfbfSEd Maste insn_emulator_ap->SetBaton(baton);
1020435933ddSDimitry Andric insn_emulator_ap->SetCallbacks(read_mem_callback, write_mem_callback,
1021435933ddSDimitry Andric read_reg_callback, write_reg_callback);
10224bb0738eSEd Maste insn_emulator_ap->SetInstruction(GetOpcode(), GetAddress(), nullptr);
1023ac7ddfbfSEd Maste return insn_emulator_ap->EvaluateInstruction(evaluate_options);
1024ac7ddfbfSEd Maste }
1025ac7ddfbfSEd Maste
1026ac7ddfbfSEd Maste return false;
1027ac7ddfbfSEd Maste }
1028ac7ddfbfSEd Maste
GetData(DataExtractor & data)1029435933ddSDimitry Andric uint32_t Instruction::GetData(DataExtractor &data) {
1030ac7ddfbfSEd Maste return m_opcode.GetData(data);
1031ac7ddfbfSEd Maste }
1032ac7ddfbfSEd Maste
InstructionList()1033435933ddSDimitry Andric InstructionList::InstructionList() : m_instructions() {}
1034ac7ddfbfSEd Maste
10354bb0738eSEd Maste InstructionList::~InstructionList() = default;
1036ac7ddfbfSEd Maste
GetSize() const1037435933ddSDimitry Andric size_t InstructionList::GetSize() const { return m_instructions.size(); }
1038ac7ddfbfSEd Maste
GetMaxOpcocdeByteSize() const1039435933ddSDimitry Andric uint32_t InstructionList::GetMaxOpcocdeByteSize() const {
1040ac7ddfbfSEd Maste uint32_t max_inst_size = 0;
1041ac7ddfbfSEd Maste collection::const_iterator pos, end;
1042435933ddSDimitry Andric for (pos = m_instructions.begin(), end = m_instructions.end(); pos != end;
1043435933ddSDimitry Andric ++pos) {
1044ac7ddfbfSEd Maste uint32_t inst_size = (*pos)->GetOpcode().GetByteSize();
1045ac7ddfbfSEd Maste if (max_inst_size < inst_size)
1046ac7ddfbfSEd Maste max_inst_size = inst_size;
1047ac7ddfbfSEd Maste }
1048ac7ddfbfSEd Maste return max_inst_size;
1049ac7ddfbfSEd Maste }
1050ac7ddfbfSEd Maste
GetInstructionAtIndex(size_t idx) const1051435933ddSDimitry Andric InstructionSP InstructionList::GetInstructionAtIndex(size_t idx) const {
1052ac7ddfbfSEd Maste InstructionSP inst_sp;
1053ac7ddfbfSEd Maste if (idx < m_instructions.size())
1054ac7ddfbfSEd Maste inst_sp = m_instructions[idx];
1055ac7ddfbfSEd Maste return inst_sp;
1056ac7ddfbfSEd Maste }
1057ac7ddfbfSEd Maste
Dump(Stream * s,bool show_address,bool show_bytes,const ExecutionContext * exe_ctx)1058435933ddSDimitry Andric void InstructionList::Dump(Stream *s, bool show_address, bool show_bytes,
1059435933ddSDimitry Andric const ExecutionContext *exe_ctx) {
1060ac7ddfbfSEd Maste const uint32_t max_opcode_byte_size = GetMaxOpcocdeByteSize();
1061ac7ddfbfSEd Maste collection::const_iterator pos, begin, end;
10621c3bbb01SEd Maste
10634bb0738eSEd Maste const FormatEntity::Entry *disassembly_format = nullptr;
10641c3bbb01SEd Maste FormatEntity::Entry format;
1065435933ddSDimitry Andric if (exe_ctx && exe_ctx->HasTargetScope()) {
1066435933ddSDimitry Andric disassembly_format =
1067435933ddSDimitry Andric exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
1068435933ddSDimitry Andric } else {
10691c3bbb01SEd Maste FormatEntity::Parse("${addr}: ", format);
10701c3bbb01SEd Maste disassembly_format = &format;
10711c3bbb01SEd Maste }
10721c3bbb01SEd Maste
1073ac7ddfbfSEd Maste for (begin = m_instructions.begin(), end = m_instructions.end(), pos = begin;
1074435933ddSDimitry Andric pos != end; ++pos) {
1075ac7ddfbfSEd Maste if (pos != begin)
1076ac7ddfbfSEd Maste s->EOL();
1077435933ddSDimitry Andric (*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx,
1078435933ddSDimitry Andric nullptr, nullptr, disassembly_format, 0);
1079ac7ddfbfSEd Maste }
1080ac7ddfbfSEd Maste }
1081ac7ddfbfSEd Maste
Clear()1082435933ddSDimitry Andric void InstructionList::Clear() { m_instructions.clear(); }
1083ac7ddfbfSEd Maste
Append(lldb::InstructionSP & inst_sp)1084435933ddSDimitry Andric void InstructionList::Append(lldb::InstructionSP &inst_sp) {
1085ac7ddfbfSEd Maste if (inst_sp)
1086ac7ddfbfSEd Maste m_instructions.push_back(inst_sp);
1087ac7ddfbfSEd Maste }
1088ac7ddfbfSEd Maste
1089ac7ddfbfSEd Maste uint32_t
GetIndexOfNextBranchInstruction(uint32_t start,Target & target) const1090435933ddSDimitry Andric InstructionList::GetIndexOfNextBranchInstruction(uint32_t start,
1091435933ddSDimitry Andric Target &target) const {
1092ac7ddfbfSEd Maste size_t num_instructions = m_instructions.size();
1093ac7ddfbfSEd Maste
1094ac7ddfbfSEd Maste uint32_t next_branch = UINT32_MAX;
10951c3bbb01SEd Maste size_t i;
1096435933ddSDimitry Andric for (i = start; i < num_instructions; i++) {
1097435933ddSDimitry Andric if (m_instructions[i]->DoesBranch()) {
1098ac7ddfbfSEd Maste next_branch = i;
1099ac7ddfbfSEd Maste break;
1100ac7ddfbfSEd Maste }
1101ac7ddfbfSEd Maste }
11021c3bbb01SEd Maste
11034ba319b5SDimitry Andric // Hexagon needs the first instruction of the packet with the branch. Go
11044ba319b5SDimitry Andric // backwards until we find an instruction marked end-of-packet, or until we
11054ba319b5SDimitry Andric // hit start.
1106435933ddSDimitry Andric if (target.GetArchitecture().GetTriple().getArch() == llvm::Triple::hexagon) {
11071c3bbb01SEd Maste // If we didn't find a branch, find the last packet start.
1108435933ddSDimitry Andric if (next_branch == UINT32_MAX) {
11091c3bbb01SEd Maste i = num_instructions - 1;
11101c3bbb01SEd Maste }
11111c3bbb01SEd Maste
1112435933ddSDimitry Andric while (i > start) {
11131c3bbb01SEd Maste --i;
11141c3bbb01SEd Maste
11155517e702SDimitry Andric Status error;
11161c3bbb01SEd Maste uint32_t inst_bytes;
11171c3bbb01SEd Maste bool prefer_file_cache = false; // Read from process if process is running
11181c3bbb01SEd Maste lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1119435933ddSDimitry Andric target.ReadMemory(m_instructions[i]->GetAddress(), prefer_file_cache,
1120435933ddSDimitry Andric &inst_bytes, sizeof(inst_bytes), error, &load_addr);
11211c3bbb01SEd Maste // If we have an error reading memory, return start
11221c3bbb01SEd Maste if (!error.Success())
11231c3bbb01SEd Maste return start;
11244ba319b5SDimitry Andric // check if this is the last instruction in a packet bits 15:14 will be
11254ba319b5SDimitry Andric // 11b or 00b for a duplex
11261c3bbb01SEd Maste if (((inst_bytes & 0xC000) == 0xC000) ||
1127435933ddSDimitry Andric ((inst_bytes & 0xC000) == 0x0000)) {
11281c3bbb01SEd Maste // instruction after this should be the start of next packet
11291c3bbb01SEd Maste next_branch = i + 1;
11301c3bbb01SEd Maste break;
11311c3bbb01SEd Maste }
11321c3bbb01SEd Maste }
11331c3bbb01SEd Maste
1134435933ddSDimitry Andric if (next_branch == UINT32_MAX) {
11351c3bbb01SEd Maste // We couldn't find the previous packet, so return start
11361c3bbb01SEd Maste next_branch = start;
11371c3bbb01SEd Maste }
11381c3bbb01SEd Maste }
1139ac7ddfbfSEd Maste return next_branch;
1140ac7ddfbfSEd Maste }
1141ac7ddfbfSEd Maste
1142ac7ddfbfSEd Maste uint32_t
GetIndexOfInstructionAtAddress(const Address & address)1143435933ddSDimitry Andric InstructionList::GetIndexOfInstructionAtAddress(const Address &address) {
1144ac7ddfbfSEd Maste size_t num_instructions = m_instructions.size();
1145ac7ddfbfSEd Maste uint32_t index = UINT32_MAX;
1146435933ddSDimitry Andric for (size_t i = 0; i < num_instructions; i++) {
1147435933ddSDimitry Andric if (m_instructions[i]->GetAddress() == address) {
1148ac7ddfbfSEd Maste index = i;
1149ac7ddfbfSEd Maste break;
1150ac7ddfbfSEd Maste }
1151ac7ddfbfSEd Maste }
1152ac7ddfbfSEd Maste return index;
1153ac7ddfbfSEd Maste }
1154ac7ddfbfSEd Maste
115512b93ac6SEd Maste uint32_t
GetIndexOfInstructionAtLoadAddress(lldb::addr_t load_addr,Target & target)1156435933ddSDimitry Andric InstructionList::GetIndexOfInstructionAtLoadAddress(lldb::addr_t load_addr,
1157435933ddSDimitry Andric Target &target) {
115812b93ac6SEd Maste Address address;
115912b93ac6SEd Maste address.SetLoadAddress(load_addr, &target);
116012b93ac6SEd Maste return GetIndexOfInstructionAtAddress(address);
116112b93ac6SEd Maste }
116212b93ac6SEd Maste
ParseInstructions(const ExecutionContext * exe_ctx,const AddressRange & range,Stream * error_strm_ptr,bool prefer_file_cache)1163435933ddSDimitry Andric size_t Disassembler::ParseInstructions(const ExecutionContext *exe_ctx,
1164ac7ddfbfSEd Maste const AddressRange &range,
1165ac7ddfbfSEd Maste Stream *error_strm_ptr,
1166435933ddSDimitry Andric bool prefer_file_cache) {
1167435933ddSDimitry Andric if (exe_ctx) {
1168ac7ddfbfSEd Maste Target *target = exe_ctx->GetTargetPtr();
1169ac7ddfbfSEd Maste const addr_t byte_size = range.GetByteSize();
1170435933ddSDimitry Andric if (target == nullptr || byte_size == 0 ||
1171435933ddSDimitry Andric !range.GetBaseAddress().IsValid())
1172ac7ddfbfSEd Maste return 0;
1173ac7ddfbfSEd Maste
1174f678e45dSDimitry Andric auto data_sp = std::make_shared<DataBufferHeap>(byte_size, '\0');
1175ac7ddfbfSEd Maste
11765517e702SDimitry Andric Status error;
1177ac7ddfbfSEd Maste lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1178435933ddSDimitry Andric const size_t bytes_read = target->ReadMemory(
1179f678e45dSDimitry Andric range.GetBaseAddress(), prefer_file_cache, data_sp->GetBytes(),
1180f678e45dSDimitry Andric data_sp->GetByteSize(), error, &load_addr);
1181ac7ddfbfSEd Maste
1182435933ddSDimitry Andric if (bytes_read > 0) {
1183f678e45dSDimitry Andric if (bytes_read != data_sp->GetByteSize())
1184f678e45dSDimitry Andric data_sp->SetByteSize(bytes_read);
1185435933ddSDimitry Andric DataExtractor data(data_sp, m_arch.GetByteOrder(),
1186ac7ddfbfSEd Maste m_arch.GetAddressByteSize());
1187ac7ddfbfSEd Maste const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1188435933ddSDimitry Andric return DecodeInstructions(range.GetBaseAddress(), data, 0, UINT32_MAX,
1189435933ddSDimitry Andric false, data_from_file);
1190435933ddSDimitry Andric } else if (error_strm_ptr) {
1191ac7ddfbfSEd Maste const char *error_cstr = error.AsCString();
1192435933ddSDimitry Andric if (error_cstr) {
1193ac7ddfbfSEd Maste error_strm_ptr->Printf("error: %s\n", error_cstr);
1194ac7ddfbfSEd Maste }
1195ac7ddfbfSEd Maste }
1196435933ddSDimitry Andric } else if (error_strm_ptr) {
1197ac7ddfbfSEd Maste error_strm_ptr->PutCString("error: invalid execution context\n");
1198ac7ddfbfSEd Maste }
1199ac7ddfbfSEd Maste return 0;
1200ac7ddfbfSEd Maste }
1201ac7ddfbfSEd Maste
ParseInstructions(const ExecutionContext * exe_ctx,const Address & start,uint32_t num_instructions,bool prefer_file_cache)1202435933ddSDimitry Andric size_t Disassembler::ParseInstructions(const ExecutionContext *exe_ctx,
1203ac7ddfbfSEd Maste const Address &start,
1204ac7ddfbfSEd Maste uint32_t num_instructions,
1205435933ddSDimitry Andric bool prefer_file_cache) {
1206ac7ddfbfSEd Maste m_instruction_list.Clear();
1207ac7ddfbfSEd Maste
12084bb0738eSEd Maste if (exe_ctx == nullptr || num_instructions == 0 || !start.IsValid())
1209ac7ddfbfSEd Maste return 0;
1210ac7ddfbfSEd Maste
1211ac7ddfbfSEd Maste Target *target = exe_ctx->GetTargetPtr();
1212ac7ddfbfSEd Maste // Calculate the max buffer size we will need in order to disassemble
1213ac7ddfbfSEd Maste const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize();
1214ac7ddfbfSEd Maste
12154bb0738eSEd Maste if (target == nullptr || byte_size == 0)
1216ac7ddfbfSEd Maste return 0;
1217ac7ddfbfSEd Maste
1218ac7ddfbfSEd Maste DataBufferHeap *heap_buffer = new DataBufferHeap(byte_size, '\0');
1219ac7ddfbfSEd Maste DataBufferSP data_sp(heap_buffer);
1220ac7ddfbfSEd Maste
12215517e702SDimitry Andric Status error;
1222ac7ddfbfSEd Maste lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1223435933ddSDimitry Andric const size_t bytes_read =
1224435933ddSDimitry Andric target->ReadMemory(start, prefer_file_cache, heap_buffer->GetBytes(),
1225435933ddSDimitry Andric byte_size, error, &load_addr);
1226ac7ddfbfSEd Maste
1227ac7ddfbfSEd Maste const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1228ac7ddfbfSEd Maste
1229ac7ddfbfSEd Maste if (bytes_read == 0)
1230ac7ddfbfSEd Maste return 0;
1231435933ddSDimitry Andric DataExtractor data(data_sp, m_arch.GetByteOrder(),
1232ac7ddfbfSEd Maste m_arch.GetAddressByteSize());
1233ac7ddfbfSEd Maste
1234ac7ddfbfSEd Maste const bool append_instructions = true;
1235435933ddSDimitry Andric DecodeInstructions(start, data, 0, num_instructions, append_instructions,
1236ac7ddfbfSEd Maste data_from_file);
1237ac7ddfbfSEd Maste
1238ac7ddfbfSEd Maste return m_instruction_list.GetSize();
1239ac7ddfbfSEd Maste }
1240ac7ddfbfSEd Maste
1241ac7ddfbfSEd Maste //----------------------------------------------------------------------
1242ac7ddfbfSEd Maste // Disassembler copy constructor
1243ac7ddfbfSEd Maste //----------------------------------------------------------------------
Disassembler(const ArchSpec & arch,const char * flavor)1244435933ddSDimitry Andric Disassembler::Disassembler(const ArchSpec &arch, const char *flavor)
1245435933ddSDimitry Andric : m_arch(arch), m_instruction_list(), m_base_addr(LLDB_INVALID_ADDRESS),
1246435933ddSDimitry Andric m_flavor() {
12474bb0738eSEd Maste if (flavor == nullptr)
1248ac7ddfbfSEd Maste m_flavor.assign("default");
1249ac7ddfbfSEd Maste else
1250ac7ddfbfSEd Maste m_flavor.assign(flavor);
12511c3bbb01SEd Maste
12521c3bbb01SEd Maste // If this is an arm variant that can only include thumb (T16, T32)
12534ba319b5SDimitry Andric // instructions, force the arch triple to be "thumbv.." instead of "armv..."
1254435933ddSDimitry Andric if (arch.IsAlwaysThumbInstructions()) {
12551c3bbb01SEd Maste std::string thumb_arch_name(arch.GetTriple().getArchName().str());
12561c3bbb01SEd Maste // Replace "arm" with "thumb" so we get all thumb variants correct
1257435933ddSDimitry Andric if (thumb_arch_name.size() > 3) {
12581c3bbb01SEd Maste thumb_arch_name.erase(0, 3);
12591c3bbb01SEd Maste thumb_arch_name.insert(0, "thumb");
12601c3bbb01SEd Maste }
12611c3bbb01SEd Maste m_arch.SetTriple(thumb_arch_name.c_str());
12621c3bbb01SEd Maste }
1263ac7ddfbfSEd Maste }
1264ac7ddfbfSEd Maste
12654bb0738eSEd Maste Disassembler::~Disassembler() = default;
1266ac7ddfbfSEd Maste
GetInstructionList()1267435933ddSDimitry Andric InstructionList &Disassembler::GetInstructionList() {
1268ac7ddfbfSEd Maste return m_instruction_list;
1269ac7ddfbfSEd Maste }
1270ac7ddfbfSEd Maste
GetInstructionList() const1271435933ddSDimitry Andric const InstructionList &Disassembler::GetInstructionList() const {
1272ac7ddfbfSEd Maste return m_instruction_list;
1273ac7ddfbfSEd Maste }
1274ac7ddfbfSEd Maste
1275ac7ddfbfSEd Maste //----------------------------------------------------------------------
1276ac7ddfbfSEd Maste // Class PseudoInstruction
1277ac7ddfbfSEd Maste //----------------------------------------------------------------------
12784bb0738eSEd Maste
PseudoInstruction()1279435933ddSDimitry Andric PseudoInstruction::PseudoInstruction()
12804ba319b5SDimitry Andric : Instruction(Address(), AddressClass::eUnknown), m_description() {}
1281ac7ddfbfSEd Maste
12824bb0738eSEd Maste PseudoInstruction::~PseudoInstruction() = default;
1283ac7ddfbfSEd Maste
DoesBranch()1284435933ddSDimitry Andric bool PseudoInstruction::DoesBranch() {
1285ac7ddfbfSEd Maste // This is NOT a valid question for a pseudo instruction.
1286ac7ddfbfSEd Maste return false;
1287ac7ddfbfSEd Maste }
1288ac7ddfbfSEd Maste
HasDelaySlot()1289435933ddSDimitry Andric bool PseudoInstruction::HasDelaySlot() {
12909f2f44ceSEd Maste // This is NOT a valid question for a pseudo instruction.
12919f2f44ceSEd Maste return false;
12929f2f44ceSEd Maste }
12939f2f44ceSEd Maste
Decode(const lldb_private::Disassembler & disassembler,const lldb_private::DataExtractor & data,lldb::offset_t data_offset)1294435933ddSDimitry Andric size_t PseudoInstruction::Decode(const lldb_private::Disassembler &disassembler,
1295ac7ddfbfSEd Maste const lldb_private::DataExtractor &data,
1296435933ddSDimitry Andric lldb::offset_t data_offset) {
1297ac7ddfbfSEd Maste return m_opcode.GetByteSize();
1298ac7ddfbfSEd Maste }
1299ac7ddfbfSEd Maste
SetOpcode(size_t opcode_size,void * opcode_data)1300435933ddSDimitry Andric void PseudoInstruction::SetOpcode(size_t opcode_size, void *opcode_data) {
1301ac7ddfbfSEd Maste if (!opcode_data)
1302ac7ddfbfSEd Maste return;
1303ac7ddfbfSEd Maste
1304435933ddSDimitry Andric switch (opcode_size) {
1305435933ddSDimitry Andric case 8: {
1306ac7ddfbfSEd Maste uint8_t value8 = *((uint8_t *)opcode_data);
130712b93ac6SEd Maste m_opcode.SetOpcode8(value8, eByteOrderInvalid);
1308ac7ddfbfSEd Maste break;
1309ac7ddfbfSEd Maste }
1310435933ddSDimitry Andric case 16: {
1311ac7ddfbfSEd Maste uint16_t value16 = *((uint16_t *)opcode_data);
131212b93ac6SEd Maste m_opcode.SetOpcode16(value16, eByteOrderInvalid);
1313ac7ddfbfSEd Maste break;
1314ac7ddfbfSEd Maste }
1315435933ddSDimitry Andric case 32: {
1316ac7ddfbfSEd Maste uint32_t value32 = *((uint32_t *)opcode_data);
131712b93ac6SEd Maste m_opcode.SetOpcode32(value32, eByteOrderInvalid);
1318ac7ddfbfSEd Maste break;
1319ac7ddfbfSEd Maste }
1320435933ddSDimitry Andric case 64: {
1321ac7ddfbfSEd Maste uint64_t value64 = *((uint64_t *)opcode_data);
132212b93ac6SEd Maste m_opcode.SetOpcode64(value64, eByteOrderInvalid);
1323ac7ddfbfSEd Maste break;
1324ac7ddfbfSEd Maste }
1325ac7ddfbfSEd Maste default:
1326ac7ddfbfSEd Maste break;
1327ac7ddfbfSEd Maste }
1328ac7ddfbfSEd Maste }
1329ac7ddfbfSEd Maste
SetDescription(llvm::StringRef description)1330435933ddSDimitry Andric void PseudoInstruction::SetDescription(llvm::StringRef description) {
1331ac7ddfbfSEd Maste m_description = description;
1332ac7ddfbfSEd Maste }
1333435933ddSDimitry Andric
BuildRegister(ConstString & r)1334435933ddSDimitry Andric Instruction::Operand Instruction::Operand::BuildRegister(ConstString &r) {
1335435933ddSDimitry Andric Operand ret;
1336435933ddSDimitry Andric ret.m_type = Type::Register;
1337435933ddSDimitry Andric ret.m_register = r;
1338435933ddSDimitry Andric return ret;
1339435933ddSDimitry Andric }
1340435933ddSDimitry Andric
BuildImmediate(lldb::addr_t imm,bool neg)1341435933ddSDimitry Andric Instruction::Operand Instruction::Operand::BuildImmediate(lldb::addr_t imm,
1342435933ddSDimitry Andric bool neg) {
1343435933ddSDimitry Andric Operand ret;
1344435933ddSDimitry Andric ret.m_type = Type::Immediate;
1345435933ddSDimitry Andric ret.m_immediate = imm;
1346435933ddSDimitry Andric ret.m_negative = neg;
1347435933ddSDimitry Andric return ret;
1348435933ddSDimitry Andric }
1349435933ddSDimitry Andric
BuildImmediate(int64_t imm)1350435933ddSDimitry Andric Instruction::Operand Instruction::Operand::BuildImmediate(int64_t imm) {
1351435933ddSDimitry Andric Operand ret;
1352435933ddSDimitry Andric ret.m_type = Type::Immediate;
1353435933ddSDimitry Andric if (imm < 0) {
1354435933ddSDimitry Andric ret.m_immediate = -imm;
1355435933ddSDimitry Andric ret.m_negative = true;
1356435933ddSDimitry Andric } else {
1357435933ddSDimitry Andric ret.m_immediate = imm;
1358435933ddSDimitry Andric ret.m_negative = false;
1359435933ddSDimitry Andric }
1360435933ddSDimitry Andric return ret;
1361435933ddSDimitry Andric }
1362435933ddSDimitry Andric
1363435933ddSDimitry Andric Instruction::Operand
BuildDereference(const Operand & ref)1364435933ddSDimitry Andric Instruction::Operand::BuildDereference(const Operand &ref) {
1365435933ddSDimitry Andric Operand ret;
1366435933ddSDimitry Andric ret.m_type = Type::Dereference;
1367435933ddSDimitry Andric ret.m_children = {ref};
1368435933ddSDimitry Andric return ret;
1369435933ddSDimitry Andric }
1370435933ddSDimitry Andric
BuildSum(const Operand & lhs,const Operand & rhs)1371435933ddSDimitry Andric Instruction::Operand Instruction::Operand::BuildSum(const Operand &lhs,
1372435933ddSDimitry Andric const Operand &rhs) {
1373435933ddSDimitry Andric Operand ret;
1374435933ddSDimitry Andric ret.m_type = Type::Sum;
1375435933ddSDimitry Andric ret.m_children = {lhs, rhs};
1376435933ddSDimitry Andric return ret;
1377435933ddSDimitry Andric }
1378435933ddSDimitry Andric
BuildProduct(const Operand & lhs,const Operand & rhs)1379435933ddSDimitry Andric Instruction::Operand Instruction::Operand::BuildProduct(const Operand &lhs,
1380435933ddSDimitry Andric const Operand &rhs) {
1381435933ddSDimitry Andric Operand ret;
1382435933ddSDimitry Andric ret.m_type = Type::Product;
1383435933ddSDimitry Andric ret.m_children = {lhs, rhs};
1384435933ddSDimitry Andric return ret;
1385435933ddSDimitry Andric }
1386435933ddSDimitry Andric
1387435933ddSDimitry Andric std::function<bool(const Instruction::Operand &)>
MatchBinaryOp(std::function<bool (const Instruction::Operand &)> base,std::function<bool (const Instruction::Operand &)> left,std::function<bool (const Instruction::Operand &)> right)1388435933ddSDimitry Andric lldb_private::OperandMatchers::MatchBinaryOp(
1389435933ddSDimitry Andric std::function<bool(const Instruction::Operand &)> base,
1390435933ddSDimitry Andric std::function<bool(const Instruction::Operand &)> left,
1391435933ddSDimitry Andric std::function<bool(const Instruction::Operand &)> right) {
1392435933ddSDimitry Andric return [base, left, right](const Instruction::Operand &op) -> bool {
1393435933ddSDimitry Andric return (base(op) && op.m_children.size() == 2 &&
1394435933ddSDimitry Andric ((left(op.m_children[0]) && right(op.m_children[1])) ||
1395435933ddSDimitry Andric (left(op.m_children[1]) && right(op.m_children[0]))));
1396435933ddSDimitry Andric };
1397435933ddSDimitry Andric }
1398435933ddSDimitry Andric
1399435933ddSDimitry Andric std::function<bool(const Instruction::Operand &)>
MatchUnaryOp(std::function<bool (const Instruction::Operand &)> base,std::function<bool (const Instruction::Operand &)> child)1400435933ddSDimitry Andric lldb_private::OperandMatchers::MatchUnaryOp(
1401435933ddSDimitry Andric std::function<bool(const Instruction::Operand &)> base,
1402435933ddSDimitry Andric std::function<bool(const Instruction::Operand &)> child) {
1403435933ddSDimitry Andric return [base, child](const Instruction::Operand &op) -> bool {
1404435933ddSDimitry Andric return (base(op) && op.m_children.size() == 1 && child(op.m_children[0]));
1405435933ddSDimitry Andric };
1406435933ddSDimitry Andric }
1407435933ddSDimitry Andric
1408435933ddSDimitry Andric std::function<bool(const Instruction::Operand &)>
MatchRegOp(const RegisterInfo & info)1409435933ddSDimitry Andric lldb_private::OperandMatchers::MatchRegOp(const RegisterInfo &info) {
1410435933ddSDimitry Andric return [&info](const Instruction::Operand &op) {
1411435933ddSDimitry Andric return (op.m_type == Instruction::Operand::Type::Register &&
1412435933ddSDimitry Andric (op.m_register == ConstString(info.name) ||
1413435933ddSDimitry Andric op.m_register == ConstString(info.alt_name)));
1414435933ddSDimitry Andric };
1415435933ddSDimitry Andric }
1416435933ddSDimitry Andric
1417435933ddSDimitry Andric std::function<bool(const Instruction::Operand &)>
FetchRegOp(ConstString & reg)1418435933ddSDimitry Andric lldb_private::OperandMatchers::FetchRegOp(ConstString ®) {
1419435933ddSDimitry Andric return [®](const Instruction::Operand &op) {
1420435933ddSDimitry Andric if (op.m_type != Instruction::Operand::Type::Register) {
1421435933ddSDimitry Andric return false;
1422435933ddSDimitry Andric }
1423435933ddSDimitry Andric reg = op.m_register;
1424435933ddSDimitry Andric return true;
1425435933ddSDimitry Andric };
1426435933ddSDimitry Andric }
1427435933ddSDimitry Andric
1428435933ddSDimitry Andric std::function<bool(const Instruction::Operand &)>
MatchImmOp(int64_t imm)1429435933ddSDimitry Andric lldb_private::OperandMatchers::MatchImmOp(int64_t imm) {
1430435933ddSDimitry Andric return [imm](const Instruction::Operand &op) {
1431435933ddSDimitry Andric return (op.m_type == Instruction::Operand::Type::Immediate &&
1432435933ddSDimitry Andric ((op.m_negative && op.m_immediate == (uint64_t)-imm) ||
1433435933ddSDimitry Andric (!op.m_negative && op.m_immediate == (uint64_t)imm)));
1434435933ddSDimitry Andric };
1435435933ddSDimitry Andric }
1436435933ddSDimitry Andric
1437435933ddSDimitry Andric std::function<bool(const Instruction::Operand &)>
FetchImmOp(int64_t & imm)1438435933ddSDimitry Andric lldb_private::OperandMatchers::FetchImmOp(int64_t &imm) {
1439435933ddSDimitry Andric return [&imm](const Instruction::Operand &op) {
1440435933ddSDimitry Andric if (op.m_type != Instruction::Operand::Type::Immediate) {
1441435933ddSDimitry Andric return false;
1442435933ddSDimitry Andric }
1443435933ddSDimitry Andric if (op.m_negative) {
1444435933ddSDimitry Andric imm = -((int64_t)op.m_immediate);
1445435933ddSDimitry Andric } else {
1446435933ddSDimitry Andric imm = ((int64_t)op.m_immediate);
1447435933ddSDimitry Andric }
1448435933ddSDimitry Andric return true;
1449435933ddSDimitry Andric };
1450435933ddSDimitry Andric }
1451435933ddSDimitry Andric
1452435933ddSDimitry Andric std::function<bool(const Instruction::Operand &)>
MatchOpType(Instruction::Operand::Type type)1453435933ddSDimitry Andric lldb_private::OperandMatchers::MatchOpType(Instruction::Operand::Type type) {
1454435933ddSDimitry Andric return [type](const Instruction::Operand &op) { return op.m_type == type; };
1455435933ddSDimitry Andric }
1456