1cfca06d7SDimitry Andric //===-- Module.cpp --------------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f3fbd1c0SDimitry Andric #include "lldb/Core/Module.h"
10f3fbd1c0SDimitry Andric
1194994d37SDimitry Andric #include "lldb/Core/AddressRange.h"
12f21a844fSEd Maste #include "lldb/Core/AddressResolverFileLine.h"
1377fc4c14SDimitry Andric #include "lldb/Core/DataFileCache.h"
1494994d37SDimitry Andric #include "lldb/Core/Debugger.h"
1594994d37SDimitry Andric #include "lldb/Core/Mangled.h"
16f034231aSEd Maste #include "lldb/Core/ModuleSpec.h"
1794994d37SDimitry Andric #include "lldb/Core/SearchFilter.h"
18f034231aSEd Maste #include "lldb/Core/Section.h"
1914f1b3e8SDimitry Andric #include "lldb/Host/FileSystem.h"
20f034231aSEd Maste #include "lldb/Host/Host.h"
21cfca06d7SDimitry Andric #include "lldb/Host/HostInfo.h"
22f034231aSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
23f034231aSEd Maste #include "lldb/Interpreter/ScriptInterpreter.h"
24f034231aSEd Maste #include "lldb/Symbol/CompileUnit.h"
2594994d37SDimitry Andric #include "lldb/Symbol/Function.h"
26f034231aSEd Maste #include "lldb/Symbol/ObjectFile.h"
2794994d37SDimitry Andric #include "lldb/Symbol/Symbol.h"
28f034231aSEd Maste #include "lldb/Symbol/SymbolContext.h"
29e81d9d49SDimitry Andric #include "lldb/Symbol/SymbolFile.h"
30b1c73532SDimitry Andric #include "lldb/Symbol/SymbolLocator.h"
31f034231aSEd Maste #include "lldb/Symbol/SymbolVendor.h"
3294994d37SDimitry Andric #include "lldb/Symbol/Symtab.h"
3394994d37SDimitry Andric #include "lldb/Symbol/Type.h"
3494994d37SDimitry Andric #include "lldb/Symbol/TypeList.h"
3514f1b3e8SDimitry Andric #include "lldb/Symbol/TypeMap.h"
36e81d9d49SDimitry Andric #include "lldb/Symbol/TypeSystem.h"
37e81d9d49SDimitry Andric #include "lldb/Target/Language.h"
38f034231aSEd Maste #include "lldb/Target/Process.h"
39f034231aSEd Maste #include "lldb/Target/Target.h"
4074a628f7SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
417fa27ce4SDimitry Andric #include "lldb/Utility/FileSpecList.h"
42f73363f1SDimitry Andric #include "lldb/Utility/LLDBAssert.h"
43145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
4474a628f7SDimitry Andric #include "lldb/Utility/Log.h"
4574a628f7SDimitry Andric #include "lldb/Utility/RegularExpression.h"
46b76161e4SDimitry Andric #include "lldb/Utility/Status.h"
4794994d37SDimitry Andric #include "lldb/Utility/Stream.h"
4874a628f7SDimitry Andric #include "lldb/Utility/StreamString.h"
491b306c26SDimitry Andric #include "lldb/Utility/Timer.h"
50f034231aSEd Maste
51f73363f1SDimitry Andric #if defined(_WIN32)
5294994d37SDimitry Andric #include "lldb/Host/windows/PosixApi.h"
5374a628f7SDimitry Andric #endif
5474a628f7SDimitry Andric
5574a628f7SDimitry Andric #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
5674a628f7SDimitry Andric #include "Plugins/Language/ObjC/ObjCLanguage.h"
570cac4ca3SEd Maste
5894994d37SDimitry Andric #include "llvm/ADT/STLExtras.h"
5994994d37SDimitry Andric #include "llvm/Support/Compiler.h"
6077fc4c14SDimitry Andric #include "llvm/Support/DJB.h"
6174a628f7SDimitry Andric #include "llvm/Support/FileSystem.h"
6277fc4c14SDimitry Andric #include "llvm/Support/FormatVariadic.h"
6377fc4c14SDimitry Andric #include "llvm/Support/JSON.h"
6474a628f7SDimitry Andric #include "llvm/Support/Signals.h"
6594994d37SDimitry Andric #include "llvm/Support/raw_ostream.h"
6674a628f7SDimitry Andric
67344a3780SDimitry Andric #include <cassert>
68344a3780SDimitry Andric #include <cinttypes>
69344a3780SDimitry Andric #include <cstdarg>
7094994d37SDimitry Andric #include <cstdint>
71344a3780SDimitry Andric #include <cstring>
7294994d37SDimitry Andric #include <map>
73e3b55780SDimitry Andric #include <optional>
7494994d37SDimitry Andric #include <type_traits>
7594994d37SDimitry Andric #include <utility>
7674a628f7SDimitry Andric
7774a628f7SDimitry Andric namespace lldb_private {
7874a628f7SDimitry Andric class CompilerDeclContext;
7974a628f7SDimitry Andric }
8074a628f7SDimitry Andric namespace lldb_private {
8174a628f7SDimitry Andric class VariableList;
8274a628f7SDimitry Andric }
8374a628f7SDimitry Andric
84f034231aSEd Maste using namespace lldb;
85f034231aSEd Maste using namespace lldb_private;
86f034231aSEd Maste
87f73363f1SDimitry Andric // Shared pointers to modules track module lifetimes in targets and in the
88f73363f1SDimitry Andric // global module, but this collection will track all module objects that are
89f73363f1SDimitry Andric // still alive
90f034231aSEd Maste typedef std::vector<Module *> ModuleCollection;
91f034231aSEd Maste
GetModuleCollection()9214f1b3e8SDimitry Andric static ModuleCollection &GetModuleCollection() {
9314f1b3e8SDimitry Andric // This module collection needs to live past any module, so we could either
94f73363f1SDimitry Andric // make it a shared pointer in each module or just leak is. Since it is only
95f73363f1SDimitry Andric // an empty vector by the time all the modules have gone away, we just leak
96f73363f1SDimitry Andric // it for now. If we decide this is a big problem we can introduce a
97f73363f1SDimitry Andric // Finalize method that will tear everything down in a predictable order.
98f034231aSEd Maste
99f3fbd1c0SDimitry Andric static ModuleCollection *g_module_collection = nullptr;
100f3fbd1c0SDimitry Andric if (g_module_collection == nullptr)
101f034231aSEd Maste g_module_collection = new ModuleCollection();
102f034231aSEd Maste
103f034231aSEd Maste return *g_module_collection;
104f034231aSEd Maste }
105f034231aSEd Maste
GetAllocationModuleCollectionMutex()10614f1b3e8SDimitry Andric std::recursive_mutex &Module::GetAllocationModuleCollectionMutex() {
107f034231aSEd Maste // NOTE: The mutex below must be leaked since the global module list in
108f73363f1SDimitry Andric // the ModuleList class will get torn at some point, and we can't know if it
109f73363f1SDimitry Andric // will tear itself down before the "g_module_collection_mutex" below will.
110f73363f1SDimitry Andric // So we leak a Mutex object below to safeguard against that
111f034231aSEd Maste
112f3fbd1c0SDimitry Andric static std::recursive_mutex *g_module_collection_mutex = nullptr;
113f3fbd1c0SDimitry Andric if (g_module_collection_mutex == nullptr)
114f3fbd1c0SDimitry Andric g_module_collection_mutex = new std::recursive_mutex; // NOTE: known leak
115f3fbd1c0SDimitry Andric return *g_module_collection_mutex;
116f034231aSEd Maste }
117f034231aSEd Maste
GetNumberAllocatedModules()11814f1b3e8SDimitry Andric size_t Module::GetNumberAllocatedModules() {
11914f1b3e8SDimitry Andric std::lock_guard<std::recursive_mutex> guard(
12014f1b3e8SDimitry Andric GetAllocationModuleCollectionMutex());
121f034231aSEd Maste return GetModuleCollection().size();
122f034231aSEd Maste }
123f034231aSEd Maste
GetAllocatedModuleAtIndex(size_t idx)12414f1b3e8SDimitry Andric Module *Module::GetAllocatedModuleAtIndex(size_t idx) {
12514f1b3e8SDimitry Andric std::lock_guard<std::recursive_mutex> guard(
12614f1b3e8SDimitry Andric GetAllocationModuleCollectionMutex());
127f034231aSEd Maste ModuleCollection &modules = GetModuleCollection();
128f034231aSEd Maste if (idx < modules.size())
129f034231aSEd Maste return modules[idx];
130f3fbd1c0SDimitry Andric return nullptr;
131f034231aSEd Maste }
132f034231aSEd Maste
Module(const ModuleSpec & module_spec)133f3fbd1c0SDimitry Andric Module::Module(const ModuleSpec &module_spec)
134145449b1SDimitry Andric : m_file_has_changed(false), m_first_file_changed_log(false) {
135f034231aSEd Maste // Scope for locker below...
136f034231aSEd Maste {
13714f1b3e8SDimitry Andric std::lock_guard<std::recursive_mutex> guard(
13814f1b3e8SDimitry Andric GetAllocationModuleCollectionMutex());
139f034231aSEd Maste GetModuleCollection().push_back(this);
140f034231aSEd Maste }
141f034231aSEd Maste
142145449b1SDimitry Andric Log *log(GetLog(LLDBLog::Object | LLDBLog::Modules));
143f3fbd1c0SDimitry Andric if (log != nullptr)
144ead24645SDimitry Andric LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')",
145ead24645SDimitry Andric static_cast<void *>(this),
14614f1b3e8SDimitry Andric module_spec.GetArchitecture().GetArchitectureName(),
14714f1b3e8SDimitry Andric module_spec.GetFileSpec().GetPath().c_str(),
1480cac4ca3SEd Maste module_spec.GetObjectName().IsEmpty() ? "" : "(",
1491f917f69SDimitry Andric module_spec.GetObjectName().AsCString(""),
1500cac4ca3SEd Maste module_spec.GetObjectName().IsEmpty() ? "" : ")");
1510cac4ca3SEd Maste
152cfca06d7SDimitry Andric auto data_sp = module_spec.GetData();
153cfca06d7SDimitry Andric lldb::offset_t file_size = 0;
154cfca06d7SDimitry Andric if (data_sp)
155cfca06d7SDimitry Andric file_size = data_sp->GetByteSize();
156cfca06d7SDimitry Andric
157f73363f1SDimitry Andric // First extract all module specifications from the file using the local file
158f73363f1SDimitry Andric // path. If there are no specifications, then don't fill anything in
1590cac4ca3SEd Maste ModuleSpecList modules_specs;
160cfca06d7SDimitry Andric if (ObjectFile::GetModuleSpecifications(
161cfca06d7SDimitry Andric module_spec.GetFileSpec(), 0, file_size, modules_specs, data_sp) == 0)
1620cac4ca3SEd Maste return;
1630cac4ca3SEd Maste
1640cac4ca3SEd Maste // Now make sure that one of the module specifications matches what we just
16514f1b3e8SDimitry Andric // extract. We might have a module specification that specifies a file
166f73363f1SDimitry Andric // "/usr/lib/dyld" with UUID XXX, but we might have a local version of
167f73363f1SDimitry Andric // "/usr/lib/dyld" that has
1680cac4ca3SEd Maste // UUID YYY and we don't want those to match. If they don't match, just don't
1690cac4ca3SEd Maste // fill any ivars in so we don't accidentally grab the wrong file later since
1700cac4ca3SEd Maste // they don't match...
1710cac4ca3SEd Maste ModuleSpec matching_module_spec;
17294994d37SDimitry Andric if (!modules_specs.FindMatchingModuleSpec(module_spec,
17394994d37SDimitry Andric matching_module_spec)) {
17494994d37SDimitry Andric if (log) {
175ead24645SDimitry Andric LLDB_LOGF(log, "Found local object file but the specs didn't match");
17694994d37SDimitry Andric }
1770cac4ca3SEd Maste return;
17894994d37SDimitry Andric }
1790cac4ca3SEd Maste
180cfca06d7SDimitry Andric // Set m_data_sp if it was initially provided in the ModuleSpec. Note that
181cfca06d7SDimitry Andric // we cannot use the data_sp variable here, because it will have been
182cfca06d7SDimitry Andric // modified by GetModuleSpecifications().
183cfca06d7SDimitry Andric if (auto module_spec_data_sp = module_spec.GetData()) {
184cfca06d7SDimitry Andric m_data_sp = module_spec_data_sp;
185cfca06d7SDimitry Andric m_mod_time = {};
186cfca06d7SDimitry Andric } else {
1870cac4ca3SEd Maste if (module_spec.GetFileSpec())
18814f1b3e8SDimitry Andric m_mod_time =
189cfca06d7SDimitry Andric FileSystem::Instance().GetModificationTime(module_spec.GetFileSpec());
190cfca06d7SDimitry Andric else if (matching_module_spec.GetFileSpec())
191cfca06d7SDimitry Andric m_mod_time = FileSystem::Instance().GetModificationTime(
192cfca06d7SDimitry Andric matching_module_spec.GetFileSpec());
193cfca06d7SDimitry Andric }
1940cac4ca3SEd Maste
195f73363f1SDimitry Andric // Copy the architecture from the actual spec if we got one back, else use
196f73363f1SDimitry Andric // the one that was specified
1970cac4ca3SEd Maste if (matching_module_spec.GetArchitecture().IsValid())
1980cac4ca3SEd Maste m_arch = matching_module_spec.GetArchitecture();
1990cac4ca3SEd Maste else if (module_spec.GetArchitecture().IsValid())
2000cac4ca3SEd Maste m_arch = module_spec.GetArchitecture();
2010cac4ca3SEd Maste
2020cac4ca3SEd Maste // Copy the file spec over and use the specified one (if there was one) so we
20314f1b3e8SDimitry Andric // don't use a path that might have gotten resolved a path in
20414f1b3e8SDimitry Andric // 'matching_module_spec'
2050cac4ca3SEd Maste if (module_spec.GetFileSpec())
2060cac4ca3SEd Maste m_file = module_spec.GetFileSpec();
2070cac4ca3SEd Maste else if (matching_module_spec.GetFileSpec())
2080cac4ca3SEd Maste m_file = matching_module_spec.GetFileSpec();
2090cac4ca3SEd Maste
2100cac4ca3SEd Maste // Copy the platform file spec over
2110cac4ca3SEd Maste if (module_spec.GetPlatformFileSpec())
2120cac4ca3SEd Maste m_platform_file = module_spec.GetPlatformFileSpec();
2130cac4ca3SEd Maste else if (matching_module_spec.GetPlatformFileSpec())
2140cac4ca3SEd Maste m_platform_file = matching_module_spec.GetPlatformFileSpec();
2150cac4ca3SEd Maste
2160cac4ca3SEd Maste // Copy the symbol file spec over
2170cac4ca3SEd Maste if (module_spec.GetSymbolFileSpec())
2180cac4ca3SEd Maste m_symfile_spec = module_spec.GetSymbolFileSpec();
2190cac4ca3SEd Maste else if (matching_module_spec.GetSymbolFileSpec())
2200cac4ca3SEd Maste m_symfile_spec = matching_module_spec.GetSymbolFileSpec();
2210cac4ca3SEd Maste
2220cac4ca3SEd Maste // Copy the object name over
2230cac4ca3SEd Maste if (matching_module_spec.GetObjectName())
2240cac4ca3SEd Maste m_object_name = matching_module_spec.GetObjectName();
2250cac4ca3SEd Maste else
2260cac4ca3SEd Maste m_object_name = module_spec.GetObjectName();
2270cac4ca3SEd Maste
228f73363f1SDimitry Andric // Always trust the object offset (file offset) and object modification time
229f73363f1SDimitry Andric // (for mod time in a BSD static archive) of from the matching module
230f73363f1SDimitry Andric // specification
2310cac4ca3SEd Maste m_object_offset = matching_module_spec.GetObjectOffset();
2320cac4ca3SEd Maste m_object_mod_time = matching_module_spec.GetObjectModificationTime();
233f034231aSEd Maste }
234f034231aSEd Maste
Module(const FileSpec & file_spec,const ArchSpec & arch,ConstString object_name,lldb::offset_t object_offset,const llvm::sys::TimePoint<> & object_mod_time)23514f1b3e8SDimitry Andric Module::Module(const FileSpec &file_spec, const ArchSpec &arch,
236b1c73532SDimitry Andric ConstString object_name, lldb::offset_t object_offset,
23714f1b3e8SDimitry Andric const llvm::sys::TimePoint<> &object_mod_time)
23877fc4c14SDimitry Andric : m_mod_time(FileSystem::Instance().GetModificationTime(file_spec)),
239b1c73532SDimitry Andric m_arch(arch), m_file(file_spec), m_object_name(object_name),
240b1c73532SDimitry Andric m_object_offset(object_offset), m_object_mod_time(object_mod_time),
241b1c73532SDimitry Andric m_file_has_changed(false), m_first_file_changed_log(false) {
242f034231aSEd Maste // Scope for locker below...
243f034231aSEd Maste {
24414f1b3e8SDimitry Andric std::lock_guard<std::recursive_mutex> guard(
24514f1b3e8SDimitry Andric GetAllocationModuleCollectionMutex());
246f034231aSEd Maste GetModuleCollection().push_back(this);
247f034231aSEd Maste }
248f034231aSEd Maste
249145449b1SDimitry Andric Log *log(GetLog(LLDBLog::Object | LLDBLog::Modules));
250f3fbd1c0SDimitry Andric if (log != nullptr)
251ead24645SDimitry Andric LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')",
252ead24645SDimitry Andric static_cast<void *>(this), m_arch.GetArchitectureName(),
253ead24645SDimitry Andric m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(",
2541f917f69SDimitry Andric m_object_name.AsCString(""), m_object_name.IsEmpty() ? "" : ")");
255f034231aSEd Maste }
256f034231aSEd Maste
Module()257344a3780SDimitry Andric Module::Module() : m_file_has_changed(false), m_first_file_changed_log(false) {
25814f1b3e8SDimitry Andric std::lock_guard<std::recursive_mutex> guard(
25914f1b3e8SDimitry Andric GetAllocationModuleCollectionMutex());
2600cac4ca3SEd Maste GetModuleCollection().push_back(this);
2610cac4ca3SEd Maste }
2620cac4ca3SEd Maste
~Module()26314f1b3e8SDimitry Andric Module::~Module() {
264f73363f1SDimitry Andric // Lock our module down while we tear everything down to make sure we don't
265f73363f1SDimitry Andric // get any access to the module while it is being destroyed
266f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
267f034231aSEd Maste // Scope for locker below...
268f034231aSEd Maste {
26914f1b3e8SDimitry Andric std::lock_guard<std::recursive_mutex> guard(
27014f1b3e8SDimitry Andric GetAllocationModuleCollectionMutex());
271f034231aSEd Maste ModuleCollection &modules = GetModuleCollection();
272f034231aSEd Maste ModuleCollection::iterator end = modules.end();
273f034231aSEd Maste ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
274f034231aSEd Maste assert(pos != end);
275f034231aSEd Maste modules.erase(pos);
276f034231aSEd Maste }
277145449b1SDimitry Andric Log *log(GetLog(LLDBLog::Object | LLDBLog::Modules));
278f3fbd1c0SDimitry Andric if (log != nullptr)
279ead24645SDimitry Andric LLDB_LOGF(log, "%p Module::~Module((%s) '%s%s%s%s')",
28014f1b3e8SDimitry Andric static_cast<void *>(this), m_arch.GetArchitectureName(),
28114f1b3e8SDimitry Andric m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(",
2821f917f69SDimitry Andric m_object_name.AsCString(""), m_object_name.IsEmpty() ? "" : ")");
283f034231aSEd Maste // Release any auto pointers before we start tearing down our member
284f034231aSEd Maste // variables since the object file and symbol files might need to make
285f034231aSEd Maste // function calls back into this module object. The ordering is important
286f034231aSEd Maste // here because symbol files can require the module object file. So we tear
287f034231aSEd Maste // down the symbol file first, then the object file.
2885f29bb8aSDimitry Andric m_sections_up.reset();
2895f29bb8aSDimitry Andric m_symfile_up.reset();
290f034231aSEd Maste m_objfile_sp.reset();
291f034231aSEd Maste }
292f034231aSEd Maste
GetMemoryObjectFile(const lldb::ProcessSP & process_sp,lldb::addr_t header_addr,Status & error,size_t size_to_read)29314f1b3e8SDimitry Andric ObjectFile *Module::GetMemoryObjectFile(const lldb::ProcessSP &process_sp,
294b76161e4SDimitry Andric lldb::addr_t header_addr, Status &error,
29514f1b3e8SDimitry Andric size_t size_to_read) {
29614f1b3e8SDimitry Andric if (m_objfile_sp) {
297f034231aSEd Maste error.SetErrorString("object file already exists");
29814f1b3e8SDimitry Andric } else {
299f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
30014f1b3e8SDimitry Andric if (process_sp) {
301f034231aSEd Maste m_did_load_objfile = true;
302145449b1SDimitry Andric std::shared_ptr<DataBufferHeap> data_sp =
303145449b1SDimitry Andric std::make_shared<DataBufferHeap>(size_to_read, 0);
304b76161e4SDimitry Andric Status readmem_error;
30514f1b3e8SDimitry Andric const size_t bytes_read =
306145449b1SDimitry Andric process_sp->ReadMemory(header_addr, data_sp->GetBytes(),
307145449b1SDimitry Andric data_sp->GetByteSize(), readmem_error);
308cfca06d7SDimitry Andric if (bytes_read < size_to_read)
309145449b1SDimitry Andric data_sp->SetByteSize(bytes_read);
310145449b1SDimitry Andric if (data_sp->GetByteSize() > 0) {
31114f1b3e8SDimitry Andric m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp,
31214f1b3e8SDimitry Andric header_addr, data_sp);
31314f1b3e8SDimitry Andric if (m_objfile_sp) {
314f034231aSEd Maste StreamString s;
315f034231aSEd Maste s.Printf("0x%16.16" PRIx64, header_addr);
31614f1b3e8SDimitry Andric m_object_name.SetString(s.GetString());
317f034231aSEd Maste
31814f1b3e8SDimitry Andric // Once we get the object file, update our module with the object
319f73363f1SDimitry Andric // file's architecture since it might differ in vendor/os if some
320f73363f1SDimitry Andric // parts were unknown.
32194994d37SDimitry Andric m_arch = m_objfile_sp->GetArchitecture();
3225f29bb8aSDimitry Andric
3235f29bb8aSDimitry Andric // Augment the arch with the target's information in case
3245f29bb8aSDimitry Andric // we are unable to extract the os/environment from memory.
3255f29bb8aSDimitry Andric m_arch.MergeFrom(process_sp->GetTarget().GetArchitecture());
32614f1b3e8SDimitry Andric } else {
327f034231aSEd Maste error.SetErrorString("unable to find suitable object file plug-in");
328f034231aSEd Maste }
32914f1b3e8SDimitry Andric } else {
33014f1b3e8SDimitry Andric error.SetErrorStringWithFormat("unable to read header from memory: %s",
33114f1b3e8SDimitry Andric readmem_error.AsCString());
332f034231aSEd Maste }
33314f1b3e8SDimitry Andric } else {
334f034231aSEd Maste error.SetErrorString("invalid process");
335f034231aSEd Maste }
336f034231aSEd Maste }
337f034231aSEd Maste return m_objfile_sp.get();
338f034231aSEd Maste }
339f034231aSEd Maste
GetUUID()34014f1b3e8SDimitry Andric const lldb_private::UUID &Module::GetUUID() {
341f73363f1SDimitry Andric if (!m_did_set_uuid.load()) {
342f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
343f73363f1SDimitry Andric if (!m_did_set_uuid.load()) {
344f034231aSEd Maste ObjectFile *obj_file = GetObjectFile();
345f034231aSEd Maste
34614f1b3e8SDimitry Andric if (obj_file != nullptr) {
3475f29bb8aSDimitry Andric m_uuid = obj_file->GetUUID();
348f73363f1SDimitry Andric m_did_set_uuid = true;
349f034231aSEd Maste }
350f034231aSEd Maste }
351e81d9d49SDimitry Andric }
352f034231aSEd Maste return m_uuid;
353f034231aSEd Maste }
354f034231aSEd Maste
SetUUID(const lldb_private::UUID & uuid)355f73363f1SDimitry Andric void Module::SetUUID(const lldb_private::UUID &uuid) {
356f73363f1SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
357f73363f1SDimitry Andric if (!m_did_set_uuid) {
358f73363f1SDimitry Andric m_uuid = uuid;
359f73363f1SDimitry Andric m_did_set_uuid = true;
360f73363f1SDimitry Andric } else {
361f73363f1SDimitry Andric lldbassert(0 && "Attempting to overwrite the existing module UUID");
362f73363f1SDimitry Andric }
363f73363f1SDimitry Andric }
364f73363f1SDimitry Andric
365e3b55780SDimitry Andric llvm::Expected<TypeSystemSP>
GetTypeSystemForLanguage(LanguageType language)366ead24645SDimitry Andric Module::GetTypeSystemForLanguage(LanguageType language) {
367e81d9d49SDimitry Andric return m_type_system_map.GetTypeSystemForLanguage(language, this, true);
368f034231aSEd Maste }
369f034231aSEd Maste
ForEachTypeSystem(llvm::function_ref<bool (lldb::TypeSystemSP)> callback)370e3b55780SDimitry Andric void Module::ForEachTypeSystem(
371e3b55780SDimitry Andric llvm::function_ref<bool(lldb::TypeSystemSP)> callback) {
372e3b55780SDimitry Andric m_type_system_map.ForEach(callback);
373e3b55780SDimitry Andric }
374e3b55780SDimitry Andric
ParseAllDebugSymbols()37514f1b3e8SDimitry Andric void Module::ParseAllDebugSymbols() {
376f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
377f034231aSEd Maste size_t num_comp_units = GetNumCompileUnits();
378f034231aSEd Maste if (num_comp_units == 0)
379f034231aSEd Maste return;
380f034231aSEd Maste
381ead24645SDimitry Andric SymbolFile *symbols = GetSymbolFile();
382f034231aSEd Maste
38314f1b3e8SDimitry Andric for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++) {
384cfca06d7SDimitry Andric SymbolContext sc;
385cfca06d7SDimitry Andric sc.module_sp = shared_from_this();
386f034231aSEd Maste sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
38794994d37SDimitry Andric if (!sc.comp_unit)
38894994d37SDimitry Andric continue;
38994994d37SDimitry Andric
390f034231aSEd Maste symbols->ParseVariablesForContext(sc);
391f034231aSEd Maste
39294994d37SDimitry Andric symbols->ParseFunctions(*sc.comp_unit);
393f034231aSEd Maste
39494994d37SDimitry Andric sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) {
39594994d37SDimitry Andric symbols->ParseBlocksRecursive(*f);
396f034231aSEd Maste
397f034231aSEd Maste // Parse the variables for this function and all its blocks
39894994d37SDimitry Andric sc.function = f.get();
399f034231aSEd Maste symbols->ParseVariablesForContext(sc);
40094994d37SDimitry Andric return false;
40194994d37SDimitry Andric });
402f034231aSEd Maste
403f034231aSEd Maste // Parse all types for this compile unit
40494994d37SDimitry Andric symbols->ParseTypes(*sc.comp_unit);
405f034231aSEd Maste }
406f034231aSEd Maste }
407f034231aSEd Maste
CalculateSymbolContext(SymbolContext * sc)40814f1b3e8SDimitry Andric void Module::CalculateSymbolContext(SymbolContext *sc) {
409f034231aSEd Maste sc->module_sp = shared_from_this();
410f034231aSEd Maste }
411f034231aSEd Maste
CalculateSymbolContextModule()41214f1b3e8SDimitry Andric ModuleSP Module::CalculateSymbolContextModule() { return shared_from_this(); }
413f034231aSEd Maste
DumpSymbolContext(Stream * s)41414f1b3e8SDimitry Andric void Module::DumpSymbolContext(Stream *s) {
4150cac4ca3SEd Maste s->Printf(", Module{%p}", static_cast<void *>(this));
416f034231aSEd Maste }
417f034231aSEd Maste
GetNumCompileUnits()41814f1b3e8SDimitry Andric size_t Module::GetNumCompileUnits() {
419f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
420ead24645SDimitry Andric if (SymbolFile *symbols = GetSymbolFile())
421f034231aSEd Maste return symbols->GetNumCompileUnits();
422f034231aSEd Maste return 0;
423f034231aSEd Maste }
424f034231aSEd Maste
GetCompileUnitAtIndex(size_t index)42514f1b3e8SDimitry Andric CompUnitSP Module::GetCompileUnitAtIndex(size_t index) {
426f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
427f034231aSEd Maste size_t num_comp_units = GetNumCompileUnits();
428f034231aSEd Maste CompUnitSP cu_sp;
429f034231aSEd Maste
43014f1b3e8SDimitry Andric if (index < num_comp_units) {
431ead24645SDimitry Andric if (SymbolFile *symbols = GetSymbolFile())
432f034231aSEd Maste cu_sp = symbols->GetCompileUnitAtIndex(index);
433f034231aSEd Maste }
434f034231aSEd Maste return cu_sp;
435f034231aSEd Maste }
436f034231aSEd Maste
ResolveFileAddress(lldb::addr_t vm_addr,Address & so_addr)43714f1b3e8SDimitry Andric bool Module::ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr) {
438f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
439f034231aSEd Maste SectionList *section_list = GetSectionList();
440f034231aSEd Maste if (section_list)
441f034231aSEd Maste return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
442f034231aSEd Maste return false;
443f034231aSEd Maste }
444f034231aSEd Maste
ResolveSymbolContextForAddress(const Address & so_addr,lldb::SymbolContextItem resolve_scope,SymbolContext & sc,bool resolve_tail_call_address)44514f1b3e8SDimitry Andric uint32_t Module::ResolveSymbolContextForAddress(
44694994d37SDimitry Andric const Address &so_addr, lldb::SymbolContextItem resolve_scope,
44794994d37SDimitry Andric SymbolContext &sc, bool resolve_tail_call_address) {
448f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
449f034231aSEd Maste uint32_t resolved_flags = 0;
450f034231aSEd Maste
45114f1b3e8SDimitry Andric // Clear the result symbol context in case we don't find anything, but don't
45214f1b3e8SDimitry Andric // clear the target
453f034231aSEd Maste sc.Clear(false);
454f034231aSEd Maste
455f034231aSEd Maste // Get the section from the section/offset address.
456f034231aSEd Maste SectionSP section_sp(so_addr.GetSection());
457f034231aSEd Maste
458f034231aSEd Maste // Make sure the section matches this module before we try and match anything
45914f1b3e8SDimitry Andric if (section_sp && section_sp->GetModule().get() == this) {
460f73363f1SDimitry Andric // If the section offset based address resolved itself, then this is the
461f73363f1SDimitry Andric // right module.
462f034231aSEd Maste sc.module_sp = shared_from_this();
463f034231aSEd Maste resolved_flags |= eSymbolContextModule;
464f034231aSEd Maste
465ead24645SDimitry Andric SymbolFile *symfile = GetSymbolFile();
466ead24645SDimitry Andric if (!symfile)
467f21a844fSEd Maste return resolved_flags;
468f21a844fSEd Maste
469f73363f1SDimitry Andric // Resolve the compile unit, function, block, line table or line entry if
470f73363f1SDimitry Andric // requested.
471f034231aSEd Maste if (resolve_scope & eSymbolContextCompUnit ||
472f034231aSEd Maste resolve_scope & eSymbolContextFunction ||
473f034231aSEd Maste resolve_scope & eSymbolContextBlock ||
474f3fbd1c0SDimitry Andric resolve_scope & eSymbolContextLineEntry ||
47514f1b3e8SDimitry Andric resolve_scope & eSymbolContextVariable) {
476145449b1SDimitry Andric symfile->SetLoadDebugInfoEnabled();
47714f1b3e8SDimitry Andric resolved_flags |=
478ead24645SDimitry Andric symfile->ResolveSymbolContext(so_addr, resolve_scope, sc);
479f034231aSEd Maste }
480f034231aSEd Maste
481f73363f1SDimitry Andric // Resolve the symbol if requested, but don't re-look it up if we've
482f73363f1SDimitry Andric // already found it.
48314f1b3e8SDimitry Andric if (resolve_scope & eSymbolContextSymbol &&
48414f1b3e8SDimitry Andric !(resolved_flags & eSymbolContextSymbol)) {
485ead24645SDimitry Andric Symtab *symtab = symfile->GetSymtab();
48614f1b3e8SDimitry Andric if (symtab && so_addr.IsSectionOffset()) {
4872fc5d2d1SDimitry Andric Symbol *matching_symbol = nullptr;
4882fc5d2d1SDimitry Andric
48914f1b3e8SDimitry Andric symtab->ForEachSymbolContainingFileAddress(
49014f1b3e8SDimitry Andric so_addr.GetFileAddress(),
4912fc5d2d1SDimitry Andric [&matching_symbol](Symbol *symbol) -> bool {
49214f1b3e8SDimitry Andric if (symbol->GetType() != eSymbolTypeInvalid) {
4932fc5d2d1SDimitry Andric matching_symbol = symbol;
4942fc5d2d1SDimitry Andric return false; // Stop iterating
4952fc5d2d1SDimitry Andric }
4962fc5d2d1SDimitry Andric return true; // Keep iterating
4972fc5d2d1SDimitry Andric });
4982fc5d2d1SDimitry Andric sc.symbol = matching_symbol;
49914f1b3e8SDimitry Andric if (!sc.symbol && resolve_scope & eSymbolContextFunction &&
50014f1b3e8SDimitry Andric !(resolved_flags & eSymbolContextFunction)) {
50114f1b3e8SDimitry Andric bool verify_unique = false; // No need to check again since
50214f1b3e8SDimitry Andric // ResolveSymbolContext failed to find a
50314f1b3e8SDimitry Andric // symbol at this address.
504f21a844fSEd Maste if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
50514f1b3e8SDimitry Andric sc.symbol =
50614f1b3e8SDimitry Andric obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
507f21a844fSEd Maste }
508f21a844fSEd Maste
50914f1b3e8SDimitry Andric if (sc.symbol) {
51014f1b3e8SDimitry Andric if (sc.symbol->IsSynthetic()) {
511f73363f1SDimitry Andric // We have a synthetic symbol so lets check if the object file from
512f73363f1SDimitry Andric // the symbol file in the symbol vendor is different than the
513f73363f1SDimitry Andric // object file for the module, and if so search its symbol table to
514f73363f1SDimitry Andric // see if we can come up with a better symbol. For example dSYM
515f73363f1SDimitry Andric // files on MacOSX have an unstripped symbol table inside of them.
516f21a844fSEd Maste ObjectFile *symtab_objfile = symtab->GetObjectFile();
51714f1b3e8SDimitry Andric if (symtab_objfile && symtab_objfile->IsStripped()) {
518f21a844fSEd Maste ObjectFile *symfile_objfile = symfile->GetObjectFile();
51914f1b3e8SDimitry Andric if (symfile_objfile != symtab_objfile) {
520f21a844fSEd Maste Symtab *symfile_symtab = symfile_objfile->GetSymtab();
52114f1b3e8SDimitry Andric if (symfile_symtab) {
52214f1b3e8SDimitry Andric Symbol *symbol =
52314f1b3e8SDimitry Andric symfile_symtab->FindSymbolContainingFileAddress(
52414f1b3e8SDimitry Andric so_addr.GetFileAddress());
52514f1b3e8SDimitry Andric if (symbol && !symbol->IsSynthetic()) {
526f21a844fSEd Maste sc.symbol = symbol;
527f21a844fSEd Maste }
528f21a844fSEd Maste }
529f21a844fSEd Maste }
530f21a844fSEd Maste }
531f21a844fSEd Maste }
532f034231aSEd Maste resolved_flags |= eSymbolContextSymbol;
533f034231aSEd Maste }
534f034231aSEd Maste }
535f034231aSEd Maste }
536f21a844fSEd Maste
53714f1b3e8SDimitry Andric // For function symbols, so_addr may be off by one. This is a convention
538f73363f1SDimitry Andric // consistent with FDE row indices in eh_frame sections, but requires extra
539f73363f1SDimitry Andric // logic here to permit symbol lookup for disassembly and unwind.
54014f1b3e8SDimitry Andric if (resolve_scope & eSymbolContextSymbol &&
54114f1b3e8SDimitry Andric !(resolved_flags & eSymbolContextSymbol) && resolve_tail_call_address &&
54214f1b3e8SDimitry Andric so_addr.IsSectionOffset()) {
543f21a844fSEd Maste Address previous_addr = so_addr;
544f21a844fSEd Maste previous_addr.Slide(-1);
545f21a844fSEd Maste
546f21a844fSEd Maste bool do_resolve_tail_call_address = false; // prevent recursion
54714f1b3e8SDimitry Andric const uint32_t flags = ResolveSymbolContextForAddress(
54814f1b3e8SDimitry Andric previous_addr, resolve_scope, sc, do_resolve_tail_call_address);
54914f1b3e8SDimitry Andric if (flags & eSymbolContextSymbol) {
550f21a844fSEd Maste AddressRange addr_range;
55114f1b3e8SDimitry Andric if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
55214f1b3e8SDimitry Andric false, addr_range)) {
55314f1b3e8SDimitry Andric if (addr_range.GetBaseAddress().GetSection() ==
55414f1b3e8SDimitry Andric so_addr.GetSection()) {
55514f1b3e8SDimitry Andric // If the requested address is one past the address range of a
556f73363f1SDimitry Andric // function (i.e. a tail call), or the decremented address is the
557f73363f1SDimitry Andric // start of a function (i.e. some forms of trampoline), indicate
558f73363f1SDimitry Andric // that the symbol has been resolved.
55914f1b3e8SDimitry Andric if (so_addr.GetOffset() ==
56014f1b3e8SDimitry Andric addr_range.GetBaseAddress().GetOffset() ||
56177fc4c14SDimitry Andric so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() +
56214f1b3e8SDimitry Andric addr_range.GetByteSize()) {
563f21a844fSEd Maste resolved_flags |= flags;
564f21a844fSEd Maste }
56514f1b3e8SDimitry Andric } else {
56614f1b3e8SDimitry Andric sc.symbol =
56714f1b3e8SDimitry Andric nullptr; // Don't trust the symbol if the sections didn't match.
568f21a844fSEd Maste }
569f21a844fSEd Maste }
570f21a844fSEd Maste }
571f034231aSEd Maste }
572f034231aSEd Maste }
573f034231aSEd Maste return resolved_flags;
574f034231aSEd Maste }
575f034231aSEd Maste
ResolveSymbolContextForFilePath(const char * file_path,uint32_t line,bool check_inlines,lldb::SymbolContextItem resolve_scope,SymbolContextList & sc_list)57694994d37SDimitry Andric uint32_t Module::ResolveSymbolContextForFilePath(
57794994d37SDimitry Andric const char *file_path, uint32_t line, bool check_inlines,
57894994d37SDimitry Andric lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
57994994d37SDimitry Andric FileSpec file_spec(file_path);
58014f1b3e8SDimitry Andric return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
58114f1b3e8SDimitry Andric resolve_scope, sc_list);
582f034231aSEd Maste }
583f034231aSEd Maste
ResolveSymbolContextsForFileSpec(const FileSpec & file_spec,uint32_t line,bool check_inlines,lldb::SymbolContextItem resolve_scope,SymbolContextList & sc_list)58494994d37SDimitry Andric uint32_t Module::ResolveSymbolContextsForFileSpec(
58594994d37SDimitry Andric const FileSpec &file_spec, uint32_t line, bool check_inlines,
58694994d37SDimitry Andric lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
587f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
588b60736ecSDimitry Andric LLDB_SCOPED_TIMERF("Module::ResolveSymbolContextForFilePath (%s:%u, "
58914f1b3e8SDimitry Andric "check_inlines = %s, resolve_scope = 0x%8.8x)",
59014f1b3e8SDimitry Andric file_spec.GetPath().c_str(), line,
59114f1b3e8SDimitry Andric check_inlines ? "yes" : "no", resolve_scope);
592f034231aSEd Maste
593f034231aSEd Maste const uint32_t initial_count = sc_list.GetSize();
594f034231aSEd Maste
595344a3780SDimitry Andric if (SymbolFile *symbols = GetSymbolFile()) {
596344a3780SDimitry Andric // TODO: Handle SourceLocationSpec column information
597e3b55780SDimitry Andric SourceLocationSpec location_spec(file_spec, line, /*column=*/std::nullopt,
598344a3780SDimitry Andric check_inlines, /*exact_match=*/false);
599344a3780SDimitry Andric
600344a3780SDimitry Andric symbols->ResolveSymbolContext(location_spec, resolve_scope, sc_list);
601344a3780SDimitry Andric }
602f034231aSEd Maste
603f034231aSEd Maste return sc_list.GetSize() - initial_count;
604f034231aSEd Maste }
605f034231aSEd Maste
FindGlobalVariables(ConstString name,const CompilerDeclContext & parent_decl_ctx,size_t max_matches,VariableList & variables)606ead24645SDimitry Andric void Module::FindGlobalVariables(ConstString name,
607cfca06d7SDimitry Andric const CompilerDeclContext &parent_decl_ctx,
608ead24645SDimitry Andric size_t max_matches, VariableList &variables) {
609ead24645SDimitry Andric if (SymbolFile *symbols = GetSymbolFile())
610ead24645SDimitry Andric symbols->FindGlobalVariables(name, parent_decl_ctx, max_matches, variables);
611f73363f1SDimitry Andric }
612f73363f1SDimitry Andric
FindGlobalVariables(const RegularExpression & regex,size_t max_matches,VariableList & variables)613ead24645SDimitry Andric void Module::FindGlobalVariables(const RegularExpression ®ex,
614ead24645SDimitry Andric size_t max_matches, VariableList &variables) {
615ead24645SDimitry Andric SymbolFile *symbols = GetSymbolFile();
616f73363f1SDimitry Andric if (symbols)
617ead24645SDimitry Andric symbols->FindGlobalVariables(regex, max_matches, variables);
618f034231aSEd Maste }
619f034231aSEd Maste
FindCompileUnits(const FileSpec & path,SymbolContextList & sc_list)620ead24645SDimitry Andric void Module::FindCompileUnits(const FileSpec &path,
62114f1b3e8SDimitry Andric SymbolContextList &sc_list) {
622f034231aSEd Maste const size_t num_compile_units = GetNumCompileUnits();
623f034231aSEd Maste SymbolContext sc;
624f034231aSEd Maste sc.module_sp = shared_from_this();
62514f1b3e8SDimitry Andric for (size_t i = 0; i < num_compile_units; ++i) {
626f034231aSEd Maste sc.comp_unit = GetCompileUnitAtIndex(i).get();
62714f1b3e8SDimitry Andric if (sc.comp_unit) {
628706b4fc4SDimitry Andric if (FileSpec::Match(path, sc.comp_unit->GetPrimaryFile()))
629f034231aSEd Maste sc_list.Append(sc);
630f034231aSEd Maste }
631f034231aSEd Maste }
632f034231aSEd Maste }
633f034231aSEd Maste
LookupInfo(ConstString name,FunctionNameType name_type_mask,LanguageType language)6345f29bb8aSDimitry Andric Module::LookupInfo::LookupInfo(ConstString name,
63594994d37SDimitry Andric FunctionNameType name_type_mask,
63694994d37SDimitry Andric LanguageType language)
637145449b1SDimitry Andric : m_name(name), m_lookup_name(), m_language(language) {
638f3fbd1c0SDimitry Andric const char *name_cstr = name.GetCString();
639f3fbd1c0SDimitry Andric llvm::StringRef basename;
640f3fbd1c0SDimitry Andric llvm::StringRef context;
641f3fbd1c0SDimitry Andric
64214f1b3e8SDimitry Andric if (name_type_mask & eFunctionNameTypeAuto) {
643f3fbd1c0SDimitry Andric if (CPlusPlusLanguage::IsCPPMangledName(name_cstr))
644f3fbd1c0SDimitry Andric m_name_type_mask = eFunctionNameTypeFull;
645f3fbd1c0SDimitry Andric else if ((language == eLanguageTypeUnknown ||
646f3fbd1c0SDimitry Andric Language::LanguageIsObjC(language)) &&
647f3fbd1c0SDimitry Andric ObjCLanguage::IsPossibleObjCMethodName(name_cstr))
648f3fbd1c0SDimitry Andric m_name_type_mask = eFunctionNameTypeFull;
64914f1b3e8SDimitry Andric else if (Language::LanguageIsC(language)) {
650f3fbd1c0SDimitry Andric m_name_type_mask = eFunctionNameTypeFull;
65114f1b3e8SDimitry Andric } else {
652f3fbd1c0SDimitry Andric if ((language == eLanguageTypeUnknown ||
653f3fbd1c0SDimitry Andric Language::LanguageIsObjC(language)) &&
654f3fbd1c0SDimitry Andric ObjCLanguage::IsPossibleObjCSelector(name_cstr))
655f3fbd1c0SDimitry Andric m_name_type_mask |= eFunctionNameTypeSelector;
656f3fbd1c0SDimitry Andric
657f3fbd1c0SDimitry Andric CPlusPlusLanguage::MethodName cpp_method(name);
658f3fbd1c0SDimitry Andric basename = cpp_method.GetBasename();
65914f1b3e8SDimitry Andric if (basename.empty()) {
66014f1b3e8SDimitry Andric if (CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
66114f1b3e8SDimitry Andric basename))
662f3fbd1c0SDimitry Andric m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
663f3fbd1c0SDimitry Andric else
664f3fbd1c0SDimitry Andric m_name_type_mask |= eFunctionNameTypeFull;
66514f1b3e8SDimitry Andric } else {
666f3fbd1c0SDimitry Andric m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
667f3fbd1c0SDimitry Andric }
668f3fbd1c0SDimitry Andric }
66914f1b3e8SDimitry Andric } else {
670f3fbd1c0SDimitry Andric m_name_type_mask = name_type_mask;
67114f1b3e8SDimitry Andric if (name_type_mask & eFunctionNameTypeMethod ||
67214f1b3e8SDimitry Andric name_type_mask & eFunctionNameTypeBase) {
67314f1b3e8SDimitry Andric // If they've asked for a CPP method or function name and it can't be
674f73363f1SDimitry Andric // that, we don't even need to search for CPP methods or names.
675f3fbd1c0SDimitry Andric CPlusPlusLanguage::MethodName cpp_method(name);
67614f1b3e8SDimitry Andric if (cpp_method.IsValid()) {
677f3fbd1c0SDimitry Andric basename = cpp_method.GetBasename();
678f3fbd1c0SDimitry Andric
67914f1b3e8SDimitry Andric if (!cpp_method.GetQualifiers().empty()) {
68014f1b3e8SDimitry Andric // There is a "const" or other qualifier following the end of the
681f73363f1SDimitry Andric // function parens, this can't be a eFunctionNameTypeBase
682f3fbd1c0SDimitry Andric m_name_type_mask &= ~(eFunctionNameTypeBase);
683f3fbd1c0SDimitry Andric if (m_name_type_mask == eFunctionNameTypeNone)
684f3fbd1c0SDimitry Andric return;
685f3fbd1c0SDimitry Andric }
68614f1b3e8SDimitry Andric } else {
68714f1b3e8SDimitry Andric // If the CPP method parser didn't manage to chop this up, try to fill
688f73363f1SDimitry Andric // in the base name if we can. If a::b::c is passed in, we need to just
689f73363f1SDimitry Andric // look up "c", and then we'll filter the result later.
69014f1b3e8SDimitry Andric CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
69114f1b3e8SDimitry Andric basename);
692f3fbd1c0SDimitry Andric }
693f3fbd1c0SDimitry Andric }
694f3fbd1c0SDimitry Andric
69514f1b3e8SDimitry Andric if (name_type_mask & eFunctionNameTypeSelector) {
69614f1b3e8SDimitry Andric if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr)) {
697f3fbd1c0SDimitry Andric m_name_type_mask &= ~(eFunctionNameTypeSelector);
698f3fbd1c0SDimitry Andric if (m_name_type_mask == eFunctionNameTypeNone)
699f3fbd1c0SDimitry Andric return;
700f3fbd1c0SDimitry Andric }
701f3fbd1c0SDimitry Andric }
702f3fbd1c0SDimitry Andric
70314f1b3e8SDimitry Andric // Still try and get a basename in case someone specifies a name type mask
70474a628f7SDimitry Andric // of eFunctionNameTypeFull and a name like "A::func"
70514f1b3e8SDimitry Andric if (basename.empty()) {
70674a628f7SDimitry Andric if (name_type_mask & eFunctionNameTypeFull &&
70774a628f7SDimitry Andric !CPlusPlusLanguage::IsCPPMangledName(name_cstr)) {
708f3fbd1c0SDimitry Andric CPlusPlusLanguage::MethodName cpp_method(name);
709f3fbd1c0SDimitry Andric basename = cpp_method.GetBasename();
710f3fbd1c0SDimitry Andric if (basename.empty())
71114f1b3e8SDimitry Andric CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
71214f1b3e8SDimitry Andric basename);
713f3fbd1c0SDimitry Andric }
714f3fbd1c0SDimitry Andric }
715f3fbd1c0SDimitry Andric }
716f3fbd1c0SDimitry Andric
71714f1b3e8SDimitry Andric if (!basename.empty()) {
718f73363f1SDimitry Andric // The name supplied was a partial C++ path like "a::count". In this case
719f73363f1SDimitry Andric // we want to do a lookup on the basename "count" and then make sure any
720f73363f1SDimitry Andric // matching results contain "a::count" so that it would match "b::a::count"
721f73363f1SDimitry Andric // and "a::count". This is why we set "match_name_after_lookup" to true
722f3fbd1c0SDimitry Andric m_lookup_name.SetString(basename);
723f3fbd1c0SDimitry Andric m_match_name_after_lookup = true;
72414f1b3e8SDimitry Andric } else {
72514f1b3e8SDimitry Andric // The name is already correct, just use the exact name as supplied, and we
726f73363f1SDimitry Andric // won't need to check if any matches contain "name"
727f3fbd1c0SDimitry Andric m_lookup_name = name;
728f3fbd1c0SDimitry Andric m_match_name_after_lookup = false;
729f3fbd1c0SDimitry Andric }
730f3fbd1c0SDimitry Andric }
731f3fbd1c0SDimitry Andric
NameMatchesLookupInfo(ConstString function_name,LanguageType language_type) const732e3b55780SDimitry Andric bool Module::LookupInfo::NameMatchesLookupInfo(
733e3b55780SDimitry Andric ConstString function_name, LanguageType language_type) const {
734e3b55780SDimitry Andric // We always keep unnamed symbols
735e3b55780SDimitry Andric if (!function_name)
736e3b55780SDimitry Andric return true;
737e3b55780SDimitry Andric
738e3b55780SDimitry Andric // If we match exactly, we can return early
739e3b55780SDimitry Andric if (m_name == function_name)
740e3b55780SDimitry Andric return true;
741e3b55780SDimitry Andric
742e3b55780SDimitry Andric // If function_name is mangled, we'll need to demangle it.
743e3b55780SDimitry Andric // In the pathologial case where the function name "looks" mangled but is
744e3b55780SDimitry Andric // actually demangled (e.g. a method named _Zonk), this operation should be
745e3b55780SDimitry Andric // relatively inexpensive since no demangling is actually occuring. See
746e3b55780SDimitry Andric // Mangled::SetValue for more context.
747e3b55780SDimitry Andric const bool function_name_may_be_mangled =
7487fa27ce4SDimitry Andric Mangled::GetManglingScheme(function_name) != Mangled::eManglingSchemeNone;
749e3b55780SDimitry Andric ConstString demangled_function_name = function_name;
750e3b55780SDimitry Andric if (function_name_may_be_mangled) {
751e3b55780SDimitry Andric Mangled mangled_function_name(function_name);
752e3b55780SDimitry Andric demangled_function_name = mangled_function_name.GetDemangledName();
753e3b55780SDimitry Andric }
754e3b55780SDimitry Andric
755e3b55780SDimitry Andric // If the symbol has a language, then let the language make the match.
756e3b55780SDimitry Andric // Otherwise just check that the demangled function name contains the
757e3b55780SDimitry Andric // demangled user-provided name.
758e3b55780SDimitry Andric if (Language *language = Language::FindPlugin(language_type))
7597fa27ce4SDimitry Andric return language->DemangledNameContainsPath(m_name, demangled_function_name);
760e3b55780SDimitry Andric
7617fa27ce4SDimitry Andric llvm::StringRef function_name_ref = demangled_function_name;
7627fa27ce4SDimitry Andric return function_name_ref.contains(m_name);
763e3b55780SDimitry Andric }
764e3b55780SDimitry Andric
Prune(SymbolContextList & sc_list,size_t start_idx) const76514f1b3e8SDimitry Andric void Module::LookupInfo::Prune(SymbolContextList &sc_list,
76614f1b3e8SDimitry Andric size_t start_idx) const {
76714f1b3e8SDimitry Andric if (m_match_name_after_lookup && m_name) {
768f3fbd1c0SDimitry Andric SymbolContext sc;
769f3fbd1c0SDimitry Andric size_t i = start_idx;
77014f1b3e8SDimitry Andric while (i < sc_list.GetSize()) {
771f3fbd1c0SDimitry Andric if (!sc_list.GetContextAtIndex(i, sc))
772f3fbd1c0SDimitry Andric break;
773145449b1SDimitry Andric
774e3b55780SDimitry Andric bool keep_it =
775e3b55780SDimitry Andric NameMatchesLookupInfo(sc.GetFunctionName(), sc.GetLanguage());
776145449b1SDimitry Andric if (keep_it)
777145449b1SDimitry Andric ++i;
778145449b1SDimitry Andric else
779145449b1SDimitry Andric sc_list.RemoveContextAtIndex(i);
780f3fbd1c0SDimitry Andric }
781f3fbd1c0SDimitry Andric }
782f3fbd1c0SDimitry Andric
78314f1b3e8SDimitry Andric // If we have only full name matches we might have tried to set breakpoint on
78474a628f7SDimitry Andric // "func" and specified eFunctionNameTypeFull, but we might have found
78574a628f7SDimitry Andric // "a::func()", "a::b::func()", "c::func()", "func()" and "func". Only
78674a628f7SDimitry Andric // "func()" and "func" should end up matching.
78714f1b3e8SDimitry Andric if (m_name_type_mask == eFunctionNameTypeFull) {
788f3fbd1c0SDimitry Andric SymbolContext sc;
789f3fbd1c0SDimitry Andric size_t i = start_idx;
79014f1b3e8SDimitry Andric while (i < sc_list.GetSize()) {
791f3fbd1c0SDimitry Andric if (!sc_list.GetContextAtIndex(i, sc))
792f3fbd1c0SDimitry Andric break;
793f73363f1SDimitry Andric // Make sure the mangled and demangled names don't match before we try to
794f73363f1SDimitry Andric // pull anything out
79574a628f7SDimitry Andric ConstString mangled_name(sc.GetFunctionName(Mangled::ePreferMangled));
796f3fbd1c0SDimitry Andric ConstString full_name(sc.GetFunctionName());
79777fc4c14SDimitry Andric if (mangled_name != m_name && full_name != m_name) {
798f3fbd1c0SDimitry Andric CPlusPlusLanguage::MethodName cpp_method(full_name);
79914f1b3e8SDimitry Andric if (cpp_method.IsValid()) {
80014f1b3e8SDimitry Andric if (cpp_method.GetContext().empty()) {
8017fa27ce4SDimitry Andric if (cpp_method.GetBasename().compare(m_name) != 0) {
802f3fbd1c0SDimitry Andric sc_list.RemoveContextAtIndex(i);
803f3fbd1c0SDimitry Andric continue;
804f3fbd1c0SDimitry Andric }
80514f1b3e8SDimitry Andric } else {
80674a628f7SDimitry Andric std::string qualified_name;
80774a628f7SDimitry Andric llvm::StringRef anon_prefix("(anonymous namespace)");
80874a628f7SDimitry Andric if (cpp_method.GetContext() == anon_prefix)
80974a628f7SDimitry Andric qualified_name = cpp_method.GetBasename().str();
81074a628f7SDimitry Andric else
81174a628f7SDimitry Andric qualified_name = cpp_method.GetScopeQualifiedName();
81294994d37SDimitry Andric if (qualified_name != m_name.GetCString()) {
813f3fbd1c0SDimitry Andric sc_list.RemoveContextAtIndex(i);
814f3fbd1c0SDimitry Andric continue;
815f3fbd1c0SDimitry Andric }
816f3fbd1c0SDimitry Andric }
817f3fbd1c0SDimitry Andric }
81874a628f7SDimitry Andric }
819f3fbd1c0SDimitry Andric ++i;
820f3fbd1c0SDimitry Andric }
821f3fbd1c0SDimitry Andric }
822f3fbd1c0SDimitry Andric }
823f3fbd1c0SDimitry Andric
FindFunctions(const Module::LookupInfo & lookup_info,const CompilerDeclContext & parent_decl_ctx,const ModuleFunctionSearchOptions & options,SymbolContextList & sc_list)824e3b55780SDimitry Andric void Module::FindFunctions(const Module::LookupInfo &lookup_info,
825e3b55780SDimitry Andric const CompilerDeclContext &parent_decl_ctx,
826e3b55780SDimitry Andric const ModuleFunctionSearchOptions &options,
827e3b55780SDimitry Andric SymbolContextList &sc_list) {
828e3b55780SDimitry Andric // Find all the functions (not symbols, but debug information functions...
829e3b55780SDimitry Andric if (SymbolFile *symbols = GetSymbolFile()) {
830e3b55780SDimitry Andric symbols->FindFunctions(lookup_info, parent_decl_ctx,
831e3b55780SDimitry Andric options.include_inlines, sc_list);
832e3b55780SDimitry Andric // Now check our symbol table for symbols that are code symbols if
833e3b55780SDimitry Andric // requested
834e3b55780SDimitry Andric if (options.include_symbols) {
835e3b55780SDimitry Andric if (Symtab *symtab = symbols->GetSymtab()) {
836e3b55780SDimitry Andric symtab->FindFunctionSymbols(lookup_info.GetLookupName(),
837e3b55780SDimitry Andric lookup_info.GetNameTypeMask(), sc_list);
838e3b55780SDimitry Andric }
839e3b55780SDimitry Andric }
840e3b55780SDimitry Andric }
841e3b55780SDimitry Andric }
842e3b55780SDimitry Andric
FindFunctions(ConstString name,const CompilerDeclContext & parent_decl_ctx,FunctionNameType name_type_mask,const ModuleFunctionSearchOptions & options,SymbolContextList & sc_list)843ead24645SDimitry Andric void Module::FindFunctions(ConstString name,
844cfca06d7SDimitry Andric const CompilerDeclContext &parent_decl_ctx,
84594994d37SDimitry Andric FunctionNameType name_type_mask,
846c0981da4SDimitry Andric const ModuleFunctionSearchOptions &options,
847ead24645SDimitry Andric SymbolContextList &sc_list) {
848f034231aSEd Maste const size_t old_size = sc_list.GetSize();
849f3fbd1c0SDimitry Andric LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
850e3b55780SDimitry Andric FindFunctions(lookup_info, parent_decl_ctx, options, sc_list);
851e3b55780SDimitry Andric if (name_type_mask & eFunctionNameTypeAuto) {
852f3fbd1c0SDimitry Andric const size_t new_size = sc_list.GetSize();
853f3fbd1c0SDimitry Andric if (old_size < new_size)
854f3fbd1c0SDimitry Andric lookup_info.Prune(sc_list, old_size);
855f034231aSEd Maste }
856f034231aSEd Maste }
857f034231aSEd Maste
FindFunctions(llvm::ArrayRef<CompilerContext> compiler_ctx,FunctionNameType name_type_mask,const ModuleFunctionSearchOptions & options,SymbolContextList & sc_list)858950076cdSDimitry Andric void Module::FindFunctions(llvm::ArrayRef<CompilerContext> compiler_ctx,
859950076cdSDimitry Andric FunctionNameType name_type_mask,
860950076cdSDimitry Andric const ModuleFunctionSearchOptions &options,
861950076cdSDimitry Andric SymbolContextList &sc_list) {
862950076cdSDimitry Andric if (compiler_ctx.empty() ||
863950076cdSDimitry Andric compiler_ctx.back().kind != CompilerContextKind::Function)
864950076cdSDimitry Andric return;
865950076cdSDimitry Andric ConstString name = compiler_ctx.back().name;
866950076cdSDimitry Andric SymbolContextList unfiltered;
867950076cdSDimitry Andric FindFunctions(name, CompilerDeclContext(), name_type_mask, options,
868950076cdSDimitry Andric unfiltered);
869950076cdSDimitry Andric // Filter by context.
870950076cdSDimitry Andric for (auto &sc : unfiltered)
871950076cdSDimitry Andric if (sc.function && compiler_ctx.equals(sc.function->GetCompilerContext()))
872950076cdSDimitry Andric sc_list.Append(sc);
873950076cdSDimitry Andric }
874950076cdSDimitry Andric
FindFunctions(const RegularExpression & regex,const ModuleFunctionSearchOptions & options,SymbolContextList & sc_list)875c0981da4SDimitry Andric void Module::FindFunctions(const RegularExpression ®ex,
876c0981da4SDimitry Andric const ModuleFunctionSearchOptions &options,
877ead24645SDimitry Andric SymbolContextList &sc_list) {
878f034231aSEd Maste const size_t start_size = sc_list.GetSize();
879f034231aSEd Maste
880ead24645SDimitry Andric if (SymbolFile *symbols = GetSymbolFile()) {
881c0981da4SDimitry Andric symbols->FindFunctions(regex, options.include_inlines, sc_list);
882f034231aSEd Maste
883f73363f1SDimitry Andric // Now check our symbol table for symbols that are code symbols if
884f73363f1SDimitry Andric // requested
885c0981da4SDimitry Andric if (options.include_symbols) {
886f034231aSEd Maste Symtab *symtab = symbols->GetSymtab();
88714f1b3e8SDimitry Andric if (symtab) {
888f034231aSEd Maste std::vector<uint32_t> symbol_indexes;
88914f1b3e8SDimitry Andric symtab->AppendSymbolIndexesMatchingRegExAndType(
89014f1b3e8SDimitry Andric regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny,
89114f1b3e8SDimitry Andric symbol_indexes);
892f034231aSEd Maste const size_t num_matches = symbol_indexes.size();
89314f1b3e8SDimitry Andric if (num_matches) {
894f034231aSEd Maste SymbolContext sc(this);
895f034231aSEd Maste const size_t end_functions_added_index = sc_list.GetSize();
89614f1b3e8SDimitry Andric size_t num_functions_added_to_sc_list =
89714f1b3e8SDimitry Andric end_functions_added_index - start_size;
89814f1b3e8SDimitry Andric if (num_functions_added_to_sc_list == 0) {
899f73363f1SDimitry Andric // No functions were added, just symbols, so we can just append
900f73363f1SDimitry Andric // them
90114f1b3e8SDimitry Andric for (size_t i = 0; i < num_matches; ++i) {
902f034231aSEd Maste sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
903f034231aSEd Maste SymbolType sym_type = sc.symbol->GetType();
904f034231aSEd Maste if (sc.symbol && (sym_type == eSymbolTypeCode ||
905f034231aSEd Maste sym_type == eSymbolTypeResolver))
906f034231aSEd Maste sc_list.Append(sc);
907f034231aSEd Maste }
90814f1b3e8SDimitry Andric } else {
909f034231aSEd Maste typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
910f034231aSEd Maste FileAddrToIndexMap file_addr_to_index;
91114f1b3e8SDimitry Andric for (size_t i = start_size; i < end_functions_added_index; ++i) {
912f034231aSEd Maste const SymbolContext &sc = sc_list[i];
913f034231aSEd Maste if (sc.block)
914f034231aSEd Maste continue;
91514f1b3e8SDimitry Andric file_addr_to_index[sc.function->GetAddressRange()
91614f1b3e8SDimitry Andric .GetBaseAddress()
91714f1b3e8SDimitry Andric .GetFileAddress()] = i;
918f034231aSEd Maste }
919f034231aSEd Maste
920f034231aSEd Maste FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
921f034231aSEd Maste // Functions were added so we need to merge symbols into any
922f034231aSEd Maste // existing function symbol contexts
92314f1b3e8SDimitry Andric for (size_t i = start_size; i < num_matches; ++i) {
924f034231aSEd Maste sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
925f034231aSEd Maste SymbolType sym_type = sc.symbol->GetType();
92614f1b3e8SDimitry Andric if (sc.symbol && sc.symbol->ValueIsAddress() &&
92714f1b3e8SDimitry Andric (sym_type == eSymbolTypeCode ||
92814f1b3e8SDimitry Andric sym_type == eSymbolTypeResolver)) {
92914f1b3e8SDimitry Andric FileAddrToIndexMap::const_iterator pos =
93014f1b3e8SDimitry Andric file_addr_to_index.find(
93114f1b3e8SDimitry Andric sc.symbol->GetAddressRef().GetFileAddress());
932f034231aSEd Maste if (pos == end)
933f034231aSEd Maste sc_list.Append(sc);
934f034231aSEd Maste else
935f034231aSEd Maste sc_list[pos->second].symbol = sc.symbol;
936f034231aSEd Maste }
937f034231aSEd Maste }
938f034231aSEd Maste }
939f034231aSEd Maste }
940f034231aSEd Maste }
941f034231aSEd Maste }
942f034231aSEd Maste }
943f034231aSEd Maste }
944f034231aSEd Maste
FindAddressesForLine(const lldb::TargetSP target_sp,const FileSpec & file,uint32_t line,Function * function,std::vector<Address> & output_local,std::vector<Address> & output_extern)94514f1b3e8SDimitry Andric void Module::FindAddressesForLine(const lldb::TargetSP target_sp,
946f21a844fSEd Maste const FileSpec &file, uint32_t line,
947f21a844fSEd Maste Function *function,
94814f1b3e8SDimitry Andric std::vector<Address> &output_local,
94914f1b3e8SDimitry Andric std::vector<Address> &output_extern) {
950f21a844fSEd Maste SearchFilterByModule filter(target_sp, m_file);
951344a3780SDimitry Andric
952344a3780SDimitry Andric // TODO: Handle SourceLocationSpec column information
953e3b55780SDimitry Andric SourceLocationSpec location_spec(file, line, /*column=*/std::nullopt,
954344a3780SDimitry Andric /*check_inlines=*/true,
955344a3780SDimitry Andric /*exact_match=*/false);
956344a3780SDimitry Andric AddressResolverFileLine resolver(location_spec);
957f21a844fSEd Maste resolver.ResolveAddress(filter);
958f21a844fSEd Maste
95914f1b3e8SDimitry Andric for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++) {
960f21a844fSEd Maste Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
961f21a844fSEd Maste Function *f = addr.CalculateSymbolContextFunction();
962f21a844fSEd Maste if (f && f == function)
963f21a844fSEd Maste output_local.push_back(addr);
964f21a844fSEd Maste else
965f21a844fSEd Maste output_extern.push_back(addr);
966f21a844fSEd Maste }
967f21a844fSEd Maste }
968f21a844fSEd Maste
FindTypes(const TypeQuery & query,TypeResults & results)969312c0ed1SDimitry Andric void Module::FindTypes(const TypeQuery &query, TypeResults &results) {
970ead24645SDimitry Andric if (SymbolFile *symbols = GetSymbolFile())
971312c0ed1SDimitry Andric symbols->FindTypes(query, results);
972ead24645SDimitry Andric }
973ead24645SDimitry Andric
9747fa27ce4SDimitry Andric static Debugger::DebuggerList
DebuggersOwningModuleRequestingInterruption(Module & module)9757fa27ce4SDimitry Andric DebuggersOwningModuleRequestingInterruption(Module &module) {
9764df029ccSDimitry Andric Debugger::DebuggerList requestors =
9774df029ccSDimitry Andric Debugger::DebuggersRequestingInterruption();
9787fa27ce4SDimitry Andric Debugger::DebuggerList interruptors;
9797fa27ce4SDimitry Andric if (requestors.empty())
9807fa27ce4SDimitry Andric return interruptors;
9817fa27ce4SDimitry Andric
9827fa27ce4SDimitry Andric for (auto debugger_sp : requestors) {
9837fa27ce4SDimitry Andric if (!debugger_sp->InterruptRequested())
9847fa27ce4SDimitry Andric continue;
9857fa27ce4SDimitry Andric if (debugger_sp->GetTargetList()
9867fa27ce4SDimitry Andric .AnyTargetContainsModule(module))
9877fa27ce4SDimitry Andric interruptors.push_back(debugger_sp);
9887fa27ce4SDimitry Andric }
9897fa27ce4SDimitry Andric return interruptors;
9907fa27ce4SDimitry Andric }
9917fa27ce4SDimitry Andric
GetSymbolFile(bool can_create,Stream * feedback_strm)992ead24645SDimitry Andric SymbolFile *Module::GetSymbolFile(bool can_create, Stream *feedback_strm) {
993ead24645SDimitry Andric if (!m_did_load_symfile.load()) {
994f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
995ead24645SDimitry Andric if (!m_did_load_symfile.load() && can_create) {
9964df029ccSDimitry Andric Debugger::DebuggerList interruptors =
9974df029ccSDimitry Andric DebuggersOwningModuleRequestingInterruption(*this);
9987fa27ce4SDimitry Andric if (!interruptors.empty()) {
9997fa27ce4SDimitry Andric for (auto debugger_sp : interruptors) {
10007fa27ce4SDimitry Andric REPORT_INTERRUPTION(*(debugger_sp.get()),
10017fa27ce4SDimitry Andric "Interrupted fetching symbols for module {0}",
10027fa27ce4SDimitry Andric this->GetFileSpec());
10037fa27ce4SDimitry Andric }
10047fa27ce4SDimitry Andric return nullptr;
10057fa27ce4SDimitry Andric }
1006f034231aSEd Maste ObjectFile *obj_file = GetObjectFile();
100714f1b3e8SDimitry Andric if (obj_file != nullptr) {
1008b60736ecSDimitry Andric LLDB_SCOPED_TIMER();
10095f29bb8aSDimitry Andric m_symfile_up.reset(
101014f1b3e8SDimitry Andric SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
1011ead24645SDimitry Andric m_did_load_symfile = true;
1012ac9a064cSDimitry Andric if (m_unwind_table)
1013ac9a064cSDimitry Andric m_unwind_table->Update();
1014f034231aSEd Maste }
1015f034231aSEd Maste }
1016e81d9d49SDimitry Andric }
1017ead24645SDimitry Andric return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr;
1018ead24645SDimitry Andric }
1019ead24645SDimitry Andric
GetSymtab()1020ead24645SDimitry Andric Symtab *Module::GetSymtab() {
1021ead24645SDimitry Andric if (SymbolFile *symbols = GetSymbolFile())
1022ead24645SDimitry Andric return symbols->GetSymtab();
1023ead24645SDimitry Andric return nullptr;
1024f034231aSEd Maste }
1025f034231aSEd Maste
SetFileSpecAndObjectName(const FileSpec & file,ConstString object_name)102614f1b3e8SDimitry Andric void Module::SetFileSpecAndObjectName(const FileSpec &file,
10275f29bb8aSDimitry Andric ConstString object_name) {
1028f73363f1SDimitry Andric // Container objects whose paths do not specify a file directly can call this
1029f73363f1SDimitry Andric // function to correct the file and object names.
1030f034231aSEd Maste m_file = file;
103194994d37SDimitry Andric m_mod_time = FileSystem::Instance().GetModificationTime(file);
1032f034231aSEd Maste m_object_name = object_name;
1033f034231aSEd Maste }
1034f034231aSEd Maste
GetArchitecture() const103514f1b3e8SDimitry Andric const ArchSpec &Module::GetArchitecture() const { return m_arch; }
1036f034231aSEd Maste
GetSpecificationDescription() const103714f1b3e8SDimitry Andric std::string Module::GetSpecificationDescription() const {
1038f034231aSEd Maste std::string spec(GetFileSpec().GetPath());
103914f1b3e8SDimitry Andric if (m_object_name) {
1040f034231aSEd Maste spec += '(';
1041f034231aSEd Maste spec += m_object_name.GetCString();
1042f034231aSEd Maste spec += ')';
1043f034231aSEd Maste }
1044f034231aSEd Maste return spec;
1045f034231aSEd Maste }
1046f034231aSEd Maste
GetDescription(llvm::raw_ostream & s,lldb::DescriptionLevel level)1047706b4fc4SDimitry Andric void Module::GetDescription(llvm::raw_ostream &s,
1048706b4fc4SDimitry Andric lldb::DescriptionLevel level) {
104914f1b3e8SDimitry Andric if (level >= eDescriptionLevelFull) {
1050f034231aSEd Maste if (m_arch.IsValid())
1051706b4fc4SDimitry Andric s << llvm::formatv("({0}) ", m_arch.GetArchitectureName());
1052f034231aSEd Maste }
1053f034231aSEd Maste
105414f1b3e8SDimitry Andric if (level == eDescriptionLevelBrief) {
1055f034231aSEd Maste const char *filename = m_file.GetFilename().GetCString();
1056f034231aSEd Maste if (filename)
1057706b4fc4SDimitry Andric s << filename;
105814f1b3e8SDimitry Andric } else {
1059f034231aSEd Maste char path[PATH_MAX];
1060f034231aSEd Maste if (m_file.GetPath(path, sizeof(path)))
1061706b4fc4SDimitry Andric s << path;
1062f034231aSEd Maste }
1063f034231aSEd Maste
1064f034231aSEd Maste const char *object_name = m_object_name.GetCString();
1065f034231aSEd Maste if (object_name)
1066706b4fc4SDimitry Andric s << llvm::formatv("({0})", object_name);
1067f034231aSEd Maste }
1068f034231aSEd Maste
FileHasChanged() const106914f1b3e8SDimitry Andric bool Module::FileHasChanged() const {
1070cfca06d7SDimitry Andric // We have provided the DataBuffer for this module to avoid accessing the
1071cfca06d7SDimitry Andric // filesystem. We never want to reload those files.
1072cfca06d7SDimitry Andric if (m_data_sp)
1073cfca06d7SDimitry Andric return false;
1074f3fbd1c0SDimitry Andric if (!m_file_has_changed)
107514f1b3e8SDimitry Andric m_file_has_changed =
107694994d37SDimitry Andric (FileSystem::Instance().GetModificationTime(m_file) != m_mod_time);
1077f034231aSEd Maste return m_file_has_changed;
1078f034231aSEd Maste }
1079f034231aSEd Maste
ReportWarningOptimization(std::optional<lldb::user_id_t> debugger_id)1080145449b1SDimitry Andric void Module::ReportWarningOptimization(
1081e3b55780SDimitry Andric std::optional<lldb::user_id_t> debugger_id) {
1082145449b1SDimitry Andric ConstString file_name = GetFileSpec().GetFilename();
1083145449b1SDimitry Andric if (file_name.IsEmpty())
1084145449b1SDimitry Andric return;
1085145449b1SDimitry Andric
1086145449b1SDimitry Andric StreamString ss;
10877fa27ce4SDimitry Andric ss << file_name
1088145449b1SDimitry Andric << " was compiled with optimization - stepping may behave "
1089145449b1SDimitry Andric "oddly; variables may not be available.";
1090145449b1SDimitry Andric Debugger::ReportWarning(std::string(ss.GetString()), debugger_id,
1091145449b1SDimitry Andric &m_optimization_warning);
1092145449b1SDimitry Andric }
1093145449b1SDimitry Andric
ReportWarningUnsupportedLanguage(LanguageType language,std::optional<lldb::user_id_t> debugger_id)1094145449b1SDimitry Andric void Module::ReportWarningUnsupportedLanguage(
1095e3b55780SDimitry Andric LanguageType language, std::optional<lldb::user_id_t> debugger_id) {
1096145449b1SDimitry Andric StreamString ss;
1097145449b1SDimitry Andric ss << "This version of LLDB has no plugin for the language \""
1098145449b1SDimitry Andric << Language::GetNameForLanguageType(language)
1099145449b1SDimitry Andric << "\". "
1100145449b1SDimitry Andric "Inspection of frame variables will be limited.";
1101145449b1SDimitry Andric Debugger::ReportWarning(std::string(ss.GetString()), debugger_id,
1102145449b1SDimitry Andric &m_language_warning);
1103145449b1SDimitry Andric }
1104145449b1SDimitry Andric
ReportErrorIfModifyDetected(const llvm::formatv_object_base & payload)1105e3b55780SDimitry Andric void Module::ReportErrorIfModifyDetected(
1106e3b55780SDimitry Andric const llvm::formatv_object_base &payload) {
110714f1b3e8SDimitry Andric if (!m_first_file_changed_log) {
110814f1b3e8SDimitry Andric if (FileHasChanged()) {
1109f034231aSEd Maste m_first_file_changed_log = true;
1110f034231aSEd Maste StreamString strm;
1111145449b1SDimitry Andric strm.PutCString("the object file ");
1112706b4fc4SDimitry Andric GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull);
1113f034231aSEd Maste strm.PutCString(" has been modified\n");
1114e3b55780SDimitry Andric strm.PutCString(payload.str());
111514f1b3e8SDimitry Andric strm.PutCString("The debug session should be aborted as the original "
1116145449b1SDimitry Andric "debug information has been overwritten.");
1117145449b1SDimitry Andric Debugger::ReportError(std::string(strm.GetString()));
1118f034231aSEd Maste }
1119f034231aSEd Maste }
1120f034231aSEd Maste }
1121f034231aSEd Maste
ReportError(const llvm::formatv_object_base & payload)1122e3b55780SDimitry Andric void Module::ReportError(const llvm::formatv_object_base &payload) {
1123145449b1SDimitry Andric StreamString strm;
1124145449b1SDimitry Andric GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelBrief);
1125145449b1SDimitry Andric strm.PutChar(' ');
1126e3b55780SDimitry Andric strm.PutCString(payload.str());
1127e3b55780SDimitry Andric Debugger::ReportError(strm.GetString().str());
1128145449b1SDimitry Andric }
1129145449b1SDimitry Andric
ReportWarning(const llvm::formatv_object_base & payload)1130e3b55780SDimitry Andric void Module::ReportWarning(const llvm::formatv_object_base &payload) {
1131f034231aSEd Maste StreamString strm;
1132706b4fc4SDimitry Andric GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull);
1133f034231aSEd Maste strm.PutChar(' ');
1134e3b55780SDimitry Andric strm.PutCString(payload.str());
1135145449b1SDimitry Andric Debugger::ReportWarning(std::string(strm.GetString()));
1136f034231aSEd Maste }
1137f034231aSEd Maste
LogMessage(Log * log,const llvm::formatv_object_base & payload)1138e3b55780SDimitry Andric void Module::LogMessage(Log *log, const llvm::formatv_object_base &payload) {
1139f034231aSEd Maste StreamString log_message;
1140706b4fc4SDimitry Andric GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull);
1141f034231aSEd Maste log_message.PutCString(": ");
1142e3b55780SDimitry Andric log_message.PutCString(payload.str());
114314f1b3e8SDimitry Andric log->PutCString(log_message.GetData());
1144f034231aSEd Maste }
1145f034231aSEd Maste
LogMessageVerboseBacktrace(Log * log,const llvm::formatv_object_base & payload)1146e3b55780SDimitry Andric void Module::LogMessageVerboseBacktrace(
1147e3b55780SDimitry Andric Log *log, const llvm::formatv_object_base &payload) {
1148f034231aSEd Maste StreamString log_message;
1149706b4fc4SDimitry Andric GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull);
1150f034231aSEd Maste log_message.PutCString(": ");
1151e3b55780SDimitry Andric log_message.PutCString(payload.str());
115214f1b3e8SDimitry Andric if (log->GetVerbose()) {
11535e95aa85SEd Maste std::string back_trace;
11545e95aa85SEd Maste llvm::raw_string_ostream stream(back_trace);
11555e95aa85SEd Maste llvm::sys::PrintStackTrace(stream);
115614f1b3e8SDimitry Andric log_message.PutCString(back_trace);
11575e95aa85SEd Maste }
115814f1b3e8SDimitry Andric log->PutCString(log_message.GetData());
1159f034231aSEd Maste }
1160f034231aSEd Maste
Dump(Stream * s)116114f1b3e8SDimitry Andric void Module::Dump(Stream *s) {
1162f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
1163f034231aSEd Maste // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1164f034231aSEd Maste s->Indent();
116514f1b3e8SDimitry Andric s->Printf("Module %s%s%s%s\n", m_file.GetPath().c_str(),
1166f034231aSEd Maste m_object_name ? "(" : "",
1167f034231aSEd Maste m_object_name ? m_object_name.GetCString() : "",
1168f034231aSEd Maste m_object_name ? ")" : "");
1169f034231aSEd Maste
1170f034231aSEd Maste s->IndentMore();
1171f034231aSEd Maste
1172f034231aSEd Maste ObjectFile *objfile = GetObjectFile();
1173f034231aSEd Maste if (objfile)
1174f034231aSEd Maste objfile->Dump(s);
1175f034231aSEd Maste
1176ead24645SDimitry Andric if (SymbolFile *symbols = GetSymbolFile())
1177ead24645SDimitry Andric symbols->Dump(*s);
1178f034231aSEd Maste
1179f034231aSEd Maste s->IndentLess();
1180f034231aSEd Maste }
1181f034231aSEd Maste
GetObjectName() const11825f29bb8aSDimitry Andric ConstString Module::GetObjectName() const { return m_object_name; }
1183f034231aSEd Maste
GetObjectFile()118414f1b3e8SDimitry Andric ObjectFile *Module::GetObjectFile() {
118514f1b3e8SDimitry Andric if (!m_did_load_objfile.load()) {
1186f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
118714f1b3e8SDimitry Andric if (!m_did_load_objfile.load()) {
1188b60736ecSDimitry Andric LLDB_SCOPED_TIMERF("Module::GetObjectFile () module = %s",
118914f1b3e8SDimitry Andric GetFileSpec().GetFilename().AsCString(""));
1190f034231aSEd Maste lldb::offset_t data_offset = 0;
1191cfca06d7SDimitry Andric lldb::offset_t file_size = 0;
1192cfca06d7SDimitry Andric
1193cfca06d7SDimitry Andric if (m_data_sp)
1194cfca06d7SDimitry Andric file_size = m_data_sp->GetByteSize();
1195cfca06d7SDimitry Andric else if (m_file)
1196cfca06d7SDimitry Andric file_size = FileSystem::Instance().GetByteSize(m_file);
1197cfca06d7SDimitry Andric
119814f1b3e8SDimitry Andric if (file_size > m_object_offset) {
1199f034231aSEd Maste m_did_load_objfile = true;
1200cfca06d7SDimitry Andric // FindPlugin will modify its data_sp argument. Do not let it
1201cfca06d7SDimitry Andric // modify our m_data_sp member.
1202cfca06d7SDimitry Andric auto data_sp = m_data_sp;
120314f1b3e8SDimitry Andric m_objfile_sp = ObjectFile::FindPlugin(
120414f1b3e8SDimitry Andric shared_from_this(), &m_file, m_object_offset,
120514f1b3e8SDimitry Andric file_size - m_object_offset, data_sp, data_offset);
120614f1b3e8SDimitry Andric if (m_objfile_sp) {
120714f1b3e8SDimitry Andric // Once we get the object file, update our module with the object
1208f73363f1SDimitry Andric // file's architecture since it might differ in vendor/os if some
1209f73363f1SDimitry Andric // parts were unknown. But since the matching arch might already be
1210f73363f1SDimitry Andric // more specific than the generic COFF architecture, only merge in
1211f73363f1SDimitry Andric // those values that overwrite unspecified unknown values.
121294994d37SDimitry Andric m_arch.MergeFrom(m_objfile_sp->GetArchitecture());
121314f1b3e8SDimitry Andric } else {
1214b1c73532SDimitry Andric ReportError("failed to load objfile for {0}\nDebugging will be "
1215b1c73532SDimitry Andric "degraded for this module.",
121614f1b3e8SDimitry Andric GetFileSpec().GetPath().c_str());
1217205afe67SEd Maste }
1218f034231aSEd Maste }
1219f034231aSEd Maste }
1220e81d9d49SDimitry Andric }
1221f034231aSEd Maste return m_objfile_sp.get();
1222f034231aSEd Maste }
1223f034231aSEd Maste
GetSectionList()122414f1b3e8SDimitry Andric SectionList *Module::GetSectionList() {
12255f29bb8aSDimitry Andric // Populate m_sections_up with sections from objfile.
12265f29bb8aSDimitry Andric if (!m_sections_up) {
1227f034231aSEd Maste ObjectFile *obj_file = GetObjectFile();
1228f3fbd1c0SDimitry Andric if (obj_file != nullptr)
1229f034231aSEd Maste obj_file->CreateSections(*GetUnifiedSectionList());
1230f034231aSEd Maste }
12315f29bb8aSDimitry Andric return m_sections_up.get();
1232f034231aSEd Maste }
1233f034231aSEd Maste
SectionFileAddressesChanged()123414f1b3e8SDimitry Andric void Module::SectionFileAddressesChanged() {
12350cac4ca3SEd Maste ObjectFile *obj_file = GetObjectFile();
12360cac4ca3SEd Maste if (obj_file)
12370cac4ca3SEd Maste obj_file->SectionFileAddressesChanged();
1238ead24645SDimitry Andric if (SymbolFile *symbols = GetSymbolFile())
1239ead24645SDimitry Andric symbols->SectionFileAddressesChanged();
12400cac4ca3SEd Maste }
12410cac4ca3SEd Maste
GetUnwindTable()12425f29bb8aSDimitry Andric UnwindTable &Module::GetUnwindTable() {
1243e3b55780SDimitry Andric if (!m_unwind_table) {
1244e3b55780SDimitry Andric if (!m_symfile_spec)
1245b1c73532SDimitry Andric SymbolLocator::DownloadSymbolFileAsync(GetUUID());
1246ac9a064cSDimitry Andric m_unwind_table.emplace(*this);
1247e3b55780SDimitry Andric }
12485f29bb8aSDimitry Andric return *m_unwind_table;
1249f034231aSEd Maste }
1250f034231aSEd Maste
GetUnifiedSectionList()12515f29bb8aSDimitry Andric SectionList *Module::GetUnifiedSectionList() {
12525f29bb8aSDimitry Andric if (!m_sections_up)
1253ead24645SDimitry Andric m_sections_up = std::make_unique<SectionList>();
12545f29bb8aSDimitry Andric return m_sections_up.get();
12555f29bb8aSDimitry Andric }
12565f29bb8aSDimitry Andric
FindFirstSymbolWithNameAndType(ConstString name,SymbolType symbol_type)12575f29bb8aSDimitry Andric const Symbol *Module::FindFirstSymbolWithNameAndType(ConstString name,
125814f1b3e8SDimitry Andric SymbolType symbol_type) {
1259b60736ecSDimitry Andric LLDB_SCOPED_TIMERF(
1260b60736ecSDimitry Andric "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
126114f1b3e8SDimitry Andric name.AsCString(), symbol_type);
1262ead24645SDimitry Andric if (Symtab *symtab = GetSymtab())
126314f1b3e8SDimitry Andric return symtab->FindFirstSymbolWithNameAndType(
126414f1b3e8SDimitry Andric name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
1265f3fbd1c0SDimitry Andric return nullptr;
1266f034231aSEd Maste }
SymbolIndicesToSymbolContextList(Symtab * symtab,std::vector<uint32_t> & symbol_indexes,SymbolContextList & sc_list)126714f1b3e8SDimitry Andric void Module::SymbolIndicesToSymbolContextList(
126814f1b3e8SDimitry Andric Symtab *symtab, std::vector<uint32_t> &symbol_indexes,
126914f1b3e8SDimitry Andric SymbolContextList &sc_list) {
1270f034231aSEd Maste // No need to protect this call using m_mutex all other method calls are
1271f034231aSEd Maste // already thread safe.
1272f034231aSEd Maste
1273f034231aSEd Maste size_t num_indices = symbol_indexes.size();
127414f1b3e8SDimitry Andric if (num_indices > 0) {
1275f034231aSEd Maste SymbolContext sc;
1276f034231aSEd Maste CalculateSymbolContext(&sc);
127714f1b3e8SDimitry Andric for (size_t i = 0; i < num_indices; i++) {
1278f034231aSEd Maste sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
1279f034231aSEd Maste if (sc.symbol)
1280f034231aSEd Maste sc_list.Append(sc);
1281f034231aSEd Maste }
1282f034231aSEd Maste }
1283f034231aSEd Maste }
1284f034231aSEd Maste
FindFunctionSymbols(ConstString name,uint32_t name_type_mask,SymbolContextList & sc_list)128577fc4c14SDimitry Andric void Module::FindFunctionSymbols(ConstString name, uint32_t name_type_mask,
128614f1b3e8SDimitry Andric SymbolContextList &sc_list) {
1287b60736ecSDimitry Andric LLDB_SCOPED_TIMERF("Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
128814f1b3e8SDimitry Andric name.AsCString(), name_type_mask);
1289ead24645SDimitry Andric if (Symtab *symtab = GetSymtab())
1290ead24645SDimitry Andric symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
1291f034231aSEd Maste }
1292f034231aSEd Maste
FindSymbolsWithNameAndType(ConstString name,SymbolType symbol_type,SymbolContextList & sc_list)1293ead24645SDimitry Andric void Module::FindSymbolsWithNameAndType(ConstString name,
129414f1b3e8SDimitry Andric SymbolType symbol_type,
129514f1b3e8SDimitry Andric SymbolContextList &sc_list) {
1296f034231aSEd Maste // No need to protect this call using m_mutex all other method calls are
1297f034231aSEd Maste // already thread safe.
1298ead24645SDimitry Andric if (Symtab *symtab = GetSymtab()) {
1299f034231aSEd Maste std::vector<uint32_t> symbol_indexes;
1300f034231aSEd Maste symtab->FindAllSymbolsWithNameAndType(name, symbol_type, symbol_indexes);
1301f034231aSEd Maste SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
1302f034231aSEd Maste }
1303f034231aSEd Maste }
1304f034231aSEd Maste
FindSymbolsMatchingRegExAndType(const RegularExpression & regex,SymbolType symbol_type,SymbolContextList & sc_list,Mangled::NamePreference mangling_preference)1305e3b55780SDimitry Andric void Module::FindSymbolsMatchingRegExAndType(
1306e3b55780SDimitry Andric const RegularExpression ®ex, SymbolType symbol_type,
1307e3b55780SDimitry Andric SymbolContextList &sc_list, Mangled::NamePreference mangling_preference) {
1308f034231aSEd Maste // No need to protect this call using m_mutex all other method calls are
1309f034231aSEd Maste // already thread safe.
1310b60736ecSDimitry Andric LLDB_SCOPED_TIMERF(
1311f034231aSEd Maste "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
131214f1b3e8SDimitry Andric regex.GetText().str().c_str(), symbol_type);
1313ead24645SDimitry Andric if (Symtab *symtab = GetSymtab()) {
1314f034231aSEd Maste std::vector<uint32_t> symbol_indexes;
131514f1b3e8SDimitry Andric symtab->FindAllSymbolsMatchingRexExAndType(
131614f1b3e8SDimitry Andric regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny,
1317e3b55780SDimitry Andric symbol_indexes, mangling_preference);
1318f034231aSEd Maste SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
1319f034231aSEd Maste }
1320f034231aSEd Maste }
1321f034231aSEd Maste
PreloadSymbols()1322773dd0e6SDimitry Andric void Module::PreloadSymbols() {
1323773dd0e6SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
1324ead24645SDimitry Andric SymbolFile *sym_file = GetSymbolFile();
1325ead24645SDimitry Andric if (!sym_file)
1326773dd0e6SDimitry Andric return;
1327ead24645SDimitry Andric
1328f65dcba8SDimitry Andric // Load the object file symbol table and any symbols from the SymbolFile that
1329f65dcba8SDimitry Andric // get appended using SymbolFile::AddSymbols(...).
1330ead24645SDimitry Andric if (Symtab *symtab = sym_file->GetSymtab())
1331773dd0e6SDimitry Andric symtab->PreloadSymbols();
1332f65dcba8SDimitry Andric
1333f65dcba8SDimitry Andric // Now let the symbol file preload its data and the symbol table will be
1334f65dcba8SDimitry Andric // available without needing to take the module lock.
1335f65dcba8SDimitry Andric sym_file->PreloadSymbols();
1336773dd0e6SDimitry Andric }
1337773dd0e6SDimitry Andric
SetSymbolFileFileSpec(const FileSpec & file)133814f1b3e8SDimitry Andric void Module::SetSymbolFileFileSpec(const FileSpec &file) {
133994994d37SDimitry Andric if (!FileSystem::Instance().Exists(file))
13405e95aa85SEd Maste return;
13415f29bb8aSDimitry Andric if (m_symfile_up) {
134214f1b3e8SDimitry Andric // Remove any sections in the unified section list that come from the
134314f1b3e8SDimitry Andric // current symbol vendor.
1344f034231aSEd Maste SectionList *section_list = GetSectionList();
1345ead24645SDimitry Andric SymbolFile *symbol_file = GetSymbolFile();
134614f1b3e8SDimitry Andric if (section_list && symbol_file) {
1347f034231aSEd Maste ObjectFile *obj_file = symbol_file->GetObjectFile();
134814f1b3e8SDimitry Andric // Make sure we have an object file and that the symbol vendor's objfile
1349f73363f1SDimitry Andric // isn't the same as the module's objfile before we remove any sections
1350f73363f1SDimitry Andric // for it...
135114f1b3e8SDimitry Andric if (obj_file) {
135214f1b3e8SDimitry Andric // Check to make sure we aren't trying to specify the file we already
135314f1b3e8SDimitry Andric // have
135414f1b3e8SDimitry Andric if (obj_file->GetFileSpec() == file) {
13555e95aa85SEd Maste // We are being told to add the exact same file that we already have
13565e95aa85SEd Maste // we don't have to do anything.
13575e95aa85SEd Maste return;
13585e95aa85SEd Maste }
13595e95aa85SEd Maste
136014f1b3e8SDimitry Andric // Cleare the current symtab as we are going to replace it with a new
136114f1b3e8SDimitry Andric // one
1362e81d9d49SDimitry Andric obj_file->ClearSymtab();
1363e81d9d49SDimitry Andric
136414f1b3e8SDimitry Andric // The symbol file might be a directory bundle ("/tmp/a.out.dSYM")
1365f73363f1SDimitry Andric // instead of a full path to the symbol file within the bundle
136614f1b3e8SDimitry Andric // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to
136714f1b3e8SDimitry Andric // check this
136894994d37SDimitry Andric if (FileSystem::Instance().IsDirectory(file)) {
13695e95aa85SEd Maste std::string new_path(file.GetPath());
13705e95aa85SEd Maste std::string old_path(obj_file->GetFileSpec().GetPath());
1371312c0ed1SDimitry Andric if (llvm::StringRef(old_path).starts_with(new_path)) {
137214f1b3e8SDimitry Andric // We specified the same bundle as the symbol file that we already
137314f1b3e8SDimitry Andric // have
13745e95aa85SEd Maste return;
13755e95aa85SEd Maste }
13765e95aa85SEd Maste }
13775e95aa85SEd Maste
137814f1b3e8SDimitry Andric if (obj_file != m_objfile_sp.get()) {
1379f034231aSEd Maste size_t num_sections = section_list->GetNumSections(0);
138014f1b3e8SDimitry Andric for (size_t idx = num_sections; idx > 0; --idx) {
138114f1b3e8SDimitry Andric lldb::SectionSP section_sp(
138214f1b3e8SDimitry Andric section_list->GetSectionAtIndex(idx - 1));
138314f1b3e8SDimitry Andric if (section_sp->GetObjectFile() == obj_file) {
1384f034231aSEd Maste section_list->DeleteSection(idx - 1);
1385f034231aSEd Maste }
1386f034231aSEd Maste }
1387f034231aSEd Maste }
1388f034231aSEd Maste }
1389f034231aSEd Maste }
139014f1b3e8SDimitry Andric // Keep all old symbol files around in case there are any lingering type
1391f73363f1SDimitry Andric // references in any SBValue objects that might have been handed out.
13925f29bb8aSDimitry Andric m_old_symfiles.push_back(std::move(m_symfile_up));
13935e95aa85SEd Maste }
1394f034231aSEd Maste m_symfile_spec = file;
13955f29bb8aSDimitry Andric m_symfile_up.reset();
1396ead24645SDimitry Andric m_did_load_symfile = false;
1397f034231aSEd Maste }
1398f034231aSEd Maste
IsExecutable()139914f1b3e8SDimitry Andric bool Module::IsExecutable() {
1400f3fbd1c0SDimitry Andric if (GetObjectFile() == nullptr)
1401f034231aSEd Maste return false;
1402f034231aSEd Maste else
1403f034231aSEd Maste return GetObjectFile()->IsExecutable();
1404f034231aSEd Maste }
1405f034231aSEd Maste
IsLoadedInTarget(Target * target)140614f1b3e8SDimitry Andric bool Module::IsLoadedInTarget(Target *target) {
1407f034231aSEd Maste ObjectFile *obj_file = GetObjectFile();
140814f1b3e8SDimitry Andric if (obj_file) {
1409f034231aSEd Maste SectionList *sections = GetSectionList();
141014f1b3e8SDimitry Andric if (sections != nullptr) {
1411f034231aSEd Maste size_t num_sections = sections->GetSize();
141214f1b3e8SDimitry Andric for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) {
1413f034231aSEd Maste SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
141414f1b3e8SDimitry Andric if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) {
1415f034231aSEd Maste return true;
1416f034231aSEd Maste }
1417f034231aSEd Maste }
1418f034231aSEd Maste }
1419f034231aSEd Maste }
1420f034231aSEd Maste return false;
1421f034231aSEd Maste }
1422f034231aSEd Maste
LoadScriptingResourceInTarget(Target * target,Status & error,Stream & feedback_stream)1423b76161e4SDimitry Andric bool Module::LoadScriptingResourceInTarget(Target *target, Status &error,
14247fa27ce4SDimitry Andric Stream &feedback_stream) {
142514f1b3e8SDimitry Andric if (!target) {
1426f034231aSEd Maste error.SetErrorString("invalid destination Target");
1427f034231aSEd Maste return false;
1428f034231aSEd Maste }
1429f034231aSEd Maste
143014f1b3e8SDimitry Andric LoadScriptFromSymFile should_load =
143114f1b3e8SDimitry Andric target->TargetProperties::GetLoadScriptFromSymbolFile();
14320cac4ca3SEd Maste
14330cac4ca3SEd Maste if (should_load == eLoadScriptFromSymFileFalse)
14340cac4ca3SEd Maste return false;
1435f034231aSEd Maste
1436f034231aSEd Maste Debugger &debugger = target->GetDebugger();
1437f034231aSEd Maste const ScriptLanguage script_language = debugger.GetScriptLanguage();
143814f1b3e8SDimitry Andric if (script_language != eScriptLanguageNone) {
1439f034231aSEd Maste
1440f034231aSEd Maste PlatformSP platform_sp(target->GetPlatform());
1441f034231aSEd Maste
144214f1b3e8SDimitry Andric if (!platform_sp) {
1443f034231aSEd Maste error.SetErrorString("invalid Platform");
1444f034231aSEd Maste return false;
1445f034231aSEd Maste }
1446f034231aSEd Maste
144714f1b3e8SDimitry Andric FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources(
144814f1b3e8SDimitry Andric target, *this, feedback_stream);
1449f034231aSEd Maste
1450f034231aSEd Maste const uint32_t num_specs = file_specs.GetSize();
145114f1b3e8SDimitry Andric if (num_specs) {
14525f29bb8aSDimitry Andric ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
145314f1b3e8SDimitry Andric if (script_interpreter) {
145414f1b3e8SDimitry Andric for (uint32_t i = 0; i < num_specs; ++i) {
1455f034231aSEd Maste FileSpec scripting_fspec(file_specs.GetFileSpecAtIndex(i));
145694994d37SDimitry Andric if (scripting_fspec &&
145794994d37SDimitry Andric FileSystem::Instance().Exists(scripting_fspec)) {
145814f1b3e8SDimitry Andric if (should_load == eLoadScriptFromSymFileWarn) {
14597fa27ce4SDimitry Andric feedback_stream.Printf(
146014f1b3e8SDimitry Andric "warning: '%s' contains a debug script. To run this script "
146114f1b3e8SDimitry Andric "in "
146214f1b3e8SDimitry Andric "this debug session:\n\n command script import "
146314f1b3e8SDimitry Andric "\"%s\"\n\n"
1464f034231aSEd Maste "To run all discovered debug scripts in this session:\n\n"
146514f1b3e8SDimitry Andric " settings set target.load-script-from-symbol-file "
146614f1b3e8SDimitry Andric "true\n",
1467f034231aSEd Maste GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1468f034231aSEd Maste scripting_fspec.GetPath().c_str());
1469f034231aSEd Maste return false;
1470f034231aSEd Maste }
1471f034231aSEd Maste StreamString scripting_stream;
1472706b4fc4SDimitry Andric scripting_fspec.Dump(scripting_stream.AsRawOstream());
1473344a3780SDimitry Andric LoadScriptOptions options;
147414f1b3e8SDimitry Andric bool did_load = script_interpreter->LoadScriptingModule(
1475344a3780SDimitry Andric scripting_stream.GetData(), options, error);
1476f034231aSEd Maste if (!did_load)
1477f034231aSEd Maste return false;
1478f034231aSEd Maste }
1479f034231aSEd Maste }
148014f1b3e8SDimitry Andric } else {
1481f034231aSEd Maste error.SetErrorString("invalid ScriptInterpreter");
1482f034231aSEd Maste return false;
1483f034231aSEd Maste }
1484f034231aSEd Maste }
1485f034231aSEd Maste }
1486f034231aSEd Maste return true;
1487f034231aSEd Maste }
1488f034231aSEd Maste
SetArchitecture(const ArchSpec & new_arch)148914f1b3e8SDimitry Andric bool Module::SetArchitecture(const ArchSpec &new_arch) {
149014f1b3e8SDimitry Andric if (!m_arch.IsValid()) {
1491f034231aSEd Maste m_arch = new_arch;
1492f034231aSEd Maste return true;
1493f034231aSEd Maste }
14945e95aa85SEd Maste return m_arch.IsCompatibleMatch(new_arch);
1495f034231aSEd Maste }
1496f034231aSEd Maste
SetLoadAddress(Target & target,lldb::addr_t value,bool value_is_offset,bool & changed)149714f1b3e8SDimitry Andric bool Module::SetLoadAddress(Target &target, lldb::addr_t value,
149814f1b3e8SDimitry Andric bool value_is_offset, bool &changed) {
1499866dcdacSEd Maste ObjectFile *object_file = GetObjectFile();
150014f1b3e8SDimitry Andric if (object_file != nullptr) {
1501866dcdacSEd Maste changed = object_file->SetLoadAddress(target, value, value_is_offset);
1502866dcdacSEd Maste return true;
150314f1b3e8SDimitry Andric } else {
1504866dcdacSEd Maste changed = false;
1505f034231aSEd Maste }
1506866dcdacSEd Maste return false;
1507f034231aSEd Maste }
1508f034231aSEd Maste
MatchesModuleSpec(const ModuleSpec & module_ref)150914f1b3e8SDimitry Andric bool Module::MatchesModuleSpec(const ModuleSpec &module_ref) {
1510f034231aSEd Maste const UUID &uuid = module_ref.GetUUID();
1511f034231aSEd Maste
151214f1b3e8SDimitry Andric if (uuid.IsValid()) {
1513f034231aSEd Maste // If the UUID matches, then nothing more needs to match...
1514f3fbd1c0SDimitry Andric return (uuid == GetUUID());
1515f034231aSEd Maste }
1516f034231aSEd Maste
1517f034231aSEd Maste const FileSpec &file_spec = module_ref.GetFileSpec();
1518706b4fc4SDimitry Andric if (!FileSpec::Match(file_spec, m_file) &&
1519706b4fc4SDimitry Andric !FileSpec::Match(file_spec, m_platform_file))
1520f034231aSEd Maste return false;
1521f034231aSEd Maste
1522f034231aSEd Maste const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1523706b4fc4SDimitry Andric if (!FileSpec::Match(platform_file_spec, GetPlatformFileSpec()))
1524f034231aSEd Maste return false;
1525f034231aSEd Maste
1526f034231aSEd Maste const ArchSpec &arch = module_ref.GetArchitecture();
152714f1b3e8SDimitry Andric if (arch.IsValid()) {
1528f034231aSEd Maste if (!m_arch.IsCompatibleMatch(arch))
1529f034231aSEd Maste return false;
1530f034231aSEd Maste }
1531f034231aSEd Maste
15325f29bb8aSDimitry Andric ConstString object_name = module_ref.GetObjectName();
153314f1b3e8SDimitry Andric if (object_name) {
1534f034231aSEd Maste if (object_name != GetObjectName())
1535f034231aSEd Maste return false;
1536f034231aSEd Maste }
1537f034231aSEd Maste return true;
1538f034231aSEd Maste }
1539f034231aSEd Maste
FindSourceFile(const FileSpec & orig_spec,FileSpec & new_spec) const154014f1b3e8SDimitry Andric bool Module::FindSourceFile(const FileSpec &orig_spec,
154114f1b3e8SDimitry Andric FileSpec &new_spec) const {
1542f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
1543344a3780SDimitry Andric if (auto remapped = m_source_mappings.FindFile(orig_spec)) {
1544344a3780SDimitry Andric new_spec = *remapped;
1545344a3780SDimitry Andric return true;
1546344a3780SDimitry Andric }
1547344a3780SDimitry Andric return false;
1548f034231aSEd Maste }
1549f034231aSEd Maste
RemapSourceFile(llvm::StringRef path) const1550e3b55780SDimitry Andric std::optional<std::string> Module::RemapSourceFile(llvm::StringRef path) const {
1551f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
1552344a3780SDimitry Andric if (auto remapped = m_source_mappings.RemapPath(path))
1553344a3780SDimitry Andric return remapped->GetPath();
1554344a3780SDimitry Andric return {};
1555f034231aSEd Maste }
1556f034231aSEd Maste
RegisterXcodeSDK(llvm::StringRef sdk_name,llvm::StringRef sysroot)155777fc4c14SDimitry Andric void Module::RegisterXcodeSDK(llvm::StringRef sdk_name,
155877fc4c14SDimitry Andric llvm::StringRef sysroot) {
15597fa27ce4SDimitry Andric auto sdk_path_or_err =
15607fa27ce4SDimitry Andric HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk_name.str()});
1561e3b55780SDimitry Andric
1562e3b55780SDimitry Andric if (!sdk_path_or_err) {
1563e3b55780SDimitry Andric Debugger::ReportError("Error while searching for Xcode SDK: " +
1564e3b55780SDimitry Andric toString(sdk_path_or_err.takeError()));
1565e3b55780SDimitry Andric return;
1566e3b55780SDimitry Andric }
1567e3b55780SDimitry Andric
1568e3b55780SDimitry Andric auto sdk_path = *sdk_path_or_err;
1569c0981da4SDimitry Andric if (sdk_path.empty())
1570cfca06d7SDimitry Andric return;
1571cfca06d7SDimitry Andric // If the SDK changed for a previously registered source path, update it.
1572cfca06d7SDimitry Andric // This could happend with -fdebug-prefix-map, otherwise it's unlikely.
1573c0981da4SDimitry Andric if (!m_source_mappings.Replace(sysroot, sdk_path, true))
1574cfca06d7SDimitry Andric // In the general case, however, append it to the list.
1575c0981da4SDimitry Andric m_source_mappings.Append(sysroot, sdk_path, false);
1576cfca06d7SDimitry Andric }
1577cfca06d7SDimitry Andric
MergeArchitecture(const ArchSpec & arch_spec)1578ead24645SDimitry Andric bool Module::MergeArchitecture(const ArchSpec &arch_spec) {
1579ead24645SDimitry Andric if (!arch_spec.IsValid())
1580ead24645SDimitry Andric return false;
1581145449b1SDimitry Andric LLDB_LOGF(GetLog(LLDBLog::Object | LLDBLog::Modules),
1582ead24645SDimitry Andric "module has arch %s, merging/replacing with arch %s",
1583ead24645SDimitry Andric m_arch.GetTriple().getTriple().c_str(),
1584ead24645SDimitry Andric arch_spec.GetTriple().getTriple().c_str());
1585ead24645SDimitry Andric if (!m_arch.IsCompatibleMatch(arch_spec)) {
1586ead24645SDimitry Andric // The new architecture is different, we just need to replace it.
1587ead24645SDimitry Andric return SetArchitecture(arch_spec);
1588ead24645SDimitry Andric }
1589ead24645SDimitry Andric
1590ead24645SDimitry Andric // Merge bits from arch_spec into "merged_arch" and set our architecture.
1591ead24645SDimitry Andric ArchSpec merged_arch(m_arch);
1592ead24645SDimitry Andric merged_arch.MergeFrom(arch_spec);
1593ead24645SDimitry Andric // SetArchitecture() is a no-op if m_arch is already valid.
1594ead24645SDimitry Andric m_arch = ArchSpec();
1595ead24645SDimitry Andric return SetArchitecture(merged_arch);
1596ead24645SDimitry Andric }
1597ead24645SDimitry Andric
GetVersion()1598f73363f1SDimitry Andric llvm::VersionTuple Module::GetVersion() {
1599f73363f1SDimitry Andric if (ObjectFile *obj_file = GetObjectFile())
1600f73363f1SDimitry Andric return obj_file->GetVersion();
1601f73363f1SDimitry Andric return llvm::VersionTuple();
16020cac4ca3SEd Maste }
16030cac4ca3SEd Maste
GetIsDynamicLinkEditor()160414f1b3e8SDimitry Andric bool Module::GetIsDynamicLinkEditor() {
160512bd4897SEd Maste ObjectFile *obj_file = GetObjectFile();
160612bd4897SEd Maste
160712bd4897SEd Maste if (obj_file)
160812bd4897SEd Maste return obj_file->GetIsDynamicLinkEditor();
160912bd4897SEd Maste
161012bd4897SEd Maste return false;
161112bd4897SEd Maste }
161277fc4c14SDimitry Andric
Hash()161377fc4c14SDimitry Andric uint32_t Module::Hash() {
161477fc4c14SDimitry Andric std::string identifier;
161577fc4c14SDimitry Andric llvm::raw_string_ostream id_strm(identifier);
161677fc4c14SDimitry Andric id_strm << m_arch.GetTriple().str() << '-' << m_file.GetPath();
161777fc4c14SDimitry Andric if (m_object_name)
16187fa27ce4SDimitry Andric id_strm << '(' << m_object_name << ')';
161977fc4c14SDimitry Andric if (m_object_offset > 0)
162077fc4c14SDimitry Andric id_strm << m_object_offset;
162177fc4c14SDimitry Andric const auto mtime = llvm::sys::toTimeT(m_object_mod_time);
162277fc4c14SDimitry Andric if (mtime > 0)
162377fc4c14SDimitry Andric id_strm << mtime;
162477fc4c14SDimitry Andric return llvm::djbHash(id_strm.str());
162577fc4c14SDimitry Andric }
162677fc4c14SDimitry Andric
GetCacheKey()162777fc4c14SDimitry Andric std::string Module::GetCacheKey() {
162877fc4c14SDimitry Andric std::string key;
162977fc4c14SDimitry Andric llvm::raw_string_ostream strm(key);
163077fc4c14SDimitry Andric strm << m_arch.GetTriple().str() << '-' << m_file.GetFilename();
163177fc4c14SDimitry Andric if (m_object_name)
16327fa27ce4SDimitry Andric strm << '(' << m_object_name << ')';
163377fc4c14SDimitry Andric strm << '-' << llvm::format_hex(Hash(), 10);
163477fc4c14SDimitry Andric return strm.str();
163577fc4c14SDimitry Andric }
163677fc4c14SDimitry Andric
GetIndexCache()163777fc4c14SDimitry Andric DataFileCache *Module::GetIndexCache() {
163877fc4c14SDimitry Andric if (!ModuleList::GetGlobalModuleListProperties().GetEnableLLDBIndexCache())
163977fc4c14SDimitry Andric return nullptr;
164077fc4c14SDimitry Andric // NOTE: intentional leak so we don't crash if global destructor chain gets
164177fc4c14SDimitry Andric // called as other threads still use the result of this function
1642145449b1SDimitry Andric static DataFileCache *g_data_file_cache =
1643145449b1SDimitry Andric new DataFileCache(ModuleList::GetGlobalModuleListProperties()
1644145449b1SDimitry Andric .GetLLDBIndexCachePath()
1645145449b1SDimitry Andric .GetPath());
164677fc4c14SDimitry Andric return g_data_file_cache;
164777fc4c14SDimitry Andric }
1648