180814287SRaphael Isemann //===-- Module.cpp --------------------------------------------------------===// 230fdc8d8SChris Lattner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 630fdc8d8SChris Lattner // 730fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 830fdc8d8SChris Lattner 9c5dac77aSEugene Zelenko #include "lldb/Core/Module.h" 10c5dac77aSEugene Zelenko 11672d2c12SJonas Devlieghere #include "lldb/Core/AddressRange.h" 12f86248d9SRichard Mitton #include "lldb/Core/AddressResolverFileLine.h" 13672d2c12SJonas Devlieghere #include "lldb/Core/Debugger.h" 14672d2c12SJonas Devlieghere #include "lldb/Core/FileSpecList.h" 15672d2c12SJonas Devlieghere #include "lldb/Core/Mangled.h" 161f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h" 17672d2c12SJonas Devlieghere #include "lldb/Core/SearchFilter.h" 181f746071SGreg Clayton #include "lldb/Core/Section.h" 191408bf72SPavel Labath #include "lldb/Host/FileSystem.h" 20e38a5eddSGreg Clayton #include "lldb/Host/Host.h" 21f0c08b7eSAdrian Prantl #include "lldb/Host/HostInfo.h" 221759848bSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h" 231759848bSEnrico Granata #include "lldb/Interpreter/ScriptInterpreter.h" 241f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h" 25672d2c12SJonas Devlieghere #include "lldb/Symbol/Function.h" 2630fdc8d8SChris Lattner #include "lldb/Symbol/ObjectFile.h" 27672d2c12SJonas Devlieghere #include "lldb/Symbol/Symbol.h" 2830fdc8d8SChris Lattner #include "lldb/Symbol/SymbolContext.h" 2956939cb3SGreg Clayton #include "lldb/Symbol/SymbolFile.h" 3030fdc8d8SChris Lattner #include "lldb/Symbol/SymbolVendor.h" 31672d2c12SJonas Devlieghere #include "lldb/Symbol/Symtab.h" 32672d2c12SJonas Devlieghere #include "lldb/Symbol/Type.h" 33672d2c12SJonas Devlieghere #include "lldb/Symbol/TypeList.h" 34b9c1b51eSKate Stone #include "lldb/Symbol/TypeMap.h" 3556939cb3SGreg Clayton #include "lldb/Symbol/TypeSystem.h" 360e0984eeSJim Ingham #include "lldb/Target/Language.h" 37c9660546SGreg Clayton #include "lldb/Target/Process.h" 38c9660546SGreg Clayton #include "lldb/Target/Target.h" 39666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h" 409fecd372SLeonard Mosescu #include "lldb/Utility/LLDBAssert.h" 416f9e6901SZachary Turner #include "lldb/Utility/Log.h" 42672d2c12SJonas Devlieghere #include "lldb/Utility/Logging.h" 43bf9a7730SZachary Turner #include "lldb/Utility/RegularExpression.h" 4497206d57SZachary Turner #include "lldb/Utility/Status.h" 45672d2c12SJonas Devlieghere #include "lldb/Utility/Stream.h" 46bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 4738d0632eSPavel Labath #include "lldb/Utility/Timer.h" 4830fdc8d8SChris Lattner 49b1cb0b79SNico Weber #if defined(_WIN32) 50672d2c12SJonas Devlieghere #include "lldb/Host/windows/PosixApi.h" 512f3df613SZachary Turner #endif 522f3df613SZachary Turner 532f3df613SZachary Turner #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 54ce512d5cSAlex Langford #include "Plugins/Language/ObjC/ObjCLanguage.h" 5523f8c95aSGreg Clayton 56672d2c12SJonas Devlieghere #include "llvm/ADT/STLExtras.h" 57672d2c12SJonas Devlieghere #include "llvm/Support/Compiler.h" 582f3df613SZachary Turner #include "llvm/Support/FileSystem.h" 592f3df613SZachary Turner #include "llvm/Support/Signals.h" 60672d2c12SJonas Devlieghere #include "llvm/Support/raw_ostream.h" 612f3df613SZachary Turner 6276e47d48SRaphael Isemann #include <cassert> 6376e47d48SRaphael Isemann #include <cinttypes> 6476e47d48SRaphael Isemann #include <cstdarg> 65672d2c12SJonas Devlieghere #include <cstdint> 6676e47d48SRaphael Isemann #include <cstring> 67672d2c12SJonas Devlieghere #include <map> 68672d2c12SJonas Devlieghere #include <type_traits> 69672d2c12SJonas Devlieghere #include <utility> 702f3df613SZachary Turner 712f3df613SZachary Turner namespace lldb_private { 722f3df613SZachary Turner class CompilerDeclContext; 732f3df613SZachary Turner } 742f3df613SZachary Turner namespace lldb_private { 752f3df613SZachary Turner class VariableList; 762f3df613SZachary Turner } 772f3df613SZachary Turner 7830fdc8d8SChris Lattner using namespace lldb; 7930fdc8d8SChris Lattner using namespace lldb_private; 8030fdc8d8SChris Lattner 8105097246SAdrian Prantl // Shared pointers to modules track module lifetimes in targets and in the 8205097246SAdrian Prantl // global module, but this collection will track all module objects that are 8305097246SAdrian Prantl // still alive 8465a03991SGreg Clayton typedef std::vector<Module *> ModuleCollection; 8565a03991SGreg Clayton 86b9c1b51eSKate Stone static ModuleCollection &GetModuleCollection() { 87b9c1b51eSKate Stone // This module collection needs to live past any module, so we could either 8805097246SAdrian Prantl // make it a shared pointer in each module or just leak is. Since it is only 8905097246SAdrian Prantl // an empty vector by the time all the modules have gone away, we just leak 9005097246SAdrian Prantl // it for now. If we decide this is a big problem we can introduce a 9105097246SAdrian Prantl // Finalize method that will tear everything down in a predictable order. 92549f7374SJim Ingham 93c5dac77aSEugene Zelenko static ModuleCollection *g_module_collection = nullptr; 94c5dac77aSEugene Zelenko if (g_module_collection == nullptr) 95549f7374SJim Ingham g_module_collection = new ModuleCollection(); 96549f7374SJim Ingham 97549f7374SJim Ingham return *g_module_collection; 9865a03991SGreg Clayton } 9965a03991SGreg Clayton 100b9c1b51eSKate Stone std::recursive_mutex &Module::GetAllocationModuleCollectionMutex() { 101b26e6bebSGreg Clayton // NOTE: The mutex below must be leaked since the global module list in 10205097246SAdrian Prantl // the ModuleList class will get torn at some point, and we can't know if it 10305097246SAdrian Prantl // will tear itself down before the "g_module_collection_mutex" below will. 10405097246SAdrian Prantl // So we leak a Mutex object below to safeguard against that 105b26e6bebSGreg Clayton 10616ff8604SSaleem Abdulrasool static std::recursive_mutex *g_module_collection_mutex = nullptr; 107c5dac77aSEugene Zelenko if (g_module_collection_mutex == nullptr) 10816ff8604SSaleem Abdulrasool g_module_collection_mutex = new std::recursive_mutex; // NOTE: known leak 10916ff8604SSaleem Abdulrasool return *g_module_collection_mutex; 11065a03991SGreg Clayton } 11165a03991SGreg Clayton 112b9c1b51eSKate Stone size_t Module::GetNumberAllocatedModules() { 113b9c1b51eSKate Stone std::lock_guard<std::recursive_mutex> guard( 114b9c1b51eSKate Stone GetAllocationModuleCollectionMutex()); 11565a03991SGreg Clayton return GetModuleCollection().size(); 11665a03991SGreg Clayton } 11765a03991SGreg Clayton 118b9c1b51eSKate Stone Module *Module::GetAllocatedModuleAtIndex(size_t idx) { 119b9c1b51eSKate Stone std::lock_guard<std::recursive_mutex> guard( 120b9c1b51eSKate Stone GetAllocationModuleCollectionMutex()); 12165a03991SGreg Clayton ModuleCollection &modules = GetModuleCollection(); 12265a03991SGreg Clayton if (idx < modules.size()) 12365a03991SGreg Clayton return modules[idx]; 124c5dac77aSEugene Zelenko return nullptr; 12565a03991SGreg Clayton } 12665a03991SGreg Clayton 12716ff8604SSaleem Abdulrasool Module::Module(const ModuleSpec &module_spec) 1281408bf72SPavel Labath : m_object_offset(0), m_file_has_changed(false), 129b9c1b51eSKate Stone m_first_file_changed_log(false) { 130b9a01b39SGreg Clayton // Scope for locker below... 131b9a01b39SGreg Clayton { 132b9c1b51eSKate Stone std::lock_guard<std::recursive_mutex> guard( 133b9c1b51eSKate Stone GetAllocationModuleCollectionMutex()); 134b9a01b39SGreg Clayton GetModuleCollection().push_back(this); 135b9a01b39SGreg Clayton } 136b9a01b39SGreg Clayton 137b9c1b51eSKate Stone Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT | 138b9c1b51eSKate Stone LIBLLDB_LOG_MODULES)); 139c5dac77aSEugene Zelenko if (log != nullptr) 14063e5fb76SJonas Devlieghere LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')", 14163e5fb76SJonas Devlieghere static_cast<void *>(this), 142b9c1b51eSKate Stone module_spec.GetArchitecture().GetArchitectureName(), 143b9c1b51eSKate Stone module_spec.GetFileSpec().GetPath().c_str(), 14434f1159bSGreg Clayton module_spec.GetObjectName().IsEmpty() ? "" : "(", 145b9c1b51eSKate Stone module_spec.GetObjectName().IsEmpty() 146b9c1b51eSKate Stone ? "" 147b9c1b51eSKate Stone : module_spec.GetObjectName().AsCString(""), 14834f1159bSGreg Clayton module_spec.GetObjectName().IsEmpty() ? "" : ")"); 14934f1159bSGreg Clayton 150a4a00cedSFred Riss auto data_sp = module_spec.GetData(); 151a4a00cedSFred Riss lldb::offset_t file_size = 0; 152a4a00cedSFred Riss if (data_sp) 153a4a00cedSFred Riss file_size = data_sp->GetByteSize(); 154a4a00cedSFred Riss 15505097246SAdrian Prantl // First extract all module specifications from the file using the local file 15605097246SAdrian Prantl // path. If there are no specifications, then don't fill anything in 15734f1159bSGreg Clayton ModuleSpecList modules_specs; 158a4a00cedSFred Riss if (ObjectFile::GetModuleSpecifications( 159a4a00cedSFred Riss module_spec.GetFileSpec(), 0, file_size, modules_specs, data_sp) == 0) 16034f1159bSGreg Clayton return; 16134f1159bSGreg Clayton 16234f1159bSGreg Clayton // Now make sure that one of the module specifications matches what we just 163b9c1b51eSKate Stone // extract. We might have a module specification that specifies a file 16405097246SAdrian Prantl // "/usr/lib/dyld" with UUID XXX, but we might have a local version of 16505097246SAdrian Prantl // "/usr/lib/dyld" that has 16634f1159bSGreg Clayton // UUID YYY and we don't want those to match. If they don't match, just don't 16734f1159bSGreg Clayton // fill any ivars in so we don't accidentally grab the wrong file later since 16834f1159bSGreg Clayton // they don't match... 16934f1159bSGreg Clayton ModuleSpec matching_module_spec; 1709ba51579SLeonard Mosescu if (!modules_specs.FindMatchingModuleSpec(module_spec, 1719ba51579SLeonard Mosescu matching_module_spec)) { 1729ba51579SLeonard Mosescu if (log) { 17363e5fb76SJonas Devlieghere LLDB_LOGF(log, "Found local object file but the specs didn't match"); 1749ba51579SLeonard Mosescu } 17534f1159bSGreg Clayton return; 1769ba51579SLeonard Mosescu } 1777ab7f89aSGreg Clayton 178a4a00cedSFred Riss // Set m_data_sp if it was initially provided in the ModuleSpec. Note that 179a4a00cedSFred Riss // we cannot use the data_sp variable here, because it will have been 180a4a00cedSFred Riss // modified by GetModuleSpecifications(). 181a4a00cedSFred Riss if (auto module_spec_data_sp = module_spec.GetData()) { 182a4a00cedSFred Riss m_data_sp = module_spec_data_sp; 183a4a00cedSFred Riss m_mod_time = {}; 184a4a00cedSFred Riss } else { 1857ab7f89aSGreg Clayton if (module_spec.GetFileSpec()) 1861408bf72SPavel Labath m_mod_time = 187a4a00cedSFred Riss FileSystem::Instance().GetModificationTime(module_spec.GetFileSpec()); 188a4a00cedSFred Riss else if (matching_module_spec.GetFileSpec()) 189a4a00cedSFred Riss m_mod_time = FileSystem::Instance().GetModificationTime( 190a4a00cedSFred Riss matching_module_spec.GetFileSpec()); 191a4a00cedSFred Riss } 1927ab7f89aSGreg Clayton 19305097246SAdrian Prantl // Copy the architecture from the actual spec if we got one back, else use 19405097246SAdrian Prantl // the one that was specified 1957ab7f89aSGreg Clayton if (matching_module_spec.GetArchitecture().IsValid()) 19634f1159bSGreg Clayton m_arch = matching_module_spec.GetArchitecture(); 1977ab7f89aSGreg Clayton else if (module_spec.GetArchitecture().IsValid()) 1987ab7f89aSGreg Clayton m_arch = module_spec.GetArchitecture(); 1997ab7f89aSGreg Clayton 200d93c4a33SBruce Mitchener // Copy the file spec over and use the specified one (if there was one) so we 201b9c1b51eSKate Stone // don't use a path that might have gotten resolved a path in 202b9c1b51eSKate Stone // 'matching_module_spec' 2037ab7f89aSGreg Clayton if (module_spec.GetFileSpec()) 20434f1159bSGreg Clayton m_file = module_spec.GetFileSpec(); 2057ab7f89aSGreg Clayton else if (matching_module_spec.GetFileSpec()) 2067ab7f89aSGreg Clayton m_file = matching_module_spec.GetFileSpec(); 2077ab7f89aSGreg Clayton 2087ab7f89aSGreg Clayton // Copy the platform file spec over 2097ab7f89aSGreg Clayton if (module_spec.GetPlatformFileSpec()) 21034f1159bSGreg Clayton m_platform_file = module_spec.GetPlatformFileSpec(); 2117ab7f89aSGreg Clayton else if (matching_module_spec.GetPlatformFileSpec()) 2127ab7f89aSGreg Clayton m_platform_file = matching_module_spec.GetPlatformFileSpec(); 2137ab7f89aSGreg Clayton 2147ab7f89aSGreg Clayton // Copy the symbol file spec over 2157ab7f89aSGreg Clayton if (module_spec.GetSymbolFileSpec()) 21634f1159bSGreg Clayton m_symfile_spec = module_spec.GetSymbolFileSpec(); 2177ab7f89aSGreg Clayton else if (matching_module_spec.GetSymbolFileSpec()) 2187ab7f89aSGreg Clayton m_symfile_spec = matching_module_spec.GetSymbolFileSpec(); 2197ab7f89aSGreg Clayton 2207ab7f89aSGreg Clayton // Copy the object name over 2217ab7f89aSGreg Clayton if (matching_module_spec.GetObjectName()) 2227ab7f89aSGreg Clayton m_object_name = matching_module_spec.GetObjectName(); 2237ab7f89aSGreg Clayton else 22434f1159bSGreg Clayton m_object_name = module_spec.GetObjectName(); 2257ab7f89aSGreg Clayton 22605097246SAdrian Prantl // Always trust the object offset (file offset) and object modification time 22705097246SAdrian Prantl // (for mod time in a BSD static archive) of from the matching module 22805097246SAdrian Prantl // specification 22936d7c894SGreg Clayton m_object_offset = matching_module_spec.GetObjectOffset(); 23036d7c894SGreg Clayton m_object_mod_time = matching_module_spec.GetObjectModificationTime(); 231b9a01b39SGreg Clayton } 232b9a01b39SGreg Clayton 233b9c1b51eSKate Stone Module::Module(const FileSpec &file_spec, const ArchSpec &arch, 234b9c1b51eSKate Stone const ConstString *object_name, lldb::offset_t object_offset, 2357e2cfbf0SPavel Labath const llvm::sys::TimePoint<> &object_mod_time) 23646376966SJonas Devlieghere : m_mod_time(FileSystem::Instance().GetModificationTime(file_spec)), m_arch(arch), 2371408bf72SPavel Labath m_file(file_spec), m_object_offset(object_offset), 2387e2cfbf0SPavel Labath m_object_mod_time(object_mod_time), m_file_has_changed(false), 2397e2cfbf0SPavel Labath m_first_file_changed_log(false) { 24065a03991SGreg Clayton // Scope for locker below... 24165a03991SGreg Clayton { 242b9c1b51eSKate Stone std::lock_guard<std::recursive_mutex> guard( 243b9c1b51eSKate Stone GetAllocationModuleCollectionMutex()); 24465a03991SGreg Clayton GetModuleCollection().push_back(this); 24565a03991SGreg Clayton } 24665a03991SGreg Clayton 24730fdc8d8SChris Lattner if (object_name) 24830fdc8d8SChris Lattner m_object_name = *object_name; 24957abc5d6SGreg Clayton 250b9c1b51eSKate Stone Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT | 251b9c1b51eSKate Stone LIBLLDB_LOG_MODULES)); 252c5dac77aSEugene Zelenko if (log != nullptr) 25363e5fb76SJonas Devlieghere LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')", 25463e5fb76SJonas Devlieghere static_cast<void *>(this), m_arch.GetArchitectureName(), 25563e5fb76SJonas Devlieghere m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(", 256b9c1b51eSKate Stone m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), 257b9c1b51eSKate Stone m_object_name.IsEmpty() ? "" : ")"); 25830fdc8d8SChris Lattner } 25930fdc8d8SChris Lattner 2609494c510SJonas Devlieghere Module::Module() : m_file_has_changed(false), m_first_file_changed_log(false) { 261b9c1b51eSKate Stone std::lock_guard<std::recursive_mutex> guard( 262b9c1b51eSKate Stone GetAllocationModuleCollectionMutex()); 26323f8c95aSGreg Clayton GetModuleCollection().push_back(this); 26423f8c95aSGreg Clayton } 26523f8c95aSGreg Clayton 266b9c1b51eSKate Stone Module::~Module() { 26705097246SAdrian Prantl // Lock our module down while we tear everything down to make sure we don't 26805097246SAdrian Prantl // get any access to the module while it is being destroyed 26916ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 27065a03991SGreg Clayton // Scope for locker below... 27165a03991SGreg Clayton { 272b9c1b51eSKate Stone std::lock_guard<std::recursive_mutex> guard( 273b9c1b51eSKate Stone GetAllocationModuleCollectionMutex()); 27465a03991SGreg Clayton ModuleCollection &modules = GetModuleCollection(); 27565a03991SGreg Clayton ModuleCollection::iterator end = modules.end(); 27665a03991SGreg Clayton ModuleCollection::iterator pos = std::find(modules.begin(), end, this); 2773a18e319SGreg Clayton assert(pos != end); 27865a03991SGreg Clayton modules.erase(pos); 27965a03991SGreg Clayton } 280b9c1b51eSKate Stone Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT | 281b9c1b51eSKate Stone LIBLLDB_LOG_MODULES)); 282c5dac77aSEugene Zelenko if (log != nullptr) 28363e5fb76SJonas Devlieghere LLDB_LOGF(log, "%p Module::~Module((%s) '%s%s%s%s')", 284b9c1b51eSKate Stone static_cast<void *>(this), m_arch.GetArchitectureName(), 285b9c1b51eSKate Stone m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(", 28630fdc8d8SChris Lattner m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), 28730fdc8d8SChris Lattner m_object_name.IsEmpty() ? "" : ")"); 2886beaaa68SGreg Clayton // Release any auto pointers before we start tearing down our member 2896beaaa68SGreg Clayton // variables since the object file and symbol files might need to make 2906beaaa68SGreg Clayton // function calls back into this module object. The ordering is important 2916beaaa68SGreg Clayton // here because symbol files can require the module object file. So we tear 2926beaaa68SGreg Clayton // down the symbol file first, then the object file. 293d5b44036SJonas Devlieghere m_sections_up.reset(); 294d5b44036SJonas Devlieghere m_symfile_up.reset(); 295762f7135SGreg Clayton m_objfile_sp.reset(); 29630fdc8d8SChris Lattner } 29730fdc8d8SChris Lattner 298b9c1b51eSKate Stone ObjectFile *Module::GetMemoryObjectFile(const lldb::ProcessSP &process_sp, 29997206d57SZachary Turner lldb::addr_t header_addr, Status &error, 300b9c1b51eSKate Stone size_t size_to_read) { 301b9c1b51eSKate Stone if (m_objfile_sp) { 302c7f09ccaSGreg Clayton error.SetErrorString("object file already exists"); 303b9c1b51eSKate Stone } else { 30416ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 305b9c1b51eSKate Stone if (process_sp) { 306c7f09ccaSGreg Clayton m_did_load_objfile = true; 307a8f3ae7cSJonas Devlieghere auto data_up = std::make_unique<DataBufferHeap>(size_to_read, 0); 30897206d57SZachary Turner Status readmem_error; 309b9c1b51eSKate Stone const size_t bytes_read = 310d5b44036SJonas Devlieghere process_sp->ReadMemory(header_addr, data_up->GetBytes(), 311d5b44036SJonas Devlieghere data_up->GetByteSize(), readmem_error); 312256e6169SPaolo Severini if (bytes_read < size_to_read) 313256e6169SPaolo Severini data_up->SetByteSize(bytes_read); 314256e6169SPaolo Severini if (data_up->GetByteSize() > 0) { 315d5b44036SJonas Devlieghere DataBufferSP data_sp(data_up.release()); 316b9c1b51eSKate Stone m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, 317b9c1b51eSKate Stone header_addr, data_sp); 318b9c1b51eSKate Stone if (m_objfile_sp) { 3193e10cf3bSGreg Clayton StreamString s; 320d01b2953SDaniel Malea s.Printf("0x%16.16" PRIx64, header_addr); 321c156427dSZachary Turner m_object_name.SetString(s.GetString()); 3223e10cf3bSGreg Clayton 323b9c1b51eSKate Stone // Once we get the object file, update our module with the object 32405097246SAdrian Prantl // file's architecture since it might differ in vendor/os if some 32505097246SAdrian Prantl // parts were unknown. 326f760f5aeSPavel Labath m_arch = m_objfile_sp->GetArchitecture(); 327a07287ecSAlex Langford 328a07287ecSAlex Langford // Augment the arch with the target's information in case 329a07287ecSAlex Langford // we are unable to extract the os/environment from memory. 330a07287ecSAlex Langford m_arch.MergeFrom(process_sp->GetTarget().GetArchitecture()); 331b9c1b51eSKate Stone } else { 332c7f09ccaSGreg Clayton error.SetErrorString("unable to find suitable object file plug-in"); 333c7f09ccaSGreg Clayton } 334b9c1b51eSKate Stone } else { 335b9c1b51eSKate Stone error.SetErrorStringWithFormat("unable to read header from memory: %s", 336b9c1b51eSKate Stone readmem_error.AsCString()); 337c7f09ccaSGreg Clayton } 338b9c1b51eSKate Stone } else { 339c7f09ccaSGreg Clayton error.SetErrorString("invalid process"); 340c7f09ccaSGreg Clayton } 341c7f09ccaSGreg Clayton } 342c7f09ccaSGreg Clayton return m_objfile_sp.get(); 343c7f09ccaSGreg Clayton } 344c7f09ccaSGreg Clayton 345b9c1b51eSKate Stone const lldb_private::UUID &Module::GetUUID() { 3469fecd372SLeonard Mosescu if (!m_did_set_uuid.load()) { 34716ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 3489fecd372SLeonard Mosescu if (!m_did_set_uuid.load()) { 34930fdc8d8SChris Lattner ObjectFile *obj_file = GetObjectFile(); 35030fdc8d8SChris Lattner 351b9c1b51eSKate Stone if (obj_file != nullptr) { 352bd334efdSPavel Labath m_uuid = obj_file->GetUUID(); 3539fecd372SLeonard Mosescu m_did_set_uuid = true; 35430fdc8d8SChris Lattner } 35530fdc8d8SChris Lattner } 35688c05f54SGreg Clayton } 35730fdc8d8SChris Lattner return m_uuid; 35830fdc8d8SChris Lattner } 35930fdc8d8SChris Lattner 3609fecd372SLeonard Mosescu void Module::SetUUID(const lldb_private::UUID &uuid) { 3619fecd372SLeonard Mosescu std::lock_guard<std::recursive_mutex> guard(m_mutex); 3629fecd372SLeonard Mosescu if (!m_did_set_uuid) { 3639fecd372SLeonard Mosescu m_uuid = uuid; 3649fecd372SLeonard Mosescu m_did_set_uuid = true; 3659fecd372SLeonard Mosescu } else { 366fbe748aeSRichard Smith lldbassert(0 && "Attempting to overwrite the existing module UUID"); 3679fecd372SLeonard Mosescu } 3689fecd372SLeonard Mosescu } 3699fecd372SLeonard Mosescu 3700e252e38SAlex Langford llvm::Expected<TypeSystem &> 3710e252e38SAlex Langford Module::GetTypeSystemForLanguage(LanguageType language) { 3725beec213SGreg Clayton return m_type_system_map.GetTypeSystemForLanguage(language, this, true); 3736beaaa68SGreg Clayton } 3746beaaa68SGreg Clayton 375b9c1b51eSKate Stone void Module::ParseAllDebugSymbols() { 37616ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 377c7bece56SGreg Clayton size_t num_comp_units = GetNumCompileUnits(); 37830fdc8d8SChris Lattner if (num_comp_units == 0) 37930fdc8d8SChris Lattner return; 38030fdc8d8SChris Lattner 38123f70e83SPavel Labath SymbolFile *symbols = GetSymbolFile(); 38230fdc8d8SChris Lattner 383b9c1b51eSKate Stone for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++) { 3848131cb6eSPavel Labath SymbolContext sc; 3858131cb6eSPavel Labath sc.module_sp = shared_from_this(); 38630fdc8d8SChris Lattner sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get(); 387863f8c18SZachary Turner if (!sc.comp_unit) 388863f8c18SZachary Turner continue; 389863f8c18SZachary Turner 39030fdc8d8SChris Lattner symbols->ParseVariablesForContext(sc); 39130fdc8d8SChris Lattner 392863f8c18SZachary Turner symbols->ParseFunctions(*sc.comp_unit); 39330fdc8d8SChris Lattner 394a7f19e5fSRaphael Isemann sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) { 395ffc1b8fdSZachary Turner symbols->ParseBlocksRecursive(*f); 396ffc1b8fdSZachary Turner 39730fdc8d8SChris Lattner // Parse the variables for this function and all its blocks 398ffc1b8fdSZachary Turner sc.function = f.get(); 39930fdc8d8SChris Lattner symbols->ParseVariablesForContext(sc); 400a7f19e5fSRaphael Isemann return false; 401a7f19e5fSRaphael Isemann }); 40230fdc8d8SChris Lattner 40330fdc8d8SChris Lattner // Parse all types for this compile unit 404863f8c18SZachary Turner symbols->ParseTypes(*sc.comp_unit); 40530fdc8d8SChris Lattner } 40630fdc8d8SChris Lattner } 40730fdc8d8SChris Lattner 408b9c1b51eSKate Stone void Module::CalculateSymbolContext(SymbolContext *sc) { 409e1cd1be6SGreg Clayton sc->module_sp = shared_from_this(); 41030fdc8d8SChris Lattner } 41130fdc8d8SChris Lattner 412b9c1b51eSKate Stone ModuleSP Module::CalculateSymbolContextModule() { return shared_from_this(); } 4137e9b1fd0SGreg Clayton 414b9c1b51eSKate Stone void Module::DumpSymbolContext(Stream *s) { 415324a1036SSaleem Abdulrasool s->Printf(", Module{%p}", static_cast<void *>(this)); 41630fdc8d8SChris Lattner } 41730fdc8d8SChris Lattner 418b9c1b51eSKate Stone size_t Module::GetNumCompileUnits() { 41916ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 4205c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF("Module::GetNumCompileUnits (module = %p)", 421324a1036SSaleem Abdulrasool static_cast<void *>(this)); 42223f70e83SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 42330fdc8d8SChris Lattner return symbols->GetNumCompileUnits(); 42430fdc8d8SChris Lattner return 0; 42530fdc8d8SChris Lattner } 42630fdc8d8SChris Lattner 427b9c1b51eSKate Stone CompUnitSP Module::GetCompileUnitAtIndex(size_t index) { 42816ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 429c7bece56SGreg Clayton size_t num_comp_units = GetNumCompileUnits(); 43030fdc8d8SChris Lattner CompUnitSP cu_sp; 43130fdc8d8SChris Lattner 432b9c1b51eSKate Stone if (index < num_comp_units) { 43323f70e83SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 43430fdc8d8SChris Lattner cu_sp = symbols->GetCompileUnitAtIndex(index); 43530fdc8d8SChris Lattner } 43630fdc8d8SChris Lattner return cu_sp; 43730fdc8d8SChris Lattner } 43830fdc8d8SChris Lattner 439b9c1b51eSKate Stone bool Module::ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr) { 44016ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 4413046e668SGreg Clayton SectionList *section_list = GetSectionList(); 4423046e668SGreg Clayton if (section_list) 4433046e668SGreg Clayton return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list); 44430fdc8d8SChris Lattner return false; 44530fdc8d8SChris Lattner } 44630fdc8d8SChris Lattner 447b9c1b51eSKate Stone uint32_t Module::ResolveSymbolContextForAddress( 448991e4453SZachary Turner const Address &so_addr, lldb::SymbolContextItem resolve_scope, 449991e4453SZachary Turner SymbolContext &sc, bool resolve_tail_call_address) { 45016ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 45130fdc8d8SChris Lattner uint32_t resolved_flags = 0; 45230fdc8d8SChris Lattner 453b9c1b51eSKate Stone // Clear the result symbol context in case we don't find anything, but don't 454b9c1b51eSKate Stone // clear the target 45572310355SGreg Clayton sc.Clear(false); 45630fdc8d8SChris Lattner 45730fdc8d8SChris Lattner // Get the section from the section/offset address. 458e72dfb32SGreg Clayton SectionSP section_sp(so_addr.GetSection()); 45930fdc8d8SChris Lattner 46030fdc8d8SChris Lattner // Make sure the section matches this module before we try and match anything 461b9c1b51eSKate Stone if (section_sp && section_sp->GetModule().get() == this) { 46205097246SAdrian Prantl // If the section offset based address resolved itself, then this is the 46305097246SAdrian Prantl // right module. 464e1cd1be6SGreg Clayton sc.module_sp = shared_from_this(); 46530fdc8d8SChris Lattner resolved_flags |= eSymbolContextModule; 46630fdc8d8SChris Lattner 46723f70e83SPavel Labath SymbolFile *symfile = GetSymbolFile(); 46823f70e83SPavel Labath if (!symfile) 46938807141SAshok Thirumurthi return resolved_flags; 47038807141SAshok Thirumurthi 47105097246SAdrian Prantl // Resolve the compile unit, function, block, line table or line entry if 47205097246SAdrian Prantl // requested. 47330fdc8d8SChris Lattner if (resolve_scope & eSymbolContextCompUnit || 47430fdc8d8SChris Lattner resolve_scope & eSymbolContextFunction || 47530fdc8d8SChris Lattner resolve_scope & eSymbolContextBlock || 4764c8e7828SGreg Clayton resolve_scope & eSymbolContextLineEntry || 477b9c1b51eSKate Stone resolve_scope & eSymbolContextVariable) { 478b9c1b51eSKate Stone resolved_flags |= 47923f70e83SPavel Labath symfile->ResolveSymbolContext(so_addr, resolve_scope, sc); 48030fdc8d8SChris Lattner } 48130fdc8d8SChris Lattner 48205097246SAdrian Prantl // Resolve the symbol if requested, but don't re-look it up if we've 48305097246SAdrian Prantl // already found it. 484b9c1b51eSKate Stone if (resolve_scope & eSymbolContextSymbol && 485b9c1b51eSKate Stone !(resolved_flags & eSymbolContextSymbol)) { 48623f70e83SPavel Labath Symtab *symtab = symfile->GetSymtab(); 487b9c1b51eSKate Stone if (symtab && so_addr.IsSectionOffset()) { 4880d9dd7dfSMohit K. Bhakkad Symbol *matching_symbol = nullptr; 489c35b91ceSAdrian McCarthy 490b9c1b51eSKate Stone symtab->ForEachSymbolContainingFileAddress( 491b9c1b51eSKate Stone so_addr.GetFileAddress(), 492c35b91ceSAdrian McCarthy [&matching_symbol](Symbol *symbol) -> bool { 493b9c1b51eSKate Stone if (symbol->GetType() != eSymbolTypeInvalid) { 4940d9dd7dfSMohit K. Bhakkad matching_symbol = symbol; 4950d9dd7dfSMohit K. Bhakkad return false; // Stop iterating 4960d9dd7dfSMohit K. Bhakkad } 4970d9dd7dfSMohit K. Bhakkad return true; // Keep iterating 4980d9dd7dfSMohit K. Bhakkad }); 4990d9dd7dfSMohit K. Bhakkad sc.symbol = matching_symbol; 500b9c1b51eSKate Stone if (!sc.symbol && resolve_scope & eSymbolContextFunction && 501b9c1b51eSKate Stone !(resolved_flags & eSymbolContextFunction)) { 502b9c1b51eSKate Stone bool verify_unique = false; // No need to check again since 503b9c1b51eSKate Stone // ResolveSymbolContext failed to find a 504b9c1b51eSKate Stone // symbol at this address. 50535729bb1SAshok Thirumurthi if (ObjectFile *obj_file = sc.module_sp->GetObjectFile()) 506b9c1b51eSKate Stone sc.symbol = 507b9c1b51eSKate Stone obj_file->ResolveSymbolForAddress(so_addr, verify_unique); 50835729bb1SAshok Thirumurthi } 50935729bb1SAshok Thirumurthi 510b9c1b51eSKate Stone if (sc.symbol) { 511b9c1b51eSKate Stone if (sc.symbol->IsSynthetic()) { 51205097246SAdrian Prantl // We have a synthetic symbol so lets check if the object file from 51305097246SAdrian Prantl // the symbol file in the symbol vendor is different than the 51405097246SAdrian Prantl // object file for the module, and if so search its symbol table to 51505097246SAdrian Prantl // see if we can come up with a better symbol. For example dSYM 51605097246SAdrian Prantl // files on MacOSX have an unstripped symbol table inside of them. 51793e2861bSGreg Clayton ObjectFile *symtab_objfile = symtab->GetObjectFile(); 518b9c1b51eSKate Stone if (symtab_objfile && symtab_objfile->IsStripped()) { 51993e2861bSGreg Clayton ObjectFile *symfile_objfile = symfile->GetObjectFile(); 520b9c1b51eSKate Stone if (symfile_objfile != symtab_objfile) { 52193e2861bSGreg Clayton Symtab *symfile_symtab = symfile_objfile->GetSymtab(); 522b9c1b51eSKate Stone if (symfile_symtab) { 523b9c1b51eSKate Stone Symbol *symbol = 524b9c1b51eSKate Stone symfile_symtab->FindSymbolContainingFileAddress( 525b9c1b51eSKate Stone so_addr.GetFileAddress()); 526b9c1b51eSKate Stone if (symbol && !symbol->IsSynthetic()) { 52793e2861bSGreg Clayton sc.symbol = symbol; 52893e2861bSGreg Clayton } 52993e2861bSGreg Clayton } 53093e2861bSGreg Clayton } 53193e2861bSGreg Clayton } 53293e2861bSGreg Clayton } 53330fdc8d8SChris Lattner resolved_flags |= eSymbolContextSymbol; 53430fdc8d8SChris Lattner } 53530fdc8d8SChris Lattner } 53693e2861bSGreg Clayton } 53738807141SAshok Thirumurthi 538b9c1b51eSKate Stone // For function symbols, so_addr may be off by one. This is a convention 53905097246SAdrian Prantl // consistent with FDE row indices in eh_frame sections, but requires extra 54005097246SAdrian Prantl // logic here to permit symbol lookup for disassembly and unwind. 541b9c1b51eSKate Stone if (resolve_scope & eSymbolContextSymbol && 542b9c1b51eSKate Stone !(resolved_flags & eSymbolContextSymbol) && resolve_tail_call_address && 543b9c1b51eSKate Stone so_addr.IsSectionOffset()) { 54438807141SAshok Thirumurthi Address previous_addr = so_addr; 545edfaae39SGreg Clayton previous_addr.Slide(-1); 54638807141SAshok Thirumurthi 54735729bb1SAshok Thirumurthi bool do_resolve_tail_call_address = false; // prevent recursion 548b9c1b51eSKate Stone const uint32_t flags = ResolveSymbolContextForAddress( 549b9c1b51eSKate Stone previous_addr, resolve_scope, sc, do_resolve_tail_call_address); 550b9c1b51eSKate Stone if (flags & eSymbolContextSymbol) { 55138807141SAshok Thirumurthi AddressRange addr_range; 552b9c1b51eSKate Stone if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, 553b9c1b51eSKate Stone false, addr_range)) { 554b9c1b51eSKate Stone if (addr_range.GetBaseAddress().GetSection() == 555b9c1b51eSKate Stone so_addr.GetSection()) { 556b9c1b51eSKate Stone // If the requested address is one past the address range of a 55705097246SAdrian Prantl // function (i.e. a tail call), or the decremented address is the 55805097246SAdrian Prantl // start of a function (i.e. some forms of trampoline), indicate 55905097246SAdrian Prantl // that the symbol has been resolved. 560b9c1b51eSKate Stone if (so_addr.GetOffset() == 561b9c1b51eSKate Stone addr_range.GetBaseAddress().GetOffset() || 562b9c1b51eSKate Stone so_addr.GetOffset() == 563b9c1b51eSKate Stone addr_range.GetBaseAddress().GetOffset() + 564b9c1b51eSKate Stone addr_range.GetByteSize()) { 56538807141SAshok Thirumurthi resolved_flags |= flags; 56638807141SAshok Thirumurthi } 567b9c1b51eSKate Stone } else { 568b9c1b51eSKate Stone sc.symbol = 569b9c1b51eSKate Stone nullptr; // Don't trust the symbol if the sections didn't match. 57038807141SAshok Thirumurthi } 57138807141SAshok Thirumurthi } 57230fdc8d8SChris Lattner } 57330fdc8d8SChris Lattner } 57430fdc8d8SChris Lattner } 57530fdc8d8SChris Lattner return resolved_flags; 57630fdc8d8SChris Lattner } 57730fdc8d8SChris Lattner 578991e4453SZachary Turner uint32_t Module::ResolveSymbolContextForFilePath( 579991e4453SZachary Turner const char *file_path, uint32_t line, bool check_inlines, 580991e4453SZachary Turner lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) { 5818f3be7a3SJonas Devlieghere FileSpec file_spec(file_path); 582b9c1b51eSKate Stone return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines, 583b9c1b51eSKate Stone resolve_scope, sc_list); 58430fdc8d8SChris Lattner } 58530fdc8d8SChris Lattner 586991e4453SZachary Turner uint32_t Module::ResolveSymbolContextsForFileSpec( 587991e4453SZachary Turner const FileSpec &file_spec, uint32_t line, bool check_inlines, 588991e4453SZachary Turner lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) { 58916ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 5905c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF("Module::ResolveSymbolContextForFilePath (%s:%u, " 591b9c1b51eSKate Stone "check_inlines = %s, resolve_scope = 0x%8.8x)", 592b9c1b51eSKate Stone file_spec.GetPath().c_str(), line, 593b9c1b51eSKate Stone check_inlines ? "yes" : "no", resolve_scope); 59430fdc8d8SChris Lattner 59530fdc8d8SChris Lattner const uint32_t initial_count = sc_list.GetSize(); 59630fdc8d8SChris Lattner 5973e2ed744SMed Ismail Bennani if (SymbolFile *symbols = GetSymbolFile()) { 5983e2ed744SMed Ismail Bennani // TODO: Handle SourceLocationSpec column information 5993e2ed744SMed Ismail Bennani SourceLocationSpec location_spec(file_spec, line, /*column=*/llvm::None, 6003e2ed744SMed Ismail Bennani check_inlines, /*exact_match=*/false); 6013e2ed744SMed Ismail Bennani 6023e2ed744SMed Ismail Bennani symbols->ResolveSymbolContext(location_spec, resolve_scope, sc_list); 6033e2ed744SMed Ismail Bennani } 60430fdc8d8SChris Lattner 60530fdc8d8SChris Lattner return sc_list.GetSize() - initial_count; 60630fdc8d8SChris Lattner } 60730fdc8d8SChris Lattner 6081ad655e2SAdrian Prantl void Module::FindGlobalVariables(ConstString name, 609f9568a95SRaphael Isemann const CompilerDeclContext &parent_decl_ctx, 6101ad655e2SAdrian Prantl size_t max_matches, VariableList &variables) { 61123f70e83SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 6121ad655e2SAdrian Prantl symbols->FindGlobalVariables(name, parent_decl_ctx, max_matches, variables); 61334cda14bSPavel Labath } 61434cda14bSPavel Labath 6151ad655e2SAdrian Prantl void Module::FindGlobalVariables(const RegularExpression ®ex, 6161ad655e2SAdrian Prantl size_t max_matches, VariableList &variables) { 61723f70e83SPavel Labath SymbolFile *symbols = GetSymbolFile(); 61834cda14bSPavel Labath if (symbols) 6191ad655e2SAdrian Prantl symbols->FindGlobalVariables(regex, max_matches, variables); 62030fdc8d8SChris Lattner } 62130fdc8d8SChris Lattner 6221ad655e2SAdrian Prantl void Module::FindCompileUnits(const FileSpec &path, 623b9c1b51eSKate Stone SymbolContextList &sc_list) { 624c7bece56SGreg Clayton const size_t num_compile_units = GetNumCompileUnits(); 625644247c1SGreg Clayton SymbolContext sc; 626e1cd1be6SGreg Clayton sc.module_sp = shared_from_this(); 627b9c1b51eSKate Stone for (size_t i = 0; i < num_compile_units; ++i) { 628644247c1SGreg Clayton sc.comp_unit = GetCompileUnitAtIndex(i).get(); 629b9c1b51eSKate Stone if (sc.comp_unit) { 630532290e6SPavel Labath if (FileSpec::Match(path, sc.comp_unit->GetPrimaryFile())) 631644247c1SGreg Clayton sc_list.Append(sc); 632644247c1SGreg Clayton } 6332dafd8edSGreg Clayton } 634644247c1SGreg Clayton } 635644247c1SGreg Clayton 6360e4c4821SAdrian Prantl Module::LookupInfo::LookupInfo(ConstString name, 637117b1fa1SZachary Turner FunctionNameType name_type_mask, 638ce512d5cSAlex Langford LanguageType language) 639ce512d5cSAlex Langford : m_name(name), m_lookup_name(), m_language(language), 640117b1fa1SZachary Turner m_name_type_mask(eFunctionNameTypeNone), 641b9c1b51eSKate Stone m_match_name_after_lookup(false) { 642ce512d5cSAlex Langford const char *name_cstr = name.GetCString(); 6436234a5c8SGreg Clayton llvm::StringRef basename; 6446234a5c8SGreg Clayton llvm::StringRef context; 6456234a5c8SGreg Clayton 646cd2134e4SAlex Langford if (name_type_mask & eFunctionNameTypeAuto) { 647ce512d5cSAlex Langford if (CPlusPlusLanguage::IsCPPMangledName(name_cstr)) 648cd2134e4SAlex Langford m_name_type_mask = eFunctionNameTypeFull; 649ce512d5cSAlex Langford else if ((language == eLanguageTypeUnknown || 650ce512d5cSAlex Langford Language::LanguageIsObjC(language)) && 651ce512d5cSAlex Langford ObjCLanguage::IsPossibleObjCMethodName(name_cstr)) 652ce512d5cSAlex Langford m_name_type_mask = eFunctionNameTypeFull; 653ce512d5cSAlex Langford else if (Language::LanguageIsC(language)) { 654ce512d5cSAlex Langford m_name_type_mask = eFunctionNameTypeFull; 655ce512d5cSAlex Langford } else { 656ce512d5cSAlex Langford if ((language == eLanguageTypeUnknown || 657ce512d5cSAlex Langford Language::LanguageIsObjC(language)) && 658ce512d5cSAlex Langford ObjCLanguage::IsPossibleObjCSelector(name_cstr)) 659ce512d5cSAlex Langford m_name_type_mask |= eFunctionNameTypeSelector; 660cd2134e4SAlex Langford 661ce512d5cSAlex Langford CPlusPlusLanguage::MethodName cpp_method(name); 662ce512d5cSAlex Langford basename = cpp_method.GetBasename(); 663ce512d5cSAlex Langford if (basename.empty()) { 664ce512d5cSAlex Langford if (CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, 665ce512d5cSAlex Langford basename)) 666ce512d5cSAlex Langford m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase); 667ce512d5cSAlex Langford else 668ce512d5cSAlex Langford m_name_type_mask |= eFunctionNameTypeFull; 669ce512d5cSAlex Langford } else { 670ce512d5cSAlex Langford m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase); 671ce512d5cSAlex Langford } 672ce512d5cSAlex Langford } 673b9c1b51eSKate Stone } else { 6746234a5c8SGreg Clayton m_name_type_mask = name_type_mask; 675ce512d5cSAlex Langford if (name_type_mask & eFunctionNameTypeMethod || 676ce512d5cSAlex Langford name_type_mask & eFunctionNameTypeBase) { 677ce512d5cSAlex Langford // If they've asked for a CPP method or function name and it can't be 678ce512d5cSAlex Langford // that, we don't even need to search for CPP methods or names. 679ce512d5cSAlex Langford CPlusPlusLanguage::MethodName cpp_method(name); 680ce512d5cSAlex Langford if (cpp_method.IsValid()) { 681ce512d5cSAlex Langford basename = cpp_method.GetBasename(); 682ce512d5cSAlex Langford 683ce512d5cSAlex Langford if (!cpp_method.GetQualifiers().empty()) { 684ce512d5cSAlex Langford // There is a "const" or other qualifier following the end of the 685ce512d5cSAlex Langford // function parens, this can't be a eFunctionNameTypeBase 686ce512d5cSAlex Langford m_name_type_mask &= ~(eFunctionNameTypeBase); 687ce512d5cSAlex Langford if (m_name_type_mask == eFunctionNameTypeNone) 688ce512d5cSAlex Langford return; 6896234a5c8SGreg Clayton } 690b9c1b51eSKate Stone } else { 691ce512d5cSAlex Langford // If the CPP method parser didn't manage to chop this up, try to fill 692ce512d5cSAlex Langford // in the base name if we can. If a::b::c is passed in, we need to just 693ce512d5cSAlex Langford // look up "c", and then we'll filter the result later. 694ce512d5cSAlex Langford CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, 695ce512d5cSAlex Langford basename); 6966234a5c8SGreg Clayton } 6976234a5c8SGreg Clayton } 698ce512d5cSAlex Langford 699ce512d5cSAlex Langford if (name_type_mask & eFunctionNameTypeSelector) { 700ce512d5cSAlex Langford if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr)) { 701ce512d5cSAlex Langford m_name_type_mask &= ~(eFunctionNameTypeSelector); 702ce512d5cSAlex Langford if (m_name_type_mask == eFunctionNameTypeNone) 703ce512d5cSAlex Langford return; 704ce512d5cSAlex Langford } 705ce512d5cSAlex Langford } 706ce512d5cSAlex Langford 707ce512d5cSAlex Langford // Still try and get a basename in case someone specifies a name type mask 708ce512d5cSAlex Langford // of eFunctionNameTypeFull and a name like "A::func" 709ce512d5cSAlex Langford if (basename.empty()) { 710ce512d5cSAlex Langford if (name_type_mask & eFunctionNameTypeFull && 711ce512d5cSAlex Langford !CPlusPlusLanguage::IsCPPMangledName(name_cstr)) { 712ce512d5cSAlex Langford CPlusPlusLanguage::MethodName cpp_method(name); 713ce512d5cSAlex Langford basename = cpp_method.GetBasename(); 714ce512d5cSAlex Langford if (basename.empty()) 715ce512d5cSAlex Langford CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, 716ce512d5cSAlex Langford basename); 717ce512d5cSAlex Langford } 7186234a5c8SGreg Clayton } 7196234a5c8SGreg Clayton } 7206234a5c8SGreg Clayton 721b9c1b51eSKate Stone if (!basename.empty()) { 722ce512d5cSAlex Langford // The name supplied was a partial C++ path like "a::count". In this case 723ce512d5cSAlex Langford // we want to do a lookup on the basename "count" and then make sure any 724ce512d5cSAlex Langford // matching results contain "a::count" so that it would match "b::a::count" 725ce512d5cSAlex Langford // and "a::count". This is why we set "match_name_after_lookup" to true 7266234a5c8SGreg Clayton m_lookup_name.SetString(basename); 7276234a5c8SGreg Clayton m_match_name_after_lookup = true; 728ce512d5cSAlex Langford } else { 729ce512d5cSAlex Langford // The name is already correct, just use the exact name as supplied, and we 730ce512d5cSAlex Langford // won't need to check if any matches contain "name" 731ce512d5cSAlex Langford m_lookup_name = name; 732ce512d5cSAlex Langford m_match_name_after_lookup = false; 7336234a5c8SGreg Clayton } 7346234a5c8SGreg Clayton } 7356234a5c8SGreg Clayton 736b9c1b51eSKate Stone void Module::LookupInfo::Prune(SymbolContextList &sc_list, 737b9c1b51eSKate Stone size_t start_idx) const { 738b9c1b51eSKate Stone if (m_match_name_after_lookup && m_name) { 7396234a5c8SGreg Clayton SymbolContext sc; 7406234a5c8SGreg Clayton size_t i = start_idx; 741b9c1b51eSKate Stone while (i < sc_list.GetSize()) { 7426234a5c8SGreg Clayton if (!sc_list.GetContextAtIndex(i, sc)) 7436234a5c8SGreg Clayton break; 7446234a5c8SGreg Clayton ConstString full_name(sc.GetFunctionName()); 745b9c1b51eSKate Stone if (full_name && 746b9c1b51eSKate Stone ::strstr(full_name.GetCString(), m_name.GetCString()) == nullptr) { 7476234a5c8SGreg Clayton sc_list.RemoveContextAtIndex(i); 748b9c1b51eSKate Stone } else { 7496234a5c8SGreg Clayton ++i; 7506234a5c8SGreg Clayton } 7516234a5c8SGreg Clayton } 7526234a5c8SGreg Clayton } 7536234a5c8SGreg Clayton 754b9c1b51eSKate Stone // If we have only full name matches we might have tried to set breakpoint on 7555d0c1146SGreg Clayton // "func" and specified eFunctionNameTypeFull, but we might have found 7565d0c1146SGreg Clayton // "a::func()", "a::b::func()", "c::func()", "func()" and "func". Only 7575d0c1146SGreg Clayton // "func()" and "func" should end up matching. 758b9c1b51eSKate Stone if (m_name_type_mask == eFunctionNameTypeFull) { 7596234a5c8SGreg Clayton SymbolContext sc; 7606234a5c8SGreg Clayton size_t i = start_idx; 761b9c1b51eSKate Stone while (i < sc_list.GetSize()) { 7626234a5c8SGreg Clayton if (!sc_list.GetContextAtIndex(i, sc)) 7636234a5c8SGreg Clayton break; 76405097246SAdrian Prantl // Make sure the mangled and demangled names don't match before we try to 76505097246SAdrian Prantl // pull anything out 7665d0c1146SGreg Clayton ConstString mangled_name(sc.GetFunctionName(Mangled::ePreferMangled)); 7676234a5c8SGreg Clayton ConstString full_name(sc.GetFunctionName()); 7685d0c1146SGreg Clayton if (mangled_name != m_name && full_name != m_name) 7695d0c1146SGreg Clayton { 7706234a5c8SGreg Clayton CPlusPlusLanguage::MethodName cpp_method(full_name); 771b9c1b51eSKate Stone if (cpp_method.IsValid()) { 772b9c1b51eSKate Stone if (cpp_method.GetContext().empty()) { 773b9c1b51eSKate Stone if (cpp_method.GetBasename().compare(m_name.GetStringRef()) != 0) { 7746234a5c8SGreg Clayton sc_list.RemoveContextAtIndex(i); 7756234a5c8SGreg Clayton continue; 7766234a5c8SGreg Clayton } 777b9c1b51eSKate Stone } else { 7785d0c1146SGreg Clayton std::string qualified_name; 7795d0c1146SGreg Clayton llvm::StringRef anon_prefix("(anonymous namespace)"); 7805d0c1146SGreg Clayton if (cpp_method.GetContext() == anon_prefix) 7815d0c1146SGreg Clayton qualified_name = cpp_method.GetBasename().str(); 7825d0c1146SGreg Clayton else 7835d0c1146SGreg Clayton qualified_name = cpp_method.GetScopeQualifiedName(); 7848d20cfdfSJonas Devlieghere if (qualified_name != m_name.GetCString()) { 7856234a5c8SGreg Clayton sc_list.RemoveContextAtIndex(i); 7866234a5c8SGreg Clayton continue; 7876234a5c8SGreg Clayton } 7886234a5c8SGreg Clayton } 7896234a5c8SGreg Clayton } 7905d0c1146SGreg Clayton } 7916234a5c8SGreg Clayton ++i; 7926234a5c8SGreg Clayton } 7936234a5c8SGreg Clayton } 7946234a5c8SGreg Clayton } 7956234a5c8SGreg Clayton 7961ad655e2SAdrian Prantl void Module::FindFunctions(ConstString name, 797f9568a95SRaphael Isemann const CompilerDeclContext &parent_decl_ctx, 798117b1fa1SZachary Turner FunctionNameType name_type_mask, 799c020be17SJonas Devlieghere const ModuleFunctionSearchOptions &options, 8001ad655e2SAdrian Prantl SymbolContextList &sc_list) { 80143fe217bSGreg Clayton const size_t old_size = sc_list.GetSize(); 802931180e6SGreg Clayton 803931180e6SGreg Clayton // Find all the functions (not symbols, but debug information functions... 80423f70e83SPavel Labath SymbolFile *symbols = GetSymbolFile(); 80543fe217bSGreg Clayton 806b9c1b51eSKate Stone if (name_type_mask & eFunctionNameTypeAuto) { 8076234a5c8SGreg Clayton LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown); 80843fe217bSGreg Clayton 809b9c1b51eSKate Stone if (symbols) { 810b9c1b51eSKate Stone symbols->FindFunctions(lookup_info.GetLookupName(), parent_decl_ctx, 811c020be17SJonas Devlieghere lookup_info.GetNameTypeMask(), 812c020be17SJonas Devlieghere options.include_inlines, sc_list); 81343fe217bSGreg Clayton 814b9c1b51eSKate Stone // Now check our symbol table for symbols that are code symbols if 815b9c1b51eSKate Stone // requested 816c020be17SJonas Devlieghere if (options.include_symbols) { 817a7499c98SMichael Sartain Symtab *symtab = symbols->GetSymtab(); 81843fe217bSGreg Clayton if (symtab) 819b9c1b51eSKate Stone symtab->FindFunctionSymbols(lookup_info.GetLookupName(), 820b9c1b51eSKate Stone lookup_info.GetNameTypeMask(), sc_list); 82143fe217bSGreg Clayton } 82243fe217bSGreg Clayton } 82343fe217bSGreg Clayton 8246234a5c8SGreg Clayton const size_t new_size = sc_list.GetSize(); 8256234a5c8SGreg Clayton 8266234a5c8SGreg Clayton if (old_size < new_size) 8276234a5c8SGreg Clayton lookup_info.Prune(sc_list, old_size); 828b9c1b51eSKate Stone } else { 829b9c1b51eSKate Stone if (symbols) { 830b9c1b51eSKate Stone symbols->FindFunctions(name, parent_decl_ctx, name_type_mask, 831c020be17SJonas Devlieghere options.include_inlines, sc_list); 832931180e6SGreg Clayton 833b9c1b51eSKate Stone // Now check our symbol table for symbols that are code symbols if 834b9c1b51eSKate Stone // requested 835c020be17SJonas Devlieghere if (options.include_symbols) { 836a7499c98SMichael Sartain Symtab *symtab = symbols->GetSymtab(); 837931180e6SGreg Clayton if (symtab) 83843fe217bSGreg Clayton symtab->FindFunctionSymbols(name, name_type_mask, sc_list); 839931180e6SGreg Clayton } 840931180e6SGreg Clayton } 841931180e6SGreg Clayton } 84230fdc8d8SChris Lattner } 84330fdc8d8SChris Lattner 844c020be17SJonas Devlieghere void Module::FindFunctions(const RegularExpression ®ex, 845c020be17SJonas Devlieghere const ModuleFunctionSearchOptions &options, 8461ad655e2SAdrian Prantl SymbolContextList &sc_list) { 847c7bece56SGreg Clayton const size_t start_size = sc_list.GetSize(); 848931180e6SGreg Clayton 84923f70e83SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) { 850c020be17SJonas Devlieghere symbols->FindFunctions(regex, options.include_inlines, sc_list); 851a7499c98SMichael Sartain 85205097246SAdrian Prantl // Now check our symbol table for symbols that are code symbols if 85305097246SAdrian Prantl // requested 854c020be17SJonas Devlieghere if (options.include_symbols) { 855a7499c98SMichael Sartain Symtab *symtab = symbols->GetSymtab(); 856b9c1b51eSKate Stone if (symtab) { 857931180e6SGreg Clayton std::vector<uint32_t> symbol_indexes; 858b9c1b51eSKate Stone symtab->AppendSymbolIndexesMatchingRegExAndType( 859b9c1b51eSKate Stone regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, 860b9c1b51eSKate Stone symbol_indexes); 861c7bece56SGreg Clayton const size_t num_matches = symbol_indexes.size(); 862b9c1b51eSKate Stone if (num_matches) { 863931180e6SGreg Clayton SymbolContext sc(this); 864d8cf1a11SGreg Clayton const size_t end_functions_added_index = sc_list.GetSize(); 865b9c1b51eSKate Stone size_t num_functions_added_to_sc_list = 866b9c1b51eSKate Stone end_functions_added_index - start_size; 867b9c1b51eSKate Stone if (num_functions_added_to_sc_list == 0) { 86805097246SAdrian Prantl // No functions were added, just symbols, so we can just append 86905097246SAdrian Prantl // them 870b9c1b51eSKate Stone for (size_t i = 0; i < num_matches; ++i) { 871931180e6SGreg Clayton sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 87200049b8bSMatt Kopec SymbolType sym_type = sc.symbol->GetType(); 87300049b8bSMatt Kopec if (sc.symbol && (sym_type == eSymbolTypeCode || 87400049b8bSMatt Kopec sym_type == eSymbolTypeResolver)) 875d8cf1a11SGreg Clayton sc_list.Append(sc); 876d8cf1a11SGreg Clayton } 877b9c1b51eSKate Stone } else { 878d8cf1a11SGreg Clayton typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap; 879d8cf1a11SGreg Clayton FileAddrToIndexMap file_addr_to_index; 880b9c1b51eSKate Stone for (size_t i = start_size; i < end_functions_added_index; ++i) { 881d8cf1a11SGreg Clayton const SymbolContext &sc = sc_list[i]; 882d8cf1a11SGreg Clayton if (sc.block) 883d8cf1a11SGreg Clayton continue; 884b9c1b51eSKate Stone file_addr_to_index[sc.function->GetAddressRange() 885b9c1b51eSKate Stone .GetBaseAddress() 886b9c1b51eSKate Stone .GetFileAddress()] = i; 887d8cf1a11SGreg Clayton } 888d8cf1a11SGreg Clayton 889d8cf1a11SGreg Clayton FileAddrToIndexMap::const_iterator end = file_addr_to_index.end(); 890d8cf1a11SGreg Clayton // Functions were added so we need to merge symbols into any 891d8cf1a11SGreg Clayton // existing function symbol contexts 892b9c1b51eSKate Stone for (size_t i = start_size; i < num_matches; ++i) { 893d8cf1a11SGreg Clayton sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 894d8cf1a11SGreg Clayton SymbolType sym_type = sc.symbol->GetType(); 895b9c1b51eSKate Stone if (sc.symbol && sc.symbol->ValueIsAddress() && 896b9c1b51eSKate Stone (sym_type == eSymbolTypeCode || 897b9c1b51eSKate Stone sym_type == eSymbolTypeResolver)) { 898b9c1b51eSKate Stone FileAddrToIndexMap::const_iterator pos = 899b9c1b51eSKate Stone file_addr_to_index.find( 900b9c1b51eSKate Stone sc.symbol->GetAddressRef().GetFileAddress()); 901d8cf1a11SGreg Clayton if (pos == end) 902d8cf1a11SGreg Clayton sc_list.Append(sc); 903d8cf1a11SGreg Clayton else 904d8cf1a11SGreg Clayton sc_list[pos->second].symbol = sc.symbol; 905d8cf1a11SGreg Clayton } 906d8cf1a11SGreg Clayton } 907931180e6SGreg Clayton } 908931180e6SGreg Clayton } 909931180e6SGreg Clayton } 910931180e6SGreg Clayton } 911931180e6SGreg Clayton } 91230fdc8d8SChris Lattner } 91330fdc8d8SChris Lattner 914b9c1b51eSKate Stone void Module::FindAddressesForLine(const lldb::TargetSP target_sp, 915f86248d9SRichard Mitton const FileSpec &file, uint32_t line, 916f86248d9SRichard Mitton Function *function, 917b9c1b51eSKate Stone std::vector<Address> &output_local, 918b9c1b51eSKate Stone std::vector<Address> &output_extern) { 919f86248d9SRichard Mitton SearchFilterByModule filter(target_sp, m_file); 9203e2ed744SMed Ismail Bennani 9213e2ed744SMed Ismail Bennani // TODO: Handle SourceLocationSpec column information 9223e2ed744SMed Ismail Bennani SourceLocationSpec location_spec(file, line, /*column=*/llvm::None, 9233e2ed744SMed Ismail Bennani /*check_inlines=*/true, 9243e2ed744SMed Ismail Bennani /*exact_match=*/false); 9253e2ed744SMed Ismail Bennani AddressResolverFileLine resolver(location_spec); 926f86248d9SRichard Mitton resolver.ResolveAddress(filter); 927f86248d9SRichard Mitton 928b9c1b51eSKate Stone for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++) { 929f86248d9SRichard Mitton Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress(); 930f86248d9SRichard Mitton Function *f = addr.CalculateSymbolContextFunction(); 931f86248d9SRichard Mitton if (f && f == function) 932f86248d9SRichard Mitton output_local.push_back(addr); 933f86248d9SRichard Mitton else 934f86248d9SRichard Mitton output_extern.push_back(addr); 935f86248d9SRichard Mitton } 936f86248d9SRichard Mitton } 937f86248d9SRichard Mitton 938bf9d84c0SAdrian Prantl void Module::FindTypes_Impl( 939f9568a95SRaphael Isemann ConstString name, const CompilerDeclContext &parent_decl_ctx, 940d4d428efSAdrian Prantl size_t max_matches, 941ae088e52SGreg Clayton llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 942b9c1b51eSKate Stone TypeMap &types) { 9435c1c8443SJonas Devlieghere LLDB_SCOPED_TIMER(); 94423f70e83SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 945bf9d84c0SAdrian Prantl symbols->FindTypes(name, parent_decl_ctx, max_matches, 946b9c1b51eSKate Stone searched_symbol_files, types); 9473504eee8SGreg Clayton } 9483504eee8SGreg Clayton 949bf9d84c0SAdrian Prantl void Module::FindTypesInNamespace(ConstString type_name, 950f9568a95SRaphael Isemann const CompilerDeclContext &parent_decl_ctx, 951b9c1b51eSKate Stone size_t max_matches, TypeList &type_list) { 9524069730cSRavitheja Addepally TypeMap types_map; 953ae088e52SGreg Clayton llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; 954bf9d84c0SAdrian Prantl FindTypes_Impl(type_name, parent_decl_ctx, max_matches, searched_symbol_files, 955bf9d84c0SAdrian Prantl types_map); 956bf9d84c0SAdrian Prantl if (types_map.GetSize()) { 957576495e6SZachary Turner SymbolContext sc; 958576495e6SZachary Turner sc.module_sp = shared_from_this(); 9594069730cSRavitheja Addepally sc.SortTypeList(types_map, type_list); 960576495e6SZachary Turner } 9616f3533fbSEnrico Granata } 9626f3533fbSEnrico Granata 963b9c1b51eSKate Stone lldb::TypeSP Module::FindFirstType(const SymbolContext &sc, 9640e4c4821SAdrian Prantl ConstString name, bool exact_match) { 965b43165b7SGreg Clayton TypeList type_list; 966ae088e52SGreg Clayton llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; 967576495e6SZachary Turner FindTypes(name, exact_match, 1, searched_symbol_files, type_list); 968bf9d84c0SAdrian Prantl if (type_list.GetSize()) 969b43165b7SGreg Clayton return type_list.GetTypeAtIndex(0); 970b43165b7SGreg Clayton return TypeSP(); 971b43165b7SGreg Clayton } 972b43165b7SGreg Clayton 973bf9d84c0SAdrian Prantl void Module::FindTypes( 9740e4c4821SAdrian Prantl ConstString name, bool exact_match, size_t max_matches, 975ae088e52SGreg Clayton llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 976b9c1b51eSKate Stone TypeList &types) { 97784db9105SGreg Clayton const char *type_name_cstr = name.GetCString(); 978556b1611STamas Berghammer llvm::StringRef type_scope; 979556b1611STamas Berghammer llvm::StringRef type_basename; 9807bc31332SGreg Clayton TypeClass type_class = eTypeClassAny; 9814069730cSRavitheja Addepally TypeMap typesmap; 9821739b7d0SFrederic Riss 983b9c1b51eSKate Stone if (Type::GetTypeScopeAndBasename(type_name_cstr, type_scope, type_basename, 984b9c1b51eSKate Stone type_class)) { 98584db9105SGreg Clayton // Check if "name" starts with "::" which means the qualified type starts 98684db9105SGreg Clayton // from the root namespace and implies and exact match. The typenames we 98784db9105SGreg Clayton // get back from clang do not start with "::" so we need to strip this off 988d93c4a33SBruce Mitchener // in order to get the qualified names to match 989556b1611STamas Berghammer exact_match = type_scope.consume_front("::"); 9906f3533fbSEnrico Granata 991556b1611STamas Berghammer ConstString type_basename_const_str(type_basename); 992f9568a95SRaphael Isemann FindTypes_Impl(type_basename_const_str, CompilerDeclContext(), max_matches, 993bf9d84c0SAdrian Prantl searched_symbol_files, typesmap); 994bf9d84c0SAdrian Prantl if (typesmap.GetSize()) 995adcd0268SBenjamin Kramer typesmap.RemoveMismatchedTypes(std::string(type_scope), 996adcd0268SBenjamin Kramer std::string(type_basename), type_class, 997b9c1b51eSKate Stone exact_match); 998b9c1b51eSKate Stone } else { 999b9c1b51eSKate Stone // The type is not in a namespace/class scope, just search for it by 1000b9c1b51eSKate Stone // basename 1001c485f056SGreg Clayton if (type_class != eTypeClassAny && !type_basename.empty()) { 1002b9c1b51eSKate Stone // The "type_name_cstr" will have been modified if we have a valid type 100305097246SAdrian Prantl // class prefix (like "struct", "class", "union", "typedef" etc). 1004f9568a95SRaphael Isemann FindTypes_Impl(ConstString(type_basename), CompilerDeclContext(), 1005f9568a95SRaphael Isemann UINT_MAX, searched_symbol_files, typesmap); 1006adcd0268SBenjamin Kramer typesmap.RemoveMismatchedTypes(std::string(type_scope), 1007adcd0268SBenjamin Kramer std::string(type_basename), type_class, 1008c485f056SGreg Clayton exact_match); 1009b9c1b51eSKate Stone } else { 1010f9568a95SRaphael Isemann FindTypes_Impl(name, CompilerDeclContext(), UINT_MAX, 1011f9568a95SRaphael Isemann searched_symbol_files, typesmap); 1012c485f056SGreg Clayton if (exact_match) { 1013c485f056SGreg Clayton std::string name_str(name.AsCString("")); 1014adcd0268SBenjamin Kramer typesmap.RemoveMismatchedTypes(std::string(type_scope), name_str, 1015adcd0268SBenjamin Kramer type_class, exact_match); 1016c485f056SGreg Clayton } 101784db9105SGreg Clayton } 10187bc31332SGreg Clayton } 1019bf9d84c0SAdrian Prantl if (typesmap.GetSize()) { 1020576495e6SZachary Turner SymbolContext sc; 1021576495e6SZachary Turner sc.module_sp = shared_from_this(); 10224069730cSRavitheja Addepally sc.SortTypeList(typesmap, types); 1023576495e6SZachary Turner } 10246f3533fbSEnrico Granata } 10256f3533fbSEnrico Granata 10263b73dcdcSAdrian Prantl void Module::FindTypes( 10273b73dcdcSAdrian Prantl llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages, 10283b73dcdcSAdrian Prantl llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 10293b73dcdcSAdrian Prantl TypeMap &types) { 10305c1c8443SJonas Devlieghere LLDB_SCOPED_TIMER(); 1031aa97a89dSAdrian Prantl if (SymbolFile *symbols = GetSymbolFile()) 10323b73dcdcSAdrian Prantl symbols->FindTypes(pattern, languages, searched_symbol_files, types); 1033aa97a89dSAdrian Prantl } 1034aa97a89dSAdrian Prantl 1035579d6d1aSPavel Labath SymbolFile *Module::GetSymbolFile(bool can_create, Stream *feedback_strm) { 1036579d6d1aSPavel Labath if (!m_did_load_symfile.load()) { 103716ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 1038579d6d1aSPavel Labath if (!m_did_load_symfile.load() && can_create) { 103930fdc8d8SChris Lattner ObjectFile *obj_file = GetObjectFile(); 1040b9c1b51eSKate Stone if (obj_file != nullptr) { 10415c1c8443SJonas Devlieghere LLDB_SCOPED_TIMER(); 1042d5b44036SJonas Devlieghere m_symfile_up.reset( 1043b9c1b51eSKate Stone SymbolVendor::FindPlugin(shared_from_this(), feedback_strm)); 1044579d6d1aSPavel Labath m_did_load_symfile = true; 104530fdc8d8SChris Lattner } 104630fdc8d8SChris Lattner } 104788c05f54SGreg Clayton } 1048579d6d1aSPavel Labath return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr; 104923f70e83SPavel Labath } 105023f70e83SPavel Labath 1051d5d47a35SPavel Labath Symtab *Module::GetSymtab() { 1052d5d47a35SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 1053d5d47a35SPavel Labath return symbols->GetSymtab(); 1054d5d47a35SPavel Labath return nullptr; 1055d5d47a35SPavel Labath } 1056d5d47a35SPavel Labath 1057b9c1b51eSKate Stone void Module::SetFileSpecAndObjectName(const FileSpec &file, 10580e4c4821SAdrian Prantl ConstString object_name) { 105905097246SAdrian Prantl // Container objects whose paths do not specify a file directly can call this 106005097246SAdrian Prantl // function to correct the file and object names. 106130fdc8d8SChris Lattner m_file = file; 106246376966SJonas Devlieghere m_mod_time = FileSystem::Instance().GetModificationTime(file); 106330fdc8d8SChris Lattner m_object_name = object_name; 106430fdc8d8SChris Lattner } 106530fdc8d8SChris Lattner 1066b9c1b51eSKate Stone const ArchSpec &Module::GetArchitecture() const { return m_arch; } 106730fdc8d8SChris Lattner 1068b9c1b51eSKate Stone std::string Module::GetSpecificationDescription() const { 1069b5ad4ec7SGreg Clayton std::string spec(GetFileSpec().GetPath()); 1070b9c1b51eSKate Stone if (m_object_name) { 1071b5ad4ec7SGreg Clayton spec += '('; 1072b5ad4ec7SGreg Clayton spec += m_object_name.GetCString(); 1073b5ad4ec7SGreg Clayton spec += ')'; 1074b5ad4ec7SGreg Clayton } 1075b5ad4ec7SGreg Clayton return spec; 1076b5ad4ec7SGreg Clayton } 1077b5ad4ec7SGreg Clayton 1078c4c464f8SRaphael Isemann void Module::GetDescription(llvm::raw_ostream &s, 1079c4c464f8SRaphael Isemann lldb::DescriptionLevel level) { 1080b9c1b51eSKate Stone if (level >= eDescriptionLevelFull) { 1081cfd1acedSGreg Clayton if (m_arch.IsValid()) 1082c4c464f8SRaphael Isemann s << llvm::formatv("({0}) ", m_arch.GetArchitectureName()); 1083c982b3d6SGreg Clayton } 1084ceb6b139SCaroline Tice 1085b9c1b51eSKate Stone if (level == eDescriptionLevelBrief) { 1086c982b3d6SGreg Clayton const char *filename = m_file.GetFilename().GetCString(); 1087c982b3d6SGreg Clayton if (filename) 1088c4c464f8SRaphael Isemann s << filename; 1089b9c1b51eSKate Stone } else { 1090cfd1acedSGreg Clayton char path[PATH_MAX]; 1091cfd1acedSGreg Clayton if (m_file.GetPath(path, sizeof(path))) 1092c4c464f8SRaphael Isemann s << path; 1093c982b3d6SGreg Clayton } 1094cfd1acedSGreg Clayton 1095cfd1acedSGreg Clayton const char *object_name = m_object_name.GetCString(); 1096cfd1acedSGreg Clayton if (object_name) 1097c4c464f8SRaphael Isemann s << llvm::formatv("({0})", object_name); 1098ceb6b139SCaroline Tice } 1099ceb6b139SCaroline Tice 1100b9c1b51eSKate Stone void Module::ReportError(const char *format, ...) { 1101b9c1b51eSKate Stone if (format && format[0]) { 1102e38a5eddSGreg Clayton StreamString strm; 1103e38a5eddSGreg Clayton strm.PutCString("error: "); 1104c4c464f8SRaphael Isemann GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelBrief); 11058b35334eSGreg Clayton strm.PutChar(' '); 1106c982b3d6SGreg Clayton va_list args; 1107c982b3d6SGreg Clayton va_start(args, format); 1108e38a5eddSGreg Clayton strm.PrintfVarArg(format, args); 1109c982b3d6SGreg Clayton va_end(args); 1110e38a5eddSGreg Clayton 1111e38a5eddSGreg Clayton const int format_len = strlen(format); 1112b9c1b51eSKate Stone if (format_len > 0) { 1113e38a5eddSGreg Clayton const char last_char = format[format_len - 1]; 1114376230c9SRaphael Isemann if (last_char != '\n' && last_char != '\r') 1115e38a5eddSGreg Clayton strm.EOL(); 1116e38a5eddSGreg Clayton } 1117c156427dSZachary Turner Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData()); 1118e38a5eddSGreg Clayton } 1119e38a5eddSGreg Clayton } 1120e38a5eddSGreg Clayton 1121b9c1b51eSKate Stone bool Module::FileHasChanged() const { 1122a4a00cedSFred Riss // We have provided the DataBuffer for this module to avoid accessing the 1123a4a00cedSFred Riss // filesystem. We never want to reload those files. 1124a4a00cedSFred Riss if (m_data_sp) 1125a4a00cedSFred Riss return false; 1126c5dac77aSEugene Zelenko if (!m_file_has_changed) 11271408bf72SPavel Labath m_file_has_changed = 112846376966SJonas Devlieghere (FileSystem::Instance().GetModificationTime(m_file) != m_mod_time); 11291d60909eSGreg Clayton return m_file_has_changed; 11301d60909eSGreg Clayton } 11311d60909eSGreg Clayton 1132b9c1b51eSKate Stone void Module::ReportErrorIfModifyDetected(const char *format, ...) { 1133b9c1b51eSKate Stone if (!m_first_file_changed_log) { 1134b9c1b51eSKate Stone if (FileHasChanged()) { 11351d60909eSGreg Clayton m_first_file_changed_log = true; 1136b9c1b51eSKate Stone if (format) { 1137e38a5eddSGreg Clayton StreamString strm; 1138e38a5eddSGreg Clayton strm.PutCString("error: the object file "); 1139c4c464f8SRaphael Isemann GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull); 1140e38a5eddSGreg Clayton strm.PutCString(" has been modified\n"); 1141e38a5eddSGreg Clayton 1142e38a5eddSGreg Clayton va_list args; 1143e38a5eddSGreg Clayton va_start(args, format); 1144e38a5eddSGreg Clayton strm.PrintfVarArg(format, args); 1145e38a5eddSGreg Clayton va_end(args); 1146e38a5eddSGreg Clayton 1147e38a5eddSGreg Clayton const int format_len = strlen(format); 1148b9c1b51eSKate Stone if (format_len > 0) { 1149e38a5eddSGreg Clayton const char last_char = format[format_len - 1]; 1150376230c9SRaphael Isemann if (last_char != '\n' && last_char != '\r') 1151e38a5eddSGreg Clayton strm.EOL(); 1152e38a5eddSGreg Clayton } 1153b9c1b51eSKate Stone strm.PutCString("The debug session should be aborted as the original " 1154b9c1b51eSKate Stone "debug information has been overwritten.\n"); 1155c156427dSZachary Turner Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData()); 1156e38a5eddSGreg Clayton } 1157e38a5eddSGreg Clayton } 1158c982b3d6SGreg Clayton } 11591d60909eSGreg Clayton } 1160c982b3d6SGreg Clayton 1161b9c1b51eSKate Stone void Module::ReportWarning(const char *format, ...) { 1162b9c1b51eSKate Stone if (format && format[0]) { 1163e38a5eddSGreg Clayton StreamString strm; 1164e38a5eddSGreg Clayton strm.PutCString("warning: "); 1165c4c464f8SRaphael Isemann GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull); 11668b35334eSGreg Clayton strm.PutChar(' '); 1167c982b3d6SGreg Clayton 1168c982b3d6SGreg Clayton va_list args; 1169c982b3d6SGreg Clayton va_start(args, format); 1170e38a5eddSGreg Clayton strm.PrintfVarArg(format, args); 1171c982b3d6SGreg Clayton va_end(args); 1172e38a5eddSGreg Clayton 1173e38a5eddSGreg Clayton const int format_len = strlen(format); 1174b9c1b51eSKate Stone if (format_len > 0) { 1175e38a5eddSGreg Clayton const char last_char = format[format_len - 1]; 1176376230c9SRaphael Isemann if (last_char != '\n' && last_char != '\r') 1177e38a5eddSGreg Clayton strm.EOL(); 1178e38a5eddSGreg Clayton } 1179c156427dSZachary Turner Host::SystemLog(Host::eSystemLogWarning, "%s", strm.GetData()); 1180e38a5eddSGreg Clayton } 1181c982b3d6SGreg Clayton } 1182c982b3d6SGreg Clayton 1183b9c1b51eSKate Stone void Module::LogMessage(Log *log, const char *format, ...) { 1184b9c1b51eSKate Stone if (log != nullptr) { 1185c982b3d6SGreg Clayton StreamString log_message; 1186c4c464f8SRaphael Isemann GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull); 1187c982b3d6SGreg Clayton log_message.PutCString(": "); 1188c982b3d6SGreg Clayton va_list args; 1189c982b3d6SGreg Clayton va_start(args, format); 1190c982b3d6SGreg Clayton log_message.PrintfVarArg(format, args); 1191c982b3d6SGreg Clayton va_end(args); 1192c156427dSZachary Turner log->PutCString(log_message.GetData()); 1193c982b3d6SGreg Clayton } 1194c982b3d6SGreg Clayton } 1195c982b3d6SGreg Clayton 1196b9c1b51eSKate Stone void Module::LogMessageVerboseBacktrace(Log *log, const char *format, ...) { 1197b9c1b51eSKate Stone if (log != nullptr) { 1198d61c0fc0SGreg Clayton StreamString log_message; 1199c4c464f8SRaphael Isemann GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull); 1200d61c0fc0SGreg Clayton log_message.PutCString(": "); 1201d61c0fc0SGreg Clayton va_list args; 1202d61c0fc0SGreg Clayton va_start(args, format); 1203d61c0fc0SGreg Clayton log_message.PrintfVarArg(format, args); 1204d61c0fc0SGreg Clayton va_end(args); 1205b9c1b51eSKate Stone if (log->GetVerbose()) { 1206a893d301SZachary Turner std::string back_trace; 1207a893d301SZachary Turner llvm::raw_string_ostream stream(back_trace); 1208a893d301SZachary Turner llvm::sys::PrintStackTrace(stream); 1209771ef6d4SMalcolm Parsons log_message.PutCString(back_trace); 1210a893d301SZachary Turner } 1211c156427dSZachary Turner log->PutCString(log_message.GetData()); 1212d61c0fc0SGreg Clayton } 1213d61c0fc0SGreg Clayton } 1214d61c0fc0SGreg Clayton 1215b9c1b51eSKate Stone void Module::Dump(Stream *s) { 121616ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 12178941142aSGreg Clayton // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 121830fdc8d8SChris Lattner s->Indent(); 1219b9c1b51eSKate Stone s->Printf("Module %s%s%s%s\n", m_file.GetPath().c_str(), 122030fdc8d8SChris Lattner m_object_name ? "(" : "", 122130fdc8d8SChris Lattner m_object_name ? m_object_name.GetCString() : "", 122230fdc8d8SChris Lattner m_object_name ? ")" : ""); 122330fdc8d8SChris Lattner 122430fdc8d8SChris Lattner s->IndentMore(); 122530fdc8d8SChris Lattner 1226a7499c98SMichael Sartain ObjectFile *objfile = GetObjectFile(); 122730fdc8d8SChris Lattner if (objfile) 122830fdc8d8SChris Lattner objfile->Dump(s); 122930fdc8d8SChris Lattner 1230d5d47a35SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 1231d5d47a35SPavel Labath symbols->Dump(*s); 123230fdc8d8SChris Lattner 123330fdc8d8SChris Lattner s->IndentLess(); 123430fdc8d8SChris Lattner } 123530fdc8d8SChris Lattner 12360e4c4821SAdrian Prantl ConstString Module::GetObjectName() const { return m_object_name; } 123730fdc8d8SChris Lattner 1238b9c1b51eSKate Stone ObjectFile *Module::GetObjectFile() { 1239b9c1b51eSKate Stone if (!m_did_load_objfile.load()) { 124016ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 1241b9c1b51eSKate Stone if (!m_did_load_objfile.load()) { 12425c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF("Module::GetObjectFile () module = %s", 1243b9c1b51eSKate Stone GetFileSpec().GetFilename().AsCString("")); 12445ce9c565SGreg Clayton lldb::offset_t data_offset = 0; 1245a4a00cedSFred Riss lldb::offset_t file_size = 0; 1246a4a00cedSFred Riss 1247a4a00cedSFred Riss if (m_data_sp) 1248a4a00cedSFred Riss file_size = m_data_sp->GetByteSize(); 1249a4a00cedSFred Riss else if (m_file) 1250a4a00cedSFred Riss file_size = FileSystem::Instance().GetByteSize(m_file); 1251a4a00cedSFred Riss 1252b9c1b51eSKate Stone if (file_size > m_object_offset) { 12532540a8a7SGreg Clayton m_did_load_objfile = true; 1254a4a00cedSFred Riss // FindPlugin will modify its data_sp argument. Do not let it 1255a4a00cedSFred Riss // modify our m_data_sp member. 1256a4a00cedSFred Riss auto data_sp = m_data_sp; 1257b9c1b51eSKate Stone m_objfile_sp = ObjectFile::FindPlugin( 1258b9c1b51eSKate Stone shared_from_this(), &m_file, m_object_offset, 1259b9c1b51eSKate Stone file_size - m_object_offset, data_sp, data_offset); 1260b9c1b51eSKate Stone if (m_objfile_sp) { 1261b9c1b51eSKate Stone // Once we get the object file, update our module with the object 126205097246SAdrian Prantl // file's architecture since it might differ in vendor/os if some 126305097246SAdrian Prantl // parts were unknown. But since the matching arch might already be 126405097246SAdrian Prantl // more specific than the generic COFF architecture, only merge in 126505097246SAdrian Prantl // those values that overwrite unspecified unknown values. 1266f760f5aeSPavel Labath m_arch.MergeFrom(m_objfile_sp->GetArchitecture()); 1267b9c1b51eSKate Stone } else { 1268b9c1b51eSKate Stone ReportError("failed to load objfile for %s", 1269b9c1b51eSKate Stone GetFileSpec().GetPath().c_str()); 12700ee56ce6STodd Fiala } 127130fdc8d8SChris Lattner } 12722540a8a7SGreg Clayton } 127388c05f54SGreg Clayton } 1274762f7135SGreg Clayton return m_objfile_sp.get(); 127530fdc8d8SChris Lattner } 127630fdc8d8SChris Lattner 1277b9c1b51eSKate Stone SectionList *Module::GetSectionList() { 1278d5b44036SJonas Devlieghere // Populate m_sections_up with sections from objfile. 1279d5b44036SJonas Devlieghere if (!m_sections_up) { 12803046e668SGreg Clayton ObjectFile *obj_file = GetObjectFile(); 1281c5dac77aSEugene Zelenko if (obj_file != nullptr) 12823046e668SGreg Clayton obj_file->CreateSections(*GetUnifiedSectionList()); 12833046e668SGreg Clayton } 1284d5b44036SJonas Devlieghere return m_sections_up.get(); 12853046e668SGreg Clayton } 12863046e668SGreg Clayton 1287b9c1b51eSKate Stone void Module::SectionFileAddressesChanged() { 128805a09c67SJason Molenda ObjectFile *obj_file = GetObjectFile(); 128905a09c67SJason Molenda if (obj_file) 129005a09c67SJason Molenda obj_file->SectionFileAddressesChanged(); 129123f70e83SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 129223f70e83SPavel Labath symbols->SectionFileAddressesChanged(); 129305a09c67SJason Molenda } 129405a09c67SJason Molenda 1295dec96392SPavel Labath UnwindTable &Module::GetUnwindTable() { 1296dec96392SPavel Labath if (!m_unwind_table) 1297dec96392SPavel Labath m_unwind_table.emplace(*this); 1298dec96392SPavel Labath return *m_unwind_table; 1299dec96392SPavel Labath } 1300dec96392SPavel Labath 1301b9c1b51eSKate Stone SectionList *Module::GetUnifiedSectionList() { 1302d5b44036SJonas Devlieghere if (!m_sections_up) 1303a8f3ae7cSJonas Devlieghere m_sections_up = std::make_unique<SectionList>(); 1304d5b44036SJonas Devlieghere return m_sections_up.get(); 1305a7499c98SMichael Sartain } 130630fdc8d8SChris Lattner 13070e4c4821SAdrian Prantl const Symbol *Module::FindFirstSymbolWithNameAndType(ConstString name, 1308b9c1b51eSKate Stone SymbolType symbol_type) { 13095c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF( 13105c1c8443SJonas Devlieghere "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)", 1311b9c1b51eSKate Stone name.AsCString(), symbol_type); 1312d5d47a35SPavel Labath if (Symtab *symtab = GetSymtab()) 1313b9c1b51eSKate Stone return symtab->FindFirstSymbolWithNameAndType( 1314b9c1b51eSKate Stone name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny); 1315c5dac77aSEugene Zelenko return nullptr; 131630fdc8d8SChris Lattner } 1317b9c1b51eSKate Stone void Module::SymbolIndicesToSymbolContextList( 1318b9c1b51eSKate Stone Symtab *symtab, std::vector<uint32_t> &symbol_indexes, 1319b9c1b51eSKate Stone SymbolContextList &sc_list) { 132030fdc8d8SChris Lattner // No need to protect this call using m_mutex all other method calls are 132130fdc8d8SChris Lattner // already thread safe. 132230fdc8d8SChris Lattner 132330fdc8d8SChris Lattner size_t num_indices = symbol_indexes.size(); 1324b9c1b51eSKate Stone if (num_indices > 0) { 132530fdc8d8SChris Lattner SymbolContext sc; 132630fdc8d8SChris Lattner CalculateSymbolContext(&sc); 1327b9c1b51eSKate Stone for (size_t i = 0; i < num_indices; i++) { 132830fdc8d8SChris Lattner sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 132930fdc8d8SChris Lattner if (sc.symbol) 133030fdc8d8SChris Lattner sc_list.Append(sc); 133130fdc8d8SChris Lattner } 133230fdc8d8SChris Lattner } 133330fdc8d8SChris Lattner } 133430fdc8d8SChris Lattner 13351ad655e2SAdrian Prantl void Module::FindFunctionSymbols(ConstString name, 1336c1b2ccfdSGreg Clayton uint32_t name_type_mask, 1337b9c1b51eSKate Stone SymbolContextList &sc_list) { 13385c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF("Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)", 1339b9c1b51eSKate Stone name.AsCString(), name_type_mask); 1340d5d47a35SPavel Labath if (Symtab *symtab = GetSymtab()) 13411ad655e2SAdrian Prantl symtab->FindFunctionSymbols(name, name_type_mask, sc_list); 1342c1b2ccfdSGreg Clayton } 1343c1b2ccfdSGreg Clayton 13441ad655e2SAdrian Prantl void Module::FindSymbolsWithNameAndType(ConstString name, 1345b9c1b51eSKate Stone SymbolType symbol_type, 1346b9c1b51eSKate Stone SymbolContextList &sc_list) { 134730fdc8d8SChris Lattner // No need to protect this call using m_mutex all other method calls are 134830fdc8d8SChris Lattner // already thread safe. 13495c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF( 13505c1c8443SJonas Devlieghere "Module::FindSymbolsWithNameAndType (name = %s, type = %i)", 1351b9c1b51eSKate Stone name.AsCString(), symbol_type); 1352d5d47a35SPavel Labath if (Symtab *symtab = GetSymtab()) { 135330fdc8d8SChris Lattner std::vector<uint32_t> symbol_indexes; 135430fdc8d8SChris Lattner symtab->FindAllSymbolsWithNameAndType(name, symbol_type, symbol_indexes); 135530fdc8d8SChris Lattner SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list); 135630fdc8d8SChris Lattner } 135730fdc8d8SChris Lattner } 135830fdc8d8SChris Lattner 13591ad655e2SAdrian Prantl void Module::FindSymbolsMatchingRegExAndType(const RegularExpression ®ex, 1360b9c1b51eSKate Stone SymbolType symbol_type, 1361b9c1b51eSKate Stone SymbolContextList &sc_list) { 136230fdc8d8SChris Lattner // No need to protect this call using m_mutex all other method calls are 136330fdc8d8SChris Lattner // already thread safe. 13645c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF( 136530fdc8d8SChris Lattner "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)", 136695eae423SZachary Turner regex.GetText().str().c_str(), symbol_type); 1367d5d47a35SPavel Labath if (Symtab *symtab = GetSymtab()) { 136830fdc8d8SChris Lattner std::vector<uint32_t> symbol_indexes; 1369b9c1b51eSKate Stone symtab->FindAllSymbolsMatchingRexExAndType( 1370b9c1b51eSKate Stone regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, 1371b9c1b51eSKate Stone symbol_indexes); 137230fdc8d8SChris Lattner SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list); 137330fdc8d8SChris Lattner } 137430fdc8d8SChris Lattner } 137530fdc8d8SChris Lattner 13767fca8c07SJim Ingham void Module::PreloadSymbols() { 13777fca8c07SJim Ingham std::lock_guard<std::recursive_mutex> guard(m_mutex); 137823f70e83SPavel Labath SymbolFile *sym_file = GetSymbolFile(); 137923f70e83SPavel Labath if (!sym_file) 13807fca8c07SJim Ingham return; 138123f70e83SPavel Labath 13827fca8c07SJim Ingham // Prime the symbol file first, since it adds symbols to the symbol table. 138323f70e83SPavel Labath sym_file->PreloadSymbols(); 138423f70e83SPavel Labath 13857fca8c07SJim Ingham // Now we can prime the symbol table. 138623f70e83SPavel Labath if (Symtab *symtab = sym_file->GetSymtab()) 13877fca8c07SJim Ingham symtab->PreloadSymbols(); 13887fca8c07SJim Ingham } 13897fca8c07SJim Ingham 1390b9c1b51eSKate Stone void Module::SetSymbolFileFileSpec(const FileSpec &file) { 1391dbd7fabaSJonas Devlieghere if (!FileSystem::Instance().Exists(file)) 139290271672SGreg Clayton return; 1393d5b44036SJonas Devlieghere if (m_symfile_up) { 1394b9c1b51eSKate Stone // Remove any sections in the unified section list that come from the 1395b9c1b51eSKate Stone // current symbol vendor. 13963046e668SGreg Clayton SectionList *section_list = GetSectionList(); 139723f70e83SPavel Labath SymbolFile *symbol_file = GetSymbolFile(); 1398b9c1b51eSKate Stone if (section_list && symbol_file) { 1399a7499c98SMichael Sartain ObjectFile *obj_file = symbol_file->GetObjectFile(); 1400b9c1b51eSKate Stone // Make sure we have an object file and that the symbol vendor's objfile 140105097246SAdrian Prantl // isn't the same as the module's objfile before we remove any sections 140205097246SAdrian Prantl // for it... 1403b9c1b51eSKate Stone if (obj_file) { 1404b9c1b51eSKate Stone // Check to make sure we aren't trying to specify the file we already 1405b9c1b51eSKate Stone // have 1406b9c1b51eSKate Stone if (obj_file->GetFileSpec() == file) { 140790271672SGreg Clayton // We are being told to add the exact same file that we already have 140890271672SGreg Clayton // we don't have to do anything. 140990271672SGreg Clayton return; 141090271672SGreg Clayton } 1411d00438e8STamas Berghammer 1412b9c1b51eSKate Stone // Cleare the current symtab as we are going to replace it with a new 1413b9c1b51eSKate Stone // one 1414d00438e8STamas Berghammer obj_file->ClearSymtab(); 141590271672SGreg Clayton 1416dec96392SPavel Labath // Clear the unwind table too, as that may also be affected by the 1417dec96392SPavel Labath // symbol file information. 1418dec96392SPavel Labath m_unwind_table.reset(); 1419dec96392SPavel Labath 1420b9c1b51eSKate Stone // The symbol file might be a directory bundle ("/tmp/a.out.dSYM") 142105097246SAdrian Prantl // instead of a full path to the symbol file within the bundle 1422b9c1b51eSKate Stone // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to 1423b9c1b51eSKate Stone // check this 142490271672SGreg Clayton 14253a58d898SJonas Devlieghere if (FileSystem::Instance().IsDirectory(file)) { 142690271672SGreg Clayton std::string new_path(file.GetPath()); 142790271672SGreg Clayton std::string old_path(obj_file->GetFileSpec().GetPath()); 1428e8f13f4fSBenjamin Kramer if (llvm::StringRef(old_path).startswith(new_path)) { 1429b9c1b51eSKate Stone // We specified the same bundle as the symbol file that we already 1430b9c1b51eSKate Stone // have 143190271672SGreg Clayton return; 143290271672SGreg Clayton } 143390271672SGreg Clayton } 143490271672SGreg Clayton 1435b9c1b51eSKate Stone if (obj_file != m_objfile_sp.get()) { 1436a7499c98SMichael Sartain size_t num_sections = section_list->GetNumSections(0); 1437b9c1b51eSKate Stone for (size_t idx = num_sections; idx > 0; --idx) { 1438b9c1b51eSKate Stone lldb::SectionSP section_sp( 1439b9c1b51eSKate Stone section_list->GetSectionAtIndex(idx - 1)); 1440b9c1b51eSKate Stone if (section_sp->GetObjectFile() == obj_file) { 14413046e668SGreg Clayton section_list->DeleteSection(idx - 1); 1442a7499c98SMichael Sartain } 1443a7499c98SMichael Sartain } 1444a7499c98SMichael Sartain } 1445a7499c98SMichael Sartain } 1446a7499c98SMichael Sartain } 1447b9c1b51eSKate Stone // Keep all old symbol files around in case there are any lingering type 144805097246SAdrian Prantl // references in any SBValue objects that might have been handed out. 1449d5b44036SJonas Devlieghere m_old_symfiles.push_back(std::move(m_symfile_up)); 145090271672SGreg Clayton } 1451e01e07b6SGreg Clayton m_symfile_spec = file; 1452d5b44036SJonas Devlieghere m_symfile_up.reset(); 1453579d6d1aSPavel Labath m_did_load_symfile = false; 1454e01e07b6SGreg Clayton } 1455e01e07b6SGreg Clayton 1456b9c1b51eSKate Stone bool Module::IsExecutable() { 1457c5dac77aSEugene Zelenko if (GetObjectFile() == nullptr) 14585aee162fSJim Ingham return false; 14595aee162fSJim Ingham else 14605aee162fSJim Ingham return GetObjectFile()->IsExecutable(); 14615aee162fSJim Ingham } 14625aee162fSJim Ingham 1463b9c1b51eSKate Stone bool Module::IsLoadedInTarget(Target *target) { 1464b53cb271SJim Ingham ObjectFile *obj_file = GetObjectFile(); 1465b9c1b51eSKate Stone if (obj_file) { 14663046e668SGreg Clayton SectionList *sections = GetSectionList(); 1467b9c1b51eSKate Stone if (sections != nullptr) { 1468b53cb271SJim Ingham size_t num_sections = sections->GetSize(); 1469b9c1b51eSKate Stone for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) { 1470b53cb271SJim Ingham SectionSP section_sp = sections->GetSectionAtIndex(sect_idx); 1471b9c1b51eSKate Stone if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) { 1472b53cb271SJim Ingham return true; 1473b53cb271SJim Ingham } 1474b53cb271SJim Ingham } 1475b53cb271SJim Ingham } 1476b53cb271SJim Ingham } 1477b53cb271SJim Ingham return false; 1478b53cb271SJim Ingham } 14791759848bSEnrico Granata 148097206d57SZachary Turner bool Module::LoadScriptingResourceInTarget(Target *target, Status &error, 1481b9c1b51eSKate Stone Stream *feedback_stream) { 1482b9c1b51eSKate Stone if (!target) { 14831759848bSEnrico Granata error.SetErrorString("invalid destination Target"); 14841759848bSEnrico Granata return false; 14851759848bSEnrico Granata } 14861759848bSEnrico Granata 1487b9c1b51eSKate Stone LoadScriptFromSymFile should_load = 1488b9c1b51eSKate Stone target->TargetProperties::GetLoadScriptFromSymbolFile(); 14892ea43cdcSEnrico Granata 1490994740fbSGreg Clayton if (should_load == eLoadScriptFromSymFileFalse) 1491994740fbSGreg Clayton return false; 1492994740fbSGreg Clayton 149391c0e749SGreg Clayton Debugger &debugger = target->GetDebugger(); 149491c0e749SGreg Clayton const ScriptLanguage script_language = debugger.GetScriptLanguage(); 1495b9c1b51eSKate Stone if (script_language != eScriptLanguageNone) { 149691c0e749SGreg Clayton 14971759848bSEnrico Granata PlatformSP platform_sp(target->GetPlatform()); 14981759848bSEnrico Granata 1499b9c1b51eSKate Stone if (!platform_sp) { 15001759848bSEnrico Granata error.SetErrorString("invalid Platform"); 15011759848bSEnrico Granata return false; 15021759848bSEnrico Granata } 15031759848bSEnrico Granata 1504b9c1b51eSKate Stone FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources( 1505b9c1b51eSKate Stone target, *this, feedback_stream); 150691c0e749SGreg Clayton 150791c0e749SGreg Clayton const uint32_t num_specs = file_specs.GetSize(); 1508b9c1b51eSKate Stone if (num_specs) { 15092b29b432SJonas Devlieghere ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter(); 1510b9c1b51eSKate Stone if (script_interpreter) { 1511b9c1b51eSKate Stone for (uint32_t i = 0; i < num_specs; ++i) { 151291c0e749SGreg Clayton FileSpec scripting_fspec(file_specs.GetFileSpecAtIndex(i)); 1513dbd7fabaSJonas Devlieghere if (scripting_fspec && 1514dbd7fabaSJonas Devlieghere FileSystem::Instance().Exists(scripting_fspec)) { 1515b9c1b51eSKate Stone if (should_load == eLoadScriptFromSymFileWarn) { 1516397ddd5fSEnrico Granata if (feedback_stream) 1517b9c1b51eSKate Stone feedback_stream->Printf( 1518b9c1b51eSKate Stone "warning: '%s' contains a debug script. To run this script " 1519b9c1b51eSKate Stone "in " 1520b9c1b51eSKate Stone "this debug session:\n\n command script import " 1521b9c1b51eSKate Stone "\"%s\"\n\n" 1522d516deb4SJim Ingham "To run all discovered debug scripts in this session:\n\n" 1523b9c1b51eSKate Stone " settings set target.load-script-from-symbol-file " 1524b9c1b51eSKate Stone "true\n", 1525d516deb4SJim Ingham GetFileSpec().GetFileNameStrippingExtension().GetCString(), 1526d516deb4SJim Ingham scripting_fspec.GetPath().c_str()); 15272ea43cdcSEnrico Granata return false; 15282ea43cdcSEnrico Granata } 15291759848bSEnrico Granata StreamString scripting_stream; 15304dac97ebSRaphael Isemann scripting_fspec.Dump(scripting_stream.AsRawOstream()); 1531f9517353SJonas Devlieghere LoadScriptOptions options; 1532b9c1b51eSKate Stone bool did_load = script_interpreter->LoadScriptingModule( 1533f9517353SJonas Devlieghere scripting_stream.GetData(), options, error); 15341759848bSEnrico Granata if (!did_load) 15351759848bSEnrico Granata return false; 15361759848bSEnrico Granata } 153791c0e749SGreg Clayton } 1538b9c1b51eSKate Stone } else { 15391759848bSEnrico Granata error.SetErrorString("invalid ScriptInterpreter"); 15401759848bSEnrico Granata return false; 15411759848bSEnrico Granata } 15421759848bSEnrico Granata } 1543b9d8890bSGreg Clayton } 15441759848bSEnrico Granata return true; 15451759848bSEnrico Granata } 15461759848bSEnrico Granata 1547b9c1b51eSKate Stone bool Module::SetArchitecture(const ArchSpec &new_arch) { 1548b9c1b51eSKate Stone if (!m_arch.IsValid()) { 15495aee162fSJim Ingham m_arch = new_arch; 15505aee162fSJim Ingham return true; 15515aee162fSJim Ingham } 1552b6cd5fe9SChaoren Lin return m_arch.IsCompatibleMatch(new_arch); 15535aee162fSJim Ingham } 15545aee162fSJim Ingham 1555b9c1b51eSKate Stone bool Module::SetLoadAddress(Target &target, lldb::addr_t value, 1556b9c1b51eSKate Stone bool value_is_offset, bool &changed) { 15579e02dacdSSteve Pucci ObjectFile *object_file = GetObjectFile(); 1558b9c1b51eSKate Stone if (object_file != nullptr) { 1559751caf65SGreg Clayton changed = object_file->SetLoadAddress(target, value, value_is_offset); 15607524e090SGreg Clayton return true; 1561b9c1b51eSKate Stone } else { 15627524e090SGreg Clayton changed = false; 1563c9660546SGreg Clayton } 15649e02dacdSSteve Pucci return false; 1565c9660546SGreg Clayton } 1566c9660546SGreg Clayton 1567b9c1b51eSKate Stone bool Module::MatchesModuleSpec(const ModuleSpec &module_ref) { 1568b9a01b39SGreg Clayton const UUID &uuid = module_ref.GetUUID(); 1569b9a01b39SGreg Clayton 1570b9c1b51eSKate Stone if (uuid.IsValid()) { 1571b9a01b39SGreg Clayton // If the UUID matches, then nothing more needs to match... 1572c5dac77aSEugene Zelenko return (uuid == GetUUID()); 1573b9a01b39SGreg Clayton } 1574b9a01b39SGreg Clayton 1575b9a01b39SGreg Clayton const FileSpec &file_spec = module_ref.GetFileSpec(); 1576532290e6SPavel Labath if (!FileSpec::Match(file_spec, m_file) && 1577532290e6SPavel Labath !FileSpec::Match(file_spec, m_platform_file)) 1578b9a01b39SGreg Clayton return false; 1579b9a01b39SGreg Clayton 1580b9a01b39SGreg Clayton const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec(); 1581532290e6SPavel Labath if (!FileSpec::Match(platform_file_spec, GetPlatformFileSpec())) 1582b9a01b39SGreg Clayton return false; 1583b9a01b39SGreg Clayton 1584b9a01b39SGreg Clayton const ArchSpec &arch = module_ref.GetArchitecture(); 1585b9c1b51eSKate Stone if (arch.IsValid()) { 1586bf4b7be6SSean Callanan if (!m_arch.IsCompatibleMatch(arch)) 1587b9a01b39SGreg Clayton return false; 1588b9a01b39SGreg Clayton } 1589b9a01b39SGreg Clayton 15900e4c4821SAdrian Prantl ConstString object_name = module_ref.GetObjectName(); 1591b9c1b51eSKate Stone if (object_name) { 1592b9a01b39SGreg Clayton if (object_name != GetObjectName()) 1593b9a01b39SGreg Clayton return false; 1594b9a01b39SGreg Clayton } 1595b9a01b39SGreg Clayton return true; 1596b9a01b39SGreg Clayton } 1597b9a01b39SGreg Clayton 1598b9c1b51eSKate Stone bool Module::FindSourceFile(const FileSpec &orig_spec, 1599b9c1b51eSKate Stone FileSpec &new_spec) const { 160016ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 1601a3463722SAdrian Prantl if (auto remapped = m_source_mappings.FindFile(orig_spec)) { 1602a3463722SAdrian Prantl new_spec = *remapped; 1603a3463722SAdrian Prantl return true; 1604a3463722SAdrian Prantl } 1605a3463722SAdrian Prantl return false; 1606d804d285SGreg Clayton } 1607d804d285SGreg Clayton 1608a0e1b11fSAdrian Prantl llvm::Optional<std::string> Module::RemapSourceFile(llvm::StringRef path) const { 160916ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 1610a0e1b11fSAdrian Prantl if (auto remapped = m_source_mappings.RemapPath(path)) 1611a0e1b11fSAdrian Prantl return remapped->GetPath(); 1612a0e1b11fSAdrian Prantl return {}; 1613f9be6933SGreg Clayton } 1614f9be6933SGreg Clayton 16151e05d7b3SAdrian Prantl void Module::RegisterXcodeSDK(llvm::StringRef sdk_name, llvm::StringRef sysroot) { 16161e05d7b3SAdrian Prantl XcodeSDK sdk(sdk_name.str()); 1617*dfd499a6SXu Jun llvm::StringRef sdk_path(HostInfo::GetXcodeSDKPath(sdk)); 1618*dfd499a6SXu Jun if (sdk_path.empty()) 16191e05d7b3SAdrian Prantl return; 1620f0c08b7eSAdrian Prantl // If the SDK changed for a previously registered source path, update it. 16211e05d7b3SAdrian Prantl // This could happend with -fdebug-prefix-map, otherwise it's unlikely. 1622*dfd499a6SXu Jun if (!m_source_mappings.Replace(sysroot, sdk_path, true)) 16231e05d7b3SAdrian Prantl // In the general case, however, append it to the list. 1624*dfd499a6SXu Jun m_source_mappings.Append(sysroot, sdk_path, false); 16251e05d7b3SAdrian Prantl } 16261e05d7b3SAdrian Prantl 162724610611SAdrian Prantl bool Module::MergeArchitecture(const ArchSpec &arch_spec) { 162824610611SAdrian Prantl if (!arch_spec.IsValid()) 162924610611SAdrian Prantl return false; 163024610611SAdrian Prantl LLDB_LOG(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_MODULES), 163124610611SAdrian Prantl "module has arch %s, merging/replacing with arch %s", 163224610611SAdrian Prantl m_arch.GetTriple().getTriple().c_str(), 163324610611SAdrian Prantl arch_spec.GetTriple().getTriple().c_str()); 163424610611SAdrian Prantl if (!m_arch.IsCompatibleMatch(arch_spec)) { 163524610611SAdrian Prantl // The new architecture is different, we just need to replace it. 163624610611SAdrian Prantl return SetArchitecture(arch_spec); 163724610611SAdrian Prantl } 163824610611SAdrian Prantl 163924610611SAdrian Prantl // Merge bits from arch_spec into "merged_arch" and set our architecture. 164024610611SAdrian Prantl ArchSpec merged_arch(m_arch); 164124610611SAdrian Prantl merged_arch.MergeFrom(arch_spec); 164224610611SAdrian Prantl // SetArchitecture() is a no-op if m_arch is already valid. 164324610611SAdrian Prantl m_arch = ArchSpec(); 164424610611SAdrian Prantl return SetArchitecture(merged_arch); 164524610611SAdrian Prantl } 164624610611SAdrian Prantl 16472272c481SPavel Labath llvm::VersionTuple Module::GetVersion() { 16482272c481SPavel Labath if (ObjectFile *obj_file = GetObjectFile()) 16492272c481SPavel Labath return obj_file->GetVersion(); 16502272c481SPavel Labath return llvm::VersionTuple(); 16513467d80bSEnrico Granata } 165243fe217bSGreg Clayton 1653b9c1b51eSKate Stone bool Module::GetIsDynamicLinkEditor() { 165408928f30SGreg Clayton ObjectFile *obj_file = GetObjectFile(); 165508928f30SGreg Clayton 165608928f30SGreg Clayton if (obj_file) 165708928f30SGreg Clayton return obj_file->GetIsDynamicLinkEditor(); 165808928f30SGreg Clayton 165908928f30SGreg Clayton return false; 166008928f30SGreg Clayton } 1661