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 
5970e4c4821SAdrian Prantl size_t Module::FindGlobalVariables(ConstString name,
59899558cc4SGreg Clayton                                    const CompilerDeclContext *parent_decl_ctx,
599c7bece56SGreg Clayton                                    size_t max_matches,
600b9c1b51eSKate Stone                                    VariableList &variables) {
60123f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
60234cda14bSPavel Labath     return symbols->FindGlobalVariables(name, parent_decl_ctx, max_matches,
60334cda14bSPavel Labath                                         variables);
60434cda14bSPavel Labath   return 0;
60534cda14bSPavel Labath }
60634cda14bSPavel Labath 
60734cda14bSPavel Labath size_t Module::FindGlobalVariables(const RegularExpression &regex,
60834cda14bSPavel Labath                                    size_t max_matches,
60934cda14bSPavel Labath                                    VariableList &variables) {
61023f70e83SPavel Labath   SymbolFile *symbols = GetSymbolFile();
61134cda14bSPavel Labath   if (symbols)
61234cda14bSPavel Labath     return symbols->FindGlobalVariables(regex, max_matches, variables);
61330fdc8d8SChris Lattner   return 0;
61430fdc8d8SChris Lattner }
61530fdc8d8SChris Lattner 
616b9c1b51eSKate Stone size_t Module::FindCompileUnits(const FileSpec &path, bool append,
617b9c1b51eSKate Stone                                 SymbolContextList &sc_list) {
618644247c1SGreg Clayton   if (!append)
619644247c1SGreg Clayton     sc_list.Clear();
620644247c1SGreg Clayton 
621c7bece56SGreg Clayton   const size_t start_size = sc_list.GetSize();
622c7bece56SGreg Clayton   const size_t num_compile_units = GetNumCompileUnits();
623644247c1SGreg Clayton   SymbolContext sc;
624e1cd1be6SGreg Clayton   sc.module_sp = shared_from_this();
625ddd7a2a6SSean Callanan   const bool compare_directory = (bool)path.GetDirectory();
626b9c1b51eSKate Stone   for (size_t i = 0; i < num_compile_units; ++i) {
627644247c1SGreg Clayton     sc.comp_unit = GetCompileUnitAtIndex(i).get();
628b9c1b51eSKate Stone     if (sc.comp_unit) {
629644247c1SGreg Clayton       if (FileSpec::Equal(*sc.comp_unit, path, compare_directory))
630644247c1SGreg Clayton         sc_list.Append(sc);
631644247c1SGreg Clayton     }
6322dafd8edSGreg Clayton   }
633644247c1SGreg Clayton   return sc_list.GetSize() - start_size;
634644247c1SGreg Clayton }
635644247c1SGreg Clayton 
6360e4c4821SAdrian Prantl Module::LookupInfo::LookupInfo(ConstString name,
637117b1fa1SZachary Turner                                FunctionNameType name_type_mask,
638117b1fa1SZachary Turner                                LanguageType language)
639117b1fa1SZachary Turner     : m_name(name), m_lookup_name(), m_language(language),
640117b1fa1SZachary Turner       m_name_type_mask(eFunctionNameTypeNone),
641b9c1b51eSKate Stone       m_match_name_after_lookup(false) {
6426234a5c8SGreg Clayton   const char *name_cstr = name.GetCString();
6436234a5c8SGreg Clayton   llvm::StringRef basename;
6446234a5c8SGreg Clayton   llvm::StringRef context;
6456234a5c8SGreg Clayton 
646b9c1b51eSKate Stone   if (name_type_mask & eFunctionNameTypeAuto) {
6476234a5c8SGreg Clayton     if (CPlusPlusLanguage::IsCPPMangledName(name_cstr))
6486234a5c8SGreg Clayton       m_name_type_mask = eFunctionNameTypeFull;
6496234a5c8SGreg Clayton     else if ((language == eLanguageTypeUnknown ||
6506234a5c8SGreg Clayton               Language::LanguageIsObjC(language)) &&
6516234a5c8SGreg Clayton              ObjCLanguage::IsPossibleObjCMethodName(name_cstr))
6526234a5c8SGreg Clayton       m_name_type_mask = eFunctionNameTypeFull;
653b9c1b51eSKate Stone     else if (Language::LanguageIsC(language)) {
6546234a5c8SGreg Clayton       m_name_type_mask = eFunctionNameTypeFull;
655b9c1b51eSKate Stone     } else {
6566234a5c8SGreg Clayton       if ((language == eLanguageTypeUnknown ||
6576234a5c8SGreg Clayton            Language::LanguageIsObjC(language)) &&
6586234a5c8SGreg Clayton           ObjCLanguage::IsPossibleObjCSelector(name_cstr))
6596234a5c8SGreg Clayton         m_name_type_mask |= eFunctionNameTypeSelector;
6606234a5c8SGreg Clayton 
6616234a5c8SGreg Clayton       CPlusPlusLanguage::MethodName cpp_method(name);
6626234a5c8SGreg Clayton       basename = cpp_method.GetBasename();
663b9c1b51eSKate Stone       if (basename.empty()) {
664b9c1b51eSKate Stone         if (CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
665b9c1b51eSKate Stone                                                            basename))
6666234a5c8SGreg Clayton           m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
6676234a5c8SGreg Clayton         else
6686234a5c8SGreg Clayton           m_name_type_mask |= eFunctionNameTypeFull;
669b9c1b51eSKate Stone       } else {
6706234a5c8SGreg Clayton         m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
6716234a5c8SGreg Clayton       }
6726234a5c8SGreg Clayton     }
673b9c1b51eSKate Stone   } else {
6746234a5c8SGreg Clayton     m_name_type_mask = name_type_mask;
675b9c1b51eSKate Stone     if (name_type_mask & eFunctionNameTypeMethod ||
676b9c1b51eSKate Stone         name_type_mask & eFunctionNameTypeBase) {
677b9c1b51eSKate Stone       // If they've asked for a CPP method or function name and it can't be
67805097246SAdrian Prantl       // that, we don't even need to search for CPP methods or names.
6796234a5c8SGreg Clayton       CPlusPlusLanguage::MethodName cpp_method(name);
680b9c1b51eSKate Stone       if (cpp_method.IsValid()) {
6816234a5c8SGreg Clayton         basename = cpp_method.GetBasename();
6826234a5c8SGreg Clayton 
683b9c1b51eSKate Stone         if (!cpp_method.GetQualifiers().empty()) {
684b9c1b51eSKate Stone           // There is a "const" or other qualifier following the end of the
68505097246SAdrian Prantl           // function parens, this can't be a eFunctionNameTypeBase
6866234a5c8SGreg Clayton           m_name_type_mask &= ~(eFunctionNameTypeBase);
6876234a5c8SGreg Clayton           if (m_name_type_mask == eFunctionNameTypeNone)
6886234a5c8SGreg Clayton             return;
6896234a5c8SGreg Clayton         }
690b9c1b51eSKate Stone       } else {
691b9c1b51eSKate Stone         // If the CPP method parser didn't manage to chop this up, try to fill
69205097246SAdrian Prantl         // in the base name if we can. If a::b::c is passed in, we need to just
69305097246SAdrian Prantl         // look up "c", and then we'll filter the result later.
694b9c1b51eSKate Stone         CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
695b9c1b51eSKate Stone                                                        basename);
6966234a5c8SGreg Clayton       }
6976234a5c8SGreg Clayton     }
6986234a5c8SGreg Clayton 
699b9c1b51eSKate Stone     if (name_type_mask & eFunctionNameTypeSelector) {
700b9c1b51eSKate Stone       if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr)) {
7016234a5c8SGreg Clayton         m_name_type_mask &= ~(eFunctionNameTypeSelector);
7026234a5c8SGreg Clayton         if (m_name_type_mask == eFunctionNameTypeNone)
7036234a5c8SGreg Clayton           return;
7046234a5c8SGreg Clayton       }
7056234a5c8SGreg Clayton     }
7066234a5c8SGreg Clayton 
707b9c1b51eSKate Stone     // Still try and get a basename in case someone specifies a name type mask
7085d0c1146SGreg Clayton     // of eFunctionNameTypeFull and a name like "A::func"
709b9c1b51eSKate Stone     if (basename.empty()) {
7105d0c1146SGreg Clayton       if (name_type_mask & eFunctionNameTypeFull &&
7115d0c1146SGreg Clayton           !CPlusPlusLanguage::IsCPPMangledName(name_cstr)) {
7126234a5c8SGreg Clayton         CPlusPlusLanguage::MethodName cpp_method(name);
7136234a5c8SGreg Clayton         basename = cpp_method.GetBasename();
7146234a5c8SGreg Clayton         if (basename.empty())
715b9c1b51eSKate Stone           CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
716b9c1b51eSKate Stone                                                          basename);
7176234a5c8SGreg Clayton       }
7186234a5c8SGreg Clayton     }
7196234a5c8SGreg Clayton   }
7206234a5c8SGreg Clayton 
721b9c1b51eSKate Stone   if (!basename.empty()) {
72205097246SAdrian Prantl     // The name supplied was a partial C++ path like "a::count". In this case
72305097246SAdrian Prantl     // we want to do a lookup on the basename "count" and then make sure any
72405097246SAdrian Prantl     // matching results contain "a::count" so that it would match "b::a::count"
72505097246SAdrian Prantl     // and "a::count". This is why we set "match_name_after_lookup" to true
7266234a5c8SGreg Clayton     m_lookup_name.SetString(basename);
7276234a5c8SGreg Clayton     m_match_name_after_lookup = true;
728b9c1b51eSKate Stone   } else {
729b9c1b51eSKate Stone     // The name is already correct, just use the exact name as supplied, and we
73005097246SAdrian Prantl     // won't need to check if any matches contain "name"
7316234a5c8SGreg Clayton     m_lookup_name = name;
7326234a5c8SGreg Clayton     m_match_name_after_lookup = false;
7336234a5c8SGreg Clayton   }
7346234a5c8SGreg Clayton }
7356234a5c8SGreg Clayton 
736b9c1b51eSKate Stone void Module::LookupInfo::Prune(SymbolContextList &sc_list,
737b9c1b51eSKate Stone                                size_t start_idx) const {
738b9c1b51eSKate Stone   if (m_match_name_after_lookup && m_name) {
7396234a5c8SGreg Clayton     SymbolContext sc;
7406234a5c8SGreg Clayton     size_t i = start_idx;
741b9c1b51eSKate Stone     while (i < sc_list.GetSize()) {
7426234a5c8SGreg Clayton       if (!sc_list.GetContextAtIndex(i, sc))
7436234a5c8SGreg Clayton         break;
7446234a5c8SGreg Clayton       ConstString full_name(sc.GetFunctionName());
745b9c1b51eSKate Stone       if (full_name &&
746b9c1b51eSKate Stone           ::strstr(full_name.GetCString(), m_name.GetCString()) == nullptr) {
7476234a5c8SGreg Clayton         sc_list.RemoveContextAtIndex(i);
748b9c1b51eSKate Stone       } else {
7496234a5c8SGreg Clayton         ++i;
7506234a5c8SGreg Clayton       }
7516234a5c8SGreg Clayton     }
7526234a5c8SGreg Clayton   }
7536234a5c8SGreg Clayton 
754b9c1b51eSKate Stone   // If we have only full name matches we might have tried to set breakpoint on
7555d0c1146SGreg Clayton   // "func" and specified eFunctionNameTypeFull, but we might have found
7565d0c1146SGreg Clayton   // "a::func()", "a::b::func()", "c::func()", "func()" and "func". Only
7575d0c1146SGreg Clayton   // "func()" and "func" should end up matching.
758b9c1b51eSKate Stone   if (m_name_type_mask == eFunctionNameTypeFull) {
7596234a5c8SGreg Clayton     SymbolContext sc;
7606234a5c8SGreg Clayton     size_t i = start_idx;
761b9c1b51eSKate Stone     while (i < sc_list.GetSize()) {
7626234a5c8SGreg Clayton       if (!sc_list.GetContextAtIndex(i, sc))
7636234a5c8SGreg Clayton         break;
76405097246SAdrian Prantl       // Make sure the mangled and demangled names don't match before we try to
76505097246SAdrian Prantl       // pull anything out
7665d0c1146SGreg Clayton       ConstString mangled_name(sc.GetFunctionName(Mangled::ePreferMangled));
7676234a5c8SGreg Clayton       ConstString full_name(sc.GetFunctionName());
7685d0c1146SGreg Clayton       if (mangled_name != m_name && full_name != m_name)
7695d0c1146SGreg Clayton       {
7706234a5c8SGreg Clayton         CPlusPlusLanguage::MethodName cpp_method(full_name);
771b9c1b51eSKate Stone         if (cpp_method.IsValid()) {
772b9c1b51eSKate Stone           if (cpp_method.GetContext().empty()) {
773b9c1b51eSKate Stone             if (cpp_method.GetBasename().compare(m_name.GetStringRef()) != 0) {
7746234a5c8SGreg Clayton               sc_list.RemoveContextAtIndex(i);
7756234a5c8SGreg Clayton               continue;
7766234a5c8SGreg Clayton             }
777b9c1b51eSKate Stone           } else {
7785d0c1146SGreg Clayton             std::string qualified_name;
7795d0c1146SGreg Clayton             llvm::StringRef anon_prefix("(anonymous namespace)");
7805d0c1146SGreg Clayton             if (cpp_method.GetContext() == anon_prefix)
7815d0c1146SGreg Clayton               qualified_name = cpp_method.GetBasename().str();
7825d0c1146SGreg Clayton             else
7835d0c1146SGreg Clayton               qualified_name = cpp_method.GetScopeQualifiedName();
7848d20cfdfSJonas Devlieghere             if (qualified_name != m_name.GetCString()) {
7856234a5c8SGreg Clayton               sc_list.RemoveContextAtIndex(i);
7866234a5c8SGreg Clayton               continue;
7876234a5c8SGreg Clayton             }
7886234a5c8SGreg Clayton           }
7896234a5c8SGreg Clayton         }
7905d0c1146SGreg Clayton       }
7916234a5c8SGreg Clayton       ++i;
7926234a5c8SGreg Clayton     }
7936234a5c8SGreg Clayton   }
7946234a5c8SGreg Clayton }
7956234a5c8SGreg Clayton 
7960e4c4821SAdrian Prantl size_t Module::FindFunctions(ConstString name,
79799558cc4SGreg Clayton                              const CompilerDeclContext *parent_decl_ctx,
798117b1fa1SZachary Turner                              FunctionNameType name_type_mask,
799117b1fa1SZachary Turner                              bool include_symbols, bool include_inlines,
800117b1fa1SZachary Turner                              bool append, SymbolContextList &sc_list) {
801931180e6SGreg Clayton   if (!append)
802931180e6SGreg Clayton     sc_list.Clear();
803931180e6SGreg Clayton 
80443fe217bSGreg Clayton   const size_t old_size = sc_list.GetSize();
805931180e6SGreg Clayton 
806931180e6SGreg Clayton   // Find all the functions (not symbols, but debug information functions...
80723f70e83SPavel Labath   SymbolFile *symbols = GetSymbolFile();
80843fe217bSGreg Clayton 
809b9c1b51eSKate Stone   if (name_type_mask & eFunctionNameTypeAuto) {
8106234a5c8SGreg Clayton     LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
81143fe217bSGreg Clayton 
812b9c1b51eSKate Stone     if (symbols) {
813b9c1b51eSKate Stone       symbols->FindFunctions(lookup_info.GetLookupName(), parent_decl_ctx,
814b9c1b51eSKate Stone                              lookup_info.GetNameTypeMask(), include_inlines,
815b9c1b51eSKate Stone                              append, sc_list);
81643fe217bSGreg Clayton 
817b9c1b51eSKate Stone       // Now check our symbol table for symbols that are code symbols if
818b9c1b51eSKate Stone       // requested
819b9c1b51eSKate Stone       if (include_symbols) {
820a7499c98SMichael Sartain         Symtab *symtab = symbols->GetSymtab();
82143fe217bSGreg Clayton         if (symtab)
822b9c1b51eSKate Stone           symtab->FindFunctionSymbols(lookup_info.GetLookupName(),
823b9c1b51eSKate Stone                                       lookup_info.GetNameTypeMask(), sc_list);
82443fe217bSGreg Clayton       }
82543fe217bSGreg Clayton     }
82643fe217bSGreg Clayton 
8276234a5c8SGreg Clayton     const size_t new_size = sc_list.GetSize();
8286234a5c8SGreg Clayton 
8296234a5c8SGreg Clayton     if (old_size < new_size)
8306234a5c8SGreg Clayton       lookup_info.Prune(sc_list, old_size);
831b9c1b51eSKate Stone   } else {
832b9c1b51eSKate Stone     if (symbols) {
833b9c1b51eSKate Stone       symbols->FindFunctions(name, parent_decl_ctx, name_type_mask,
834b9c1b51eSKate Stone                              include_inlines, append, sc_list);
835931180e6SGreg Clayton 
836b9c1b51eSKate Stone       // Now check our symbol table for symbols that are code symbols if
837b9c1b51eSKate Stone       // requested
838b9c1b51eSKate Stone       if (include_symbols) {
839a7499c98SMichael Sartain         Symtab *symtab = symbols->GetSymtab();
840931180e6SGreg Clayton         if (symtab)
84143fe217bSGreg Clayton           symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
842931180e6SGreg Clayton       }
843931180e6SGreg Clayton     }
844931180e6SGreg Clayton   }
84543fe217bSGreg Clayton 
84643fe217bSGreg Clayton   return sc_list.GetSize() - old_size;
84730fdc8d8SChris Lattner }
84830fdc8d8SChris Lattner 
849b9c1b51eSKate Stone size_t Module::FindFunctions(const RegularExpression &regex,
850b9c1b51eSKate Stone                              bool include_symbols, bool include_inlines,
851b9c1b51eSKate Stone                              bool append, SymbolContextList &sc_list) {
852931180e6SGreg Clayton   if (!append)
853931180e6SGreg Clayton     sc_list.Clear();
854931180e6SGreg Clayton 
855c7bece56SGreg Clayton   const size_t start_size = sc_list.GetSize();
856931180e6SGreg Clayton 
85723f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile()) {
8589df05fbbSSean Callanan     symbols->FindFunctions(regex, include_inlines, append, sc_list);
859a7499c98SMichael Sartain 
86005097246SAdrian Prantl     // Now check our symbol table for symbols that are code symbols if
86105097246SAdrian Prantl     // requested
862b9c1b51eSKate Stone     if (include_symbols) {
863a7499c98SMichael Sartain       Symtab *symtab = symbols->GetSymtab();
864b9c1b51eSKate Stone       if (symtab) {
865931180e6SGreg Clayton         std::vector<uint32_t> symbol_indexes;
866b9c1b51eSKate Stone         symtab->AppendSymbolIndexesMatchingRegExAndType(
867b9c1b51eSKate Stone             regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny,
868b9c1b51eSKate Stone             symbol_indexes);
869c7bece56SGreg Clayton         const size_t num_matches = symbol_indexes.size();
870b9c1b51eSKate Stone         if (num_matches) {
871931180e6SGreg Clayton           SymbolContext sc(this);
872d8cf1a11SGreg Clayton           const size_t end_functions_added_index = sc_list.GetSize();
873b9c1b51eSKate Stone           size_t num_functions_added_to_sc_list =
874b9c1b51eSKate Stone               end_functions_added_index - start_size;
875b9c1b51eSKate Stone           if (num_functions_added_to_sc_list == 0) {
87605097246SAdrian Prantl             // No functions were added, just symbols, so we can just append
87705097246SAdrian Prantl             // them
878b9c1b51eSKate Stone             for (size_t i = 0; i < num_matches; ++i) {
879931180e6SGreg Clayton               sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
88000049b8bSMatt Kopec               SymbolType sym_type = sc.symbol->GetType();
88100049b8bSMatt Kopec               if (sc.symbol && (sym_type == eSymbolTypeCode ||
88200049b8bSMatt Kopec                                 sym_type == eSymbolTypeResolver))
883d8cf1a11SGreg Clayton                 sc_list.Append(sc);
884d8cf1a11SGreg Clayton             }
885b9c1b51eSKate Stone           } else {
886d8cf1a11SGreg Clayton             typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
887d8cf1a11SGreg Clayton             FileAddrToIndexMap file_addr_to_index;
888b9c1b51eSKate Stone             for (size_t i = start_size; i < end_functions_added_index; ++i) {
889d8cf1a11SGreg Clayton               const SymbolContext &sc = sc_list[i];
890d8cf1a11SGreg Clayton               if (sc.block)
891d8cf1a11SGreg Clayton                 continue;
892b9c1b51eSKate Stone               file_addr_to_index[sc.function->GetAddressRange()
893b9c1b51eSKate Stone                                      .GetBaseAddress()
894b9c1b51eSKate Stone                                      .GetFileAddress()] = i;
895d8cf1a11SGreg Clayton             }
896d8cf1a11SGreg Clayton 
897d8cf1a11SGreg Clayton             FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
898d8cf1a11SGreg Clayton             // Functions were added so we need to merge symbols into any
899d8cf1a11SGreg Clayton             // existing function symbol contexts
900b9c1b51eSKate Stone             for (size_t i = start_size; i < num_matches; ++i) {
901d8cf1a11SGreg Clayton               sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
902d8cf1a11SGreg Clayton               SymbolType sym_type = sc.symbol->GetType();
903b9c1b51eSKate Stone               if (sc.symbol && sc.symbol->ValueIsAddress() &&
904b9c1b51eSKate Stone                   (sym_type == eSymbolTypeCode ||
905b9c1b51eSKate Stone                    sym_type == eSymbolTypeResolver)) {
906b9c1b51eSKate Stone                 FileAddrToIndexMap::const_iterator pos =
907b9c1b51eSKate Stone                     file_addr_to_index.find(
908b9c1b51eSKate Stone                         sc.symbol->GetAddressRef().GetFileAddress());
909d8cf1a11SGreg Clayton                 if (pos == end)
910d8cf1a11SGreg Clayton                   sc_list.Append(sc);
911d8cf1a11SGreg Clayton                 else
912d8cf1a11SGreg Clayton                   sc_list[pos->second].symbol = sc.symbol;
913d8cf1a11SGreg Clayton               }
914d8cf1a11SGreg Clayton             }
915931180e6SGreg Clayton           }
916931180e6SGreg Clayton         }
917931180e6SGreg Clayton       }
918931180e6SGreg Clayton     }
919931180e6SGreg Clayton   }
920931180e6SGreg Clayton   return sc_list.GetSize() - start_size;
92130fdc8d8SChris Lattner }
92230fdc8d8SChris Lattner 
923b9c1b51eSKate Stone void Module::FindAddressesForLine(const lldb::TargetSP target_sp,
924f86248d9SRichard Mitton                                   const FileSpec &file, uint32_t line,
925f86248d9SRichard Mitton                                   Function *function,
926b9c1b51eSKate Stone                                   std::vector<Address> &output_local,
927b9c1b51eSKate Stone                                   std::vector<Address> &output_extern) {
928f86248d9SRichard Mitton   SearchFilterByModule filter(target_sp, m_file);
929f86248d9SRichard Mitton   AddressResolverFileLine resolver(file, line, true);
930f86248d9SRichard Mitton   resolver.ResolveAddress(filter);
931f86248d9SRichard Mitton 
932b9c1b51eSKate Stone   for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++) {
933f86248d9SRichard Mitton     Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
934f86248d9SRichard Mitton     Function *f = addr.CalculateSymbolContextFunction();
935f86248d9SRichard Mitton     if (f && f == function)
936f86248d9SRichard Mitton       output_local.push_back(addr);
937f86248d9SRichard Mitton     else
938f86248d9SRichard Mitton       output_extern.push_back(addr);
939f86248d9SRichard Mitton   }
940f86248d9SRichard Mitton }
941f86248d9SRichard Mitton 
942b9c1b51eSKate Stone size_t Module::FindTypes_Impl(
9430e4c4821SAdrian Prantl     ConstString name, const CompilerDeclContext *parent_decl_ctx,
944576495e6SZachary Turner     bool append, size_t max_matches,
945ae088e52SGreg Clayton     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
946b9c1b51eSKate Stone     TypeMap &types) {
947f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
948f9d16476SPavel Labath   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
94923f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
950576495e6SZachary Turner     return symbols->FindTypes(name, parent_decl_ctx, append, max_matches,
951b9c1b51eSKate Stone                               searched_symbol_files, types);
9523504eee8SGreg Clayton   return 0;
9533504eee8SGreg Clayton }
9543504eee8SGreg Clayton 
9550e4c4821SAdrian Prantl size_t Module::FindTypesInNamespace(ConstString type_name,
95699558cc4SGreg Clayton                                     const CompilerDeclContext *parent_decl_ctx,
957b9c1b51eSKate Stone                                     size_t max_matches, TypeList &type_list) {
95884db9105SGreg Clayton   const bool append = true;
9594069730cSRavitheja Addepally   TypeMap types_map;
960ae088e52SGreg Clayton   llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
961b9c1b51eSKate Stone   size_t num_types =
962576495e6SZachary Turner       FindTypes_Impl(type_name, parent_decl_ctx, append, max_matches,
963b9c1b51eSKate Stone                      searched_symbol_files, types_map);
964576495e6SZachary Turner   if (num_types > 0) {
965576495e6SZachary Turner     SymbolContext sc;
966576495e6SZachary Turner     sc.module_sp = shared_from_this();
9674069730cSRavitheja Addepally     sc.SortTypeList(types_map, type_list);
968576495e6SZachary Turner   }
9694069730cSRavitheja Addepally   return num_types;
9706f3533fbSEnrico Granata }
9716f3533fbSEnrico Granata 
972b9c1b51eSKate Stone lldb::TypeSP Module::FindFirstType(const SymbolContext &sc,
9730e4c4821SAdrian Prantl                                    ConstString name, bool exact_match) {
974b43165b7SGreg Clayton   TypeList type_list;
975ae088e52SGreg Clayton   llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
976b9c1b51eSKate Stone   const size_t num_matches =
977576495e6SZachary Turner       FindTypes(name, exact_match, 1, searched_symbol_files, type_list);
978b43165b7SGreg Clayton   if (num_matches)
979b43165b7SGreg Clayton     return type_list.GetTypeAtIndex(0);
980b43165b7SGreg Clayton   return TypeSP();
981b43165b7SGreg Clayton }
982b43165b7SGreg Clayton 
983b9c1b51eSKate Stone size_t Module::FindTypes(
9840e4c4821SAdrian Prantl     ConstString name, bool exact_match, size_t max_matches,
985ae088e52SGreg Clayton     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
986b9c1b51eSKate Stone     TypeList &types) {
987c7bece56SGreg Clayton   size_t num_matches = 0;
98884db9105SGreg Clayton   const char *type_name_cstr = name.GetCString();
989556b1611STamas Berghammer   llvm::StringRef type_scope;
990556b1611STamas Berghammer   llvm::StringRef type_basename;
99184db9105SGreg Clayton   const bool append = true;
9927bc31332SGreg Clayton   TypeClass type_class = eTypeClassAny;
9934069730cSRavitheja Addepally   TypeMap typesmap;
9941739b7d0SFrederic Riss 
995b9c1b51eSKate Stone   if (Type::GetTypeScopeAndBasename(type_name_cstr, type_scope, type_basename,
996b9c1b51eSKate Stone                                     type_class)) {
99784db9105SGreg Clayton     // Check if "name" starts with "::" which means the qualified type starts
99884db9105SGreg Clayton     // from the root namespace and implies and exact match. The typenames we
99984db9105SGreg Clayton     // get back from clang do not start with "::" so we need to strip this off
1000d93c4a33SBruce Mitchener     // in order to get the qualified names to match
1001556b1611STamas Berghammer     exact_match = type_scope.consume_front("::");
10026f3533fbSEnrico Granata 
1003556b1611STamas Berghammer     ConstString type_basename_const_str(type_basename);
1004576495e6SZachary Turner     if (FindTypes_Impl(type_basename_const_str, nullptr, append, max_matches,
1005576495e6SZachary Turner                        searched_symbol_files, typesmap)) {
1006b9c1b51eSKate Stone       typesmap.RemoveMismatchedTypes(type_scope, type_basename, type_class,
1007b9c1b51eSKate Stone                                      exact_match);
10084069730cSRavitheja Addepally       num_matches = typesmap.GetSize();
10096f3533fbSEnrico Granata     }
1010b9c1b51eSKate Stone   } else {
1011b9c1b51eSKate Stone     // The type is not in a namespace/class scope, just search for it by
1012b9c1b51eSKate Stone     // basename
1013c485f056SGreg Clayton     if (type_class != eTypeClassAny && !type_basename.empty()) {
1014b9c1b51eSKate Stone       // The "type_name_cstr" will have been modified if we have a valid type
101505097246SAdrian Prantl       // class prefix (like "struct", "class", "union", "typedef" etc).
1016576495e6SZachary Turner       FindTypes_Impl(ConstString(type_basename), nullptr, append, UINT_MAX,
1017576495e6SZachary Turner                      searched_symbol_files, typesmap);
1018c485f056SGreg Clayton       typesmap.RemoveMismatchedTypes(type_scope, type_basename, type_class,
1019c485f056SGreg Clayton                                      exact_match);
10204069730cSRavitheja Addepally       num_matches = typesmap.GetSize();
1021b9c1b51eSKate Stone     } else {
1022576495e6SZachary Turner       num_matches = FindTypes_Impl(name, nullptr, append, UINT_MAX,
1023b9c1b51eSKate Stone                                    searched_symbol_files, typesmap);
1024c485f056SGreg Clayton       if (exact_match) {
1025c485f056SGreg Clayton         std::string name_str(name.AsCString(""));
1026c485f056SGreg Clayton         typesmap.RemoveMismatchedTypes(type_scope, name_str, type_class,
1027c485f056SGreg Clayton                                        exact_match);
10289cd4def1SFrederic Riss         num_matches = typesmap.GetSize();
1029c485f056SGreg Clayton       }
103084db9105SGreg Clayton     }
10317bc31332SGreg Clayton   }
1032576495e6SZachary Turner   if (num_matches > 0) {
1033576495e6SZachary Turner     SymbolContext sc;
1034576495e6SZachary Turner     sc.module_sp = shared_from_this();
10354069730cSRavitheja Addepally     sc.SortTypeList(typesmap, types);
1036576495e6SZachary Turner   }
103784db9105SGreg Clayton   return num_matches;
10386f3533fbSEnrico Granata }
10396f3533fbSEnrico Granata 
1040*aa97a89dSAdrian Prantl size_t Module::FindTypes(llvm::ArrayRef<CompilerContext> pattern,
1041*aa97a89dSAdrian Prantl                          LanguageSet languages, bool append, TypeMap &types) {
1042*aa97a89dSAdrian Prantl   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1043*aa97a89dSAdrian Prantl   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
1044*aa97a89dSAdrian Prantl   if (SymbolFile *symbols = GetSymbolFile())
1045*aa97a89dSAdrian Prantl     return symbols->FindTypes(pattern, languages, append, types);
1046*aa97a89dSAdrian Prantl   return 0;
1047*aa97a89dSAdrian Prantl }
1048*aa97a89dSAdrian Prantl 
1049579d6d1aSPavel Labath SymbolFile *Module::GetSymbolFile(bool can_create, Stream *feedback_strm) {
1050579d6d1aSPavel Labath   if (!m_did_load_symfile.load()) {
105116ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1052579d6d1aSPavel Labath     if (!m_did_load_symfile.load() && can_create) {
105330fdc8d8SChris Lattner       ObjectFile *obj_file = GetObjectFile();
1054b9c1b51eSKate Stone       if (obj_file != nullptr) {
1055f9d16476SPavel Labath         static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1056f9d16476SPavel Labath         Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
1057d5b44036SJonas Devlieghere         m_symfile_up.reset(
1058b9c1b51eSKate Stone             SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
1059579d6d1aSPavel Labath         m_did_load_symfile = true;
106030fdc8d8SChris Lattner       }
106130fdc8d8SChris Lattner     }
106288c05f54SGreg Clayton   }
1063579d6d1aSPavel Labath   return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr;
106423f70e83SPavel Labath }
106523f70e83SPavel Labath 
1066d5d47a35SPavel Labath Symtab *Module::GetSymtab() {
1067d5d47a35SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
1068d5d47a35SPavel Labath     return symbols->GetSymtab();
1069d5d47a35SPavel Labath   return nullptr;
1070d5d47a35SPavel Labath }
1071d5d47a35SPavel Labath 
1072b9c1b51eSKate Stone void Module::SetFileSpecAndObjectName(const FileSpec &file,
10730e4c4821SAdrian Prantl                                       ConstString object_name) {
107405097246SAdrian Prantl   // Container objects whose paths do not specify a file directly can call this
107505097246SAdrian Prantl   // function to correct the file and object names.
107630fdc8d8SChris Lattner   m_file = file;
107746376966SJonas Devlieghere   m_mod_time = FileSystem::Instance().GetModificationTime(file);
107830fdc8d8SChris Lattner   m_object_name = object_name;
107930fdc8d8SChris Lattner }
108030fdc8d8SChris Lattner 
1081b9c1b51eSKate Stone const ArchSpec &Module::GetArchitecture() const { return m_arch; }
108230fdc8d8SChris Lattner 
1083b9c1b51eSKate Stone std::string Module::GetSpecificationDescription() const {
1084b5ad4ec7SGreg Clayton   std::string spec(GetFileSpec().GetPath());
1085b9c1b51eSKate Stone   if (m_object_name) {
1086b5ad4ec7SGreg Clayton     spec += '(';
1087b5ad4ec7SGreg Clayton     spec += m_object_name.GetCString();
1088b5ad4ec7SGreg Clayton     spec += ')';
1089b5ad4ec7SGreg Clayton   }
1090b5ad4ec7SGreg Clayton   return spec;
1091b5ad4ec7SGreg Clayton }
1092b5ad4ec7SGreg Clayton 
1093b9c1b51eSKate Stone void Module::GetDescription(Stream *s, lldb::DescriptionLevel level) {
109416ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1095ceb6b139SCaroline Tice 
1096b9c1b51eSKate Stone   if (level >= eDescriptionLevelFull) {
1097cfd1acedSGreg Clayton     if (m_arch.IsValid())
109864195a2cSGreg Clayton       s->Printf("(%s) ", m_arch.GetArchitectureName());
1099c982b3d6SGreg Clayton   }
1100ceb6b139SCaroline Tice 
1101b9c1b51eSKate Stone   if (level == eDescriptionLevelBrief) {
1102c982b3d6SGreg Clayton     const char *filename = m_file.GetFilename().GetCString();
1103c982b3d6SGreg Clayton     if (filename)
1104c982b3d6SGreg Clayton       s->PutCString(filename);
1105b9c1b51eSKate Stone   } else {
1106cfd1acedSGreg Clayton     char path[PATH_MAX];
1107cfd1acedSGreg Clayton     if (m_file.GetPath(path, sizeof(path)))
1108cfd1acedSGreg Clayton       s->PutCString(path);
1109c982b3d6SGreg Clayton   }
1110cfd1acedSGreg Clayton 
1111cfd1acedSGreg Clayton   const char *object_name = m_object_name.GetCString();
1112cfd1acedSGreg Clayton   if (object_name)
1113cfd1acedSGreg Clayton     s->Printf("(%s)", object_name);
1114ceb6b139SCaroline Tice }
1115ceb6b139SCaroline Tice 
1116b9c1b51eSKate Stone void Module::ReportError(const char *format, ...) {
1117b9c1b51eSKate Stone   if (format && format[0]) {
1118e38a5eddSGreg Clayton     StreamString strm;
1119e38a5eddSGreg Clayton     strm.PutCString("error: ");
1120e38a5eddSGreg Clayton     GetDescription(&strm, lldb::eDescriptionLevelBrief);
11218b35334eSGreg Clayton     strm.PutChar(' ');
1122c982b3d6SGreg Clayton     va_list args;
1123c982b3d6SGreg Clayton     va_start(args, format);
1124e38a5eddSGreg Clayton     strm.PrintfVarArg(format, args);
1125c982b3d6SGreg Clayton     va_end(args);
1126e38a5eddSGreg Clayton 
1127e38a5eddSGreg Clayton     const int format_len = strlen(format);
1128b9c1b51eSKate Stone     if (format_len > 0) {
1129e38a5eddSGreg Clayton       const char last_char = format[format_len - 1];
1130376230c9SRaphael Isemann       if (last_char != '\n' && last_char != '\r')
1131e38a5eddSGreg Clayton         strm.EOL();
1132e38a5eddSGreg Clayton     }
1133c156427dSZachary Turner     Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData());
1134e38a5eddSGreg Clayton   }
1135e38a5eddSGreg Clayton }
1136e38a5eddSGreg Clayton 
1137b9c1b51eSKate Stone bool Module::FileHasChanged() const {
1138c5dac77aSEugene Zelenko   if (!m_file_has_changed)
11391408bf72SPavel Labath     m_file_has_changed =
114046376966SJonas Devlieghere         (FileSystem::Instance().GetModificationTime(m_file) != m_mod_time);
11411d60909eSGreg Clayton   return m_file_has_changed;
11421d60909eSGreg Clayton }
11431d60909eSGreg Clayton 
1144b9c1b51eSKate Stone void Module::ReportErrorIfModifyDetected(const char *format, ...) {
1145b9c1b51eSKate Stone   if (!m_first_file_changed_log) {
1146b9c1b51eSKate Stone     if (FileHasChanged()) {
11471d60909eSGreg Clayton       m_first_file_changed_log = true;
1148b9c1b51eSKate Stone       if (format) {
1149e38a5eddSGreg Clayton         StreamString strm;
1150e38a5eddSGreg Clayton         strm.PutCString("error: the object file ");
1151e38a5eddSGreg Clayton         GetDescription(&strm, lldb::eDescriptionLevelFull);
1152e38a5eddSGreg Clayton         strm.PutCString(" has been modified\n");
1153e38a5eddSGreg Clayton 
1154e38a5eddSGreg Clayton         va_list args;
1155e38a5eddSGreg Clayton         va_start(args, format);
1156e38a5eddSGreg Clayton         strm.PrintfVarArg(format, args);
1157e38a5eddSGreg Clayton         va_end(args);
1158e38a5eddSGreg Clayton 
1159e38a5eddSGreg Clayton         const int format_len = strlen(format);
1160b9c1b51eSKate Stone         if (format_len > 0) {
1161e38a5eddSGreg Clayton           const char last_char = format[format_len - 1];
1162376230c9SRaphael Isemann           if (last_char != '\n' && last_char != '\r')
1163e38a5eddSGreg Clayton             strm.EOL();
1164e38a5eddSGreg Clayton         }
1165b9c1b51eSKate Stone         strm.PutCString("The debug session should be aborted as the original "
1166b9c1b51eSKate Stone                         "debug information has been overwritten.\n");
1167c156427dSZachary Turner         Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData());
1168e38a5eddSGreg Clayton       }
1169e38a5eddSGreg Clayton     }
1170c982b3d6SGreg Clayton   }
11711d60909eSGreg Clayton }
1172c982b3d6SGreg Clayton 
1173b9c1b51eSKate Stone void Module::ReportWarning(const char *format, ...) {
1174b9c1b51eSKate Stone   if (format && format[0]) {
1175e38a5eddSGreg Clayton     StreamString strm;
1176e38a5eddSGreg Clayton     strm.PutCString("warning: ");
11778b35334eSGreg Clayton     GetDescription(&strm, lldb::eDescriptionLevelFull);
11788b35334eSGreg Clayton     strm.PutChar(' ');
1179c982b3d6SGreg Clayton 
1180c982b3d6SGreg Clayton     va_list args;
1181c982b3d6SGreg Clayton     va_start(args, format);
1182e38a5eddSGreg Clayton     strm.PrintfVarArg(format, args);
1183c982b3d6SGreg Clayton     va_end(args);
1184e38a5eddSGreg Clayton 
1185e38a5eddSGreg Clayton     const int format_len = strlen(format);
1186b9c1b51eSKate Stone     if (format_len > 0) {
1187e38a5eddSGreg Clayton       const char last_char = format[format_len - 1];
1188376230c9SRaphael Isemann       if (last_char != '\n' && last_char != '\r')
1189e38a5eddSGreg Clayton         strm.EOL();
1190e38a5eddSGreg Clayton     }
1191c156427dSZachary Turner     Host::SystemLog(Host::eSystemLogWarning, "%s", strm.GetData());
1192e38a5eddSGreg Clayton   }
1193c982b3d6SGreg Clayton }
1194c982b3d6SGreg Clayton 
1195b9c1b51eSKate Stone void Module::LogMessage(Log *log, const char *format, ...) {
1196b9c1b51eSKate Stone   if (log != nullptr) {
1197c982b3d6SGreg Clayton     StreamString log_message;
11988b35334eSGreg Clayton     GetDescription(&log_message, lldb::eDescriptionLevelFull);
1199c982b3d6SGreg Clayton     log_message.PutCString(": ");
1200c982b3d6SGreg Clayton     va_list args;
1201c982b3d6SGreg Clayton     va_start(args, format);
1202c982b3d6SGreg Clayton     log_message.PrintfVarArg(format, args);
1203c982b3d6SGreg Clayton     va_end(args);
1204c156427dSZachary Turner     log->PutCString(log_message.GetData());
1205c982b3d6SGreg Clayton   }
1206c982b3d6SGreg Clayton }
1207c982b3d6SGreg Clayton 
1208b9c1b51eSKate Stone void Module::LogMessageVerboseBacktrace(Log *log, const char *format, ...) {
1209b9c1b51eSKate Stone   if (log != nullptr) {
1210d61c0fc0SGreg Clayton     StreamString log_message;
1211d61c0fc0SGreg Clayton     GetDescription(&log_message, lldb::eDescriptionLevelFull);
1212d61c0fc0SGreg Clayton     log_message.PutCString(": ");
1213d61c0fc0SGreg Clayton     va_list args;
1214d61c0fc0SGreg Clayton     va_start(args, format);
1215d61c0fc0SGreg Clayton     log_message.PrintfVarArg(format, args);
1216d61c0fc0SGreg Clayton     va_end(args);
1217b9c1b51eSKate Stone     if (log->GetVerbose()) {
1218a893d301SZachary Turner       std::string back_trace;
1219a893d301SZachary Turner       llvm::raw_string_ostream stream(back_trace);
1220a893d301SZachary Turner       llvm::sys::PrintStackTrace(stream);
1221771ef6d4SMalcolm Parsons       log_message.PutCString(back_trace);
1222a893d301SZachary Turner     }
1223c156427dSZachary Turner     log->PutCString(log_message.GetData());
1224d61c0fc0SGreg Clayton   }
1225d61c0fc0SGreg Clayton }
1226d61c0fc0SGreg Clayton 
1227b9c1b51eSKate Stone void Module::Dump(Stream *s) {
122816ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
12298941142aSGreg Clayton   // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
123030fdc8d8SChris Lattner   s->Indent();
1231b9c1b51eSKate Stone   s->Printf("Module %s%s%s%s\n", m_file.GetPath().c_str(),
123230fdc8d8SChris Lattner             m_object_name ? "(" : "",
123330fdc8d8SChris Lattner             m_object_name ? m_object_name.GetCString() : "",
123430fdc8d8SChris Lattner             m_object_name ? ")" : "");
123530fdc8d8SChris Lattner 
123630fdc8d8SChris Lattner   s->IndentMore();
123730fdc8d8SChris Lattner 
1238a7499c98SMichael Sartain   ObjectFile *objfile = GetObjectFile();
123930fdc8d8SChris Lattner   if (objfile)
124030fdc8d8SChris Lattner     objfile->Dump(s);
124130fdc8d8SChris Lattner 
1242d5d47a35SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
1243d5d47a35SPavel Labath     symbols->Dump(*s);
124430fdc8d8SChris Lattner 
124530fdc8d8SChris Lattner   s->IndentLess();
124630fdc8d8SChris Lattner }
124730fdc8d8SChris Lattner 
12480e4c4821SAdrian Prantl ConstString Module::GetObjectName() const { return m_object_name; }
124930fdc8d8SChris Lattner 
1250b9c1b51eSKate Stone ObjectFile *Module::GetObjectFile() {
1251b9c1b51eSKate Stone   if (!m_did_load_objfile.load()) {
125216ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1253b9c1b51eSKate Stone     if (!m_did_load_objfile.load()) {
1254f9d16476SPavel Labath       static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1255f9d16476SPavel Labath       Timer scoped_timer(func_cat, "Module::GetObjectFile () module = %s",
1256b9c1b51eSKate Stone                          GetFileSpec().GetFilename().AsCString(""));
12575ce9c565SGreg Clayton       DataBufferSP data_sp;
12585ce9c565SGreg Clayton       lldb::offset_t data_offset = 0;
125959b78bcbSJonas Devlieghere       const lldb::offset_t file_size =
126059b78bcbSJonas Devlieghere           FileSystem::Instance().GetByteSize(m_file);
1261b9c1b51eSKate Stone       if (file_size > m_object_offset) {
12622540a8a7SGreg Clayton         m_did_load_objfile = true;
1263b9c1b51eSKate Stone         m_objfile_sp = ObjectFile::FindPlugin(
1264b9c1b51eSKate Stone             shared_from_this(), &m_file, m_object_offset,
1265b9c1b51eSKate Stone             file_size - m_object_offset, data_sp, data_offset);
1266b9c1b51eSKate Stone         if (m_objfile_sp) {
1267b9c1b51eSKate Stone           // Once we get the object file, update our module with the object
126805097246SAdrian Prantl           // file's architecture since it might differ in vendor/os if some
126905097246SAdrian Prantl           // parts were unknown.  But since the matching arch might already be
127005097246SAdrian Prantl           // more specific than the generic COFF architecture, only merge in
127105097246SAdrian Prantl           // those values that overwrite unspecified unknown values.
1272f760f5aeSPavel Labath           m_arch.MergeFrom(m_objfile_sp->GetArchitecture());
1273b9c1b51eSKate Stone         } else {
1274b9c1b51eSKate Stone           ReportError("failed to load objfile for %s",
1275b9c1b51eSKate Stone                       GetFileSpec().GetPath().c_str());
12760ee56ce6STodd Fiala         }
127730fdc8d8SChris Lattner       }
12782540a8a7SGreg Clayton     }
127988c05f54SGreg Clayton   }
1280762f7135SGreg Clayton   return m_objfile_sp.get();
128130fdc8d8SChris Lattner }
128230fdc8d8SChris Lattner 
1283b9c1b51eSKate Stone SectionList *Module::GetSectionList() {
1284d5b44036SJonas Devlieghere   // Populate m_sections_up with sections from objfile.
1285d5b44036SJonas Devlieghere   if (!m_sections_up) {
12863046e668SGreg Clayton     ObjectFile *obj_file = GetObjectFile();
1287c5dac77aSEugene Zelenko     if (obj_file != nullptr)
12883046e668SGreg Clayton       obj_file->CreateSections(*GetUnifiedSectionList());
12893046e668SGreg Clayton   }
1290d5b44036SJonas Devlieghere   return m_sections_up.get();
12913046e668SGreg Clayton }
12923046e668SGreg Clayton 
1293b9c1b51eSKate Stone void Module::SectionFileAddressesChanged() {
129405a09c67SJason Molenda   ObjectFile *obj_file = GetObjectFile();
129505a09c67SJason Molenda   if (obj_file)
129605a09c67SJason Molenda     obj_file->SectionFileAddressesChanged();
129723f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
129823f70e83SPavel Labath     symbols->SectionFileAddressesChanged();
129905a09c67SJason Molenda }
130005a09c67SJason Molenda 
1301dec96392SPavel Labath UnwindTable &Module::GetUnwindTable() {
1302dec96392SPavel Labath   if (!m_unwind_table)
1303dec96392SPavel Labath     m_unwind_table.emplace(*this);
1304dec96392SPavel Labath   return *m_unwind_table;
1305dec96392SPavel Labath }
1306dec96392SPavel Labath 
1307b9c1b51eSKate Stone SectionList *Module::GetUnifiedSectionList() {
1308d5b44036SJonas Devlieghere   if (!m_sections_up)
1309a8f3ae7cSJonas Devlieghere     m_sections_up = std::make_unique<SectionList>();
1310d5b44036SJonas Devlieghere   return m_sections_up.get();
1311a7499c98SMichael Sartain }
131230fdc8d8SChris Lattner 
13130e4c4821SAdrian Prantl const Symbol *Module::FindFirstSymbolWithNameAndType(ConstString name,
1314b9c1b51eSKate Stone                                                      SymbolType symbol_type) {
1315f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1316b9c1b51eSKate Stone   Timer scoped_timer(
1317f9d16476SPavel Labath       func_cat, "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1318b9c1b51eSKate Stone       name.AsCString(), symbol_type);
1319d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab())
1320b9c1b51eSKate Stone     return symtab->FindFirstSymbolWithNameAndType(
1321b9c1b51eSKate Stone         name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
1322c5dac77aSEugene Zelenko   return nullptr;
132330fdc8d8SChris Lattner }
1324b9c1b51eSKate Stone void Module::SymbolIndicesToSymbolContextList(
1325b9c1b51eSKate Stone     Symtab *symtab, std::vector<uint32_t> &symbol_indexes,
1326b9c1b51eSKate Stone     SymbolContextList &sc_list) {
132730fdc8d8SChris Lattner   // No need to protect this call using m_mutex all other method calls are
132830fdc8d8SChris Lattner   // already thread safe.
132930fdc8d8SChris Lattner 
133030fdc8d8SChris Lattner   size_t num_indices = symbol_indexes.size();
1331b9c1b51eSKate Stone   if (num_indices > 0) {
133230fdc8d8SChris Lattner     SymbolContext sc;
133330fdc8d8SChris Lattner     CalculateSymbolContext(&sc);
1334b9c1b51eSKate Stone     for (size_t i = 0; i < num_indices; i++) {
133530fdc8d8SChris Lattner       sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
133630fdc8d8SChris Lattner       if (sc.symbol)
133730fdc8d8SChris Lattner         sc_list.Append(sc);
133830fdc8d8SChris Lattner     }
133930fdc8d8SChris Lattner   }
134030fdc8d8SChris Lattner }
134130fdc8d8SChris Lattner 
13420e4c4821SAdrian Prantl size_t Module::FindFunctionSymbols(ConstString name,
1343c1b2ccfdSGreg Clayton                                    uint32_t name_type_mask,
1344b9c1b51eSKate Stone                                    SymbolContextList &sc_list) {
1345f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1346f9d16476SPavel Labath   Timer scoped_timer(func_cat,
1347c1b2ccfdSGreg Clayton                      "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1348b9c1b51eSKate Stone                      name.AsCString(), name_type_mask);
1349d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab())
1350c1b2ccfdSGreg Clayton     return symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
1351c1b2ccfdSGreg Clayton   return 0;
1352c1b2ccfdSGreg Clayton }
1353c1b2ccfdSGreg Clayton 
13540e4c4821SAdrian Prantl size_t Module::FindSymbolsWithNameAndType(ConstString name,
1355b9c1b51eSKate Stone                                           SymbolType symbol_type,
1356b9c1b51eSKate Stone                                           SymbolContextList &sc_list) {
135730fdc8d8SChris Lattner   // No need to protect this call using m_mutex all other method calls are
135830fdc8d8SChris Lattner   // already thread safe.
135930fdc8d8SChris Lattner 
1360f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1361b9c1b51eSKate Stone   Timer scoped_timer(
1362f9d16476SPavel Labath       func_cat, "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1363b9c1b51eSKate Stone       name.AsCString(), symbol_type);
136430fdc8d8SChris Lattner   const size_t initial_size = sc_list.GetSize();
1365d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab()) {
136630fdc8d8SChris Lattner     std::vector<uint32_t> symbol_indexes;
136730fdc8d8SChris Lattner     symtab->FindAllSymbolsWithNameAndType(name, symbol_type, symbol_indexes);
136830fdc8d8SChris Lattner     SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
136930fdc8d8SChris Lattner   }
137030fdc8d8SChris Lattner   return sc_list.GetSize() - initial_size;
137130fdc8d8SChris Lattner }
137230fdc8d8SChris Lattner 
1373b9c1b51eSKate Stone size_t Module::FindSymbolsMatchingRegExAndType(const RegularExpression &regex,
1374b9c1b51eSKate Stone                                                SymbolType symbol_type,
1375b9c1b51eSKate Stone                                                SymbolContextList &sc_list) {
137630fdc8d8SChris Lattner   // No need to protect this call using m_mutex all other method calls are
137730fdc8d8SChris Lattner   // already thread safe.
137830fdc8d8SChris Lattner 
1379f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1380b9c1b51eSKate Stone   Timer scoped_timer(
1381f9d16476SPavel Labath       func_cat,
138230fdc8d8SChris Lattner       "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
138395eae423SZachary Turner       regex.GetText().str().c_str(), symbol_type);
138430fdc8d8SChris Lattner   const size_t initial_size = sc_list.GetSize();
1385d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab()) {
138630fdc8d8SChris Lattner     std::vector<uint32_t> symbol_indexes;
1387b9c1b51eSKate Stone     symtab->FindAllSymbolsMatchingRexExAndType(
1388b9c1b51eSKate Stone         regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny,
1389b9c1b51eSKate Stone         symbol_indexes);
139030fdc8d8SChris Lattner     SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
139130fdc8d8SChris Lattner   }
139230fdc8d8SChris Lattner   return sc_list.GetSize() - initial_size;
139330fdc8d8SChris Lattner }
139430fdc8d8SChris Lattner 
13957fca8c07SJim Ingham void Module::PreloadSymbols() {
13967fca8c07SJim Ingham   std::lock_guard<std::recursive_mutex> guard(m_mutex);
139723f70e83SPavel Labath   SymbolFile *sym_file = GetSymbolFile();
139823f70e83SPavel Labath   if (!sym_file)
13997fca8c07SJim Ingham     return;
140023f70e83SPavel Labath 
14017fca8c07SJim Ingham   // Prime the symbol file first, since it adds symbols to the symbol table.
140223f70e83SPavel Labath   sym_file->PreloadSymbols();
140323f70e83SPavel Labath 
14047fca8c07SJim Ingham   // Now we can prime the symbol table.
140523f70e83SPavel Labath   if (Symtab *symtab = sym_file->GetSymtab())
14067fca8c07SJim Ingham     symtab->PreloadSymbols();
14077fca8c07SJim Ingham }
14087fca8c07SJim Ingham 
1409b9c1b51eSKate Stone void Module::SetSymbolFileFileSpec(const FileSpec &file) {
1410dbd7fabaSJonas Devlieghere   if (!FileSystem::Instance().Exists(file))
141190271672SGreg Clayton     return;
1412d5b44036SJonas Devlieghere   if (m_symfile_up) {
1413b9c1b51eSKate Stone     // Remove any sections in the unified section list that come from the
1414b9c1b51eSKate Stone     // current symbol vendor.
14153046e668SGreg Clayton     SectionList *section_list = GetSectionList();
141623f70e83SPavel Labath     SymbolFile *symbol_file = GetSymbolFile();
1417b9c1b51eSKate Stone     if (section_list && symbol_file) {
1418a7499c98SMichael Sartain       ObjectFile *obj_file = symbol_file->GetObjectFile();
1419b9c1b51eSKate Stone       // Make sure we have an object file and that the symbol vendor's objfile
142005097246SAdrian Prantl       // isn't the same as the module's objfile before we remove any sections
142105097246SAdrian Prantl       // for it...
1422b9c1b51eSKate Stone       if (obj_file) {
1423b9c1b51eSKate Stone         // Check to make sure we aren't trying to specify the file we already
1424b9c1b51eSKate Stone         // have
1425b9c1b51eSKate Stone         if (obj_file->GetFileSpec() == file) {
142690271672SGreg Clayton           // We are being told to add the exact same file that we already have
142790271672SGreg Clayton           // we don't have to do anything.
142890271672SGreg Clayton           return;
142990271672SGreg Clayton         }
1430d00438e8STamas Berghammer 
1431b9c1b51eSKate Stone         // Cleare the current symtab as we are going to replace it with a new
1432b9c1b51eSKate Stone         // one
1433d00438e8STamas Berghammer         obj_file->ClearSymtab();
143490271672SGreg Clayton 
1435dec96392SPavel Labath         // Clear the unwind table too, as that may also be affected by the
1436dec96392SPavel Labath         // symbol file information.
1437dec96392SPavel Labath         m_unwind_table.reset();
1438dec96392SPavel Labath 
1439b9c1b51eSKate Stone         // The symbol file might be a directory bundle ("/tmp/a.out.dSYM")
144005097246SAdrian Prantl         // instead of a full path to the symbol file within the bundle
1441b9c1b51eSKate Stone         // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to
1442b9c1b51eSKate Stone         // check this
144390271672SGreg Clayton 
14443a58d898SJonas Devlieghere         if (FileSystem::Instance().IsDirectory(file)) {
144590271672SGreg Clayton           std::string new_path(file.GetPath());
144690271672SGreg Clayton           std::string old_path(obj_file->GetFileSpec().GetPath());
1447b9c1b51eSKate Stone           if (old_path.find(new_path) == 0) {
1448b9c1b51eSKate Stone             // We specified the same bundle as the symbol file that we already
1449b9c1b51eSKate Stone             // have
145090271672SGreg Clayton             return;
145190271672SGreg Clayton           }
145290271672SGreg Clayton         }
145390271672SGreg Clayton 
1454b9c1b51eSKate Stone         if (obj_file != m_objfile_sp.get()) {
1455a7499c98SMichael Sartain           size_t num_sections = section_list->GetNumSections(0);
1456b9c1b51eSKate Stone           for (size_t idx = num_sections; idx > 0; --idx) {
1457b9c1b51eSKate Stone             lldb::SectionSP section_sp(
1458b9c1b51eSKate Stone                 section_list->GetSectionAtIndex(idx - 1));
1459b9c1b51eSKate Stone             if (section_sp->GetObjectFile() == obj_file) {
14603046e668SGreg Clayton               section_list->DeleteSection(idx - 1);
1461a7499c98SMichael Sartain             }
1462a7499c98SMichael Sartain           }
1463a7499c98SMichael Sartain         }
1464a7499c98SMichael Sartain       }
1465a7499c98SMichael Sartain     }
1466b9c1b51eSKate Stone     // Keep all old symbol files around in case there are any lingering type
146705097246SAdrian Prantl     // references in any SBValue objects that might have been handed out.
1468d5b44036SJonas Devlieghere     m_old_symfiles.push_back(std::move(m_symfile_up));
146990271672SGreg Clayton   }
1470e01e07b6SGreg Clayton   m_symfile_spec = file;
1471d5b44036SJonas Devlieghere   m_symfile_up.reset();
1472579d6d1aSPavel Labath   m_did_load_symfile = false;
1473e01e07b6SGreg Clayton }
1474e01e07b6SGreg Clayton 
1475b9c1b51eSKate Stone bool Module::IsExecutable() {
1476c5dac77aSEugene Zelenko   if (GetObjectFile() == nullptr)
14775aee162fSJim Ingham     return false;
14785aee162fSJim Ingham   else
14795aee162fSJim Ingham     return GetObjectFile()->IsExecutable();
14805aee162fSJim Ingham }
14815aee162fSJim Ingham 
1482b9c1b51eSKate Stone bool Module::IsLoadedInTarget(Target *target) {
1483b53cb271SJim Ingham   ObjectFile *obj_file = GetObjectFile();
1484b9c1b51eSKate Stone   if (obj_file) {
14853046e668SGreg Clayton     SectionList *sections = GetSectionList();
1486b9c1b51eSKate Stone     if (sections != nullptr) {
1487b53cb271SJim Ingham       size_t num_sections = sections->GetSize();
1488b9c1b51eSKate Stone       for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) {
1489b53cb271SJim Ingham         SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1490b9c1b51eSKate Stone         if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) {
1491b53cb271SJim Ingham           return true;
1492b53cb271SJim Ingham         }
1493b53cb271SJim Ingham       }
1494b53cb271SJim Ingham     }
1495b53cb271SJim Ingham   }
1496b53cb271SJim Ingham   return false;
1497b53cb271SJim Ingham }
14981759848bSEnrico Granata 
149997206d57SZachary Turner bool Module::LoadScriptingResourceInTarget(Target *target, Status &error,
1500b9c1b51eSKate Stone                                            Stream *feedback_stream) {
1501b9c1b51eSKate Stone   if (!target) {
15021759848bSEnrico Granata     error.SetErrorString("invalid destination Target");
15031759848bSEnrico Granata     return false;
15041759848bSEnrico Granata   }
15051759848bSEnrico Granata 
1506b9c1b51eSKate Stone   LoadScriptFromSymFile should_load =
1507b9c1b51eSKate Stone       target->TargetProperties::GetLoadScriptFromSymbolFile();
15082ea43cdcSEnrico Granata 
1509994740fbSGreg Clayton   if (should_load == eLoadScriptFromSymFileFalse)
1510994740fbSGreg Clayton     return false;
1511994740fbSGreg Clayton 
151291c0e749SGreg Clayton   Debugger &debugger = target->GetDebugger();
151391c0e749SGreg Clayton   const ScriptLanguage script_language = debugger.GetScriptLanguage();
1514b9c1b51eSKate Stone   if (script_language != eScriptLanguageNone) {
151591c0e749SGreg Clayton 
15161759848bSEnrico Granata     PlatformSP platform_sp(target->GetPlatform());
15171759848bSEnrico Granata 
1518b9c1b51eSKate Stone     if (!platform_sp) {
15191759848bSEnrico Granata       error.SetErrorString("invalid Platform");
15201759848bSEnrico Granata       return false;
15211759848bSEnrico Granata     }
15221759848bSEnrico Granata 
1523b9c1b51eSKate Stone     FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources(
1524b9c1b51eSKate Stone         target, *this, feedback_stream);
152591c0e749SGreg Clayton 
152691c0e749SGreg Clayton     const uint32_t num_specs = file_specs.GetSize();
1527b9c1b51eSKate Stone     if (num_specs) {
15282b29b432SJonas Devlieghere       ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
1529b9c1b51eSKate Stone       if (script_interpreter) {
1530b9c1b51eSKate Stone         for (uint32_t i = 0; i < num_specs; ++i) {
153191c0e749SGreg Clayton           FileSpec scripting_fspec(file_specs.GetFileSpecAtIndex(i));
1532dbd7fabaSJonas Devlieghere           if (scripting_fspec &&
1533dbd7fabaSJonas Devlieghere               FileSystem::Instance().Exists(scripting_fspec)) {
1534b9c1b51eSKate Stone             if (should_load == eLoadScriptFromSymFileWarn) {
1535397ddd5fSEnrico Granata               if (feedback_stream)
1536b9c1b51eSKate Stone                 feedback_stream->Printf(
1537b9c1b51eSKate Stone                     "warning: '%s' contains a debug script. To run this script "
1538b9c1b51eSKate Stone                     "in "
1539b9c1b51eSKate Stone                     "this debug session:\n\n    command script import "
1540b9c1b51eSKate Stone                     "\"%s\"\n\n"
1541d516deb4SJim Ingham                     "To run all discovered debug scripts in this session:\n\n"
1542b9c1b51eSKate Stone                     "    settings set target.load-script-from-symbol-file "
1543b9c1b51eSKate Stone                     "true\n",
1544d516deb4SJim Ingham                     GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1545d516deb4SJim Ingham                     scripting_fspec.GetPath().c_str());
15462ea43cdcSEnrico Granata               return false;
15472ea43cdcSEnrico Granata             }
15481759848bSEnrico Granata             StreamString scripting_stream;
15491759848bSEnrico Granata             scripting_fspec.Dump(&scripting_stream);
1550e0c70f1bSEnrico Granata             const bool can_reload = true;
15516a51085eSJim Ingham             const bool init_lldb_globals = false;
1552b9c1b51eSKate Stone             bool did_load = script_interpreter->LoadScriptingModule(
1553b9c1b51eSKate Stone                 scripting_stream.GetData(), can_reload, init_lldb_globals,
1554d516deb4SJim Ingham                 error);
15551759848bSEnrico Granata             if (!did_load)
15561759848bSEnrico Granata               return false;
15571759848bSEnrico Granata           }
155891c0e749SGreg Clayton         }
1559b9c1b51eSKate Stone       } else {
15601759848bSEnrico Granata         error.SetErrorString("invalid ScriptInterpreter");
15611759848bSEnrico Granata         return false;
15621759848bSEnrico Granata       }
15631759848bSEnrico Granata     }
1564b9d8890bSGreg Clayton   }
15651759848bSEnrico Granata   return true;
15661759848bSEnrico Granata }
15671759848bSEnrico Granata 
1568b9c1b51eSKate Stone bool Module::SetArchitecture(const ArchSpec &new_arch) {
1569b9c1b51eSKate Stone   if (!m_arch.IsValid()) {
15705aee162fSJim Ingham     m_arch = new_arch;
15715aee162fSJim Ingham     return true;
15725aee162fSJim Ingham   }
1573b6cd5fe9SChaoren Lin   return m_arch.IsCompatibleMatch(new_arch);
15745aee162fSJim Ingham }
15755aee162fSJim Ingham 
1576b9c1b51eSKate Stone bool Module::SetLoadAddress(Target &target, lldb::addr_t value,
1577b9c1b51eSKate Stone                             bool value_is_offset, bool &changed) {
15789e02dacdSSteve Pucci   ObjectFile *object_file = GetObjectFile();
1579b9c1b51eSKate Stone   if (object_file != nullptr) {
1580751caf65SGreg Clayton     changed = object_file->SetLoadAddress(target, value, value_is_offset);
15817524e090SGreg Clayton     return true;
1582b9c1b51eSKate Stone   } else {
15837524e090SGreg Clayton     changed = false;
1584c9660546SGreg Clayton   }
15859e02dacdSSteve Pucci   return false;
1586c9660546SGreg Clayton }
1587c9660546SGreg Clayton 
1588b9c1b51eSKate Stone bool Module::MatchesModuleSpec(const ModuleSpec &module_ref) {
1589b9a01b39SGreg Clayton   const UUID &uuid = module_ref.GetUUID();
1590b9a01b39SGreg Clayton 
1591b9c1b51eSKate Stone   if (uuid.IsValid()) {
1592b9a01b39SGreg Clayton     // If the UUID matches, then nothing more needs to match...
1593c5dac77aSEugene Zelenko     return (uuid == GetUUID());
1594b9a01b39SGreg Clayton   }
1595b9a01b39SGreg Clayton 
1596b9a01b39SGreg Clayton   const FileSpec &file_spec = module_ref.GetFileSpec();
1597b9c1b51eSKate Stone   if (file_spec) {
1598980662eeSTamas Berghammer     if (!FileSpec::Equal(file_spec, m_file, (bool)file_spec.GetDirectory()) &&
1599b9c1b51eSKate Stone         !FileSpec::Equal(file_spec, m_platform_file,
1600b9c1b51eSKate Stone                          (bool)file_spec.GetDirectory()))
1601b9a01b39SGreg Clayton       return false;
1602b9a01b39SGreg Clayton   }
1603b9a01b39SGreg Clayton 
1604b9a01b39SGreg Clayton   const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1605b9c1b51eSKate Stone   if (platform_file_spec) {
1606b9c1b51eSKate Stone     if (!FileSpec::Equal(platform_file_spec, GetPlatformFileSpec(),
1607b9c1b51eSKate Stone                          (bool)platform_file_spec.GetDirectory()))
1608b9a01b39SGreg Clayton       return false;
1609b9a01b39SGreg Clayton   }
1610b9a01b39SGreg Clayton 
1611b9a01b39SGreg Clayton   const ArchSpec &arch = module_ref.GetArchitecture();
1612b9c1b51eSKate Stone   if (arch.IsValid()) {
1613bf4b7be6SSean Callanan     if (!m_arch.IsCompatibleMatch(arch))
1614b9a01b39SGreg Clayton       return false;
1615b9a01b39SGreg Clayton   }
1616b9a01b39SGreg Clayton 
16170e4c4821SAdrian Prantl   ConstString object_name = module_ref.GetObjectName();
1618b9c1b51eSKate Stone   if (object_name) {
1619b9a01b39SGreg Clayton     if (object_name != GetObjectName())
1620b9a01b39SGreg Clayton       return false;
1621b9a01b39SGreg Clayton   }
1622b9a01b39SGreg Clayton   return true;
1623b9a01b39SGreg Clayton }
1624b9a01b39SGreg Clayton 
1625b9c1b51eSKate Stone bool Module::FindSourceFile(const FileSpec &orig_spec,
1626b9c1b51eSKate Stone                             FileSpec &new_spec) const {
162716ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1628d804d285SGreg Clayton   return m_source_mappings.FindFile(orig_spec, new_spec);
1629d804d285SGreg Clayton }
1630d804d285SGreg Clayton 
1631a498f0ecSZachary Turner bool Module::RemapSourceFile(llvm::StringRef path,
1632a498f0ecSZachary Turner                              std::string &new_path) const {
163316ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1634f9be6933SGreg Clayton   return m_source_mappings.RemapPath(path, new_path);
1635f9be6933SGreg Clayton }
1636f9be6933SGreg Clayton 
16372272c481SPavel Labath llvm::VersionTuple Module::GetVersion() {
16382272c481SPavel Labath   if (ObjectFile *obj_file = GetObjectFile())
16392272c481SPavel Labath     return obj_file->GetVersion();
16402272c481SPavel Labath   return llvm::VersionTuple();
16413467d80bSEnrico Granata }
164243fe217bSGreg Clayton 
1643b9c1b51eSKate Stone bool Module::GetIsDynamicLinkEditor() {
164408928f30SGreg Clayton   ObjectFile *obj_file = GetObjectFile();
164508928f30SGreg Clayton 
164608928f30SGreg Clayton   if (obj_file)
164708928f30SGreg Clayton     return obj_file->GetIsDynamicLinkEditor();
164808928f30SGreg Clayton 
164908928f30SGreg Clayton   return false;
165008928f30SGreg Clayton }
1651