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