1254721Semaste//===-- Address.cpp ---------------------------------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste 10254721Semaste#include "lldb/Core/Address.h" 11254721Semaste#include "lldb/Core/Module.h" 12254721Semaste#include "lldb/Core/Section.h" 13254721Semaste#include "lldb/Symbol/Block.h" 14254721Semaste#include "lldb/Symbol/ObjectFile.h" 15254721Semaste#include "lldb/Symbol/Variable.h" 16254721Semaste#include "lldb/Symbol/VariableList.h" 17254721Semaste#include "lldb/Target/ExecutionContext.h" 18254721Semaste#include "lldb/Target/Process.h" 19269024Semaste#include "lldb/Target/SectionLoadList.h" 20254721Semaste#include "lldb/Target/Target.h" 21254721Semaste#include "lldb/Symbol/SymbolVendor.h" 22254721Semaste 23254721Semaste#include "llvm/ADT/Triple.h" 24254721Semaste 25254721Semasteusing namespace lldb; 26254721Semasteusing namespace lldb_private; 27254721Semaste 28254721Semastestatic size_t 29254721SemasteReadBytes (ExecutionContextScope *exe_scope, const Address &address, void *dst, size_t dst_len) 30254721Semaste{ 31254721Semaste if (exe_scope == NULL) 32254721Semaste return 0; 33254721Semaste 34254721Semaste TargetSP target_sp (exe_scope->CalculateTarget()); 35254721Semaste if (target_sp) 36254721Semaste { 37254721Semaste Error error; 38254721Semaste bool prefer_file_cache = false; 39254721Semaste return target_sp->ReadMemory (address, prefer_file_cache, dst, dst_len, error); 40254721Semaste } 41254721Semaste return 0; 42254721Semaste} 43254721Semaste 44254721Semastestatic bool 45254721SemasteGetByteOrderAndAddressSize (ExecutionContextScope *exe_scope, const Address &address, ByteOrder& byte_order, uint32_t& addr_size) 46254721Semaste{ 47254721Semaste byte_order = eByteOrderInvalid; 48254721Semaste addr_size = 0; 49254721Semaste if (exe_scope == NULL) 50254721Semaste return false; 51254721Semaste 52254721Semaste TargetSP target_sp (exe_scope->CalculateTarget()); 53254721Semaste if (target_sp) 54254721Semaste { 55254721Semaste byte_order = target_sp->GetArchitecture().GetByteOrder(); 56254721Semaste addr_size = target_sp->GetArchitecture().GetAddressByteSize(); 57254721Semaste } 58254721Semaste 59254721Semaste if (byte_order == eByteOrderInvalid || addr_size == 0) 60254721Semaste { 61254721Semaste ModuleSP module_sp (address.GetModule()); 62254721Semaste if (module_sp) 63254721Semaste { 64254721Semaste byte_order = module_sp->GetArchitecture().GetByteOrder(); 65254721Semaste addr_size = module_sp->GetArchitecture().GetAddressByteSize(); 66254721Semaste } 67254721Semaste } 68254721Semaste return byte_order != eByteOrderInvalid && addr_size != 0; 69254721Semaste} 70254721Semaste 71254721Semastestatic uint64_t 72254721SemasteReadUIntMax64 (ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, bool &success) 73254721Semaste{ 74254721Semaste uint64_t uval64 = 0; 75254721Semaste if (exe_scope == NULL || byte_size > sizeof(uint64_t)) 76254721Semaste { 77254721Semaste success = false; 78254721Semaste return 0; 79254721Semaste } 80254721Semaste uint64_t buf = 0; 81254721Semaste 82254721Semaste success = ReadBytes (exe_scope, address, &buf, byte_size) == byte_size; 83254721Semaste if (success) 84254721Semaste { 85254721Semaste ByteOrder byte_order = eByteOrderInvalid; 86254721Semaste uint32_t addr_size = 0; 87254721Semaste if (GetByteOrderAndAddressSize (exe_scope, address, byte_order, addr_size)) 88254721Semaste { 89254721Semaste DataExtractor data (&buf, sizeof(buf), byte_order, addr_size); 90254721Semaste lldb::offset_t offset = 0; 91254721Semaste uval64 = data.GetU64(&offset); 92254721Semaste } 93254721Semaste else 94254721Semaste success = false; 95254721Semaste } 96254721Semaste return uval64; 97254721Semaste} 98254721Semaste 99254721Semastestatic bool 100254721SemasteReadAddress (ExecutionContextScope *exe_scope, const Address &address, uint32_t pointer_size, Address &deref_so_addr) 101254721Semaste{ 102254721Semaste if (exe_scope == NULL) 103254721Semaste return false; 104254721Semaste 105254721Semaste 106254721Semaste bool success = false; 107254721Semaste addr_t deref_addr = ReadUIntMax64 (exe_scope, address, pointer_size, success); 108254721Semaste if (success) 109254721Semaste { 110254721Semaste ExecutionContext exe_ctx; 111254721Semaste exe_scope->CalculateExecutionContext(exe_ctx); 112254721Semaste // If we have any sections that are loaded, try and resolve using the 113254721Semaste // section load list 114254721Semaste Target *target = exe_ctx.GetTargetPtr(); 115254721Semaste if (target && !target->GetSectionLoadList().IsEmpty()) 116254721Semaste { 117254721Semaste if (target->GetSectionLoadList().ResolveLoadAddress (deref_addr, deref_so_addr)) 118254721Semaste return true; 119254721Semaste } 120254721Semaste else 121254721Semaste { 122254721Semaste // If we were not running, yet able to read an integer, we must 123254721Semaste // have a module 124254721Semaste ModuleSP module_sp (address.GetModule()); 125254721Semaste 126254721Semaste assert (module_sp); 127254721Semaste if (module_sp->ResolveFileAddress(deref_addr, deref_so_addr)) 128254721Semaste return true; 129254721Semaste } 130254721Semaste 131254721Semaste // We couldn't make "deref_addr" into a section offset value, but we were 132254721Semaste // able to read the address, so we return a section offset address with 133254721Semaste // no section and "deref_addr" as the offset (address). 134254721Semaste deref_so_addr.SetRawAddress(deref_addr); 135254721Semaste return true; 136254721Semaste } 137254721Semaste return false; 138254721Semaste} 139254721Semaste 140254721Semastestatic bool 141254721SemasteDumpUInt (ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, Stream* strm) 142254721Semaste{ 143254721Semaste if (exe_scope == NULL || byte_size == 0) 144254721Semaste return 0; 145254721Semaste std::vector<uint8_t> buf(byte_size, 0); 146254721Semaste 147254721Semaste if (ReadBytes (exe_scope, address, &buf[0], buf.size()) == buf.size()) 148254721Semaste { 149254721Semaste ByteOrder byte_order = eByteOrderInvalid; 150254721Semaste uint32_t addr_size = 0; 151254721Semaste if (GetByteOrderAndAddressSize (exe_scope, address, byte_order, addr_size)) 152254721Semaste { 153254721Semaste DataExtractor data (&buf.front(), buf.size(), byte_order, addr_size); 154254721Semaste 155254721Semaste data.Dump (strm, 156254721Semaste 0, // Start offset in "data" 157254721Semaste eFormatHex, // Print as characters 158254721Semaste buf.size(), // Size of item 159254721Semaste 1, // Items count 160254721Semaste UINT32_MAX, // num per line 161254721Semaste LLDB_INVALID_ADDRESS,// base address 162254721Semaste 0, // bitfield bit size 163254721Semaste 0); // bitfield bit offset 164254721Semaste 165254721Semaste return true; 166254721Semaste } 167254721Semaste } 168254721Semaste return false; 169254721Semaste} 170254721Semaste 171254721Semaste 172254721Semastestatic size_t 173254721SemasteReadCStringFromMemory (ExecutionContextScope *exe_scope, const Address &address, Stream *strm) 174254721Semaste{ 175254721Semaste if (exe_scope == NULL) 176254721Semaste return 0; 177254721Semaste const size_t k_buf_len = 256; 178254721Semaste char buf[k_buf_len+1]; 179254721Semaste buf[k_buf_len] = '\0'; // NULL terminate 180254721Semaste 181254721Semaste // Byte order and address size don't matter for C string dumping.. 182254721Semaste DataExtractor data (buf, sizeof(buf), lldb::endian::InlHostByteOrder(), 4); 183254721Semaste size_t total_len = 0; 184254721Semaste size_t bytes_read; 185254721Semaste Address curr_address(address); 186254721Semaste strm->PutChar ('"'); 187254721Semaste while ((bytes_read = ReadBytes (exe_scope, curr_address, buf, k_buf_len)) > 0) 188254721Semaste { 189254721Semaste size_t len = strlen(buf); 190254721Semaste if (len == 0) 191254721Semaste break; 192254721Semaste if (len > bytes_read) 193254721Semaste len = bytes_read; 194254721Semaste 195254721Semaste data.Dump (strm, 196254721Semaste 0, // Start offset in "data" 197254721Semaste eFormatChar, // Print as characters 198254721Semaste 1, // Size of item (1 byte for a char!) 199254721Semaste len, // How many bytes to print? 200254721Semaste UINT32_MAX, // num per line 201254721Semaste LLDB_INVALID_ADDRESS,// base address 202254721Semaste 0, // bitfield bit size 203254721Semaste 204254721Semaste 0); // bitfield bit offset 205254721Semaste 206254721Semaste total_len += bytes_read; 207254721Semaste 208254721Semaste if (len < k_buf_len) 209254721Semaste break; 210254721Semaste curr_address.SetOffset (curr_address.GetOffset() + bytes_read); 211254721Semaste } 212254721Semaste strm->PutChar ('"'); 213254721Semaste return total_len; 214254721Semaste} 215254721Semaste 216254721SemasteAddress::Address (lldb::addr_t abs_addr) : 217254721Semaste m_section_wp (), 218254721Semaste m_offset (abs_addr) 219254721Semaste{ 220254721Semaste} 221254721Semaste 222254721SemasteAddress::Address (addr_t address, const SectionList *section_list) : 223254721Semaste m_section_wp (), 224254721Semaste m_offset (LLDB_INVALID_ADDRESS) 225254721Semaste{ 226254721Semaste ResolveAddressUsingFileSections(address, section_list); 227254721Semaste} 228254721Semaste 229254721Semasteconst Address& 230254721SemasteAddress::operator= (const Address& rhs) 231254721Semaste{ 232254721Semaste if (this != &rhs) 233254721Semaste { 234254721Semaste m_section_wp = rhs.m_section_wp; 235254721Semaste m_offset = rhs.m_offset.load(); 236254721Semaste } 237254721Semaste return *this; 238254721Semaste} 239254721Semaste 240254721Semastebool 241254721SemasteAddress::ResolveAddressUsingFileSections (addr_t file_addr, const SectionList *section_list) 242254721Semaste{ 243254721Semaste if (section_list) 244254721Semaste { 245254721Semaste SectionSP section_sp (section_list->FindSectionContainingFileAddress(file_addr)); 246254721Semaste m_section_wp = section_sp; 247254721Semaste if (section_sp) 248254721Semaste { 249254721Semaste assert( section_sp->ContainsFileAddress(file_addr) ); 250254721Semaste m_offset = file_addr - section_sp->GetFileAddress(); 251254721Semaste return true; // Successfully transformed addr into a section offset address 252254721Semaste } 253254721Semaste } 254254721Semaste m_offset = file_addr; 255254721Semaste return false; // Failed to resolve this address to a section offset value 256254721Semaste} 257254721Semaste 258254721SemasteModuleSP 259254721SemasteAddress::GetModule () const 260254721Semaste{ 261254721Semaste lldb::ModuleSP module_sp; 262254721Semaste SectionSP section_sp (GetSection()); 263254721Semaste if (section_sp) 264254721Semaste module_sp = section_sp->GetModule(); 265254721Semaste return module_sp; 266254721Semaste} 267254721Semaste 268254721Semasteaddr_t 269254721SemasteAddress::GetFileAddress () const 270254721Semaste{ 271254721Semaste SectionSP section_sp (GetSection()); 272254721Semaste if (section_sp) 273254721Semaste { 274254721Semaste addr_t sect_file_addr = section_sp->GetFileAddress(); 275254721Semaste if (sect_file_addr == LLDB_INVALID_ADDRESS) 276254721Semaste { 277254721Semaste // Section isn't resolved, we can't return a valid file address 278254721Semaste return LLDB_INVALID_ADDRESS; 279254721Semaste } 280254721Semaste // We have a valid file range, so we can return the file based 281254721Semaste // address by adding the file base address to our offset 282254721Semaste return sect_file_addr + m_offset; 283254721Semaste } 284263367Semaste else if (SectionWasDeletedPrivate()) 285263363Semaste { 286263363Semaste // Used to have a valid section but it got deleted so the 287263363Semaste // offset doesn't mean anything without the section 288263363Semaste return LLDB_INVALID_ADDRESS; 289263363Semaste } 290254721Semaste // No section, we just return the offset since it is the value in this case 291254721Semaste return m_offset; 292254721Semaste} 293254721Semaste 294254721Semasteaddr_t 295254721SemasteAddress::GetLoadAddress (Target *target) const 296254721Semaste{ 297254721Semaste SectionSP section_sp (GetSection()); 298263363Semaste if (section_sp) 299254721Semaste { 300263363Semaste if (target) 301263363Semaste { 302263363Semaste addr_t sect_load_addr = section_sp->GetLoadBaseAddress (target); 303254721Semaste 304263363Semaste if (sect_load_addr != LLDB_INVALID_ADDRESS) 305263363Semaste { 306263363Semaste // We have a valid file range, so we can return the file based 307263363Semaste // address by adding the file base address to our offset 308263363Semaste return sect_load_addr + m_offset; 309263363Semaste } 310254721Semaste } 311254721Semaste } 312263367Semaste else if (SectionWasDeletedPrivate()) 313263363Semaste { 314263363Semaste // Used to have a valid section but it got deleted so the 315263363Semaste // offset doesn't mean anything without the section 316263363Semaste return LLDB_INVALID_ADDRESS; 317263363Semaste } 318263363Semaste else 319263363Semaste { 320263363Semaste // We don't have a section so the offset is the load address 321263363Semaste return m_offset; 322263363Semaste } 323263363Semaste // The section isn't resolved or an invalid target was passed in 324263363Semaste // so we can't return a valid load address. 325254721Semaste return LLDB_INVALID_ADDRESS; 326254721Semaste} 327254721Semaste 328254721Semasteaddr_t 329254721SemasteAddress::GetCallableLoadAddress (Target *target, bool is_indirect) const 330254721Semaste{ 331269024Semaste addr_t code_addr = LLDB_INVALID_ADDRESS; 332269024Semaste 333269024Semaste if (is_indirect && target) 334269024Semaste { 335254721Semaste ProcessSP processSP = target->GetProcessSP(); 336254721Semaste Error error; 337254721Semaste if (processSP.get()) 338269024Semaste { 339269024Semaste code_addr = processSP->ResolveIndirectFunction(this, error); 340269024Semaste if (!error.Success()) 341269024Semaste code_addr = LLDB_INVALID_ADDRESS; 342269024Semaste } 343254721Semaste } 344269024Semaste else 345269024Semaste { 346269024Semaste code_addr = GetLoadAddress (target); 347269024Semaste } 348269024Semaste 349269024Semaste if (code_addr == LLDB_INVALID_ADDRESS) 350269024Semaste return code_addr; 351269024Semaste 352254721Semaste if (target) 353254721Semaste return target->GetCallableLoadAddress (code_addr, GetAddressClass()); 354254721Semaste return code_addr; 355254721Semaste} 356254721Semaste 357254721Semastebool 358254721SemasteAddress::SetCallableLoadAddress (lldb::addr_t load_addr, Target *target) 359254721Semaste{ 360254721Semaste if (SetLoadAddress (load_addr, target)) 361254721Semaste { 362254721Semaste if (target) 363254721Semaste m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass()); 364254721Semaste return true; 365254721Semaste } 366254721Semaste return false; 367254721Semaste} 368254721Semaste 369254721Semasteaddr_t 370254721SemasteAddress::GetOpcodeLoadAddress (Target *target) const 371254721Semaste{ 372254721Semaste addr_t code_addr = GetLoadAddress (target); 373254721Semaste if (code_addr != LLDB_INVALID_ADDRESS) 374254721Semaste code_addr = target->GetOpcodeLoadAddress (code_addr, GetAddressClass()); 375254721Semaste return code_addr; 376254721Semaste} 377254721Semaste 378254721Semastebool 379254721SemasteAddress::SetOpcodeLoadAddress (lldb::addr_t load_addr, Target *target) 380254721Semaste{ 381254721Semaste if (SetLoadAddress (load_addr, target)) 382254721Semaste { 383254721Semaste if (target) 384254721Semaste m_offset = target->GetOpcodeLoadAddress (m_offset, GetAddressClass()); 385254721Semaste return true; 386254721Semaste } 387254721Semaste return false; 388254721Semaste} 389254721Semaste 390254721Semastebool 391254721SemasteAddress::Dump (Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style, uint32_t addr_size) const 392254721Semaste{ 393254721Semaste // If the section was NULL, only load address is going to work unless we are 394254721Semaste // trying to deref a pointer 395254721Semaste SectionSP section_sp (GetSection()); 396254721Semaste if (!section_sp && style != DumpStyleResolvedPointerDescription) 397254721Semaste style = DumpStyleLoadAddress; 398254721Semaste 399254721Semaste ExecutionContext exe_ctx (exe_scope); 400254721Semaste Target *target = exe_ctx.GetTargetPtr(); 401254721Semaste // If addr_byte_size is UINT32_MAX, then determine the correct address 402254721Semaste // byte size for the process or default to the size of addr_t 403254721Semaste if (addr_size == UINT32_MAX) 404254721Semaste { 405254721Semaste if (target) 406254721Semaste addr_size = target->GetArchitecture().GetAddressByteSize (); 407254721Semaste else 408254721Semaste addr_size = sizeof(addr_t); 409254721Semaste } 410254721Semaste 411254721Semaste Address so_addr; 412254721Semaste switch (style) 413254721Semaste { 414254721Semaste case DumpStyleInvalid: 415254721Semaste return false; 416254721Semaste 417254721Semaste case DumpStyleSectionNameOffset: 418254721Semaste if (section_sp) 419254721Semaste { 420254721Semaste section_sp->DumpName(s); 421254721Semaste s->Printf (" + %" PRIu64, m_offset.load()); 422254721Semaste } 423254721Semaste else 424254721Semaste { 425254721Semaste s->Address(m_offset, addr_size); 426254721Semaste } 427254721Semaste break; 428254721Semaste 429254721Semaste case DumpStyleSectionPointerOffset: 430254721Semaste s->Printf("(Section *)%p + ", section_sp.get()); 431254721Semaste s->Address(m_offset, addr_size); 432254721Semaste break; 433254721Semaste 434254721Semaste case DumpStyleModuleWithFileAddress: 435254721Semaste if (section_sp) 436254721Semaste s->Printf("%s[", section_sp->GetModule()->GetFileSpec().GetFilename().AsCString()); 437254721Semaste // Fall through 438254721Semaste case DumpStyleFileAddress: 439254721Semaste { 440254721Semaste addr_t file_addr = GetFileAddress(); 441254721Semaste if (file_addr == LLDB_INVALID_ADDRESS) 442254721Semaste { 443254721Semaste if (fallback_style != DumpStyleInvalid) 444254721Semaste return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 445254721Semaste return false; 446254721Semaste } 447254721Semaste s->Address (file_addr, addr_size); 448254721Semaste if (style == DumpStyleModuleWithFileAddress && section_sp) 449254721Semaste s->PutChar(']'); 450254721Semaste } 451254721Semaste break; 452254721Semaste 453254721Semaste case DumpStyleLoadAddress: 454254721Semaste { 455254721Semaste addr_t load_addr = GetLoadAddress (target); 456254721Semaste if (load_addr == LLDB_INVALID_ADDRESS) 457254721Semaste { 458254721Semaste if (fallback_style != DumpStyleInvalid) 459254721Semaste return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 460254721Semaste return false; 461254721Semaste } 462254721Semaste s->Address (load_addr, addr_size); 463254721Semaste } 464254721Semaste break; 465254721Semaste 466254721Semaste case DumpStyleResolvedDescription: 467254721Semaste case DumpStyleResolvedDescriptionNoModule: 468254721Semaste if (IsSectionOffset()) 469254721Semaste { 470254721Semaste uint32_t pointer_size = 4; 471254721Semaste ModuleSP module_sp (GetModule()); 472254721Semaste if (target) 473254721Semaste pointer_size = target->GetArchitecture().GetAddressByteSize(); 474254721Semaste else if (module_sp) 475254721Semaste pointer_size = module_sp->GetArchitecture().GetAddressByteSize(); 476254721Semaste 477254721Semaste bool showed_info = false; 478254721Semaste if (section_sp) 479254721Semaste { 480254721Semaste SectionType sect_type = section_sp->GetType(); 481254721Semaste switch (sect_type) 482254721Semaste { 483254721Semaste case eSectionTypeData: 484254721Semaste if (module_sp) 485254721Semaste { 486254721Semaste SymbolVendor *sym_vendor = module_sp->GetSymbolVendor(); 487254721Semaste if (sym_vendor) 488254721Semaste { 489254721Semaste Symtab *symtab = sym_vendor->GetSymtab(); 490254721Semaste if (symtab) 491254721Semaste { 492254721Semaste const addr_t file_Addr = GetFileAddress(); 493254721Semaste Symbol *symbol = symtab->FindSymbolContainingFileAddress (file_Addr); 494254721Semaste if (symbol) 495254721Semaste { 496254721Semaste const char *symbol_name = symbol->GetName().AsCString(); 497254721Semaste if (symbol_name) 498254721Semaste { 499254721Semaste s->PutCString(symbol_name); 500254721Semaste addr_t delta = file_Addr - symbol->GetAddress().GetFileAddress(); 501254721Semaste if (delta) 502254721Semaste s->Printf(" + %" PRIu64, delta); 503254721Semaste showed_info = true; 504254721Semaste } 505254721Semaste } 506254721Semaste } 507254721Semaste } 508254721Semaste } 509254721Semaste break; 510254721Semaste 511254721Semaste case eSectionTypeDataCString: 512254721Semaste // Read the C string from memory and display it 513254721Semaste showed_info = true; 514254721Semaste ReadCStringFromMemory (exe_scope, *this, s); 515254721Semaste break; 516254721Semaste 517254721Semaste case eSectionTypeDataCStringPointers: 518254721Semaste { 519254721Semaste if (ReadAddress (exe_scope, *this, pointer_size, so_addr)) 520254721Semaste { 521254721Semaste#if VERBOSE_OUTPUT 522254721Semaste s->PutCString("(char *)"); 523254721Semaste so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress); 524254721Semaste s->PutCString(": "); 525254721Semaste#endif 526254721Semaste showed_info = true; 527254721Semaste ReadCStringFromMemory (exe_scope, so_addr, s); 528254721Semaste } 529254721Semaste } 530254721Semaste break; 531254721Semaste 532254721Semaste case eSectionTypeDataObjCMessageRefs: 533254721Semaste { 534254721Semaste if (ReadAddress (exe_scope, *this, pointer_size, so_addr)) 535254721Semaste { 536254721Semaste if (target && so_addr.IsSectionOffset()) 537254721Semaste { 538254721Semaste SymbolContext func_sc; 539254721Semaste target->GetImages().ResolveSymbolContextForAddress (so_addr, 540254721Semaste eSymbolContextEverything, 541254721Semaste func_sc); 542254721Semaste if (func_sc.function || func_sc.symbol) 543254721Semaste { 544254721Semaste showed_info = true; 545254721Semaste#if VERBOSE_OUTPUT 546254721Semaste s->PutCString ("(objc_msgref *) -> { (func*)"); 547254721Semaste so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress); 548254721Semaste#else 549254721Semaste s->PutCString ("{ "); 550254721Semaste#endif 551254721Semaste Address cstr_addr(*this); 552254721Semaste cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size); 553254721Semaste func_sc.DumpStopContext(s, exe_scope, so_addr, true, true, false); 554254721Semaste if (ReadAddress (exe_scope, cstr_addr, pointer_size, so_addr)) 555254721Semaste { 556254721Semaste#if VERBOSE_OUTPUT 557254721Semaste s->PutCString("), (char *)"); 558254721Semaste so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress); 559254721Semaste s->PutCString(" ("); 560254721Semaste#else 561254721Semaste s->PutCString(", "); 562254721Semaste#endif 563254721Semaste ReadCStringFromMemory (exe_scope, so_addr, s); 564254721Semaste } 565254721Semaste#if VERBOSE_OUTPUT 566254721Semaste s->PutCString(") }"); 567254721Semaste#else 568254721Semaste s->PutCString(" }"); 569254721Semaste#endif 570254721Semaste } 571254721Semaste } 572254721Semaste } 573254721Semaste } 574254721Semaste break; 575254721Semaste 576254721Semaste case eSectionTypeDataObjCCFStrings: 577254721Semaste { 578254721Semaste Address cfstring_data_addr(*this); 579254721Semaste cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() + (2 * pointer_size)); 580254721Semaste if (ReadAddress (exe_scope, cfstring_data_addr, pointer_size, so_addr)) 581254721Semaste { 582254721Semaste#if VERBOSE_OUTPUT 583254721Semaste s->PutCString("(CFString *) "); 584254721Semaste cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress); 585254721Semaste s->PutCString(" -> @"); 586254721Semaste#else 587254721Semaste s->PutChar('@'); 588254721Semaste#endif 589254721Semaste if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription)) 590254721Semaste showed_info = true; 591254721Semaste } 592254721Semaste } 593254721Semaste break; 594254721Semaste 595254721Semaste case eSectionTypeData4: 596254721Semaste // Read the 4 byte data and display it 597254721Semaste showed_info = true; 598254721Semaste s->PutCString("(uint32_t) "); 599254721Semaste DumpUInt (exe_scope, *this, 4, s); 600254721Semaste break; 601254721Semaste 602254721Semaste case eSectionTypeData8: 603254721Semaste // Read the 8 byte data and display it 604254721Semaste showed_info = true; 605254721Semaste s->PutCString("(uint64_t) "); 606254721Semaste DumpUInt (exe_scope, *this, 8, s); 607254721Semaste break; 608254721Semaste 609254721Semaste case eSectionTypeData16: 610254721Semaste // Read the 16 byte data and display it 611254721Semaste showed_info = true; 612254721Semaste s->PutCString("(uint128_t) "); 613254721Semaste DumpUInt (exe_scope, *this, 16, s); 614254721Semaste break; 615254721Semaste 616254721Semaste case eSectionTypeDataPointers: 617254721Semaste // Read the pointer data and display it 618254721Semaste { 619254721Semaste if (ReadAddress (exe_scope, *this, pointer_size, so_addr)) 620254721Semaste { 621254721Semaste s->PutCString ("(void *)"); 622254721Semaste so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress); 623254721Semaste 624254721Semaste showed_info = true; 625254721Semaste if (so_addr.IsSectionOffset()) 626254721Semaste { 627254721Semaste SymbolContext pointer_sc; 628254721Semaste if (target) 629254721Semaste { 630254721Semaste target->GetImages().ResolveSymbolContextForAddress (so_addr, 631254721Semaste eSymbolContextEverything, 632254721Semaste pointer_sc); 633254721Semaste if (pointer_sc.function || pointer_sc.symbol) 634254721Semaste { 635254721Semaste s->PutCString(": "); 636254721Semaste pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false, false); 637254721Semaste } 638254721Semaste } 639254721Semaste } 640254721Semaste } 641254721Semaste } 642254721Semaste break; 643254721Semaste 644254721Semaste default: 645254721Semaste break; 646254721Semaste } 647254721Semaste } 648254721Semaste 649254721Semaste if (!showed_info) 650254721Semaste { 651254721Semaste if (module_sp) 652254721Semaste { 653254721Semaste SymbolContext sc; 654254721Semaste module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextEverything, sc); 655254721Semaste if (sc.function || sc.symbol) 656254721Semaste { 657254721Semaste bool show_stop_context = true; 658254721Semaste const bool show_module = (style == DumpStyleResolvedDescription); 659254721Semaste const bool show_fullpaths = false; 660254721Semaste const bool show_inlined_frames = true; 661254721Semaste if (sc.function == NULL && sc.symbol != NULL) 662254721Semaste { 663254721Semaste // If we have just a symbol make sure it is in the right section 664254721Semaste if (sc.symbol->ValueIsAddress()) 665254721Semaste { 666254721Semaste if (sc.symbol->GetAddress().GetSection() != GetSection()) 667254721Semaste { 668254721Semaste // don't show the module if the symbol is a trampoline symbol 669254721Semaste show_stop_context = false; 670254721Semaste } 671254721Semaste } 672254721Semaste } 673254721Semaste if (show_stop_context) 674254721Semaste { 675254721Semaste // We have a function or a symbol from the same 676254721Semaste // sections as this address. 677254721Semaste sc.DumpStopContext (s, 678254721Semaste exe_scope, 679254721Semaste *this, 680254721Semaste show_fullpaths, 681254721Semaste show_module, 682254721Semaste show_inlined_frames); 683254721Semaste } 684254721Semaste else 685254721Semaste { 686254721Semaste // We found a symbol but it was in a different 687254721Semaste // section so it isn't the symbol we should be 688254721Semaste // showing, just show the section name + offset 689254721Semaste Dump (s, exe_scope, DumpStyleSectionNameOffset); 690254721Semaste } 691254721Semaste } 692254721Semaste } 693254721Semaste } 694254721Semaste } 695254721Semaste else 696254721Semaste { 697254721Semaste if (fallback_style != DumpStyleInvalid) 698254721Semaste return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 699254721Semaste return false; 700254721Semaste } 701254721Semaste break; 702254721Semaste 703254721Semaste case DumpStyleDetailedSymbolContext: 704254721Semaste if (IsSectionOffset()) 705254721Semaste { 706254721Semaste ModuleSP module_sp (GetModule()); 707254721Semaste if (module_sp) 708254721Semaste { 709254721Semaste SymbolContext sc; 710254721Semaste module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextEverything, sc); 711254721Semaste if (sc.symbol) 712254721Semaste { 713254721Semaste // If we have just a symbol make sure it is in the same section 714254721Semaste // as our address. If it isn't, then we might have just found 715254721Semaste // the last symbol that came before the address that we are 716254721Semaste // looking up that has nothing to do with our address lookup. 717254721Semaste if (sc.symbol->ValueIsAddress() && sc.symbol->GetAddress().GetSection() != GetSection()) 718254721Semaste sc.symbol = NULL; 719254721Semaste } 720254721Semaste sc.GetDescription(s, eDescriptionLevelBrief, target); 721254721Semaste 722254721Semaste if (sc.block) 723254721Semaste { 724254721Semaste bool can_create = true; 725254721Semaste bool get_parent_variables = true; 726254721Semaste bool stop_if_block_is_inlined_function = false; 727254721Semaste VariableList variable_list; 728254721Semaste sc.block->AppendVariables (can_create, 729254721Semaste get_parent_variables, 730254721Semaste stop_if_block_is_inlined_function, 731254721Semaste &variable_list); 732254721Semaste 733254721Semaste const size_t num_variables = variable_list.GetSize(); 734254721Semaste for (size_t var_idx = 0; var_idx < num_variables; ++var_idx) 735254721Semaste { 736254721Semaste Variable *var = variable_list.GetVariableAtIndex (var_idx).get(); 737254721Semaste if (var && var->LocationIsValidForAddress (*this)) 738254721Semaste { 739254721Semaste s->Indent(); 740254721Semaste s->Printf (" Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\", type= \"%s\", location =", 741254721Semaste var->GetID(), 742254721Semaste var->GetName().GetCString(), 743254721Semaste var->GetType()->GetName().GetCString()); 744254721Semaste var->DumpLocationForAddress(s, *this); 745254721Semaste s->PutCString(", decl = "); 746254721Semaste var->GetDeclaration().DumpStopContext(s, false); 747254721Semaste s->EOL(); 748254721Semaste } 749254721Semaste } 750254721Semaste } 751254721Semaste } 752254721Semaste } 753254721Semaste else 754254721Semaste { 755254721Semaste if (fallback_style != DumpStyleInvalid) 756254721Semaste return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 757254721Semaste return false; 758254721Semaste } 759254721Semaste break; 760254721Semaste case DumpStyleResolvedPointerDescription: 761254721Semaste { 762254721Semaste Process *process = exe_ctx.GetProcessPtr(); 763254721Semaste if (process) 764254721Semaste { 765254721Semaste addr_t load_addr = GetLoadAddress (target); 766254721Semaste if (load_addr != LLDB_INVALID_ADDRESS) 767254721Semaste { 768254721Semaste Error memory_error; 769254721Semaste addr_t dereferenced_load_addr = process->ReadPointerFromMemory(load_addr, memory_error); 770254721Semaste if (dereferenced_load_addr != LLDB_INVALID_ADDRESS) 771254721Semaste { 772254721Semaste Address dereferenced_addr; 773254721Semaste if (dereferenced_addr.SetLoadAddress(dereferenced_load_addr, target)) 774254721Semaste { 775254721Semaste StreamString strm; 776254721Semaste if (dereferenced_addr.Dump (&strm, exe_scope, DumpStyleResolvedDescription, DumpStyleInvalid, addr_size)) 777254721Semaste { 778254721Semaste s->Address (dereferenced_load_addr, addr_size, " -> ", " "); 779254721Semaste s->Write(strm.GetData(), strm.GetSize()); 780254721Semaste return true; 781254721Semaste } 782254721Semaste } 783254721Semaste } 784254721Semaste } 785254721Semaste } 786254721Semaste if (fallback_style != DumpStyleInvalid) 787254721Semaste return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 788254721Semaste return false; 789254721Semaste } 790254721Semaste break; 791254721Semaste } 792254721Semaste 793254721Semaste return true; 794254721Semaste} 795254721Semaste 796263363Semastebool 797263363SemasteAddress::SectionWasDeleted() const 798263363Semaste{ 799263367Semaste if (GetSection()) 800263367Semaste return false; 801263367Semaste return SectionWasDeletedPrivate(); 802263367Semaste} 803263367Semaste 804263367Semastebool 805263367SemasteAddress::SectionWasDeletedPrivate() const 806263367Semaste{ 807263363Semaste lldb::SectionWP empty_section_wp; 808263363Semaste 809263363Semaste // If either call to "std::weak_ptr::owner_before(...) value returns true, this 810263363Semaste // indicates that m_section_wp once contained (possibly still does) a reference 811263363Semaste // to a valid shared pointer. This helps us know if we had a valid reference to 812263363Semaste // a section which is now invalid because the module it was in was unloaded/deleted, 813263363Semaste // or if the address doesn't have a valid reference to a section. 814263363Semaste return empty_section_wp.owner_before(m_section_wp) || m_section_wp.owner_before(empty_section_wp); 815263363Semaste} 816263363Semaste 817254721Semasteuint32_t 818254721SemasteAddress::CalculateSymbolContext (SymbolContext *sc, uint32_t resolve_scope) const 819254721Semaste{ 820254721Semaste sc->Clear(false); 821254721Semaste // Absolute addresses don't have enough information to reconstruct even their target. 822254721Semaste 823254721Semaste SectionSP section_sp (GetSection()); 824254721Semaste if (section_sp) 825254721Semaste { 826254721Semaste ModuleSP module_sp (section_sp->GetModule()); 827254721Semaste if (module_sp) 828254721Semaste { 829254721Semaste sc->module_sp = module_sp; 830254721Semaste if (sc->module_sp) 831254721Semaste return sc->module_sp->ResolveSymbolContextForAddress (*this, resolve_scope, *sc); 832254721Semaste } 833254721Semaste } 834254721Semaste return 0; 835254721Semaste} 836254721Semaste 837254721SemasteModuleSP 838254721SemasteAddress::CalculateSymbolContextModule () const 839254721Semaste{ 840254721Semaste SectionSP section_sp (GetSection()); 841254721Semaste if (section_sp) 842254721Semaste return section_sp->GetModule(); 843254721Semaste return ModuleSP(); 844254721Semaste} 845254721Semaste 846254721SemasteCompileUnit * 847254721SemasteAddress::CalculateSymbolContextCompileUnit () const 848254721Semaste{ 849254721Semaste SectionSP section_sp (GetSection()); 850254721Semaste if (section_sp) 851254721Semaste { 852254721Semaste SymbolContext sc; 853254721Semaste sc.module_sp = section_sp->GetModule(); 854254721Semaste if (sc.module_sp) 855254721Semaste { 856254721Semaste sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextCompUnit, sc); 857254721Semaste return sc.comp_unit; 858254721Semaste } 859254721Semaste } 860254721Semaste return NULL; 861254721Semaste} 862254721Semaste 863254721SemasteFunction * 864254721SemasteAddress::CalculateSymbolContextFunction () const 865254721Semaste{ 866254721Semaste SectionSP section_sp (GetSection()); 867254721Semaste if (section_sp) 868254721Semaste { 869254721Semaste SymbolContext sc; 870254721Semaste sc.module_sp = section_sp->GetModule(); 871254721Semaste if (sc.module_sp) 872254721Semaste { 873254721Semaste sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextFunction, sc); 874254721Semaste return sc.function; 875254721Semaste } 876254721Semaste } 877254721Semaste return NULL; 878254721Semaste} 879254721Semaste 880254721SemasteBlock * 881254721SemasteAddress::CalculateSymbolContextBlock () const 882254721Semaste{ 883254721Semaste SectionSP section_sp (GetSection()); 884254721Semaste if (section_sp) 885254721Semaste { 886254721Semaste SymbolContext sc; 887254721Semaste sc.module_sp = section_sp->GetModule(); 888254721Semaste if (sc.module_sp) 889254721Semaste { 890254721Semaste sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextBlock, sc); 891254721Semaste return sc.block; 892254721Semaste } 893254721Semaste } 894254721Semaste return NULL; 895254721Semaste} 896254721Semaste 897254721SemasteSymbol * 898254721SemasteAddress::CalculateSymbolContextSymbol () const 899254721Semaste{ 900254721Semaste SectionSP section_sp (GetSection()); 901254721Semaste if (section_sp) 902254721Semaste { 903254721Semaste SymbolContext sc; 904254721Semaste sc.module_sp = section_sp->GetModule(); 905254721Semaste if (sc.module_sp) 906254721Semaste { 907254721Semaste sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextSymbol, sc); 908254721Semaste return sc.symbol; 909254721Semaste } 910254721Semaste } 911254721Semaste return NULL; 912254721Semaste} 913254721Semaste 914254721Semastebool 915254721SemasteAddress::CalculateSymbolContextLineEntry (LineEntry &line_entry) const 916254721Semaste{ 917254721Semaste SectionSP section_sp (GetSection()); 918254721Semaste if (section_sp) 919254721Semaste { 920254721Semaste SymbolContext sc; 921254721Semaste sc.module_sp = section_sp->GetModule(); 922254721Semaste if (sc.module_sp) 923254721Semaste { 924254721Semaste sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextLineEntry, sc); 925254721Semaste if (sc.line_entry.IsValid()) 926254721Semaste { 927254721Semaste line_entry = sc.line_entry; 928254721Semaste return true; 929254721Semaste } 930254721Semaste } 931254721Semaste } 932254721Semaste line_entry.Clear(); 933254721Semaste return false; 934254721Semaste} 935254721Semaste 936254721Semasteint 937254721SemasteAddress::CompareFileAddress (const Address& a, const Address& b) 938254721Semaste{ 939254721Semaste addr_t a_file_addr = a.GetFileAddress(); 940254721Semaste addr_t b_file_addr = b.GetFileAddress(); 941254721Semaste if (a_file_addr < b_file_addr) 942254721Semaste return -1; 943254721Semaste if (a_file_addr > b_file_addr) 944254721Semaste return +1; 945254721Semaste return 0; 946254721Semaste} 947254721Semaste 948254721Semaste 949254721Semasteint 950254721SemasteAddress::CompareLoadAddress (const Address& a, const Address& b, Target *target) 951254721Semaste{ 952254721Semaste assert (target != NULL); 953254721Semaste addr_t a_load_addr = a.GetLoadAddress (target); 954254721Semaste addr_t b_load_addr = b.GetLoadAddress (target); 955254721Semaste if (a_load_addr < b_load_addr) 956254721Semaste return -1; 957254721Semaste if (a_load_addr > b_load_addr) 958254721Semaste return +1; 959254721Semaste return 0; 960254721Semaste} 961254721Semaste 962254721Semasteint 963254721SemasteAddress::CompareModulePointerAndOffset (const Address& a, const Address& b) 964254721Semaste{ 965254721Semaste ModuleSP a_module_sp (a.GetModule()); 966254721Semaste ModuleSP b_module_sp (b.GetModule()); 967254721Semaste Module *a_module = a_module_sp.get(); 968254721Semaste Module *b_module = b_module_sp.get(); 969254721Semaste if (a_module < b_module) 970254721Semaste return -1; 971254721Semaste if (a_module > b_module) 972254721Semaste return +1; 973254721Semaste // Modules are the same, just compare the file address since they should 974254721Semaste // be unique 975254721Semaste addr_t a_file_addr = a.GetFileAddress(); 976254721Semaste addr_t b_file_addr = b.GetFileAddress(); 977254721Semaste if (a_file_addr < b_file_addr) 978254721Semaste return -1; 979254721Semaste if (a_file_addr > b_file_addr) 980254721Semaste return +1; 981254721Semaste return 0; 982254721Semaste} 983254721Semaste 984254721Semaste 985254721Semastesize_t 986254721SemasteAddress::MemorySize () const 987254721Semaste{ 988254721Semaste // Noting special for the memory size of a single Address object, 989254721Semaste // it is just the size of itself. 990254721Semaste return sizeof(Address); 991254721Semaste} 992254721Semaste 993254721Semaste 994254721Semaste//---------------------------------------------------------------------- 995254721Semaste// NOTE: Be careful using this operator. It can correctly compare two 996254721Semaste// addresses from the same Module correctly. It can't compare two 997254721Semaste// addresses from different modules in any meaningful way, but it will 998254721Semaste// compare the module pointers. 999254721Semaste// 1000254721Semaste// To sum things up: 1001254721Semaste// - works great for addresses within the same module 1002254721Semaste// - it works for addresses across multiple modules, but don't expect the 1003254721Semaste// address results to make much sense 1004254721Semaste// 1005254721Semaste// This basically lets Address objects be used in ordered collection 1006254721Semaste// classes. 1007254721Semaste//---------------------------------------------------------------------- 1008254721Semaste 1009254721Semastebool 1010254721Semastelldb_private::operator< (const Address& lhs, const Address& rhs) 1011254721Semaste{ 1012254721Semaste ModuleSP lhs_module_sp (lhs.GetModule()); 1013254721Semaste ModuleSP rhs_module_sp (rhs.GetModule()); 1014254721Semaste Module *lhs_module = lhs_module_sp.get(); 1015254721Semaste Module *rhs_module = rhs_module_sp.get(); 1016254721Semaste if (lhs_module == rhs_module) 1017254721Semaste { 1018254721Semaste // Addresses are in the same module, just compare the file addresses 1019254721Semaste return lhs.GetFileAddress() < rhs.GetFileAddress(); 1020254721Semaste } 1021254721Semaste else 1022254721Semaste { 1023254721Semaste // The addresses are from different modules, just use the module 1024254721Semaste // pointer value to get consistent ordering 1025254721Semaste return lhs_module < rhs_module; 1026254721Semaste } 1027254721Semaste} 1028254721Semaste 1029254721Semastebool 1030254721Semastelldb_private::operator> (const Address& lhs, const Address& rhs) 1031254721Semaste{ 1032254721Semaste ModuleSP lhs_module_sp (lhs.GetModule()); 1033254721Semaste ModuleSP rhs_module_sp (rhs.GetModule()); 1034254721Semaste Module *lhs_module = lhs_module_sp.get(); 1035254721Semaste Module *rhs_module = rhs_module_sp.get(); 1036254721Semaste if (lhs_module == rhs_module) 1037254721Semaste { 1038254721Semaste // Addresses are in the same module, just compare the file addresses 1039254721Semaste return lhs.GetFileAddress() > rhs.GetFileAddress(); 1040254721Semaste } 1041254721Semaste else 1042254721Semaste { 1043254721Semaste // The addresses are from different modules, just use the module 1044254721Semaste // pointer value to get consistent ordering 1045254721Semaste return lhs_module > rhs_module; 1046254721Semaste } 1047254721Semaste} 1048254721Semaste 1049254721Semaste 1050254721Semaste// The operator == checks for exact equality only (same section, same offset) 1051254721Semastebool 1052254721Semastelldb_private::operator== (const Address& a, const Address& rhs) 1053254721Semaste{ 1054254721Semaste return a.GetOffset() == rhs.GetOffset() && 1055254721Semaste a.GetSection() == rhs.GetSection(); 1056254721Semaste} 1057254721Semaste// The operator != checks for exact inequality only (differing section, or 1058254721Semaste// different offset) 1059254721Semastebool 1060254721Semastelldb_private::operator!= (const Address& a, const Address& rhs) 1061254721Semaste{ 1062254721Semaste return a.GetOffset() != rhs.GetOffset() || 1063254721Semaste a.GetSection() != rhs.GetSection(); 1064254721Semaste} 1065254721Semaste 1066254721SemasteAddressClass 1067254721SemasteAddress::GetAddressClass () const 1068254721Semaste{ 1069254721Semaste ModuleSP module_sp (GetModule()); 1070254721Semaste if (module_sp) 1071254721Semaste { 1072254721Semaste ObjectFile *obj_file = module_sp->GetObjectFile(); 1073254721Semaste if (obj_file) 1074254721Semaste { 1075254721Semaste // Give the symbol vendor a chance to add to the unified section list. 1076254721Semaste module_sp->GetSymbolVendor(); 1077254721Semaste return obj_file->GetAddressClass (GetFileAddress()); 1078254721Semaste } 1079254721Semaste } 1080254721Semaste return eAddressClassUnknown; 1081254721Semaste} 1082254721Semaste 1083254721Semastebool 1084254721SemasteAddress::SetLoadAddress (lldb::addr_t load_addr, Target *target) 1085254721Semaste{ 1086254721Semaste if (target && target->GetSectionLoadList().ResolveLoadAddress(load_addr, *this)) 1087254721Semaste return true; 1088254721Semaste m_section_wp.reset(); 1089254721Semaste m_offset = load_addr; 1090254721Semaste return false; 1091254721Semaste} 1092254721Semaste 1093