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