130fdc8d8SChris Lattner //===-- SBModule.cpp --------------------------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 330fdc8d8SChris Lattner // The LLVM Compiler Infrastructure 430fdc8d8SChris Lattner // 530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source 630fdc8d8SChris Lattner // License. See LICENSE.TXT for details. 730fdc8d8SChris Lattner // 830fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 930fdc8d8SChris Lattner 1030fdc8d8SChris Lattner #include "lldb/API/SBModule.h" 1109960031SGreg Clayton #include "lldb/API/SBAddress.h" 1209960031SGreg Clayton #include "lldb/API/SBFileSpec.h" 13226cce25SGreg Clayton #include "lldb/API/SBModuleSpec.h" 14c9660546SGreg Clayton #include "lldb/API/SBProcess.h" 15dde9cff3SCaroline Tice #include "lldb/API/SBStream.h" 16fe356d35SGreg Clayton #include "lldb/API/SBSymbolContextList.h" 1730fdc8d8SChris Lattner #include "lldb/Core/Module.h" 18ceb6b139SCaroline Tice #include "lldb/Core/Log.h" 191f746071SGreg Clayton #include "lldb/Core/Section.h" 2038adbbb8SGreg Clayton #include "lldb/Core/StreamString.h" 21dea8cb4fSGreg Clayton #include "lldb/Core/ValueObjectList.h" 22dea8cb4fSGreg Clayton #include "lldb/Core/ValueObjectVariable.h" 231f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h" 24*ae088e52SGreg Clayton #include "lldb/Symbol/SymbolFile.h" 256f3533fbSEnrico Granata #include "lldb/Symbol/SymbolVendor.h" 261f746071SGreg Clayton #include "lldb/Symbol/Symtab.h" 2756939cb3SGreg Clayton #include "lldb/Symbol/TypeSystem.h" 28dea8cb4fSGreg Clayton #include "lldb/Symbol/VariableList.h" 29dea8cb4fSGreg Clayton #include "lldb/Target/Target.h" 3030fdc8d8SChris Lattner 3130fdc8d8SChris Lattner using namespace lldb; 32ceb6b139SCaroline Tice using namespace lldb_private; 3330fdc8d8SChris Lattner 3430fdc8d8SChris Lattner 3530fdc8d8SChris Lattner SBModule::SBModule () : 366611103cSGreg Clayton m_opaque_sp () 3730fdc8d8SChris Lattner { 3830fdc8d8SChris Lattner } 3930fdc8d8SChris Lattner 4030fdc8d8SChris Lattner SBModule::SBModule (const lldb::ModuleSP& module_sp) : 416611103cSGreg Clayton m_opaque_sp (module_sp) 4230fdc8d8SChris Lattner { 4330fdc8d8SChris Lattner } 4430fdc8d8SChris Lattner 45226cce25SGreg Clayton SBModule::SBModule(const SBModuleSpec &module_spec) : 46226cce25SGreg Clayton m_opaque_sp () 47226cce25SGreg Clayton { 48226cce25SGreg Clayton ModuleSP module_sp; 49226cce25SGreg Clayton Error error = ModuleList::GetSharedModule (*module_spec.m_opaque_ap, 50226cce25SGreg Clayton module_sp, 51226cce25SGreg Clayton NULL, 52226cce25SGreg Clayton NULL, 53226cce25SGreg Clayton NULL); 54226cce25SGreg Clayton if (module_sp) 55226cce25SGreg Clayton SetSP(module_sp); 56226cce25SGreg Clayton } 57226cce25SGreg Clayton 58efabb123SGreg Clayton SBModule::SBModule(const SBModule &rhs) : 59efabb123SGreg Clayton m_opaque_sp (rhs.m_opaque_sp) 60efabb123SGreg Clayton { 61efabb123SGreg Clayton } 62efabb123SGreg Clayton 63c9660546SGreg Clayton SBModule::SBModule (lldb::SBProcess &process, lldb::addr_t header_addr) : 64c9660546SGreg Clayton m_opaque_sp () 65c9660546SGreg Clayton { 66c9660546SGreg Clayton ProcessSP process_sp (process.GetSP()); 67c9660546SGreg Clayton if (process_sp) 68c859e2d5SGreg Clayton { 6939f7ee86SGreg Clayton m_opaque_sp = process_sp->ReadModuleFromMemory (FileSpec(), header_addr); 7039f7ee86SGreg Clayton if (m_opaque_sp) 7139f7ee86SGreg Clayton { 7239f7ee86SGreg Clayton Target &target = process_sp->GetTarget(); 7339f7ee86SGreg Clayton bool changed = false; 74751caf65SGreg Clayton m_opaque_sp->SetLoadAddress(target, 0, true, changed); 7539f7ee86SGreg Clayton target.GetImages().Append(m_opaque_sp); 7639f7ee86SGreg Clayton } 77c859e2d5SGreg Clayton } 78c9660546SGreg Clayton } 79c9660546SGreg Clayton 80efabb123SGreg Clayton const SBModule & 81efabb123SGreg Clayton SBModule::operator = (const SBModule &rhs) 82efabb123SGreg Clayton { 83efabb123SGreg Clayton if (this != &rhs) 84efabb123SGreg Clayton m_opaque_sp = rhs.m_opaque_sp; 85efabb123SGreg Clayton return *this; 86efabb123SGreg Clayton } 87efabb123SGreg Clayton 8830fdc8d8SChris Lattner SBModule::~SBModule () 8930fdc8d8SChris Lattner { 9030fdc8d8SChris Lattner } 9130fdc8d8SChris Lattner 9230fdc8d8SChris Lattner bool 9330fdc8d8SChris Lattner SBModule::IsValid () const 9430fdc8d8SChris Lattner { 956611103cSGreg Clayton return m_opaque_sp.get() != NULL; 9630fdc8d8SChris Lattner } 9730fdc8d8SChris Lattner 985d3bca4eSJim Ingham void 995d3bca4eSJim Ingham SBModule::Clear() 1005d3bca4eSJim Ingham { 1015d3bca4eSJim Ingham m_opaque_sp.reset(); 1025d3bca4eSJim Ingham } 1035d3bca4eSJim Ingham 10430fdc8d8SChris Lattner SBFileSpec 10530fdc8d8SChris Lattner SBModule::GetFileSpec () const 10630fdc8d8SChris Lattner { 1075160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 108ceb6b139SCaroline Tice 10930fdc8d8SChris Lattner SBFileSpec file_spec; 110c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 111c2ff9318SGreg Clayton if (module_sp) 112c2ff9318SGreg Clayton file_spec.SetFileSpec(module_sp->GetFileSpec()); 113ceb6b139SCaroline Tice 114ceb6b139SCaroline Tice if (log) 115cfd1acedSGreg Clayton log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)", 116324a1036SSaleem Abdulrasool static_cast<void*>(module_sp.get()), 117324a1036SSaleem Abdulrasool static_cast<const void*>(file_spec.get())); 118ceb6b139SCaroline Tice 11930fdc8d8SChris Lattner return file_spec; 12030fdc8d8SChris Lattner } 12130fdc8d8SChris Lattner 1222289fa48SGreg Clayton lldb::SBFileSpec 1232289fa48SGreg Clayton SBModule::GetPlatformFileSpec () const 1242289fa48SGreg Clayton { 1255160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1262289fa48SGreg Clayton 1272289fa48SGreg Clayton SBFileSpec file_spec; 128c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 129c2ff9318SGreg Clayton if (module_sp) 130c2ff9318SGreg Clayton file_spec.SetFileSpec(module_sp->GetPlatformFileSpec()); 1312289fa48SGreg Clayton 1322289fa48SGreg Clayton if (log) 1332289fa48SGreg Clayton log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)", 134324a1036SSaleem Abdulrasool static_cast<void*>(module_sp.get()), 135324a1036SSaleem Abdulrasool static_cast<const void*>(file_spec.get())); 1362289fa48SGreg Clayton 1372289fa48SGreg Clayton return file_spec; 1382289fa48SGreg Clayton } 1392289fa48SGreg Clayton 1402289fa48SGreg Clayton bool 1412289fa48SGreg Clayton SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file) 1422289fa48SGreg Clayton { 1432289fa48SGreg Clayton bool result = false; 1445160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1452289fa48SGreg Clayton 146c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 147c2ff9318SGreg Clayton if (module_sp) 1482289fa48SGreg Clayton { 149c2ff9318SGreg Clayton module_sp->SetPlatformFileSpec(*platform_file); 1502289fa48SGreg Clayton result = true; 1512289fa48SGreg Clayton } 1522289fa48SGreg Clayton 1532289fa48SGreg Clayton if (log) 154b5ad4ec7SGreg Clayton log->Printf ("SBModule(%p)::SetPlatformFileSpec (SBFileSpec(%p (%s)) => %i", 155324a1036SSaleem Abdulrasool static_cast<void*>(module_sp.get()), 156324a1036SSaleem Abdulrasool static_cast<const void*>(platform_file.get()), 157324a1036SSaleem Abdulrasool platform_file->GetPath().c_str(), result); 1582289fa48SGreg Clayton return result; 1592289fa48SGreg Clayton } 1602289fa48SGreg Clayton 161fbb76349SGreg Clayton lldb::SBFileSpec 162fbb76349SGreg Clayton SBModule::GetRemoteInstallFileSpec () 163fbb76349SGreg Clayton { 164fbb76349SGreg Clayton SBFileSpec sb_file_spec; 165fbb76349SGreg Clayton ModuleSP module_sp (GetSP ()); 166fbb76349SGreg Clayton if (module_sp) 167fbb76349SGreg Clayton sb_file_spec.SetFileSpec (module_sp->GetRemoteInstallFileSpec()); 168fbb76349SGreg Clayton return sb_file_spec; 169fbb76349SGreg Clayton } 170fbb76349SGreg Clayton 171fbb76349SGreg Clayton bool 172fbb76349SGreg Clayton SBModule::SetRemoteInstallFileSpec (lldb::SBFileSpec &file) 173fbb76349SGreg Clayton { 174fbb76349SGreg Clayton ModuleSP module_sp (GetSP ()); 175fbb76349SGreg Clayton if (module_sp) 176fbb76349SGreg Clayton { 177fbb76349SGreg Clayton module_sp->SetRemoteInstallFileSpec(file.ref()); 178fbb76349SGreg Clayton return true; 179fbb76349SGreg Clayton } 180fbb76349SGreg Clayton return false; 181fbb76349SGreg Clayton } 1822289fa48SGreg Clayton 1832289fa48SGreg Clayton 18430fdc8d8SChris Lattner const uint8_t * 18530fdc8d8SChris Lattner SBModule::GetUUIDBytes () const 18630fdc8d8SChris Lattner { 1875160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 188ceb6b139SCaroline Tice 1894838131bSGreg Clayton const uint8_t *uuid_bytes = NULL; 190c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 191c2ff9318SGreg Clayton if (module_sp) 192c2ff9318SGreg Clayton uuid_bytes = (const uint8_t *)module_sp->GetUUID().GetBytes(); 193ceb6b139SCaroline Tice 194ceb6b139SCaroline Tice if (log) 1954838131bSGreg Clayton { 1964838131bSGreg Clayton if (uuid_bytes) 1974838131bSGreg Clayton { 1984838131bSGreg Clayton StreamString s; 199c2ff9318SGreg Clayton module_sp->GetUUID().Dump (&s); 200324a1036SSaleem Abdulrasool log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", 201324a1036SSaleem Abdulrasool static_cast<void*>(module_sp.get()), s.GetData()); 2024838131bSGreg Clayton } 2034838131bSGreg Clayton else 204324a1036SSaleem Abdulrasool log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", 205324a1036SSaleem Abdulrasool static_cast<void*>(module_sp.get())); 2064838131bSGreg Clayton } 2074838131bSGreg Clayton return uuid_bytes; 20830fdc8d8SChris Lattner } 20930fdc8d8SChris Lattner 21030fdc8d8SChris Lattner 211df2963edSJohnny Chen const char * 212df2963edSJohnny Chen SBModule::GetUUIDString () const 213df2963edSJohnny Chen { 2145160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 215df2963edSJohnny Chen 216f1be855aSGreg Clayton const char *uuid_cstr = NULL; 217c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 218c2ff9318SGreg Clayton if (module_sp) 219c16b4af0SJason Molenda { 220f1be855aSGreg Clayton // We are going to return a "const char *" value through the public 22158ef391fSBruce Mitchener // API, so we need to constify it so it gets added permanently the 222f1be855aSGreg Clayton // string pool and then we don't need to worry about the lifetime of the 223f1be855aSGreg Clayton // string as it will never go away once it has been put into the ConstString 224f1be855aSGreg Clayton // string pool 225f1be855aSGreg Clayton uuid_cstr = ConstString(module_sp->GetUUID().GetAsString()).GetCString(); 226f1be855aSGreg Clayton } 227f1be855aSGreg Clayton 228f1be855aSGreg Clayton if (uuid_cstr && uuid_cstr[0]) 229f1be855aSGreg Clayton { 230f1be855aSGreg Clayton if (log) 231f1be855aSGreg Clayton log->Printf ("SBModule(%p)::GetUUIDString () => %s", static_cast<void*>(module_sp.get()), uuid_cstr); 232f1be855aSGreg Clayton return uuid_cstr; 233c16b4af0SJason Molenda } 234df2963edSJohnny Chen 235df2963edSJohnny Chen if (log) 236f1be855aSGreg Clayton log->Printf ("SBModule(%p)::GetUUIDString () => NULL", static_cast<void*>(module_sp.get())); 237f1be855aSGreg Clayton return NULL; 238df2963edSJohnny Chen } 239df2963edSJohnny Chen 240df2963edSJohnny Chen 24130fdc8d8SChris Lattner bool 24230fdc8d8SChris Lattner SBModule::operator == (const SBModule &rhs) const 24330fdc8d8SChris Lattner { 2446611103cSGreg Clayton if (m_opaque_sp) 2456611103cSGreg Clayton return m_opaque_sp.get() == rhs.m_opaque_sp.get(); 24630fdc8d8SChris Lattner return false; 24730fdc8d8SChris Lattner } 24830fdc8d8SChris Lattner 24930fdc8d8SChris Lattner bool 25030fdc8d8SChris Lattner SBModule::operator != (const SBModule &rhs) const 25130fdc8d8SChris Lattner { 2526611103cSGreg Clayton if (m_opaque_sp) 2536611103cSGreg Clayton return m_opaque_sp.get() != rhs.m_opaque_sp.get(); 25430fdc8d8SChris Lattner return false; 25530fdc8d8SChris Lattner } 25630fdc8d8SChris Lattner 257acdbe816SGreg Clayton ModuleSP 258acdbe816SGreg Clayton SBModule::GetSP () const 259cac9c5f9SGreg Clayton { 260cac9c5f9SGreg Clayton return m_opaque_sp; 261cac9c5f9SGreg Clayton } 26230fdc8d8SChris Lattner 26330fdc8d8SChris Lattner void 264acdbe816SGreg Clayton SBModule::SetSP (const ModuleSP &module_sp) 26530fdc8d8SChris Lattner { 2666611103cSGreg Clayton m_opaque_sp = module_sp; 26730fdc8d8SChris Lattner } 26830fdc8d8SChris Lattner 269cac9c5f9SGreg Clayton SBAddress 270cac9c5f9SGreg Clayton SBModule::ResolveFileAddress (lldb::addr_t vm_addr) 27109960031SGreg Clayton { 272cac9c5f9SGreg Clayton lldb::SBAddress sb_addr; 273c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 274c2ff9318SGreg Clayton if (module_sp) 275cac9c5f9SGreg Clayton { 276cac9c5f9SGreg Clayton Address addr; 277c2ff9318SGreg Clayton if (module_sp->ResolveFileAddress (vm_addr, addr)) 278cac9c5f9SGreg Clayton sb_addr.ref() = addr; 279cac9c5f9SGreg Clayton } 280cac9c5f9SGreg Clayton return sb_addr; 28109960031SGreg Clayton } 28209960031SGreg Clayton 28309960031SGreg Clayton SBSymbolContext 28409960031SGreg Clayton SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope) 28509960031SGreg Clayton { 28609960031SGreg Clayton SBSymbolContext sb_sc; 287c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 288c2ff9318SGreg Clayton if (module_sp && addr.IsValid()) 289c2ff9318SGreg Clayton module_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc); 29009960031SGreg Clayton return sb_sc; 29109960031SGreg Clayton } 29209960031SGreg Clayton 293dde9cff3SCaroline Tice bool 294dde9cff3SCaroline Tice SBModule::GetDescription (SBStream &description) 295dde9cff3SCaroline Tice { 296da7bc7d0SGreg Clayton Stream &strm = description.ref(); 297da7bc7d0SGreg Clayton 298c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 299c2ff9318SGreg Clayton if (module_sp) 300dde9cff3SCaroline Tice { 301c2ff9318SGreg Clayton module_sp->GetDescription (&strm); 302dde9cff3SCaroline Tice } 303dde9cff3SCaroline Tice else 304da7bc7d0SGreg Clayton strm.PutCString ("No value"); 305dde9cff3SCaroline Tice 306dde9cff3SCaroline Tice return true; 307dde9cff3SCaroline Tice } 308bbdabce2SGreg Clayton 309873a7a4bSJohnny Chen uint32_t 310873a7a4bSJohnny Chen SBModule::GetNumCompileUnits() 311873a7a4bSJohnny Chen { 312873a7a4bSJohnny Chen ModuleSP module_sp (GetSP ()); 313873a7a4bSJohnny Chen if (module_sp) 314873a7a4bSJohnny Chen { 315873a7a4bSJohnny Chen return module_sp->GetNumCompileUnits (); 316873a7a4bSJohnny Chen } 317873a7a4bSJohnny Chen return 0; 318873a7a4bSJohnny Chen } 319873a7a4bSJohnny Chen 320873a7a4bSJohnny Chen SBCompileUnit 321873a7a4bSJohnny Chen SBModule::GetCompileUnitAtIndex (uint32_t index) 322873a7a4bSJohnny Chen { 323873a7a4bSJohnny Chen SBCompileUnit sb_cu; 324873a7a4bSJohnny Chen ModuleSP module_sp (GetSP ()); 325873a7a4bSJohnny Chen if (module_sp) 326873a7a4bSJohnny Chen { 327873a7a4bSJohnny Chen CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex (index); 328873a7a4bSJohnny Chen sb_cu.reset(cu_sp.get()); 329873a7a4bSJohnny Chen } 330873a7a4bSJohnny Chen return sb_cu; 331873a7a4bSJohnny Chen } 332873a7a4bSJohnny Chen 333a7499c98SMichael Sartain static Symtab * 334a7499c98SMichael Sartain GetUnifiedSymbolTable (const lldb::ModuleSP& module_sp) 335a7499c98SMichael Sartain { 336a7499c98SMichael Sartain if (module_sp) 337a7499c98SMichael Sartain { 338a7499c98SMichael Sartain SymbolVendor *symbols = module_sp->GetSymbolVendor(); 339a7499c98SMichael Sartain if (symbols) 340a7499c98SMichael Sartain return symbols->GetSymtab(); 341a7499c98SMichael Sartain } 342a7499c98SMichael Sartain return NULL; 343a7499c98SMichael Sartain } 344a7499c98SMichael Sartain 345bbdabce2SGreg Clayton size_t 346bbdabce2SGreg Clayton SBModule::GetNumSymbols () 347bbdabce2SGreg Clayton { 348c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 349c2ff9318SGreg Clayton if (module_sp) 350bbdabce2SGreg Clayton { 351a7499c98SMichael Sartain Symtab *symtab = GetUnifiedSymbolTable (module_sp); 352bbdabce2SGreg Clayton if (symtab) 353bbdabce2SGreg Clayton return symtab->GetNumSymbols(); 354bbdabce2SGreg Clayton } 355bbdabce2SGreg Clayton return 0; 356bbdabce2SGreg Clayton } 357bbdabce2SGreg Clayton 358bbdabce2SGreg Clayton SBSymbol 359bbdabce2SGreg Clayton SBModule::GetSymbolAtIndex (size_t idx) 360bbdabce2SGreg Clayton { 361bbdabce2SGreg Clayton SBSymbol sb_symbol; 362c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 363a7499c98SMichael Sartain Symtab *symtab = GetUnifiedSymbolTable (module_sp); 364bbdabce2SGreg Clayton if (symtab) 365bbdabce2SGreg Clayton sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx)); 366bbdabce2SGreg Clayton return sb_symbol; 367bbdabce2SGreg Clayton } 368fe356d35SGreg Clayton 369e14e1925SGreg Clayton lldb::SBSymbol 370e14e1925SGreg Clayton SBModule::FindSymbol (const char *name, 371e14e1925SGreg Clayton lldb::SymbolType symbol_type) 372e14e1925SGreg Clayton { 373e14e1925SGreg Clayton SBSymbol sb_symbol; 374e14e1925SGreg Clayton if (name && name[0]) 375e14e1925SGreg Clayton { 376e14e1925SGreg Clayton ModuleSP module_sp (GetSP ()); 377a7499c98SMichael Sartain Symtab *symtab = GetUnifiedSymbolTable (module_sp); 378e14e1925SGreg Clayton if (symtab) 379e14e1925SGreg Clayton sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(ConstString(name), symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny)); 380e14e1925SGreg Clayton } 381e14e1925SGreg Clayton return sb_symbol; 382e14e1925SGreg Clayton } 383e14e1925SGreg Clayton 384e14e1925SGreg Clayton 385e14e1925SGreg Clayton lldb::SBSymbolContextList 386e14e1925SGreg Clayton SBModule::FindSymbols (const char *name, lldb::SymbolType symbol_type) 387e14e1925SGreg Clayton { 388e14e1925SGreg Clayton SBSymbolContextList sb_sc_list; 389e14e1925SGreg Clayton if (name && name[0]) 390e14e1925SGreg Clayton { 391e14e1925SGreg Clayton ModuleSP module_sp (GetSP ()); 392a7499c98SMichael Sartain Symtab *symtab = GetUnifiedSymbolTable (module_sp); 393e14e1925SGreg Clayton if (symtab) 394e14e1925SGreg Clayton { 395e14e1925SGreg Clayton std::vector<uint32_t> matching_symbol_indexes; 396e14e1925SGreg Clayton const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type, matching_symbol_indexes); 397e14e1925SGreg Clayton if (num_matches) 398e14e1925SGreg Clayton { 399e14e1925SGreg Clayton SymbolContext sc; 400e14e1925SGreg Clayton sc.module_sp = module_sp; 401e14e1925SGreg Clayton SymbolContextList &sc_list = *sb_sc_list; 402e14e1925SGreg Clayton for (size_t i=0; i<num_matches; ++i) 403e14e1925SGreg Clayton { 404e14e1925SGreg Clayton sc.symbol = symtab->SymbolAtIndex (matching_symbol_indexes[i]); 405e14e1925SGreg Clayton if (sc.symbol) 406e14e1925SGreg Clayton sc_list.Append(sc); 407e14e1925SGreg Clayton } 408e14e1925SGreg Clayton } 409e14e1925SGreg Clayton } 410e14e1925SGreg Clayton } 411e14e1925SGreg Clayton return sb_sc_list; 412e14e1925SGreg Clayton 413e14e1925SGreg Clayton } 414e14e1925SGreg Clayton 415e14e1925SGreg Clayton 416e14e1925SGreg Clayton 417cac9c5f9SGreg Clayton size_t 418cac9c5f9SGreg Clayton SBModule::GetNumSections () 419cac9c5f9SGreg Clayton { 420c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 421c2ff9318SGreg Clayton if (module_sp) 422cac9c5f9SGreg Clayton { 423a7499c98SMichael Sartain // Give the symbol vendor a chance to add to the unified section list. 424a7499c98SMichael Sartain module_sp->GetSymbolVendor(); 4253046e668SGreg Clayton SectionList *section_list = module_sp->GetSectionList(); 426cac9c5f9SGreg Clayton if (section_list) 427cac9c5f9SGreg Clayton return section_list->GetSize(); 428cac9c5f9SGreg Clayton } 429cac9c5f9SGreg Clayton return 0; 430cac9c5f9SGreg Clayton } 431cac9c5f9SGreg Clayton 432cac9c5f9SGreg Clayton SBSection 433cac9c5f9SGreg Clayton SBModule::GetSectionAtIndex (size_t idx) 434cac9c5f9SGreg Clayton { 435cac9c5f9SGreg Clayton SBSection sb_section; 436c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 437c2ff9318SGreg Clayton if (module_sp) 438cac9c5f9SGreg Clayton { 439a7499c98SMichael Sartain // Give the symbol vendor a chance to add to the unified section list. 440a7499c98SMichael Sartain module_sp->GetSymbolVendor(); 4413046e668SGreg Clayton SectionList *section_list = module_sp->GetSectionList (); 442cac9c5f9SGreg Clayton 443cac9c5f9SGreg Clayton if (section_list) 444e72dfb32SGreg Clayton sb_section.SetSP(section_list->GetSectionAtIndex (idx)); 445cac9c5f9SGreg Clayton } 446cac9c5f9SGreg Clayton return sb_section; 447cac9c5f9SGreg Clayton } 448cac9c5f9SGreg Clayton 4495569e64eSGreg Clayton lldb::SBSymbolContextList 450fe356d35SGreg Clayton SBModule::FindFunctions (const char *name, 4515569e64eSGreg Clayton uint32_t name_type_mask) 452fe356d35SGreg Clayton { 4535569e64eSGreg Clayton lldb::SBSymbolContextList sb_sc_list; 454c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 455c2ff9318SGreg Clayton if (name && module_sp) 456fe356d35SGreg Clayton { 4575569e64eSGreg Clayton const bool append = true; 458fe356d35SGreg Clayton const bool symbols_ok = true; 4599df05fbbSSean Callanan const bool inlines_ok = true; 460c2ff9318SGreg Clayton module_sp->FindFunctions (ConstString(name), 461b6d70ebcSSean Callanan NULL, 462fe356d35SGreg Clayton name_type_mask, 463fe356d35SGreg Clayton symbols_ok, 4649df05fbbSSean Callanan inlines_ok, 465fe356d35SGreg Clayton append, 4665569e64eSGreg Clayton *sb_sc_list); 467fe356d35SGreg Clayton } 4685569e64eSGreg Clayton return sb_sc_list; 469fe356d35SGreg Clayton } 470fe356d35SGreg Clayton 471dea8cb4fSGreg Clayton 472dea8cb4fSGreg Clayton SBValueList 473dea8cb4fSGreg Clayton SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches) 474dea8cb4fSGreg Clayton { 475dea8cb4fSGreg Clayton SBValueList sb_value_list; 476c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 477c2ff9318SGreg Clayton if (name && module_sp) 478dea8cb4fSGreg Clayton { 479dea8cb4fSGreg Clayton VariableList variable_list; 480c2ff9318SGreg Clayton const uint32_t match_count = module_sp->FindGlobalVariables (ConstString (name), 481b6d70ebcSSean Callanan NULL, 482dea8cb4fSGreg Clayton false, 483dea8cb4fSGreg Clayton max_matches, 484dea8cb4fSGreg Clayton variable_list); 485dea8cb4fSGreg Clayton 486dea8cb4fSGreg Clayton if (match_count > 0) 487dea8cb4fSGreg Clayton { 488dea8cb4fSGreg Clayton for (uint32_t i=0; i<match_count; ++i) 489dea8cb4fSGreg Clayton { 490dea8cb4fSGreg Clayton lldb::ValueObjectSP valobj_sp; 491b9556accSGreg Clayton TargetSP target_sp (target.GetSP()); 492b9556accSGreg Clayton valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i)); 493dea8cb4fSGreg Clayton if (valobj_sp) 49485425d77SEnrico Granata sb_value_list.Append(SBValue(valobj_sp)); 495dea8cb4fSGreg Clayton } 496dea8cb4fSGreg Clayton } 497dea8cb4fSGreg Clayton } 498dea8cb4fSGreg Clayton 499dea8cb4fSGreg Clayton return sb_value_list; 500dea8cb4fSGreg Clayton } 5016f3533fbSEnrico Granata 502bcd80b47SEnrico Granata lldb::SBValue 503bcd80b47SEnrico Granata SBModule::FindFirstGlobalVariable (lldb::SBTarget &target, const char *name) 504bcd80b47SEnrico Granata { 505bcd80b47SEnrico Granata SBValueList sb_value_list(FindGlobalVariables(target, name, 1)); 506bcd80b47SEnrico Granata if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0) 507bcd80b47SEnrico Granata return sb_value_list.GetValueAtIndex(0); 508bcd80b47SEnrico Granata return SBValue(); 509bcd80b47SEnrico Granata } 510bcd80b47SEnrico Granata 5116f3533fbSEnrico Granata lldb::SBType 5126f3533fbSEnrico Granata SBModule::FindFirstType (const char *name_cstr) 5136f3533fbSEnrico Granata { 514fe42ac4dSGreg Clayton SBType sb_type; 515c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 516c2ff9318SGreg Clayton if (name_cstr && module_sp) 517fe42ac4dSGreg Clayton { 5186f3533fbSEnrico Granata SymbolContext sc; 51984db9105SGreg Clayton const bool exact_match = false; 5206f3533fbSEnrico Granata ConstString name(name_cstr); 5216f3533fbSEnrico Granata 522b43165b7SGreg Clayton sb_type = SBType (module_sp->FindFirstType(sc, name, exact_match)); 5236f3533fbSEnrico Granata 524b43165b7SGreg Clayton if (!sb_type.IsValid()) 52556939cb3SGreg Clayton { 52656939cb3SGreg Clayton TypeSystem *type_system = module_sp->GetTypeSystemForLanguage(eLanguageTypeC); 52756939cb3SGreg Clayton if (type_system) 52856939cb3SGreg Clayton sb_type = SBType (type_system->GetBuiltinTypeByName(name)); 52956939cb3SGreg Clayton } 5306f3533fbSEnrico Granata } 531fe42ac4dSGreg Clayton return sb_type; 5326f3533fbSEnrico Granata } 5336f3533fbSEnrico Granata 534b43165b7SGreg Clayton lldb::SBType 535b43165b7SGreg Clayton SBModule::GetBasicType(lldb::BasicType type) 536b43165b7SGreg Clayton { 537b43165b7SGreg Clayton ModuleSP module_sp (GetSP ()); 538b43165b7SGreg Clayton if (module_sp) 53956939cb3SGreg Clayton { 54056939cb3SGreg Clayton TypeSystem *type_system = module_sp->GetTypeSystemForLanguage(eLanguageTypeC); 54156939cb3SGreg Clayton if (type_system) 54256939cb3SGreg Clayton return SBType (type_system->GetBasicTypeFromAST(type)); 54356939cb3SGreg Clayton } 544b43165b7SGreg Clayton return SBType(); 545b43165b7SGreg Clayton } 546b43165b7SGreg Clayton 5476f3533fbSEnrico Granata lldb::SBTypeList 5486f3533fbSEnrico Granata SBModule::FindTypes (const char *type) 5496f3533fbSEnrico Granata { 5506f3533fbSEnrico Granata SBTypeList retval; 5516f3533fbSEnrico Granata 552c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 553c2ff9318SGreg Clayton if (type && module_sp) 554fe42ac4dSGreg Clayton { 5556f3533fbSEnrico Granata SymbolContext sc; 5566f3533fbSEnrico Granata TypeList type_list; 55784db9105SGreg Clayton const bool exact_match = false; 5586f3533fbSEnrico Granata ConstString name(type); 559*ae088e52SGreg Clayton llvm::DenseSet<SymbolFile *> searched_symbol_files; 560b43165b7SGreg Clayton const uint32_t num_matches = module_sp->FindTypes (sc, 5616f3533fbSEnrico Granata name, 56284db9105SGreg Clayton exact_match, 5636f3533fbSEnrico Granata UINT32_MAX, 564*ae088e52SGreg Clayton searched_symbol_files, 5656f3533fbSEnrico Granata type_list); 5666f3533fbSEnrico Granata 567b43165b7SGreg Clayton if (num_matches > 0) 568b43165b7SGreg Clayton { 5696f3533fbSEnrico Granata for (size_t idx = 0; idx < num_matches; idx++) 5706f3533fbSEnrico Granata { 571fe42ac4dSGreg Clayton TypeSP type_sp (type_list.GetTypeAtIndex(idx)); 572fe42ac4dSGreg Clayton if (type_sp) 573fe42ac4dSGreg Clayton retval.Append(SBType(type_sp)); 574fe42ac4dSGreg Clayton } 5756f3533fbSEnrico Granata } 576b43165b7SGreg Clayton else 577b43165b7SGreg Clayton { 57856939cb3SGreg Clayton TypeSystem *type_system = module_sp->GetTypeSystemForLanguage(eLanguageTypeC); 57956939cb3SGreg Clayton if (type_system) 58056939cb3SGreg Clayton { 58156939cb3SGreg Clayton CompilerType compiler_type = type_system->GetBuiltinTypeByName(name); 58256939cb3SGreg Clayton if (compiler_type) 58356939cb3SGreg Clayton retval.Append(SBType(compiler_type)); 58456939cb3SGreg Clayton } 585b43165b7SGreg Clayton } 586b43165b7SGreg Clayton } 5876f3533fbSEnrico Granata 5886f3533fbSEnrico Granata return retval; 5896f3533fbSEnrico Granata } 590cac9c5f9SGreg Clayton 5911f4db7daSGreg Clayton lldb::SBType 5921f4db7daSGreg Clayton SBModule::GetTypeByID (lldb::user_id_t uid) 5931f4db7daSGreg Clayton { 5941f4db7daSGreg Clayton ModuleSP module_sp (GetSP ()); 5951f4db7daSGreg Clayton if (module_sp) 5961f4db7daSGreg Clayton { 5971f4db7daSGreg Clayton SymbolVendor* vendor = module_sp->GetSymbolVendor(); 5981f4db7daSGreg Clayton if (vendor) 5991f4db7daSGreg Clayton { 6001f4db7daSGreg Clayton Type *type_ptr = vendor->ResolveTypeUID(uid); 6011f4db7daSGreg Clayton if (type_ptr) 6021f4db7daSGreg Clayton return SBType(type_ptr->shared_from_this()); 6031f4db7daSGreg Clayton } 6041f4db7daSGreg Clayton } 6051f4db7daSGreg Clayton return SBType(); 6061f4db7daSGreg Clayton } 6071f4db7daSGreg Clayton 608f02500c7SGreg Clayton lldb::SBTypeList 609f02500c7SGreg Clayton SBModule::GetTypes (uint32_t type_mask) 610f02500c7SGreg Clayton { 611f02500c7SGreg Clayton SBTypeList sb_type_list; 612f02500c7SGreg Clayton 613f02500c7SGreg Clayton ModuleSP module_sp (GetSP ()); 614f02500c7SGreg Clayton if (module_sp) 615f02500c7SGreg Clayton { 616f02500c7SGreg Clayton SymbolVendor* vendor = module_sp->GetSymbolVendor(); 617f02500c7SGreg Clayton if (vendor) 618f02500c7SGreg Clayton { 619f02500c7SGreg Clayton TypeList type_list; 620f02500c7SGreg Clayton vendor->GetTypes (NULL, type_mask, type_list); 621f02500c7SGreg Clayton sb_type_list.m_opaque_ap->Append(type_list); 622f02500c7SGreg Clayton } 623f02500c7SGreg Clayton } 624f02500c7SGreg Clayton return sb_type_list; 625f02500c7SGreg Clayton } 626cac9c5f9SGreg Clayton 627cac9c5f9SGreg Clayton SBSection 628cac9c5f9SGreg Clayton SBModule::FindSection (const char *sect_name) 629cac9c5f9SGreg Clayton { 630cac9c5f9SGreg Clayton SBSection sb_section; 631cac9c5f9SGreg Clayton 632c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 633c2ff9318SGreg Clayton if (sect_name && module_sp) 634cac9c5f9SGreg Clayton { 635a7499c98SMichael Sartain // Give the symbol vendor a chance to add to the unified section list. 636a7499c98SMichael Sartain module_sp->GetSymbolVendor(); 6373046e668SGreg Clayton SectionList *section_list = module_sp->GetSectionList(); 638cac9c5f9SGreg Clayton if (section_list) 639cac9c5f9SGreg Clayton { 640cac9c5f9SGreg Clayton ConstString const_sect_name(sect_name); 641cac9c5f9SGreg Clayton SectionSP section_sp (section_list->FindSectionByName(const_sect_name)); 642cac9c5f9SGreg Clayton if (section_sp) 643cac9c5f9SGreg Clayton { 644e72dfb32SGreg Clayton sb_section.SetSP (section_sp); 645cac9c5f9SGreg Clayton } 646cac9c5f9SGreg Clayton } 647cac9c5f9SGreg Clayton } 648cac9c5f9SGreg Clayton return sb_section; 649cac9c5f9SGreg Clayton } 650cac9c5f9SGreg Clayton 65113d1950aSGreg Clayton lldb::ByteOrder 65213d1950aSGreg Clayton SBModule::GetByteOrder () 65313d1950aSGreg Clayton { 654c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 655c2ff9318SGreg Clayton if (module_sp) 656c2ff9318SGreg Clayton return module_sp->GetArchitecture().GetByteOrder(); 65713d1950aSGreg Clayton return eByteOrderInvalid; 65813d1950aSGreg Clayton } 65913d1950aSGreg Clayton 66013d1950aSGreg Clayton const char * 66113d1950aSGreg Clayton SBModule::GetTriple () 66213d1950aSGreg Clayton { 663c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 664c2ff9318SGreg Clayton if (module_sp) 66513d1950aSGreg Clayton { 666c2ff9318SGreg Clayton std::string triple (module_sp->GetArchitecture().GetTriple().str()); 66713d1950aSGreg Clayton // Unique the string so we don't run into ownership issues since 66813d1950aSGreg Clayton // the const strings put the string into the string pool once and 66913d1950aSGreg Clayton // the strings never comes out 67013d1950aSGreg Clayton ConstString const_triple (triple.c_str()); 67113d1950aSGreg Clayton return const_triple.GetCString(); 67213d1950aSGreg Clayton } 67313d1950aSGreg Clayton return NULL; 67413d1950aSGreg Clayton } 67513d1950aSGreg Clayton 67613d1950aSGreg Clayton uint32_t 67713d1950aSGreg Clayton SBModule::GetAddressByteSize() 67813d1950aSGreg Clayton { 679c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 680c2ff9318SGreg Clayton if (module_sp) 681c2ff9318SGreg Clayton return module_sp->GetArchitecture().GetAddressByteSize(); 68213d1950aSGreg Clayton return sizeof(void*); 68313d1950aSGreg Clayton } 68413d1950aSGreg Clayton 685c2ff9318SGreg Clayton 686c2ff9318SGreg Clayton uint32_t 687c2ff9318SGreg Clayton SBModule::GetVersion (uint32_t *versions, uint32_t num_versions) 688c2ff9318SGreg Clayton { 689c2ff9318SGreg Clayton ModuleSP module_sp (GetSP ()); 690c2ff9318SGreg Clayton if (module_sp) 6913467d80bSEnrico Granata return module_sp->GetVersion(versions, num_versions); 6923467d80bSEnrico Granata else 693c2ff9318SGreg Clayton { 694c2ff9318SGreg Clayton if (versions && num_versions) 695c2ff9318SGreg Clayton { 696c2ff9318SGreg Clayton for (uint32_t i=0; i<num_versions; ++i) 697c2ff9318SGreg Clayton versions[i] = UINT32_MAX; 698c2ff9318SGreg Clayton } 699c2ff9318SGreg Clayton return 0; 700c2ff9318SGreg Clayton } 7013467d80bSEnrico Granata } 702c2ff9318SGreg Clayton 703eb2c19a5SIlia K lldb::SBFileSpec 704eb2c19a5SIlia K SBModule::GetSymbolFileSpec() const 705eb2c19a5SIlia K { 706eb2c19a5SIlia K lldb::SBFileSpec sb_file_spec; 707eb2c19a5SIlia K ModuleSP module_sp(GetSP()); 708eb2c19a5SIlia K if (module_sp) 709eb2c19a5SIlia K { 710eb2c19a5SIlia K SymbolVendor *symbol_vendor_ptr = module_sp->GetSymbolVendor(); 711eb2c19a5SIlia K if (symbol_vendor_ptr) 712eb2c19a5SIlia K sb_file_spec.SetFileSpec(symbol_vendor_ptr->GetMainFileSpec()); 713eb2c19a5SIlia K } 714eb2c19a5SIlia K return sb_file_spec; 715eb2c19a5SIlia K } 716eb2c19a5SIlia K 717eb2c19a5SIlia K lldb::SBAddress 718eb2c19a5SIlia K SBModule::GetObjectFileHeaderAddress() const 719eb2c19a5SIlia K { 720eb2c19a5SIlia K lldb::SBAddress sb_addr; 721eb2c19a5SIlia K ModuleSP module_sp (GetSP ()); 722eb2c19a5SIlia K if (module_sp) 723eb2c19a5SIlia K { 724eb2c19a5SIlia K ObjectFile *objfile_ptr = module_sp->GetObjectFile(); 725eb2c19a5SIlia K if (objfile_ptr) 726eb2c19a5SIlia K sb_addr.ref() = objfile_ptr->GetHeaderAddress(); 727eb2c19a5SIlia K } 728eb2c19a5SIlia K return sb_addr; 729eb2c19a5SIlia K } 730