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