1 //===-- Address.cpp -------------------------------------------------------===// 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/Address.h" 10 #include "lldb/Core/Debugger.h" 11 #include "lldb/Core/Declaration.h" 12 #include "lldb/Core/DumpDataExtractor.h" 13 #include "lldb/Core/Module.h" 14 #include "lldb/Core/ModuleList.h" 15 #include "lldb/Core/Section.h" 16 #include "lldb/Symbol/Block.h" 17 #include "lldb/Symbol/LineEntry.h" 18 #include "lldb/Symbol/ObjectFile.h" 19 #include "lldb/Symbol/Symbol.h" 20 #include "lldb/Symbol/SymbolContext.h" 21 #include "lldb/Symbol/SymbolVendor.h" 22 #include "lldb/Symbol/Symtab.h" 23 #include "lldb/Symbol/Type.h" 24 #include "lldb/Symbol/Variable.h" 25 #include "lldb/Symbol/VariableList.h" 26 #include "lldb/Target/ABI.h" 27 #include "lldb/Target/ExecutionContext.h" 28 #include "lldb/Target/ExecutionContextScope.h" 29 #include "lldb/Target/Process.h" 30 #include "lldb/Target/SectionLoadList.h" 31 #include "lldb/Target/Target.h" 32 #include "lldb/Utility/AnsiTerminal.h" 33 #include "lldb/Utility/ConstString.h" 34 #include "lldb/Utility/DataExtractor.h" 35 #include "lldb/Utility/Endian.h" 36 #include "lldb/Utility/FileSpec.h" 37 #include "lldb/Utility/Status.h" 38 #include "lldb/Utility/Stream.h" 39 #include "lldb/Utility/StreamString.h" 40 41 #include "llvm/ADT/StringRef.h" 42 #include "llvm/Support/Compiler.h" 43 #include "llvm/TargetParser/Triple.h" 44 45 #include <cstdint> 46 #include <memory> 47 #include <vector> 48 49 #include <cassert> 50 #include <cinttypes> 51 #include <cstring> 52 53 namespace lldb_private { 54 class CompileUnit; 55 } 56 namespace lldb_private { 57 class Function; 58 } 59 60 using namespace lldb; 61 using namespace lldb_private; 62 63 static size_t ReadBytes(ExecutionContextScope *exe_scope, 64 const Address &address, void *dst, size_t dst_len) { 65 if (exe_scope == nullptr) 66 return 0; 67 68 TargetSP target_sp(exe_scope->CalculateTarget()); 69 if (target_sp) { 70 Status error; 71 bool force_live_memory = true; 72 return target_sp->ReadMemory(address, dst, dst_len, error, 73 force_live_memory); 74 } 75 return 0; 76 } 77 78 static bool GetByteOrderAndAddressSize(ExecutionContextScope *exe_scope, 79 const Address &address, 80 ByteOrder &byte_order, 81 uint32_t &addr_size) { 82 byte_order = eByteOrderInvalid; 83 addr_size = 0; 84 if (exe_scope == nullptr) 85 return false; 86 87 TargetSP target_sp(exe_scope->CalculateTarget()); 88 if (target_sp) { 89 byte_order = target_sp->GetArchitecture().GetByteOrder(); 90 addr_size = target_sp->GetArchitecture().GetAddressByteSize(); 91 } 92 93 if (byte_order == eByteOrderInvalid || addr_size == 0) { 94 ModuleSP module_sp(address.GetModule()); 95 if (module_sp) { 96 byte_order = module_sp->GetArchitecture().GetByteOrder(); 97 addr_size = module_sp->GetArchitecture().GetAddressByteSize(); 98 } 99 } 100 return byte_order != eByteOrderInvalid && addr_size != 0; 101 } 102 103 static uint64_t ReadUIntMax64(ExecutionContextScope *exe_scope, 104 const Address &address, uint32_t byte_size, 105 bool &success) { 106 uint64_t uval64 = 0; 107 if (exe_scope == nullptr || byte_size > sizeof(uint64_t)) { 108 success = false; 109 return 0; 110 } 111 uint64_t buf = 0; 112 113 success = ReadBytes(exe_scope, address, &buf, byte_size) == byte_size; 114 if (success) { 115 ByteOrder byte_order = eByteOrderInvalid; 116 uint32_t addr_size = 0; 117 if (GetByteOrderAndAddressSize(exe_scope, address, byte_order, addr_size)) { 118 DataExtractor data(&buf, sizeof(buf), byte_order, addr_size); 119 lldb::offset_t offset = 0; 120 uval64 = data.GetU64(&offset); 121 } else 122 success = false; 123 } 124 return uval64; 125 } 126 127 static bool ReadAddress(ExecutionContextScope *exe_scope, 128 const Address &address, uint32_t pointer_size, 129 Address &deref_so_addr) { 130 if (exe_scope == nullptr) 131 return false; 132 133 bool success = false; 134 addr_t deref_addr = ReadUIntMax64(exe_scope, address, pointer_size, success); 135 if (success) { 136 ExecutionContext exe_ctx; 137 exe_scope->CalculateExecutionContext(exe_ctx); 138 // If we have any sections that are loaded, try and resolve using the 139 // section load list 140 Target *target = exe_ctx.GetTargetPtr(); 141 if (target && !target->GetSectionLoadList().IsEmpty()) { 142 if (target->GetSectionLoadList().ResolveLoadAddress(deref_addr, 143 deref_so_addr)) 144 return true; 145 } else { 146 // If we were not running, yet able to read an integer, we must have a 147 // module 148 ModuleSP module_sp(address.GetModule()); 149 150 assert(module_sp); 151 if (module_sp->ResolveFileAddress(deref_addr, deref_so_addr)) 152 return true; 153 } 154 155 // We couldn't make "deref_addr" into a section offset value, but we were 156 // able to read the address, so we return a section offset address with no 157 // section and "deref_addr" as the offset (address). 158 deref_so_addr.SetRawAddress(deref_addr); 159 return true; 160 } 161 return false; 162 } 163 164 static bool DumpUInt(ExecutionContextScope *exe_scope, const Address &address, 165 uint32_t byte_size, Stream *strm) { 166 if (exe_scope == nullptr || byte_size == 0) 167 return false; 168 std::vector<uint8_t> buf(byte_size, 0); 169 170 if (ReadBytes(exe_scope, address, &buf[0], buf.size()) == buf.size()) { 171 ByteOrder byte_order = eByteOrderInvalid; 172 uint32_t addr_size = 0; 173 if (GetByteOrderAndAddressSize(exe_scope, address, byte_order, addr_size)) { 174 DataExtractor data(&buf.front(), buf.size(), byte_order, addr_size); 175 176 DumpDataExtractor(data, strm, 177 0, // Start offset in "data" 178 eFormatHex, // Print as characters 179 buf.size(), // Size of item 180 1, // Items count 181 UINT32_MAX, // num per line 182 LLDB_INVALID_ADDRESS, // base address 183 0, // bitfield bit size 184 0); // bitfield bit offset 185 186 return true; 187 } 188 } 189 return false; 190 } 191 192 static size_t ReadCStringFromMemory(ExecutionContextScope *exe_scope, 193 const Address &address, Stream *strm) { 194 if (exe_scope == nullptr) 195 return 0; 196 const size_t k_buf_len = 256; 197 char buf[k_buf_len + 1]; 198 buf[k_buf_len] = '\0'; // NULL terminate 199 200 // Byte order and address size don't matter for C string dumping.. 201 DataExtractor data(buf, sizeof(buf), endian::InlHostByteOrder(), 4); 202 size_t total_len = 0; 203 size_t bytes_read; 204 Address curr_address(address); 205 strm->PutChar('"'); 206 while ((bytes_read = ReadBytes(exe_scope, curr_address, buf, k_buf_len)) > 207 0) { 208 size_t len = strlen(buf); 209 if (len == 0) 210 break; 211 if (len > bytes_read) 212 len = bytes_read; 213 214 DumpDataExtractor(data, strm, 215 0, // Start offset in "data" 216 eFormatChar, // Print as characters 217 1, // Size of item (1 byte for a char!) 218 len, // How many bytes to print? 219 UINT32_MAX, // num per line 220 LLDB_INVALID_ADDRESS, // base address 221 0, // bitfield bit size 222 223 0); // bitfield bit offset 224 225 total_len += bytes_read; 226 227 if (len < k_buf_len) 228 break; 229 curr_address.SetOffset(curr_address.GetOffset() + bytes_read); 230 } 231 strm->PutChar('"'); 232 return total_len; 233 } 234 235 Address::Address(lldb::addr_t abs_addr) : m_section_wp(), m_offset(abs_addr) {} 236 237 Address::Address(addr_t address, const SectionList *section_list) 238 : m_section_wp() { 239 ResolveAddressUsingFileSections(address, section_list); 240 } 241 242 const Address &Address::operator=(const Address &rhs) { 243 if (this != &rhs) { 244 m_section_wp = rhs.m_section_wp; 245 m_offset = rhs.m_offset; 246 } 247 return *this; 248 } 249 250 bool Address::ResolveAddressUsingFileSections(addr_t file_addr, 251 const SectionList *section_list) { 252 if (section_list) { 253 SectionSP section_sp( 254 section_list->FindSectionContainingFileAddress(file_addr)); 255 m_section_wp = section_sp; 256 if (section_sp) { 257 assert(section_sp->ContainsFileAddress(file_addr)); 258 m_offset = file_addr - section_sp->GetFileAddress(); 259 return true; // Successfully transformed addr into a section offset 260 // address 261 } 262 } 263 m_offset = file_addr; 264 return false; // Failed to resolve this address to a section offset value 265 } 266 267 /// if "addr_range_ptr" is not NULL, then fill in with the address range of the function. 268 bool Address::ResolveFunctionScope(SymbolContext &sym_ctx, 269 AddressRange *addr_range_ptr) { 270 constexpr SymbolContextItem resolve_scope = 271 eSymbolContextFunction | eSymbolContextSymbol; 272 273 if (!(CalculateSymbolContext(&sym_ctx, resolve_scope) & resolve_scope)) { 274 if (addr_range_ptr) 275 addr_range_ptr->Clear(); 276 return false; 277 } 278 279 if (!addr_range_ptr) 280 return true; 281 282 return sym_ctx.GetAddressRange(resolve_scope, 0, false, *addr_range_ptr); 283 } 284 285 ModuleSP Address::GetModule() const { 286 lldb::ModuleSP module_sp; 287 SectionSP section_sp(GetSection()); 288 if (section_sp) 289 module_sp = section_sp->GetModule(); 290 return module_sp; 291 } 292 293 addr_t Address::GetFileAddress() const { 294 SectionSP section_sp(GetSection()); 295 if (section_sp) { 296 addr_t sect_file_addr = section_sp->GetFileAddress(); 297 if (sect_file_addr == LLDB_INVALID_ADDRESS) { 298 // Section isn't resolved, we can't return a valid file address 299 return LLDB_INVALID_ADDRESS; 300 } 301 // We have a valid file range, so we can return the file based address by 302 // adding the file base address to our offset 303 return sect_file_addr + m_offset; 304 } else if (SectionWasDeletedPrivate()) { 305 // Used to have a valid section but it got deleted so the offset doesn't 306 // mean anything without the section 307 return LLDB_INVALID_ADDRESS; 308 } 309 // No section, we just return the offset since it is the value in this case 310 return m_offset; 311 } 312 313 addr_t Address::GetLoadAddress(Target *target) const { 314 SectionSP section_sp(GetSection()); 315 if (section_sp) { 316 if (target) { 317 addr_t sect_load_addr = section_sp->GetLoadBaseAddress(target); 318 319 if (sect_load_addr != LLDB_INVALID_ADDRESS) { 320 // We have a valid file range, so we can return the file based address 321 // by adding the file base address to our offset 322 return sect_load_addr + m_offset; 323 } 324 } 325 } else if (SectionWasDeletedPrivate()) { 326 // Used to have a valid section but it got deleted so the offset doesn't 327 // mean anything without the section 328 return LLDB_INVALID_ADDRESS; 329 } else { 330 // We don't have a section so the offset is the load address 331 return m_offset; 332 } 333 // The section isn't resolved or an invalid target was passed in so we can't 334 // return a valid load address. 335 return LLDB_INVALID_ADDRESS; 336 } 337 338 addr_t Address::GetCallableLoadAddress(Target *target, bool is_indirect) const { 339 addr_t code_addr = LLDB_INVALID_ADDRESS; 340 341 if (is_indirect && target) { 342 ProcessSP processSP = target->GetProcessSP(); 343 Status error; 344 if (processSP) { 345 code_addr = processSP->ResolveIndirectFunction(this, error); 346 if (!error.Success()) 347 code_addr = LLDB_INVALID_ADDRESS; 348 } 349 } else { 350 code_addr = GetLoadAddress(target); 351 } 352 353 if (code_addr == LLDB_INVALID_ADDRESS) 354 return code_addr; 355 356 if (target) 357 return target->GetCallableLoadAddress(code_addr, GetAddressClass()); 358 return code_addr; 359 } 360 361 bool Address::SetCallableLoadAddress(lldb::addr_t load_addr, Target *target) { 362 if (SetLoadAddress(load_addr, target)) { 363 if (target) 364 m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass()); 365 return true; 366 } 367 return false; 368 } 369 370 addr_t Address::GetOpcodeLoadAddress(Target *target, 371 AddressClass addr_class) const { 372 addr_t code_addr = GetLoadAddress(target); 373 if (code_addr != LLDB_INVALID_ADDRESS) { 374 if (addr_class == AddressClass::eInvalid) 375 addr_class = GetAddressClass(); 376 code_addr = target->GetOpcodeLoadAddress(code_addr, addr_class); 377 } 378 return code_addr; 379 } 380 381 bool Address::SetOpcodeLoadAddress(lldb::addr_t load_addr, Target *target, 382 AddressClass addr_class, 383 bool allow_section_end) { 384 if (SetLoadAddress(load_addr, target, allow_section_end)) { 385 if (target) { 386 if (addr_class == AddressClass::eInvalid) 387 addr_class = GetAddressClass(); 388 m_offset = target->GetOpcodeLoadAddress(m_offset, addr_class); 389 } 390 return true; 391 } 392 return false; 393 } 394 395 bool Address::GetDescription(Stream &s, Target &target, 396 DescriptionLevel level) const { 397 assert(level == eDescriptionLevelBrief && 398 "Non-brief descriptions not implemented"); 399 LineEntry line_entry; 400 if (CalculateSymbolContextLineEntry(line_entry)) { 401 s.Printf(" (%s:%u:%u)", line_entry.file.GetFilename().GetCString(), 402 line_entry.line, line_entry.column); 403 return true; 404 } 405 return false; 406 } 407 408 bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, 409 DumpStyle fallback_style, uint32_t addr_size, 410 bool all_ranges, llvm::StringRef pattern) const { 411 // If the section was nullptr, only load address is going to work unless we 412 // are trying to deref a pointer 413 SectionSP section_sp(GetSection()); 414 if (!section_sp && style != DumpStyleResolvedPointerDescription) 415 style = DumpStyleLoadAddress; 416 417 ExecutionContext exe_ctx(exe_scope); 418 Target *target = exe_ctx.GetTargetPtr(); 419 // If addr_byte_size is UINT32_MAX, then determine the correct address byte 420 // size for the process or default to the size of addr_t 421 if (addr_size == UINT32_MAX) { 422 if (target) 423 addr_size = target->GetArchitecture().GetAddressByteSize(); 424 else 425 addr_size = sizeof(addr_t); 426 } 427 428 Address so_addr; 429 switch (style) { 430 case DumpStyleInvalid: 431 return false; 432 433 case DumpStyleSectionNameOffset: 434 if (section_sp) { 435 section_sp->DumpName(s->AsRawOstream()); 436 s->Printf(" + %" PRIu64, m_offset); 437 } else { 438 DumpAddress(s->AsRawOstream(), m_offset, addr_size); 439 } 440 break; 441 442 case DumpStyleSectionPointerOffset: 443 s->Printf("(Section *)%p + ", static_cast<void *>(section_sp.get())); 444 DumpAddress(s->AsRawOstream(), m_offset, addr_size); 445 break; 446 447 case DumpStyleModuleWithFileAddress: 448 if (section_sp) { 449 ModuleSP module_sp = section_sp->GetModule(); 450 if (module_sp) 451 s->Printf("%s[", module_sp->GetFileSpec().GetFilename().AsCString( 452 "<Unknown>")); 453 else 454 s->Printf("%s[", "<Unknown>"); 455 } 456 [[fallthrough]]; 457 case DumpStyleFileAddress: { 458 addr_t file_addr = GetFileAddress(); 459 if (file_addr == LLDB_INVALID_ADDRESS) { 460 if (fallback_style != DumpStyleInvalid) 461 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 462 return false; 463 } 464 DumpAddress(s->AsRawOstream(), file_addr, addr_size); 465 if (style == DumpStyleModuleWithFileAddress && section_sp) 466 s->PutChar(']'); 467 } break; 468 469 case DumpStyleLoadAddress: { 470 addr_t load_addr = GetLoadAddress(target); 471 472 /* 473 * MIPS: 474 * Display address in compressed form for MIPS16 or microMIPS 475 * if the address belongs to AddressClass::eCodeAlternateISA. 476 */ 477 if (target) { 478 const llvm::Triple::ArchType llvm_arch = 479 target->GetArchitecture().GetMachine(); 480 if (llvm_arch == llvm::Triple::mips || 481 llvm_arch == llvm::Triple::mipsel || 482 llvm_arch == llvm::Triple::mips64 || 483 llvm_arch == llvm::Triple::mips64el) 484 load_addr = GetCallableLoadAddress(target); 485 } 486 487 if (load_addr == LLDB_INVALID_ADDRESS) { 488 if (fallback_style != DumpStyleInvalid) 489 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 490 return false; 491 } 492 DumpAddress(s->AsRawOstream(), load_addr, addr_size); 493 } break; 494 495 case DumpStyleResolvedDescription: 496 case DumpStyleResolvedDescriptionNoModule: 497 case DumpStyleResolvedDescriptionNoFunctionArguments: 498 case DumpStyleNoFunctionName: 499 if (IsSectionOffset()) { 500 uint32_t pointer_size = 4; 501 ModuleSP module_sp(GetModule()); 502 if (target) 503 pointer_size = target->GetArchitecture().GetAddressByteSize(); 504 else if (module_sp) 505 pointer_size = module_sp->GetArchitecture().GetAddressByteSize(); 506 bool showed_info = false; 507 if (section_sp) { 508 SectionType sect_type = section_sp->GetType(); 509 switch (sect_type) { 510 case eSectionTypeData: 511 if (module_sp) { 512 if (Symtab *symtab = module_sp->GetSymtab()) { 513 const addr_t file_Addr = GetFileAddress(); 514 Symbol *symbol = 515 symtab->FindSymbolContainingFileAddress(file_Addr); 516 if (symbol) { 517 const char *symbol_name = symbol->GetName().AsCString(); 518 if (symbol_name) { 519 llvm::StringRef ansi_prefix; 520 llvm::StringRef ansi_suffix; 521 if (target) { 522 ansi_prefix = 523 target->GetDebugger().GetRegexMatchAnsiPrefix(); 524 ansi_suffix = 525 target->GetDebugger().GetRegexMatchAnsiSuffix(); 526 } 527 s->PutCStringColorHighlighted(symbol_name, pattern, 528 ansi_prefix, ansi_suffix); 529 addr_t delta = 530 file_Addr - symbol->GetAddressRef().GetFileAddress(); 531 if (delta) 532 s->Printf(" + %" PRIu64, delta); 533 showed_info = true; 534 } 535 } 536 } 537 } 538 break; 539 540 case eSectionTypeDataCString: 541 // Read the C string from memory and display it 542 showed_info = true; 543 ReadCStringFromMemory(exe_scope, *this, s); 544 break; 545 546 case eSectionTypeDataCStringPointers: 547 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) { 548 #if VERBOSE_OUTPUT 549 s->PutCString("(char *)"); 550 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, 551 DumpStyleFileAddress); 552 s->PutCString(": "); 553 #endif 554 showed_info = true; 555 ReadCStringFromMemory(exe_scope, so_addr, s); 556 } 557 break; 558 559 case eSectionTypeDataObjCMessageRefs: 560 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) { 561 if (target && so_addr.IsSectionOffset()) { 562 SymbolContext func_sc; 563 target->GetImages().ResolveSymbolContextForAddress( 564 so_addr, eSymbolContextEverything, func_sc); 565 if (func_sc.function != nullptr || func_sc.symbol != nullptr) { 566 showed_info = true; 567 #if VERBOSE_OUTPUT 568 s->PutCString("(objc_msgref *) -> { (func*)"); 569 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, 570 DumpStyleFileAddress); 571 #else 572 s->PutCString("{ "); 573 #endif 574 Address cstr_addr(*this); 575 cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size); 576 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true, 577 false, true, true); 578 if (ReadAddress(exe_scope, cstr_addr, pointer_size, so_addr)) { 579 #if VERBOSE_OUTPUT 580 s->PutCString("), (char *)"); 581 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, 582 DumpStyleFileAddress); 583 s->PutCString(" ("); 584 #else 585 s->PutCString(", "); 586 #endif 587 ReadCStringFromMemory(exe_scope, so_addr, s); 588 } 589 #if VERBOSE_OUTPUT 590 s->PutCString(") }"); 591 #else 592 s->PutCString(" }"); 593 #endif 594 } 595 } 596 } 597 break; 598 599 case eSectionTypeDataObjCCFStrings: { 600 Address cfstring_data_addr(*this); 601 cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() + 602 (2 * pointer_size)); 603 if (ReadAddress(exe_scope, cfstring_data_addr, pointer_size, 604 so_addr)) { 605 #if VERBOSE_OUTPUT 606 s->PutCString("(CFString *) "); 607 cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress, 608 DumpStyleFileAddress); 609 s->PutCString(" -> @"); 610 #else 611 s->PutChar('@'); 612 #endif 613 if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription)) 614 showed_info = true; 615 } 616 } break; 617 618 case eSectionTypeData4: 619 // Read the 4 byte data and display it 620 showed_info = true; 621 s->PutCString("(uint32_t) "); 622 DumpUInt(exe_scope, *this, 4, s); 623 break; 624 625 case eSectionTypeData8: 626 // Read the 8 byte data and display it 627 showed_info = true; 628 s->PutCString("(uint64_t) "); 629 DumpUInt(exe_scope, *this, 8, s); 630 break; 631 632 case eSectionTypeData16: 633 // Read the 16 byte data and display it 634 showed_info = true; 635 s->PutCString("(uint128_t) "); 636 DumpUInt(exe_scope, *this, 16, s); 637 break; 638 639 case eSectionTypeDataPointers: 640 // Read the pointer data and display it 641 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) { 642 s->PutCString("(void *)"); 643 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, 644 DumpStyleFileAddress); 645 646 showed_info = true; 647 if (so_addr.IsSectionOffset()) { 648 SymbolContext pointer_sc; 649 if (target) { 650 target->GetImages().ResolveSymbolContextForAddress( 651 so_addr, eSymbolContextEverything, pointer_sc); 652 if (pointer_sc.function != nullptr || 653 pointer_sc.symbol != nullptr) { 654 s->PutCString(": "); 655 pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false, 656 false, true, true, pattern); 657 } 658 } 659 } 660 } 661 break; 662 663 default: 664 break; 665 } 666 } 667 668 if (!showed_info) { 669 if (module_sp) { 670 SymbolContext sc; 671 module_sp->ResolveSymbolContextForAddress( 672 *this, eSymbolContextEverything, sc); 673 if (sc.function || sc.symbol) { 674 bool show_stop_context = true; 675 const bool show_module = (style == DumpStyleResolvedDescription); 676 const bool show_fullpaths = false; 677 const bool show_inlined_frames = true; 678 const bool show_function_arguments = 679 (style != DumpStyleResolvedDescriptionNoFunctionArguments); 680 const bool show_function_name = (style != DumpStyleNoFunctionName); 681 if (sc.function == nullptr && sc.symbol != nullptr) { 682 // If we have just a symbol make sure it is in the right section 683 if (sc.symbol->ValueIsAddress()) { 684 if (sc.symbol->GetAddressRef().GetSection() != GetSection()) { 685 // don't show the module if the symbol is a trampoline symbol 686 show_stop_context = false; 687 } 688 } 689 } 690 if (show_stop_context) { 691 // We have a function or a symbol from the same sections as this 692 // address. 693 sc.DumpStopContext(s, exe_scope, *this, show_fullpaths, 694 show_module, show_inlined_frames, 695 show_function_arguments, show_function_name, 696 pattern); 697 } else { 698 // We found a symbol but it was in a different section so it 699 // isn't the symbol we should be showing, just show the section 700 // name + offset 701 Dump(s, exe_scope, DumpStyleSectionNameOffset, DumpStyleInvalid, 702 UINT32_MAX, false, pattern); 703 } 704 } 705 } 706 } 707 } else { 708 if (fallback_style != DumpStyleInvalid) 709 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size, 710 false, pattern); 711 return false; 712 } 713 break; 714 715 case DumpStyleDetailedSymbolContext: 716 if (IsSectionOffset()) { 717 ModuleSP module_sp(GetModule()); 718 if (module_sp) { 719 SymbolContext sc; 720 module_sp->ResolveSymbolContextForAddress( 721 *this, eSymbolContextEverything | eSymbolContextVariable, sc); 722 if (sc.symbol) { 723 // If we have just a symbol make sure it is in the same section as 724 // our address. If it isn't, then we might have just found the last 725 // symbol that came before the address that we are looking up that 726 // has nothing to do with our address lookup. 727 if (sc.symbol->ValueIsAddress() && 728 sc.symbol->GetAddressRef().GetSection() != GetSection()) 729 sc.symbol = nullptr; 730 } 731 sc.GetDescription(s, eDescriptionLevelBrief, target, pattern); 732 733 if (sc.block) { 734 bool can_create = true; 735 bool get_parent_variables = true; 736 bool stop_if_block_is_inlined_function = false; 737 VariableList variable_list; 738 addr_t file_addr = GetFileAddress(); 739 sc.block->AppendVariables( 740 can_create, get_parent_variables, 741 stop_if_block_is_inlined_function, 742 [&](Variable *var) { 743 return var && var->LocationIsValidForAddress(*this); 744 }, 745 &variable_list); 746 ABISP abi = 747 ABI::FindPlugin(ProcessSP(), module_sp->GetArchitecture()); 748 for (const VariableSP &var_sp : variable_list) { 749 s->Indent(); 750 s->Printf(" Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"", 751 var_sp->GetID(), var_sp->GetName().GetCString()); 752 Type *type = var_sp->GetType(); 753 if (type) 754 s->Printf(", type = \"%s\"", type->GetName().GetCString()); 755 else 756 s->PutCString(", type = <unknown>"); 757 s->PutCString(", valid ranges = "); 758 if (var_sp->GetScopeRange().IsEmpty()) 759 s->PutCString("<block>"); 760 else if (all_ranges) { 761 for (auto range : var_sp->GetScopeRange()) 762 DumpAddressRange(s->AsRawOstream(), range.GetRangeBase(), 763 range.GetRangeEnd(), addr_size); 764 } else if (auto *range = 765 var_sp->GetScopeRange().FindEntryThatContains( 766 file_addr)) 767 DumpAddressRange(s->AsRawOstream(), range->GetRangeBase(), 768 range->GetRangeEnd(), addr_size); 769 s->PutCString(", location = "); 770 var_sp->DumpLocations(s, all_ranges ? LLDB_INVALID_ADDRESS : *this); 771 s->PutCString(", decl = "); 772 var_sp->GetDeclaration().DumpStopContext(s, false); 773 s->EOL(); 774 } 775 } 776 } 777 } else { 778 if (fallback_style != DumpStyleInvalid) 779 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size, 780 false, pattern); 781 return false; 782 } 783 break; 784 785 case DumpStyleResolvedPointerDescription: { 786 Process *process = exe_ctx.GetProcessPtr(); 787 if (process) { 788 addr_t load_addr = GetLoadAddress(target); 789 if (load_addr != LLDB_INVALID_ADDRESS) { 790 Status memory_error; 791 addr_t dereferenced_load_addr = 792 process->ReadPointerFromMemory(load_addr, memory_error); 793 if (dereferenced_load_addr != LLDB_INVALID_ADDRESS) { 794 Address dereferenced_addr; 795 if (dereferenced_addr.SetLoadAddress(dereferenced_load_addr, 796 target)) { 797 StreamString strm; 798 if (dereferenced_addr.Dump(&strm, exe_scope, 799 DumpStyleResolvedDescription, 800 DumpStyleInvalid, addr_size)) { 801 DumpAddress(s->AsRawOstream(), dereferenced_load_addr, addr_size, 802 " -> ", " "); 803 s->Write(strm.GetString().data(), strm.GetSize()); 804 return true; 805 } 806 } 807 } 808 } 809 } 810 if (fallback_style != DumpStyleInvalid) 811 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 812 return false; 813 } break; 814 } 815 816 return true; 817 } 818 819 bool Address::SectionWasDeleted() const { 820 if (GetSection()) 821 return false; 822 return SectionWasDeletedPrivate(); 823 } 824 825 bool Address::SectionWasDeletedPrivate() const { 826 lldb::SectionWP empty_section_wp; 827 828 // If either call to "std::weak_ptr::owner_before(...) value returns true, 829 // this indicates that m_section_wp once contained (possibly still does) a 830 // reference to a valid shared pointer. This helps us know if we had a valid 831 // reference to a section which is now invalid because the module it was in 832 // was unloaded/deleted, or if the address doesn't have a valid reference to 833 // a section. 834 return empty_section_wp.owner_before(m_section_wp) || 835 m_section_wp.owner_before(empty_section_wp); 836 } 837 838 uint32_t 839 Address::CalculateSymbolContext(SymbolContext *sc, 840 SymbolContextItem resolve_scope) const { 841 sc->Clear(false); 842 // Absolute addresses don't have enough information to reconstruct even their 843 // target. 844 845 SectionSP section_sp(GetSection()); 846 if (section_sp) { 847 ModuleSP module_sp(section_sp->GetModule()); 848 if (module_sp) { 849 sc->module_sp = module_sp; 850 if (sc->module_sp) 851 return sc->module_sp->ResolveSymbolContextForAddress( 852 *this, resolve_scope, *sc); 853 } 854 } 855 return 0; 856 } 857 858 ModuleSP Address::CalculateSymbolContextModule() const { 859 SectionSP section_sp(GetSection()); 860 if (section_sp) 861 return section_sp->GetModule(); 862 return ModuleSP(); 863 } 864 865 CompileUnit *Address::CalculateSymbolContextCompileUnit() const { 866 SectionSP section_sp(GetSection()); 867 if (section_sp) { 868 SymbolContext sc; 869 sc.module_sp = section_sp->GetModule(); 870 if (sc.module_sp) { 871 sc.module_sp->ResolveSymbolContextForAddress(*this, 872 eSymbolContextCompUnit, sc); 873 return sc.comp_unit; 874 } 875 } 876 return nullptr; 877 } 878 879 Function *Address::CalculateSymbolContextFunction() const { 880 SectionSP section_sp(GetSection()); 881 if (section_sp) { 882 SymbolContext sc; 883 sc.module_sp = section_sp->GetModule(); 884 if (sc.module_sp) { 885 sc.module_sp->ResolveSymbolContextForAddress(*this, 886 eSymbolContextFunction, sc); 887 return sc.function; 888 } 889 } 890 return nullptr; 891 } 892 893 Block *Address::CalculateSymbolContextBlock() const { 894 SectionSP section_sp(GetSection()); 895 if (section_sp) { 896 SymbolContext sc; 897 sc.module_sp = section_sp->GetModule(); 898 if (sc.module_sp) { 899 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextBlock, 900 sc); 901 return sc.block; 902 } 903 } 904 return nullptr; 905 } 906 907 Symbol *Address::CalculateSymbolContextSymbol() const { 908 SectionSP section_sp(GetSection()); 909 if (section_sp) { 910 SymbolContext sc; 911 sc.module_sp = section_sp->GetModule(); 912 if (sc.module_sp) { 913 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextSymbol, 914 sc); 915 return sc.symbol; 916 } 917 } 918 return nullptr; 919 } 920 921 bool Address::CalculateSymbolContextLineEntry(LineEntry &line_entry) const { 922 SectionSP section_sp(GetSection()); 923 if (section_sp) { 924 SymbolContext sc; 925 sc.module_sp = section_sp->GetModule(); 926 if (sc.module_sp) { 927 sc.module_sp->ResolveSymbolContextForAddress(*this, 928 eSymbolContextLineEntry, sc); 929 if (sc.line_entry.IsValid()) { 930 line_entry = sc.line_entry; 931 return true; 932 } 933 } 934 } 935 line_entry.Clear(); 936 return false; 937 } 938 939 int Address::CompareFileAddress(const Address &a, const Address &b) { 940 addr_t a_file_addr = a.GetFileAddress(); 941 addr_t b_file_addr = b.GetFileAddress(); 942 if (a_file_addr < b_file_addr) 943 return -1; 944 if (a_file_addr > b_file_addr) 945 return +1; 946 return 0; 947 } 948 949 int Address::CompareLoadAddress(const Address &a, const Address &b, 950 Target *target) { 951 assert(target != nullptr); 952 addr_t a_load_addr = a.GetLoadAddress(target); 953 addr_t b_load_addr = b.GetLoadAddress(target); 954 if (a_load_addr < b_load_addr) 955 return -1; 956 if (a_load_addr > b_load_addr) 957 return +1; 958 return 0; 959 } 960 961 int Address::CompareModulePointerAndOffset(const Address &a, const Address &b) { 962 ModuleSP a_module_sp(a.GetModule()); 963 ModuleSP b_module_sp(b.GetModule()); 964 Module *a_module = a_module_sp.get(); 965 Module *b_module = b_module_sp.get(); 966 if (a_module < b_module) 967 return -1; 968 if (a_module > b_module) 969 return +1; 970 // Modules are the same, just compare the file address since they should be 971 // unique 972 addr_t a_file_addr = a.GetFileAddress(); 973 addr_t b_file_addr = b.GetFileAddress(); 974 if (a_file_addr < b_file_addr) 975 return -1; 976 if (a_file_addr > b_file_addr) 977 return +1; 978 return 0; 979 } 980 981 size_t Address::MemorySize() const { 982 // Noting special for the memory size of a single Address object, it is just 983 // the size of itself. 984 return sizeof(Address); 985 } 986 987 // NOTE: Be careful using this operator. It can correctly compare two 988 // addresses from the same Module correctly. It can't compare two addresses 989 // from different modules in any meaningful way, but it will compare the module 990 // pointers. 991 // 992 // To sum things up: 993 // - works great for addresses within the same module - it works for addresses 994 // across multiple modules, but don't expect the 995 // address results to make much sense 996 // 997 // This basically lets Address objects be used in ordered collection classes. 998 999 bool lldb_private::operator<(const Address &lhs, const Address &rhs) { 1000 ModuleSP lhs_module_sp(lhs.GetModule()); 1001 ModuleSP rhs_module_sp(rhs.GetModule()); 1002 Module *lhs_module = lhs_module_sp.get(); 1003 Module *rhs_module = rhs_module_sp.get(); 1004 if (lhs_module == rhs_module) { 1005 // Addresses are in the same module, just compare the file addresses 1006 return lhs.GetFileAddress() < rhs.GetFileAddress(); 1007 } else { 1008 // The addresses are from different modules, just use the module pointer 1009 // value to get consistent ordering 1010 return lhs_module < rhs_module; 1011 } 1012 } 1013 1014 bool lldb_private::operator>(const Address &lhs, const Address &rhs) { 1015 ModuleSP lhs_module_sp(lhs.GetModule()); 1016 ModuleSP rhs_module_sp(rhs.GetModule()); 1017 Module *lhs_module = lhs_module_sp.get(); 1018 Module *rhs_module = rhs_module_sp.get(); 1019 if (lhs_module == rhs_module) { 1020 // Addresses are in the same module, just compare the file addresses 1021 return lhs.GetFileAddress() > rhs.GetFileAddress(); 1022 } else { 1023 // The addresses are from different modules, just use the module pointer 1024 // value to get consistent ordering 1025 return lhs_module > rhs_module; 1026 } 1027 } 1028 1029 // The operator == checks for exact equality only (same section, same offset) 1030 bool lldb_private::operator==(const Address &a, const Address &rhs) { 1031 return a.GetOffset() == rhs.GetOffset() && a.GetSection() == rhs.GetSection(); 1032 } 1033 1034 // The operator != checks for exact inequality only (differing section, or 1035 // different offset) 1036 bool lldb_private::operator!=(const Address &a, const Address &rhs) { 1037 return a.GetOffset() != rhs.GetOffset() || a.GetSection() != rhs.GetSection(); 1038 } 1039 1040 AddressClass Address::GetAddressClass() const { 1041 ModuleSP module_sp(GetModule()); 1042 if (module_sp) { 1043 ObjectFile *obj_file = module_sp->GetObjectFile(); 1044 if (obj_file) { 1045 // Give the symbol file a chance to add to the unified section list 1046 // and to the symtab. 1047 module_sp->GetSymtab(); 1048 return obj_file->GetAddressClass(GetFileAddress()); 1049 } 1050 } 1051 return AddressClass::eUnknown; 1052 } 1053 1054 bool Address::SetLoadAddress(lldb::addr_t load_addr, Target *target, 1055 bool allow_section_end) { 1056 if (target && target->GetSectionLoadList().ResolveLoadAddress( 1057 load_addr, *this, allow_section_end)) 1058 return true; 1059 m_section_wp.reset(); 1060 m_offset = load_addr; 1061 return false; 1062 } 1063