1 //===-- Disassembler.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/Disassembler.h"
17 // Other libraries and framework includes
19 #include "lldb/Core/DataBufferHeap.h"
20 #include "lldb/Core/DataExtractor.h"
21 #include "lldb/Core/Debugger.h"
22 #include "lldb/Core/EmulateInstruction.h"
23 #include "lldb/Core/Error.h"
24 #include "lldb/Core/Module.h"
25 #include "lldb/Core/PluginManager.h"
26 #include "lldb/Core/RegularExpression.h"
27 #include "lldb/Core/Timer.h"
28 #include "lldb/Host/FileSystem.h"
29 #include "lldb/Interpreter/OptionValue.h"
30 #include "lldb/Interpreter/OptionValueArray.h"
31 #include "lldb/Interpreter/OptionValueDictionary.h"
32 #include "lldb/Interpreter/OptionValueRegex.h"
33 #include "lldb/Interpreter/OptionValueString.h"
34 #include "lldb/Interpreter/OptionValueUInt64.h"
35 #include "lldb/Symbol/Function.h"
36 #include "lldb/Symbol/ObjectFile.h"
37 #include "lldb/Target/ExecutionContext.h"
38 #include "lldb/Target/Process.h"
39 #include "lldb/Target/SectionLoadList.h"
40 #include "lldb/Target/StackFrame.h"
41 #include "lldb/Target/Target.h"
42 #include "lldb/lldb-private.h"
44 #define DEFAULT_DISASM_BYTE_SIZE 32
47 using namespace lldb_private;
49 DisassemblerSP Disassembler::FindPlugin(const ArchSpec &arch,
51 const char *plugin_name) {
52 Timer scoped_timer(LLVM_PRETTY_FUNCTION,
53 "Disassembler::FindPlugin (arch = %s, plugin_name = %s)",
54 arch.GetArchitectureName(), plugin_name);
56 DisassemblerCreateInstance create_callback = nullptr;
59 ConstString const_plugin_name(plugin_name);
60 create_callback = PluginManager::GetDisassemblerCreateCallbackForPluginName(
62 if (create_callback) {
63 DisassemblerSP disassembler_sp(create_callback(arch, flavor));
66 return disassembler_sp;
69 for (uint32_t idx = 0;
70 (create_callback = PluginManager::GetDisassemblerCreateCallbackAtIndex(
73 DisassemblerSP disassembler_sp(create_callback(arch, flavor));
76 return disassembler_sp;
79 return DisassemblerSP();
82 DisassemblerSP Disassembler::FindPluginForTarget(const TargetSP target_sp,
85 const char *plugin_name) {
86 if (target_sp && flavor == nullptr) {
87 // FIXME - we don't have the mechanism in place to do per-architecture
88 // settings. But since we know that for now
89 // we only support flavors on x86 & x86_64,
90 if (arch.GetTriple().getArch() == llvm::Triple::x86 ||
91 arch.GetTriple().getArch() == llvm::Triple::x86_64)
92 flavor = target_sp->GetDisassemblyFlavor();
94 return FindPlugin(arch, flavor, plugin_name);
97 static void ResolveAddress(const ExecutionContext &exe_ctx, const Address &addr,
98 Address &resolved_addr) {
99 if (!addr.IsSectionOffset()) {
100 // If we weren't passed in a section offset address range,
101 // try and resolve it to something
102 Target *target = exe_ctx.GetTargetPtr();
104 if (target->GetSectionLoadList().IsEmpty()) {
105 target->GetImages().ResolveFileAddress(addr.GetOffset(), resolved_addr);
107 target->GetSectionLoadList().ResolveLoadAddress(addr.GetOffset(),
110 // We weren't able to resolve the address, just treat it as a
112 if (resolved_addr.IsValid())
116 resolved_addr = addr;
119 size_t Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
120 const char *plugin_name, const char *flavor,
121 const ExecutionContext &exe_ctx,
122 SymbolContextList &sc_list,
123 uint32_t num_instructions,
124 bool mixed_source_and_assembly,
125 uint32_t num_mixed_context_lines,
126 uint32_t options, Stream &strm) {
127 size_t success_count = 0;
128 const size_t count = sc_list.GetSize();
131 const uint32_t scope =
132 eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
133 const bool use_inline_block_range = true;
134 for (size_t i = 0; i < count; ++i) {
135 if (!sc_list.GetContextAtIndex(i, sc))
137 for (uint32_t range_idx = 0;
138 sc.GetAddressRange(scope, range_idx, use_inline_block_range, range);
140 if (Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, range,
141 num_instructions, mixed_source_and_assembly,
142 num_mixed_context_lines, options, strm)) {
148 return success_count;
151 bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
152 const char *plugin_name, const char *flavor,
153 const ExecutionContext &exe_ctx,
154 const ConstString &name, Module *module,
155 uint32_t num_instructions,
156 bool mixed_source_and_assembly,
157 uint32_t num_mixed_context_lines,
158 uint32_t options, Stream &strm) {
159 SymbolContextList sc_list;
161 const bool include_symbols = true;
162 const bool include_inlines = true;
164 module->FindFunctions(name, nullptr, eFunctionNameTypeAuto,
165 include_symbols, include_inlines, true, sc_list);
166 } else if (exe_ctx.GetTargetPtr()) {
167 exe_ctx.GetTargetPtr()->GetImages().FindFunctions(
168 name, eFunctionNameTypeAuto, include_symbols, include_inlines, false,
173 if (sc_list.GetSize()) {
174 return Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, sc_list,
175 num_instructions, mixed_source_and_assembly,
176 num_mixed_context_lines, options, strm);
181 lldb::DisassemblerSP Disassembler::DisassembleRange(
182 const ArchSpec &arch, const char *plugin_name, const char *flavor,
183 const ExecutionContext &exe_ctx, const AddressRange &range,
184 bool prefer_file_cache) {
185 lldb::DisassemblerSP disasm_sp;
186 if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid()) {
187 disasm_sp = Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch,
188 flavor, plugin_name);
191 size_t bytes_disassembled = disasm_sp->ParseInstructions(
192 &exe_ctx, range, nullptr, prefer_file_cache);
193 if (bytes_disassembled == 0)
201 Disassembler::DisassembleBytes(const ArchSpec &arch, const char *plugin_name,
202 const char *flavor, const Address &start,
203 const void *src, size_t src_len,
204 uint32_t num_instructions, bool data_from_file) {
205 lldb::DisassemblerSP disasm_sp;
208 disasm_sp = Disassembler::FindPlugin(arch, flavor, plugin_name);
211 DataExtractor data(src, src_len, arch.GetByteOrder(),
212 arch.GetAddressByteSize());
214 (void)disasm_sp->DecodeInstructions(start, data, 0, num_instructions,
215 false, data_from_file);
222 bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
223 const char *plugin_name, const char *flavor,
224 const ExecutionContext &exe_ctx,
225 const AddressRange &disasm_range,
226 uint32_t num_instructions,
227 bool mixed_source_and_assembly,
228 uint32_t num_mixed_context_lines,
229 uint32_t options, Stream &strm) {
230 if (disasm_range.GetByteSize()) {
231 lldb::DisassemblerSP disasm_sp(Disassembler::FindPluginForTarget(
232 exe_ctx.GetTargetSP(), arch, flavor, plugin_name));
236 ResolveAddress(exe_ctx, disasm_range.GetBaseAddress(),
237 range.GetBaseAddress());
238 range.SetByteSize(disasm_range.GetByteSize());
239 const bool prefer_file_cache = false;
240 size_t bytes_disassembled = disasm_sp->ParseInstructions(
241 &exe_ctx, range, &strm, prefer_file_cache);
242 if (bytes_disassembled == 0)
245 return PrintInstructions(disasm_sp.get(), debugger, arch, exe_ctx,
246 num_instructions, mixed_source_and_assembly,
247 num_mixed_context_lines, options, strm);
253 bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
254 const char *plugin_name, const char *flavor,
255 const ExecutionContext &exe_ctx,
256 const Address &start_address,
257 uint32_t num_instructions,
258 bool mixed_source_and_assembly,
259 uint32_t num_mixed_context_lines,
260 uint32_t options, Stream &strm) {
261 if (num_instructions > 0) {
262 lldb::DisassemblerSP disasm_sp(Disassembler::FindPluginForTarget(
263 exe_ctx.GetTargetSP(), arch, flavor, plugin_name));
266 ResolveAddress(exe_ctx, start_address, addr);
267 const bool prefer_file_cache = false;
268 size_t bytes_disassembled = disasm_sp->ParseInstructions(
269 &exe_ctx, addr, num_instructions, prefer_file_cache);
270 if (bytes_disassembled == 0)
272 return PrintInstructions(disasm_sp.get(), debugger, arch, exe_ctx,
273 num_instructions, mixed_source_and_assembly,
274 num_mixed_context_lines, options, strm);
280 Disassembler::SourceLine
281 Disassembler::GetFunctionDeclLineEntry(const SymbolContext &sc) {
282 SourceLine decl_line;
283 if (sc.function && sc.line_entry.IsValid()) {
284 LineEntry prologue_end_line = sc.line_entry;
285 FileSpec func_decl_file;
286 uint32_t func_decl_line;
287 sc.function->GetStartLineSourceInfo(func_decl_file, func_decl_line);
288 if (func_decl_file == prologue_end_line.file ||
289 func_decl_file == prologue_end_line.original_file) {
290 decl_line.file = func_decl_file;
291 decl_line.line = func_decl_line;
292 // TODO do we care about column on these entries? If so, we need to
293 // plumb that through GetStartLineSourceInfo.
294 decl_line.column = 0;
300 void Disassembler::AddLineToSourceLineTables(
302 std::map<FileSpec, std::set<uint32_t>> &source_lines_seen) {
303 if (line.IsValid()) {
304 auto source_lines_seen_pos = source_lines_seen.find(line.file);
305 if (source_lines_seen_pos == source_lines_seen.end()) {
306 std::set<uint32_t> lines;
307 lines.insert(line.line);
308 source_lines_seen.emplace(line.file, lines);
310 source_lines_seen_pos->second.insert(line.line);
315 bool Disassembler::ElideMixedSourceAndDisassemblyLine(
316 const ExecutionContext &exe_ctx, const SymbolContext &sc,
319 // TODO: should we also check target.process.thread.step-avoid-libraries ?
321 const RegularExpression *avoid_regex = nullptr;
323 // Skip any line #0 entries - they are implementation details
327 ThreadSP thread_sp = exe_ctx.GetThreadSP();
329 avoid_regex = thread_sp->GetSymbolsToAvoidRegexp();
331 TargetSP target_sp = exe_ctx.GetTargetSP();
334 OptionValueSP value_sp = target_sp->GetDebugger().GetPropertyValue(
335 &exe_ctx, "target.process.thread.step-avoid-regexp", false, error);
336 if (value_sp && value_sp->GetType() == OptionValue::eTypeRegex) {
337 OptionValueRegex *re = value_sp->GetAsRegex();
339 avoid_regex = re->GetCurrentValue();
344 if (avoid_regex && sc.symbol != nullptr) {
345 const char *function_name =
346 sc.GetFunctionName(Mangled::ePreferDemangledWithoutArguments)
349 RegularExpression::Match regex_match(1);
350 if (avoid_regex->Execute(function_name, ®ex_match)) {
351 // skip this source line
356 // don't skip this source line
360 bool Disassembler::PrintInstructions(Disassembler *disasm_ptr,
361 Debugger &debugger, const ArchSpec &arch,
362 const ExecutionContext &exe_ctx,
363 uint32_t num_instructions,
364 bool mixed_source_and_assembly,
365 uint32_t num_mixed_context_lines,
366 uint32_t options, Stream &strm) {
367 // We got some things disassembled...
368 size_t num_instructions_found = disasm_ptr->GetInstructionList().GetSize();
370 if (num_instructions > 0 && num_instructions < num_instructions_found)
371 num_instructions_found = num_instructions;
373 const uint32_t max_opcode_byte_size =
374 disasm_ptr->GetInstructionList().GetMaxOpcocdeByteSize();
376 SymbolContext prev_sc;
377 AddressRange current_source_line_range;
378 const Address *pc_addr_ptr = nullptr;
379 StackFrame *frame = exe_ctx.GetFramePtr();
381 TargetSP target_sp(exe_ctx.GetTargetSP());
382 SourceManager &source_manager =
383 target_sp ? target_sp->GetSourceManager() : debugger.GetSourceManager();
386 pc_addr_ptr = &frame->GetFrameCodeAddress();
388 const uint32_t scope =
389 eSymbolContextLineEntry | eSymbolContextFunction | eSymbolContextSymbol;
390 const bool use_inline_block_range = false;
392 const FormatEntity::Entry *disassembly_format = nullptr;
393 FormatEntity::Entry format;
394 if (exe_ctx.HasTargetScope()) {
396 exe_ctx.GetTargetRef().GetDebugger().GetDisassemblyFormat();
398 FormatEntity::Parse("${addr}: ", format);
399 disassembly_format = &format;
402 // First pass: step through the list of instructions,
403 // find how long the initial addresses strings are, insert padding
404 // in the second pass so the opcodes all line up nicely.
406 // Also build up the source line mapping if this is mixed source & assembly
408 // Calculate the source line for each assembly instruction (eliding inlined
410 // which the user wants to skip).
412 std::map<FileSpec, std::set<uint32_t>> source_lines_seen;
413 Symbol *previous_symbol = nullptr;
415 size_t address_text_size = 0;
416 for (size_t i = 0; i < num_instructions_found; ++i) {
418 disasm_ptr->GetInstructionList().GetInstructionAtIndex(i).get();
420 const Address &addr = inst->GetAddress();
421 ModuleSP module_sp(addr.GetModule());
423 const uint32_t resolve_mask = eSymbolContextFunction |
424 eSymbolContextSymbol |
425 eSymbolContextLineEntry;
426 uint32_t resolved_mask =
427 module_sp->ResolveSymbolContextForAddress(addr, resolve_mask, sc);
429 StreamString strmstr;
430 Debugger::FormatDisassemblerAddress(disassembly_format, &sc, nullptr,
431 &exe_ctx, &addr, strmstr);
432 size_t cur_line = strmstr.GetSizeOfLastLine();
433 if (cur_line > address_text_size)
434 address_text_size = cur_line;
436 // Add entries to our "source_lines_seen" map+set which list which
437 // sources lines occur in this disassembly session. We will print
438 // lines of context around a source line, but we don't want to print
439 // a source line that has a line table entry of its own - we'll leave
440 // that source line to be printed when it actually occurs in the
443 if (mixed_source_and_assembly && sc.line_entry.IsValid()) {
444 if (sc.symbol != previous_symbol) {
445 SourceLine decl_line = GetFunctionDeclLineEntry(sc);
446 if (ElideMixedSourceAndDisassemblyLine(exe_ctx, sc, decl_line) ==
448 AddLineToSourceLineTables(decl_line, source_lines_seen);
450 if (sc.line_entry.IsValid()) {
451 SourceLine this_line;
452 this_line.file = sc.line_entry.file;
453 this_line.line = sc.line_entry.line;
454 this_line.column = sc.line_entry.column;
455 if (ElideMixedSourceAndDisassemblyLine(exe_ctx, sc, this_line) ==
457 AddLineToSourceLineTables(this_line, source_lines_seen);
466 previous_symbol = nullptr;
467 SourceLine previous_line;
468 for (size_t i = 0; i < num_instructions_found; ++i) {
470 disasm_ptr->GetInstructionList().GetInstructionAtIndex(i).get();
473 const Address &addr = inst->GetAddress();
474 const bool inst_is_at_pc = pc_addr_ptr && addr == *pc_addr_ptr;
475 SourceLinesToDisplay source_lines_to_display;
479 ModuleSP module_sp(addr.GetModule());
481 uint32_t resolved_mask = module_sp->ResolveSymbolContextForAddress(
482 addr, eSymbolContextEverything, sc);
484 if (mixed_source_and_assembly) {
486 // If we've started a new function (non-inlined), print all of the
487 // source lines from the
488 // function declaration until the first line table entry - typically
489 // the opening curly brace of
491 if (previous_symbol != sc.symbol) {
492 // The default disassembly format puts an extra blank line between
494 // when we're displaying the source context for a function, we
496 // a blank line after the source context or we'll end up with two
498 if (previous_symbol != nullptr)
499 source_lines_to_display.print_source_context_end_eol = false;
501 previous_symbol = sc.symbol;
502 if (sc.function && sc.line_entry.IsValid()) {
503 LineEntry prologue_end_line = sc.line_entry;
504 if (ElideMixedSourceAndDisassemblyLine(
505 exe_ctx, sc, prologue_end_line) == false) {
506 FileSpec func_decl_file;
507 uint32_t func_decl_line;
508 sc.function->GetStartLineSourceInfo(func_decl_file,
510 if (func_decl_file == prologue_end_line.file ||
511 func_decl_file == prologue_end_line.original_file) {
512 // Add all the lines between the function declaration
513 // and the first non-prologue source line to the list
514 // of lines to print.
515 for (uint32_t lineno = func_decl_line;
516 lineno <= prologue_end_line.line; lineno++) {
517 SourceLine this_line;
518 this_line.file = func_decl_file;
519 this_line.line = lineno;
520 source_lines_to_display.lines.push_back(this_line);
522 // Mark the last line as the "current" one. Usually
523 // this is the open curly brace.
524 if (source_lines_to_display.lines.size() > 0)
525 source_lines_to_display.current_source_line =
526 source_lines_to_display.lines.size() - 1;
530 sc.GetAddressRange(scope, 0, use_inline_block_range,
531 current_source_line_range);
534 // If we've left a previous source line's address range, print a new
536 if (!current_source_line_range.ContainsFileAddress(addr)) {
537 sc.GetAddressRange(scope, 0, use_inline_block_range,
538 current_source_line_range);
540 if (sc != prev_sc && sc.comp_unit && sc.line_entry.IsValid()) {
541 SourceLine this_line;
542 this_line.file = sc.line_entry.file;
543 this_line.line = sc.line_entry.line;
545 if (ElideMixedSourceAndDisassemblyLine(exe_ctx, sc,
546 this_line) == false) {
547 // Only print this source line if it is different from the
548 // last source line we printed. There may have been inlined
549 // functions between these lines that we elided, resulting in
550 // the same line being printed twice in a row for a contiguous
551 // block of assembly instructions.
552 if (this_line != previous_line) {
554 std::vector<uint32_t> previous_lines;
556 i < num_mixed_context_lines &&
557 (this_line.line - num_mixed_context_lines) > 0;
560 this_line.line - num_mixed_context_lines + i;
561 auto pos = source_lines_seen.find(this_line.file);
562 if (pos != source_lines_seen.end()) {
563 if (pos->second.count(line) == 1) {
564 previous_lines.clear();
566 previous_lines.push_back(line);
570 for (size_t i = 0; i < previous_lines.size(); i++) {
571 SourceLine previous_line;
572 previous_line.file = this_line.file;
573 previous_line.line = previous_lines[i];
574 auto pos = source_lines_seen.find(previous_line.file);
575 if (pos != source_lines_seen.end()) {
576 pos->second.insert(previous_line.line);
578 source_lines_to_display.lines.push_back(previous_line);
581 source_lines_to_display.lines.push_back(this_line);
582 source_lines_to_display.current_source_line =
583 source_lines_to_display.lines.size() - 1;
585 for (uint32_t i = 0; i < num_mixed_context_lines; i++) {
586 SourceLine next_line;
587 next_line.file = this_line.file;
588 next_line.line = this_line.line + i + 1;
589 auto pos = source_lines_seen.find(next_line.file);
590 if (pos != source_lines_seen.end()) {
591 if (pos->second.count(next_line.line) == 1)
593 pos->second.insert(next_line.line);
595 source_lines_to_display.lines.push_back(next_line);
598 previous_line = this_line;
608 if (source_lines_to_display.lines.size() > 0) {
610 for (size_t idx = 0; idx < source_lines_to_display.lines.size();
612 SourceLine ln = source_lines_to_display.lines[idx];
613 const char *line_highlight = "";
614 if (inst_is_at_pc && (options & eOptionMarkPCSourceLine)) {
615 line_highlight = "->";
616 } else if (idx == source_lines_to_display.current_source_line) {
617 line_highlight = "**";
619 source_manager.DisplaySourceLinesWithLineNumbers(
620 ln.file, ln.line, ln.column, 0, 0, line_highlight, &strm);
622 if (source_lines_to_display.print_source_context_end_eol)
626 const bool show_bytes = (options & eOptionShowBytes) != 0;
627 inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx, &sc,
628 &prev_sc, nullptr, address_text_size);
638 bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
639 const char *plugin_name, const char *flavor,
640 const ExecutionContext &exe_ctx,
641 uint32_t num_instructions,
642 bool mixed_source_and_assembly,
643 uint32_t num_mixed_context_lines,
644 uint32_t options, Stream &strm) {
646 StackFrame *frame = exe_ctx.GetFramePtr();
649 frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
651 range = sc.function->GetAddressRange();
652 } else if (sc.symbol && sc.symbol->ValueIsAddress()) {
653 range.GetBaseAddress() = sc.symbol->GetAddressRef();
654 range.SetByteSize(sc.symbol->GetByteSize());
656 range.GetBaseAddress() = frame->GetFrameCodeAddress();
659 if (range.GetBaseAddress().IsValid() && range.GetByteSize() == 0)
660 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
663 return Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, range,
664 num_instructions, mixed_source_and_assembly,
665 num_mixed_context_lines, options, strm);
668 Instruction::Instruction(const Address &address, AddressClass addr_class)
669 : m_address(address), m_address_class(addr_class), m_opcode(),
670 m_calculated_strings(false) {}
672 Instruction::~Instruction() = default;
674 AddressClass Instruction::GetAddressClass() {
675 if (m_address_class == eAddressClassInvalid)
676 m_address_class = m_address.GetAddressClass();
677 return m_address_class;
680 void Instruction::Dump(lldb_private::Stream *s, uint32_t max_opcode_byte_size,
681 bool show_address, bool show_bytes,
682 const ExecutionContext *exe_ctx,
683 const SymbolContext *sym_ctx,
684 const SymbolContext *prev_sym_ctx,
685 const FormatEntity::Entry *disassembly_addr_format,
686 size_t max_address_text_size) {
687 size_t opcode_column_width = 7;
688 const size_t operand_column_width = 25;
690 CalculateMnemonicOperandsAndCommentIfNeeded(exe_ctx);
695 Debugger::FormatDisassemblerAddress(disassembly_addr_format, sym_ctx,
696 prev_sym_ctx, exe_ctx, &m_address, ss);
697 ss.FillLastLineToColumn(max_address_text_size, ' ');
701 if (m_opcode.GetType() == Opcode::eTypeBytes) {
702 // x86_64 and i386 are the only ones that use bytes right now so
703 // pad out the byte dump to be able to always show 15 bytes (3 chars each)
705 if (max_opcode_byte_size > 0)
706 m_opcode.Dump(&ss, max_opcode_byte_size * 3 + 1);
708 m_opcode.Dump(&ss, 15 * 3 + 1);
710 // Else, we have ARM or MIPS which can show up to a uint32_t
711 // 0x00000000 (10 spaces) plus two for padding...
712 if (max_opcode_byte_size > 0)
713 m_opcode.Dump(&ss, max_opcode_byte_size * 3 + 1);
715 m_opcode.Dump(&ss, 12);
719 const size_t opcode_pos = ss.GetSizeOfLastLine();
721 // The default opcode size of 7 characters is plenty for most architectures
722 // but some like arm can pull out the occasional vqrshrun.s16. We won't get
723 // consistent column spacing in these cases, unfortunately.
724 if (m_opcode_name.length() >= opcode_column_width) {
725 opcode_column_width = m_opcode_name.length() + 1;
728 ss.PutCString(m_opcode_name);
729 ss.FillLastLineToColumn(opcode_pos + opcode_column_width, ' ');
730 ss.PutCString(m_mnemonics);
732 if (!m_comment.empty()) {
733 ss.FillLastLineToColumn(
734 opcode_pos + opcode_column_width + operand_column_width, ' ');
735 ss.PutCString(" ; ");
736 ss.PutCString(m_comment);
738 s->PutCString(ss.GetString());
741 bool Instruction::DumpEmulation(const ArchSpec &arch) {
742 std::unique_ptr<EmulateInstruction> insn_emulator_ap(
743 EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr));
744 if (insn_emulator_ap) {
745 insn_emulator_ap->SetInstruction(GetOpcode(), GetAddress(), nullptr);
746 return insn_emulator_ap->EvaluateInstruction(0);
752 bool Instruction::HasDelaySlot() {
757 OptionValueSP Instruction::ReadArray(FILE *in_file, Stream *out_stream,
758 OptionValue::Type data_type) {
762 OptionValueSP option_value_sp(new OptionValueArray(1u << data_type));
766 if (!fgets(buffer, 1023, in_file)) {
768 "Instruction::ReadArray: Error reading file (fgets).\n");
769 option_value_sp.reset();
770 return option_value_sp;
773 std::string line(buffer);
775 size_t len = line.size();
776 if (line[len - 1] == '\n') {
777 line[len - 1] = '\0';
778 line.resize(len - 1);
781 if ((line.size() == 1) && line[0] == ']') {
788 static RegularExpression g_reg_exp(
789 llvm::StringRef("^[ \t]*([^ \t]+)[ \t]*$"));
790 RegularExpression::Match regex_match(1);
791 bool reg_exp_success = g_reg_exp.Execute(line, ®ex_match);
793 regex_match.GetMatchAtIndex(line.c_str(), 1, value);
797 OptionValueSP data_value_sp;
799 case OptionValue::eTypeUInt64:
800 data_value_sp.reset(new OptionValueUInt64(0, 0));
801 data_value_sp->SetValueFromString(value);
803 // Other types can be added later as needed.
805 data_value_sp.reset(new OptionValueString(value.c_str(), ""));
809 option_value_sp->GetAsArray()->InsertValue(idx, data_value_sp);
814 return option_value_sp;
817 OptionValueSP Instruction::ReadDictionary(FILE *in_file, Stream *out_stream) {
821 OptionValueSP option_value_sp(new OptionValueDictionary());
822 static ConstString encoding_key("data_encoding");
823 OptionValue::Type data_type = OptionValue::eTypeInvalid;
826 // Read the next line in the file
827 if (!fgets(buffer, 1023, in_file)) {
829 "Instruction::ReadDictionary: Error reading file (fgets).\n");
830 option_value_sp.reset();
831 return option_value_sp;
834 // Check to see if the line contains the end-of-dictionary marker ("}")
835 std::string line(buffer);
837 size_t len = line.size();
838 if (line[len - 1] == '\n') {
839 line[len - 1] = '\0';
840 line.resize(len - 1);
843 if ((line.size() == 1) && (line[0] == '}')) {
848 // Try to find a key-value pair in the current line and add it to the
851 static RegularExpression g_reg_exp(llvm::StringRef(
852 "^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*=[ \t]*(.*)[ \t]*$"));
853 RegularExpression::Match regex_match(2);
855 bool reg_exp_success = g_reg_exp.Execute(line, ®ex_match);
858 if (reg_exp_success) {
859 regex_match.GetMatchAtIndex(line.c_str(), 1, key);
860 regex_match.GetMatchAtIndex(line.c_str(), 2, value);
862 out_stream->Printf("Instruction::ReadDictionary: Failure executing "
863 "regular expression.\n");
864 option_value_sp.reset();
865 return option_value_sp;
868 ConstString const_key(key.c_str());
869 // Check value to see if it's the start of an array or dictionary.
871 lldb::OptionValueSP value_sp;
872 assert(value.empty() == false);
873 assert(key.empty() == false);
875 if (value[0] == '{') {
876 assert(value.size() == 1);
877 // value is a dictionary
878 value_sp = ReadDictionary(in_file, out_stream);
880 option_value_sp.reset();
881 return option_value_sp;
883 } else if (value[0] == '[') {
884 assert(value.size() == 1);
886 value_sp = ReadArray(in_file, out_stream, data_type);
888 option_value_sp.reset();
889 return option_value_sp;
891 // We've used the data_type to read an array; re-set the type to Invalid
892 data_type = OptionValue::eTypeInvalid;
893 } else if ((value[0] == '0') && (value[1] == 'x')) {
894 value_sp.reset(new OptionValueUInt64(0, 0));
895 value_sp->SetValueFromString(value);
897 size_t len = value.size();
898 if ((value[0] == '"') && (value[len - 1] == '"'))
899 value = value.substr(1, len - 2);
900 value_sp.reset(new OptionValueString(value.c_str(), ""));
903 if (const_key == encoding_key) {
904 // A 'data_encoding=..." is NOT a normal key-value pair; it is meta-data
906 // data type of an upcoming array (usually the next bit of data to be
908 if (strcmp(value.c_str(), "uint32_t") == 0)
909 data_type = OptionValue::eTypeUInt64;
911 option_value_sp->GetAsDictionary()->SetValueForKey(const_key, value_sp,
916 return option_value_sp;
919 bool Instruction::TestEmulation(Stream *out_stream, const char *file_name) {
924 out_stream->Printf("Instruction::TestEmulation: Missing file_name.");
927 FILE *test_file = FileSystem::Fopen(file_name, "r");
930 "Instruction::TestEmulation: Attempt to open test file failed.");
935 if (!fgets(buffer, 255, test_file)) {
937 "Instruction::TestEmulation: Error reading first line of test file.\n");
942 if (strncmp(buffer, "InstructionEmulationState={", 27) != 0) {
943 out_stream->Printf("Instructin::TestEmulation: Test file does not contain "
944 "emulation state dictionary\n");
949 // Read all the test information from the test file into an
950 // OptionValueDictionary.
952 OptionValueSP data_dictionary_sp(ReadDictionary(test_file, out_stream));
953 if (!data_dictionary_sp) {
955 "Instruction::TestEmulation: Error reading Dictionary Object.\n");
962 OptionValueDictionary *data_dictionary =
963 data_dictionary_sp->GetAsDictionary();
964 static ConstString description_key("assembly_string");
965 static ConstString triple_key("triple");
967 OptionValueSP value_sp = data_dictionary->GetValueForKey(description_key);
970 out_stream->Printf("Instruction::TestEmulation: Test file does not "
971 "contain description string.\n");
975 SetDescription(value_sp->GetStringValue());
977 value_sp = data_dictionary->GetValueForKey(triple_key);
980 "Instruction::TestEmulation: Test file does not contain triple.\n");
985 arch.SetTriple(llvm::Triple(value_sp->GetStringValue()));
987 bool success = false;
988 std::unique_ptr<EmulateInstruction> insn_emulator_ap(
989 EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr));
990 if (insn_emulator_ap)
992 insn_emulator_ap->TestEmulation(out_stream, arch, data_dictionary);
995 out_stream->Printf("Emulation test succeeded.");
997 out_stream->Printf("Emulation test failed.");
1002 bool Instruction::Emulate(
1003 const ArchSpec &arch, uint32_t evaluate_options, void *baton,
1004 EmulateInstruction::ReadMemoryCallback read_mem_callback,
1005 EmulateInstruction::WriteMemoryCallback write_mem_callback,
1006 EmulateInstruction::ReadRegisterCallback read_reg_callback,
1007 EmulateInstruction::WriteRegisterCallback write_reg_callback) {
1008 std::unique_ptr<EmulateInstruction> insn_emulator_ap(
1009 EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr));
1010 if (insn_emulator_ap) {
1011 insn_emulator_ap->SetBaton(baton);
1012 insn_emulator_ap->SetCallbacks(read_mem_callback, write_mem_callback,
1013 read_reg_callback, write_reg_callback);
1014 insn_emulator_ap->SetInstruction(GetOpcode(), GetAddress(), nullptr);
1015 return insn_emulator_ap->EvaluateInstruction(evaluate_options);
1021 uint32_t Instruction::GetData(DataExtractor &data) {
1022 return m_opcode.GetData(data);
1025 InstructionList::InstructionList() : m_instructions() {}
1027 InstructionList::~InstructionList() = default;
1029 size_t InstructionList::GetSize() const { return m_instructions.size(); }
1031 uint32_t InstructionList::GetMaxOpcocdeByteSize() const {
1032 uint32_t max_inst_size = 0;
1033 collection::const_iterator pos, end;
1034 for (pos = m_instructions.begin(), end = m_instructions.end(); pos != end;
1036 uint32_t inst_size = (*pos)->GetOpcode().GetByteSize();
1037 if (max_inst_size < inst_size)
1038 max_inst_size = inst_size;
1040 return max_inst_size;
1043 InstructionSP InstructionList::GetInstructionAtIndex(size_t idx) const {
1044 InstructionSP inst_sp;
1045 if (idx < m_instructions.size())
1046 inst_sp = m_instructions[idx];
1050 void InstructionList::Dump(Stream *s, bool show_address, bool show_bytes,
1051 const ExecutionContext *exe_ctx) {
1052 const uint32_t max_opcode_byte_size = GetMaxOpcocdeByteSize();
1053 collection::const_iterator pos, begin, end;
1055 const FormatEntity::Entry *disassembly_format = nullptr;
1056 FormatEntity::Entry format;
1057 if (exe_ctx && exe_ctx->HasTargetScope()) {
1058 disassembly_format =
1059 exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
1061 FormatEntity::Parse("${addr}: ", format);
1062 disassembly_format = &format;
1065 for (begin = m_instructions.begin(), end = m_instructions.end(), pos = begin;
1066 pos != end; ++pos) {
1069 (*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx,
1070 nullptr, nullptr, disassembly_format, 0);
1074 void InstructionList::Clear() { m_instructions.clear(); }
1076 void InstructionList::Append(lldb::InstructionSP &inst_sp) {
1078 m_instructions.push_back(inst_sp);
1082 InstructionList::GetIndexOfNextBranchInstruction(uint32_t start,
1083 Target &target) const {
1084 size_t num_instructions = m_instructions.size();
1086 uint32_t next_branch = UINT32_MAX;
1088 for (i = start; i < num_instructions; i++) {
1089 if (m_instructions[i]->DoesBranch()) {
1095 // Hexagon needs the first instruction of the packet with the branch.
1096 // Go backwards until we find an instruction marked end-of-packet, or
1097 // until we hit start.
1098 if (target.GetArchitecture().GetTriple().getArch() == llvm::Triple::hexagon) {
1099 // If we didn't find a branch, find the last packet start.
1100 if (next_branch == UINT32_MAX) {
1101 i = num_instructions - 1;
1108 uint32_t inst_bytes;
1109 bool prefer_file_cache = false; // Read from process if process is running
1110 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1111 target.ReadMemory(m_instructions[i]->GetAddress(), prefer_file_cache,
1112 &inst_bytes, sizeof(inst_bytes), error, &load_addr);
1113 // If we have an error reading memory, return start
1114 if (!error.Success())
1116 // check if this is the last instruction in a packet
1117 // bits 15:14 will be 11b or 00b for a duplex
1118 if (((inst_bytes & 0xC000) == 0xC000) ||
1119 ((inst_bytes & 0xC000) == 0x0000)) {
1120 // instruction after this should be the start of next packet
1121 next_branch = i + 1;
1126 if (next_branch == UINT32_MAX) {
1127 // We couldn't find the previous packet, so return start
1128 next_branch = start;
1135 InstructionList::GetIndexOfInstructionAtAddress(const Address &address) {
1136 size_t num_instructions = m_instructions.size();
1137 uint32_t index = UINT32_MAX;
1138 for (size_t i = 0; i < num_instructions; i++) {
1139 if (m_instructions[i]->GetAddress() == address) {
1148 InstructionList::GetIndexOfInstructionAtLoadAddress(lldb::addr_t load_addr,
1151 address.SetLoadAddress(load_addr, &target);
1152 return GetIndexOfInstructionAtAddress(address);
1155 size_t Disassembler::ParseInstructions(const ExecutionContext *exe_ctx,
1156 const AddressRange &range,
1157 Stream *error_strm_ptr,
1158 bool prefer_file_cache) {
1160 Target *target = exe_ctx->GetTargetPtr();
1161 const addr_t byte_size = range.GetByteSize();
1162 if (target == nullptr || byte_size == 0 ||
1163 !range.GetBaseAddress().IsValid())
1166 DataBufferHeap *heap_buffer = new DataBufferHeap(byte_size, '\0');
1167 DataBufferSP data_sp(heap_buffer);
1170 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1171 const size_t bytes_read = target->ReadMemory(
1172 range.GetBaseAddress(), prefer_file_cache, heap_buffer->GetBytes(),
1173 heap_buffer->GetByteSize(), error, &load_addr);
1175 if (bytes_read > 0) {
1176 if (bytes_read != heap_buffer->GetByteSize())
1177 heap_buffer->SetByteSize(bytes_read);
1178 DataExtractor data(data_sp, m_arch.GetByteOrder(),
1179 m_arch.GetAddressByteSize());
1180 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1181 return DecodeInstructions(range.GetBaseAddress(), data, 0, UINT32_MAX,
1182 false, data_from_file);
1183 } else if (error_strm_ptr) {
1184 const char *error_cstr = error.AsCString();
1186 error_strm_ptr->Printf("error: %s\n", error_cstr);
1189 } else if (error_strm_ptr) {
1190 error_strm_ptr->PutCString("error: invalid execution context\n");
1195 size_t Disassembler::ParseInstructions(const ExecutionContext *exe_ctx,
1196 const Address &start,
1197 uint32_t num_instructions,
1198 bool prefer_file_cache) {
1199 m_instruction_list.Clear();
1201 if (exe_ctx == nullptr || num_instructions == 0 || !start.IsValid())
1204 Target *target = exe_ctx->GetTargetPtr();
1205 // Calculate the max buffer size we will need in order to disassemble
1206 const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize();
1208 if (target == nullptr || byte_size == 0)
1211 DataBufferHeap *heap_buffer = new DataBufferHeap(byte_size, '\0');
1212 DataBufferSP data_sp(heap_buffer);
1215 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1216 const size_t bytes_read =
1217 target->ReadMemory(start, prefer_file_cache, heap_buffer->GetBytes(),
1218 byte_size, error, &load_addr);
1220 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1222 if (bytes_read == 0)
1224 DataExtractor data(data_sp, m_arch.GetByteOrder(),
1225 m_arch.GetAddressByteSize());
1227 const bool append_instructions = true;
1228 DecodeInstructions(start, data, 0, num_instructions, append_instructions,
1231 return m_instruction_list.GetSize();
1234 //----------------------------------------------------------------------
1235 // Disassembler copy constructor
1236 //----------------------------------------------------------------------
1237 Disassembler::Disassembler(const ArchSpec &arch, const char *flavor)
1238 : m_arch(arch), m_instruction_list(), m_base_addr(LLDB_INVALID_ADDRESS),
1240 if (flavor == nullptr)
1241 m_flavor.assign("default");
1243 m_flavor.assign(flavor);
1245 // If this is an arm variant that can only include thumb (T16, T32)
1246 // instructions, force the arch triple to be "thumbv.." instead of
1248 if (arch.IsAlwaysThumbInstructions()) {
1249 std::string thumb_arch_name(arch.GetTriple().getArchName().str());
1250 // Replace "arm" with "thumb" so we get all thumb variants correct
1251 if (thumb_arch_name.size() > 3) {
1252 thumb_arch_name.erase(0, 3);
1253 thumb_arch_name.insert(0, "thumb");
1255 m_arch.SetTriple(thumb_arch_name.c_str());
1259 Disassembler::~Disassembler() = default;
1261 InstructionList &Disassembler::GetInstructionList() {
1262 return m_instruction_list;
1265 const InstructionList &Disassembler::GetInstructionList() const {
1266 return m_instruction_list;
1269 //----------------------------------------------------------------------
1270 // Class PseudoInstruction
1271 //----------------------------------------------------------------------
1273 PseudoInstruction::PseudoInstruction()
1274 : Instruction(Address(), eAddressClassUnknown), m_description() {}
1276 PseudoInstruction::~PseudoInstruction() = default;
1278 bool PseudoInstruction::DoesBranch() {
1279 // This is NOT a valid question for a pseudo instruction.
1283 bool PseudoInstruction::HasDelaySlot() {
1284 // This is NOT a valid question for a pseudo instruction.
1288 size_t PseudoInstruction::Decode(const lldb_private::Disassembler &disassembler,
1289 const lldb_private::DataExtractor &data,
1290 lldb::offset_t data_offset) {
1291 return m_opcode.GetByteSize();
1294 void PseudoInstruction::SetOpcode(size_t opcode_size, void *opcode_data) {
1298 switch (opcode_size) {
1300 uint8_t value8 = *((uint8_t *)opcode_data);
1301 m_opcode.SetOpcode8(value8, eByteOrderInvalid);
1305 uint16_t value16 = *((uint16_t *)opcode_data);
1306 m_opcode.SetOpcode16(value16, eByteOrderInvalid);
1310 uint32_t value32 = *((uint32_t *)opcode_data);
1311 m_opcode.SetOpcode32(value32, eByteOrderInvalid);
1315 uint64_t value64 = *((uint64_t *)opcode_data);
1316 m_opcode.SetOpcode64(value64, eByteOrderInvalid);
1324 void PseudoInstruction::SetDescription(llvm::StringRef description) {
1325 m_description = description;
1328 Instruction::Operand Instruction::Operand::BuildRegister(ConstString &r) {
1330 ret.m_type = Type::Register;
1335 Instruction::Operand Instruction::Operand::BuildImmediate(lldb::addr_t imm,
1338 ret.m_type = Type::Immediate;
1339 ret.m_immediate = imm;
1340 ret.m_negative = neg;
1344 Instruction::Operand Instruction::Operand::BuildImmediate(int64_t imm) {
1346 ret.m_type = Type::Immediate;
1348 ret.m_immediate = -imm;
1349 ret.m_negative = true;
1351 ret.m_immediate = imm;
1352 ret.m_negative = false;
1357 Instruction::Operand
1358 Instruction::Operand::BuildDereference(const Operand &ref) {
1360 ret.m_type = Type::Dereference;
1361 ret.m_children = {ref};
1365 Instruction::Operand Instruction::Operand::BuildSum(const Operand &lhs,
1366 const Operand &rhs) {
1368 ret.m_type = Type::Sum;
1369 ret.m_children = {lhs, rhs};
1373 Instruction::Operand Instruction::Operand::BuildProduct(const Operand &lhs,
1374 const Operand &rhs) {
1376 ret.m_type = Type::Product;
1377 ret.m_children = {lhs, rhs};
1381 std::function<bool(const Instruction::Operand &)>
1382 lldb_private::OperandMatchers::MatchBinaryOp(
1383 std::function<bool(const Instruction::Operand &)> base,
1384 std::function<bool(const Instruction::Operand &)> left,
1385 std::function<bool(const Instruction::Operand &)> right) {
1386 return [base, left, right](const Instruction::Operand &op) -> bool {
1387 return (base(op) && op.m_children.size() == 2 &&
1388 ((left(op.m_children[0]) && right(op.m_children[1])) ||
1389 (left(op.m_children[1]) && right(op.m_children[0]))));
1393 std::function<bool(const Instruction::Operand &)>
1394 lldb_private::OperandMatchers::MatchUnaryOp(
1395 std::function<bool(const Instruction::Operand &)> base,
1396 std::function<bool(const Instruction::Operand &)> child) {
1397 return [base, child](const Instruction::Operand &op) -> bool {
1398 return (base(op) && op.m_children.size() == 1 && child(op.m_children[0]));
1402 std::function<bool(const Instruction::Operand &)>
1403 lldb_private::OperandMatchers::MatchRegOp(const RegisterInfo &info) {
1404 return [&info](const Instruction::Operand &op) {
1405 return (op.m_type == Instruction::Operand::Type::Register &&
1406 (op.m_register == ConstString(info.name) ||
1407 op.m_register == ConstString(info.alt_name)));
1411 std::function<bool(const Instruction::Operand &)>
1412 lldb_private::OperandMatchers::FetchRegOp(ConstString ®) {
1413 return [®](const Instruction::Operand &op) {
1414 if (op.m_type != Instruction::Operand::Type::Register) {
1417 reg = op.m_register;
1422 std::function<bool(const Instruction::Operand &)>
1423 lldb_private::OperandMatchers::MatchImmOp(int64_t imm) {
1424 return [imm](const Instruction::Operand &op) {
1425 return (op.m_type == Instruction::Operand::Type::Immediate &&
1426 ((op.m_negative && op.m_immediate == (uint64_t)-imm) ||
1427 (!op.m_negative && op.m_immediate == (uint64_t)imm)));
1431 std::function<bool(const Instruction::Operand &)>
1432 lldb_private::OperandMatchers::FetchImmOp(int64_t &imm) {
1433 return [&imm](const Instruction::Operand &op) {
1434 if (op.m_type != Instruction::Operand::Type::Immediate) {
1437 if (op.m_negative) {
1438 imm = -((int64_t)op.m_immediate);
1440 imm = ((int64_t)op.m_immediate);
1446 std::function<bool(const Instruction::Operand &)>
1447 lldb_private::OperandMatchers::MatchOpType(Instruction::Operand::Type type) {
1448 return [type](const Instruction::Operand &op) { return op.m_type == type; };