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