180814287SRaphael Isemann //===-- Module.cpp --------------------------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
9c5dac77aSEugene Zelenko #include "lldb/Core/Module.h"
10c5dac77aSEugene Zelenko 
11672d2c12SJonas Devlieghere #include "lldb/Core/AddressRange.h"
12f86248d9SRichard Mitton #include "lldb/Core/AddressResolverFileLine.h"
13672d2c12SJonas Devlieghere #include "lldb/Core/Debugger.h"
14672d2c12SJonas Devlieghere #include "lldb/Core/FileSpecList.h"
15672d2c12SJonas Devlieghere #include "lldb/Core/Mangled.h"
161f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
17672d2c12SJonas Devlieghere #include "lldb/Core/SearchFilter.h"
181f746071SGreg Clayton #include "lldb/Core/Section.h"
191408bf72SPavel Labath #include "lldb/Host/FileSystem.h"
20e38a5eddSGreg Clayton #include "lldb/Host/Host.h"
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);
300256e6169SPaolo Severini       if (bytes_read < size_to_read)
301256e6169SPaolo Severini         data_up->SetByteSize(bytes_read);
302256e6169SPaolo Severini       if (data_up->GetByteSize() > 0) {
303d5b44036SJonas Devlieghere         DataBufferSP data_sp(data_up.release());
304b9c1b51eSKate Stone         m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp,
305b9c1b51eSKate Stone                                               header_addr, data_sp);
306b9c1b51eSKate Stone         if (m_objfile_sp) {
3073e10cf3bSGreg Clayton           StreamString s;
308d01b2953SDaniel Malea           s.Printf("0x%16.16" PRIx64, header_addr);
309c156427dSZachary Turner           m_object_name.SetString(s.GetString());
3103e10cf3bSGreg Clayton 
311b9c1b51eSKate Stone           // Once we get the object file, update our module with the object
31205097246SAdrian Prantl           // file's architecture since it might differ in vendor/os if some
31305097246SAdrian Prantl           // parts were unknown.
314f760f5aeSPavel Labath           m_arch = m_objfile_sp->GetArchitecture();
315a07287ecSAlex Langford 
316a07287ecSAlex Langford           // Augment the arch with the target's information in case
317a07287ecSAlex Langford           // we are unable to extract the os/environment from memory.
318a07287ecSAlex Langford           m_arch.MergeFrom(process_sp->GetTarget().GetArchitecture());
319b9c1b51eSKate Stone         } else {
320c7f09ccaSGreg Clayton           error.SetErrorString("unable to find suitable object file plug-in");
321c7f09ccaSGreg Clayton         }
322b9c1b51eSKate Stone       } else {
323b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unable to read header from memory: %s",
324b9c1b51eSKate Stone                                        readmem_error.AsCString());
325c7f09ccaSGreg Clayton       }
326b9c1b51eSKate Stone     } else {
327c7f09ccaSGreg Clayton       error.SetErrorString("invalid process");
328c7f09ccaSGreg Clayton     }
329c7f09ccaSGreg Clayton   }
330c7f09ccaSGreg Clayton   return m_objfile_sp.get();
331c7f09ccaSGreg Clayton }
332c7f09ccaSGreg Clayton 
333b9c1b51eSKate Stone const lldb_private::UUID &Module::GetUUID() {
3349fecd372SLeonard Mosescu   if (!m_did_set_uuid.load()) {
33516ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
3369fecd372SLeonard Mosescu     if (!m_did_set_uuid.load()) {
33730fdc8d8SChris Lattner       ObjectFile *obj_file = GetObjectFile();
33830fdc8d8SChris Lattner 
339b9c1b51eSKate Stone       if (obj_file != nullptr) {
340bd334efdSPavel Labath         m_uuid = obj_file->GetUUID();
3419fecd372SLeonard Mosescu         m_did_set_uuid = true;
34230fdc8d8SChris Lattner       }
34330fdc8d8SChris Lattner     }
34488c05f54SGreg Clayton   }
34530fdc8d8SChris Lattner   return m_uuid;
34630fdc8d8SChris Lattner }
34730fdc8d8SChris Lattner 
3489fecd372SLeonard Mosescu void Module::SetUUID(const lldb_private::UUID &uuid) {
3499fecd372SLeonard Mosescu   std::lock_guard<std::recursive_mutex> guard(m_mutex);
3509fecd372SLeonard Mosescu   if (!m_did_set_uuid) {
3519fecd372SLeonard Mosescu     m_uuid = uuid;
3529fecd372SLeonard Mosescu     m_did_set_uuid = true;
3539fecd372SLeonard Mosescu   } else {
354fbe748aeSRichard Smith     lldbassert(0 && "Attempting to overwrite the existing module UUID");
3559fecd372SLeonard Mosescu   }
3569fecd372SLeonard Mosescu }
3579fecd372SLeonard Mosescu 
3580e252e38SAlex Langford llvm::Expected<TypeSystem &>
3590e252e38SAlex Langford Module::GetTypeSystemForLanguage(LanguageType language) {
3605beec213SGreg Clayton   return m_type_system_map.GetTypeSystemForLanguage(language, this, true);
3616beaaa68SGreg Clayton }
3626beaaa68SGreg Clayton 
363b9c1b51eSKate Stone void Module::ParseAllDebugSymbols() {
36416ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
365c7bece56SGreg Clayton   size_t num_comp_units = GetNumCompileUnits();
36630fdc8d8SChris Lattner   if (num_comp_units == 0)
36730fdc8d8SChris Lattner     return;
36830fdc8d8SChris Lattner 
36923f70e83SPavel Labath   SymbolFile *symbols = GetSymbolFile();
37030fdc8d8SChris Lattner 
371b9c1b51eSKate Stone   for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++) {
3728131cb6eSPavel Labath     SymbolContext sc;
3738131cb6eSPavel Labath     sc.module_sp = shared_from_this();
37430fdc8d8SChris Lattner     sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
375863f8c18SZachary Turner     if (!sc.comp_unit)
376863f8c18SZachary Turner       continue;
377863f8c18SZachary Turner 
37830fdc8d8SChris Lattner     symbols->ParseVariablesForContext(sc);
37930fdc8d8SChris Lattner 
380863f8c18SZachary Turner     symbols->ParseFunctions(*sc.comp_unit);
38130fdc8d8SChris Lattner 
382a7f19e5fSRaphael Isemann     sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) {
383ffc1b8fdSZachary Turner       symbols->ParseBlocksRecursive(*f);
384ffc1b8fdSZachary Turner 
38530fdc8d8SChris Lattner       // Parse the variables for this function and all its blocks
386ffc1b8fdSZachary Turner       sc.function = f.get();
38730fdc8d8SChris Lattner       symbols->ParseVariablesForContext(sc);
388a7f19e5fSRaphael Isemann       return false;
389a7f19e5fSRaphael Isemann     });
39030fdc8d8SChris Lattner 
39130fdc8d8SChris Lattner     // Parse all types for this compile unit
392863f8c18SZachary Turner     symbols->ParseTypes(*sc.comp_unit);
39330fdc8d8SChris Lattner   }
39430fdc8d8SChris Lattner }
39530fdc8d8SChris Lattner 
396b9c1b51eSKate Stone void Module::CalculateSymbolContext(SymbolContext *sc) {
397e1cd1be6SGreg Clayton   sc->module_sp = shared_from_this();
39830fdc8d8SChris Lattner }
39930fdc8d8SChris Lattner 
400b9c1b51eSKate Stone ModuleSP Module::CalculateSymbolContextModule() { return shared_from_this(); }
4017e9b1fd0SGreg Clayton 
402b9c1b51eSKate Stone void Module::DumpSymbolContext(Stream *s) {
403324a1036SSaleem Abdulrasool   s->Printf(", Module{%p}", static_cast<void *>(this));
40430fdc8d8SChris Lattner }
40530fdc8d8SChris Lattner 
406b9c1b51eSKate Stone size_t Module::GetNumCompileUnits() {
40716ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
408f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
409f9d16476SPavel Labath   Timer scoped_timer(func_cat, "Module::GetNumCompileUnits (module = %p)",
410324a1036SSaleem Abdulrasool                      static_cast<void *>(this));
41123f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
41230fdc8d8SChris Lattner     return symbols->GetNumCompileUnits();
41330fdc8d8SChris Lattner   return 0;
41430fdc8d8SChris Lattner }
41530fdc8d8SChris Lattner 
416b9c1b51eSKate Stone CompUnitSP Module::GetCompileUnitAtIndex(size_t index) {
41716ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
418c7bece56SGreg Clayton   size_t num_comp_units = GetNumCompileUnits();
41930fdc8d8SChris Lattner   CompUnitSP cu_sp;
42030fdc8d8SChris Lattner 
421b9c1b51eSKate Stone   if (index < num_comp_units) {
42223f70e83SPavel Labath     if (SymbolFile *symbols = GetSymbolFile())
42330fdc8d8SChris Lattner       cu_sp = symbols->GetCompileUnitAtIndex(index);
42430fdc8d8SChris Lattner   }
42530fdc8d8SChris Lattner   return cu_sp;
42630fdc8d8SChris Lattner }
42730fdc8d8SChris Lattner 
428b9c1b51eSKate Stone bool Module::ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr) {
42916ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
430f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
431f9d16476SPavel Labath   Timer scoped_timer(func_cat,
432b9c1b51eSKate Stone                      "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")",
433b9c1b51eSKate Stone                      vm_addr);
4343046e668SGreg Clayton   SectionList *section_list = GetSectionList();
4353046e668SGreg Clayton   if (section_list)
4363046e668SGreg Clayton     return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
43730fdc8d8SChris Lattner   return false;
43830fdc8d8SChris Lattner }
43930fdc8d8SChris Lattner 
440b9c1b51eSKate Stone uint32_t Module::ResolveSymbolContextForAddress(
441991e4453SZachary Turner     const Address &so_addr, lldb::SymbolContextItem resolve_scope,
442991e4453SZachary Turner     SymbolContext &sc, bool resolve_tail_call_address) {
44316ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
44430fdc8d8SChris Lattner   uint32_t resolved_flags = 0;
44530fdc8d8SChris Lattner 
446b9c1b51eSKate Stone   // Clear the result symbol context in case we don't find anything, but don't
447b9c1b51eSKate Stone   // clear the target
44872310355SGreg Clayton   sc.Clear(false);
44930fdc8d8SChris Lattner 
45030fdc8d8SChris Lattner   // Get the section from the section/offset address.
451e72dfb32SGreg Clayton   SectionSP section_sp(so_addr.GetSection());
45230fdc8d8SChris Lattner 
45330fdc8d8SChris Lattner   // Make sure the section matches this module before we try and match anything
454b9c1b51eSKate Stone   if (section_sp && section_sp->GetModule().get() == this) {
45505097246SAdrian Prantl     // If the section offset based address resolved itself, then this is the
45605097246SAdrian Prantl     // right module.
457e1cd1be6SGreg Clayton     sc.module_sp = shared_from_this();
45830fdc8d8SChris Lattner     resolved_flags |= eSymbolContextModule;
45930fdc8d8SChris Lattner 
46023f70e83SPavel Labath     SymbolFile *symfile = GetSymbolFile();
46123f70e83SPavel Labath     if (!symfile)
46238807141SAshok Thirumurthi       return resolved_flags;
46338807141SAshok Thirumurthi 
46405097246SAdrian Prantl     // Resolve the compile unit, function, block, line table or line entry if
46505097246SAdrian Prantl     // requested.
46630fdc8d8SChris Lattner     if (resolve_scope & eSymbolContextCompUnit ||
46730fdc8d8SChris Lattner         resolve_scope & eSymbolContextFunction ||
46830fdc8d8SChris Lattner         resolve_scope & eSymbolContextBlock ||
4694c8e7828SGreg Clayton         resolve_scope & eSymbolContextLineEntry ||
470b9c1b51eSKate Stone         resolve_scope & eSymbolContextVariable) {
471b9c1b51eSKate Stone       resolved_flags |=
47223f70e83SPavel Labath           symfile->ResolveSymbolContext(so_addr, resolve_scope, sc);
47330fdc8d8SChris Lattner     }
47430fdc8d8SChris Lattner 
47505097246SAdrian Prantl     // Resolve the symbol if requested, but don't re-look it up if we've
47605097246SAdrian Prantl     // already found it.
477b9c1b51eSKate Stone     if (resolve_scope & eSymbolContextSymbol &&
478b9c1b51eSKate Stone         !(resolved_flags & eSymbolContextSymbol)) {
47923f70e83SPavel Labath       Symtab *symtab = symfile->GetSymtab();
480b9c1b51eSKate Stone       if (symtab && so_addr.IsSectionOffset()) {
4810d9dd7dfSMohit K. Bhakkad         Symbol *matching_symbol = nullptr;
482c35b91ceSAdrian McCarthy 
483b9c1b51eSKate Stone         symtab->ForEachSymbolContainingFileAddress(
484b9c1b51eSKate Stone             so_addr.GetFileAddress(),
485c35b91ceSAdrian McCarthy             [&matching_symbol](Symbol *symbol) -> bool {
486b9c1b51eSKate Stone               if (symbol->GetType() != eSymbolTypeInvalid) {
4870d9dd7dfSMohit K. Bhakkad                 matching_symbol = symbol;
4880d9dd7dfSMohit K. Bhakkad                 return false; // Stop iterating
4890d9dd7dfSMohit K. Bhakkad               }
4900d9dd7dfSMohit K. Bhakkad               return true; // Keep iterating
4910d9dd7dfSMohit K. Bhakkad             });
4920d9dd7dfSMohit K. Bhakkad         sc.symbol = matching_symbol;
493b9c1b51eSKate Stone         if (!sc.symbol && resolve_scope & eSymbolContextFunction &&
494b9c1b51eSKate Stone             !(resolved_flags & eSymbolContextFunction)) {
495b9c1b51eSKate Stone           bool verify_unique = false; // No need to check again since
496b9c1b51eSKate Stone                                       // ResolveSymbolContext failed to find a
497b9c1b51eSKate Stone                                       // symbol at this address.
49835729bb1SAshok Thirumurthi           if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
499b9c1b51eSKate Stone             sc.symbol =
500b9c1b51eSKate Stone                 obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
50135729bb1SAshok Thirumurthi         }
50235729bb1SAshok Thirumurthi 
503b9c1b51eSKate Stone         if (sc.symbol) {
504b9c1b51eSKate Stone           if (sc.symbol->IsSynthetic()) {
50505097246SAdrian Prantl             // We have a synthetic symbol so lets check if the object file from
50605097246SAdrian Prantl             // the symbol file in the symbol vendor is different than the
50705097246SAdrian Prantl             // object file for the module, and if so search its symbol table to
50805097246SAdrian Prantl             // see if we can come up with a better symbol. For example dSYM
50905097246SAdrian Prantl             // files on MacOSX have an unstripped symbol table inside of them.
51093e2861bSGreg Clayton             ObjectFile *symtab_objfile = symtab->GetObjectFile();
511b9c1b51eSKate Stone             if (symtab_objfile && symtab_objfile->IsStripped()) {
51293e2861bSGreg Clayton               ObjectFile *symfile_objfile = symfile->GetObjectFile();
513b9c1b51eSKate Stone               if (symfile_objfile != symtab_objfile) {
51493e2861bSGreg Clayton                 Symtab *symfile_symtab = symfile_objfile->GetSymtab();
515b9c1b51eSKate Stone                 if (symfile_symtab) {
516b9c1b51eSKate Stone                   Symbol *symbol =
517b9c1b51eSKate Stone                       symfile_symtab->FindSymbolContainingFileAddress(
518b9c1b51eSKate Stone                           so_addr.GetFileAddress());
519b9c1b51eSKate Stone                   if (symbol && !symbol->IsSynthetic()) {
52093e2861bSGreg Clayton                     sc.symbol = symbol;
52193e2861bSGreg Clayton                   }
52293e2861bSGreg Clayton                 }
52393e2861bSGreg Clayton               }
52493e2861bSGreg Clayton             }
52593e2861bSGreg Clayton           }
52630fdc8d8SChris Lattner           resolved_flags |= eSymbolContextSymbol;
52730fdc8d8SChris Lattner         }
52830fdc8d8SChris Lattner       }
52993e2861bSGreg Clayton     }
53038807141SAshok Thirumurthi 
531b9c1b51eSKate Stone     // For function symbols, so_addr may be off by one.  This is a convention
53205097246SAdrian Prantl     // consistent with FDE row indices in eh_frame sections, but requires extra
53305097246SAdrian Prantl     // logic here to permit symbol lookup for disassembly and unwind.
534b9c1b51eSKate Stone     if (resolve_scope & eSymbolContextSymbol &&
535b9c1b51eSKate Stone         !(resolved_flags & eSymbolContextSymbol) && resolve_tail_call_address &&
536b9c1b51eSKate Stone         so_addr.IsSectionOffset()) {
53738807141SAshok Thirumurthi       Address previous_addr = so_addr;
538edfaae39SGreg Clayton       previous_addr.Slide(-1);
53938807141SAshok Thirumurthi 
54035729bb1SAshok Thirumurthi       bool do_resolve_tail_call_address = false; // prevent recursion
541b9c1b51eSKate Stone       const uint32_t flags = ResolveSymbolContextForAddress(
542b9c1b51eSKate Stone           previous_addr, resolve_scope, sc, do_resolve_tail_call_address);
543b9c1b51eSKate Stone       if (flags & eSymbolContextSymbol) {
54438807141SAshok Thirumurthi         AddressRange addr_range;
545b9c1b51eSKate Stone         if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
546b9c1b51eSKate Stone                                false, addr_range)) {
547b9c1b51eSKate Stone           if (addr_range.GetBaseAddress().GetSection() ==
548b9c1b51eSKate Stone               so_addr.GetSection()) {
549b9c1b51eSKate Stone             // If the requested address is one past the address range of a
55005097246SAdrian Prantl             // function (i.e. a tail call), or the decremented address is the
55105097246SAdrian Prantl             // start of a function (i.e. some forms of trampoline), indicate
55205097246SAdrian Prantl             // that the symbol has been resolved.
553b9c1b51eSKate Stone             if (so_addr.GetOffset() ==
554b9c1b51eSKate Stone                     addr_range.GetBaseAddress().GetOffset() ||
555b9c1b51eSKate Stone                 so_addr.GetOffset() ==
556b9c1b51eSKate Stone                     addr_range.GetBaseAddress().GetOffset() +
557b9c1b51eSKate Stone                         addr_range.GetByteSize()) {
55838807141SAshok Thirumurthi               resolved_flags |= flags;
55938807141SAshok Thirumurthi             }
560b9c1b51eSKate Stone           } else {
561b9c1b51eSKate Stone             sc.symbol =
562b9c1b51eSKate Stone                 nullptr; // Don't trust the symbol if the sections didn't match.
56338807141SAshok Thirumurthi           }
56438807141SAshok Thirumurthi         }
56530fdc8d8SChris Lattner       }
56630fdc8d8SChris Lattner     }
56730fdc8d8SChris Lattner   }
56830fdc8d8SChris Lattner   return resolved_flags;
56930fdc8d8SChris Lattner }
57030fdc8d8SChris Lattner 
571991e4453SZachary Turner uint32_t Module::ResolveSymbolContextForFilePath(
572991e4453SZachary Turner     const char *file_path, uint32_t line, bool check_inlines,
573991e4453SZachary Turner     lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
5748f3be7a3SJonas Devlieghere   FileSpec file_spec(file_path);
575b9c1b51eSKate Stone   return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
576b9c1b51eSKate Stone                                           resolve_scope, sc_list);
57730fdc8d8SChris Lattner }
57830fdc8d8SChris Lattner 
579991e4453SZachary Turner uint32_t Module::ResolveSymbolContextsForFileSpec(
580991e4453SZachary Turner     const FileSpec &file_spec, uint32_t line, bool check_inlines,
581991e4453SZachary Turner     lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
58216ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
583f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
584f9d16476SPavel Labath   Timer scoped_timer(func_cat,
585b9c1b51eSKate Stone                      "Module::ResolveSymbolContextForFilePath (%s:%u, "
586b9c1b51eSKate Stone                      "check_inlines = %s, resolve_scope = 0x%8.8x)",
587b9c1b51eSKate Stone                      file_spec.GetPath().c_str(), line,
588b9c1b51eSKate Stone                      check_inlines ? "yes" : "no", resolve_scope);
58930fdc8d8SChris Lattner 
59030fdc8d8SChris Lattner   const uint32_t initial_count = sc_list.GetSize();
59130fdc8d8SChris Lattner 
59223f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
593b9c1b51eSKate Stone     symbols->ResolveSymbolContext(file_spec, line, check_inlines, resolve_scope,
594b9c1b51eSKate Stone                                   sc_list);
59530fdc8d8SChris Lattner 
59630fdc8d8SChris Lattner   return sc_list.GetSize() - initial_count;
59730fdc8d8SChris Lattner }
59830fdc8d8SChris Lattner 
5991ad655e2SAdrian Prantl void Module::FindGlobalVariables(ConstString name,
600f9568a95SRaphael Isemann                                  const CompilerDeclContext &parent_decl_ctx,
6011ad655e2SAdrian Prantl                                  size_t max_matches, VariableList &variables) {
60223f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
6031ad655e2SAdrian Prantl     symbols->FindGlobalVariables(name, parent_decl_ctx, max_matches, variables);
60434cda14bSPavel Labath }
60534cda14bSPavel Labath 
6061ad655e2SAdrian Prantl void Module::FindGlobalVariables(const RegularExpression &regex,
6071ad655e2SAdrian Prantl                                  size_t max_matches, VariableList &variables) {
60823f70e83SPavel Labath   SymbolFile *symbols = GetSymbolFile();
60934cda14bSPavel Labath   if (symbols)
6101ad655e2SAdrian Prantl     symbols->FindGlobalVariables(regex, max_matches, variables);
61130fdc8d8SChris Lattner }
61230fdc8d8SChris Lattner 
6131ad655e2SAdrian Prantl void Module::FindCompileUnits(const FileSpec &path,
614b9c1b51eSKate Stone                               SymbolContextList &sc_list) {
615c7bece56SGreg Clayton   const size_t num_compile_units = GetNumCompileUnits();
616644247c1SGreg Clayton   SymbolContext sc;
617e1cd1be6SGreg Clayton   sc.module_sp = shared_from_this();
618b9c1b51eSKate Stone   for (size_t i = 0; i < num_compile_units; ++i) {
619644247c1SGreg Clayton     sc.comp_unit = GetCompileUnitAtIndex(i).get();
620b9c1b51eSKate Stone     if (sc.comp_unit) {
621532290e6SPavel Labath       if (FileSpec::Match(path, sc.comp_unit->GetPrimaryFile()))
622644247c1SGreg Clayton         sc_list.Append(sc);
623644247c1SGreg Clayton     }
6242dafd8edSGreg Clayton   }
625644247c1SGreg Clayton }
626644247c1SGreg Clayton 
6270e4c4821SAdrian Prantl Module::LookupInfo::LookupInfo(ConstString name,
628117b1fa1SZachary Turner                                FunctionNameType name_type_mask,
629117b1fa1SZachary Turner                                LanguageType language)
630117b1fa1SZachary Turner     : m_name(name), m_lookup_name(), m_language(language),
631117b1fa1SZachary Turner       m_name_type_mask(eFunctionNameTypeNone),
632b9c1b51eSKate Stone       m_match_name_after_lookup(false) {
6336234a5c8SGreg Clayton   const char *name_cstr = name.GetCString();
6346234a5c8SGreg Clayton   llvm::StringRef basename;
6356234a5c8SGreg Clayton   llvm::StringRef context;
6366234a5c8SGreg Clayton 
637b9c1b51eSKate Stone   if (name_type_mask & eFunctionNameTypeAuto) {
6386234a5c8SGreg Clayton     if (CPlusPlusLanguage::IsCPPMangledName(name_cstr))
6396234a5c8SGreg Clayton       m_name_type_mask = eFunctionNameTypeFull;
6406234a5c8SGreg Clayton     else if ((language == eLanguageTypeUnknown ||
6416234a5c8SGreg Clayton               Language::LanguageIsObjC(language)) &&
6426234a5c8SGreg Clayton              ObjCLanguage::IsPossibleObjCMethodName(name_cstr))
6436234a5c8SGreg Clayton       m_name_type_mask = eFunctionNameTypeFull;
644b9c1b51eSKate Stone     else if (Language::LanguageIsC(language)) {
6456234a5c8SGreg Clayton       m_name_type_mask = eFunctionNameTypeFull;
646b9c1b51eSKate Stone     } else {
6476234a5c8SGreg Clayton       if ((language == eLanguageTypeUnknown ||
6486234a5c8SGreg Clayton            Language::LanguageIsObjC(language)) &&
6496234a5c8SGreg Clayton           ObjCLanguage::IsPossibleObjCSelector(name_cstr))
6506234a5c8SGreg Clayton         m_name_type_mask |= eFunctionNameTypeSelector;
6516234a5c8SGreg Clayton 
6526234a5c8SGreg Clayton       CPlusPlusLanguage::MethodName cpp_method(name);
6536234a5c8SGreg Clayton       basename = cpp_method.GetBasename();
654b9c1b51eSKate Stone       if (basename.empty()) {
655b9c1b51eSKate Stone         if (CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
656b9c1b51eSKate Stone                                                            basename))
6576234a5c8SGreg Clayton           m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
6586234a5c8SGreg Clayton         else
6596234a5c8SGreg Clayton           m_name_type_mask |= eFunctionNameTypeFull;
660b9c1b51eSKate Stone       } else {
6616234a5c8SGreg Clayton         m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
6626234a5c8SGreg Clayton       }
6636234a5c8SGreg Clayton     }
664b9c1b51eSKate Stone   } else {
6656234a5c8SGreg Clayton     m_name_type_mask = name_type_mask;
666b9c1b51eSKate Stone     if (name_type_mask & eFunctionNameTypeMethod ||
667b9c1b51eSKate Stone         name_type_mask & eFunctionNameTypeBase) {
668b9c1b51eSKate Stone       // If they've asked for a CPP method or function name and it can't be
66905097246SAdrian Prantl       // that, we don't even need to search for CPP methods or names.
6706234a5c8SGreg Clayton       CPlusPlusLanguage::MethodName cpp_method(name);
671b9c1b51eSKate Stone       if (cpp_method.IsValid()) {
6726234a5c8SGreg Clayton         basename = cpp_method.GetBasename();
6736234a5c8SGreg Clayton 
674b9c1b51eSKate Stone         if (!cpp_method.GetQualifiers().empty()) {
675b9c1b51eSKate Stone           // There is a "const" or other qualifier following the end of the
67605097246SAdrian Prantl           // function parens, this can't be a eFunctionNameTypeBase
6776234a5c8SGreg Clayton           m_name_type_mask &= ~(eFunctionNameTypeBase);
6786234a5c8SGreg Clayton           if (m_name_type_mask == eFunctionNameTypeNone)
6796234a5c8SGreg Clayton             return;
6806234a5c8SGreg Clayton         }
681b9c1b51eSKate Stone       } else {
682b9c1b51eSKate Stone         // If the CPP method parser didn't manage to chop this up, try to fill
68305097246SAdrian Prantl         // in the base name if we can. If a::b::c is passed in, we need to just
68405097246SAdrian Prantl         // look up "c", and then we'll filter the result later.
685b9c1b51eSKate Stone         CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
686b9c1b51eSKate Stone                                                        basename);
6876234a5c8SGreg Clayton       }
6886234a5c8SGreg Clayton     }
6896234a5c8SGreg Clayton 
690b9c1b51eSKate Stone     if (name_type_mask & eFunctionNameTypeSelector) {
691b9c1b51eSKate Stone       if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr)) {
6926234a5c8SGreg Clayton         m_name_type_mask &= ~(eFunctionNameTypeSelector);
6936234a5c8SGreg Clayton         if (m_name_type_mask == eFunctionNameTypeNone)
6946234a5c8SGreg Clayton           return;
6956234a5c8SGreg Clayton       }
6966234a5c8SGreg Clayton     }
6976234a5c8SGreg Clayton 
698b9c1b51eSKate Stone     // Still try and get a basename in case someone specifies a name type mask
6995d0c1146SGreg Clayton     // of eFunctionNameTypeFull and a name like "A::func"
700b9c1b51eSKate Stone     if (basename.empty()) {
7015d0c1146SGreg Clayton       if (name_type_mask & eFunctionNameTypeFull &&
7025d0c1146SGreg Clayton           !CPlusPlusLanguage::IsCPPMangledName(name_cstr)) {
7036234a5c8SGreg Clayton         CPlusPlusLanguage::MethodName cpp_method(name);
7046234a5c8SGreg Clayton         basename = cpp_method.GetBasename();
7056234a5c8SGreg Clayton         if (basename.empty())
706b9c1b51eSKate Stone           CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
707b9c1b51eSKate Stone                                                          basename);
7086234a5c8SGreg Clayton       }
7096234a5c8SGreg Clayton     }
7106234a5c8SGreg Clayton   }
7116234a5c8SGreg Clayton 
712b9c1b51eSKate Stone   if (!basename.empty()) {
71305097246SAdrian Prantl     // The name supplied was a partial C++ path like "a::count". In this case
71405097246SAdrian Prantl     // we want to do a lookup on the basename "count" and then make sure any
71505097246SAdrian Prantl     // matching results contain "a::count" so that it would match "b::a::count"
71605097246SAdrian Prantl     // and "a::count". This is why we set "match_name_after_lookup" to true
7176234a5c8SGreg Clayton     m_lookup_name.SetString(basename);
7186234a5c8SGreg Clayton     m_match_name_after_lookup = true;
719b9c1b51eSKate Stone   } else {
720b9c1b51eSKate Stone     // The name is already correct, just use the exact name as supplied, and we
72105097246SAdrian Prantl     // won't need to check if any matches contain "name"
7226234a5c8SGreg Clayton     m_lookup_name = name;
7236234a5c8SGreg Clayton     m_match_name_after_lookup = false;
7246234a5c8SGreg Clayton   }
7256234a5c8SGreg Clayton }
7266234a5c8SGreg Clayton 
727b9c1b51eSKate Stone void Module::LookupInfo::Prune(SymbolContextList &sc_list,
728b9c1b51eSKate Stone                                size_t start_idx) const {
729b9c1b51eSKate Stone   if (m_match_name_after_lookup && m_name) {
7306234a5c8SGreg Clayton     SymbolContext sc;
7316234a5c8SGreg Clayton     size_t i = start_idx;
732b9c1b51eSKate Stone     while (i < sc_list.GetSize()) {
7336234a5c8SGreg Clayton       if (!sc_list.GetContextAtIndex(i, sc))
7346234a5c8SGreg Clayton         break;
7356234a5c8SGreg Clayton       ConstString full_name(sc.GetFunctionName());
736b9c1b51eSKate Stone       if (full_name &&
737b9c1b51eSKate Stone           ::strstr(full_name.GetCString(), m_name.GetCString()) == nullptr) {
7386234a5c8SGreg Clayton         sc_list.RemoveContextAtIndex(i);
739b9c1b51eSKate Stone       } else {
7406234a5c8SGreg Clayton         ++i;
7416234a5c8SGreg Clayton       }
7426234a5c8SGreg Clayton     }
7436234a5c8SGreg Clayton   }
7446234a5c8SGreg Clayton 
745b9c1b51eSKate Stone   // If we have only full name matches we might have tried to set breakpoint on
7465d0c1146SGreg Clayton   // "func" and specified eFunctionNameTypeFull, but we might have found
7475d0c1146SGreg Clayton   // "a::func()", "a::b::func()", "c::func()", "func()" and "func". Only
7485d0c1146SGreg Clayton   // "func()" and "func" should end up matching.
749b9c1b51eSKate Stone   if (m_name_type_mask == eFunctionNameTypeFull) {
7506234a5c8SGreg Clayton     SymbolContext sc;
7516234a5c8SGreg Clayton     size_t i = start_idx;
752b9c1b51eSKate Stone     while (i < sc_list.GetSize()) {
7536234a5c8SGreg Clayton       if (!sc_list.GetContextAtIndex(i, sc))
7546234a5c8SGreg Clayton         break;
75505097246SAdrian Prantl       // Make sure the mangled and demangled names don't match before we try to
75605097246SAdrian Prantl       // pull anything out
7575d0c1146SGreg Clayton       ConstString mangled_name(sc.GetFunctionName(Mangled::ePreferMangled));
7586234a5c8SGreg Clayton       ConstString full_name(sc.GetFunctionName());
7595d0c1146SGreg Clayton       if (mangled_name != m_name && full_name != m_name)
7605d0c1146SGreg Clayton       {
7616234a5c8SGreg Clayton         CPlusPlusLanguage::MethodName cpp_method(full_name);
762b9c1b51eSKate Stone         if (cpp_method.IsValid()) {
763b9c1b51eSKate Stone           if (cpp_method.GetContext().empty()) {
764b9c1b51eSKate Stone             if (cpp_method.GetBasename().compare(m_name.GetStringRef()) != 0) {
7656234a5c8SGreg Clayton               sc_list.RemoveContextAtIndex(i);
7666234a5c8SGreg Clayton               continue;
7676234a5c8SGreg Clayton             }
768b9c1b51eSKate Stone           } else {
7695d0c1146SGreg Clayton             std::string qualified_name;
7705d0c1146SGreg Clayton             llvm::StringRef anon_prefix("(anonymous namespace)");
7715d0c1146SGreg Clayton             if (cpp_method.GetContext() == anon_prefix)
7725d0c1146SGreg Clayton               qualified_name = cpp_method.GetBasename().str();
7735d0c1146SGreg Clayton             else
7745d0c1146SGreg Clayton               qualified_name = cpp_method.GetScopeQualifiedName();
7758d20cfdfSJonas Devlieghere             if (qualified_name != m_name.GetCString()) {
7766234a5c8SGreg Clayton               sc_list.RemoveContextAtIndex(i);
7776234a5c8SGreg Clayton               continue;
7786234a5c8SGreg Clayton             }
7796234a5c8SGreg Clayton           }
7806234a5c8SGreg Clayton         }
7815d0c1146SGreg Clayton       }
7826234a5c8SGreg Clayton       ++i;
7836234a5c8SGreg Clayton     }
7846234a5c8SGreg Clayton   }
7856234a5c8SGreg Clayton }
7866234a5c8SGreg Clayton 
7871ad655e2SAdrian Prantl void Module::FindFunctions(ConstString name,
788f9568a95SRaphael Isemann                            const CompilerDeclContext &parent_decl_ctx,
789117b1fa1SZachary Turner                            FunctionNameType name_type_mask,
790117b1fa1SZachary Turner                            bool include_symbols, bool include_inlines,
7911ad655e2SAdrian Prantl                            SymbolContextList &sc_list) {
79243fe217bSGreg Clayton   const size_t old_size = sc_list.GetSize();
793931180e6SGreg Clayton 
794931180e6SGreg Clayton   // Find all the functions (not symbols, but debug information functions...
79523f70e83SPavel Labath   SymbolFile *symbols = GetSymbolFile();
79643fe217bSGreg Clayton 
797b9c1b51eSKate Stone   if (name_type_mask & eFunctionNameTypeAuto) {
7986234a5c8SGreg Clayton     LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
79943fe217bSGreg Clayton 
800b9c1b51eSKate Stone     if (symbols) {
801b9c1b51eSKate Stone       symbols->FindFunctions(lookup_info.GetLookupName(), parent_decl_ctx,
802b9c1b51eSKate Stone                              lookup_info.GetNameTypeMask(), include_inlines,
8031ad655e2SAdrian Prantl                              sc_list);
80443fe217bSGreg Clayton 
805b9c1b51eSKate Stone       // Now check our symbol table for symbols that are code symbols if
806b9c1b51eSKate Stone       // requested
807b9c1b51eSKate Stone       if (include_symbols) {
808a7499c98SMichael Sartain         Symtab *symtab = symbols->GetSymtab();
80943fe217bSGreg Clayton         if (symtab)
810b9c1b51eSKate Stone           symtab->FindFunctionSymbols(lookup_info.GetLookupName(),
811b9c1b51eSKate Stone                                       lookup_info.GetNameTypeMask(), sc_list);
81243fe217bSGreg Clayton       }
81343fe217bSGreg Clayton     }
81443fe217bSGreg Clayton 
8156234a5c8SGreg Clayton     const size_t new_size = sc_list.GetSize();
8166234a5c8SGreg Clayton 
8176234a5c8SGreg Clayton     if (old_size < new_size)
8186234a5c8SGreg Clayton       lookup_info.Prune(sc_list, old_size);
819b9c1b51eSKate Stone   } else {
820b9c1b51eSKate Stone     if (symbols) {
821b9c1b51eSKate Stone       symbols->FindFunctions(name, parent_decl_ctx, name_type_mask,
8221ad655e2SAdrian Prantl                              include_inlines, sc_list);
823931180e6SGreg Clayton 
824b9c1b51eSKate Stone       // Now check our symbol table for symbols that are code symbols if
825b9c1b51eSKate Stone       // requested
826b9c1b51eSKate Stone       if (include_symbols) {
827a7499c98SMichael Sartain         Symtab *symtab = symbols->GetSymtab();
828931180e6SGreg Clayton         if (symtab)
82943fe217bSGreg Clayton           symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
830931180e6SGreg Clayton       }
831931180e6SGreg Clayton     }
832931180e6SGreg Clayton   }
83330fdc8d8SChris Lattner }
83430fdc8d8SChris Lattner 
8351ad655e2SAdrian Prantl void Module::FindFunctions(const RegularExpression &regex, bool include_symbols,
8361ad655e2SAdrian Prantl                            bool include_inlines,
8371ad655e2SAdrian Prantl                            SymbolContextList &sc_list) {
838c7bece56SGreg Clayton   const size_t start_size = sc_list.GetSize();
839931180e6SGreg Clayton 
84023f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile()) {
8411ad655e2SAdrian Prantl     symbols->FindFunctions(regex, include_inlines, sc_list);
842a7499c98SMichael Sartain 
84305097246SAdrian Prantl     // Now check our symbol table for symbols that are code symbols if
84405097246SAdrian Prantl     // requested
845b9c1b51eSKate Stone     if (include_symbols) {
846a7499c98SMichael Sartain       Symtab *symtab = symbols->GetSymtab();
847b9c1b51eSKate Stone       if (symtab) {
848931180e6SGreg Clayton         std::vector<uint32_t> symbol_indexes;
849b9c1b51eSKate Stone         symtab->AppendSymbolIndexesMatchingRegExAndType(
850b9c1b51eSKate Stone             regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny,
851b9c1b51eSKate Stone             symbol_indexes);
852c7bece56SGreg Clayton         const size_t num_matches = symbol_indexes.size();
853b9c1b51eSKate Stone         if (num_matches) {
854931180e6SGreg Clayton           SymbolContext sc(this);
855d8cf1a11SGreg Clayton           const size_t end_functions_added_index = sc_list.GetSize();
856b9c1b51eSKate Stone           size_t num_functions_added_to_sc_list =
857b9c1b51eSKate Stone               end_functions_added_index - start_size;
858b9c1b51eSKate Stone           if (num_functions_added_to_sc_list == 0) {
85905097246SAdrian Prantl             // No functions were added, just symbols, so we can just append
86005097246SAdrian Prantl             // them
861b9c1b51eSKate Stone             for (size_t i = 0; i < num_matches; ++i) {
862931180e6SGreg Clayton               sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
86300049b8bSMatt Kopec               SymbolType sym_type = sc.symbol->GetType();
86400049b8bSMatt Kopec               if (sc.symbol && (sym_type == eSymbolTypeCode ||
86500049b8bSMatt Kopec                                 sym_type == eSymbolTypeResolver))
866d8cf1a11SGreg Clayton                 sc_list.Append(sc);
867d8cf1a11SGreg Clayton             }
868b9c1b51eSKate Stone           } else {
869d8cf1a11SGreg Clayton             typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
870d8cf1a11SGreg Clayton             FileAddrToIndexMap file_addr_to_index;
871b9c1b51eSKate Stone             for (size_t i = start_size; i < end_functions_added_index; ++i) {
872d8cf1a11SGreg Clayton               const SymbolContext &sc = sc_list[i];
873d8cf1a11SGreg Clayton               if (sc.block)
874d8cf1a11SGreg Clayton                 continue;
875b9c1b51eSKate Stone               file_addr_to_index[sc.function->GetAddressRange()
876b9c1b51eSKate Stone                                      .GetBaseAddress()
877b9c1b51eSKate Stone                                      .GetFileAddress()] = i;
878d8cf1a11SGreg Clayton             }
879d8cf1a11SGreg Clayton 
880d8cf1a11SGreg Clayton             FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
881d8cf1a11SGreg Clayton             // Functions were added so we need to merge symbols into any
882d8cf1a11SGreg Clayton             // existing function symbol contexts
883b9c1b51eSKate Stone             for (size_t i = start_size; i < num_matches; ++i) {
884d8cf1a11SGreg Clayton               sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
885d8cf1a11SGreg Clayton               SymbolType sym_type = sc.symbol->GetType();
886b9c1b51eSKate Stone               if (sc.symbol && sc.symbol->ValueIsAddress() &&
887b9c1b51eSKate Stone                   (sym_type == eSymbolTypeCode ||
888b9c1b51eSKate Stone                    sym_type == eSymbolTypeResolver)) {
889b9c1b51eSKate Stone                 FileAddrToIndexMap::const_iterator pos =
890b9c1b51eSKate Stone                     file_addr_to_index.find(
891b9c1b51eSKate Stone                         sc.symbol->GetAddressRef().GetFileAddress());
892d8cf1a11SGreg Clayton                 if (pos == end)
893d8cf1a11SGreg Clayton                   sc_list.Append(sc);
894d8cf1a11SGreg Clayton                 else
895d8cf1a11SGreg Clayton                   sc_list[pos->second].symbol = sc.symbol;
896d8cf1a11SGreg Clayton               }
897d8cf1a11SGreg Clayton             }
898931180e6SGreg Clayton           }
899931180e6SGreg Clayton         }
900931180e6SGreg Clayton       }
901931180e6SGreg Clayton     }
902931180e6SGreg Clayton   }
90330fdc8d8SChris Lattner }
90430fdc8d8SChris Lattner 
905b9c1b51eSKate Stone void Module::FindAddressesForLine(const lldb::TargetSP target_sp,
906f86248d9SRichard Mitton                                   const FileSpec &file, uint32_t line,
907f86248d9SRichard Mitton                                   Function *function,
908b9c1b51eSKate Stone                                   std::vector<Address> &output_local,
909b9c1b51eSKate Stone                                   std::vector<Address> &output_extern) {
910f86248d9SRichard Mitton   SearchFilterByModule filter(target_sp, m_file);
911f86248d9SRichard Mitton   AddressResolverFileLine resolver(file, line, true);
912f86248d9SRichard Mitton   resolver.ResolveAddress(filter);
913f86248d9SRichard Mitton 
914b9c1b51eSKate Stone   for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++) {
915f86248d9SRichard Mitton     Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
916f86248d9SRichard Mitton     Function *f = addr.CalculateSymbolContextFunction();
917f86248d9SRichard Mitton     if (f && f == function)
918f86248d9SRichard Mitton       output_local.push_back(addr);
919f86248d9SRichard Mitton     else
920f86248d9SRichard Mitton       output_extern.push_back(addr);
921f86248d9SRichard Mitton   }
922f86248d9SRichard Mitton }
923f86248d9SRichard Mitton 
924bf9d84c0SAdrian Prantl void Module::FindTypes_Impl(
925f9568a95SRaphael Isemann     ConstString name, const CompilerDeclContext &parent_decl_ctx,
926d4d428efSAdrian Prantl     size_t max_matches,
927ae088e52SGreg Clayton     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
928b9c1b51eSKate Stone     TypeMap &types) {
929f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
930f9d16476SPavel Labath   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
93123f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
932bf9d84c0SAdrian Prantl     symbols->FindTypes(name, parent_decl_ctx, max_matches,
933b9c1b51eSKate Stone                        searched_symbol_files, types);
9343504eee8SGreg Clayton }
9353504eee8SGreg Clayton 
936bf9d84c0SAdrian Prantl void Module::FindTypesInNamespace(ConstString type_name,
937f9568a95SRaphael Isemann                                   const CompilerDeclContext &parent_decl_ctx,
938b9c1b51eSKate Stone                                   size_t max_matches, TypeList &type_list) {
9394069730cSRavitheja Addepally   TypeMap types_map;
940ae088e52SGreg Clayton   llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
941bf9d84c0SAdrian Prantl   FindTypes_Impl(type_name, parent_decl_ctx, max_matches, searched_symbol_files,
942bf9d84c0SAdrian Prantl                  types_map);
943bf9d84c0SAdrian Prantl   if (types_map.GetSize()) {
944576495e6SZachary Turner     SymbolContext sc;
945576495e6SZachary Turner     sc.module_sp = shared_from_this();
9464069730cSRavitheja Addepally     sc.SortTypeList(types_map, type_list);
947576495e6SZachary Turner   }
9486f3533fbSEnrico Granata }
9496f3533fbSEnrico Granata 
950b9c1b51eSKate Stone lldb::TypeSP Module::FindFirstType(const SymbolContext &sc,
9510e4c4821SAdrian Prantl                                    ConstString name, bool exact_match) {
952b43165b7SGreg Clayton   TypeList type_list;
953ae088e52SGreg Clayton   llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
954576495e6SZachary Turner   FindTypes(name, exact_match, 1, searched_symbol_files, type_list);
955bf9d84c0SAdrian Prantl   if (type_list.GetSize())
956b43165b7SGreg Clayton     return type_list.GetTypeAtIndex(0);
957b43165b7SGreg Clayton   return TypeSP();
958b43165b7SGreg Clayton }
959b43165b7SGreg Clayton 
960bf9d84c0SAdrian Prantl void Module::FindTypes(
9610e4c4821SAdrian Prantl     ConstString name, bool exact_match, size_t max_matches,
962ae088e52SGreg Clayton     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
963b9c1b51eSKate Stone     TypeList &types) {
96484db9105SGreg Clayton   const char *type_name_cstr = name.GetCString();
965556b1611STamas Berghammer   llvm::StringRef type_scope;
966556b1611STamas Berghammer   llvm::StringRef type_basename;
9677bc31332SGreg Clayton   TypeClass type_class = eTypeClassAny;
9684069730cSRavitheja Addepally   TypeMap typesmap;
9691739b7d0SFrederic Riss 
970b9c1b51eSKate Stone   if (Type::GetTypeScopeAndBasename(type_name_cstr, type_scope, type_basename,
971b9c1b51eSKate Stone                                     type_class)) {
97284db9105SGreg Clayton     // Check if "name" starts with "::" which means the qualified type starts
97384db9105SGreg Clayton     // from the root namespace and implies and exact match. The typenames we
97484db9105SGreg Clayton     // get back from clang do not start with "::" so we need to strip this off
975d93c4a33SBruce Mitchener     // in order to get the qualified names to match
976556b1611STamas Berghammer     exact_match = type_scope.consume_front("::");
9776f3533fbSEnrico Granata 
978556b1611STamas Berghammer     ConstString type_basename_const_str(type_basename);
979f9568a95SRaphael Isemann     FindTypes_Impl(type_basename_const_str, CompilerDeclContext(), max_matches,
980bf9d84c0SAdrian Prantl                    searched_symbol_files, typesmap);
981bf9d84c0SAdrian Prantl     if (typesmap.GetSize())
982adcd0268SBenjamin Kramer       typesmap.RemoveMismatchedTypes(std::string(type_scope),
983adcd0268SBenjamin Kramer                                      std::string(type_basename), type_class,
984b9c1b51eSKate Stone                                      exact_match);
985b9c1b51eSKate Stone   } else {
986b9c1b51eSKate Stone     // The type is not in a namespace/class scope, just search for it by
987b9c1b51eSKate Stone     // basename
988c485f056SGreg Clayton     if (type_class != eTypeClassAny && !type_basename.empty()) {
989b9c1b51eSKate Stone       // The "type_name_cstr" will have been modified if we have a valid type
99005097246SAdrian Prantl       // class prefix (like "struct", "class", "union", "typedef" etc).
991f9568a95SRaphael Isemann       FindTypes_Impl(ConstString(type_basename), CompilerDeclContext(),
992f9568a95SRaphael Isemann                      UINT_MAX, searched_symbol_files, typesmap);
993adcd0268SBenjamin Kramer       typesmap.RemoveMismatchedTypes(std::string(type_scope),
994adcd0268SBenjamin Kramer                                      std::string(type_basename), type_class,
995c485f056SGreg Clayton                                      exact_match);
996b9c1b51eSKate Stone     } else {
997f9568a95SRaphael Isemann       FindTypes_Impl(name, CompilerDeclContext(), UINT_MAX,
998f9568a95SRaphael Isemann                      searched_symbol_files, typesmap);
999c485f056SGreg Clayton       if (exact_match) {
1000c485f056SGreg Clayton         std::string name_str(name.AsCString(""));
1001adcd0268SBenjamin Kramer         typesmap.RemoveMismatchedTypes(std::string(type_scope), name_str,
1002adcd0268SBenjamin Kramer                                        type_class, exact_match);
1003c485f056SGreg Clayton       }
100484db9105SGreg Clayton     }
10057bc31332SGreg Clayton   }
1006bf9d84c0SAdrian Prantl   if (typesmap.GetSize()) {
1007576495e6SZachary Turner     SymbolContext sc;
1008576495e6SZachary Turner     sc.module_sp = shared_from_this();
10094069730cSRavitheja Addepally     sc.SortTypeList(typesmap, types);
1010576495e6SZachary Turner   }
10116f3533fbSEnrico Granata }
10126f3533fbSEnrico Granata 
10133b73dcdcSAdrian Prantl void Module::FindTypes(
10143b73dcdcSAdrian Prantl     llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages,
10153b73dcdcSAdrian Prantl     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
10163b73dcdcSAdrian Prantl     TypeMap &types) {
1017aa97a89dSAdrian Prantl   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1018aa97a89dSAdrian Prantl   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
1019aa97a89dSAdrian Prantl   if (SymbolFile *symbols = GetSymbolFile())
10203b73dcdcSAdrian Prantl     symbols->FindTypes(pattern, languages, searched_symbol_files, types);
1021aa97a89dSAdrian Prantl }
1022aa97a89dSAdrian Prantl 
1023579d6d1aSPavel Labath SymbolFile *Module::GetSymbolFile(bool can_create, Stream *feedback_strm) {
1024579d6d1aSPavel Labath   if (!m_did_load_symfile.load()) {
102516ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1026579d6d1aSPavel Labath     if (!m_did_load_symfile.load() && can_create) {
102730fdc8d8SChris Lattner       ObjectFile *obj_file = GetObjectFile();
1028b9c1b51eSKate Stone       if (obj_file != nullptr) {
1029f9d16476SPavel Labath         static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1030f9d16476SPavel Labath         Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
1031d5b44036SJonas Devlieghere         m_symfile_up.reset(
1032b9c1b51eSKate Stone             SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
1033579d6d1aSPavel Labath         m_did_load_symfile = true;
103430fdc8d8SChris Lattner       }
103530fdc8d8SChris Lattner     }
103688c05f54SGreg Clayton   }
1037579d6d1aSPavel Labath   return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr;
103823f70e83SPavel Labath }
103923f70e83SPavel Labath 
1040d5d47a35SPavel Labath Symtab *Module::GetSymtab() {
1041d5d47a35SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
1042d5d47a35SPavel Labath     return symbols->GetSymtab();
1043d5d47a35SPavel Labath   return nullptr;
1044d5d47a35SPavel Labath }
1045d5d47a35SPavel Labath 
1046b9c1b51eSKate Stone void Module::SetFileSpecAndObjectName(const FileSpec &file,
10470e4c4821SAdrian Prantl                                       ConstString object_name) {
104805097246SAdrian Prantl   // Container objects whose paths do not specify a file directly can call this
104905097246SAdrian Prantl   // function to correct the file and object names.
105030fdc8d8SChris Lattner   m_file = file;
105146376966SJonas Devlieghere   m_mod_time = FileSystem::Instance().GetModificationTime(file);
105230fdc8d8SChris Lattner   m_object_name = object_name;
105330fdc8d8SChris Lattner }
105430fdc8d8SChris Lattner 
1055b9c1b51eSKate Stone const ArchSpec &Module::GetArchitecture() const { return m_arch; }
105630fdc8d8SChris Lattner 
1057b9c1b51eSKate Stone std::string Module::GetSpecificationDescription() const {
1058b5ad4ec7SGreg Clayton   std::string spec(GetFileSpec().GetPath());
1059b9c1b51eSKate Stone   if (m_object_name) {
1060b5ad4ec7SGreg Clayton     spec += '(';
1061b5ad4ec7SGreg Clayton     spec += m_object_name.GetCString();
1062b5ad4ec7SGreg Clayton     spec += ')';
1063b5ad4ec7SGreg Clayton   }
1064b5ad4ec7SGreg Clayton   return spec;
1065b5ad4ec7SGreg Clayton }
1066b5ad4ec7SGreg Clayton 
1067c4c464f8SRaphael Isemann void Module::GetDescription(llvm::raw_ostream &s,
1068c4c464f8SRaphael Isemann                             lldb::DescriptionLevel level) {
106916ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1070ceb6b139SCaroline Tice 
1071b9c1b51eSKate Stone   if (level >= eDescriptionLevelFull) {
1072cfd1acedSGreg Clayton     if (m_arch.IsValid())
1073c4c464f8SRaphael Isemann       s << llvm::formatv("({0}) ", m_arch.GetArchitectureName());
1074c982b3d6SGreg Clayton   }
1075ceb6b139SCaroline Tice 
1076b9c1b51eSKate Stone   if (level == eDescriptionLevelBrief) {
1077c982b3d6SGreg Clayton     const char *filename = m_file.GetFilename().GetCString();
1078c982b3d6SGreg Clayton     if (filename)
1079c4c464f8SRaphael Isemann       s << filename;
1080b9c1b51eSKate Stone   } else {
1081cfd1acedSGreg Clayton     char path[PATH_MAX];
1082cfd1acedSGreg Clayton     if (m_file.GetPath(path, sizeof(path)))
1083c4c464f8SRaphael Isemann       s << path;
1084c982b3d6SGreg Clayton   }
1085cfd1acedSGreg Clayton 
1086cfd1acedSGreg Clayton   const char *object_name = m_object_name.GetCString();
1087cfd1acedSGreg Clayton   if (object_name)
1088c4c464f8SRaphael Isemann     s << llvm::formatv("({0})", object_name);
1089ceb6b139SCaroline Tice }
1090ceb6b139SCaroline Tice 
1091b9c1b51eSKate Stone void Module::ReportError(const char *format, ...) {
1092b9c1b51eSKate Stone   if (format && format[0]) {
1093e38a5eddSGreg Clayton     StreamString strm;
1094e38a5eddSGreg Clayton     strm.PutCString("error: ");
1095c4c464f8SRaphael Isemann     GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelBrief);
10968b35334eSGreg Clayton     strm.PutChar(' ');
1097c982b3d6SGreg Clayton     va_list args;
1098c982b3d6SGreg Clayton     va_start(args, format);
1099e38a5eddSGreg Clayton     strm.PrintfVarArg(format, args);
1100c982b3d6SGreg Clayton     va_end(args);
1101e38a5eddSGreg Clayton 
1102e38a5eddSGreg Clayton     const int format_len = strlen(format);
1103b9c1b51eSKate Stone     if (format_len > 0) {
1104e38a5eddSGreg Clayton       const char last_char = format[format_len - 1];
1105376230c9SRaphael Isemann       if (last_char != '\n' && last_char != '\r')
1106e38a5eddSGreg Clayton         strm.EOL();
1107e38a5eddSGreg Clayton     }
1108c156427dSZachary Turner     Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData());
1109e38a5eddSGreg Clayton   }
1110e38a5eddSGreg Clayton }
1111e38a5eddSGreg Clayton 
1112b9c1b51eSKate Stone bool Module::FileHasChanged() const {
1113c5dac77aSEugene Zelenko   if (!m_file_has_changed)
11141408bf72SPavel Labath     m_file_has_changed =
111546376966SJonas Devlieghere         (FileSystem::Instance().GetModificationTime(m_file) != m_mod_time);
11161d60909eSGreg Clayton   return m_file_has_changed;
11171d60909eSGreg Clayton }
11181d60909eSGreg Clayton 
1119b9c1b51eSKate Stone void Module::ReportErrorIfModifyDetected(const char *format, ...) {
1120b9c1b51eSKate Stone   if (!m_first_file_changed_log) {
1121b9c1b51eSKate Stone     if (FileHasChanged()) {
11221d60909eSGreg Clayton       m_first_file_changed_log = true;
1123b9c1b51eSKate Stone       if (format) {
1124e38a5eddSGreg Clayton         StreamString strm;
1125e38a5eddSGreg Clayton         strm.PutCString("error: the object file ");
1126c4c464f8SRaphael Isemann         GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull);
1127e38a5eddSGreg Clayton         strm.PutCString(" has been modified\n");
1128e38a5eddSGreg Clayton 
1129e38a5eddSGreg Clayton         va_list args;
1130e38a5eddSGreg Clayton         va_start(args, format);
1131e38a5eddSGreg Clayton         strm.PrintfVarArg(format, args);
1132e38a5eddSGreg Clayton         va_end(args);
1133e38a5eddSGreg Clayton 
1134e38a5eddSGreg Clayton         const int format_len = strlen(format);
1135b9c1b51eSKate Stone         if (format_len > 0) {
1136e38a5eddSGreg Clayton           const char last_char = format[format_len - 1];
1137376230c9SRaphael Isemann           if (last_char != '\n' && last_char != '\r')
1138e38a5eddSGreg Clayton             strm.EOL();
1139e38a5eddSGreg Clayton         }
1140b9c1b51eSKate Stone         strm.PutCString("The debug session should be aborted as the original "
1141b9c1b51eSKate Stone                         "debug information has been overwritten.\n");
1142c156427dSZachary Turner         Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData());
1143e38a5eddSGreg Clayton       }
1144e38a5eddSGreg Clayton     }
1145c982b3d6SGreg Clayton   }
11461d60909eSGreg Clayton }
1147c982b3d6SGreg Clayton 
1148b9c1b51eSKate Stone void Module::ReportWarning(const char *format, ...) {
1149b9c1b51eSKate Stone   if (format && format[0]) {
1150e38a5eddSGreg Clayton     StreamString strm;
1151e38a5eddSGreg Clayton     strm.PutCString("warning: ");
1152c4c464f8SRaphael Isemann     GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull);
11538b35334eSGreg Clayton     strm.PutChar(' ');
1154c982b3d6SGreg Clayton 
1155c982b3d6SGreg Clayton     va_list args;
1156c982b3d6SGreg Clayton     va_start(args, format);
1157e38a5eddSGreg Clayton     strm.PrintfVarArg(format, args);
1158c982b3d6SGreg Clayton     va_end(args);
1159e38a5eddSGreg Clayton 
1160e38a5eddSGreg Clayton     const int format_len = strlen(format);
1161b9c1b51eSKate Stone     if (format_len > 0) {
1162e38a5eddSGreg Clayton       const char last_char = format[format_len - 1];
1163376230c9SRaphael Isemann       if (last_char != '\n' && last_char != '\r')
1164e38a5eddSGreg Clayton         strm.EOL();
1165e38a5eddSGreg Clayton     }
1166c156427dSZachary Turner     Host::SystemLog(Host::eSystemLogWarning, "%s", strm.GetData());
1167e38a5eddSGreg Clayton   }
1168c982b3d6SGreg Clayton }
1169c982b3d6SGreg Clayton 
1170b9c1b51eSKate Stone void Module::LogMessage(Log *log, const char *format, ...) {
1171b9c1b51eSKate Stone   if (log != nullptr) {
1172c982b3d6SGreg Clayton     StreamString log_message;
1173c4c464f8SRaphael Isemann     GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull);
1174c982b3d6SGreg Clayton     log_message.PutCString(": ");
1175c982b3d6SGreg Clayton     va_list args;
1176c982b3d6SGreg Clayton     va_start(args, format);
1177c982b3d6SGreg Clayton     log_message.PrintfVarArg(format, args);
1178c982b3d6SGreg Clayton     va_end(args);
1179c156427dSZachary Turner     log->PutCString(log_message.GetData());
1180c982b3d6SGreg Clayton   }
1181c982b3d6SGreg Clayton }
1182c982b3d6SGreg Clayton 
1183b9c1b51eSKate Stone void Module::LogMessageVerboseBacktrace(Log *log, const char *format, ...) {
1184b9c1b51eSKate Stone   if (log != nullptr) {
1185d61c0fc0SGreg Clayton     StreamString log_message;
1186c4c464f8SRaphael Isemann     GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull);
1187d61c0fc0SGreg Clayton     log_message.PutCString(": ");
1188d61c0fc0SGreg Clayton     va_list args;
1189d61c0fc0SGreg Clayton     va_start(args, format);
1190d61c0fc0SGreg Clayton     log_message.PrintfVarArg(format, args);
1191d61c0fc0SGreg Clayton     va_end(args);
1192b9c1b51eSKate Stone     if (log->GetVerbose()) {
1193a893d301SZachary Turner       std::string back_trace;
1194a893d301SZachary Turner       llvm::raw_string_ostream stream(back_trace);
1195a893d301SZachary Turner       llvm::sys::PrintStackTrace(stream);
1196771ef6d4SMalcolm Parsons       log_message.PutCString(back_trace);
1197a893d301SZachary Turner     }
1198c156427dSZachary Turner     log->PutCString(log_message.GetData());
1199d61c0fc0SGreg Clayton   }
1200d61c0fc0SGreg Clayton }
1201d61c0fc0SGreg Clayton 
1202b9c1b51eSKate Stone void Module::Dump(Stream *s) {
120316ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
12048941142aSGreg Clayton   // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
120530fdc8d8SChris Lattner   s->Indent();
1206b9c1b51eSKate Stone   s->Printf("Module %s%s%s%s\n", m_file.GetPath().c_str(),
120730fdc8d8SChris Lattner             m_object_name ? "(" : "",
120830fdc8d8SChris Lattner             m_object_name ? m_object_name.GetCString() : "",
120930fdc8d8SChris Lattner             m_object_name ? ")" : "");
121030fdc8d8SChris Lattner 
121130fdc8d8SChris Lattner   s->IndentMore();
121230fdc8d8SChris Lattner 
1213a7499c98SMichael Sartain   ObjectFile *objfile = GetObjectFile();
121430fdc8d8SChris Lattner   if (objfile)
121530fdc8d8SChris Lattner     objfile->Dump(s);
121630fdc8d8SChris Lattner 
1217d5d47a35SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
1218d5d47a35SPavel Labath     symbols->Dump(*s);
121930fdc8d8SChris Lattner 
122030fdc8d8SChris Lattner   s->IndentLess();
122130fdc8d8SChris Lattner }
122230fdc8d8SChris Lattner 
12230e4c4821SAdrian Prantl ConstString Module::GetObjectName() const { return m_object_name; }
122430fdc8d8SChris Lattner 
1225b9c1b51eSKate Stone ObjectFile *Module::GetObjectFile() {
1226b9c1b51eSKate Stone   if (!m_did_load_objfile.load()) {
122716ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1228b9c1b51eSKate Stone     if (!m_did_load_objfile.load()) {
1229f9d16476SPavel Labath       static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1230f9d16476SPavel Labath       Timer scoped_timer(func_cat, "Module::GetObjectFile () module = %s",
1231b9c1b51eSKate Stone                          GetFileSpec().GetFilename().AsCString(""));
12325ce9c565SGreg Clayton       DataBufferSP data_sp;
12335ce9c565SGreg Clayton       lldb::offset_t data_offset = 0;
123459b78bcbSJonas Devlieghere       const lldb::offset_t file_size =
123559b78bcbSJonas Devlieghere           FileSystem::Instance().GetByteSize(m_file);
1236b9c1b51eSKate Stone       if (file_size > m_object_offset) {
12372540a8a7SGreg Clayton         m_did_load_objfile = true;
1238b9c1b51eSKate Stone         m_objfile_sp = ObjectFile::FindPlugin(
1239b9c1b51eSKate Stone             shared_from_this(), &m_file, m_object_offset,
1240b9c1b51eSKate Stone             file_size - m_object_offset, data_sp, data_offset);
1241b9c1b51eSKate Stone         if (m_objfile_sp) {
1242b9c1b51eSKate Stone           // Once we get the object file, update our module with the object
124305097246SAdrian Prantl           // file's architecture since it might differ in vendor/os if some
124405097246SAdrian Prantl           // parts were unknown.  But since the matching arch might already be
124505097246SAdrian Prantl           // more specific than the generic COFF architecture, only merge in
124605097246SAdrian Prantl           // those values that overwrite unspecified unknown values.
1247f760f5aeSPavel Labath           m_arch.MergeFrom(m_objfile_sp->GetArchitecture());
1248b9c1b51eSKate Stone         } else {
1249b9c1b51eSKate Stone           ReportError("failed to load objfile for %s",
1250b9c1b51eSKate Stone                       GetFileSpec().GetPath().c_str());
12510ee56ce6STodd Fiala         }
125230fdc8d8SChris Lattner       }
12532540a8a7SGreg Clayton     }
125488c05f54SGreg Clayton   }
1255762f7135SGreg Clayton   return m_objfile_sp.get();
125630fdc8d8SChris Lattner }
125730fdc8d8SChris Lattner 
1258b9c1b51eSKate Stone SectionList *Module::GetSectionList() {
1259d5b44036SJonas Devlieghere   // Populate m_sections_up with sections from objfile.
1260d5b44036SJonas Devlieghere   if (!m_sections_up) {
12613046e668SGreg Clayton     ObjectFile *obj_file = GetObjectFile();
1262c5dac77aSEugene Zelenko     if (obj_file != nullptr)
12633046e668SGreg Clayton       obj_file->CreateSections(*GetUnifiedSectionList());
12643046e668SGreg Clayton   }
1265d5b44036SJonas Devlieghere   return m_sections_up.get();
12663046e668SGreg Clayton }
12673046e668SGreg Clayton 
1268b9c1b51eSKate Stone void Module::SectionFileAddressesChanged() {
126905a09c67SJason Molenda   ObjectFile *obj_file = GetObjectFile();
127005a09c67SJason Molenda   if (obj_file)
127105a09c67SJason Molenda     obj_file->SectionFileAddressesChanged();
127223f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
127323f70e83SPavel Labath     symbols->SectionFileAddressesChanged();
127405a09c67SJason Molenda }
127505a09c67SJason Molenda 
1276dec96392SPavel Labath UnwindTable &Module::GetUnwindTable() {
1277dec96392SPavel Labath   if (!m_unwind_table)
1278dec96392SPavel Labath     m_unwind_table.emplace(*this);
1279dec96392SPavel Labath   return *m_unwind_table;
1280dec96392SPavel Labath }
1281dec96392SPavel Labath 
1282b9c1b51eSKate Stone SectionList *Module::GetUnifiedSectionList() {
1283d5b44036SJonas Devlieghere   if (!m_sections_up)
1284a8f3ae7cSJonas Devlieghere     m_sections_up = std::make_unique<SectionList>();
1285d5b44036SJonas Devlieghere   return m_sections_up.get();
1286a7499c98SMichael Sartain }
128730fdc8d8SChris Lattner 
12880e4c4821SAdrian Prantl const Symbol *Module::FindFirstSymbolWithNameAndType(ConstString name,
1289b9c1b51eSKate Stone                                                      SymbolType symbol_type) {
1290f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1291b9c1b51eSKate Stone   Timer scoped_timer(
1292f9d16476SPavel Labath       func_cat, "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1293b9c1b51eSKate Stone       name.AsCString(), symbol_type);
1294d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab())
1295b9c1b51eSKate Stone     return symtab->FindFirstSymbolWithNameAndType(
1296b9c1b51eSKate Stone         name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
1297c5dac77aSEugene Zelenko   return nullptr;
129830fdc8d8SChris Lattner }
1299b9c1b51eSKate Stone void Module::SymbolIndicesToSymbolContextList(
1300b9c1b51eSKate Stone     Symtab *symtab, std::vector<uint32_t> &symbol_indexes,
1301b9c1b51eSKate Stone     SymbolContextList &sc_list) {
130230fdc8d8SChris Lattner   // No need to protect this call using m_mutex all other method calls are
130330fdc8d8SChris Lattner   // already thread safe.
130430fdc8d8SChris Lattner 
130530fdc8d8SChris Lattner   size_t num_indices = symbol_indexes.size();
1306b9c1b51eSKate Stone   if (num_indices > 0) {
130730fdc8d8SChris Lattner     SymbolContext sc;
130830fdc8d8SChris Lattner     CalculateSymbolContext(&sc);
1309b9c1b51eSKate Stone     for (size_t i = 0; i < num_indices; i++) {
131030fdc8d8SChris Lattner       sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
131130fdc8d8SChris Lattner       if (sc.symbol)
131230fdc8d8SChris Lattner         sc_list.Append(sc);
131330fdc8d8SChris Lattner     }
131430fdc8d8SChris Lattner   }
131530fdc8d8SChris Lattner }
131630fdc8d8SChris Lattner 
13171ad655e2SAdrian Prantl void Module::FindFunctionSymbols(ConstString name,
1318c1b2ccfdSGreg Clayton                                    uint32_t name_type_mask,
1319b9c1b51eSKate Stone                                    SymbolContextList &sc_list) {
1320f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1321f9d16476SPavel Labath   Timer scoped_timer(func_cat,
1322c1b2ccfdSGreg Clayton                      "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1323b9c1b51eSKate Stone                      name.AsCString(), name_type_mask);
1324d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab())
13251ad655e2SAdrian Prantl     symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
1326c1b2ccfdSGreg Clayton }
1327c1b2ccfdSGreg Clayton 
13281ad655e2SAdrian Prantl void Module::FindSymbolsWithNameAndType(ConstString name,
1329b9c1b51eSKate Stone                                           SymbolType symbol_type,
1330b9c1b51eSKate Stone                                           SymbolContextList &sc_list) {
133130fdc8d8SChris Lattner   // No need to protect this call using m_mutex all other method calls are
133230fdc8d8SChris Lattner   // already thread safe.
133330fdc8d8SChris Lattner 
1334f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1335b9c1b51eSKate Stone   Timer scoped_timer(
1336f9d16476SPavel Labath       func_cat, "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1337b9c1b51eSKate Stone       name.AsCString(), symbol_type);
1338d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab()) {
133930fdc8d8SChris Lattner     std::vector<uint32_t> symbol_indexes;
134030fdc8d8SChris Lattner     symtab->FindAllSymbolsWithNameAndType(name, symbol_type, symbol_indexes);
134130fdc8d8SChris Lattner     SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
134230fdc8d8SChris Lattner   }
134330fdc8d8SChris Lattner }
134430fdc8d8SChris Lattner 
13451ad655e2SAdrian Prantl void Module::FindSymbolsMatchingRegExAndType(const RegularExpression &regex,
1346b9c1b51eSKate Stone                                              SymbolType symbol_type,
1347b9c1b51eSKate Stone                                              SymbolContextList &sc_list) {
134830fdc8d8SChris Lattner   // No need to protect this call using m_mutex all other method calls are
134930fdc8d8SChris Lattner   // already thread safe.
135030fdc8d8SChris Lattner 
1351f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1352b9c1b51eSKate Stone   Timer scoped_timer(
1353f9d16476SPavel Labath       func_cat,
135430fdc8d8SChris Lattner       "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
135595eae423SZachary Turner       regex.GetText().str().c_str(), symbol_type);
1356d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab()) {
135730fdc8d8SChris Lattner     std::vector<uint32_t> symbol_indexes;
1358b9c1b51eSKate Stone     symtab->FindAllSymbolsMatchingRexExAndType(
1359b9c1b51eSKate Stone         regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny,
1360b9c1b51eSKate Stone         symbol_indexes);
136130fdc8d8SChris Lattner     SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
136230fdc8d8SChris Lattner   }
136330fdc8d8SChris Lattner }
136430fdc8d8SChris Lattner 
13657fca8c07SJim Ingham void Module::PreloadSymbols() {
13667fca8c07SJim Ingham   std::lock_guard<std::recursive_mutex> guard(m_mutex);
136723f70e83SPavel Labath   SymbolFile *sym_file = GetSymbolFile();
136823f70e83SPavel Labath   if (!sym_file)
13697fca8c07SJim Ingham     return;
137023f70e83SPavel Labath 
13717fca8c07SJim Ingham   // Prime the symbol file first, since it adds symbols to the symbol table.
137223f70e83SPavel Labath   sym_file->PreloadSymbols();
137323f70e83SPavel Labath 
13747fca8c07SJim Ingham   // Now we can prime the symbol table.
137523f70e83SPavel Labath   if (Symtab *symtab = sym_file->GetSymtab())
13767fca8c07SJim Ingham     symtab->PreloadSymbols();
13777fca8c07SJim Ingham }
13787fca8c07SJim Ingham 
1379b9c1b51eSKate Stone void Module::SetSymbolFileFileSpec(const FileSpec &file) {
1380dbd7fabaSJonas Devlieghere   if (!FileSystem::Instance().Exists(file))
138190271672SGreg Clayton     return;
1382d5b44036SJonas Devlieghere   if (m_symfile_up) {
1383b9c1b51eSKate Stone     // Remove any sections in the unified section list that come from the
1384b9c1b51eSKate Stone     // current symbol vendor.
13853046e668SGreg Clayton     SectionList *section_list = GetSectionList();
138623f70e83SPavel Labath     SymbolFile *symbol_file = GetSymbolFile();
1387b9c1b51eSKate Stone     if (section_list && symbol_file) {
1388a7499c98SMichael Sartain       ObjectFile *obj_file = symbol_file->GetObjectFile();
1389b9c1b51eSKate Stone       // Make sure we have an object file and that the symbol vendor's objfile
139005097246SAdrian Prantl       // isn't the same as the module's objfile before we remove any sections
139105097246SAdrian Prantl       // for it...
1392b9c1b51eSKate Stone       if (obj_file) {
1393b9c1b51eSKate Stone         // Check to make sure we aren't trying to specify the file we already
1394b9c1b51eSKate Stone         // have
1395b9c1b51eSKate Stone         if (obj_file->GetFileSpec() == file) {
139690271672SGreg Clayton           // We are being told to add the exact same file that we already have
139790271672SGreg Clayton           // we don't have to do anything.
139890271672SGreg Clayton           return;
139990271672SGreg Clayton         }
1400d00438e8STamas Berghammer 
1401b9c1b51eSKate Stone         // Cleare the current symtab as we are going to replace it with a new
1402b9c1b51eSKate Stone         // one
1403d00438e8STamas Berghammer         obj_file->ClearSymtab();
140490271672SGreg Clayton 
1405dec96392SPavel Labath         // Clear the unwind table too, as that may also be affected by the
1406dec96392SPavel Labath         // symbol file information.
1407dec96392SPavel Labath         m_unwind_table.reset();
1408dec96392SPavel Labath 
1409b9c1b51eSKate Stone         // The symbol file might be a directory bundle ("/tmp/a.out.dSYM")
141005097246SAdrian Prantl         // instead of a full path to the symbol file within the bundle
1411b9c1b51eSKate Stone         // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to
1412b9c1b51eSKate Stone         // check this
141390271672SGreg Clayton 
14143a58d898SJonas Devlieghere         if (FileSystem::Instance().IsDirectory(file)) {
141590271672SGreg Clayton           std::string new_path(file.GetPath());
141690271672SGreg Clayton           std::string old_path(obj_file->GetFileSpec().GetPath());
1417*e8f13f4fSBenjamin Kramer           if (llvm::StringRef(old_path).startswith(new_path)) {
1418b9c1b51eSKate Stone             // We specified the same bundle as the symbol file that we already
1419b9c1b51eSKate Stone             // have
142090271672SGreg Clayton             return;
142190271672SGreg Clayton           }
142290271672SGreg Clayton         }
142390271672SGreg Clayton 
1424b9c1b51eSKate Stone         if (obj_file != m_objfile_sp.get()) {
1425a7499c98SMichael Sartain           size_t num_sections = section_list->GetNumSections(0);
1426b9c1b51eSKate Stone           for (size_t idx = num_sections; idx > 0; --idx) {
1427b9c1b51eSKate Stone             lldb::SectionSP section_sp(
1428b9c1b51eSKate Stone                 section_list->GetSectionAtIndex(idx - 1));
1429b9c1b51eSKate Stone             if (section_sp->GetObjectFile() == obj_file) {
14303046e668SGreg Clayton               section_list->DeleteSection(idx - 1);
1431a7499c98SMichael Sartain             }
1432a7499c98SMichael Sartain           }
1433a7499c98SMichael Sartain         }
1434a7499c98SMichael Sartain       }
1435a7499c98SMichael Sartain     }
1436b9c1b51eSKate Stone     // Keep all old symbol files around in case there are any lingering type
143705097246SAdrian Prantl     // references in any SBValue objects that might have been handed out.
1438d5b44036SJonas Devlieghere     m_old_symfiles.push_back(std::move(m_symfile_up));
143990271672SGreg Clayton   }
1440e01e07b6SGreg Clayton   m_symfile_spec = file;
1441d5b44036SJonas Devlieghere   m_symfile_up.reset();
1442579d6d1aSPavel Labath   m_did_load_symfile = false;
1443e01e07b6SGreg Clayton }
1444e01e07b6SGreg Clayton 
1445b9c1b51eSKate Stone bool Module::IsExecutable() {
1446c5dac77aSEugene Zelenko   if (GetObjectFile() == nullptr)
14475aee162fSJim Ingham     return false;
14485aee162fSJim Ingham   else
14495aee162fSJim Ingham     return GetObjectFile()->IsExecutable();
14505aee162fSJim Ingham }
14515aee162fSJim Ingham 
1452b9c1b51eSKate Stone bool Module::IsLoadedInTarget(Target *target) {
1453b53cb271SJim Ingham   ObjectFile *obj_file = GetObjectFile();
1454b9c1b51eSKate Stone   if (obj_file) {
14553046e668SGreg Clayton     SectionList *sections = GetSectionList();
1456b9c1b51eSKate Stone     if (sections != nullptr) {
1457b53cb271SJim Ingham       size_t num_sections = sections->GetSize();
1458b9c1b51eSKate Stone       for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) {
1459b53cb271SJim Ingham         SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1460b9c1b51eSKate Stone         if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) {
1461b53cb271SJim Ingham           return true;
1462b53cb271SJim Ingham         }
1463b53cb271SJim Ingham       }
1464b53cb271SJim Ingham     }
1465b53cb271SJim Ingham   }
1466b53cb271SJim Ingham   return false;
1467b53cb271SJim Ingham }
14681759848bSEnrico Granata 
146997206d57SZachary Turner bool Module::LoadScriptingResourceInTarget(Target *target, Status &error,
1470b9c1b51eSKate Stone                                            Stream *feedback_stream) {
1471b9c1b51eSKate Stone   if (!target) {
14721759848bSEnrico Granata     error.SetErrorString("invalid destination Target");
14731759848bSEnrico Granata     return false;
14741759848bSEnrico Granata   }
14751759848bSEnrico Granata 
1476b9c1b51eSKate Stone   LoadScriptFromSymFile should_load =
1477b9c1b51eSKate Stone       target->TargetProperties::GetLoadScriptFromSymbolFile();
14782ea43cdcSEnrico Granata 
1479994740fbSGreg Clayton   if (should_load == eLoadScriptFromSymFileFalse)
1480994740fbSGreg Clayton     return false;
1481994740fbSGreg Clayton 
148291c0e749SGreg Clayton   Debugger &debugger = target->GetDebugger();
148391c0e749SGreg Clayton   const ScriptLanguage script_language = debugger.GetScriptLanguage();
1484b9c1b51eSKate Stone   if (script_language != eScriptLanguageNone) {
148591c0e749SGreg Clayton 
14861759848bSEnrico Granata     PlatformSP platform_sp(target->GetPlatform());
14871759848bSEnrico Granata 
1488b9c1b51eSKate Stone     if (!platform_sp) {
14891759848bSEnrico Granata       error.SetErrorString("invalid Platform");
14901759848bSEnrico Granata       return false;
14911759848bSEnrico Granata     }
14921759848bSEnrico Granata 
1493b9c1b51eSKate Stone     FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources(
1494b9c1b51eSKate Stone         target, *this, feedback_stream);
149591c0e749SGreg Clayton 
149691c0e749SGreg Clayton     const uint32_t num_specs = file_specs.GetSize();
1497b9c1b51eSKate Stone     if (num_specs) {
14982b29b432SJonas Devlieghere       ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
1499b9c1b51eSKate Stone       if (script_interpreter) {
1500b9c1b51eSKate Stone         for (uint32_t i = 0; i < num_specs; ++i) {
150191c0e749SGreg Clayton           FileSpec scripting_fspec(file_specs.GetFileSpecAtIndex(i));
1502dbd7fabaSJonas Devlieghere           if (scripting_fspec &&
1503dbd7fabaSJonas Devlieghere               FileSystem::Instance().Exists(scripting_fspec)) {
1504b9c1b51eSKate Stone             if (should_load == eLoadScriptFromSymFileWarn) {
1505397ddd5fSEnrico Granata               if (feedback_stream)
1506b9c1b51eSKate Stone                 feedback_stream->Printf(
1507b9c1b51eSKate Stone                     "warning: '%s' contains a debug script. To run this script "
1508b9c1b51eSKate Stone                     "in "
1509b9c1b51eSKate Stone                     "this debug session:\n\n    command script import "
1510b9c1b51eSKate Stone                     "\"%s\"\n\n"
1511d516deb4SJim Ingham                     "To run all discovered debug scripts in this session:\n\n"
1512b9c1b51eSKate Stone                     "    settings set target.load-script-from-symbol-file "
1513b9c1b51eSKate Stone                     "true\n",
1514d516deb4SJim Ingham                     GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1515d516deb4SJim Ingham                     scripting_fspec.GetPath().c_str());
15162ea43cdcSEnrico Granata               return false;
15172ea43cdcSEnrico Granata             }
15181759848bSEnrico Granata             StreamString scripting_stream;
15194dac97ebSRaphael Isemann             scripting_fspec.Dump(scripting_stream.AsRawOstream());
15206a51085eSJim Ingham             const bool init_lldb_globals = false;
1521b9c1b51eSKate Stone             bool did_load = script_interpreter->LoadScriptingModule(
152215625112SJonas Devlieghere                 scripting_stream.GetData(), init_lldb_globals, error);
15231759848bSEnrico Granata             if (!did_load)
15241759848bSEnrico Granata               return false;
15251759848bSEnrico Granata           }
152691c0e749SGreg Clayton         }
1527b9c1b51eSKate Stone       } else {
15281759848bSEnrico Granata         error.SetErrorString("invalid ScriptInterpreter");
15291759848bSEnrico Granata         return false;
15301759848bSEnrico Granata       }
15311759848bSEnrico Granata     }
1532b9d8890bSGreg Clayton   }
15331759848bSEnrico Granata   return true;
15341759848bSEnrico Granata }
15351759848bSEnrico Granata 
1536b9c1b51eSKate Stone bool Module::SetArchitecture(const ArchSpec &new_arch) {
1537b9c1b51eSKate Stone   if (!m_arch.IsValid()) {
15385aee162fSJim Ingham     m_arch = new_arch;
15395aee162fSJim Ingham     return true;
15405aee162fSJim Ingham   }
1541b6cd5fe9SChaoren Lin   return m_arch.IsCompatibleMatch(new_arch);
15425aee162fSJim Ingham }
15435aee162fSJim Ingham 
1544b9c1b51eSKate Stone bool Module::SetLoadAddress(Target &target, lldb::addr_t value,
1545b9c1b51eSKate Stone                             bool value_is_offset, bool &changed) {
15469e02dacdSSteve Pucci   ObjectFile *object_file = GetObjectFile();
1547b9c1b51eSKate Stone   if (object_file != nullptr) {
1548751caf65SGreg Clayton     changed = object_file->SetLoadAddress(target, value, value_is_offset);
15497524e090SGreg Clayton     return true;
1550b9c1b51eSKate Stone   } else {
15517524e090SGreg Clayton     changed = false;
1552c9660546SGreg Clayton   }
15539e02dacdSSteve Pucci   return false;
1554c9660546SGreg Clayton }
1555c9660546SGreg Clayton 
1556b9c1b51eSKate Stone bool Module::MatchesModuleSpec(const ModuleSpec &module_ref) {
1557b9a01b39SGreg Clayton   const UUID &uuid = module_ref.GetUUID();
1558b9a01b39SGreg Clayton 
1559b9c1b51eSKate Stone   if (uuid.IsValid()) {
1560b9a01b39SGreg Clayton     // If the UUID matches, then nothing more needs to match...
1561c5dac77aSEugene Zelenko     return (uuid == GetUUID());
1562b9a01b39SGreg Clayton   }
1563b9a01b39SGreg Clayton 
1564b9a01b39SGreg Clayton   const FileSpec &file_spec = module_ref.GetFileSpec();
1565532290e6SPavel Labath   if (!FileSpec::Match(file_spec, m_file) &&
1566532290e6SPavel Labath       !FileSpec::Match(file_spec, m_platform_file))
1567b9a01b39SGreg Clayton     return false;
1568b9a01b39SGreg Clayton 
1569b9a01b39SGreg Clayton   const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1570532290e6SPavel Labath   if (!FileSpec::Match(platform_file_spec, GetPlatformFileSpec()))
1571b9a01b39SGreg Clayton     return false;
1572b9a01b39SGreg Clayton 
1573b9a01b39SGreg Clayton   const ArchSpec &arch = module_ref.GetArchitecture();
1574b9c1b51eSKate Stone   if (arch.IsValid()) {
1575bf4b7be6SSean Callanan     if (!m_arch.IsCompatibleMatch(arch))
1576b9a01b39SGreg Clayton       return false;
1577b9a01b39SGreg Clayton   }
1578b9a01b39SGreg Clayton 
15790e4c4821SAdrian Prantl   ConstString object_name = module_ref.GetObjectName();
1580b9c1b51eSKate Stone   if (object_name) {
1581b9a01b39SGreg Clayton     if (object_name != GetObjectName())
1582b9a01b39SGreg Clayton       return false;
1583b9a01b39SGreg Clayton   }
1584b9a01b39SGreg Clayton   return true;
1585b9a01b39SGreg Clayton }
1586b9a01b39SGreg Clayton 
1587b9c1b51eSKate Stone bool Module::FindSourceFile(const FileSpec &orig_spec,
1588b9c1b51eSKate Stone                             FileSpec &new_spec) const {
158916ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1590d804d285SGreg Clayton   return m_source_mappings.FindFile(orig_spec, new_spec);
1591d804d285SGreg Clayton }
1592d804d285SGreg Clayton 
1593a498f0ecSZachary Turner bool Module::RemapSourceFile(llvm::StringRef path,
1594a498f0ecSZachary Turner                              std::string &new_path) const {
159516ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1596f9be6933SGreg Clayton   return m_source_mappings.RemapPath(path, new_path);
1597f9be6933SGreg Clayton }
1598f9be6933SGreg Clayton 
159924610611SAdrian Prantl bool Module::MergeArchitecture(const ArchSpec &arch_spec) {
160024610611SAdrian Prantl   if (!arch_spec.IsValid())
160124610611SAdrian Prantl     return false;
160224610611SAdrian Prantl   LLDB_LOG(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_MODULES),
160324610611SAdrian Prantl            "module has arch %s, merging/replacing with arch %s",
160424610611SAdrian Prantl            m_arch.GetTriple().getTriple().c_str(),
160524610611SAdrian Prantl            arch_spec.GetTriple().getTriple().c_str());
160624610611SAdrian Prantl   if (!m_arch.IsCompatibleMatch(arch_spec)) {
160724610611SAdrian Prantl     // The new architecture is different, we just need to replace it.
160824610611SAdrian Prantl     return SetArchitecture(arch_spec);
160924610611SAdrian Prantl   }
161024610611SAdrian Prantl 
161124610611SAdrian Prantl   // Merge bits from arch_spec into "merged_arch" and set our architecture.
161224610611SAdrian Prantl   ArchSpec merged_arch(m_arch);
161324610611SAdrian Prantl   merged_arch.MergeFrom(arch_spec);
161424610611SAdrian Prantl   // SetArchitecture() is a no-op if m_arch is already valid.
161524610611SAdrian Prantl   m_arch = ArchSpec();
161624610611SAdrian Prantl   return SetArchitecture(merged_arch);
161724610611SAdrian Prantl }
161824610611SAdrian Prantl 
16192272c481SPavel Labath llvm::VersionTuple Module::GetVersion() {
16202272c481SPavel Labath   if (ObjectFile *obj_file = GetObjectFile())
16212272c481SPavel Labath     return obj_file->GetVersion();
16222272c481SPavel Labath   return llvm::VersionTuple();
16233467d80bSEnrico Granata }
162443fe217bSGreg Clayton 
1625b9c1b51eSKate Stone bool Module::GetIsDynamicLinkEditor() {
162608928f30SGreg Clayton   ObjectFile *obj_file = GetObjectFile();
162708928f30SGreg Clayton 
162808928f30SGreg Clayton   if (obj_file)
162908928f30SGreg Clayton     return obj_file->GetIsDynamicLinkEditor();
163008928f30SGreg Clayton 
163108928f30SGreg Clayton   return false;
163208928f30SGreg Clayton }
1633