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" 542f3df613SZachary Turner #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 62672d2c12SJonas Devlieghere #include <assert.h> 63672d2c12SJonas Devlieghere #include <cstdint> 64672d2c12SJonas Devlieghere #include <inttypes.h> 65672d2c12SJonas Devlieghere #include <map> 66672d2c12SJonas Devlieghere #include <stdarg.h> 67672d2c12SJonas Devlieghere #include <string.h> 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 26016ff8604SSaleem Abdulrasool Module::Module() 2611408bf72SPavel Labath : m_object_offset(0), m_file_has_changed(false), 262b9c1b51eSKate Stone m_first_file_changed_log(false) { 263b9c1b51eSKate Stone std::lock_guard<std::recursive_mutex> guard( 264b9c1b51eSKate Stone GetAllocationModuleCollectionMutex()); 26523f8c95aSGreg Clayton GetModuleCollection().push_back(this); 26623f8c95aSGreg Clayton } 26723f8c95aSGreg Clayton 268b9c1b51eSKate Stone Module::~Module() { 26905097246SAdrian Prantl // Lock our module down while we tear everything down to make sure we don't 27005097246SAdrian Prantl // get any access to the module while it is being destroyed 27116ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 27265a03991SGreg Clayton // Scope for locker below... 27365a03991SGreg Clayton { 274b9c1b51eSKate Stone std::lock_guard<std::recursive_mutex> guard( 275b9c1b51eSKate Stone GetAllocationModuleCollectionMutex()); 27665a03991SGreg Clayton ModuleCollection &modules = GetModuleCollection(); 27765a03991SGreg Clayton ModuleCollection::iterator end = modules.end(); 27865a03991SGreg Clayton ModuleCollection::iterator pos = std::find(modules.begin(), end, this); 2793a18e319SGreg Clayton assert(pos != end); 28065a03991SGreg Clayton modules.erase(pos); 28165a03991SGreg Clayton } 282b9c1b51eSKate Stone Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT | 283b9c1b51eSKate Stone LIBLLDB_LOG_MODULES)); 284c5dac77aSEugene Zelenko if (log != nullptr) 28563e5fb76SJonas Devlieghere LLDB_LOGF(log, "%p Module::~Module((%s) '%s%s%s%s')", 286b9c1b51eSKate Stone static_cast<void *>(this), m_arch.GetArchitectureName(), 287b9c1b51eSKate Stone m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(", 28830fdc8d8SChris Lattner m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), 28930fdc8d8SChris Lattner m_object_name.IsEmpty() ? "" : ")"); 2906beaaa68SGreg Clayton // Release any auto pointers before we start tearing down our member 2916beaaa68SGreg Clayton // variables since the object file and symbol files might need to make 2926beaaa68SGreg Clayton // function calls back into this module object. The ordering is important 2936beaaa68SGreg Clayton // here because symbol files can require the module object file. So we tear 2946beaaa68SGreg Clayton // down the symbol file first, then the object file. 295d5b44036SJonas Devlieghere m_sections_up.reset(); 296d5b44036SJonas Devlieghere m_symfile_up.reset(); 297762f7135SGreg Clayton m_objfile_sp.reset(); 29830fdc8d8SChris Lattner } 29930fdc8d8SChris Lattner 300b9c1b51eSKate Stone ObjectFile *Module::GetMemoryObjectFile(const lldb::ProcessSP &process_sp, 30197206d57SZachary Turner lldb::addr_t header_addr, Status &error, 302b9c1b51eSKate Stone size_t size_to_read) { 303b9c1b51eSKate Stone if (m_objfile_sp) { 304c7f09ccaSGreg Clayton error.SetErrorString("object file already exists"); 305b9c1b51eSKate Stone } else { 30616ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 307b9c1b51eSKate Stone if (process_sp) { 308c7f09ccaSGreg Clayton m_did_load_objfile = true; 309a8f3ae7cSJonas Devlieghere auto data_up = std::make_unique<DataBufferHeap>(size_to_read, 0); 31097206d57SZachary Turner Status readmem_error; 311b9c1b51eSKate Stone const size_t bytes_read = 312d5b44036SJonas Devlieghere process_sp->ReadMemory(header_addr, data_up->GetBytes(), 313d5b44036SJonas Devlieghere data_up->GetByteSize(), readmem_error); 314256e6169SPaolo Severini if (bytes_read < size_to_read) 315256e6169SPaolo Severini data_up->SetByteSize(bytes_read); 316256e6169SPaolo Severini if (data_up->GetByteSize() > 0) { 317d5b44036SJonas Devlieghere DataBufferSP data_sp(data_up.release()); 318b9c1b51eSKate Stone m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, 319b9c1b51eSKate Stone header_addr, data_sp); 320b9c1b51eSKate Stone if (m_objfile_sp) { 3213e10cf3bSGreg Clayton StreamString s; 322d01b2953SDaniel Malea s.Printf("0x%16.16" PRIx64, header_addr); 323c156427dSZachary Turner m_object_name.SetString(s.GetString()); 3243e10cf3bSGreg Clayton 325b9c1b51eSKate Stone // Once we get the object file, update our module with the object 32605097246SAdrian Prantl // file's architecture since it might differ in vendor/os if some 32705097246SAdrian Prantl // parts were unknown. 328f760f5aeSPavel Labath m_arch = m_objfile_sp->GetArchitecture(); 329a07287ecSAlex Langford 330a07287ecSAlex Langford // Augment the arch with the target's information in case 331a07287ecSAlex Langford // we are unable to extract the os/environment from memory. 332a07287ecSAlex Langford m_arch.MergeFrom(process_sp->GetTarget().GetArchitecture()); 333b9c1b51eSKate Stone } else { 334c7f09ccaSGreg Clayton error.SetErrorString("unable to find suitable object file plug-in"); 335c7f09ccaSGreg Clayton } 336b9c1b51eSKate Stone } else { 337b9c1b51eSKate Stone error.SetErrorStringWithFormat("unable to read header from memory: %s", 338b9c1b51eSKate Stone readmem_error.AsCString()); 339c7f09ccaSGreg Clayton } 340b9c1b51eSKate Stone } else { 341c7f09ccaSGreg Clayton error.SetErrorString("invalid process"); 342c7f09ccaSGreg Clayton } 343c7f09ccaSGreg Clayton } 344c7f09ccaSGreg Clayton return m_objfile_sp.get(); 345c7f09ccaSGreg Clayton } 346c7f09ccaSGreg Clayton 347b9c1b51eSKate Stone const lldb_private::UUID &Module::GetUUID() { 3489fecd372SLeonard Mosescu if (!m_did_set_uuid.load()) { 34916ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 3509fecd372SLeonard Mosescu if (!m_did_set_uuid.load()) { 35130fdc8d8SChris Lattner ObjectFile *obj_file = GetObjectFile(); 35230fdc8d8SChris Lattner 353b9c1b51eSKate Stone if (obj_file != nullptr) { 354bd334efdSPavel Labath m_uuid = obj_file->GetUUID(); 3559fecd372SLeonard Mosescu m_did_set_uuid = true; 35630fdc8d8SChris Lattner } 35730fdc8d8SChris Lattner } 35888c05f54SGreg Clayton } 35930fdc8d8SChris Lattner return m_uuid; 36030fdc8d8SChris Lattner } 36130fdc8d8SChris Lattner 3629fecd372SLeonard Mosescu void Module::SetUUID(const lldb_private::UUID &uuid) { 3639fecd372SLeonard Mosescu std::lock_guard<std::recursive_mutex> guard(m_mutex); 3649fecd372SLeonard Mosescu if (!m_did_set_uuid) { 3659fecd372SLeonard Mosescu m_uuid = uuid; 3669fecd372SLeonard Mosescu m_did_set_uuid = true; 3679fecd372SLeonard Mosescu } else { 368fbe748aeSRichard Smith lldbassert(0 && "Attempting to overwrite the existing module UUID"); 3699fecd372SLeonard Mosescu } 3709fecd372SLeonard Mosescu } 3719fecd372SLeonard Mosescu 3720e252e38SAlex Langford llvm::Expected<TypeSystem &> 3730e252e38SAlex Langford Module::GetTypeSystemForLanguage(LanguageType language) { 3745beec213SGreg Clayton return m_type_system_map.GetTypeSystemForLanguage(language, this, true); 3756beaaa68SGreg Clayton } 3766beaaa68SGreg Clayton 377b9c1b51eSKate Stone void Module::ParseAllDebugSymbols() { 37816ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 379c7bece56SGreg Clayton size_t num_comp_units = GetNumCompileUnits(); 38030fdc8d8SChris Lattner if (num_comp_units == 0) 38130fdc8d8SChris Lattner return; 38230fdc8d8SChris Lattner 38323f70e83SPavel Labath SymbolFile *symbols = GetSymbolFile(); 38430fdc8d8SChris Lattner 385b9c1b51eSKate Stone for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++) { 3868131cb6eSPavel Labath SymbolContext sc; 3878131cb6eSPavel Labath sc.module_sp = shared_from_this(); 38830fdc8d8SChris Lattner sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get(); 389863f8c18SZachary Turner if (!sc.comp_unit) 390863f8c18SZachary Turner continue; 391863f8c18SZachary Turner 39230fdc8d8SChris Lattner symbols->ParseVariablesForContext(sc); 39330fdc8d8SChris Lattner 394863f8c18SZachary Turner symbols->ParseFunctions(*sc.comp_unit); 39530fdc8d8SChris Lattner 396a7f19e5fSRaphael Isemann sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) { 397ffc1b8fdSZachary Turner symbols->ParseBlocksRecursive(*f); 398ffc1b8fdSZachary Turner 39930fdc8d8SChris Lattner // Parse the variables for this function and all its blocks 400ffc1b8fdSZachary Turner sc.function = f.get(); 40130fdc8d8SChris Lattner symbols->ParseVariablesForContext(sc); 402a7f19e5fSRaphael Isemann return false; 403a7f19e5fSRaphael Isemann }); 40430fdc8d8SChris Lattner 40530fdc8d8SChris Lattner // Parse all types for this compile unit 406863f8c18SZachary Turner symbols->ParseTypes(*sc.comp_unit); 40730fdc8d8SChris Lattner } 40830fdc8d8SChris Lattner } 40930fdc8d8SChris Lattner 410b9c1b51eSKate Stone void Module::CalculateSymbolContext(SymbolContext *sc) { 411e1cd1be6SGreg Clayton sc->module_sp = shared_from_this(); 41230fdc8d8SChris Lattner } 41330fdc8d8SChris Lattner 414b9c1b51eSKate Stone ModuleSP Module::CalculateSymbolContextModule() { return shared_from_this(); } 4157e9b1fd0SGreg Clayton 416b9c1b51eSKate Stone void Module::DumpSymbolContext(Stream *s) { 417324a1036SSaleem Abdulrasool s->Printf(", Module{%p}", static_cast<void *>(this)); 41830fdc8d8SChris Lattner } 41930fdc8d8SChris Lattner 420b9c1b51eSKate Stone size_t Module::GetNumCompileUnits() { 42116ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 4225c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF("Module::GetNumCompileUnits (module = %p)", 423324a1036SSaleem Abdulrasool static_cast<void *>(this)); 42423f70e83SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 42530fdc8d8SChris Lattner return symbols->GetNumCompileUnits(); 42630fdc8d8SChris Lattner return 0; 42730fdc8d8SChris Lattner } 42830fdc8d8SChris Lattner 429b9c1b51eSKate Stone CompUnitSP Module::GetCompileUnitAtIndex(size_t index) { 43016ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 431c7bece56SGreg Clayton size_t num_comp_units = GetNumCompileUnits(); 43230fdc8d8SChris Lattner CompUnitSP cu_sp; 43330fdc8d8SChris Lattner 434b9c1b51eSKate Stone if (index < num_comp_units) { 43523f70e83SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 43630fdc8d8SChris Lattner cu_sp = symbols->GetCompileUnitAtIndex(index); 43730fdc8d8SChris Lattner } 43830fdc8d8SChris Lattner return cu_sp; 43930fdc8d8SChris Lattner } 44030fdc8d8SChris Lattner 441b9c1b51eSKate Stone bool Module::ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr) { 44216ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 4435c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF("Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", 444b9c1b51eSKate Stone vm_addr); 4453046e668SGreg Clayton SectionList *section_list = GetSectionList(); 4463046e668SGreg Clayton if (section_list) 4473046e668SGreg Clayton return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list); 44830fdc8d8SChris Lattner return false; 44930fdc8d8SChris Lattner } 45030fdc8d8SChris Lattner 451b9c1b51eSKate Stone uint32_t Module::ResolveSymbolContextForAddress( 452991e4453SZachary Turner const Address &so_addr, lldb::SymbolContextItem resolve_scope, 453991e4453SZachary Turner SymbolContext &sc, bool resolve_tail_call_address) { 45416ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 45530fdc8d8SChris Lattner uint32_t resolved_flags = 0; 45630fdc8d8SChris Lattner 457b9c1b51eSKate Stone // Clear the result symbol context in case we don't find anything, but don't 458b9c1b51eSKate Stone // clear the target 45972310355SGreg Clayton sc.Clear(false); 46030fdc8d8SChris Lattner 46130fdc8d8SChris Lattner // Get the section from the section/offset address. 462e72dfb32SGreg Clayton SectionSP section_sp(so_addr.GetSection()); 46330fdc8d8SChris Lattner 46430fdc8d8SChris Lattner // Make sure the section matches this module before we try and match anything 465b9c1b51eSKate Stone if (section_sp && section_sp->GetModule().get() == this) { 46605097246SAdrian Prantl // If the section offset based address resolved itself, then this is the 46705097246SAdrian Prantl // right module. 468e1cd1be6SGreg Clayton sc.module_sp = shared_from_this(); 46930fdc8d8SChris Lattner resolved_flags |= eSymbolContextModule; 47030fdc8d8SChris Lattner 47123f70e83SPavel Labath SymbolFile *symfile = GetSymbolFile(); 47223f70e83SPavel Labath if (!symfile) 47338807141SAshok Thirumurthi return resolved_flags; 47438807141SAshok Thirumurthi 47505097246SAdrian Prantl // Resolve the compile unit, function, block, line table or line entry if 47605097246SAdrian Prantl // requested. 47730fdc8d8SChris Lattner if (resolve_scope & eSymbolContextCompUnit || 47830fdc8d8SChris Lattner resolve_scope & eSymbolContextFunction || 47930fdc8d8SChris Lattner resolve_scope & eSymbolContextBlock || 4804c8e7828SGreg Clayton resolve_scope & eSymbolContextLineEntry || 481b9c1b51eSKate Stone resolve_scope & eSymbolContextVariable) { 482b9c1b51eSKate Stone resolved_flags |= 48323f70e83SPavel Labath symfile->ResolveSymbolContext(so_addr, resolve_scope, sc); 48430fdc8d8SChris Lattner } 48530fdc8d8SChris Lattner 48605097246SAdrian Prantl // Resolve the symbol if requested, but don't re-look it up if we've 48705097246SAdrian Prantl // already found it. 488b9c1b51eSKate Stone if (resolve_scope & eSymbolContextSymbol && 489b9c1b51eSKate Stone !(resolved_flags & eSymbolContextSymbol)) { 49023f70e83SPavel Labath Symtab *symtab = symfile->GetSymtab(); 491b9c1b51eSKate Stone if (symtab && so_addr.IsSectionOffset()) { 4920d9dd7dfSMohit K. Bhakkad Symbol *matching_symbol = nullptr; 493c35b91ceSAdrian McCarthy 494b9c1b51eSKate Stone symtab->ForEachSymbolContainingFileAddress( 495b9c1b51eSKate Stone so_addr.GetFileAddress(), 496c35b91ceSAdrian McCarthy [&matching_symbol](Symbol *symbol) -> bool { 497b9c1b51eSKate Stone if (symbol->GetType() != eSymbolTypeInvalid) { 4980d9dd7dfSMohit K. Bhakkad matching_symbol = symbol; 4990d9dd7dfSMohit K. Bhakkad return false; // Stop iterating 5000d9dd7dfSMohit K. Bhakkad } 5010d9dd7dfSMohit K. Bhakkad return true; // Keep iterating 5020d9dd7dfSMohit K. Bhakkad }); 5030d9dd7dfSMohit K. Bhakkad sc.symbol = matching_symbol; 504b9c1b51eSKate Stone if (!sc.symbol && resolve_scope & eSymbolContextFunction && 505b9c1b51eSKate Stone !(resolved_flags & eSymbolContextFunction)) { 506b9c1b51eSKate Stone bool verify_unique = false; // No need to check again since 507b9c1b51eSKate Stone // ResolveSymbolContext failed to find a 508b9c1b51eSKate Stone // symbol at this address. 50935729bb1SAshok Thirumurthi if (ObjectFile *obj_file = sc.module_sp->GetObjectFile()) 510b9c1b51eSKate Stone sc.symbol = 511b9c1b51eSKate Stone obj_file->ResolveSymbolForAddress(so_addr, verify_unique); 51235729bb1SAshok Thirumurthi } 51335729bb1SAshok Thirumurthi 514b9c1b51eSKate Stone if (sc.symbol) { 515b9c1b51eSKate Stone if (sc.symbol->IsSynthetic()) { 51605097246SAdrian Prantl // We have a synthetic symbol so lets check if the object file from 51705097246SAdrian Prantl // the symbol file in the symbol vendor is different than the 51805097246SAdrian Prantl // object file for the module, and if so search its symbol table to 51905097246SAdrian Prantl // see if we can come up with a better symbol. For example dSYM 52005097246SAdrian Prantl // files on MacOSX have an unstripped symbol table inside of them. 52193e2861bSGreg Clayton ObjectFile *symtab_objfile = symtab->GetObjectFile(); 522b9c1b51eSKate Stone if (symtab_objfile && symtab_objfile->IsStripped()) { 52393e2861bSGreg Clayton ObjectFile *symfile_objfile = symfile->GetObjectFile(); 524b9c1b51eSKate Stone if (symfile_objfile != symtab_objfile) { 52593e2861bSGreg Clayton Symtab *symfile_symtab = symfile_objfile->GetSymtab(); 526b9c1b51eSKate Stone if (symfile_symtab) { 527b9c1b51eSKate Stone Symbol *symbol = 528b9c1b51eSKate Stone symfile_symtab->FindSymbolContainingFileAddress( 529b9c1b51eSKate Stone so_addr.GetFileAddress()); 530b9c1b51eSKate Stone if (symbol && !symbol->IsSynthetic()) { 53193e2861bSGreg Clayton sc.symbol = symbol; 53293e2861bSGreg Clayton } 53393e2861bSGreg Clayton } 53493e2861bSGreg Clayton } 53593e2861bSGreg Clayton } 53693e2861bSGreg Clayton } 53730fdc8d8SChris Lattner resolved_flags |= eSymbolContextSymbol; 53830fdc8d8SChris Lattner } 53930fdc8d8SChris Lattner } 54093e2861bSGreg Clayton } 54138807141SAshok Thirumurthi 542b9c1b51eSKate Stone // For function symbols, so_addr may be off by one. This is a convention 54305097246SAdrian Prantl // consistent with FDE row indices in eh_frame sections, but requires extra 54405097246SAdrian Prantl // logic here to permit symbol lookup for disassembly and unwind. 545b9c1b51eSKate Stone if (resolve_scope & eSymbolContextSymbol && 546b9c1b51eSKate Stone !(resolved_flags & eSymbolContextSymbol) && resolve_tail_call_address && 547b9c1b51eSKate Stone so_addr.IsSectionOffset()) { 54838807141SAshok Thirumurthi Address previous_addr = so_addr; 549edfaae39SGreg Clayton previous_addr.Slide(-1); 55038807141SAshok Thirumurthi 55135729bb1SAshok Thirumurthi bool do_resolve_tail_call_address = false; // prevent recursion 552b9c1b51eSKate Stone const uint32_t flags = ResolveSymbolContextForAddress( 553b9c1b51eSKate Stone previous_addr, resolve_scope, sc, do_resolve_tail_call_address); 554b9c1b51eSKate Stone if (flags & eSymbolContextSymbol) { 55538807141SAshok Thirumurthi AddressRange addr_range; 556b9c1b51eSKate Stone if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, 557b9c1b51eSKate Stone false, addr_range)) { 558b9c1b51eSKate Stone if (addr_range.GetBaseAddress().GetSection() == 559b9c1b51eSKate Stone so_addr.GetSection()) { 560b9c1b51eSKate Stone // If the requested address is one past the address range of a 56105097246SAdrian Prantl // function (i.e. a tail call), or the decremented address is the 56205097246SAdrian Prantl // start of a function (i.e. some forms of trampoline), indicate 56305097246SAdrian Prantl // that the symbol has been resolved. 564b9c1b51eSKate Stone if (so_addr.GetOffset() == 565b9c1b51eSKate Stone addr_range.GetBaseAddress().GetOffset() || 566b9c1b51eSKate Stone so_addr.GetOffset() == 567b9c1b51eSKate Stone addr_range.GetBaseAddress().GetOffset() + 568b9c1b51eSKate Stone addr_range.GetByteSize()) { 56938807141SAshok Thirumurthi resolved_flags |= flags; 57038807141SAshok Thirumurthi } 571b9c1b51eSKate Stone } else { 572b9c1b51eSKate Stone sc.symbol = 573b9c1b51eSKate Stone nullptr; // Don't trust the symbol if the sections didn't match. 57438807141SAshok Thirumurthi } 57538807141SAshok Thirumurthi } 57630fdc8d8SChris Lattner } 57730fdc8d8SChris Lattner } 57830fdc8d8SChris Lattner } 57930fdc8d8SChris Lattner return resolved_flags; 58030fdc8d8SChris Lattner } 58130fdc8d8SChris Lattner 582991e4453SZachary Turner uint32_t Module::ResolveSymbolContextForFilePath( 583991e4453SZachary Turner const char *file_path, uint32_t line, bool check_inlines, 584991e4453SZachary Turner lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) { 5858f3be7a3SJonas Devlieghere FileSpec file_spec(file_path); 586b9c1b51eSKate Stone return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines, 587b9c1b51eSKate Stone resolve_scope, sc_list); 58830fdc8d8SChris Lattner } 58930fdc8d8SChris Lattner 590991e4453SZachary Turner uint32_t Module::ResolveSymbolContextsForFileSpec( 591991e4453SZachary Turner const FileSpec &file_spec, uint32_t line, bool check_inlines, 592991e4453SZachary Turner lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) { 59316ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 5945c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF("Module::ResolveSymbolContextForFilePath (%s:%u, " 595b9c1b51eSKate Stone "check_inlines = %s, resolve_scope = 0x%8.8x)", 596b9c1b51eSKate Stone file_spec.GetPath().c_str(), line, 597b9c1b51eSKate Stone check_inlines ? "yes" : "no", resolve_scope); 59830fdc8d8SChris Lattner 59930fdc8d8SChris Lattner const uint32_t initial_count = sc_list.GetSize(); 60030fdc8d8SChris Lattner 601*3e2ed744SMed Ismail Bennani if (SymbolFile *symbols = GetSymbolFile()) { 602*3e2ed744SMed Ismail Bennani // TODO: Handle SourceLocationSpec column information 603*3e2ed744SMed Ismail Bennani SourceLocationSpec location_spec(file_spec, line, /*column=*/llvm::None, 604*3e2ed744SMed Ismail Bennani check_inlines, /*exact_match=*/false); 605*3e2ed744SMed Ismail Bennani 606*3e2ed744SMed Ismail Bennani symbols->ResolveSymbolContext(location_spec, resolve_scope, sc_list); 607*3e2ed744SMed Ismail Bennani } 60830fdc8d8SChris Lattner 60930fdc8d8SChris Lattner return sc_list.GetSize() - initial_count; 61030fdc8d8SChris Lattner } 61130fdc8d8SChris Lattner 6121ad655e2SAdrian Prantl void Module::FindGlobalVariables(ConstString name, 613f9568a95SRaphael Isemann const CompilerDeclContext &parent_decl_ctx, 6141ad655e2SAdrian Prantl size_t max_matches, VariableList &variables) { 61523f70e83SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 6161ad655e2SAdrian Prantl symbols->FindGlobalVariables(name, parent_decl_ctx, max_matches, variables); 61734cda14bSPavel Labath } 61834cda14bSPavel Labath 6191ad655e2SAdrian Prantl void Module::FindGlobalVariables(const RegularExpression ®ex, 6201ad655e2SAdrian Prantl size_t max_matches, VariableList &variables) { 62123f70e83SPavel Labath SymbolFile *symbols = GetSymbolFile(); 62234cda14bSPavel Labath if (symbols) 6231ad655e2SAdrian Prantl symbols->FindGlobalVariables(regex, max_matches, variables); 62430fdc8d8SChris Lattner } 62530fdc8d8SChris Lattner 6261ad655e2SAdrian Prantl void Module::FindCompileUnits(const FileSpec &path, 627b9c1b51eSKate Stone SymbolContextList &sc_list) { 628c7bece56SGreg Clayton const size_t num_compile_units = GetNumCompileUnits(); 629644247c1SGreg Clayton SymbolContext sc; 630e1cd1be6SGreg Clayton sc.module_sp = shared_from_this(); 631b9c1b51eSKate Stone for (size_t i = 0; i < num_compile_units; ++i) { 632644247c1SGreg Clayton sc.comp_unit = GetCompileUnitAtIndex(i).get(); 633b9c1b51eSKate Stone if (sc.comp_unit) { 634532290e6SPavel Labath if (FileSpec::Match(path, sc.comp_unit->GetPrimaryFile())) 635644247c1SGreg Clayton sc_list.Append(sc); 636644247c1SGreg Clayton } 6372dafd8edSGreg Clayton } 638644247c1SGreg Clayton } 639644247c1SGreg Clayton 6400e4c4821SAdrian Prantl Module::LookupInfo::LookupInfo(ConstString name, 641117b1fa1SZachary Turner FunctionNameType name_type_mask, 642117b1fa1SZachary Turner LanguageType language) 643117b1fa1SZachary Turner : m_name(name), m_lookup_name(), m_language(language), 644117b1fa1SZachary Turner m_name_type_mask(eFunctionNameTypeNone), 645b9c1b51eSKate Stone m_match_name_after_lookup(false) { 6466234a5c8SGreg Clayton const char *name_cstr = name.GetCString(); 6476234a5c8SGreg Clayton llvm::StringRef basename; 6486234a5c8SGreg Clayton llvm::StringRef context; 6496234a5c8SGreg Clayton 650b9c1b51eSKate Stone if (name_type_mask & eFunctionNameTypeAuto) { 6516234a5c8SGreg Clayton if (CPlusPlusLanguage::IsCPPMangledName(name_cstr)) 6526234a5c8SGreg Clayton m_name_type_mask = eFunctionNameTypeFull; 6536234a5c8SGreg Clayton else if ((language == eLanguageTypeUnknown || 6546234a5c8SGreg Clayton Language::LanguageIsObjC(language)) && 6556234a5c8SGreg Clayton ObjCLanguage::IsPossibleObjCMethodName(name_cstr)) 6566234a5c8SGreg Clayton m_name_type_mask = eFunctionNameTypeFull; 657b9c1b51eSKate Stone else if (Language::LanguageIsC(language)) { 6586234a5c8SGreg Clayton m_name_type_mask = eFunctionNameTypeFull; 659b9c1b51eSKate Stone } else { 6606234a5c8SGreg Clayton if ((language == eLanguageTypeUnknown || 6616234a5c8SGreg Clayton Language::LanguageIsObjC(language)) && 6626234a5c8SGreg Clayton ObjCLanguage::IsPossibleObjCSelector(name_cstr)) 6636234a5c8SGreg Clayton m_name_type_mask |= eFunctionNameTypeSelector; 6646234a5c8SGreg Clayton 6656234a5c8SGreg Clayton CPlusPlusLanguage::MethodName cpp_method(name); 6666234a5c8SGreg Clayton basename = cpp_method.GetBasename(); 667b9c1b51eSKate Stone if (basename.empty()) { 668b9c1b51eSKate Stone if (CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, 669b9c1b51eSKate Stone basename)) 6706234a5c8SGreg Clayton m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase); 6716234a5c8SGreg Clayton else 6726234a5c8SGreg Clayton m_name_type_mask |= eFunctionNameTypeFull; 673b9c1b51eSKate Stone } else { 6746234a5c8SGreg Clayton m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase); 6756234a5c8SGreg Clayton } 6766234a5c8SGreg Clayton } 677b9c1b51eSKate Stone } else { 6786234a5c8SGreg Clayton m_name_type_mask = name_type_mask; 679b9c1b51eSKate Stone if (name_type_mask & eFunctionNameTypeMethod || 680b9c1b51eSKate Stone name_type_mask & eFunctionNameTypeBase) { 681b9c1b51eSKate Stone // If they've asked for a CPP method or function name and it can't be 68205097246SAdrian Prantl // that, we don't even need to search for CPP methods or names. 6836234a5c8SGreg Clayton CPlusPlusLanguage::MethodName cpp_method(name); 684b9c1b51eSKate Stone if (cpp_method.IsValid()) { 6856234a5c8SGreg Clayton basename = cpp_method.GetBasename(); 6866234a5c8SGreg Clayton 687b9c1b51eSKate Stone if (!cpp_method.GetQualifiers().empty()) { 688b9c1b51eSKate Stone // There is a "const" or other qualifier following the end of the 68905097246SAdrian Prantl // function parens, this can't be a eFunctionNameTypeBase 6906234a5c8SGreg Clayton m_name_type_mask &= ~(eFunctionNameTypeBase); 6916234a5c8SGreg Clayton if (m_name_type_mask == eFunctionNameTypeNone) 6926234a5c8SGreg Clayton return; 6936234a5c8SGreg Clayton } 694b9c1b51eSKate Stone } else { 695b9c1b51eSKate Stone // If the CPP method parser didn't manage to chop this up, try to fill 69605097246SAdrian Prantl // in the base name if we can. If a::b::c is passed in, we need to just 69705097246SAdrian Prantl // look up "c", and then we'll filter the result later. 698b9c1b51eSKate Stone CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, 699b9c1b51eSKate Stone basename); 7006234a5c8SGreg Clayton } 7016234a5c8SGreg Clayton } 7026234a5c8SGreg Clayton 703b9c1b51eSKate Stone if (name_type_mask & eFunctionNameTypeSelector) { 704b9c1b51eSKate Stone if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr)) { 7056234a5c8SGreg Clayton m_name_type_mask &= ~(eFunctionNameTypeSelector); 7066234a5c8SGreg Clayton if (m_name_type_mask == eFunctionNameTypeNone) 7076234a5c8SGreg Clayton return; 7086234a5c8SGreg Clayton } 7096234a5c8SGreg Clayton } 7106234a5c8SGreg Clayton 711b9c1b51eSKate Stone // Still try and get a basename in case someone specifies a name type mask 7125d0c1146SGreg Clayton // of eFunctionNameTypeFull and a name like "A::func" 713b9c1b51eSKate Stone if (basename.empty()) { 7145d0c1146SGreg Clayton if (name_type_mask & eFunctionNameTypeFull && 7155d0c1146SGreg Clayton !CPlusPlusLanguage::IsCPPMangledName(name_cstr)) { 7166234a5c8SGreg Clayton CPlusPlusLanguage::MethodName cpp_method(name); 7176234a5c8SGreg Clayton basename = cpp_method.GetBasename(); 7186234a5c8SGreg Clayton if (basename.empty()) 719b9c1b51eSKate Stone CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, 720b9c1b51eSKate Stone basename); 7216234a5c8SGreg Clayton } 7226234a5c8SGreg Clayton } 7236234a5c8SGreg Clayton } 7246234a5c8SGreg Clayton 725b9c1b51eSKate Stone if (!basename.empty()) { 72605097246SAdrian Prantl // The name supplied was a partial C++ path like "a::count". In this case 72705097246SAdrian Prantl // we want to do a lookup on the basename "count" and then make sure any 72805097246SAdrian Prantl // matching results contain "a::count" so that it would match "b::a::count" 72905097246SAdrian Prantl // and "a::count". This is why we set "match_name_after_lookup" to true 7306234a5c8SGreg Clayton m_lookup_name.SetString(basename); 7316234a5c8SGreg Clayton m_match_name_after_lookup = true; 732b9c1b51eSKate Stone } else { 733b9c1b51eSKate Stone // The name is already correct, just use the exact name as supplied, and we 73405097246SAdrian Prantl // won't need to check if any matches contain "name" 7356234a5c8SGreg Clayton m_lookup_name = name; 7366234a5c8SGreg Clayton m_match_name_after_lookup = false; 7376234a5c8SGreg Clayton } 7386234a5c8SGreg Clayton } 7396234a5c8SGreg Clayton 740b9c1b51eSKate Stone void Module::LookupInfo::Prune(SymbolContextList &sc_list, 741b9c1b51eSKate Stone size_t start_idx) const { 742b9c1b51eSKate Stone if (m_match_name_after_lookup && m_name) { 7436234a5c8SGreg Clayton SymbolContext sc; 7446234a5c8SGreg Clayton size_t i = start_idx; 745b9c1b51eSKate Stone while (i < sc_list.GetSize()) { 7466234a5c8SGreg Clayton if (!sc_list.GetContextAtIndex(i, sc)) 7476234a5c8SGreg Clayton break; 7486234a5c8SGreg Clayton ConstString full_name(sc.GetFunctionName()); 749b9c1b51eSKate Stone if (full_name && 750b9c1b51eSKate Stone ::strstr(full_name.GetCString(), m_name.GetCString()) == nullptr) { 7516234a5c8SGreg Clayton sc_list.RemoveContextAtIndex(i); 752b9c1b51eSKate Stone } else { 7536234a5c8SGreg Clayton ++i; 7546234a5c8SGreg Clayton } 7556234a5c8SGreg Clayton } 7566234a5c8SGreg Clayton } 7576234a5c8SGreg Clayton 758b9c1b51eSKate Stone // If we have only full name matches we might have tried to set breakpoint on 7595d0c1146SGreg Clayton // "func" and specified eFunctionNameTypeFull, but we might have found 7605d0c1146SGreg Clayton // "a::func()", "a::b::func()", "c::func()", "func()" and "func". Only 7615d0c1146SGreg Clayton // "func()" and "func" should end up matching. 762b9c1b51eSKate Stone if (m_name_type_mask == eFunctionNameTypeFull) { 7636234a5c8SGreg Clayton SymbolContext sc; 7646234a5c8SGreg Clayton size_t i = start_idx; 765b9c1b51eSKate Stone while (i < sc_list.GetSize()) { 7666234a5c8SGreg Clayton if (!sc_list.GetContextAtIndex(i, sc)) 7676234a5c8SGreg Clayton break; 76805097246SAdrian Prantl // Make sure the mangled and demangled names don't match before we try to 76905097246SAdrian Prantl // pull anything out 7705d0c1146SGreg Clayton ConstString mangled_name(sc.GetFunctionName(Mangled::ePreferMangled)); 7716234a5c8SGreg Clayton ConstString full_name(sc.GetFunctionName()); 7725d0c1146SGreg Clayton if (mangled_name != m_name && full_name != m_name) 7735d0c1146SGreg Clayton { 7746234a5c8SGreg Clayton CPlusPlusLanguage::MethodName cpp_method(full_name); 775b9c1b51eSKate Stone if (cpp_method.IsValid()) { 776b9c1b51eSKate Stone if (cpp_method.GetContext().empty()) { 777b9c1b51eSKate Stone if (cpp_method.GetBasename().compare(m_name.GetStringRef()) != 0) { 7786234a5c8SGreg Clayton sc_list.RemoveContextAtIndex(i); 7796234a5c8SGreg Clayton continue; 7806234a5c8SGreg Clayton } 781b9c1b51eSKate Stone } else { 7825d0c1146SGreg Clayton std::string qualified_name; 7835d0c1146SGreg Clayton llvm::StringRef anon_prefix("(anonymous namespace)"); 7845d0c1146SGreg Clayton if (cpp_method.GetContext() == anon_prefix) 7855d0c1146SGreg Clayton qualified_name = cpp_method.GetBasename().str(); 7865d0c1146SGreg Clayton else 7875d0c1146SGreg Clayton qualified_name = cpp_method.GetScopeQualifiedName(); 7888d20cfdfSJonas Devlieghere if (qualified_name != m_name.GetCString()) { 7896234a5c8SGreg Clayton sc_list.RemoveContextAtIndex(i); 7906234a5c8SGreg Clayton continue; 7916234a5c8SGreg Clayton } 7926234a5c8SGreg Clayton } 7936234a5c8SGreg Clayton } 7945d0c1146SGreg Clayton } 7956234a5c8SGreg Clayton ++i; 7966234a5c8SGreg Clayton } 7976234a5c8SGreg Clayton } 7986234a5c8SGreg Clayton } 7996234a5c8SGreg Clayton 8001ad655e2SAdrian Prantl void Module::FindFunctions(ConstString name, 801f9568a95SRaphael Isemann const CompilerDeclContext &parent_decl_ctx, 802117b1fa1SZachary Turner FunctionNameType name_type_mask, 803117b1fa1SZachary Turner bool include_symbols, bool include_inlines, 8041ad655e2SAdrian Prantl SymbolContextList &sc_list) { 80543fe217bSGreg Clayton const size_t old_size = sc_list.GetSize(); 806931180e6SGreg Clayton 807931180e6SGreg Clayton // Find all the functions (not symbols, but debug information functions... 80823f70e83SPavel Labath SymbolFile *symbols = GetSymbolFile(); 80943fe217bSGreg Clayton 810b9c1b51eSKate Stone if (name_type_mask & eFunctionNameTypeAuto) { 8116234a5c8SGreg Clayton LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown); 81243fe217bSGreg Clayton 813b9c1b51eSKate Stone if (symbols) { 814b9c1b51eSKate Stone symbols->FindFunctions(lookup_info.GetLookupName(), parent_decl_ctx, 815b9c1b51eSKate Stone lookup_info.GetNameTypeMask(), include_inlines, 8161ad655e2SAdrian Prantl sc_list); 81743fe217bSGreg Clayton 818b9c1b51eSKate Stone // Now check our symbol table for symbols that are code symbols if 819b9c1b51eSKate Stone // requested 820b9c1b51eSKate Stone if (include_symbols) { 821a7499c98SMichael Sartain Symtab *symtab = symbols->GetSymtab(); 82243fe217bSGreg Clayton if (symtab) 823b9c1b51eSKate Stone symtab->FindFunctionSymbols(lookup_info.GetLookupName(), 824b9c1b51eSKate Stone lookup_info.GetNameTypeMask(), sc_list); 82543fe217bSGreg Clayton } 82643fe217bSGreg Clayton } 82743fe217bSGreg Clayton 8286234a5c8SGreg Clayton const size_t new_size = sc_list.GetSize(); 8296234a5c8SGreg Clayton 8306234a5c8SGreg Clayton if (old_size < new_size) 8316234a5c8SGreg Clayton lookup_info.Prune(sc_list, old_size); 832b9c1b51eSKate Stone } else { 833b9c1b51eSKate Stone if (symbols) { 834b9c1b51eSKate Stone symbols->FindFunctions(name, parent_decl_ctx, name_type_mask, 8351ad655e2SAdrian Prantl include_inlines, sc_list); 836931180e6SGreg Clayton 837b9c1b51eSKate Stone // Now check our symbol table for symbols that are code symbols if 838b9c1b51eSKate Stone // requested 839b9c1b51eSKate Stone if (include_symbols) { 840a7499c98SMichael Sartain Symtab *symtab = symbols->GetSymtab(); 841931180e6SGreg Clayton if (symtab) 84243fe217bSGreg Clayton symtab->FindFunctionSymbols(name, name_type_mask, sc_list); 843931180e6SGreg Clayton } 844931180e6SGreg Clayton } 845931180e6SGreg Clayton } 84630fdc8d8SChris Lattner } 84730fdc8d8SChris Lattner 8481ad655e2SAdrian Prantl void Module::FindFunctions(const RegularExpression ®ex, bool include_symbols, 8491ad655e2SAdrian Prantl bool include_inlines, 8501ad655e2SAdrian Prantl SymbolContextList &sc_list) { 851c7bece56SGreg Clayton const size_t start_size = sc_list.GetSize(); 852931180e6SGreg Clayton 85323f70e83SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) { 8541ad655e2SAdrian Prantl symbols->FindFunctions(regex, include_inlines, sc_list); 855a7499c98SMichael Sartain 85605097246SAdrian Prantl // Now check our symbol table for symbols that are code symbols if 85705097246SAdrian Prantl // requested 858b9c1b51eSKate Stone if (include_symbols) { 859a7499c98SMichael Sartain Symtab *symtab = symbols->GetSymtab(); 860b9c1b51eSKate Stone if (symtab) { 861931180e6SGreg Clayton std::vector<uint32_t> symbol_indexes; 862b9c1b51eSKate Stone symtab->AppendSymbolIndexesMatchingRegExAndType( 863b9c1b51eSKate Stone regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, 864b9c1b51eSKate Stone symbol_indexes); 865c7bece56SGreg Clayton const size_t num_matches = symbol_indexes.size(); 866b9c1b51eSKate Stone if (num_matches) { 867931180e6SGreg Clayton SymbolContext sc(this); 868d8cf1a11SGreg Clayton const size_t end_functions_added_index = sc_list.GetSize(); 869b9c1b51eSKate Stone size_t num_functions_added_to_sc_list = 870b9c1b51eSKate Stone end_functions_added_index - start_size; 871b9c1b51eSKate Stone if (num_functions_added_to_sc_list == 0) { 87205097246SAdrian Prantl // No functions were added, just symbols, so we can just append 87305097246SAdrian Prantl // them 874b9c1b51eSKate Stone for (size_t i = 0; i < num_matches; ++i) { 875931180e6SGreg Clayton sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 87600049b8bSMatt Kopec SymbolType sym_type = sc.symbol->GetType(); 87700049b8bSMatt Kopec if (sc.symbol && (sym_type == eSymbolTypeCode || 87800049b8bSMatt Kopec sym_type == eSymbolTypeResolver)) 879d8cf1a11SGreg Clayton sc_list.Append(sc); 880d8cf1a11SGreg Clayton } 881b9c1b51eSKate Stone } else { 882d8cf1a11SGreg Clayton typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap; 883d8cf1a11SGreg Clayton FileAddrToIndexMap file_addr_to_index; 884b9c1b51eSKate Stone for (size_t i = start_size; i < end_functions_added_index; ++i) { 885d8cf1a11SGreg Clayton const SymbolContext &sc = sc_list[i]; 886d8cf1a11SGreg Clayton if (sc.block) 887d8cf1a11SGreg Clayton continue; 888b9c1b51eSKate Stone file_addr_to_index[sc.function->GetAddressRange() 889b9c1b51eSKate Stone .GetBaseAddress() 890b9c1b51eSKate Stone .GetFileAddress()] = i; 891d8cf1a11SGreg Clayton } 892d8cf1a11SGreg Clayton 893d8cf1a11SGreg Clayton FileAddrToIndexMap::const_iterator end = file_addr_to_index.end(); 894d8cf1a11SGreg Clayton // Functions were added so we need to merge symbols into any 895d8cf1a11SGreg Clayton // existing function symbol contexts 896b9c1b51eSKate Stone for (size_t i = start_size; i < num_matches; ++i) { 897d8cf1a11SGreg Clayton sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 898d8cf1a11SGreg Clayton SymbolType sym_type = sc.symbol->GetType(); 899b9c1b51eSKate Stone if (sc.symbol && sc.symbol->ValueIsAddress() && 900b9c1b51eSKate Stone (sym_type == eSymbolTypeCode || 901b9c1b51eSKate Stone sym_type == eSymbolTypeResolver)) { 902b9c1b51eSKate Stone FileAddrToIndexMap::const_iterator pos = 903b9c1b51eSKate Stone file_addr_to_index.find( 904b9c1b51eSKate Stone sc.symbol->GetAddressRef().GetFileAddress()); 905d8cf1a11SGreg Clayton if (pos == end) 906d8cf1a11SGreg Clayton sc_list.Append(sc); 907d8cf1a11SGreg Clayton else 908d8cf1a11SGreg Clayton sc_list[pos->second].symbol = sc.symbol; 909d8cf1a11SGreg Clayton } 910d8cf1a11SGreg Clayton } 911931180e6SGreg Clayton } 912931180e6SGreg Clayton } 913931180e6SGreg Clayton } 914931180e6SGreg Clayton } 915931180e6SGreg Clayton } 91630fdc8d8SChris Lattner } 91730fdc8d8SChris Lattner 918b9c1b51eSKate Stone void Module::FindAddressesForLine(const lldb::TargetSP target_sp, 919f86248d9SRichard Mitton const FileSpec &file, uint32_t line, 920f86248d9SRichard Mitton Function *function, 921b9c1b51eSKate Stone std::vector<Address> &output_local, 922b9c1b51eSKate Stone std::vector<Address> &output_extern) { 923f86248d9SRichard Mitton SearchFilterByModule filter(target_sp, m_file); 924*3e2ed744SMed Ismail Bennani 925*3e2ed744SMed Ismail Bennani // TODO: Handle SourceLocationSpec column information 926*3e2ed744SMed Ismail Bennani SourceLocationSpec location_spec(file, line, /*column=*/llvm::None, 927*3e2ed744SMed Ismail Bennani /*check_inlines=*/true, 928*3e2ed744SMed Ismail Bennani /*exact_match=*/false); 929*3e2ed744SMed Ismail Bennani AddressResolverFileLine resolver(location_spec); 930f86248d9SRichard Mitton resolver.ResolveAddress(filter); 931f86248d9SRichard Mitton 932b9c1b51eSKate Stone for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++) { 933f86248d9SRichard Mitton Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress(); 934f86248d9SRichard Mitton Function *f = addr.CalculateSymbolContextFunction(); 935f86248d9SRichard Mitton if (f && f == function) 936f86248d9SRichard Mitton output_local.push_back(addr); 937f86248d9SRichard Mitton else 938f86248d9SRichard Mitton output_extern.push_back(addr); 939f86248d9SRichard Mitton } 940f86248d9SRichard Mitton } 941f86248d9SRichard Mitton 942bf9d84c0SAdrian Prantl void Module::FindTypes_Impl( 943f9568a95SRaphael Isemann ConstString name, const CompilerDeclContext &parent_decl_ctx, 944d4d428efSAdrian Prantl size_t max_matches, 945ae088e52SGreg Clayton llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 946b9c1b51eSKate Stone TypeMap &types) { 9475c1c8443SJonas Devlieghere LLDB_SCOPED_TIMER(); 94823f70e83SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 949bf9d84c0SAdrian Prantl symbols->FindTypes(name, parent_decl_ctx, max_matches, 950b9c1b51eSKate Stone searched_symbol_files, types); 9513504eee8SGreg Clayton } 9523504eee8SGreg Clayton 953bf9d84c0SAdrian Prantl void Module::FindTypesInNamespace(ConstString type_name, 954f9568a95SRaphael Isemann const CompilerDeclContext &parent_decl_ctx, 955b9c1b51eSKate Stone size_t max_matches, TypeList &type_list) { 9564069730cSRavitheja Addepally TypeMap types_map; 957ae088e52SGreg Clayton llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; 958bf9d84c0SAdrian Prantl FindTypes_Impl(type_name, parent_decl_ctx, max_matches, searched_symbol_files, 959bf9d84c0SAdrian Prantl types_map); 960bf9d84c0SAdrian Prantl if (types_map.GetSize()) { 961576495e6SZachary Turner SymbolContext sc; 962576495e6SZachary Turner sc.module_sp = shared_from_this(); 9634069730cSRavitheja Addepally sc.SortTypeList(types_map, type_list); 964576495e6SZachary Turner } 9656f3533fbSEnrico Granata } 9666f3533fbSEnrico Granata 967b9c1b51eSKate Stone lldb::TypeSP Module::FindFirstType(const SymbolContext &sc, 9680e4c4821SAdrian Prantl ConstString name, bool exact_match) { 969b43165b7SGreg Clayton TypeList type_list; 970ae088e52SGreg Clayton llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; 971576495e6SZachary Turner FindTypes(name, exact_match, 1, searched_symbol_files, type_list); 972bf9d84c0SAdrian Prantl if (type_list.GetSize()) 973b43165b7SGreg Clayton return type_list.GetTypeAtIndex(0); 974b43165b7SGreg Clayton return TypeSP(); 975b43165b7SGreg Clayton } 976b43165b7SGreg Clayton 977bf9d84c0SAdrian Prantl void Module::FindTypes( 9780e4c4821SAdrian Prantl ConstString name, bool exact_match, size_t max_matches, 979ae088e52SGreg Clayton llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 980b9c1b51eSKate Stone TypeList &types) { 98184db9105SGreg Clayton const char *type_name_cstr = name.GetCString(); 982556b1611STamas Berghammer llvm::StringRef type_scope; 983556b1611STamas Berghammer llvm::StringRef type_basename; 9847bc31332SGreg Clayton TypeClass type_class = eTypeClassAny; 9854069730cSRavitheja Addepally TypeMap typesmap; 9861739b7d0SFrederic Riss 987b9c1b51eSKate Stone if (Type::GetTypeScopeAndBasename(type_name_cstr, type_scope, type_basename, 988b9c1b51eSKate Stone type_class)) { 98984db9105SGreg Clayton // Check if "name" starts with "::" which means the qualified type starts 99084db9105SGreg Clayton // from the root namespace and implies and exact match. The typenames we 99184db9105SGreg Clayton // get back from clang do not start with "::" so we need to strip this off 992d93c4a33SBruce Mitchener // in order to get the qualified names to match 993556b1611STamas Berghammer exact_match = type_scope.consume_front("::"); 9946f3533fbSEnrico Granata 995556b1611STamas Berghammer ConstString type_basename_const_str(type_basename); 996f9568a95SRaphael Isemann FindTypes_Impl(type_basename_const_str, CompilerDeclContext(), max_matches, 997bf9d84c0SAdrian Prantl searched_symbol_files, typesmap); 998bf9d84c0SAdrian Prantl if (typesmap.GetSize()) 999adcd0268SBenjamin Kramer typesmap.RemoveMismatchedTypes(std::string(type_scope), 1000adcd0268SBenjamin Kramer std::string(type_basename), type_class, 1001b9c1b51eSKate Stone exact_match); 1002b9c1b51eSKate Stone } else { 1003b9c1b51eSKate Stone // The type is not in a namespace/class scope, just search for it by 1004b9c1b51eSKate Stone // basename 1005c485f056SGreg Clayton if (type_class != eTypeClassAny && !type_basename.empty()) { 1006b9c1b51eSKate Stone // The "type_name_cstr" will have been modified if we have a valid type 100705097246SAdrian Prantl // class prefix (like "struct", "class", "union", "typedef" etc). 1008f9568a95SRaphael Isemann FindTypes_Impl(ConstString(type_basename), CompilerDeclContext(), 1009f9568a95SRaphael Isemann UINT_MAX, searched_symbol_files, typesmap); 1010adcd0268SBenjamin Kramer typesmap.RemoveMismatchedTypes(std::string(type_scope), 1011adcd0268SBenjamin Kramer std::string(type_basename), type_class, 1012c485f056SGreg Clayton exact_match); 1013b9c1b51eSKate Stone } else { 1014f9568a95SRaphael Isemann FindTypes_Impl(name, CompilerDeclContext(), UINT_MAX, 1015f9568a95SRaphael Isemann searched_symbol_files, typesmap); 1016c485f056SGreg Clayton if (exact_match) { 1017c485f056SGreg Clayton std::string name_str(name.AsCString("")); 1018adcd0268SBenjamin Kramer typesmap.RemoveMismatchedTypes(std::string(type_scope), name_str, 1019adcd0268SBenjamin Kramer type_class, exact_match); 1020c485f056SGreg Clayton } 102184db9105SGreg Clayton } 10227bc31332SGreg Clayton } 1023bf9d84c0SAdrian Prantl if (typesmap.GetSize()) { 1024576495e6SZachary Turner SymbolContext sc; 1025576495e6SZachary Turner sc.module_sp = shared_from_this(); 10264069730cSRavitheja Addepally sc.SortTypeList(typesmap, types); 1027576495e6SZachary Turner } 10286f3533fbSEnrico Granata } 10296f3533fbSEnrico Granata 10303b73dcdcSAdrian Prantl void Module::FindTypes( 10313b73dcdcSAdrian Prantl llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages, 10323b73dcdcSAdrian Prantl llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 10333b73dcdcSAdrian Prantl TypeMap &types) { 10345c1c8443SJonas Devlieghere LLDB_SCOPED_TIMER(); 1035aa97a89dSAdrian Prantl if (SymbolFile *symbols = GetSymbolFile()) 10363b73dcdcSAdrian Prantl symbols->FindTypes(pattern, languages, searched_symbol_files, types); 1037aa97a89dSAdrian Prantl } 1038aa97a89dSAdrian Prantl 1039579d6d1aSPavel Labath SymbolFile *Module::GetSymbolFile(bool can_create, Stream *feedback_strm) { 1040579d6d1aSPavel Labath if (!m_did_load_symfile.load()) { 104116ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 1042579d6d1aSPavel Labath if (!m_did_load_symfile.load() && can_create) { 104330fdc8d8SChris Lattner ObjectFile *obj_file = GetObjectFile(); 1044b9c1b51eSKate Stone if (obj_file != nullptr) { 10455c1c8443SJonas Devlieghere LLDB_SCOPED_TIMER(); 1046d5b44036SJonas Devlieghere m_symfile_up.reset( 1047b9c1b51eSKate Stone SymbolVendor::FindPlugin(shared_from_this(), feedback_strm)); 1048579d6d1aSPavel Labath m_did_load_symfile = true; 104930fdc8d8SChris Lattner } 105030fdc8d8SChris Lattner } 105188c05f54SGreg Clayton } 1052579d6d1aSPavel Labath return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr; 105323f70e83SPavel Labath } 105423f70e83SPavel Labath 1055d5d47a35SPavel Labath Symtab *Module::GetSymtab() { 1056d5d47a35SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 1057d5d47a35SPavel Labath return symbols->GetSymtab(); 1058d5d47a35SPavel Labath return nullptr; 1059d5d47a35SPavel Labath } 1060d5d47a35SPavel Labath 1061b9c1b51eSKate Stone void Module::SetFileSpecAndObjectName(const FileSpec &file, 10620e4c4821SAdrian Prantl ConstString object_name) { 106305097246SAdrian Prantl // Container objects whose paths do not specify a file directly can call this 106405097246SAdrian Prantl // function to correct the file and object names. 106530fdc8d8SChris Lattner m_file = file; 106646376966SJonas Devlieghere m_mod_time = FileSystem::Instance().GetModificationTime(file); 106730fdc8d8SChris Lattner m_object_name = object_name; 106830fdc8d8SChris Lattner } 106930fdc8d8SChris Lattner 1070b9c1b51eSKate Stone const ArchSpec &Module::GetArchitecture() const { return m_arch; } 107130fdc8d8SChris Lattner 1072b9c1b51eSKate Stone std::string Module::GetSpecificationDescription() const { 1073b5ad4ec7SGreg Clayton std::string spec(GetFileSpec().GetPath()); 1074b9c1b51eSKate Stone if (m_object_name) { 1075b5ad4ec7SGreg Clayton spec += '('; 1076b5ad4ec7SGreg Clayton spec += m_object_name.GetCString(); 1077b5ad4ec7SGreg Clayton spec += ')'; 1078b5ad4ec7SGreg Clayton } 1079b5ad4ec7SGreg Clayton return spec; 1080b5ad4ec7SGreg Clayton } 1081b5ad4ec7SGreg Clayton 1082c4c464f8SRaphael Isemann void Module::GetDescription(llvm::raw_ostream &s, 1083c4c464f8SRaphael Isemann lldb::DescriptionLevel level) { 1084b9c1b51eSKate Stone if (level >= eDescriptionLevelFull) { 1085cfd1acedSGreg Clayton if (m_arch.IsValid()) 1086c4c464f8SRaphael Isemann s << llvm::formatv("({0}) ", m_arch.GetArchitectureName()); 1087c982b3d6SGreg Clayton } 1088ceb6b139SCaroline Tice 1089b9c1b51eSKate Stone if (level == eDescriptionLevelBrief) { 1090c982b3d6SGreg Clayton const char *filename = m_file.GetFilename().GetCString(); 1091c982b3d6SGreg Clayton if (filename) 1092c4c464f8SRaphael Isemann s << filename; 1093b9c1b51eSKate Stone } else { 1094cfd1acedSGreg Clayton char path[PATH_MAX]; 1095cfd1acedSGreg Clayton if (m_file.GetPath(path, sizeof(path))) 1096c4c464f8SRaphael Isemann s << path; 1097c982b3d6SGreg Clayton } 1098cfd1acedSGreg Clayton 1099cfd1acedSGreg Clayton const char *object_name = m_object_name.GetCString(); 1100cfd1acedSGreg Clayton if (object_name) 1101c4c464f8SRaphael Isemann s << llvm::formatv("({0})", object_name); 1102ceb6b139SCaroline Tice } 1103ceb6b139SCaroline Tice 1104b9c1b51eSKate Stone void Module::ReportError(const char *format, ...) { 1105b9c1b51eSKate Stone if (format && format[0]) { 1106e38a5eddSGreg Clayton StreamString strm; 1107e38a5eddSGreg Clayton strm.PutCString("error: "); 1108c4c464f8SRaphael Isemann GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelBrief); 11098b35334eSGreg Clayton strm.PutChar(' '); 1110c982b3d6SGreg Clayton va_list args; 1111c982b3d6SGreg Clayton va_start(args, format); 1112e38a5eddSGreg Clayton strm.PrintfVarArg(format, args); 1113c982b3d6SGreg Clayton va_end(args); 1114e38a5eddSGreg Clayton 1115e38a5eddSGreg Clayton const int format_len = strlen(format); 1116b9c1b51eSKate Stone if (format_len > 0) { 1117e38a5eddSGreg Clayton const char last_char = format[format_len - 1]; 1118376230c9SRaphael Isemann if (last_char != '\n' && last_char != '\r') 1119e38a5eddSGreg Clayton strm.EOL(); 1120e38a5eddSGreg Clayton } 1121c156427dSZachary Turner Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData()); 1122e38a5eddSGreg Clayton } 1123e38a5eddSGreg Clayton } 1124e38a5eddSGreg Clayton 1125b9c1b51eSKate Stone bool Module::FileHasChanged() const { 1126a4a00cedSFred Riss // We have provided the DataBuffer for this module to avoid accessing the 1127a4a00cedSFred Riss // filesystem. We never want to reload those files. 1128a4a00cedSFred Riss if (m_data_sp) 1129a4a00cedSFred Riss return false; 1130c5dac77aSEugene Zelenko if (!m_file_has_changed) 11311408bf72SPavel Labath m_file_has_changed = 113246376966SJonas Devlieghere (FileSystem::Instance().GetModificationTime(m_file) != m_mod_time); 11331d60909eSGreg Clayton return m_file_has_changed; 11341d60909eSGreg Clayton } 11351d60909eSGreg Clayton 1136b9c1b51eSKate Stone void Module::ReportErrorIfModifyDetected(const char *format, ...) { 1137b9c1b51eSKate Stone if (!m_first_file_changed_log) { 1138b9c1b51eSKate Stone if (FileHasChanged()) { 11391d60909eSGreg Clayton m_first_file_changed_log = true; 1140b9c1b51eSKate Stone if (format) { 1141e38a5eddSGreg Clayton StreamString strm; 1142e38a5eddSGreg Clayton strm.PutCString("error: the object file "); 1143c4c464f8SRaphael Isemann GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull); 1144e38a5eddSGreg Clayton strm.PutCString(" has been modified\n"); 1145e38a5eddSGreg Clayton 1146e38a5eddSGreg Clayton va_list args; 1147e38a5eddSGreg Clayton va_start(args, format); 1148e38a5eddSGreg Clayton strm.PrintfVarArg(format, args); 1149e38a5eddSGreg Clayton va_end(args); 1150e38a5eddSGreg Clayton 1151e38a5eddSGreg Clayton const int format_len = strlen(format); 1152b9c1b51eSKate Stone if (format_len > 0) { 1153e38a5eddSGreg Clayton const char last_char = format[format_len - 1]; 1154376230c9SRaphael Isemann if (last_char != '\n' && last_char != '\r') 1155e38a5eddSGreg Clayton strm.EOL(); 1156e38a5eddSGreg Clayton } 1157b9c1b51eSKate Stone strm.PutCString("The debug session should be aborted as the original " 1158b9c1b51eSKate Stone "debug information has been overwritten.\n"); 1159c156427dSZachary Turner Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData()); 1160e38a5eddSGreg Clayton } 1161e38a5eddSGreg Clayton } 1162c982b3d6SGreg Clayton } 11631d60909eSGreg Clayton } 1164c982b3d6SGreg Clayton 1165b9c1b51eSKate Stone void Module::ReportWarning(const char *format, ...) { 1166b9c1b51eSKate Stone if (format && format[0]) { 1167e38a5eddSGreg Clayton StreamString strm; 1168e38a5eddSGreg Clayton strm.PutCString("warning: "); 1169c4c464f8SRaphael Isemann GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull); 11708b35334eSGreg Clayton strm.PutChar(' '); 1171c982b3d6SGreg Clayton 1172c982b3d6SGreg Clayton va_list args; 1173c982b3d6SGreg Clayton va_start(args, format); 1174e38a5eddSGreg Clayton strm.PrintfVarArg(format, args); 1175c982b3d6SGreg Clayton va_end(args); 1176e38a5eddSGreg Clayton 1177e38a5eddSGreg Clayton const int format_len = strlen(format); 1178b9c1b51eSKate Stone if (format_len > 0) { 1179e38a5eddSGreg Clayton const char last_char = format[format_len - 1]; 1180376230c9SRaphael Isemann if (last_char != '\n' && last_char != '\r') 1181e38a5eddSGreg Clayton strm.EOL(); 1182e38a5eddSGreg Clayton } 1183c156427dSZachary Turner Host::SystemLog(Host::eSystemLogWarning, "%s", strm.GetData()); 1184e38a5eddSGreg Clayton } 1185c982b3d6SGreg Clayton } 1186c982b3d6SGreg Clayton 1187b9c1b51eSKate Stone void Module::LogMessage(Log *log, const char *format, ...) { 1188b9c1b51eSKate Stone if (log != nullptr) { 1189c982b3d6SGreg Clayton StreamString log_message; 1190c4c464f8SRaphael Isemann GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull); 1191c982b3d6SGreg Clayton log_message.PutCString(": "); 1192c982b3d6SGreg Clayton va_list args; 1193c982b3d6SGreg Clayton va_start(args, format); 1194c982b3d6SGreg Clayton log_message.PrintfVarArg(format, args); 1195c982b3d6SGreg Clayton va_end(args); 1196c156427dSZachary Turner log->PutCString(log_message.GetData()); 1197c982b3d6SGreg Clayton } 1198c982b3d6SGreg Clayton } 1199c982b3d6SGreg Clayton 1200b9c1b51eSKate Stone void Module::LogMessageVerboseBacktrace(Log *log, const char *format, ...) { 1201b9c1b51eSKate Stone if (log != nullptr) { 1202d61c0fc0SGreg Clayton StreamString log_message; 1203c4c464f8SRaphael Isemann GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull); 1204d61c0fc0SGreg Clayton log_message.PutCString(": "); 1205d61c0fc0SGreg Clayton va_list args; 1206d61c0fc0SGreg Clayton va_start(args, format); 1207d61c0fc0SGreg Clayton log_message.PrintfVarArg(format, args); 1208d61c0fc0SGreg Clayton va_end(args); 1209b9c1b51eSKate Stone if (log->GetVerbose()) { 1210a893d301SZachary Turner std::string back_trace; 1211a893d301SZachary Turner llvm::raw_string_ostream stream(back_trace); 1212a893d301SZachary Turner llvm::sys::PrintStackTrace(stream); 1213771ef6d4SMalcolm Parsons log_message.PutCString(back_trace); 1214a893d301SZachary Turner } 1215c156427dSZachary Turner log->PutCString(log_message.GetData()); 1216d61c0fc0SGreg Clayton } 1217d61c0fc0SGreg Clayton } 1218d61c0fc0SGreg Clayton 1219b9c1b51eSKate Stone void Module::Dump(Stream *s) { 122016ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 12218941142aSGreg Clayton // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 122230fdc8d8SChris Lattner s->Indent(); 1223b9c1b51eSKate Stone s->Printf("Module %s%s%s%s\n", m_file.GetPath().c_str(), 122430fdc8d8SChris Lattner m_object_name ? "(" : "", 122530fdc8d8SChris Lattner m_object_name ? m_object_name.GetCString() : "", 122630fdc8d8SChris Lattner m_object_name ? ")" : ""); 122730fdc8d8SChris Lattner 122830fdc8d8SChris Lattner s->IndentMore(); 122930fdc8d8SChris Lattner 1230a7499c98SMichael Sartain ObjectFile *objfile = GetObjectFile(); 123130fdc8d8SChris Lattner if (objfile) 123230fdc8d8SChris Lattner objfile->Dump(s); 123330fdc8d8SChris Lattner 1234d5d47a35SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 1235d5d47a35SPavel Labath symbols->Dump(*s); 123630fdc8d8SChris Lattner 123730fdc8d8SChris Lattner s->IndentLess(); 123830fdc8d8SChris Lattner } 123930fdc8d8SChris Lattner 12400e4c4821SAdrian Prantl ConstString Module::GetObjectName() const { return m_object_name; } 124130fdc8d8SChris Lattner 1242b9c1b51eSKate Stone ObjectFile *Module::GetObjectFile() { 1243b9c1b51eSKate Stone if (!m_did_load_objfile.load()) { 124416ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 1245b9c1b51eSKate Stone if (!m_did_load_objfile.load()) { 12465c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF("Module::GetObjectFile () module = %s", 1247b9c1b51eSKate Stone GetFileSpec().GetFilename().AsCString("")); 12485ce9c565SGreg Clayton lldb::offset_t data_offset = 0; 1249a4a00cedSFred Riss lldb::offset_t file_size = 0; 1250a4a00cedSFred Riss 1251a4a00cedSFred Riss if (m_data_sp) 1252a4a00cedSFred Riss file_size = m_data_sp->GetByteSize(); 1253a4a00cedSFred Riss else if (m_file) 1254a4a00cedSFred Riss file_size = FileSystem::Instance().GetByteSize(m_file); 1255a4a00cedSFred Riss 1256b9c1b51eSKate Stone if (file_size > m_object_offset) { 12572540a8a7SGreg Clayton m_did_load_objfile = true; 1258a4a00cedSFred Riss // FindPlugin will modify its data_sp argument. Do not let it 1259a4a00cedSFred Riss // modify our m_data_sp member. 1260a4a00cedSFred Riss auto data_sp = m_data_sp; 1261b9c1b51eSKate Stone m_objfile_sp = ObjectFile::FindPlugin( 1262b9c1b51eSKate Stone shared_from_this(), &m_file, m_object_offset, 1263b9c1b51eSKate Stone file_size - m_object_offset, data_sp, data_offset); 1264b9c1b51eSKate Stone if (m_objfile_sp) { 1265b9c1b51eSKate Stone // Once we get the object file, update our module with the object 126605097246SAdrian Prantl // file's architecture since it might differ in vendor/os if some 126705097246SAdrian Prantl // parts were unknown. But since the matching arch might already be 126805097246SAdrian Prantl // more specific than the generic COFF architecture, only merge in 126905097246SAdrian Prantl // those values that overwrite unspecified unknown values. 1270f760f5aeSPavel Labath m_arch.MergeFrom(m_objfile_sp->GetArchitecture()); 1271b9c1b51eSKate Stone } else { 1272b9c1b51eSKate Stone ReportError("failed to load objfile for %s", 1273b9c1b51eSKate Stone GetFileSpec().GetPath().c_str()); 12740ee56ce6STodd Fiala } 127530fdc8d8SChris Lattner } 12762540a8a7SGreg Clayton } 127788c05f54SGreg Clayton } 1278762f7135SGreg Clayton return m_objfile_sp.get(); 127930fdc8d8SChris Lattner } 128030fdc8d8SChris Lattner 1281b9c1b51eSKate Stone SectionList *Module::GetSectionList() { 1282d5b44036SJonas Devlieghere // Populate m_sections_up with sections from objfile. 1283d5b44036SJonas Devlieghere if (!m_sections_up) { 12843046e668SGreg Clayton ObjectFile *obj_file = GetObjectFile(); 1285c5dac77aSEugene Zelenko if (obj_file != nullptr) 12863046e668SGreg Clayton obj_file->CreateSections(*GetUnifiedSectionList()); 12873046e668SGreg Clayton } 1288d5b44036SJonas Devlieghere return m_sections_up.get(); 12893046e668SGreg Clayton } 12903046e668SGreg Clayton 1291b9c1b51eSKate Stone void Module::SectionFileAddressesChanged() { 129205a09c67SJason Molenda ObjectFile *obj_file = GetObjectFile(); 129305a09c67SJason Molenda if (obj_file) 129405a09c67SJason Molenda obj_file->SectionFileAddressesChanged(); 129523f70e83SPavel Labath if (SymbolFile *symbols = GetSymbolFile()) 129623f70e83SPavel Labath symbols->SectionFileAddressesChanged(); 129705a09c67SJason Molenda } 129805a09c67SJason Molenda 1299dec96392SPavel Labath UnwindTable &Module::GetUnwindTable() { 1300dec96392SPavel Labath if (!m_unwind_table) 1301dec96392SPavel Labath m_unwind_table.emplace(*this); 1302dec96392SPavel Labath return *m_unwind_table; 1303dec96392SPavel Labath } 1304dec96392SPavel Labath 1305b9c1b51eSKate Stone SectionList *Module::GetUnifiedSectionList() { 1306d5b44036SJonas Devlieghere if (!m_sections_up) 1307a8f3ae7cSJonas Devlieghere m_sections_up = std::make_unique<SectionList>(); 1308d5b44036SJonas Devlieghere return m_sections_up.get(); 1309a7499c98SMichael Sartain } 131030fdc8d8SChris Lattner 13110e4c4821SAdrian Prantl const Symbol *Module::FindFirstSymbolWithNameAndType(ConstString name, 1312b9c1b51eSKate Stone SymbolType symbol_type) { 13135c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF( 13145c1c8443SJonas Devlieghere "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)", 1315b9c1b51eSKate Stone name.AsCString(), symbol_type); 1316d5d47a35SPavel Labath if (Symtab *symtab = GetSymtab()) 1317b9c1b51eSKate Stone return symtab->FindFirstSymbolWithNameAndType( 1318b9c1b51eSKate Stone name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny); 1319c5dac77aSEugene Zelenko return nullptr; 132030fdc8d8SChris Lattner } 1321b9c1b51eSKate Stone void Module::SymbolIndicesToSymbolContextList( 1322b9c1b51eSKate Stone Symtab *symtab, std::vector<uint32_t> &symbol_indexes, 1323b9c1b51eSKate Stone SymbolContextList &sc_list) { 132430fdc8d8SChris Lattner // No need to protect this call using m_mutex all other method calls are 132530fdc8d8SChris Lattner // already thread safe. 132630fdc8d8SChris Lattner 132730fdc8d8SChris Lattner size_t num_indices = symbol_indexes.size(); 1328b9c1b51eSKate Stone if (num_indices > 0) { 132930fdc8d8SChris Lattner SymbolContext sc; 133030fdc8d8SChris Lattner CalculateSymbolContext(&sc); 1331b9c1b51eSKate Stone for (size_t i = 0; i < num_indices; i++) { 133230fdc8d8SChris Lattner sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]); 133330fdc8d8SChris Lattner if (sc.symbol) 133430fdc8d8SChris Lattner sc_list.Append(sc); 133530fdc8d8SChris Lattner } 133630fdc8d8SChris Lattner } 133730fdc8d8SChris Lattner } 133830fdc8d8SChris Lattner 13391ad655e2SAdrian Prantl void Module::FindFunctionSymbols(ConstString name, 1340c1b2ccfdSGreg Clayton uint32_t name_type_mask, 1341b9c1b51eSKate Stone SymbolContextList &sc_list) { 13425c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF("Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)", 1343b9c1b51eSKate Stone name.AsCString(), name_type_mask); 1344d5d47a35SPavel Labath if (Symtab *symtab = GetSymtab()) 13451ad655e2SAdrian Prantl symtab->FindFunctionSymbols(name, name_type_mask, sc_list); 1346c1b2ccfdSGreg Clayton } 1347c1b2ccfdSGreg Clayton 13481ad655e2SAdrian Prantl void Module::FindSymbolsWithNameAndType(ConstString name, 1349b9c1b51eSKate Stone SymbolType symbol_type, 1350b9c1b51eSKate Stone SymbolContextList &sc_list) { 135130fdc8d8SChris Lattner // No need to protect this call using m_mutex all other method calls are 135230fdc8d8SChris Lattner // already thread safe. 13535c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF( 13545c1c8443SJonas Devlieghere "Module::FindSymbolsWithNameAndType (name = %s, type = %i)", 1355b9c1b51eSKate Stone name.AsCString(), symbol_type); 1356d5d47a35SPavel Labath if (Symtab *symtab = GetSymtab()) { 135730fdc8d8SChris Lattner std::vector<uint32_t> symbol_indexes; 135830fdc8d8SChris Lattner symtab->FindAllSymbolsWithNameAndType(name, symbol_type, symbol_indexes); 135930fdc8d8SChris Lattner SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list); 136030fdc8d8SChris Lattner } 136130fdc8d8SChris Lattner } 136230fdc8d8SChris Lattner 13631ad655e2SAdrian Prantl void Module::FindSymbolsMatchingRegExAndType(const RegularExpression ®ex, 1364b9c1b51eSKate Stone SymbolType symbol_type, 1365b9c1b51eSKate Stone SymbolContextList &sc_list) { 136630fdc8d8SChris Lattner // No need to protect this call using m_mutex all other method calls are 136730fdc8d8SChris Lattner // already thread safe. 13685c1c8443SJonas Devlieghere LLDB_SCOPED_TIMERF( 136930fdc8d8SChris Lattner "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)", 137095eae423SZachary Turner regex.GetText().str().c_str(), symbol_type); 1371d5d47a35SPavel Labath if (Symtab *symtab = GetSymtab()) { 137230fdc8d8SChris Lattner std::vector<uint32_t> symbol_indexes; 1373b9c1b51eSKate Stone symtab->FindAllSymbolsMatchingRexExAndType( 1374b9c1b51eSKate Stone regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, 1375b9c1b51eSKate Stone symbol_indexes); 137630fdc8d8SChris Lattner SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list); 137730fdc8d8SChris Lattner } 137830fdc8d8SChris Lattner } 137930fdc8d8SChris Lattner 13807fca8c07SJim Ingham void Module::PreloadSymbols() { 13817fca8c07SJim Ingham std::lock_guard<std::recursive_mutex> guard(m_mutex); 138223f70e83SPavel Labath SymbolFile *sym_file = GetSymbolFile(); 138323f70e83SPavel Labath if (!sym_file) 13847fca8c07SJim Ingham return; 138523f70e83SPavel Labath 13867fca8c07SJim Ingham // Prime the symbol file first, since it adds symbols to the symbol table. 138723f70e83SPavel Labath sym_file->PreloadSymbols(); 138823f70e83SPavel Labath 13897fca8c07SJim Ingham // Now we can prime the symbol table. 139023f70e83SPavel Labath if (Symtab *symtab = sym_file->GetSymtab()) 13917fca8c07SJim Ingham symtab->PreloadSymbols(); 13927fca8c07SJim Ingham } 13937fca8c07SJim Ingham 1394b9c1b51eSKate Stone void Module::SetSymbolFileFileSpec(const FileSpec &file) { 1395dbd7fabaSJonas Devlieghere if (!FileSystem::Instance().Exists(file)) 139690271672SGreg Clayton return; 1397d5b44036SJonas Devlieghere if (m_symfile_up) { 1398b9c1b51eSKate Stone // Remove any sections in the unified section list that come from the 1399b9c1b51eSKate Stone // current symbol vendor. 14003046e668SGreg Clayton SectionList *section_list = GetSectionList(); 140123f70e83SPavel Labath SymbolFile *symbol_file = GetSymbolFile(); 1402b9c1b51eSKate Stone if (section_list && symbol_file) { 1403a7499c98SMichael Sartain ObjectFile *obj_file = symbol_file->GetObjectFile(); 1404b9c1b51eSKate Stone // Make sure we have an object file and that the symbol vendor's objfile 140505097246SAdrian Prantl // isn't the same as the module's objfile before we remove any sections 140605097246SAdrian Prantl // for it... 1407b9c1b51eSKate Stone if (obj_file) { 1408b9c1b51eSKate Stone // Check to make sure we aren't trying to specify the file we already 1409b9c1b51eSKate Stone // have 1410b9c1b51eSKate Stone if (obj_file->GetFileSpec() == file) { 141190271672SGreg Clayton // We are being told to add the exact same file that we already have 141290271672SGreg Clayton // we don't have to do anything. 141390271672SGreg Clayton return; 141490271672SGreg Clayton } 1415d00438e8STamas Berghammer 1416b9c1b51eSKate Stone // Cleare the current symtab as we are going to replace it with a new 1417b9c1b51eSKate Stone // one 1418d00438e8STamas Berghammer obj_file->ClearSymtab(); 141990271672SGreg Clayton 1420dec96392SPavel Labath // Clear the unwind table too, as that may also be affected by the 1421dec96392SPavel Labath // symbol file information. 1422dec96392SPavel Labath m_unwind_table.reset(); 1423dec96392SPavel Labath 1424b9c1b51eSKate Stone // The symbol file might be a directory bundle ("/tmp/a.out.dSYM") 142505097246SAdrian Prantl // instead of a full path to the symbol file within the bundle 1426b9c1b51eSKate Stone // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to 1427b9c1b51eSKate Stone // check this 142890271672SGreg Clayton 14293a58d898SJonas Devlieghere if (FileSystem::Instance().IsDirectory(file)) { 143090271672SGreg Clayton std::string new_path(file.GetPath()); 143190271672SGreg Clayton std::string old_path(obj_file->GetFileSpec().GetPath()); 1432e8f13f4fSBenjamin Kramer if (llvm::StringRef(old_path).startswith(new_path)) { 1433b9c1b51eSKate Stone // We specified the same bundle as the symbol file that we already 1434b9c1b51eSKate Stone // have 143590271672SGreg Clayton return; 143690271672SGreg Clayton } 143790271672SGreg Clayton } 143890271672SGreg Clayton 1439b9c1b51eSKate Stone if (obj_file != m_objfile_sp.get()) { 1440a7499c98SMichael Sartain size_t num_sections = section_list->GetNumSections(0); 1441b9c1b51eSKate Stone for (size_t idx = num_sections; idx > 0; --idx) { 1442b9c1b51eSKate Stone lldb::SectionSP section_sp( 1443b9c1b51eSKate Stone section_list->GetSectionAtIndex(idx - 1)); 1444b9c1b51eSKate Stone if (section_sp->GetObjectFile() == obj_file) { 14453046e668SGreg Clayton section_list->DeleteSection(idx - 1); 1446a7499c98SMichael Sartain } 1447a7499c98SMichael Sartain } 1448a7499c98SMichael Sartain } 1449a7499c98SMichael Sartain } 1450a7499c98SMichael Sartain } 1451b9c1b51eSKate Stone // Keep all old symbol files around in case there are any lingering type 145205097246SAdrian Prantl // references in any SBValue objects that might have been handed out. 1453d5b44036SJonas Devlieghere m_old_symfiles.push_back(std::move(m_symfile_up)); 145490271672SGreg Clayton } 1455e01e07b6SGreg Clayton m_symfile_spec = file; 1456d5b44036SJonas Devlieghere m_symfile_up.reset(); 1457579d6d1aSPavel Labath m_did_load_symfile = false; 1458e01e07b6SGreg Clayton } 1459e01e07b6SGreg Clayton 1460b9c1b51eSKate Stone bool Module::IsExecutable() { 1461c5dac77aSEugene Zelenko if (GetObjectFile() == nullptr) 14625aee162fSJim Ingham return false; 14635aee162fSJim Ingham else 14645aee162fSJim Ingham return GetObjectFile()->IsExecutable(); 14655aee162fSJim Ingham } 14665aee162fSJim Ingham 1467b9c1b51eSKate Stone bool Module::IsLoadedInTarget(Target *target) { 1468b53cb271SJim Ingham ObjectFile *obj_file = GetObjectFile(); 1469b9c1b51eSKate Stone if (obj_file) { 14703046e668SGreg Clayton SectionList *sections = GetSectionList(); 1471b9c1b51eSKate Stone if (sections != nullptr) { 1472b53cb271SJim Ingham size_t num_sections = sections->GetSize(); 1473b9c1b51eSKate Stone for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) { 1474b53cb271SJim Ingham SectionSP section_sp = sections->GetSectionAtIndex(sect_idx); 1475b9c1b51eSKate Stone if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) { 1476b53cb271SJim Ingham return true; 1477b53cb271SJim Ingham } 1478b53cb271SJim Ingham } 1479b53cb271SJim Ingham } 1480b53cb271SJim Ingham } 1481b53cb271SJim Ingham return false; 1482b53cb271SJim Ingham } 14831759848bSEnrico Granata 148497206d57SZachary Turner bool Module::LoadScriptingResourceInTarget(Target *target, Status &error, 1485b9c1b51eSKate Stone Stream *feedback_stream) { 1486b9c1b51eSKate Stone if (!target) { 14871759848bSEnrico Granata error.SetErrorString("invalid destination Target"); 14881759848bSEnrico Granata return false; 14891759848bSEnrico Granata } 14901759848bSEnrico Granata 1491b9c1b51eSKate Stone LoadScriptFromSymFile should_load = 1492b9c1b51eSKate Stone target->TargetProperties::GetLoadScriptFromSymbolFile(); 14932ea43cdcSEnrico Granata 1494994740fbSGreg Clayton if (should_load == eLoadScriptFromSymFileFalse) 1495994740fbSGreg Clayton return false; 1496994740fbSGreg Clayton 149791c0e749SGreg Clayton Debugger &debugger = target->GetDebugger(); 149891c0e749SGreg Clayton const ScriptLanguage script_language = debugger.GetScriptLanguage(); 1499b9c1b51eSKate Stone if (script_language != eScriptLanguageNone) { 150091c0e749SGreg Clayton 15011759848bSEnrico Granata PlatformSP platform_sp(target->GetPlatform()); 15021759848bSEnrico Granata 1503b9c1b51eSKate Stone if (!platform_sp) { 15041759848bSEnrico Granata error.SetErrorString("invalid Platform"); 15051759848bSEnrico Granata return false; 15061759848bSEnrico Granata } 15071759848bSEnrico Granata 1508b9c1b51eSKate Stone FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources( 1509b9c1b51eSKate Stone target, *this, feedback_stream); 151091c0e749SGreg Clayton 151191c0e749SGreg Clayton const uint32_t num_specs = file_specs.GetSize(); 1512b9c1b51eSKate Stone if (num_specs) { 15132b29b432SJonas Devlieghere ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter(); 1514b9c1b51eSKate Stone if (script_interpreter) { 1515b9c1b51eSKate Stone for (uint32_t i = 0; i < num_specs; ++i) { 151691c0e749SGreg Clayton FileSpec scripting_fspec(file_specs.GetFileSpecAtIndex(i)); 1517dbd7fabaSJonas Devlieghere if (scripting_fspec && 1518dbd7fabaSJonas Devlieghere FileSystem::Instance().Exists(scripting_fspec)) { 1519b9c1b51eSKate Stone if (should_load == eLoadScriptFromSymFileWarn) { 1520397ddd5fSEnrico Granata if (feedback_stream) 1521b9c1b51eSKate Stone feedback_stream->Printf( 1522b9c1b51eSKate Stone "warning: '%s' contains a debug script. To run this script " 1523b9c1b51eSKate Stone "in " 1524b9c1b51eSKate Stone "this debug session:\n\n command script import " 1525b9c1b51eSKate Stone "\"%s\"\n\n" 1526d516deb4SJim Ingham "To run all discovered debug scripts in this session:\n\n" 1527b9c1b51eSKate Stone " settings set target.load-script-from-symbol-file " 1528b9c1b51eSKate Stone "true\n", 1529d516deb4SJim Ingham GetFileSpec().GetFileNameStrippingExtension().GetCString(), 1530d516deb4SJim Ingham scripting_fspec.GetPath().c_str()); 15312ea43cdcSEnrico Granata return false; 15322ea43cdcSEnrico Granata } 15331759848bSEnrico Granata StreamString scripting_stream; 15344dac97ebSRaphael Isemann scripting_fspec.Dump(scripting_stream.AsRawOstream()); 15356a51085eSJim Ingham const bool init_lldb_globals = false; 1536b9c1b51eSKate Stone bool did_load = script_interpreter->LoadScriptingModule( 153715625112SJonas Devlieghere scripting_stream.GetData(), init_lldb_globals, error); 15381759848bSEnrico Granata if (!did_load) 15391759848bSEnrico Granata return false; 15401759848bSEnrico Granata } 154191c0e749SGreg Clayton } 1542b9c1b51eSKate Stone } else { 15431759848bSEnrico Granata error.SetErrorString("invalid ScriptInterpreter"); 15441759848bSEnrico Granata return false; 15451759848bSEnrico Granata } 15461759848bSEnrico Granata } 1547b9d8890bSGreg Clayton } 15481759848bSEnrico Granata return true; 15491759848bSEnrico Granata } 15501759848bSEnrico Granata 1551b9c1b51eSKate Stone bool Module::SetArchitecture(const ArchSpec &new_arch) { 1552b9c1b51eSKate Stone if (!m_arch.IsValid()) { 15535aee162fSJim Ingham m_arch = new_arch; 15545aee162fSJim Ingham return true; 15555aee162fSJim Ingham } 1556b6cd5fe9SChaoren Lin return m_arch.IsCompatibleMatch(new_arch); 15575aee162fSJim Ingham } 15585aee162fSJim Ingham 1559b9c1b51eSKate Stone bool Module::SetLoadAddress(Target &target, lldb::addr_t value, 1560b9c1b51eSKate Stone bool value_is_offset, bool &changed) { 15619e02dacdSSteve Pucci ObjectFile *object_file = GetObjectFile(); 1562b9c1b51eSKate Stone if (object_file != nullptr) { 1563751caf65SGreg Clayton changed = object_file->SetLoadAddress(target, value, value_is_offset); 15647524e090SGreg Clayton return true; 1565b9c1b51eSKate Stone } else { 15667524e090SGreg Clayton changed = false; 1567c9660546SGreg Clayton } 15689e02dacdSSteve Pucci return false; 1569c9660546SGreg Clayton } 1570c9660546SGreg Clayton 1571b9c1b51eSKate Stone bool Module::MatchesModuleSpec(const ModuleSpec &module_ref) { 1572b9a01b39SGreg Clayton const UUID &uuid = module_ref.GetUUID(); 1573b9a01b39SGreg Clayton 1574b9c1b51eSKate Stone if (uuid.IsValid()) { 1575b9a01b39SGreg Clayton // If the UUID matches, then nothing more needs to match... 1576c5dac77aSEugene Zelenko return (uuid == GetUUID()); 1577b9a01b39SGreg Clayton } 1578b9a01b39SGreg Clayton 1579b9a01b39SGreg Clayton const FileSpec &file_spec = module_ref.GetFileSpec(); 1580532290e6SPavel Labath if (!FileSpec::Match(file_spec, m_file) && 1581532290e6SPavel Labath !FileSpec::Match(file_spec, m_platform_file)) 1582b9a01b39SGreg Clayton return false; 1583b9a01b39SGreg Clayton 1584b9a01b39SGreg Clayton const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec(); 1585532290e6SPavel Labath if (!FileSpec::Match(platform_file_spec, GetPlatformFileSpec())) 1586b9a01b39SGreg Clayton return false; 1587b9a01b39SGreg Clayton 1588b9a01b39SGreg Clayton const ArchSpec &arch = module_ref.GetArchitecture(); 1589b9c1b51eSKate Stone if (arch.IsValid()) { 1590bf4b7be6SSean Callanan if (!m_arch.IsCompatibleMatch(arch)) 1591b9a01b39SGreg Clayton return false; 1592b9a01b39SGreg Clayton } 1593b9a01b39SGreg Clayton 15940e4c4821SAdrian Prantl ConstString object_name = module_ref.GetObjectName(); 1595b9c1b51eSKate Stone if (object_name) { 1596b9a01b39SGreg Clayton if (object_name != GetObjectName()) 1597b9a01b39SGreg Clayton return false; 1598b9a01b39SGreg Clayton } 1599b9a01b39SGreg Clayton return true; 1600b9a01b39SGreg Clayton } 1601b9a01b39SGreg Clayton 1602b9c1b51eSKate Stone bool Module::FindSourceFile(const FileSpec &orig_spec, 1603b9c1b51eSKate Stone FileSpec &new_spec) const { 160416ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 1605d804d285SGreg Clayton return m_source_mappings.FindFile(orig_spec, new_spec); 1606d804d285SGreg Clayton } 1607d804d285SGreg Clayton 1608a498f0ecSZachary Turner bool Module::RemapSourceFile(llvm::StringRef path, 1609a498f0ecSZachary Turner std::string &new_path) const { 161016ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 1611f9be6933SGreg Clayton return m_source_mappings.RemapPath(path, new_path); 1612f9be6933SGreg Clayton } 1613f9be6933SGreg Clayton 16141e05d7b3SAdrian Prantl void Module::RegisterXcodeSDK(llvm::StringRef sdk_name, llvm::StringRef sysroot) { 16151e05d7b3SAdrian Prantl XcodeSDK sdk(sdk_name.str()); 1616f0c08b7eSAdrian Prantl ConstString sdk_path(HostInfo::GetXcodeSDKPath(sdk)); 16171e05d7b3SAdrian Prantl if (!sdk_path) 16181e05d7b3SAdrian Prantl return; 1619f0c08b7eSAdrian Prantl // If the SDK changed for a previously registered source path, update it. 16201e05d7b3SAdrian Prantl // This could happend with -fdebug-prefix-map, otherwise it's unlikely. 16211e05d7b3SAdrian Prantl ConstString sysroot_cs(sysroot); 16221e05d7b3SAdrian Prantl if (!m_source_mappings.Replace(sysroot_cs, sdk_path, true)) 16231e05d7b3SAdrian Prantl // In the general case, however, append it to the list. 16241e05d7b3SAdrian Prantl m_source_mappings.Append(sysroot_cs, 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