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 #include "lldb/Core/DumpDataExtractor.h" 12 #include "lldb/Core/Module.h" 13 #include "lldb/Core/ModuleList.h" // for ModuleList 14 #include "lldb/Core/Section.h" 15 #include "lldb/Symbol/Block.h" 16 #include "lldb/Symbol/Declaration.h" // for Declaration 17 #include "lldb/Symbol/LineEntry.h" // for LineEntry 18 #include "lldb/Symbol/ObjectFile.h" 19 #include "lldb/Symbol/Symbol.h" // for Symbol 20 #include "lldb/Symbol/SymbolContext.h" // for SymbolContext 21 #include "lldb/Symbol/SymbolVendor.h" 22 #include "lldb/Symbol/Symtab.h" // for Symtab 23 #include "lldb/Symbol/Type.h" // for Type 24 #include "lldb/Symbol/Variable.h" 25 #include "lldb/Symbol/VariableList.h" 26 #include "lldb/Target/ExecutionContext.h" 27 #include "lldb/Target/ExecutionContextScope.h" // for ExecutionContextScope 28 #include "lldb/Target/Process.h" 29 #include "lldb/Target/SectionLoadList.h" 30 #include "lldb/Target/Target.h" 31 #include "lldb/Utility/ConstString.h" // for ConstString 32 #include "lldb/Utility/DataExtractor.h" // for DataExtractor 33 #include "lldb/Utility/Endian.h" // for InlHostByteOrder 34 #include "lldb/Utility/FileSpec.h" // for FileSpec 35 #include "lldb/Utility/Status.h" // for Status 36 #include "lldb/Utility/Stream.h" // for Stream 37 #include "lldb/Utility/StreamString.h" // for StreamString 38 39 #include "llvm/ADT/StringRef.h" // for StringRef 40 #include "llvm/ADT/Triple.h" 41 #include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH 42 43 #include <cstdint> // for uint8_t, uint32_t 44 #include <memory> // for shared_ptr, operator!= 45 #include <vector> // for vector 46 47 #include <assert.h> // for assert 48 #include <inttypes.h> // for PRIu64, PRIx64 49 #include <string.h> // for size_t, strlen 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 prefer_file_cache = false; 70 return target_sp->ReadMemory(address, prefer_file_cache, dst, dst_len, 71 error); 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 145 // have a 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 155 // no 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 0; 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(), m_offset(LLDB_INVALID_ADDRESS) { 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 ModuleSP Address::GetModule() const { 266 lldb::ModuleSP module_sp; 267 SectionSP section_sp(GetSection()); 268 if (section_sp) 269 module_sp = section_sp->GetModule(); 270 return module_sp; 271 } 272 273 addr_t Address::GetFileAddress() const { 274 SectionSP section_sp(GetSection()); 275 if (section_sp) { 276 addr_t sect_file_addr = section_sp->GetFileAddress(); 277 if (sect_file_addr == LLDB_INVALID_ADDRESS) { 278 // Section isn't resolved, we can't return a valid file address 279 return LLDB_INVALID_ADDRESS; 280 } 281 // We have a valid file range, so we can return the file based 282 // address by adding the file base address to our offset 283 return sect_file_addr + m_offset; 284 } else if (SectionWasDeletedPrivate()) { 285 // Used to have a valid section but it got deleted so the 286 // offset doesn't mean anything without the section 287 return LLDB_INVALID_ADDRESS; 288 } 289 // No section, we just return the offset since it is the value in this case 290 return m_offset; 291 } 292 293 addr_t Address::GetLoadAddress(Target *target) const { 294 SectionSP section_sp(GetSection()); 295 if (section_sp) { 296 if (target) { 297 addr_t sect_load_addr = section_sp->GetLoadBaseAddress(target); 298 299 if (sect_load_addr != LLDB_INVALID_ADDRESS) { 300 // We have a valid file range, so we can return the file based 301 // address by adding the file base address to our offset 302 return sect_load_addr + m_offset; 303 } 304 } 305 } else if (SectionWasDeletedPrivate()) { 306 // Used to have a valid section but it got deleted so the 307 // offset doesn't mean anything without the section 308 return LLDB_INVALID_ADDRESS; 309 } else { 310 // We don't have a section so the offset is the load address 311 return m_offset; 312 } 313 // The section isn't resolved or an invalid target was passed in 314 // so we can't return a valid load address. 315 return LLDB_INVALID_ADDRESS; 316 } 317 318 addr_t Address::GetCallableLoadAddress(Target *target, bool is_indirect) const { 319 addr_t code_addr = LLDB_INVALID_ADDRESS; 320 321 if (is_indirect && target) { 322 ProcessSP processSP = target->GetProcessSP(); 323 Status error; 324 if (processSP) { 325 code_addr = processSP->ResolveIndirectFunction(this, error); 326 if (!error.Success()) 327 code_addr = LLDB_INVALID_ADDRESS; 328 } 329 } else { 330 code_addr = GetLoadAddress(target); 331 } 332 333 if (code_addr == LLDB_INVALID_ADDRESS) 334 return code_addr; 335 336 if (target) 337 return target->GetCallableLoadAddress(code_addr, GetAddressClass()); 338 return code_addr; 339 } 340 341 bool Address::SetCallableLoadAddress(lldb::addr_t load_addr, Target *target) { 342 if (SetLoadAddress(load_addr, target)) { 343 if (target) 344 m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass()); 345 return true; 346 } 347 return false; 348 } 349 350 addr_t Address::GetOpcodeLoadAddress(Target *target, 351 AddressClass addr_class) const { 352 addr_t code_addr = GetLoadAddress(target); 353 if (code_addr != LLDB_INVALID_ADDRESS) { 354 if (addr_class == eAddressClassInvalid) 355 addr_class = GetAddressClass(); 356 code_addr = target->GetOpcodeLoadAddress(code_addr, addr_class); 357 } 358 return code_addr; 359 } 360 361 bool Address::SetOpcodeLoadAddress(lldb::addr_t load_addr, Target *target, 362 AddressClass addr_class, 363 bool allow_section_end) { 364 if (SetLoadAddress(load_addr, target, allow_section_end)) { 365 if (target) { 366 if (addr_class == eAddressClassInvalid) 367 addr_class = GetAddressClass(); 368 m_offset = target->GetOpcodeLoadAddress(m_offset, addr_class); 369 } 370 return true; 371 } 372 return false; 373 } 374 375 bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, 376 DumpStyle fallback_style, uint32_t addr_size) const { 377 // If the section was nullptr, only load address is going to work unless we 378 // are 379 // trying to deref a pointer 380 SectionSP section_sp(GetSection()); 381 if (!section_sp && style != DumpStyleResolvedPointerDescription) 382 style = DumpStyleLoadAddress; 383 384 ExecutionContext exe_ctx(exe_scope); 385 Target *target = exe_ctx.GetTargetPtr(); 386 // If addr_byte_size is UINT32_MAX, then determine the correct address 387 // byte size for the process or default to the size of addr_t 388 if (addr_size == UINT32_MAX) { 389 if (target) 390 addr_size = target->GetArchitecture().GetAddressByteSize(); 391 else 392 addr_size = sizeof(addr_t); 393 } 394 395 Address so_addr; 396 switch (style) { 397 case DumpStyleInvalid: 398 return false; 399 400 case DumpStyleSectionNameOffset: 401 if (section_sp) { 402 section_sp->DumpName(s); 403 s->Printf(" + %" PRIu64, m_offset); 404 } else { 405 s->Address(m_offset, addr_size); 406 } 407 break; 408 409 case DumpStyleSectionPointerOffset: 410 s->Printf("(Section *)%p + ", static_cast<void *>(section_sp.get())); 411 s->Address(m_offset, addr_size); 412 break; 413 414 case DumpStyleModuleWithFileAddress: 415 if (section_sp) { 416 ModuleSP module_sp = section_sp->GetModule(); 417 if (module_sp) 418 s->Printf("%s[", module_sp->GetFileSpec().GetFilename().AsCString( 419 "<Unknown>")); 420 else 421 s->Printf("%s[", "<Unknown>"); 422 } 423 LLVM_FALLTHROUGH; 424 case DumpStyleFileAddress: { 425 addr_t file_addr = GetFileAddress(); 426 if (file_addr == LLDB_INVALID_ADDRESS) { 427 if (fallback_style != DumpStyleInvalid) 428 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 429 return false; 430 } 431 s->Address(file_addr, addr_size); 432 if (style == DumpStyleModuleWithFileAddress && section_sp) 433 s->PutChar(']'); 434 } break; 435 436 case DumpStyleLoadAddress: { 437 addr_t load_addr = GetLoadAddress(target); 438 439 /* 440 * MIPS: 441 * Display address in compressed form for MIPS16 or microMIPS 442 * if the address belongs to eAddressClassCodeAlternateISA. 443 */ 444 if (target) { 445 const llvm::Triple::ArchType llvm_arch = 446 target->GetArchitecture().GetMachine(); 447 if (llvm_arch == llvm::Triple::mips || 448 llvm_arch == llvm::Triple::mipsel || 449 llvm_arch == llvm::Triple::mips64 || 450 llvm_arch == llvm::Triple::mips64el) 451 load_addr = GetCallableLoadAddress(target); 452 } 453 454 if (load_addr == LLDB_INVALID_ADDRESS) { 455 if (fallback_style != DumpStyleInvalid) 456 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 457 return false; 458 } 459 s->Address(load_addr, addr_size); 460 } break; 461 462 case DumpStyleResolvedDescription: 463 case DumpStyleResolvedDescriptionNoModule: 464 case DumpStyleResolvedDescriptionNoFunctionArguments: 465 case DumpStyleNoFunctionName: 466 if (IsSectionOffset()) { 467 uint32_t pointer_size = 4; 468 ModuleSP module_sp(GetModule()); 469 if (target) 470 pointer_size = target->GetArchitecture().GetAddressByteSize(); 471 else if (module_sp) 472 pointer_size = module_sp->GetArchitecture().GetAddressByteSize(); 473 474 bool showed_info = false; 475 if (section_sp) { 476 SectionType sect_type = section_sp->GetType(); 477 switch (sect_type) { 478 case eSectionTypeData: 479 if (module_sp) { 480 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor(); 481 if (sym_vendor) { 482 Symtab *symtab = sym_vendor->GetSymtab(); 483 if (symtab) { 484 const addr_t file_Addr = GetFileAddress(); 485 Symbol *symbol = 486 symtab->FindSymbolContainingFileAddress(file_Addr); 487 if (symbol) { 488 const char *symbol_name = symbol->GetName().AsCString(); 489 if (symbol_name) { 490 s->PutCString(symbol_name); 491 addr_t delta = 492 file_Addr - symbol->GetAddressRef().GetFileAddress(); 493 if (delta) 494 s->Printf(" + %" PRIu64, delta); 495 showed_info = true; 496 } 497 } 498 } 499 } 500 } 501 break; 502 503 case eSectionTypeDataCString: 504 // Read the C string from memory and display it 505 showed_info = true; 506 ReadCStringFromMemory(exe_scope, *this, s); 507 break; 508 509 case eSectionTypeDataCStringPointers: 510 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) { 511 #if VERBOSE_OUTPUT 512 s->PutCString("(char *)"); 513 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, 514 DumpStyleFileAddress); 515 s->PutCString(": "); 516 #endif 517 showed_info = true; 518 ReadCStringFromMemory(exe_scope, so_addr, s); 519 } 520 break; 521 522 case eSectionTypeDataObjCMessageRefs: 523 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) { 524 if (target && so_addr.IsSectionOffset()) { 525 SymbolContext func_sc; 526 target->GetImages().ResolveSymbolContextForAddress( 527 so_addr, eSymbolContextEverything, func_sc); 528 if (func_sc.function != nullptr || func_sc.symbol != nullptr) { 529 showed_info = true; 530 #if VERBOSE_OUTPUT 531 s->PutCString("(objc_msgref *) -> { (func*)"); 532 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, 533 DumpStyleFileAddress); 534 #else 535 s->PutCString("{ "); 536 #endif 537 Address cstr_addr(*this); 538 cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size); 539 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true, 540 false, true, true); 541 if (ReadAddress(exe_scope, cstr_addr, pointer_size, so_addr)) { 542 #if VERBOSE_OUTPUT 543 s->PutCString("), (char *)"); 544 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, 545 DumpStyleFileAddress); 546 s->PutCString(" ("); 547 #else 548 s->PutCString(", "); 549 #endif 550 ReadCStringFromMemory(exe_scope, so_addr, s); 551 } 552 #if VERBOSE_OUTPUT 553 s->PutCString(") }"); 554 #else 555 s->PutCString(" }"); 556 #endif 557 } 558 } 559 } 560 break; 561 562 case eSectionTypeDataObjCCFStrings: { 563 Address cfstring_data_addr(*this); 564 cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() + 565 (2 * pointer_size)); 566 if (ReadAddress(exe_scope, cfstring_data_addr, pointer_size, 567 so_addr)) { 568 #if VERBOSE_OUTPUT 569 s->PutCString("(CFString *) "); 570 cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress, 571 DumpStyleFileAddress); 572 s->PutCString(" -> @"); 573 #else 574 s->PutChar('@'); 575 #endif 576 if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription)) 577 showed_info = true; 578 } 579 } break; 580 581 case eSectionTypeData4: 582 // Read the 4 byte data and display it 583 showed_info = true; 584 s->PutCString("(uint32_t) "); 585 DumpUInt(exe_scope, *this, 4, s); 586 break; 587 588 case eSectionTypeData8: 589 // Read the 8 byte data and display it 590 showed_info = true; 591 s->PutCString("(uint64_t) "); 592 DumpUInt(exe_scope, *this, 8, s); 593 break; 594 595 case eSectionTypeData16: 596 // Read the 16 byte data and display it 597 showed_info = true; 598 s->PutCString("(uint128_t) "); 599 DumpUInt(exe_scope, *this, 16, s); 600 break; 601 602 case eSectionTypeDataPointers: 603 // Read the pointer data and display it 604 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) { 605 s->PutCString("(void *)"); 606 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, 607 DumpStyleFileAddress); 608 609 showed_info = true; 610 if (so_addr.IsSectionOffset()) { 611 SymbolContext pointer_sc; 612 if (target) { 613 target->GetImages().ResolveSymbolContextForAddress( 614 so_addr, eSymbolContextEverything, pointer_sc); 615 if (pointer_sc.function != nullptr || 616 pointer_sc.symbol != nullptr) { 617 s->PutCString(": "); 618 pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false, 619 false, true, true); 620 } 621 } 622 } 623 } 624 break; 625 626 default: 627 break; 628 } 629 } 630 631 if (!showed_info) { 632 if (module_sp) { 633 SymbolContext sc; 634 module_sp->ResolveSymbolContextForAddress( 635 *this, eSymbolContextEverything, sc); 636 if (sc.function || sc.symbol) { 637 bool show_stop_context = true; 638 const bool show_module = (style == DumpStyleResolvedDescription); 639 const bool show_fullpaths = false; 640 const bool show_inlined_frames = true; 641 const bool show_function_arguments = 642 (style != DumpStyleResolvedDescriptionNoFunctionArguments); 643 const bool show_function_name = (style != DumpStyleNoFunctionName); 644 if (sc.function == nullptr && sc.symbol != nullptr) { 645 // If we have just a symbol make sure it is in the right section 646 if (sc.symbol->ValueIsAddress()) { 647 if (sc.symbol->GetAddressRef().GetSection() != GetSection()) { 648 // don't show the module if the symbol is a trampoline symbol 649 show_stop_context = false; 650 } 651 } 652 } 653 if (show_stop_context) { 654 // We have a function or a symbol from the same 655 // sections as this address. 656 sc.DumpStopContext(s, exe_scope, *this, show_fullpaths, 657 show_module, show_inlined_frames, 658 show_function_arguments, show_function_name); 659 } else { 660 // We found a symbol but it was in a different 661 // section so it isn't the symbol we should be 662 // showing, just show the section name + offset 663 Dump(s, exe_scope, DumpStyleSectionNameOffset); 664 } 665 } 666 } 667 } 668 } else { 669 if (fallback_style != DumpStyleInvalid) 670 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 671 return false; 672 } 673 break; 674 675 case DumpStyleDetailedSymbolContext: 676 if (IsSectionOffset()) { 677 ModuleSP module_sp(GetModule()); 678 if (module_sp) { 679 SymbolContext sc; 680 module_sp->ResolveSymbolContextForAddress( 681 *this, eSymbolContextEverything | eSymbolContextVariable, sc); 682 if (sc.symbol) { 683 // If we have just a symbol make sure it is in the same section 684 // as our address. If it isn't, then we might have just found 685 // the last symbol that came before the address that we are 686 // looking up that has nothing to do with our address lookup. 687 if (sc.symbol->ValueIsAddress() && 688 sc.symbol->GetAddressRef().GetSection() != GetSection()) 689 sc.symbol = nullptr; 690 } 691 sc.GetDescription(s, eDescriptionLevelBrief, target); 692 693 if (sc.block) { 694 bool can_create = true; 695 bool get_parent_variables = true; 696 bool stop_if_block_is_inlined_function = false; 697 VariableList variable_list; 698 sc.block->AppendVariables(can_create, get_parent_variables, 699 stop_if_block_is_inlined_function, 700 [](Variable *) { return true; }, 701 &variable_list); 702 703 const size_t num_variables = variable_list.GetSize(); 704 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx) { 705 Variable *var = variable_list.GetVariableAtIndex(var_idx).get(); 706 if (var && var->LocationIsValidForAddress(*this)) { 707 s->Indent(); 708 s->Printf(" Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"", 709 var->GetID(), var->GetName().GetCString()); 710 Type *type = var->GetType(); 711 if (type) 712 s->Printf(", type = \"%s\"", type->GetName().GetCString()); 713 else 714 s->PutCString(", type = <unknown>"); 715 s->PutCString(", location = "); 716 var->DumpLocationForAddress(s, *this); 717 s->PutCString(", decl = "); 718 var->GetDeclaration().DumpStopContext(s, false); 719 s->EOL(); 720 } 721 } 722 } 723 } 724 } else { 725 if (fallback_style != DumpStyleInvalid) 726 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 727 return false; 728 } 729 break; 730 731 case DumpStyleResolvedPointerDescription: { 732 Process *process = exe_ctx.GetProcessPtr(); 733 if (process) { 734 addr_t load_addr = GetLoadAddress(target); 735 if (load_addr != LLDB_INVALID_ADDRESS) { 736 Status memory_error; 737 addr_t dereferenced_load_addr = 738 process->ReadPointerFromMemory(load_addr, memory_error); 739 if (dereferenced_load_addr != LLDB_INVALID_ADDRESS) { 740 Address dereferenced_addr; 741 if (dereferenced_addr.SetLoadAddress(dereferenced_load_addr, 742 target)) { 743 StreamString strm; 744 if (dereferenced_addr.Dump(&strm, exe_scope, 745 DumpStyleResolvedDescription, 746 DumpStyleInvalid, addr_size)) { 747 s->Address(dereferenced_load_addr, addr_size, " -> ", " "); 748 s->Write(strm.GetString().data(), strm.GetSize()); 749 return true; 750 } 751 } 752 } 753 } 754 } 755 if (fallback_style != DumpStyleInvalid) 756 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 757 return false; 758 } break; 759 } 760 761 return true; 762 } 763 764 bool Address::SectionWasDeleted() const { 765 if (GetSection()) 766 return false; 767 return SectionWasDeletedPrivate(); 768 } 769 770 bool Address::SectionWasDeletedPrivate() const { 771 lldb::SectionWP empty_section_wp; 772 773 // If either call to "std::weak_ptr::owner_before(...) value returns true, 774 // this 775 // indicates that m_section_wp once contained (possibly still does) a 776 // reference 777 // to a valid shared pointer. This helps us know if we had a valid reference 778 // to 779 // a section which is now invalid because the module it was in was 780 // unloaded/deleted, 781 // or if the address doesn't have a valid reference to a section. 782 return empty_section_wp.owner_before(m_section_wp) || 783 m_section_wp.owner_before(empty_section_wp); 784 } 785 786 uint32_t Address::CalculateSymbolContext(SymbolContext *sc, 787 uint32_t resolve_scope) const { 788 sc->Clear(false); 789 // Absolute addresses don't have enough information to reconstruct even their 790 // target. 791 792 SectionSP section_sp(GetSection()); 793 if (section_sp) { 794 ModuleSP module_sp(section_sp->GetModule()); 795 if (module_sp) { 796 sc->module_sp = module_sp; 797 if (sc->module_sp) 798 return sc->module_sp->ResolveSymbolContextForAddress( 799 *this, resolve_scope, *sc); 800 } 801 } 802 return 0; 803 } 804 805 ModuleSP Address::CalculateSymbolContextModule() const { 806 SectionSP section_sp(GetSection()); 807 if (section_sp) 808 return section_sp->GetModule(); 809 return ModuleSP(); 810 } 811 812 CompileUnit *Address::CalculateSymbolContextCompileUnit() const { 813 SectionSP section_sp(GetSection()); 814 if (section_sp) { 815 SymbolContext sc; 816 sc.module_sp = section_sp->GetModule(); 817 if (sc.module_sp) { 818 sc.module_sp->ResolveSymbolContextForAddress(*this, 819 eSymbolContextCompUnit, sc); 820 return sc.comp_unit; 821 } 822 } 823 return nullptr; 824 } 825 826 Function *Address::CalculateSymbolContextFunction() const { 827 SectionSP section_sp(GetSection()); 828 if (section_sp) { 829 SymbolContext sc; 830 sc.module_sp = section_sp->GetModule(); 831 if (sc.module_sp) { 832 sc.module_sp->ResolveSymbolContextForAddress(*this, 833 eSymbolContextFunction, sc); 834 return sc.function; 835 } 836 } 837 return nullptr; 838 } 839 840 Block *Address::CalculateSymbolContextBlock() const { 841 SectionSP section_sp(GetSection()); 842 if (section_sp) { 843 SymbolContext sc; 844 sc.module_sp = section_sp->GetModule(); 845 if (sc.module_sp) { 846 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextBlock, 847 sc); 848 return sc.block; 849 } 850 } 851 return nullptr; 852 } 853 854 Symbol *Address::CalculateSymbolContextSymbol() const { 855 SectionSP section_sp(GetSection()); 856 if (section_sp) { 857 SymbolContext sc; 858 sc.module_sp = section_sp->GetModule(); 859 if (sc.module_sp) { 860 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextSymbol, 861 sc); 862 return sc.symbol; 863 } 864 } 865 return nullptr; 866 } 867 868 bool Address::CalculateSymbolContextLineEntry(LineEntry &line_entry) const { 869 SectionSP section_sp(GetSection()); 870 if (section_sp) { 871 SymbolContext sc; 872 sc.module_sp = section_sp->GetModule(); 873 if (sc.module_sp) { 874 sc.module_sp->ResolveSymbolContextForAddress(*this, 875 eSymbolContextLineEntry, sc); 876 if (sc.line_entry.IsValid()) { 877 line_entry = sc.line_entry; 878 return true; 879 } 880 } 881 } 882 line_entry.Clear(); 883 return false; 884 } 885 886 int Address::CompareFileAddress(const Address &a, const Address &b) { 887 addr_t a_file_addr = a.GetFileAddress(); 888 addr_t b_file_addr = b.GetFileAddress(); 889 if (a_file_addr < b_file_addr) 890 return -1; 891 if (a_file_addr > b_file_addr) 892 return +1; 893 return 0; 894 } 895 896 int Address::CompareLoadAddress(const Address &a, const Address &b, 897 Target *target) { 898 assert(target != nullptr); 899 addr_t a_load_addr = a.GetLoadAddress(target); 900 addr_t b_load_addr = b.GetLoadAddress(target); 901 if (a_load_addr < b_load_addr) 902 return -1; 903 if (a_load_addr > b_load_addr) 904 return +1; 905 return 0; 906 } 907 908 int Address::CompareModulePointerAndOffset(const Address &a, const Address &b) { 909 ModuleSP a_module_sp(a.GetModule()); 910 ModuleSP b_module_sp(b.GetModule()); 911 Module *a_module = a_module_sp.get(); 912 Module *b_module = b_module_sp.get(); 913 if (a_module < b_module) 914 return -1; 915 if (a_module > b_module) 916 return +1; 917 // Modules are the same, just compare the file address since they should 918 // be unique 919 addr_t a_file_addr = a.GetFileAddress(); 920 addr_t b_file_addr = b.GetFileAddress(); 921 if (a_file_addr < b_file_addr) 922 return -1; 923 if (a_file_addr > b_file_addr) 924 return +1; 925 return 0; 926 } 927 928 size_t Address::MemorySize() const { 929 // Noting special for the memory size of a single Address object, 930 // it is just the size of itself. 931 return sizeof(Address); 932 } 933 934 //---------------------------------------------------------------------- 935 // NOTE: Be careful using this operator. It can correctly compare two 936 // addresses from the same Module correctly. It can't compare two 937 // addresses from different modules in any meaningful way, but it will 938 // compare the module pointers. 939 // 940 // To sum things up: 941 // - works great for addresses within the same module 942 // - it works for addresses across multiple modules, but don't expect the 943 // address results to make much sense 944 // 945 // This basically lets Address objects be used in ordered collection 946 // classes. 947 //---------------------------------------------------------------------- 948 949 bool lldb_private::operator<(const Address &lhs, const Address &rhs) { 950 ModuleSP lhs_module_sp(lhs.GetModule()); 951 ModuleSP rhs_module_sp(rhs.GetModule()); 952 Module *lhs_module = lhs_module_sp.get(); 953 Module *rhs_module = rhs_module_sp.get(); 954 if (lhs_module == rhs_module) { 955 // Addresses are in the same module, just compare the file addresses 956 return lhs.GetFileAddress() < rhs.GetFileAddress(); 957 } else { 958 // The addresses are from different modules, just use the module 959 // pointer value to get consistent ordering 960 return lhs_module < rhs_module; 961 } 962 } 963 964 bool lldb_private::operator>(const Address &lhs, const Address &rhs) { 965 ModuleSP lhs_module_sp(lhs.GetModule()); 966 ModuleSP rhs_module_sp(rhs.GetModule()); 967 Module *lhs_module = lhs_module_sp.get(); 968 Module *rhs_module = rhs_module_sp.get(); 969 if (lhs_module == rhs_module) { 970 // Addresses are in the same module, just compare the file addresses 971 return lhs.GetFileAddress() > rhs.GetFileAddress(); 972 } else { 973 // The addresses are from different modules, just use the module 974 // pointer value to get consistent ordering 975 return lhs_module > rhs_module; 976 } 977 } 978 979 // The operator == checks for exact equality only (same section, same offset) 980 bool lldb_private::operator==(const Address &a, const Address &rhs) { 981 return a.GetOffset() == rhs.GetOffset() && a.GetSection() == rhs.GetSection(); 982 } 983 984 // The operator != checks for exact inequality only (differing section, or 985 // different offset) 986 bool lldb_private::operator!=(const Address &a, const Address &rhs) { 987 return a.GetOffset() != rhs.GetOffset() || a.GetSection() != rhs.GetSection(); 988 } 989 990 AddressClass Address::GetAddressClass() const { 991 ModuleSP module_sp(GetModule()); 992 if (module_sp) { 993 ObjectFile *obj_file = module_sp->GetObjectFile(); 994 if (obj_file) { 995 // Give the symbol vendor a chance to add to the unified section list. 996 module_sp->GetSymbolVendor(); 997 return obj_file->GetAddressClass(GetFileAddress()); 998 } 999 } 1000 return eAddressClassUnknown; 1001 } 1002 1003 bool Address::SetLoadAddress(lldb::addr_t load_addr, Target *target, 1004 bool allow_section_end) { 1005 if (target && target->GetSectionLoadList().ResolveLoadAddress( 1006 load_addr, *this, allow_section_end)) 1007 return true; 1008 m_section_wp.reset(); 1009 m_offset = load_addr; 1010 return false; 1011 } 1012