130fdc8d8SChris Lattner //===-- Module.cpp ----------------------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
9c5dac77aSEugene Zelenko #include "lldb/Core/Module.h"
10c5dac77aSEugene Zelenko 
11672d2c12SJonas Devlieghere #include "lldb/Core/AddressRange.h"
12f86248d9SRichard Mitton #include "lldb/Core/AddressResolverFileLine.h"
13672d2c12SJonas Devlieghere #include "lldb/Core/Debugger.h"
14672d2c12SJonas Devlieghere #include "lldb/Core/FileSpecList.h"
15672d2c12SJonas Devlieghere #include "lldb/Core/Mangled.h"
161f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
17672d2c12SJonas Devlieghere #include "lldb/Core/SearchFilter.h"
181f746071SGreg Clayton #include "lldb/Core/Section.h"
191408bf72SPavel Labath #include "lldb/Host/FileSystem.h"
20e38a5eddSGreg Clayton #include "lldb/Host/Host.h"
211759848bSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h"
221759848bSEnrico Granata #include "lldb/Interpreter/ScriptInterpreter.h"
231f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
24672d2c12SJonas Devlieghere #include "lldb/Symbol/Function.h"
2530fdc8d8SChris Lattner #include "lldb/Symbol/ObjectFile.h"
26672d2c12SJonas Devlieghere #include "lldb/Symbol/Symbol.h"
2730fdc8d8SChris Lattner #include "lldb/Symbol/SymbolContext.h"
2856939cb3SGreg Clayton #include "lldb/Symbol/SymbolFile.h"
2930fdc8d8SChris Lattner #include "lldb/Symbol/SymbolVendor.h"
30672d2c12SJonas Devlieghere #include "lldb/Symbol/Symtab.h"
31672d2c12SJonas Devlieghere #include "lldb/Symbol/Type.h"
32672d2c12SJonas Devlieghere #include "lldb/Symbol/TypeList.h"
33b9c1b51eSKate Stone #include "lldb/Symbol/TypeMap.h"
3456939cb3SGreg Clayton #include "lldb/Symbol/TypeSystem.h"
350e0984eeSJim Ingham #include "lldb/Target/Language.h"
36672d2c12SJonas Devlieghere #include "lldb/Target/Platform.h"
37c9660546SGreg Clayton #include "lldb/Target/Process.h"
38c9660546SGreg Clayton #include "lldb/Target/Target.h"
39666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
409fecd372SLeonard Mosescu #include "lldb/Utility/LLDBAssert.h"
416f9e6901SZachary Turner #include "lldb/Utility/Log.h"
42672d2c12SJonas Devlieghere #include "lldb/Utility/Logging.h"
43bf9a7730SZachary Turner #include "lldb/Utility/RegularExpression.h"
4497206d57SZachary Turner #include "lldb/Utility/Status.h"
45672d2c12SJonas Devlieghere #include "lldb/Utility/Stream.h"
46bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
4738d0632eSPavel Labath #include "lldb/Utility/Timer.h"
4830fdc8d8SChris Lattner 
49b1cb0b79SNico Weber #if defined(_WIN32)
50672d2c12SJonas Devlieghere #include "lldb/Host/windows/PosixApi.h"
512f3df613SZachary Turner #endif
522f3df613SZachary Turner 
532f3df613SZachary Turner #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
542f3df613SZachary Turner #include "Plugins/Language/ObjC/ObjCLanguage.h"
5523f8c95aSGreg Clayton 
56672d2c12SJonas Devlieghere #include "llvm/ADT/STLExtras.h"
57672d2c12SJonas Devlieghere #include "llvm/Support/Compiler.h"
582f3df613SZachary Turner #include "llvm/Support/FileSystem.h"
592f3df613SZachary Turner #include "llvm/Support/Signals.h"
60672d2c12SJonas Devlieghere #include "llvm/Support/raw_ostream.h"
612f3df613SZachary Turner 
62672d2c12SJonas Devlieghere #include <assert.h>
63672d2c12SJonas Devlieghere #include <cstdint>
64672d2c12SJonas Devlieghere #include <inttypes.h>
65672d2c12SJonas Devlieghere #include <map>
66672d2c12SJonas Devlieghere #include <stdarg.h>
67672d2c12SJonas Devlieghere #include <string.h>
68672d2c12SJonas Devlieghere #include <type_traits>
69672d2c12SJonas Devlieghere #include <utility>
702f3df613SZachary Turner 
712f3df613SZachary Turner namespace lldb_private {
722f3df613SZachary Turner class CompilerDeclContext;
732f3df613SZachary Turner }
742f3df613SZachary Turner namespace lldb_private {
752f3df613SZachary Turner class VariableList;
762f3df613SZachary Turner }
772f3df613SZachary Turner 
7830fdc8d8SChris Lattner using namespace lldb;
7930fdc8d8SChris Lattner using namespace lldb_private;
8030fdc8d8SChris Lattner 
8105097246SAdrian Prantl // Shared pointers to modules track module lifetimes in targets and in the
8205097246SAdrian Prantl // global module, but this collection will track all module objects that are
8305097246SAdrian Prantl // still alive
8465a03991SGreg Clayton typedef std::vector<Module *> ModuleCollection;
8565a03991SGreg Clayton 
86b9c1b51eSKate Stone static ModuleCollection &GetModuleCollection() {
87b9c1b51eSKate Stone   // This module collection needs to live past any module, so we could either
8805097246SAdrian Prantl   // make it a shared pointer in each module or just leak is.  Since it is only
8905097246SAdrian Prantl   // an empty vector by the time all the modules have gone away, we just leak
9005097246SAdrian Prantl   // it for now.  If we decide this is a big problem we can introduce a
9105097246SAdrian Prantl   // Finalize method that will tear everything down in a predictable order.
92549f7374SJim Ingham 
93c5dac77aSEugene Zelenko   static ModuleCollection *g_module_collection = nullptr;
94c5dac77aSEugene Zelenko   if (g_module_collection == nullptr)
95549f7374SJim Ingham     g_module_collection = new ModuleCollection();
96549f7374SJim Ingham 
97549f7374SJim Ingham   return *g_module_collection;
9865a03991SGreg Clayton }
9965a03991SGreg Clayton 
100b9c1b51eSKate Stone std::recursive_mutex &Module::GetAllocationModuleCollectionMutex() {
101b26e6bebSGreg Clayton   // NOTE: The mutex below must be leaked since the global module list in
10205097246SAdrian Prantl   // the ModuleList class will get torn at some point, and we can't know if it
10305097246SAdrian Prantl   // will tear itself down before the "g_module_collection_mutex" below will.
10405097246SAdrian Prantl   // So we leak a Mutex object below to safeguard against that
105b26e6bebSGreg Clayton 
10616ff8604SSaleem Abdulrasool   static std::recursive_mutex *g_module_collection_mutex = nullptr;
107c5dac77aSEugene Zelenko   if (g_module_collection_mutex == nullptr)
10816ff8604SSaleem Abdulrasool     g_module_collection_mutex = new std::recursive_mutex; // NOTE: known leak
10916ff8604SSaleem Abdulrasool   return *g_module_collection_mutex;
11065a03991SGreg Clayton }
11165a03991SGreg Clayton 
112b9c1b51eSKate Stone size_t Module::GetNumberAllocatedModules() {
113b9c1b51eSKate Stone   std::lock_guard<std::recursive_mutex> guard(
114b9c1b51eSKate Stone       GetAllocationModuleCollectionMutex());
11565a03991SGreg Clayton   return GetModuleCollection().size();
11665a03991SGreg Clayton }
11765a03991SGreg Clayton 
118b9c1b51eSKate Stone Module *Module::GetAllocatedModuleAtIndex(size_t idx) {
119b9c1b51eSKate Stone   std::lock_guard<std::recursive_mutex> guard(
120b9c1b51eSKate Stone       GetAllocationModuleCollectionMutex());
12165a03991SGreg Clayton   ModuleCollection &modules = GetModuleCollection();
12265a03991SGreg Clayton   if (idx < modules.size())
12365a03991SGreg Clayton     return modules[idx];
124c5dac77aSEugene Zelenko   return nullptr;
12565a03991SGreg Clayton }
12665a03991SGreg Clayton 
12716ff8604SSaleem Abdulrasool Module::Module(const ModuleSpec &module_spec)
1281408bf72SPavel Labath     : m_object_offset(0), m_file_has_changed(false),
129b9c1b51eSKate Stone       m_first_file_changed_log(false) {
130b9a01b39SGreg Clayton   // Scope for locker below...
131b9a01b39SGreg Clayton   {
132b9c1b51eSKate Stone     std::lock_guard<std::recursive_mutex> guard(
133b9c1b51eSKate Stone         GetAllocationModuleCollectionMutex());
134b9a01b39SGreg Clayton     GetModuleCollection().push_back(this);
135b9a01b39SGreg Clayton   }
136b9a01b39SGreg Clayton 
137b9c1b51eSKate Stone   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT |
138b9c1b51eSKate Stone                                                   LIBLLDB_LOG_MODULES));
139c5dac77aSEugene Zelenko   if (log != nullptr)
14063e5fb76SJonas Devlieghere     LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')",
14163e5fb76SJonas Devlieghere               static_cast<void *>(this),
142b9c1b51eSKate Stone               module_spec.GetArchitecture().GetArchitectureName(),
143b9c1b51eSKate Stone               module_spec.GetFileSpec().GetPath().c_str(),
14434f1159bSGreg Clayton               module_spec.GetObjectName().IsEmpty() ? "" : "(",
145b9c1b51eSKate Stone               module_spec.GetObjectName().IsEmpty()
146b9c1b51eSKate Stone                   ? ""
147b9c1b51eSKate Stone                   : module_spec.GetObjectName().AsCString(""),
14834f1159bSGreg Clayton               module_spec.GetObjectName().IsEmpty() ? "" : ")");
14934f1159bSGreg Clayton 
15005097246SAdrian Prantl   // First extract all module specifications from the file using the local file
15105097246SAdrian Prantl   // path. If there are no specifications, then don't fill anything in
15234f1159bSGreg Clayton   ModuleSpecList modules_specs;
153b9c1b51eSKate Stone   if (ObjectFile::GetModuleSpecifications(module_spec.GetFileSpec(), 0, 0,
154b9c1b51eSKate Stone                                           modules_specs) == 0)
15534f1159bSGreg Clayton     return;
15634f1159bSGreg Clayton 
15734f1159bSGreg Clayton   // Now make sure that one of the module specifications matches what we just
158b9c1b51eSKate Stone   // extract. We might have a module specification that specifies a file
15905097246SAdrian Prantl   // "/usr/lib/dyld" with UUID XXX, but we might have a local version of
16005097246SAdrian Prantl   // "/usr/lib/dyld" that has
16134f1159bSGreg Clayton   // UUID YYY and we don't want those to match. If they don't match, just don't
16234f1159bSGreg Clayton   // fill any ivars in so we don't accidentally grab the wrong file later since
16334f1159bSGreg Clayton   // they don't match...
16434f1159bSGreg Clayton   ModuleSpec matching_module_spec;
1659ba51579SLeonard Mosescu   if (!modules_specs.FindMatchingModuleSpec(module_spec,
1669ba51579SLeonard Mosescu                                             matching_module_spec)) {
1679ba51579SLeonard Mosescu     if (log) {
16863e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Found local object file but the specs didn't match");
1699ba51579SLeonard Mosescu     }
17034f1159bSGreg Clayton     return;
1719ba51579SLeonard Mosescu   }
1727ab7f89aSGreg Clayton 
1737ab7f89aSGreg Clayton   if (module_spec.GetFileSpec())
17446376966SJonas Devlieghere     m_mod_time = FileSystem::Instance().GetModificationTime(module_spec.GetFileSpec());
1757ab7f89aSGreg Clayton   else if (matching_module_spec.GetFileSpec())
1761408bf72SPavel Labath     m_mod_time =
17746376966SJonas Devlieghere         FileSystem::Instance().GetModificationTime(matching_module_spec.GetFileSpec());
1787ab7f89aSGreg Clayton 
17905097246SAdrian Prantl   // Copy the architecture from the actual spec if we got one back, else use
18005097246SAdrian Prantl   // the one that was specified
1817ab7f89aSGreg Clayton   if (matching_module_spec.GetArchitecture().IsValid())
18234f1159bSGreg Clayton     m_arch = matching_module_spec.GetArchitecture();
1837ab7f89aSGreg Clayton   else if (module_spec.GetArchitecture().IsValid())
1847ab7f89aSGreg Clayton     m_arch = module_spec.GetArchitecture();
1857ab7f89aSGreg Clayton 
186d93c4a33SBruce Mitchener   // Copy the file spec over and use the specified one (if there was one) so we
187b9c1b51eSKate Stone   // don't use a path that might have gotten resolved a path in
188b9c1b51eSKate Stone   // 'matching_module_spec'
1897ab7f89aSGreg Clayton   if (module_spec.GetFileSpec())
19034f1159bSGreg Clayton     m_file = module_spec.GetFileSpec();
1917ab7f89aSGreg Clayton   else if (matching_module_spec.GetFileSpec())
1927ab7f89aSGreg Clayton     m_file = matching_module_spec.GetFileSpec();
1937ab7f89aSGreg Clayton 
1947ab7f89aSGreg Clayton   // Copy the platform file spec over
1957ab7f89aSGreg Clayton   if (module_spec.GetPlatformFileSpec())
19634f1159bSGreg Clayton     m_platform_file = module_spec.GetPlatformFileSpec();
1977ab7f89aSGreg Clayton   else if (matching_module_spec.GetPlatformFileSpec())
1987ab7f89aSGreg Clayton     m_platform_file = matching_module_spec.GetPlatformFileSpec();
1997ab7f89aSGreg Clayton 
2007ab7f89aSGreg Clayton   // Copy the symbol file spec over
2017ab7f89aSGreg Clayton   if (module_spec.GetSymbolFileSpec())
20234f1159bSGreg Clayton     m_symfile_spec = module_spec.GetSymbolFileSpec();
2037ab7f89aSGreg Clayton   else if (matching_module_spec.GetSymbolFileSpec())
2047ab7f89aSGreg Clayton     m_symfile_spec = matching_module_spec.GetSymbolFileSpec();
2057ab7f89aSGreg Clayton 
2067ab7f89aSGreg Clayton   // Copy the object name over
2077ab7f89aSGreg Clayton   if (matching_module_spec.GetObjectName())
2087ab7f89aSGreg Clayton     m_object_name = matching_module_spec.GetObjectName();
2097ab7f89aSGreg Clayton   else
21034f1159bSGreg Clayton     m_object_name = module_spec.GetObjectName();
2117ab7f89aSGreg Clayton 
21205097246SAdrian Prantl   // Always trust the object offset (file offset) and object modification time
21305097246SAdrian Prantl   // (for mod time in a BSD static archive) of from the matching module
21405097246SAdrian Prantl   // specification
21536d7c894SGreg Clayton   m_object_offset = matching_module_spec.GetObjectOffset();
21636d7c894SGreg Clayton   m_object_mod_time = matching_module_spec.GetObjectModificationTime();
217b9a01b39SGreg Clayton }
218b9a01b39SGreg Clayton 
219b9c1b51eSKate Stone Module::Module(const FileSpec &file_spec, const ArchSpec &arch,
220b9c1b51eSKate Stone                const ConstString *object_name, lldb::offset_t object_offset,
2217e2cfbf0SPavel Labath                const llvm::sys::TimePoint<> &object_mod_time)
22246376966SJonas Devlieghere     : m_mod_time(FileSystem::Instance().GetModificationTime(file_spec)), m_arch(arch),
2231408bf72SPavel Labath       m_file(file_spec), m_object_offset(object_offset),
2247e2cfbf0SPavel Labath       m_object_mod_time(object_mod_time), m_file_has_changed(false),
2257e2cfbf0SPavel Labath       m_first_file_changed_log(false) {
22665a03991SGreg Clayton   // Scope for locker below...
22765a03991SGreg Clayton   {
228b9c1b51eSKate Stone     std::lock_guard<std::recursive_mutex> guard(
229b9c1b51eSKate Stone         GetAllocationModuleCollectionMutex());
23065a03991SGreg Clayton     GetModuleCollection().push_back(this);
23165a03991SGreg Clayton   }
23265a03991SGreg Clayton 
23330fdc8d8SChris Lattner   if (object_name)
23430fdc8d8SChris Lattner     m_object_name = *object_name;
23557abc5d6SGreg Clayton 
236b9c1b51eSKate Stone   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT |
237b9c1b51eSKate Stone                                                   LIBLLDB_LOG_MODULES));
238c5dac77aSEugene Zelenko   if (log != nullptr)
23963e5fb76SJonas Devlieghere     LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')",
24063e5fb76SJonas Devlieghere               static_cast<void *>(this), m_arch.GetArchitectureName(),
24163e5fb76SJonas Devlieghere               m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(",
242b9c1b51eSKate Stone               m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
243b9c1b51eSKate Stone               m_object_name.IsEmpty() ? "" : ")");
24430fdc8d8SChris Lattner }
24530fdc8d8SChris Lattner 
24616ff8604SSaleem Abdulrasool Module::Module()
2471408bf72SPavel Labath     : m_object_offset(0), m_file_has_changed(false),
248b9c1b51eSKate Stone       m_first_file_changed_log(false) {
249b9c1b51eSKate Stone   std::lock_guard<std::recursive_mutex> guard(
250b9c1b51eSKate Stone       GetAllocationModuleCollectionMutex());
25123f8c95aSGreg Clayton   GetModuleCollection().push_back(this);
25223f8c95aSGreg Clayton }
25323f8c95aSGreg Clayton 
254b9c1b51eSKate Stone Module::~Module() {
25505097246SAdrian Prantl   // Lock our module down while we tear everything down to make sure we don't
25605097246SAdrian Prantl   // get any access to the module while it is being destroyed
25716ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
25865a03991SGreg Clayton   // Scope for locker below...
25965a03991SGreg Clayton   {
260b9c1b51eSKate Stone     std::lock_guard<std::recursive_mutex> guard(
261b9c1b51eSKate Stone         GetAllocationModuleCollectionMutex());
26265a03991SGreg Clayton     ModuleCollection &modules = GetModuleCollection();
26365a03991SGreg Clayton     ModuleCollection::iterator end = modules.end();
26465a03991SGreg Clayton     ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
2653a18e319SGreg Clayton     assert(pos != end);
26665a03991SGreg Clayton     modules.erase(pos);
26765a03991SGreg Clayton   }
268b9c1b51eSKate Stone   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT |
269b9c1b51eSKate Stone                                                   LIBLLDB_LOG_MODULES));
270c5dac77aSEugene Zelenko   if (log != nullptr)
27163e5fb76SJonas Devlieghere     LLDB_LOGF(log, "%p Module::~Module((%s) '%s%s%s%s')",
272b9c1b51eSKate Stone               static_cast<void *>(this), m_arch.GetArchitectureName(),
273b9c1b51eSKate Stone               m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(",
27430fdc8d8SChris Lattner               m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
27530fdc8d8SChris Lattner               m_object_name.IsEmpty() ? "" : ")");
2766beaaa68SGreg Clayton   // Release any auto pointers before we start tearing down our member
2776beaaa68SGreg Clayton   // variables since the object file and symbol files might need to make
2786beaaa68SGreg Clayton   // function calls back into this module object. The ordering is important
2796beaaa68SGreg Clayton   // here because symbol files can require the module object file. So we tear
2806beaaa68SGreg Clayton   // down the symbol file first, then the object file.
281d5b44036SJonas Devlieghere   m_sections_up.reset();
282d5b44036SJonas Devlieghere   m_symfile_up.reset();
283762f7135SGreg Clayton   m_objfile_sp.reset();
28430fdc8d8SChris Lattner }
28530fdc8d8SChris Lattner 
286b9c1b51eSKate Stone ObjectFile *Module::GetMemoryObjectFile(const lldb::ProcessSP &process_sp,
28797206d57SZachary Turner                                         lldb::addr_t header_addr, Status &error,
288b9c1b51eSKate Stone                                         size_t size_to_read) {
289b9c1b51eSKate Stone   if (m_objfile_sp) {
290c7f09ccaSGreg Clayton     error.SetErrorString("object file already exists");
291b9c1b51eSKate Stone   } else {
29216ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
293b9c1b51eSKate Stone     if (process_sp) {
294c7f09ccaSGreg Clayton       m_did_load_objfile = true;
295a8f3ae7cSJonas Devlieghere       auto data_up = std::make_unique<DataBufferHeap>(size_to_read, 0);
29697206d57SZachary Turner       Status readmem_error;
297b9c1b51eSKate Stone       const size_t bytes_read =
298d5b44036SJonas Devlieghere           process_sp->ReadMemory(header_addr, data_up->GetBytes(),
299d5b44036SJonas Devlieghere                                  data_up->GetByteSize(), readmem_error);
300b9c1b51eSKate Stone       if (bytes_read == size_to_read) {
301d5b44036SJonas Devlieghere         DataBufferSP data_sp(data_up.release());
302b9c1b51eSKate Stone         m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp,
303b9c1b51eSKate Stone                                               header_addr, data_sp);
304b9c1b51eSKate Stone         if (m_objfile_sp) {
3053e10cf3bSGreg Clayton           StreamString s;
306d01b2953SDaniel Malea           s.Printf("0x%16.16" PRIx64, header_addr);
307c156427dSZachary Turner           m_object_name.SetString(s.GetString());
3083e10cf3bSGreg Clayton 
309b9c1b51eSKate Stone           // Once we get the object file, update our module with the object
31005097246SAdrian Prantl           // file's architecture since it might differ in vendor/os if some
31105097246SAdrian Prantl           // parts were unknown.
312f760f5aeSPavel Labath           m_arch = m_objfile_sp->GetArchitecture();
313a07287ecSAlex Langford 
314a07287ecSAlex Langford           // Augment the arch with the target's information in case
315a07287ecSAlex Langford           // we are unable to extract the os/environment from memory.
316a07287ecSAlex Langford           m_arch.MergeFrom(process_sp->GetTarget().GetArchitecture());
317b9c1b51eSKate Stone         } else {
318c7f09ccaSGreg Clayton           error.SetErrorString("unable to find suitable object file plug-in");
319c7f09ccaSGreg Clayton         }
320b9c1b51eSKate Stone       } else {
321b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unable to read header from memory: %s",
322b9c1b51eSKate Stone                                        readmem_error.AsCString());
323c7f09ccaSGreg Clayton       }
324b9c1b51eSKate Stone     } else {
325c7f09ccaSGreg Clayton       error.SetErrorString("invalid process");
326c7f09ccaSGreg Clayton     }
327c7f09ccaSGreg Clayton   }
328c7f09ccaSGreg Clayton   return m_objfile_sp.get();
329c7f09ccaSGreg Clayton }
330c7f09ccaSGreg Clayton 
331b9c1b51eSKate Stone const lldb_private::UUID &Module::GetUUID() {
3329fecd372SLeonard Mosescu   if (!m_did_set_uuid.load()) {
33316ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
3349fecd372SLeonard Mosescu     if (!m_did_set_uuid.load()) {
33530fdc8d8SChris Lattner       ObjectFile *obj_file = GetObjectFile();
33630fdc8d8SChris Lattner 
337b9c1b51eSKate Stone       if (obj_file != nullptr) {
338bd334efdSPavel Labath         m_uuid = obj_file->GetUUID();
3399fecd372SLeonard Mosescu         m_did_set_uuid = true;
34030fdc8d8SChris Lattner       }
34130fdc8d8SChris Lattner     }
34288c05f54SGreg Clayton   }
34330fdc8d8SChris Lattner   return m_uuid;
34430fdc8d8SChris Lattner }
34530fdc8d8SChris Lattner 
3469fecd372SLeonard Mosescu void Module::SetUUID(const lldb_private::UUID &uuid) {
3479fecd372SLeonard Mosescu   std::lock_guard<std::recursive_mutex> guard(m_mutex);
3489fecd372SLeonard Mosescu   if (!m_did_set_uuid) {
3499fecd372SLeonard Mosescu     m_uuid = uuid;
3509fecd372SLeonard Mosescu     m_did_set_uuid = true;
3519fecd372SLeonard Mosescu   } else {
352fbe748aeSRichard Smith     lldbassert(0 && "Attempting to overwrite the existing module UUID");
3539fecd372SLeonard Mosescu   }
3549fecd372SLeonard Mosescu }
3559fecd372SLeonard Mosescu 
3560e252e38SAlex Langford llvm::Expected<TypeSystem &>
3570e252e38SAlex Langford Module::GetTypeSystemForLanguage(LanguageType language) {
3585beec213SGreg Clayton   return m_type_system_map.GetTypeSystemForLanguage(language, this, true);
3596beaaa68SGreg Clayton }
3606beaaa68SGreg Clayton 
361b9c1b51eSKate Stone void Module::ParseAllDebugSymbols() {
36216ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
363c7bece56SGreg Clayton   size_t num_comp_units = GetNumCompileUnits();
36430fdc8d8SChris Lattner   if (num_comp_units == 0)
36530fdc8d8SChris Lattner     return;
36630fdc8d8SChris Lattner 
367a2eee184SGreg Clayton   SymbolContext sc;
368e1cd1be6SGreg Clayton   sc.module_sp = shared_from_this();
36923f70e83SPavel Labath   SymbolFile *symbols = GetSymbolFile();
37030fdc8d8SChris Lattner 
371b9c1b51eSKate Stone   for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++) {
37230fdc8d8SChris Lattner     sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
373863f8c18SZachary Turner     if (!sc.comp_unit)
374863f8c18SZachary Turner       continue;
375863f8c18SZachary Turner 
37630fdc8d8SChris Lattner     symbols->ParseVariablesForContext(sc);
37730fdc8d8SChris Lattner 
378863f8c18SZachary Turner     symbols->ParseFunctions(*sc.comp_unit);
37930fdc8d8SChris Lattner 
380a7f19e5fSRaphael Isemann     sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) {
381ffc1b8fdSZachary Turner       symbols->ParseBlocksRecursive(*f);
382ffc1b8fdSZachary Turner 
38330fdc8d8SChris Lattner       // Parse the variables for this function and all its blocks
384ffc1b8fdSZachary Turner       sc.function = f.get();
38530fdc8d8SChris Lattner       symbols->ParseVariablesForContext(sc);
386a7f19e5fSRaphael Isemann       return false;
387a7f19e5fSRaphael Isemann     });
38830fdc8d8SChris Lattner 
38930fdc8d8SChris Lattner     // Parse all types for this compile unit
390863f8c18SZachary Turner     symbols->ParseTypes(*sc.comp_unit);
39130fdc8d8SChris Lattner   }
39230fdc8d8SChris Lattner }
39330fdc8d8SChris Lattner 
394b9c1b51eSKate Stone void Module::CalculateSymbolContext(SymbolContext *sc) {
395e1cd1be6SGreg Clayton   sc->module_sp = shared_from_this();
39630fdc8d8SChris Lattner }
39730fdc8d8SChris Lattner 
398b9c1b51eSKate Stone ModuleSP Module::CalculateSymbolContextModule() { return shared_from_this(); }
3997e9b1fd0SGreg Clayton 
400b9c1b51eSKate Stone void Module::DumpSymbolContext(Stream *s) {
401324a1036SSaleem Abdulrasool   s->Printf(", Module{%p}", static_cast<void *>(this));
40230fdc8d8SChris Lattner }
40330fdc8d8SChris Lattner 
404b9c1b51eSKate Stone size_t Module::GetNumCompileUnits() {
40516ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
406f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
407f9d16476SPavel Labath   Timer scoped_timer(func_cat, "Module::GetNumCompileUnits (module = %p)",
408324a1036SSaleem Abdulrasool                      static_cast<void *>(this));
40923f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
41030fdc8d8SChris Lattner     return symbols->GetNumCompileUnits();
41130fdc8d8SChris Lattner   return 0;
41230fdc8d8SChris Lattner }
41330fdc8d8SChris Lattner 
414b9c1b51eSKate Stone CompUnitSP Module::GetCompileUnitAtIndex(size_t index) {
41516ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
416c7bece56SGreg Clayton   size_t num_comp_units = GetNumCompileUnits();
41730fdc8d8SChris Lattner   CompUnitSP cu_sp;
41830fdc8d8SChris Lattner 
419b9c1b51eSKate Stone   if (index < num_comp_units) {
42023f70e83SPavel Labath     if (SymbolFile *symbols = GetSymbolFile())
42130fdc8d8SChris Lattner       cu_sp = symbols->GetCompileUnitAtIndex(index);
42230fdc8d8SChris Lattner   }
42330fdc8d8SChris Lattner   return cu_sp;
42430fdc8d8SChris Lattner }
42530fdc8d8SChris Lattner 
426b9c1b51eSKate Stone bool Module::ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr) {
42716ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
428f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
429f9d16476SPavel Labath   Timer scoped_timer(func_cat,
430b9c1b51eSKate Stone                      "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")",
431b9c1b51eSKate Stone                      vm_addr);
4323046e668SGreg Clayton   SectionList *section_list = GetSectionList();
4333046e668SGreg Clayton   if (section_list)
4343046e668SGreg Clayton     return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
43530fdc8d8SChris Lattner   return false;
43630fdc8d8SChris Lattner }
43730fdc8d8SChris Lattner 
438b9c1b51eSKate Stone uint32_t Module::ResolveSymbolContextForAddress(
439991e4453SZachary Turner     const Address &so_addr, lldb::SymbolContextItem resolve_scope,
440991e4453SZachary Turner     SymbolContext &sc, bool resolve_tail_call_address) {
44116ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
44230fdc8d8SChris Lattner   uint32_t resolved_flags = 0;
44330fdc8d8SChris Lattner 
444b9c1b51eSKate Stone   // Clear the result symbol context in case we don't find anything, but don't
445b9c1b51eSKate Stone   // clear the target
44672310355SGreg Clayton   sc.Clear(false);
44730fdc8d8SChris Lattner 
44830fdc8d8SChris Lattner   // Get the section from the section/offset address.
449e72dfb32SGreg Clayton   SectionSP section_sp(so_addr.GetSection());
45030fdc8d8SChris Lattner 
45130fdc8d8SChris Lattner   // Make sure the section matches this module before we try and match anything
452b9c1b51eSKate Stone   if (section_sp && section_sp->GetModule().get() == this) {
45305097246SAdrian Prantl     // If the section offset based address resolved itself, then this is the
45405097246SAdrian Prantl     // right module.
455e1cd1be6SGreg Clayton     sc.module_sp = shared_from_this();
45630fdc8d8SChris Lattner     resolved_flags |= eSymbolContextModule;
45730fdc8d8SChris Lattner 
45823f70e83SPavel Labath     SymbolFile *symfile = GetSymbolFile();
45923f70e83SPavel Labath     if (!symfile)
46038807141SAshok Thirumurthi       return resolved_flags;
46138807141SAshok Thirumurthi 
46205097246SAdrian Prantl     // Resolve the compile unit, function, block, line table or line entry if
46305097246SAdrian Prantl     // requested.
46430fdc8d8SChris Lattner     if (resolve_scope & eSymbolContextCompUnit ||
46530fdc8d8SChris Lattner         resolve_scope & eSymbolContextFunction ||
46630fdc8d8SChris Lattner         resolve_scope & eSymbolContextBlock ||
4674c8e7828SGreg Clayton         resolve_scope & eSymbolContextLineEntry ||
468b9c1b51eSKate Stone         resolve_scope & eSymbolContextVariable) {
469b9c1b51eSKate Stone       resolved_flags |=
47023f70e83SPavel Labath           symfile->ResolveSymbolContext(so_addr, resolve_scope, sc);
47130fdc8d8SChris Lattner     }
47230fdc8d8SChris Lattner 
47305097246SAdrian Prantl     // Resolve the symbol if requested, but don't re-look it up if we've
47405097246SAdrian Prantl     // already found it.
475b9c1b51eSKate Stone     if (resolve_scope & eSymbolContextSymbol &&
476b9c1b51eSKate Stone         !(resolved_flags & eSymbolContextSymbol)) {
47723f70e83SPavel Labath       Symtab *symtab = symfile->GetSymtab();
478b9c1b51eSKate Stone       if (symtab && so_addr.IsSectionOffset()) {
4790d9dd7dfSMohit K. Bhakkad         Symbol *matching_symbol = nullptr;
480c35b91ceSAdrian McCarthy 
481b9c1b51eSKate Stone         symtab->ForEachSymbolContainingFileAddress(
482b9c1b51eSKate Stone             so_addr.GetFileAddress(),
483c35b91ceSAdrian McCarthy             [&matching_symbol](Symbol *symbol) -> bool {
484b9c1b51eSKate Stone               if (symbol->GetType() != eSymbolTypeInvalid) {
4850d9dd7dfSMohit K. Bhakkad                 matching_symbol = symbol;
4860d9dd7dfSMohit K. Bhakkad                 return false; // Stop iterating
4870d9dd7dfSMohit K. Bhakkad               }
4880d9dd7dfSMohit K. Bhakkad               return true; // Keep iterating
4890d9dd7dfSMohit K. Bhakkad             });
4900d9dd7dfSMohit K. Bhakkad         sc.symbol = matching_symbol;
491b9c1b51eSKate Stone         if (!sc.symbol && resolve_scope & eSymbolContextFunction &&
492b9c1b51eSKate Stone             !(resolved_flags & eSymbolContextFunction)) {
493b9c1b51eSKate Stone           bool verify_unique = false; // No need to check again since
494b9c1b51eSKate Stone                                       // ResolveSymbolContext failed to find a
495b9c1b51eSKate Stone                                       // symbol at this address.
49635729bb1SAshok Thirumurthi           if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
497b9c1b51eSKate Stone             sc.symbol =
498b9c1b51eSKate Stone                 obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
49935729bb1SAshok Thirumurthi         }
50035729bb1SAshok Thirumurthi 
501b9c1b51eSKate Stone         if (sc.symbol) {
502b9c1b51eSKate Stone           if (sc.symbol->IsSynthetic()) {
50305097246SAdrian Prantl             // We have a synthetic symbol so lets check if the object file from
50405097246SAdrian Prantl             // the symbol file in the symbol vendor is different than the
50505097246SAdrian Prantl             // object file for the module, and if so search its symbol table to
50605097246SAdrian Prantl             // see if we can come up with a better symbol. For example dSYM
50705097246SAdrian Prantl             // files on MacOSX have an unstripped symbol table inside of them.
50893e2861bSGreg Clayton             ObjectFile *symtab_objfile = symtab->GetObjectFile();
509b9c1b51eSKate Stone             if (symtab_objfile && symtab_objfile->IsStripped()) {
51093e2861bSGreg Clayton               ObjectFile *symfile_objfile = symfile->GetObjectFile();
511b9c1b51eSKate Stone               if (symfile_objfile != symtab_objfile) {
51293e2861bSGreg Clayton                 Symtab *symfile_symtab = symfile_objfile->GetSymtab();
513b9c1b51eSKate Stone                 if (symfile_symtab) {
514b9c1b51eSKate Stone                   Symbol *symbol =
515b9c1b51eSKate Stone                       symfile_symtab->FindSymbolContainingFileAddress(
516b9c1b51eSKate Stone                           so_addr.GetFileAddress());
517b9c1b51eSKate Stone                   if (symbol && !symbol->IsSynthetic()) {
51893e2861bSGreg Clayton                     sc.symbol = symbol;
51993e2861bSGreg Clayton                   }
52093e2861bSGreg Clayton                 }
52193e2861bSGreg Clayton               }
52293e2861bSGreg Clayton             }
52393e2861bSGreg Clayton           }
52430fdc8d8SChris Lattner           resolved_flags |= eSymbolContextSymbol;
52530fdc8d8SChris Lattner         }
52630fdc8d8SChris Lattner       }
52793e2861bSGreg Clayton     }
52838807141SAshok Thirumurthi 
529b9c1b51eSKate Stone     // For function symbols, so_addr may be off by one.  This is a convention
53005097246SAdrian Prantl     // consistent with FDE row indices in eh_frame sections, but requires extra
53105097246SAdrian Prantl     // logic here to permit symbol lookup for disassembly and unwind.
532b9c1b51eSKate Stone     if (resolve_scope & eSymbolContextSymbol &&
533b9c1b51eSKate Stone         !(resolved_flags & eSymbolContextSymbol) && resolve_tail_call_address &&
534b9c1b51eSKate Stone         so_addr.IsSectionOffset()) {
53538807141SAshok Thirumurthi       Address previous_addr = so_addr;
536edfaae39SGreg Clayton       previous_addr.Slide(-1);
53738807141SAshok Thirumurthi 
53835729bb1SAshok Thirumurthi       bool do_resolve_tail_call_address = false; // prevent recursion
539b9c1b51eSKate Stone       const uint32_t flags = ResolveSymbolContextForAddress(
540b9c1b51eSKate Stone           previous_addr, resolve_scope, sc, do_resolve_tail_call_address);
541b9c1b51eSKate Stone       if (flags & eSymbolContextSymbol) {
54238807141SAshok Thirumurthi         AddressRange addr_range;
543b9c1b51eSKate Stone         if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
544b9c1b51eSKate Stone                                false, addr_range)) {
545b9c1b51eSKate Stone           if (addr_range.GetBaseAddress().GetSection() ==
546b9c1b51eSKate Stone               so_addr.GetSection()) {
547b9c1b51eSKate Stone             // If the requested address is one past the address range of a
54805097246SAdrian Prantl             // function (i.e. a tail call), or the decremented address is the
54905097246SAdrian Prantl             // start of a function (i.e. some forms of trampoline), indicate
55005097246SAdrian Prantl             // that the symbol has been resolved.
551b9c1b51eSKate Stone             if (so_addr.GetOffset() ==
552b9c1b51eSKate Stone                     addr_range.GetBaseAddress().GetOffset() ||
553b9c1b51eSKate Stone                 so_addr.GetOffset() ==
554b9c1b51eSKate Stone                     addr_range.GetBaseAddress().GetOffset() +
555b9c1b51eSKate Stone                         addr_range.GetByteSize()) {
55638807141SAshok Thirumurthi               resolved_flags |= flags;
55738807141SAshok Thirumurthi             }
558b9c1b51eSKate Stone           } else {
559b9c1b51eSKate Stone             sc.symbol =
560b9c1b51eSKate Stone                 nullptr; // Don't trust the symbol if the sections didn't match.
56138807141SAshok Thirumurthi           }
56238807141SAshok Thirumurthi         }
56330fdc8d8SChris Lattner       }
56430fdc8d8SChris Lattner     }
56530fdc8d8SChris Lattner   }
56630fdc8d8SChris Lattner   return resolved_flags;
56730fdc8d8SChris Lattner }
56830fdc8d8SChris Lattner 
569991e4453SZachary Turner uint32_t Module::ResolveSymbolContextForFilePath(
570991e4453SZachary Turner     const char *file_path, uint32_t line, bool check_inlines,
571991e4453SZachary Turner     lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
5728f3be7a3SJonas Devlieghere   FileSpec file_spec(file_path);
573b9c1b51eSKate Stone   return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
574b9c1b51eSKate Stone                                           resolve_scope, sc_list);
57530fdc8d8SChris Lattner }
57630fdc8d8SChris Lattner 
577991e4453SZachary Turner uint32_t Module::ResolveSymbolContextsForFileSpec(
578991e4453SZachary Turner     const FileSpec &file_spec, uint32_t line, bool check_inlines,
579991e4453SZachary Turner     lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
58016ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
581f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
582f9d16476SPavel Labath   Timer scoped_timer(func_cat,
583b9c1b51eSKate Stone                      "Module::ResolveSymbolContextForFilePath (%s:%u, "
584b9c1b51eSKate Stone                      "check_inlines = %s, resolve_scope = 0x%8.8x)",
585b9c1b51eSKate Stone                      file_spec.GetPath().c_str(), line,
586b9c1b51eSKate Stone                      check_inlines ? "yes" : "no", resolve_scope);
58730fdc8d8SChris Lattner 
58830fdc8d8SChris Lattner   const uint32_t initial_count = sc_list.GetSize();
58930fdc8d8SChris Lattner 
59023f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
591b9c1b51eSKate Stone     symbols->ResolveSymbolContext(file_spec, line, check_inlines, resolve_scope,
592b9c1b51eSKate Stone                                   sc_list);
59330fdc8d8SChris Lattner 
59430fdc8d8SChris Lattner   return sc_list.GetSize() - initial_count;
59530fdc8d8SChris Lattner }
59630fdc8d8SChris Lattner 
5971ad655e2SAdrian Prantl void Module::FindGlobalVariables(ConstString name,
59899558cc4SGreg Clayton                                  const CompilerDeclContext *parent_decl_ctx,
5991ad655e2SAdrian Prantl                                  size_t max_matches, VariableList &variables) {
60023f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
6011ad655e2SAdrian Prantl     symbols->FindGlobalVariables(name, parent_decl_ctx, max_matches, variables);
60234cda14bSPavel Labath }
60334cda14bSPavel Labath 
6041ad655e2SAdrian Prantl void Module::FindGlobalVariables(const RegularExpression &regex,
6051ad655e2SAdrian Prantl                                  size_t max_matches, VariableList &variables) {
60623f70e83SPavel Labath   SymbolFile *symbols = GetSymbolFile();
60734cda14bSPavel Labath   if (symbols)
6081ad655e2SAdrian Prantl     symbols->FindGlobalVariables(regex, max_matches, variables);
60930fdc8d8SChris Lattner }
61030fdc8d8SChris Lattner 
6111ad655e2SAdrian Prantl void Module::FindCompileUnits(const FileSpec &path,
612b9c1b51eSKate Stone                               SymbolContextList &sc_list) {
613c7bece56SGreg Clayton   const size_t num_compile_units = GetNumCompileUnits();
614644247c1SGreg Clayton   SymbolContext sc;
615e1cd1be6SGreg Clayton   sc.module_sp = shared_from_this();
616ddd7a2a6SSean Callanan   const bool compare_directory = (bool)path.GetDirectory();
617b9c1b51eSKate Stone   for (size_t i = 0; i < num_compile_units; ++i) {
618644247c1SGreg Clayton     sc.comp_unit = GetCompileUnitAtIndex(i).get();
619b9c1b51eSKate Stone     if (sc.comp_unit) {
620644247c1SGreg Clayton       if (FileSpec::Equal(*sc.comp_unit, path, compare_directory))
621644247c1SGreg Clayton         sc_list.Append(sc);
622644247c1SGreg Clayton     }
6232dafd8edSGreg Clayton   }
624644247c1SGreg Clayton }
625644247c1SGreg Clayton 
6260e4c4821SAdrian Prantl Module::LookupInfo::LookupInfo(ConstString name,
627117b1fa1SZachary Turner                                FunctionNameType name_type_mask,
628117b1fa1SZachary Turner                                LanguageType language)
629117b1fa1SZachary Turner     : m_name(name), m_lookup_name(), m_language(language),
630117b1fa1SZachary Turner       m_name_type_mask(eFunctionNameTypeNone),
631b9c1b51eSKate Stone       m_match_name_after_lookup(false) {
6326234a5c8SGreg Clayton   const char *name_cstr = name.GetCString();
6336234a5c8SGreg Clayton   llvm::StringRef basename;
6346234a5c8SGreg Clayton   llvm::StringRef context;
6356234a5c8SGreg Clayton 
636b9c1b51eSKate Stone   if (name_type_mask & eFunctionNameTypeAuto) {
6376234a5c8SGreg Clayton     if (CPlusPlusLanguage::IsCPPMangledName(name_cstr))
6386234a5c8SGreg Clayton       m_name_type_mask = eFunctionNameTypeFull;
6396234a5c8SGreg Clayton     else if ((language == eLanguageTypeUnknown ||
6406234a5c8SGreg Clayton               Language::LanguageIsObjC(language)) &&
6416234a5c8SGreg Clayton              ObjCLanguage::IsPossibleObjCMethodName(name_cstr))
6426234a5c8SGreg Clayton       m_name_type_mask = eFunctionNameTypeFull;
643b9c1b51eSKate Stone     else if (Language::LanguageIsC(language)) {
6446234a5c8SGreg Clayton       m_name_type_mask = eFunctionNameTypeFull;
645b9c1b51eSKate Stone     } else {
6466234a5c8SGreg Clayton       if ((language == eLanguageTypeUnknown ||
6476234a5c8SGreg Clayton            Language::LanguageIsObjC(language)) &&
6486234a5c8SGreg Clayton           ObjCLanguage::IsPossibleObjCSelector(name_cstr))
6496234a5c8SGreg Clayton         m_name_type_mask |= eFunctionNameTypeSelector;
6506234a5c8SGreg Clayton 
6516234a5c8SGreg Clayton       CPlusPlusLanguage::MethodName cpp_method(name);
6526234a5c8SGreg Clayton       basename = cpp_method.GetBasename();
653b9c1b51eSKate Stone       if (basename.empty()) {
654b9c1b51eSKate Stone         if (CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
655b9c1b51eSKate Stone                                                            basename))
6566234a5c8SGreg Clayton           m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
6576234a5c8SGreg Clayton         else
6586234a5c8SGreg Clayton           m_name_type_mask |= eFunctionNameTypeFull;
659b9c1b51eSKate Stone       } else {
6606234a5c8SGreg Clayton         m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
6616234a5c8SGreg Clayton       }
6626234a5c8SGreg Clayton     }
663b9c1b51eSKate Stone   } else {
6646234a5c8SGreg Clayton     m_name_type_mask = name_type_mask;
665b9c1b51eSKate Stone     if (name_type_mask & eFunctionNameTypeMethod ||
666b9c1b51eSKate Stone         name_type_mask & eFunctionNameTypeBase) {
667b9c1b51eSKate Stone       // If they've asked for a CPP method or function name and it can't be
66805097246SAdrian Prantl       // that, we don't even need to search for CPP methods or names.
6696234a5c8SGreg Clayton       CPlusPlusLanguage::MethodName cpp_method(name);
670b9c1b51eSKate Stone       if (cpp_method.IsValid()) {
6716234a5c8SGreg Clayton         basename = cpp_method.GetBasename();
6726234a5c8SGreg Clayton 
673b9c1b51eSKate Stone         if (!cpp_method.GetQualifiers().empty()) {
674b9c1b51eSKate Stone           // There is a "const" or other qualifier following the end of the
67505097246SAdrian Prantl           // function parens, this can't be a eFunctionNameTypeBase
6766234a5c8SGreg Clayton           m_name_type_mask &= ~(eFunctionNameTypeBase);
6776234a5c8SGreg Clayton           if (m_name_type_mask == eFunctionNameTypeNone)
6786234a5c8SGreg Clayton             return;
6796234a5c8SGreg Clayton         }
680b9c1b51eSKate Stone       } else {
681b9c1b51eSKate Stone         // If the CPP method parser didn't manage to chop this up, try to fill
68205097246SAdrian Prantl         // in the base name if we can. If a::b::c is passed in, we need to just
68305097246SAdrian Prantl         // look up "c", and then we'll filter the result later.
684b9c1b51eSKate Stone         CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
685b9c1b51eSKate Stone                                                        basename);
6866234a5c8SGreg Clayton       }
6876234a5c8SGreg Clayton     }
6886234a5c8SGreg Clayton 
689b9c1b51eSKate Stone     if (name_type_mask & eFunctionNameTypeSelector) {
690b9c1b51eSKate Stone       if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr)) {
6916234a5c8SGreg Clayton         m_name_type_mask &= ~(eFunctionNameTypeSelector);
6926234a5c8SGreg Clayton         if (m_name_type_mask == eFunctionNameTypeNone)
6936234a5c8SGreg Clayton           return;
6946234a5c8SGreg Clayton       }
6956234a5c8SGreg Clayton     }
6966234a5c8SGreg Clayton 
697b9c1b51eSKate Stone     // Still try and get a basename in case someone specifies a name type mask
6985d0c1146SGreg Clayton     // of eFunctionNameTypeFull and a name like "A::func"
699b9c1b51eSKate Stone     if (basename.empty()) {
7005d0c1146SGreg Clayton       if (name_type_mask & eFunctionNameTypeFull &&
7015d0c1146SGreg Clayton           !CPlusPlusLanguage::IsCPPMangledName(name_cstr)) {
7026234a5c8SGreg Clayton         CPlusPlusLanguage::MethodName cpp_method(name);
7036234a5c8SGreg Clayton         basename = cpp_method.GetBasename();
7046234a5c8SGreg Clayton         if (basename.empty())
705b9c1b51eSKate Stone           CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
706b9c1b51eSKate Stone                                                          basename);
7076234a5c8SGreg Clayton       }
7086234a5c8SGreg Clayton     }
7096234a5c8SGreg Clayton   }
7106234a5c8SGreg Clayton 
711b9c1b51eSKate Stone   if (!basename.empty()) {
71205097246SAdrian Prantl     // The name supplied was a partial C++ path like "a::count". In this case
71305097246SAdrian Prantl     // we want to do a lookup on the basename "count" and then make sure any
71405097246SAdrian Prantl     // matching results contain "a::count" so that it would match "b::a::count"
71505097246SAdrian Prantl     // and "a::count". This is why we set "match_name_after_lookup" to true
7166234a5c8SGreg Clayton     m_lookup_name.SetString(basename);
7176234a5c8SGreg Clayton     m_match_name_after_lookup = true;
718b9c1b51eSKate Stone   } else {
719b9c1b51eSKate Stone     // The name is already correct, just use the exact name as supplied, and we
72005097246SAdrian Prantl     // won't need to check if any matches contain "name"
7216234a5c8SGreg Clayton     m_lookup_name = name;
7226234a5c8SGreg Clayton     m_match_name_after_lookup = false;
7236234a5c8SGreg Clayton   }
7246234a5c8SGreg Clayton }
7256234a5c8SGreg Clayton 
726b9c1b51eSKate Stone void Module::LookupInfo::Prune(SymbolContextList &sc_list,
727b9c1b51eSKate Stone                                size_t start_idx) const {
728b9c1b51eSKate Stone   if (m_match_name_after_lookup && m_name) {
7296234a5c8SGreg Clayton     SymbolContext sc;
7306234a5c8SGreg Clayton     size_t i = start_idx;
731b9c1b51eSKate Stone     while (i < sc_list.GetSize()) {
7326234a5c8SGreg Clayton       if (!sc_list.GetContextAtIndex(i, sc))
7336234a5c8SGreg Clayton         break;
7346234a5c8SGreg Clayton       ConstString full_name(sc.GetFunctionName());
735b9c1b51eSKate Stone       if (full_name &&
736b9c1b51eSKate Stone           ::strstr(full_name.GetCString(), m_name.GetCString()) == nullptr) {
7376234a5c8SGreg Clayton         sc_list.RemoveContextAtIndex(i);
738b9c1b51eSKate Stone       } else {
7396234a5c8SGreg Clayton         ++i;
7406234a5c8SGreg Clayton       }
7416234a5c8SGreg Clayton     }
7426234a5c8SGreg Clayton   }
7436234a5c8SGreg Clayton 
744b9c1b51eSKate Stone   // If we have only full name matches we might have tried to set breakpoint on
7455d0c1146SGreg Clayton   // "func" and specified eFunctionNameTypeFull, but we might have found
7465d0c1146SGreg Clayton   // "a::func()", "a::b::func()", "c::func()", "func()" and "func". Only
7475d0c1146SGreg Clayton   // "func()" and "func" should end up matching.
748b9c1b51eSKate Stone   if (m_name_type_mask == eFunctionNameTypeFull) {
7496234a5c8SGreg Clayton     SymbolContext sc;
7506234a5c8SGreg Clayton     size_t i = start_idx;
751b9c1b51eSKate Stone     while (i < sc_list.GetSize()) {
7526234a5c8SGreg Clayton       if (!sc_list.GetContextAtIndex(i, sc))
7536234a5c8SGreg Clayton         break;
75405097246SAdrian Prantl       // Make sure the mangled and demangled names don't match before we try to
75505097246SAdrian Prantl       // pull anything out
7565d0c1146SGreg Clayton       ConstString mangled_name(sc.GetFunctionName(Mangled::ePreferMangled));
7576234a5c8SGreg Clayton       ConstString full_name(sc.GetFunctionName());
7585d0c1146SGreg Clayton       if (mangled_name != m_name && full_name != m_name)
7595d0c1146SGreg Clayton       {
7606234a5c8SGreg Clayton         CPlusPlusLanguage::MethodName cpp_method(full_name);
761b9c1b51eSKate Stone         if (cpp_method.IsValid()) {
762b9c1b51eSKate Stone           if (cpp_method.GetContext().empty()) {
763b9c1b51eSKate Stone             if (cpp_method.GetBasename().compare(m_name.GetStringRef()) != 0) {
7646234a5c8SGreg Clayton               sc_list.RemoveContextAtIndex(i);
7656234a5c8SGreg Clayton               continue;
7666234a5c8SGreg Clayton             }
767b9c1b51eSKate Stone           } else {
7685d0c1146SGreg Clayton             std::string qualified_name;
7695d0c1146SGreg Clayton             llvm::StringRef anon_prefix("(anonymous namespace)");
7705d0c1146SGreg Clayton             if (cpp_method.GetContext() == anon_prefix)
7715d0c1146SGreg Clayton               qualified_name = cpp_method.GetBasename().str();
7725d0c1146SGreg Clayton             else
7735d0c1146SGreg Clayton               qualified_name = cpp_method.GetScopeQualifiedName();
7748d20cfdfSJonas Devlieghere             if (qualified_name != m_name.GetCString()) {
7756234a5c8SGreg Clayton               sc_list.RemoveContextAtIndex(i);
7766234a5c8SGreg Clayton               continue;
7776234a5c8SGreg Clayton             }
7786234a5c8SGreg Clayton           }
7796234a5c8SGreg Clayton         }
7805d0c1146SGreg Clayton       }
7816234a5c8SGreg Clayton       ++i;
7826234a5c8SGreg Clayton     }
7836234a5c8SGreg Clayton   }
7846234a5c8SGreg Clayton }
7856234a5c8SGreg Clayton 
7861ad655e2SAdrian Prantl void Module::FindFunctions(ConstString name,
78799558cc4SGreg Clayton                            const CompilerDeclContext *parent_decl_ctx,
788117b1fa1SZachary Turner                            FunctionNameType name_type_mask,
789117b1fa1SZachary Turner                            bool include_symbols, bool include_inlines,
7901ad655e2SAdrian Prantl                            SymbolContextList &sc_list) {
79143fe217bSGreg Clayton   const size_t old_size = sc_list.GetSize();
792931180e6SGreg Clayton 
793931180e6SGreg Clayton   // Find all the functions (not symbols, but debug information functions...
79423f70e83SPavel Labath   SymbolFile *symbols = GetSymbolFile();
79543fe217bSGreg Clayton 
796b9c1b51eSKate Stone   if (name_type_mask & eFunctionNameTypeAuto) {
7976234a5c8SGreg Clayton     LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
79843fe217bSGreg Clayton 
799b9c1b51eSKate Stone     if (symbols) {
800b9c1b51eSKate Stone       symbols->FindFunctions(lookup_info.GetLookupName(), parent_decl_ctx,
801b9c1b51eSKate Stone                              lookup_info.GetNameTypeMask(), include_inlines,
8021ad655e2SAdrian Prantl                              sc_list);
80343fe217bSGreg Clayton 
804b9c1b51eSKate Stone       // Now check our symbol table for symbols that are code symbols if
805b9c1b51eSKate Stone       // requested
806b9c1b51eSKate Stone       if (include_symbols) {
807a7499c98SMichael Sartain         Symtab *symtab = symbols->GetSymtab();
80843fe217bSGreg Clayton         if (symtab)
809b9c1b51eSKate Stone           symtab->FindFunctionSymbols(lookup_info.GetLookupName(),
810b9c1b51eSKate Stone                                       lookup_info.GetNameTypeMask(), sc_list);
81143fe217bSGreg Clayton       }
81243fe217bSGreg Clayton     }
81343fe217bSGreg Clayton 
8146234a5c8SGreg Clayton     const size_t new_size = sc_list.GetSize();
8156234a5c8SGreg Clayton 
8166234a5c8SGreg Clayton     if (old_size < new_size)
8176234a5c8SGreg Clayton       lookup_info.Prune(sc_list, old_size);
818b9c1b51eSKate Stone   } else {
819b9c1b51eSKate Stone     if (symbols) {
820b9c1b51eSKate Stone       symbols->FindFunctions(name, parent_decl_ctx, name_type_mask,
8211ad655e2SAdrian Prantl                              include_inlines, sc_list);
822931180e6SGreg Clayton 
823b9c1b51eSKate Stone       // Now check our symbol table for symbols that are code symbols if
824b9c1b51eSKate Stone       // requested
825b9c1b51eSKate Stone       if (include_symbols) {
826a7499c98SMichael Sartain         Symtab *symtab = symbols->GetSymtab();
827931180e6SGreg Clayton         if (symtab)
82843fe217bSGreg Clayton           symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
829931180e6SGreg Clayton       }
830931180e6SGreg Clayton     }
831931180e6SGreg Clayton   }
83230fdc8d8SChris Lattner }
83330fdc8d8SChris Lattner 
8341ad655e2SAdrian Prantl void Module::FindFunctions(const RegularExpression &regex, bool include_symbols,
8351ad655e2SAdrian Prantl                            bool include_inlines,
8361ad655e2SAdrian Prantl                            SymbolContextList &sc_list) {
837c7bece56SGreg Clayton   const size_t start_size = sc_list.GetSize();
838931180e6SGreg Clayton 
83923f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile()) {
8401ad655e2SAdrian Prantl     symbols->FindFunctions(regex, include_inlines, sc_list);
841a7499c98SMichael Sartain 
84205097246SAdrian Prantl     // Now check our symbol table for symbols that are code symbols if
84305097246SAdrian Prantl     // requested
844b9c1b51eSKate Stone     if (include_symbols) {
845a7499c98SMichael Sartain       Symtab *symtab = symbols->GetSymtab();
846b9c1b51eSKate Stone       if (symtab) {
847931180e6SGreg Clayton         std::vector<uint32_t> symbol_indexes;
848b9c1b51eSKate Stone         symtab->AppendSymbolIndexesMatchingRegExAndType(
849b9c1b51eSKate Stone             regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny,
850b9c1b51eSKate Stone             symbol_indexes);
851c7bece56SGreg Clayton         const size_t num_matches = symbol_indexes.size();
852b9c1b51eSKate Stone         if (num_matches) {
853931180e6SGreg Clayton           SymbolContext sc(this);
854d8cf1a11SGreg Clayton           const size_t end_functions_added_index = sc_list.GetSize();
855b9c1b51eSKate Stone           size_t num_functions_added_to_sc_list =
856b9c1b51eSKate Stone               end_functions_added_index - start_size;
857b9c1b51eSKate Stone           if (num_functions_added_to_sc_list == 0) {
85805097246SAdrian Prantl             // No functions were added, just symbols, so we can just append
85905097246SAdrian Prantl             // them
860b9c1b51eSKate Stone             for (size_t i = 0; i < num_matches; ++i) {
861931180e6SGreg Clayton               sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
86200049b8bSMatt Kopec               SymbolType sym_type = sc.symbol->GetType();
86300049b8bSMatt Kopec               if (sc.symbol && (sym_type == eSymbolTypeCode ||
86400049b8bSMatt Kopec                                 sym_type == eSymbolTypeResolver))
865d8cf1a11SGreg Clayton                 sc_list.Append(sc);
866d8cf1a11SGreg Clayton             }
867b9c1b51eSKate Stone           } else {
868d8cf1a11SGreg Clayton             typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
869d8cf1a11SGreg Clayton             FileAddrToIndexMap file_addr_to_index;
870b9c1b51eSKate Stone             for (size_t i = start_size; i < end_functions_added_index; ++i) {
871d8cf1a11SGreg Clayton               const SymbolContext &sc = sc_list[i];
872d8cf1a11SGreg Clayton               if (sc.block)
873d8cf1a11SGreg Clayton                 continue;
874b9c1b51eSKate Stone               file_addr_to_index[sc.function->GetAddressRange()
875b9c1b51eSKate Stone                                      .GetBaseAddress()
876b9c1b51eSKate Stone                                      .GetFileAddress()] = i;
877d8cf1a11SGreg Clayton             }
878d8cf1a11SGreg Clayton 
879d8cf1a11SGreg Clayton             FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
880d8cf1a11SGreg Clayton             // Functions were added so we need to merge symbols into any
881d8cf1a11SGreg Clayton             // existing function symbol contexts
882b9c1b51eSKate Stone             for (size_t i = start_size; i < num_matches; ++i) {
883d8cf1a11SGreg Clayton               sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
884d8cf1a11SGreg Clayton               SymbolType sym_type = sc.symbol->GetType();
885b9c1b51eSKate Stone               if (sc.symbol && sc.symbol->ValueIsAddress() &&
886b9c1b51eSKate Stone                   (sym_type == eSymbolTypeCode ||
887b9c1b51eSKate Stone                    sym_type == eSymbolTypeResolver)) {
888b9c1b51eSKate Stone                 FileAddrToIndexMap::const_iterator pos =
889b9c1b51eSKate Stone                     file_addr_to_index.find(
890b9c1b51eSKate Stone                         sc.symbol->GetAddressRef().GetFileAddress());
891d8cf1a11SGreg Clayton                 if (pos == end)
892d8cf1a11SGreg Clayton                   sc_list.Append(sc);
893d8cf1a11SGreg Clayton                 else
894d8cf1a11SGreg Clayton                   sc_list[pos->second].symbol = sc.symbol;
895d8cf1a11SGreg Clayton               }
896d8cf1a11SGreg Clayton             }
897931180e6SGreg Clayton           }
898931180e6SGreg Clayton         }
899931180e6SGreg Clayton       }
900931180e6SGreg Clayton     }
901931180e6SGreg Clayton   }
90230fdc8d8SChris Lattner }
90330fdc8d8SChris Lattner 
904b9c1b51eSKate Stone void Module::FindAddressesForLine(const lldb::TargetSP target_sp,
905f86248d9SRichard Mitton                                   const FileSpec &file, uint32_t line,
906f86248d9SRichard Mitton                                   Function *function,
907b9c1b51eSKate Stone                                   std::vector<Address> &output_local,
908b9c1b51eSKate Stone                                   std::vector<Address> &output_extern) {
909f86248d9SRichard Mitton   SearchFilterByModule filter(target_sp, m_file);
910f86248d9SRichard Mitton   AddressResolverFileLine resolver(file, line, true);
911f86248d9SRichard Mitton   resolver.ResolveAddress(filter);
912f86248d9SRichard Mitton 
913b9c1b51eSKate Stone   for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++) {
914f86248d9SRichard Mitton     Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
915f86248d9SRichard Mitton     Function *f = addr.CalculateSymbolContextFunction();
916f86248d9SRichard Mitton     if (f && f == function)
917f86248d9SRichard Mitton       output_local.push_back(addr);
918f86248d9SRichard Mitton     else
919f86248d9SRichard Mitton       output_extern.push_back(addr);
920f86248d9SRichard Mitton   }
921f86248d9SRichard Mitton }
922f86248d9SRichard Mitton 
923bf9d84c0SAdrian Prantl void Module::FindTypes_Impl(
9240e4c4821SAdrian Prantl     ConstString name, const CompilerDeclContext *parent_decl_ctx,
925d4d428efSAdrian Prantl     size_t max_matches,
926ae088e52SGreg Clayton     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
927b9c1b51eSKate Stone     TypeMap &types) {
928f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
929f9d16476SPavel Labath   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
93023f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
931bf9d84c0SAdrian Prantl     symbols->FindTypes(name, parent_decl_ctx, max_matches,
932b9c1b51eSKate Stone                        searched_symbol_files, types);
9333504eee8SGreg Clayton }
9343504eee8SGreg Clayton 
935bf9d84c0SAdrian Prantl void Module::FindTypesInNamespace(ConstString type_name,
93699558cc4SGreg Clayton                                   const CompilerDeclContext *parent_decl_ctx,
937b9c1b51eSKate Stone                                   size_t max_matches, TypeList &type_list) {
9384069730cSRavitheja Addepally   TypeMap types_map;
939ae088e52SGreg Clayton   llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
940bf9d84c0SAdrian Prantl   FindTypes_Impl(type_name, parent_decl_ctx, max_matches, searched_symbol_files,
941bf9d84c0SAdrian Prantl                  types_map);
942bf9d84c0SAdrian Prantl   if (types_map.GetSize()) {
943576495e6SZachary Turner     SymbolContext sc;
944576495e6SZachary Turner     sc.module_sp = shared_from_this();
9454069730cSRavitheja Addepally     sc.SortTypeList(types_map, type_list);
946576495e6SZachary Turner   }
9476f3533fbSEnrico Granata }
9486f3533fbSEnrico Granata 
949b9c1b51eSKate Stone lldb::TypeSP Module::FindFirstType(const SymbolContext &sc,
9500e4c4821SAdrian Prantl                                    ConstString name, bool exact_match) {
951b43165b7SGreg Clayton   TypeList type_list;
952ae088e52SGreg Clayton   llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
953576495e6SZachary Turner   FindTypes(name, exact_match, 1, searched_symbol_files, type_list);
954bf9d84c0SAdrian Prantl   if (type_list.GetSize())
955b43165b7SGreg Clayton     return type_list.GetTypeAtIndex(0);
956b43165b7SGreg Clayton   return TypeSP();
957b43165b7SGreg Clayton }
958b43165b7SGreg Clayton 
959bf9d84c0SAdrian Prantl void Module::FindTypes(
9600e4c4821SAdrian Prantl     ConstString name, bool exact_match, size_t max_matches,
961ae088e52SGreg Clayton     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
962b9c1b51eSKate Stone     TypeList &types) {
96384db9105SGreg Clayton   const char *type_name_cstr = name.GetCString();
964556b1611STamas Berghammer   llvm::StringRef type_scope;
965556b1611STamas Berghammer   llvm::StringRef type_basename;
9667bc31332SGreg Clayton   TypeClass type_class = eTypeClassAny;
9674069730cSRavitheja Addepally   TypeMap typesmap;
9681739b7d0SFrederic Riss 
969b9c1b51eSKate Stone   if (Type::GetTypeScopeAndBasename(type_name_cstr, type_scope, type_basename,
970b9c1b51eSKate Stone                                     type_class)) {
97184db9105SGreg Clayton     // Check if "name" starts with "::" which means the qualified type starts
97284db9105SGreg Clayton     // from the root namespace and implies and exact match. The typenames we
97384db9105SGreg Clayton     // get back from clang do not start with "::" so we need to strip this off
974d93c4a33SBruce Mitchener     // in order to get the qualified names to match
975556b1611STamas Berghammer     exact_match = type_scope.consume_front("::");
9766f3533fbSEnrico Granata 
977556b1611STamas Berghammer     ConstString type_basename_const_str(type_basename);
978bf9d84c0SAdrian Prantl     FindTypes_Impl(type_basename_const_str, nullptr, max_matches,
979bf9d84c0SAdrian Prantl                    searched_symbol_files, typesmap);
980bf9d84c0SAdrian Prantl     if (typesmap.GetSize())
981b9c1b51eSKate Stone       typesmap.RemoveMismatchedTypes(type_scope, type_basename, type_class,
982b9c1b51eSKate Stone                                      exact_match);
983b9c1b51eSKate Stone   } else {
984b9c1b51eSKate Stone     // The type is not in a namespace/class scope, just search for it by
985b9c1b51eSKate Stone     // basename
986c485f056SGreg Clayton     if (type_class != eTypeClassAny && !type_basename.empty()) {
987b9c1b51eSKate Stone       // The "type_name_cstr" will have been modified if we have a valid type
98805097246SAdrian Prantl       // class prefix (like "struct", "class", "union", "typedef" etc).
989d4d428efSAdrian Prantl       FindTypes_Impl(ConstString(type_basename), nullptr, UINT_MAX,
990576495e6SZachary Turner                      searched_symbol_files, typesmap);
991c485f056SGreg Clayton       typesmap.RemoveMismatchedTypes(type_scope, type_basename, type_class,
992c485f056SGreg Clayton                                      exact_match);
993b9c1b51eSKate Stone     } else {
994bf9d84c0SAdrian Prantl       FindTypes_Impl(name, nullptr, UINT_MAX, searched_symbol_files, typesmap);
995c485f056SGreg Clayton       if (exact_match) {
996c485f056SGreg Clayton         std::string name_str(name.AsCString(""));
997c485f056SGreg Clayton         typesmap.RemoveMismatchedTypes(type_scope, name_str, type_class,
998c485f056SGreg Clayton                                        exact_match);
999c485f056SGreg Clayton       }
100084db9105SGreg Clayton     }
10017bc31332SGreg Clayton   }
1002bf9d84c0SAdrian Prantl   if (typesmap.GetSize()) {
1003576495e6SZachary Turner     SymbolContext sc;
1004576495e6SZachary Turner     sc.module_sp = shared_from_this();
10054069730cSRavitheja Addepally     sc.SortTypeList(typesmap, types);
1006576495e6SZachary Turner   }
10076f3533fbSEnrico Granata }
10086f3533fbSEnrico Granata 
1009*3b73dcdcSAdrian Prantl void Module::FindTypes(
1010*3b73dcdcSAdrian Prantl     llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages,
1011*3b73dcdcSAdrian Prantl     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1012*3b73dcdcSAdrian Prantl     TypeMap &types) {
1013aa97a89dSAdrian Prantl   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1014aa97a89dSAdrian Prantl   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
1015aa97a89dSAdrian Prantl   if (SymbolFile *symbols = GetSymbolFile())
1016*3b73dcdcSAdrian Prantl     symbols->FindTypes(pattern, languages, searched_symbol_files, types);
1017aa97a89dSAdrian Prantl }
1018aa97a89dSAdrian Prantl 
1019579d6d1aSPavel Labath SymbolFile *Module::GetSymbolFile(bool can_create, Stream *feedback_strm) {
1020579d6d1aSPavel Labath   if (!m_did_load_symfile.load()) {
102116ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1022579d6d1aSPavel Labath     if (!m_did_load_symfile.load() && can_create) {
102330fdc8d8SChris Lattner       ObjectFile *obj_file = GetObjectFile();
1024b9c1b51eSKate Stone       if (obj_file != nullptr) {
1025f9d16476SPavel Labath         static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1026f9d16476SPavel Labath         Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
1027d5b44036SJonas Devlieghere         m_symfile_up.reset(
1028b9c1b51eSKate Stone             SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
1029579d6d1aSPavel Labath         m_did_load_symfile = true;
103030fdc8d8SChris Lattner       }
103130fdc8d8SChris Lattner     }
103288c05f54SGreg Clayton   }
1033579d6d1aSPavel Labath   return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr;
103423f70e83SPavel Labath }
103523f70e83SPavel Labath 
1036d5d47a35SPavel Labath Symtab *Module::GetSymtab() {
1037d5d47a35SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
1038d5d47a35SPavel Labath     return symbols->GetSymtab();
1039d5d47a35SPavel Labath   return nullptr;
1040d5d47a35SPavel Labath }
1041d5d47a35SPavel Labath 
1042b9c1b51eSKate Stone void Module::SetFileSpecAndObjectName(const FileSpec &file,
10430e4c4821SAdrian Prantl                                       ConstString object_name) {
104405097246SAdrian Prantl   // Container objects whose paths do not specify a file directly can call this
104505097246SAdrian Prantl   // function to correct the file and object names.
104630fdc8d8SChris Lattner   m_file = file;
104746376966SJonas Devlieghere   m_mod_time = FileSystem::Instance().GetModificationTime(file);
104830fdc8d8SChris Lattner   m_object_name = object_name;
104930fdc8d8SChris Lattner }
105030fdc8d8SChris Lattner 
1051b9c1b51eSKate Stone const ArchSpec &Module::GetArchitecture() const { return m_arch; }
105230fdc8d8SChris Lattner 
1053b9c1b51eSKate Stone std::string Module::GetSpecificationDescription() const {
1054b5ad4ec7SGreg Clayton   std::string spec(GetFileSpec().GetPath());
1055b9c1b51eSKate Stone   if (m_object_name) {
1056b5ad4ec7SGreg Clayton     spec += '(';
1057b5ad4ec7SGreg Clayton     spec += m_object_name.GetCString();
1058b5ad4ec7SGreg Clayton     spec += ')';
1059b5ad4ec7SGreg Clayton   }
1060b5ad4ec7SGreg Clayton   return spec;
1061b5ad4ec7SGreg Clayton }
1062b5ad4ec7SGreg Clayton 
1063b9c1b51eSKate Stone void Module::GetDescription(Stream *s, lldb::DescriptionLevel level) {
106416ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1065ceb6b139SCaroline Tice 
1066b9c1b51eSKate Stone   if (level >= eDescriptionLevelFull) {
1067cfd1acedSGreg Clayton     if (m_arch.IsValid())
106864195a2cSGreg Clayton       s->Printf("(%s) ", m_arch.GetArchitectureName());
1069c982b3d6SGreg Clayton   }
1070ceb6b139SCaroline Tice 
1071b9c1b51eSKate Stone   if (level == eDescriptionLevelBrief) {
1072c982b3d6SGreg Clayton     const char *filename = m_file.GetFilename().GetCString();
1073c982b3d6SGreg Clayton     if (filename)
1074c982b3d6SGreg Clayton       s->PutCString(filename);
1075b9c1b51eSKate Stone   } else {
1076cfd1acedSGreg Clayton     char path[PATH_MAX];
1077cfd1acedSGreg Clayton     if (m_file.GetPath(path, sizeof(path)))
1078cfd1acedSGreg Clayton       s->PutCString(path);
1079c982b3d6SGreg Clayton   }
1080cfd1acedSGreg Clayton 
1081cfd1acedSGreg Clayton   const char *object_name = m_object_name.GetCString();
1082cfd1acedSGreg Clayton   if (object_name)
1083cfd1acedSGreg Clayton     s->Printf("(%s)", object_name);
1084ceb6b139SCaroline Tice }
1085ceb6b139SCaroline Tice 
1086b9c1b51eSKate Stone void Module::ReportError(const char *format, ...) {
1087b9c1b51eSKate Stone   if (format && format[0]) {
1088e38a5eddSGreg Clayton     StreamString strm;
1089e38a5eddSGreg Clayton     strm.PutCString("error: ");
1090e38a5eddSGreg Clayton     GetDescription(&strm, lldb::eDescriptionLevelBrief);
10918b35334eSGreg Clayton     strm.PutChar(' ');
1092c982b3d6SGreg Clayton     va_list args;
1093c982b3d6SGreg Clayton     va_start(args, format);
1094e38a5eddSGreg Clayton     strm.PrintfVarArg(format, args);
1095c982b3d6SGreg Clayton     va_end(args);
1096e38a5eddSGreg Clayton 
1097e38a5eddSGreg Clayton     const int format_len = strlen(format);
1098b9c1b51eSKate Stone     if (format_len > 0) {
1099e38a5eddSGreg Clayton       const char last_char = format[format_len - 1];
1100376230c9SRaphael Isemann       if (last_char != '\n' && last_char != '\r')
1101e38a5eddSGreg Clayton         strm.EOL();
1102e38a5eddSGreg Clayton     }
1103c156427dSZachary Turner     Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData());
1104e38a5eddSGreg Clayton   }
1105e38a5eddSGreg Clayton }
1106e38a5eddSGreg Clayton 
1107b9c1b51eSKate Stone bool Module::FileHasChanged() const {
1108c5dac77aSEugene Zelenko   if (!m_file_has_changed)
11091408bf72SPavel Labath     m_file_has_changed =
111046376966SJonas Devlieghere         (FileSystem::Instance().GetModificationTime(m_file) != m_mod_time);
11111d60909eSGreg Clayton   return m_file_has_changed;
11121d60909eSGreg Clayton }
11131d60909eSGreg Clayton 
1114b9c1b51eSKate Stone void Module::ReportErrorIfModifyDetected(const char *format, ...) {
1115b9c1b51eSKate Stone   if (!m_first_file_changed_log) {
1116b9c1b51eSKate Stone     if (FileHasChanged()) {
11171d60909eSGreg Clayton       m_first_file_changed_log = true;
1118b9c1b51eSKate Stone       if (format) {
1119e38a5eddSGreg Clayton         StreamString strm;
1120e38a5eddSGreg Clayton         strm.PutCString("error: the object file ");
1121e38a5eddSGreg Clayton         GetDescription(&strm, lldb::eDescriptionLevelFull);
1122e38a5eddSGreg Clayton         strm.PutCString(" has been modified\n");
1123e38a5eddSGreg Clayton 
1124e38a5eddSGreg Clayton         va_list args;
1125e38a5eddSGreg Clayton         va_start(args, format);
1126e38a5eddSGreg Clayton         strm.PrintfVarArg(format, args);
1127e38a5eddSGreg Clayton         va_end(args);
1128e38a5eddSGreg Clayton 
1129e38a5eddSGreg Clayton         const int format_len = strlen(format);
1130b9c1b51eSKate Stone         if (format_len > 0) {
1131e38a5eddSGreg Clayton           const char last_char = format[format_len - 1];
1132376230c9SRaphael Isemann           if (last_char != '\n' && last_char != '\r')
1133e38a5eddSGreg Clayton             strm.EOL();
1134e38a5eddSGreg Clayton         }
1135b9c1b51eSKate Stone         strm.PutCString("The debug session should be aborted as the original "
1136b9c1b51eSKate Stone                         "debug information has been overwritten.\n");
1137c156427dSZachary Turner         Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData());
1138e38a5eddSGreg Clayton       }
1139e38a5eddSGreg Clayton     }
1140c982b3d6SGreg Clayton   }
11411d60909eSGreg Clayton }
1142c982b3d6SGreg Clayton 
1143b9c1b51eSKate Stone void Module::ReportWarning(const char *format, ...) {
1144b9c1b51eSKate Stone   if (format && format[0]) {
1145e38a5eddSGreg Clayton     StreamString strm;
1146e38a5eddSGreg Clayton     strm.PutCString("warning: ");
11478b35334eSGreg Clayton     GetDescription(&strm, lldb::eDescriptionLevelFull);
11488b35334eSGreg Clayton     strm.PutChar(' ');
1149c982b3d6SGreg Clayton 
1150c982b3d6SGreg Clayton     va_list args;
1151c982b3d6SGreg Clayton     va_start(args, format);
1152e38a5eddSGreg Clayton     strm.PrintfVarArg(format, args);
1153c982b3d6SGreg Clayton     va_end(args);
1154e38a5eddSGreg Clayton 
1155e38a5eddSGreg Clayton     const int format_len = strlen(format);
1156b9c1b51eSKate Stone     if (format_len > 0) {
1157e38a5eddSGreg Clayton       const char last_char = format[format_len - 1];
1158376230c9SRaphael Isemann       if (last_char != '\n' && last_char != '\r')
1159e38a5eddSGreg Clayton         strm.EOL();
1160e38a5eddSGreg Clayton     }
1161c156427dSZachary Turner     Host::SystemLog(Host::eSystemLogWarning, "%s", strm.GetData());
1162e38a5eddSGreg Clayton   }
1163c982b3d6SGreg Clayton }
1164c982b3d6SGreg Clayton 
1165b9c1b51eSKate Stone void Module::LogMessage(Log *log, const char *format, ...) {
1166b9c1b51eSKate Stone   if (log != nullptr) {
1167c982b3d6SGreg Clayton     StreamString log_message;
11688b35334eSGreg Clayton     GetDescription(&log_message, lldb::eDescriptionLevelFull);
1169c982b3d6SGreg Clayton     log_message.PutCString(": ");
1170c982b3d6SGreg Clayton     va_list args;
1171c982b3d6SGreg Clayton     va_start(args, format);
1172c982b3d6SGreg Clayton     log_message.PrintfVarArg(format, args);
1173c982b3d6SGreg Clayton     va_end(args);
1174c156427dSZachary Turner     log->PutCString(log_message.GetData());
1175c982b3d6SGreg Clayton   }
1176c982b3d6SGreg Clayton }
1177c982b3d6SGreg Clayton 
1178b9c1b51eSKate Stone void Module::LogMessageVerboseBacktrace(Log *log, const char *format, ...) {
1179b9c1b51eSKate Stone   if (log != nullptr) {
1180d61c0fc0SGreg Clayton     StreamString log_message;
1181d61c0fc0SGreg Clayton     GetDescription(&log_message, lldb::eDescriptionLevelFull);
1182d61c0fc0SGreg Clayton     log_message.PutCString(": ");
1183d61c0fc0SGreg Clayton     va_list args;
1184d61c0fc0SGreg Clayton     va_start(args, format);
1185d61c0fc0SGreg Clayton     log_message.PrintfVarArg(format, args);
1186d61c0fc0SGreg Clayton     va_end(args);
1187b9c1b51eSKate Stone     if (log->GetVerbose()) {
1188a893d301SZachary Turner       std::string back_trace;
1189a893d301SZachary Turner       llvm::raw_string_ostream stream(back_trace);
1190a893d301SZachary Turner       llvm::sys::PrintStackTrace(stream);
1191771ef6d4SMalcolm Parsons       log_message.PutCString(back_trace);
1192a893d301SZachary Turner     }
1193c156427dSZachary Turner     log->PutCString(log_message.GetData());
1194d61c0fc0SGreg Clayton   }
1195d61c0fc0SGreg Clayton }
1196d61c0fc0SGreg Clayton 
1197b9c1b51eSKate Stone void Module::Dump(Stream *s) {
119816ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
11998941142aSGreg Clayton   // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
120030fdc8d8SChris Lattner   s->Indent();
1201b9c1b51eSKate Stone   s->Printf("Module %s%s%s%s\n", m_file.GetPath().c_str(),
120230fdc8d8SChris Lattner             m_object_name ? "(" : "",
120330fdc8d8SChris Lattner             m_object_name ? m_object_name.GetCString() : "",
120430fdc8d8SChris Lattner             m_object_name ? ")" : "");
120530fdc8d8SChris Lattner 
120630fdc8d8SChris Lattner   s->IndentMore();
120730fdc8d8SChris Lattner 
1208a7499c98SMichael Sartain   ObjectFile *objfile = GetObjectFile();
120930fdc8d8SChris Lattner   if (objfile)
121030fdc8d8SChris Lattner     objfile->Dump(s);
121130fdc8d8SChris Lattner 
1212d5d47a35SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
1213d5d47a35SPavel Labath     symbols->Dump(*s);
121430fdc8d8SChris Lattner 
121530fdc8d8SChris Lattner   s->IndentLess();
121630fdc8d8SChris Lattner }
121730fdc8d8SChris Lattner 
12180e4c4821SAdrian Prantl ConstString Module::GetObjectName() const { return m_object_name; }
121930fdc8d8SChris Lattner 
1220b9c1b51eSKate Stone ObjectFile *Module::GetObjectFile() {
1221b9c1b51eSKate Stone   if (!m_did_load_objfile.load()) {
122216ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1223b9c1b51eSKate Stone     if (!m_did_load_objfile.load()) {
1224f9d16476SPavel Labath       static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1225f9d16476SPavel Labath       Timer scoped_timer(func_cat, "Module::GetObjectFile () module = %s",
1226b9c1b51eSKate Stone                          GetFileSpec().GetFilename().AsCString(""));
12275ce9c565SGreg Clayton       DataBufferSP data_sp;
12285ce9c565SGreg Clayton       lldb::offset_t data_offset = 0;
122959b78bcbSJonas Devlieghere       const lldb::offset_t file_size =
123059b78bcbSJonas Devlieghere           FileSystem::Instance().GetByteSize(m_file);
1231b9c1b51eSKate Stone       if (file_size > m_object_offset) {
12322540a8a7SGreg Clayton         m_did_load_objfile = true;
1233b9c1b51eSKate Stone         m_objfile_sp = ObjectFile::FindPlugin(
1234b9c1b51eSKate Stone             shared_from_this(), &m_file, m_object_offset,
1235b9c1b51eSKate Stone             file_size - m_object_offset, data_sp, data_offset);
1236b9c1b51eSKate Stone         if (m_objfile_sp) {
1237b9c1b51eSKate Stone           // Once we get the object file, update our module with the object
123805097246SAdrian Prantl           // file's architecture since it might differ in vendor/os if some
123905097246SAdrian Prantl           // parts were unknown.  But since the matching arch might already be
124005097246SAdrian Prantl           // more specific than the generic COFF architecture, only merge in
124105097246SAdrian Prantl           // those values that overwrite unspecified unknown values.
1242f760f5aeSPavel Labath           m_arch.MergeFrom(m_objfile_sp->GetArchitecture());
1243b9c1b51eSKate Stone         } else {
1244b9c1b51eSKate Stone           ReportError("failed to load objfile for %s",
1245b9c1b51eSKate Stone                       GetFileSpec().GetPath().c_str());
12460ee56ce6STodd Fiala         }
124730fdc8d8SChris Lattner       }
12482540a8a7SGreg Clayton     }
124988c05f54SGreg Clayton   }
1250762f7135SGreg Clayton   return m_objfile_sp.get();
125130fdc8d8SChris Lattner }
125230fdc8d8SChris Lattner 
1253b9c1b51eSKate Stone SectionList *Module::GetSectionList() {
1254d5b44036SJonas Devlieghere   // Populate m_sections_up with sections from objfile.
1255d5b44036SJonas Devlieghere   if (!m_sections_up) {
12563046e668SGreg Clayton     ObjectFile *obj_file = GetObjectFile();
1257c5dac77aSEugene Zelenko     if (obj_file != nullptr)
12583046e668SGreg Clayton       obj_file->CreateSections(*GetUnifiedSectionList());
12593046e668SGreg Clayton   }
1260d5b44036SJonas Devlieghere   return m_sections_up.get();
12613046e668SGreg Clayton }
12623046e668SGreg Clayton 
1263b9c1b51eSKate Stone void Module::SectionFileAddressesChanged() {
126405a09c67SJason Molenda   ObjectFile *obj_file = GetObjectFile();
126505a09c67SJason Molenda   if (obj_file)
126605a09c67SJason Molenda     obj_file->SectionFileAddressesChanged();
126723f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
126823f70e83SPavel Labath     symbols->SectionFileAddressesChanged();
126905a09c67SJason Molenda }
127005a09c67SJason Molenda 
1271dec96392SPavel Labath UnwindTable &Module::GetUnwindTable() {
1272dec96392SPavel Labath   if (!m_unwind_table)
1273dec96392SPavel Labath     m_unwind_table.emplace(*this);
1274dec96392SPavel Labath   return *m_unwind_table;
1275dec96392SPavel Labath }
1276dec96392SPavel Labath 
1277b9c1b51eSKate Stone SectionList *Module::GetUnifiedSectionList() {
1278d5b44036SJonas Devlieghere   if (!m_sections_up)
1279a8f3ae7cSJonas Devlieghere     m_sections_up = std::make_unique<SectionList>();
1280d5b44036SJonas Devlieghere   return m_sections_up.get();
1281a7499c98SMichael Sartain }
128230fdc8d8SChris Lattner 
12830e4c4821SAdrian Prantl const Symbol *Module::FindFirstSymbolWithNameAndType(ConstString name,
1284b9c1b51eSKate Stone                                                      SymbolType symbol_type) {
1285f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1286b9c1b51eSKate Stone   Timer scoped_timer(
1287f9d16476SPavel Labath       func_cat, "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1288b9c1b51eSKate Stone       name.AsCString(), symbol_type);
1289d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab())
1290b9c1b51eSKate Stone     return symtab->FindFirstSymbolWithNameAndType(
1291b9c1b51eSKate Stone         name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
1292c5dac77aSEugene Zelenko   return nullptr;
129330fdc8d8SChris Lattner }
1294b9c1b51eSKate Stone void Module::SymbolIndicesToSymbolContextList(
1295b9c1b51eSKate Stone     Symtab *symtab, std::vector<uint32_t> &symbol_indexes,
1296b9c1b51eSKate Stone     SymbolContextList &sc_list) {
129730fdc8d8SChris Lattner   // No need to protect this call using m_mutex all other method calls are
129830fdc8d8SChris Lattner   // already thread safe.
129930fdc8d8SChris Lattner 
130030fdc8d8SChris Lattner   size_t num_indices = symbol_indexes.size();
1301b9c1b51eSKate Stone   if (num_indices > 0) {
130230fdc8d8SChris Lattner     SymbolContext sc;
130330fdc8d8SChris Lattner     CalculateSymbolContext(&sc);
1304b9c1b51eSKate Stone     for (size_t i = 0; i < num_indices; i++) {
130530fdc8d8SChris Lattner       sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
130630fdc8d8SChris Lattner       if (sc.symbol)
130730fdc8d8SChris Lattner         sc_list.Append(sc);
130830fdc8d8SChris Lattner     }
130930fdc8d8SChris Lattner   }
131030fdc8d8SChris Lattner }
131130fdc8d8SChris Lattner 
13121ad655e2SAdrian Prantl void Module::FindFunctionSymbols(ConstString name,
1313c1b2ccfdSGreg Clayton                                    uint32_t name_type_mask,
1314b9c1b51eSKate Stone                                    SymbolContextList &sc_list) {
1315f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1316f9d16476SPavel Labath   Timer scoped_timer(func_cat,
1317c1b2ccfdSGreg Clayton                      "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1318b9c1b51eSKate Stone                      name.AsCString(), name_type_mask);
1319d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab())
13201ad655e2SAdrian Prantl     symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
1321c1b2ccfdSGreg Clayton }
1322c1b2ccfdSGreg Clayton 
13231ad655e2SAdrian Prantl void Module::FindSymbolsWithNameAndType(ConstString name,
1324b9c1b51eSKate Stone                                           SymbolType symbol_type,
1325b9c1b51eSKate Stone                                           SymbolContextList &sc_list) {
132630fdc8d8SChris Lattner   // No need to protect this call using m_mutex all other method calls are
132730fdc8d8SChris Lattner   // already thread safe.
132830fdc8d8SChris Lattner 
1329f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1330b9c1b51eSKate Stone   Timer scoped_timer(
1331f9d16476SPavel Labath       func_cat, "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1332b9c1b51eSKate Stone       name.AsCString(), symbol_type);
1333d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab()) {
133430fdc8d8SChris Lattner     std::vector<uint32_t> symbol_indexes;
133530fdc8d8SChris Lattner     symtab->FindAllSymbolsWithNameAndType(name, symbol_type, symbol_indexes);
133630fdc8d8SChris Lattner     SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
133730fdc8d8SChris Lattner   }
133830fdc8d8SChris Lattner }
133930fdc8d8SChris Lattner 
13401ad655e2SAdrian Prantl void Module::FindSymbolsMatchingRegExAndType(const RegularExpression &regex,
1341b9c1b51eSKate Stone                                              SymbolType symbol_type,
1342b9c1b51eSKate Stone                                              SymbolContextList &sc_list) {
134330fdc8d8SChris Lattner   // No need to protect this call using m_mutex all other method calls are
134430fdc8d8SChris Lattner   // already thread safe.
134530fdc8d8SChris Lattner 
1346f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1347b9c1b51eSKate Stone   Timer scoped_timer(
1348f9d16476SPavel Labath       func_cat,
134930fdc8d8SChris Lattner       "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
135095eae423SZachary Turner       regex.GetText().str().c_str(), symbol_type);
1351d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab()) {
135230fdc8d8SChris Lattner     std::vector<uint32_t> symbol_indexes;
1353b9c1b51eSKate Stone     symtab->FindAllSymbolsMatchingRexExAndType(
1354b9c1b51eSKate Stone         regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny,
1355b9c1b51eSKate Stone         symbol_indexes);
135630fdc8d8SChris Lattner     SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
135730fdc8d8SChris Lattner   }
135830fdc8d8SChris Lattner }
135930fdc8d8SChris Lattner 
13607fca8c07SJim Ingham void Module::PreloadSymbols() {
13617fca8c07SJim Ingham   std::lock_guard<std::recursive_mutex> guard(m_mutex);
136223f70e83SPavel Labath   SymbolFile *sym_file = GetSymbolFile();
136323f70e83SPavel Labath   if (!sym_file)
13647fca8c07SJim Ingham     return;
136523f70e83SPavel Labath 
13667fca8c07SJim Ingham   // Prime the symbol file first, since it adds symbols to the symbol table.
136723f70e83SPavel Labath   sym_file->PreloadSymbols();
136823f70e83SPavel Labath 
13697fca8c07SJim Ingham   // Now we can prime the symbol table.
137023f70e83SPavel Labath   if (Symtab *symtab = sym_file->GetSymtab())
13717fca8c07SJim Ingham     symtab->PreloadSymbols();
13727fca8c07SJim Ingham }
13737fca8c07SJim Ingham 
1374b9c1b51eSKate Stone void Module::SetSymbolFileFileSpec(const FileSpec &file) {
1375dbd7fabaSJonas Devlieghere   if (!FileSystem::Instance().Exists(file))
137690271672SGreg Clayton     return;
1377d5b44036SJonas Devlieghere   if (m_symfile_up) {
1378b9c1b51eSKate Stone     // Remove any sections in the unified section list that come from the
1379b9c1b51eSKate Stone     // current symbol vendor.
13803046e668SGreg Clayton     SectionList *section_list = GetSectionList();
138123f70e83SPavel Labath     SymbolFile *symbol_file = GetSymbolFile();
1382b9c1b51eSKate Stone     if (section_list && symbol_file) {
1383a7499c98SMichael Sartain       ObjectFile *obj_file = symbol_file->GetObjectFile();
1384b9c1b51eSKate Stone       // Make sure we have an object file and that the symbol vendor's objfile
138505097246SAdrian Prantl       // isn't the same as the module's objfile before we remove any sections
138605097246SAdrian Prantl       // for it...
1387b9c1b51eSKate Stone       if (obj_file) {
1388b9c1b51eSKate Stone         // Check to make sure we aren't trying to specify the file we already
1389b9c1b51eSKate Stone         // have
1390b9c1b51eSKate Stone         if (obj_file->GetFileSpec() == file) {
139190271672SGreg Clayton           // We are being told to add the exact same file that we already have
139290271672SGreg Clayton           // we don't have to do anything.
139390271672SGreg Clayton           return;
139490271672SGreg Clayton         }
1395d00438e8STamas Berghammer 
1396b9c1b51eSKate Stone         // Cleare the current symtab as we are going to replace it with a new
1397b9c1b51eSKate Stone         // one
1398d00438e8STamas Berghammer         obj_file->ClearSymtab();
139990271672SGreg Clayton 
1400dec96392SPavel Labath         // Clear the unwind table too, as that may also be affected by the
1401dec96392SPavel Labath         // symbol file information.
1402dec96392SPavel Labath         m_unwind_table.reset();
1403dec96392SPavel Labath 
1404b9c1b51eSKate Stone         // The symbol file might be a directory bundle ("/tmp/a.out.dSYM")
140505097246SAdrian Prantl         // instead of a full path to the symbol file within the bundle
1406b9c1b51eSKate Stone         // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to
1407b9c1b51eSKate Stone         // check this
140890271672SGreg Clayton 
14093a58d898SJonas Devlieghere         if (FileSystem::Instance().IsDirectory(file)) {
141090271672SGreg Clayton           std::string new_path(file.GetPath());
141190271672SGreg Clayton           std::string old_path(obj_file->GetFileSpec().GetPath());
1412b9c1b51eSKate Stone           if (old_path.find(new_path) == 0) {
1413b9c1b51eSKate Stone             // We specified the same bundle as the symbol file that we already
1414b9c1b51eSKate Stone             // have
141590271672SGreg Clayton             return;
141690271672SGreg Clayton           }
141790271672SGreg Clayton         }
141890271672SGreg Clayton 
1419b9c1b51eSKate Stone         if (obj_file != m_objfile_sp.get()) {
1420a7499c98SMichael Sartain           size_t num_sections = section_list->GetNumSections(0);
1421b9c1b51eSKate Stone           for (size_t idx = num_sections; idx > 0; --idx) {
1422b9c1b51eSKate Stone             lldb::SectionSP section_sp(
1423b9c1b51eSKate Stone                 section_list->GetSectionAtIndex(idx - 1));
1424b9c1b51eSKate Stone             if (section_sp->GetObjectFile() == obj_file) {
14253046e668SGreg Clayton               section_list->DeleteSection(idx - 1);
1426a7499c98SMichael Sartain             }
1427a7499c98SMichael Sartain           }
1428a7499c98SMichael Sartain         }
1429a7499c98SMichael Sartain       }
1430a7499c98SMichael Sartain     }
1431b9c1b51eSKate Stone     // Keep all old symbol files around in case there are any lingering type
143205097246SAdrian Prantl     // references in any SBValue objects that might have been handed out.
1433d5b44036SJonas Devlieghere     m_old_symfiles.push_back(std::move(m_symfile_up));
143490271672SGreg Clayton   }
1435e01e07b6SGreg Clayton   m_symfile_spec = file;
1436d5b44036SJonas Devlieghere   m_symfile_up.reset();
1437579d6d1aSPavel Labath   m_did_load_symfile = false;
1438e01e07b6SGreg Clayton }
1439e01e07b6SGreg Clayton 
1440b9c1b51eSKate Stone bool Module::IsExecutable() {
1441c5dac77aSEugene Zelenko   if (GetObjectFile() == nullptr)
14425aee162fSJim Ingham     return false;
14435aee162fSJim Ingham   else
14445aee162fSJim Ingham     return GetObjectFile()->IsExecutable();
14455aee162fSJim Ingham }
14465aee162fSJim Ingham 
1447b9c1b51eSKate Stone bool Module::IsLoadedInTarget(Target *target) {
1448b53cb271SJim Ingham   ObjectFile *obj_file = GetObjectFile();
1449b9c1b51eSKate Stone   if (obj_file) {
14503046e668SGreg Clayton     SectionList *sections = GetSectionList();
1451b9c1b51eSKate Stone     if (sections != nullptr) {
1452b53cb271SJim Ingham       size_t num_sections = sections->GetSize();
1453b9c1b51eSKate Stone       for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) {
1454b53cb271SJim Ingham         SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1455b9c1b51eSKate Stone         if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) {
1456b53cb271SJim Ingham           return true;
1457b53cb271SJim Ingham         }
1458b53cb271SJim Ingham       }
1459b53cb271SJim Ingham     }
1460b53cb271SJim Ingham   }
1461b53cb271SJim Ingham   return false;
1462b53cb271SJim Ingham }
14631759848bSEnrico Granata 
146497206d57SZachary Turner bool Module::LoadScriptingResourceInTarget(Target *target, Status &error,
1465b9c1b51eSKate Stone                                            Stream *feedback_stream) {
1466b9c1b51eSKate Stone   if (!target) {
14671759848bSEnrico Granata     error.SetErrorString("invalid destination Target");
14681759848bSEnrico Granata     return false;
14691759848bSEnrico Granata   }
14701759848bSEnrico Granata 
1471b9c1b51eSKate Stone   LoadScriptFromSymFile should_load =
1472b9c1b51eSKate Stone       target->TargetProperties::GetLoadScriptFromSymbolFile();
14732ea43cdcSEnrico Granata 
1474994740fbSGreg Clayton   if (should_load == eLoadScriptFromSymFileFalse)
1475994740fbSGreg Clayton     return false;
1476994740fbSGreg Clayton 
147791c0e749SGreg Clayton   Debugger &debugger = target->GetDebugger();
147891c0e749SGreg Clayton   const ScriptLanguage script_language = debugger.GetScriptLanguage();
1479b9c1b51eSKate Stone   if (script_language != eScriptLanguageNone) {
148091c0e749SGreg Clayton 
14811759848bSEnrico Granata     PlatformSP platform_sp(target->GetPlatform());
14821759848bSEnrico Granata 
1483b9c1b51eSKate Stone     if (!platform_sp) {
14841759848bSEnrico Granata       error.SetErrorString("invalid Platform");
14851759848bSEnrico Granata       return false;
14861759848bSEnrico Granata     }
14871759848bSEnrico Granata 
1488b9c1b51eSKate Stone     FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources(
1489b9c1b51eSKate Stone         target, *this, feedback_stream);
149091c0e749SGreg Clayton 
149191c0e749SGreg Clayton     const uint32_t num_specs = file_specs.GetSize();
1492b9c1b51eSKate Stone     if (num_specs) {
14932b29b432SJonas Devlieghere       ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
1494b9c1b51eSKate Stone       if (script_interpreter) {
1495b9c1b51eSKate Stone         for (uint32_t i = 0; i < num_specs; ++i) {
149691c0e749SGreg Clayton           FileSpec scripting_fspec(file_specs.GetFileSpecAtIndex(i));
1497dbd7fabaSJonas Devlieghere           if (scripting_fspec &&
1498dbd7fabaSJonas Devlieghere               FileSystem::Instance().Exists(scripting_fspec)) {
1499b9c1b51eSKate Stone             if (should_load == eLoadScriptFromSymFileWarn) {
1500397ddd5fSEnrico Granata               if (feedback_stream)
1501b9c1b51eSKate Stone                 feedback_stream->Printf(
1502b9c1b51eSKate Stone                     "warning: '%s' contains a debug script. To run this script "
1503b9c1b51eSKate Stone                     "in "
1504b9c1b51eSKate Stone                     "this debug session:\n\n    command script import "
1505b9c1b51eSKate Stone                     "\"%s\"\n\n"
1506d516deb4SJim Ingham                     "To run all discovered debug scripts in this session:\n\n"
1507b9c1b51eSKate Stone                     "    settings set target.load-script-from-symbol-file "
1508b9c1b51eSKate Stone                     "true\n",
1509d516deb4SJim Ingham                     GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1510d516deb4SJim Ingham                     scripting_fspec.GetPath().c_str());
15112ea43cdcSEnrico Granata               return false;
15122ea43cdcSEnrico Granata             }
15131759848bSEnrico Granata             StreamString scripting_stream;
15141759848bSEnrico Granata             scripting_fspec.Dump(&scripting_stream);
1515e0c70f1bSEnrico Granata             const bool can_reload = true;
15166a51085eSJim Ingham             const bool init_lldb_globals = false;
1517b9c1b51eSKate Stone             bool did_load = script_interpreter->LoadScriptingModule(
1518b9c1b51eSKate Stone                 scripting_stream.GetData(), can_reload, init_lldb_globals,
1519d516deb4SJim Ingham                 error);
15201759848bSEnrico Granata             if (!did_load)
15211759848bSEnrico Granata               return false;
15221759848bSEnrico Granata           }
152391c0e749SGreg Clayton         }
1524b9c1b51eSKate Stone       } else {
15251759848bSEnrico Granata         error.SetErrorString("invalid ScriptInterpreter");
15261759848bSEnrico Granata         return false;
15271759848bSEnrico Granata       }
15281759848bSEnrico Granata     }
1529b9d8890bSGreg Clayton   }
15301759848bSEnrico Granata   return true;
15311759848bSEnrico Granata }
15321759848bSEnrico Granata 
1533b9c1b51eSKate Stone bool Module::SetArchitecture(const ArchSpec &new_arch) {
1534b9c1b51eSKate Stone   if (!m_arch.IsValid()) {
15355aee162fSJim Ingham     m_arch = new_arch;
15365aee162fSJim Ingham     return true;
15375aee162fSJim Ingham   }
1538b6cd5fe9SChaoren Lin   return m_arch.IsCompatibleMatch(new_arch);
15395aee162fSJim Ingham }
15405aee162fSJim Ingham 
1541b9c1b51eSKate Stone bool Module::SetLoadAddress(Target &target, lldb::addr_t value,
1542b9c1b51eSKate Stone                             bool value_is_offset, bool &changed) {
15439e02dacdSSteve Pucci   ObjectFile *object_file = GetObjectFile();
1544b9c1b51eSKate Stone   if (object_file != nullptr) {
1545751caf65SGreg Clayton     changed = object_file->SetLoadAddress(target, value, value_is_offset);
15467524e090SGreg Clayton     return true;
1547b9c1b51eSKate Stone   } else {
15487524e090SGreg Clayton     changed = false;
1549c9660546SGreg Clayton   }
15509e02dacdSSteve Pucci   return false;
1551c9660546SGreg Clayton }
1552c9660546SGreg Clayton 
1553b9c1b51eSKate Stone bool Module::MatchesModuleSpec(const ModuleSpec &module_ref) {
1554b9a01b39SGreg Clayton   const UUID &uuid = module_ref.GetUUID();
1555b9a01b39SGreg Clayton 
1556b9c1b51eSKate Stone   if (uuid.IsValid()) {
1557b9a01b39SGreg Clayton     // If the UUID matches, then nothing more needs to match...
1558c5dac77aSEugene Zelenko     return (uuid == GetUUID());
1559b9a01b39SGreg Clayton   }
1560b9a01b39SGreg Clayton 
1561b9a01b39SGreg Clayton   const FileSpec &file_spec = module_ref.GetFileSpec();
1562b9c1b51eSKate Stone   if (file_spec) {
1563980662eeSTamas Berghammer     if (!FileSpec::Equal(file_spec, m_file, (bool)file_spec.GetDirectory()) &&
1564b9c1b51eSKate Stone         !FileSpec::Equal(file_spec, m_platform_file,
1565b9c1b51eSKate Stone                          (bool)file_spec.GetDirectory()))
1566b9a01b39SGreg Clayton       return false;
1567b9a01b39SGreg Clayton   }
1568b9a01b39SGreg Clayton 
1569b9a01b39SGreg Clayton   const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1570b9c1b51eSKate Stone   if (platform_file_spec) {
1571b9c1b51eSKate Stone     if (!FileSpec::Equal(platform_file_spec, GetPlatformFileSpec(),
1572b9c1b51eSKate Stone                          (bool)platform_file_spec.GetDirectory()))
1573b9a01b39SGreg Clayton       return false;
1574b9a01b39SGreg Clayton   }
1575b9a01b39SGreg Clayton 
1576b9a01b39SGreg Clayton   const ArchSpec &arch = module_ref.GetArchitecture();
1577b9c1b51eSKate Stone   if (arch.IsValid()) {
1578bf4b7be6SSean Callanan     if (!m_arch.IsCompatibleMatch(arch))
1579b9a01b39SGreg Clayton       return false;
1580b9a01b39SGreg Clayton   }
1581b9a01b39SGreg Clayton 
15820e4c4821SAdrian Prantl   ConstString object_name = module_ref.GetObjectName();
1583b9c1b51eSKate Stone   if (object_name) {
1584b9a01b39SGreg Clayton     if (object_name != GetObjectName())
1585b9a01b39SGreg Clayton       return false;
1586b9a01b39SGreg Clayton   }
1587b9a01b39SGreg Clayton   return true;
1588b9a01b39SGreg Clayton }
1589b9a01b39SGreg Clayton 
1590b9c1b51eSKate Stone bool Module::FindSourceFile(const FileSpec &orig_spec,
1591b9c1b51eSKate Stone                             FileSpec &new_spec) const {
159216ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1593d804d285SGreg Clayton   return m_source_mappings.FindFile(orig_spec, new_spec);
1594d804d285SGreg Clayton }
1595d804d285SGreg Clayton 
1596a498f0ecSZachary Turner bool Module::RemapSourceFile(llvm::StringRef path,
1597a498f0ecSZachary Turner                              std::string &new_path) const {
159816ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1599f9be6933SGreg Clayton   return m_source_mappings.RemapPath(path, new_path);
1600f9be6933SGreg Clayton }
1601f9be6933SGreg Clayton 
160224610611SAdrian Prantl bool Module::MergeArchitecture(const ArchSpec &arch_spec) {
160324610611SAdrian Prantl   if (!arch_spec.IsValid())
160424610611SAdrian Prantl     return false;
160524610611SAdrian Prantl   LLDB_LOG(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_MODULES),
160624610611SAdrian Prantl            "module has arch %s, merging/replacing with arch %s",
160724610611SAdrian Prantl            m_arch.GetTriple().getTriple().c_str(),
160824610611SAdrian Prantl            arch_spec.GetTriple().getTriple().c_str());
160924610611SAdrian Prantl   if (!m_arch.IsCompatibleMatch(arch_spec)) {
161024610611SAdrian Prantl     // The new architecture is different, we just need to replace it.
161124610611SAdrian Prantl     return SetArchitecture(arch_spec);
161224610611SAdrian Prantl   }
161324610611SAdrian Prantl 
161424610611SAdrian Prantl   // Merge bits from arch_spec into "merged_arch" and set our architecture.
161524610611SAdrian Prantl   ArchSpec merged_arch(m_arch);
161624610611SAdrian Prantl   merged_arch.MergeFrom(arch_spec);
161724610611SAdrian Prantl   // SetArchitecture() is a no-op if m_arch is already valid.
161824610611SAdrian Prantl   m_arch = ArchSpec();
161924610611SAdrian Prantl   return SetArchitecture(merged_arch);
162024610611SAdrian Prantl }
162124610611SAdrian Prantl 
16222272c481SPavel Labath llvm::VersionTuple Module::GetVersion() {
16232272c481SPavel Labath   if (ObjectFile *obj_file = GetObjectFile())
16242272c481SPavel Labath     return obj_file->GetVersion();
16252272c481SPavel Labath   return llvm::VersionTuple();
16263467d80bSEnrico Granata }
162743fe217bSGreg Clayton 
1628b9c1b51eSKate Stone bool Module::GetIsDynamicLinkEditor() {
162908928f30SGreg Clayton   ObjectFile *obj_file = GetObjectFile();
163008928f30SGreg Clayton 
163108928f30SGreg Clayton   if (obj_file)
163208928f30SGreg Clayton     return obj_file->GetIsDynamicLinkEditor();
163308928f30SGreg Clayton 
163408928f30SGreg Clayton   return false;
163508928f30SGreg Clayton }
1636