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