130fdc8d8SChris Lattner //===-- Module.cpp ----------------------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
9c5dac77aSEugene Zelenko #include "lldb/Core/Module.h"
10c5dac77aSEugene Zelenko 
11672d2c12SJonas Devlieghere #include "lldb/Core/AddressRange.h"
12f86248d9SRichard Mitton #include "lldb/Core/AddressResolverFileLine.h"
13672d2c12SJonas Devlieghere #include "lldb/Core/Debugger.h"
14672d2c12SJonas Devlieghere #include "lldb/Core/FileSpecList.h"
15672d2c12SJonas Devlieghere #include "lldb/Core/Mangled.h"
161f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
17672d2c12SJonas Devlieghere #include "lldb/Core/SearchFilter.h"
181f746071SGreg Clayton #include "lldb/Core/Section.h"
191408bf72SPavel Labath #include "lldb/Host/FileSystem.h"
20e38a5eddSGreg Clayton #include "lldb/Host/Host.h"
211759848bSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h"
221759848bSEnrico Granata #include "lldb/Interpreter/ScriptInterpreter.h"
231f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
24672d2c12SJonas Devlieghere #include "lldb/Symbol/Function.h"
2530fdc8d8SChris Lattner #include "lldb/Symbol/ObjectFile.h"
26672d2c12SJonas Devlieghere #include "lldb/Symbol/Symbol.h"
2730fdc8d8SChris Lattner #include "lldb/Symbol/SymbolContext.h"
2856939cb3SGreg Clayton #include "lldb/Symbol/SymbolFile.h"
2930fdc8d8SChris Lattner #include "lldb/Symbol/SymbolVendor.h"
30672d2c12SJonas Devlieghere #include "lldb/Symbol/Symtab.h"
31672d2c12SJonas Devlieghere #include "lldb/Symbol/Type.h"
32672d2c12SJonas Devlieghere #include "lldb/Symbol/TypeList.h"
33b9c1b51eSKate Stone #include "lldb/Symbol/TypeMap.h"
3456939cb3SGreg Clayton #include "lldb/Symbol/TypeSystem.h"
350e0984eeSJim Ingham #include "lldb/Target/Language.h"
36672d2c12SJonas Devlieghere #include "lldb/Target/Platform.h"
37c9660546SGreg Clayton #include "lldb/Target/Process.h"
38c9660546SGreg Clayton #include "lldb/Target/Target.h"
39666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
409fecd372SLeonard Mosescu #include "lldb/Utility/LLDBAssert.h"
416f9e6901SZachary Turner #include "lldb/Utility/Log.h"
42672d2c12SJonas Devlieghere #include "lldb/Utility/Logging.h"
43bf9a7730SZachary Turner #include "lldb/Utility/RegularExpression.h"
4497206d57SZachary Turner #include "lldb/Utility/Status.h"
45672d2c12SJonas Devlieghere #include "lldb/Utility/Stream.h"
46bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
4738d0632eSPavel Labath #include "lldb/Utility/Timer.h"
4830fdc8d8SChris Lattner 
49b1cb0b79SNico Weber #if defined(_WIN32)
50672d2c12SJonas Devlieghere #include "lldb/Host/windows/PosixApi.h"
512f3df613SZachary Turner #endif
522f3df613SZachary Turner 
532f3df613SZachary Turner #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
542f3df613SZachary Turner #include "Plugins/Language/ObjC/ObjCLanguage.h"
5523f8c95aSGreg Clayton 
56672d2c12SJonas Devlieghere #include "llvm/ADT/STLExtras.h"
57672d2c12SJonas Devlieghere #include "llvm/Support/Compiler.h"
582f3df613SZachary Turner #include "llvm/Support/FileSystem.h"
592f3df613SZachary Turner #include "llvm/Support/Signals.h"
60672d2c12SJonas Devlieghere #include "llvm/Support/raw_ostream.h"
612f3df613SZachary Turner 
62672d2c12SJonas Devlieghere #include <assert.h>
63672d2c12SJonas Devlieghere #include <cstdint>
64672d2c12SJonas Devlieghere #include <inttypes.h>
65672d2c12SJonas Devlieghere #include <map>
66672d2c12SJonas Devlieghere #include <stdarg.h>
67672d2c12SJonas Devlieghere #include <string.h>
68672d2c12SJonas Devlieghere #include <type_traits>
69672d2c12SJonas Devlieghere #include <utility>
702f3df613SZachary Turner 
712f3df613SZachary Turner namespace lldb_private {
722f3df613SZachary Turner class CompilerDeclContext;
732f3df613SZachary Turner }
742f3df613SZachary Turner namespace lldb_private {
752f3df613SZachary Turner class VariableList;
762f3df613SZachary Turner }
772f3df613SZachary Turner 
7830fdc8d8SChris Lattner using namespace lldb;
7930fdc8d8SChris Lattner using namespace lldb_private;
8030fdc8d8SChris Lattner 
8105097246SAdrian Prantl // Shared pointers to modules track module lifetimes in targets and in the
8205097246SAdrian Prantl // global module, but this collection will track all module objects that are
8305097246SAdrian Prantl // still alive
8465a03991SGreg Clayton typedef std::vector<Module *> ModuleCollection;
8565a03991SGreg Clayton 
86b9c1b51eSKate Stone static ModuleCollection &GetModuleCollection() {
87b9c1b51eSKate Stone   // This module collection needs to live past any module, so we could either
8805097246SAdrian Prantl   // make it a shared pointer in each module or just leak is.  Since it is only
8905097246SAdrian Prantl   // an empty vector by the time all the modules have gone away, we just leak
9005097246SAdrian Prantl   // it for now.  If we decide this is a big problem we can introduce a
9105097246SAdrian Prantl   // Finalize method that will tear everything down in a predictable order.
92549f7374SJim Ingham 
93c5dac77aSEugene Zelenko   static ModuleCollection *g_module_collection = nullptr;
94c5dac77aSEugene Zelenko   if (g_module_collection == nullptr)
95549f7374SJim Ingham     g_module_collection = new ModuleCollection();
96549f7374SJim Ingham 
97549f7374SJim Ingham   return *g_module_collection;
9865a03991SGreg Clayton }
9965a03991SGreg Clayton 
100b9c1b51eSKate Stone std::recursive_mutex &Module::GetAllocationModuleCollectionMutex() {
101b26e6bebSGreg Clayton   // NOTE: The mutex below must be leaked since the global module list in
10205097246SAdrian Prantl   // the ModuleList class will get torn at some point, and we can't know if it
10305097246SAdrian Prantl   // will tear itself down before the "g_module_collection_mutex" below will.
10405097246SAdrian Prantl   // So we leak a Mutex object below to safeguard against that
105b26e6bebSGreg Clayton 
10616ff8604SSaleem Abdulrasool   static std::recursive_mutex *g_module_collection_mutex = nullptr;
107c5dac77aSEugene Zelenko   if (g_module_collection_mutex == nullptr)
10816ff8604SSaleem Abdulrasool     g_module_collection_mutex = new std::recursive_mutex; // NOTE: known leak
10916ff8604SSaleem Abdulrasool   return *g_module_collection_mutex;
11065a03991SGreg Clayton }
11165a03991SGreg Clayton 
112b9c1b51eSKate Stone size_t Module::GetNumberAllocatedModules() {
113b9c1b51eSKate Stone   std::lock_guard<std::recursive_mutex> guard(
114b9c1b51eSKate Stone       GetAllocationModuleCollectionMutex());
11565a03991SGreg Clayton   return GetModuleCollection().size();
11665a03991SGreg Clayton }
11765a03991SGreg Clayton 
118b9c1b51eSKate Stone Module *Module::GetAllocatedModuleAtIndex(size_t idx) {
119b9c1b51eSKate Stone   std::lock_guard<std::recursive_mutex> guard(
120b9c1b51eSKate Stone       GetAllocationModuleCollectionMutex());
12165a03991SGreg Clayton   ModuleCollection &modules = GetModuleCollection();
12265a03991SGreg Clayton   if (idx < modules.size())
12365a03991SGreg Clayton     return modules[idx];
124c5dac77aSEugene Zelenko   return nullptr;
12565a03991SGreg Clayton }
12665a03991SGreg Clayton 
12716ff8604SSaleem Abdulrasool Module::Module(const ModuleSpec &module_spec)
1281408bf72SPavel Labath     : m_object_offset(0), m_file_has_changed(false),
129b9c1b51eSKate Stone       m_first_file_changed_log(false) {
130b9a01b39SGreg Clayton   // Scope for locker below...
131b9a01b39SGreg Clayton   {
132b9c1b51eSKate Stone     std::lock_guard<std::recursive_mutex> guard(
133b9c1b51eSKate Stone         GetAllocationModuleCollectionMutex());
134b9a01b39SGreg Clayton     GetModuleCollection().push_back(this);
135b9a01b39SGreg Clayton   }
136b9a01b39SGreg Clayton 
137b9c1b51eSKate Stone   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT |
138b9c1b51eSKate Stone                                                   LIBLLDB_LOG_MODULES));
139c5dac77aSEugene Zelenko   if (log != nullptr)
14063e5fb76SJonas Devlieghere     LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')",
14163e5fb76SJonas Devlieghere               static_cast<void *>(this),
142b9c1b51eSKate Stone               module_spec.GetArchitecture().GetArchitectureName(),
143b9c1b51eSKate Stone               module_spec.GetFileSpec().GetPath().c_str(),
14434f1159bSGreg Clayton               module_spec.GetObjectName().IsEmpty() ? "" : "(",
145b9c1b51eSKate Stone               module_spec.GetObjectName().IsEmpty()
146b9c1b51eSKate Stone                   ? ""
147b9c1b51eSKate Stone                   : module_spec.GetObjectName().AsCString(""),
14834f1159bSGreg Clayton               module_spec.GetObjectName().IsEmpty() ? "" : ")");
14934f1159bSGreg Clayton 
15005097246SAdrian Prantl   // First extract all module specifications from the file using the local file
15105097246SAdrian Prantl   // path. If there are no specifications, then don't fill anything in
15234f1159bSGreg Clayton   ModuleSpecList modules_specs;
153b9c1b51eSKate Stone   if (ObjectFile::GetModuleSpecifications(module_spec.GetFileSpec(), 0, 0,
154b9c1b51eSKate Stone                                           modules_specs) == 0)
15534f1159bSGreg Clayton     return;
15634f1159bSGreg Clayton 
15734f1159bSGreg Clayton   // Now make sure that one of the module specifications matches what we just
158b9c1b51eSKate Stone   // extract. We might have a module specification that specifies a file
15905097246SAdrian Prantl   // "/usr/lib/dyld" with UUID XXX, but we might have a local version of
16005097246SAdrian Prantl   // "/usr/lib/dyld" that has
16134f1159bSGreg Clayton   // UUID YYY and we don't want those to match. If they don't match, just don't
16234f1159bSGreg Clayton   // fill any ivars in so we don't accidentally grab the wrong file later since
16334f1159bSGreg Clayton   // they don't match...
16434f1159bSGreg Clayton   ModuleSpec matching_module_spec;
1659ba51579SLeonard Mosescu   if (!modules_specs.FindMatchingModuleSpec(module_spec,
1669ba51579SLeonard Mosescu                                             matching_module_spec)) {
1679ba51579SLeonard Mosescu     if (log) {
16863e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Found local object file but the specs didn't match");
1699ba51579SLeonard Mosescu     }
17034f1159bSGreg Clayton     return;
1719ba51579SLeonard Mosescu   }
1727ab7f89aSGreg Clayton 
1737ab7f89aSGreg Clayton   if (module_spec.GetFileSpec())
17446376966SJonas Devlieghere     m_mod_time = FileSystem::Instance().GetModificationTime(module_spec.GetFileSpec());
1757ab7f89aSGreg Clayton   else if (matching_module_spec.GetFileSpec())
1761408bf72SPavel Labath     m_mod_time =
17746376966SJonas Devlieghere         FileSystem::Instance().GetModificationTime(matching_module_spec.GetFileSpec());
1787ab7f89aSGreg Clayton 
17905097246SAdrian Prantl   // Copy the architecture from the actual spec if we got one back, else use
18005097246SAdrian Prantl   // the one that was specified
1817ab7f89aSGreg Clayton   if (matching_module_spec.GetArchitecture().IsValid())
18234f1159bSGreg Clayton     m_arch = matching_module_spec.GetArchitecture();
1837ab7f89aSGreg Clayton   else if (module_spec.GetArchitecture().IsValid())
1847ab7f89aSGreg Clayton     m_arch = module_spec.GetArchitecture();
1857ab7f89aSGreg Clayton 
186d93c4a33SBruce Mitchener   // Copy the file spec over and use the specified one (if there was one) so we
187b9c1b51eSKate Stone   // don't use a path that might have gotten resolved a path in
188b9c1b51eSKate Stone   // 'matching_module_spec'
1897ab7f89aSGreg Clayton   if (module_spec.GetFileSpec())
19034f1159bSGreg Clayton     m_file = module_spec.GetFileSpec();
1917ab7f89aSGreg Clayton   else if (matching_module_spec.GetFileSpec())
1927ab7f89aSGreg Clayton     m_file = matching_module_spec.GetFileSpec();
1937ab7f89aSGreg Clayton 
1947ab7f89aSGreg Clayton   // Copy the platform file spec over
1957ab7f89aSGreg Clayton   if (module_spec.GetPlatformFileSpec())
19634f1159bSGreg Clayton     m_platform_file = module_spec.GetPlatformFileSpec();
1977ab7f89aSGreg Clayton   else if (matching_module_spec.GetPlatformFileSpec())
1987ab7f89aSGreg Clayton     m_platform_file = matching_module_spec.GetPlatformFileSpec();
1997ab7f89aSGreg Clayton 
2007ab7f89aSGreg Clayton   // Copy the symbol file spec over
2017ab7f89aSGreg Clayton   if (module_spec.GetSymbolFileSpec())
20234f1159bSGreg Clayton     m_symfile_spec = module_spec.GetSymbolFileSpec();
2037ab7f89aSGreg Clayton   else if (matching_module_spec.GetSymbolFileSpec())
2047ab7f89aSGreg Clayton     m_symfile_spec = matching_module_spec.GetSymbolFileSpec();
2057ab7f89aSGreg Clayton 
2067ab7f89aSGreg Clayton   // Copy the object name over
2077ab7f89aSGreg Clayton   if (matching_module_spec.GetObjectName())
2087ab7f89aSGreg Clayton     m_object_name = matching_module_spec.GetObjectName();
2097ab7f89aSGreg Clayton   else
21034f1159bSGreg Clayton     m_object_name = module_spec.GetObjectName();
2117ab7f89aSGreg Clayton 
21205097246SAdrian Prantl   // Always trust the object offset (file offset) and object modification time
21305097246SAdrian Prantl   // (for mod time in a BSD static archive) of from the matching module
21405097246SAdrian Prantl   // specification
21536d7c894SGreg Clayton   m_object_offset = matching_module_spec.GetObjectOffset();
21636d7c894SGreg Clayton   m_object_mod_time = matching_module_spec.GetObjectModificationTime();
217b9a01b39SGreg Clayton }
218b9a01b39SGreg Clayton 
219b9c1b51eSKate Stone Module::Module(const FileSpec &file_spec, const ArchSpec &arch,
220b9c1b51eSKate Stone                const ConstString *object_name, lldb::offset_t object_offset,
2217e2cfbf0SPavel Labath                const llvm::sys::TimePoint<> &object_mod_time)
22246376966SJonas Devlieghere     : m_mod_time(FileSystem::Instance().GetModificationTime(file_spec)), m_arch(arch),
2231408bf72SPavel Labath       m_file(file_spec), m_object_offset(object_offset),
2247e2cfbf0SPavel Labath       m_object_mod_time(object_mod_time), m_file_has_changed(false),
2257e2cfbf0SPavel Labath       m_first_file_changed_log(false) {
22665a03991SGreg Clayton   // Scope for locker below...
22765a03991SGreg Clayton   {
228b9c1b51eSKate Stone     std::lock_guard<std::recursive_mutex> guard(
229b9c1b51eSKate Stone         GetAllocationModuleCollectionMutex());
23065a03991SGreg Clayton     GetModuleCollection().push_back(this);
23165a03991SGreg Clayton   }
23265a03991SGreg Clayton 
23330fdc8d8SChris Lattner   if (object_name)
23430fdc8d8SChris Lattner     m_object_name = *object_name;
23557abc5d6SGreg Clayton 
236b9c1b51eSKate Stone   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT |
237b9c1b51eSKate Stone                                                   LIBLLDB_LOG_MODULES));
238c5dac77aSEugene Zelenko   if (log != nullptr)
23963e5fb76SJonas Devlieghere     LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')",
24063e5fb76SJonas Devlieghere               static_cast<void *>(this), m_arch.GetArchitectureName(),
24163e5fb76SJonas Devlieghere               m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(",
242b9c1b51eSKate Stone               m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
243b9c1b51eSKate Stone               m_object_name.IsEmpty() ? "" : ")");
24430fdc8d8SChris Lattner }
24530fdc8d8SChris Lattner 
24616ff8604SSaleem Abdulrasool Module::Module()
2471408bf72SPavel Labath     : m_object_offset(0), m_file_has_changed(false),
248b9c1b51eSKate Stone       m_first_file_changed_log(false) {
249b9c1b51eSKate Stone   std::lock_guard<std::recursive_mutex> guard(
250b9c1b51eSKate Stone       GetAllocationModuleCollectionMutex());
25123f8c95aSGreg Clayton   GetModuleCollection().push_back(this);
25223f8c95aSGreg Clayton }
25323f8c95aSGreg Clayton 
254b9c1b51eSKate Stone Module::~Module() {
25505097246SAdrian Prantl   // Lock our module down while we tear everything down to make sure we don't
25605097246SAdrian Prantl   // get any access to the module while it is being destroyed
25716ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
25865a03991SGreg Clayton   // Scope for locker below...
25965a03991SGreg Clayton   {
260b9c1b51eSKate Stone     std::lock_guard<std::recursive_mutex> guard(
261b9c1b51eSKate Stone         GetAllocationModuleCollectionMutex());
26265a03991SGreg Clayton     ModuleCollection &modules = GetModuleCollection();
26365a03991SGreg Clayton     ModuleCollection::iterator end = modules.end();
26465a03991SGreg Clayton     ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
2653a18e319SGreg Clayton     assert(pos != end);
26665a03991SGreg Clayton     modules.erase(pos);
26765a03991SGreg Clayton   }
268b9c1b51eSKate Stone   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT |
269b9c1b51eSKate Stone                                                   LIBLLDB_LOG_MODULES));
270c5dac77aSEugene Zelenko   if (log != nullptr)
27163e5fb76SJonas Devlieghere     LLDB_LOGF(log, "%p Module::~Module((%s) '%s%s%s%s')",
272b9c1b51eSKate Stone               static_cast<void *>(this), m_arch.GetArchitectureName(),
273b9c1b51eSKate Stone               m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(",
27430fdc8d8SChris Lattner               m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
27530fdc8d8SChris Lattner               m_object_name.IsEmpty() ? "" : ")");
2766beaaa68SGreg Clayton   // Release any auto pointers before we start tearing down our member
2776beaaa68SGreg Clayton   // variables since the object file and symbol files might need to make
2786beaaa68SGreg Clayton   // function calls back into this module object. The ordering is important
2796beaaa68SGreg Clayton   // here because symbol files can require the module object file. So we tear
2806beaaa68SGreg Clayton   // down the symbol file first, then the object file.
281d5b44036SJonas Devlieghere   m_sections_up.reset();
282d5b44036SJonas Devlieghere   m_symfile_up.reset();
283762f7135SGreg Clayton   m_objfile_sp.reset();
28430fdc8d8SChris Lattner }
28530fdc8d8SChris Lattner 
286b9c1b51eSKate Stone ObjectFile *Module::GetMemoryObjectFile(const lldb::ProcessSP &process_sp,
28797206d57SZachary Turner                                         lldb::addr_t header_addr, Status &error,
288b9c1b51eSKate Stone                                         size_t size_to_read) {
289b9c1b51eSKate Stone   if (m_objfile_sp) {
290c7f09ccaSGreg Clayton     error.SetErrorString("object file already exists");
291b9c1b51eSKate Stone   } else {
29216ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
293b9c1b51eSKate Stone     if (process_sp) {
294c7f09ccaSGreg Clayton       m_did_load_objfile = true;
295a8f3ae7cSJonas Devlieghere       auto data_up = std::make_unique<DataBufferHeap>(size_to_read, 0);
29697206d57SZachary Turner       Status readmem_error;
297b9c1b51eSKate Stone       const size_t bytes_read =
298d5b44036SJonas Devlieghere           process_sp->ReadMemory(header_addr, data_up->GetBytes(),
299d5b44036SJonas Devlieghere                                  data_up->GetByteSize(), readmem_error);
300b9c1b51eSKate Stone       if (bytes_read == size_to_read) {
301d5b44036SJonas Devlieghere         DataBufferSP data_sp(data_up.release());
302b9c1b51eSKate Stone         m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp,
303b9c1b51eSKate Stone                                               header_addr, data_sp);
304b9c1b51eSKate Stone         if (m_objfile_sp) {
3053e10cf3bSGreg Clayton           StreamString s;
306d01b2953SDaniel Malea           s.Printf("0x%16.16" PRIx64, header_addr);
307c156427dSZachary Turner           m_object_name.SetString(s.GetString());
3083e10cf3bSGreg Clayton 
309b9c1b51eSKate Stone           // Once we get the object file, update our module with the object
31005097246SAdrian Prantl           // file's architecture since it might differ in vendor/os if some
31105097246SAdrian Prantl           // parts were unknown.
312f760f5aeSPavel Labath           m_arch = m_objfile_sp->GetArchitecture();
313a07287ecSAlex Langford 
314a07287ecSAlex Langford           // Augment the arch with the target's information in case
315a07287ecSAlex Langford           // we are unable to extract the os/environment from memory.
316a07287ecSAlex Langford           m_arch.MergeFrom(process_sp->GetTarget().GetArchitecture());
317b9c1b51eSKate Stone         } else {
318c7f09ccaSGreg Clayton           error.SetErrorString("unable to find suitable object file plug-in");
319c7f09ccaSGreg Clayton         }
320b9c1b51eSKate Stone       } else {
321b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unable to read header from memory: %s",
322b9c1b51eSKate Stone                                        readmem_error.AsCString());
323c7f09ccaSGreg Clayton       }
324b9c1b51eSKate Stone     } else {
325c7f09ccaSGreg Clayton       error.SetErrorString("invalid process");
326c7f09ccaSGreg Clayton     }
327c7f09ccaSGreg Clayton   }
328c7f09ccaSGreg Clayton   return m_objfile_sp.get();
329c7f09ccaSGreg Clayton }
330c7f09ccaSGreg Clayton 
331b9c1b51eSKate Stone const lldb_private::UUID &Module::GetUUID() {
3329fecd372SLeonard Mosescu   if (!m_did_set_uuid.load()) {
33316ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
3349fecd372SLeonard Mosescu     if (!m_did_set_uuid.load()) {
33530fdc8d8SChris Lattner       ObjectFile *obj_file = GetObjectFile();
33630fdc8d8SChris Lattner 
337b9c1b51eSKate Stone       if (obj_file != nullptr) {
338bd334efdSPavel Labath         m_uuid = obj_file->GetUUID();
3399fecd372SLeonard Mosescu         m_did_set_uuid = true;
34030fdc8d8SChris Lattner       }
34130fdc8d8SChris Lattner     }
34288c05f54SGreg Clayton   }
34330fdc8d8SChris Lattner   return m_uuid;
34430fdc8d8SChris Lattner }
34530fdc8d8SChris Lattner 
3469fecd372SLeonard Mosescu void Module::SetUUID(const lldb_private::UUID &uuid) {
3479fecd372SLeonard Mosescu   std::lock_guard<std::recursive_mutex> guard(m_mutex);
3489fecd372SLeonard Mosescu   if (!m_did_set_uuid) {
3499fecd372SLeonard Mosescu     m_uuid = uuid;
3509fecd372SLeonard Mosescu     m_did_set_uuid = true;
3519fecd372SLeonard Mosescu   } else {
352fbe748aeSRichard Smith     lldbassert(0 && "Attempting to overwrite the existing module UUID");
3539fecd372SLeonard Mosescu   }
3549fecd372SLeonard Mosescu }
3559fecd372SLeonard Mosescu 
3560e252e38SAlex Langford llvm::Expected<TypeSystem &>
3570e252e38SAlex Langford Module::GetTypeSystemForLanguage(LanguageType language) {
3585beec213SGreg Clayton   return m_type_system_map.GetTypeSystemForLanguage(language, this, true);
3596beaaa68SGreg Clayton }
3606beaaa68SGreg Clayton 
361b9c1b51eSKate Stone void Module::ParseAllDebugSymbols() {
36216ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
363c7bece56SGreg Clayton   size_t num_comp_units = GetNumCompileUnits();
36430fdc8d8SChris Lattner   if (num_comp_units == 0)
36530fdc8d8SChris Lattner     return;
36630fdc8d8SChris Lattner 
367a2eee184SGreg Clayton   SymbolContext sc;
368e1cd1be6SGreg Clayton   sc.module_sp = shared_from_this();
36923f70e83SPavel Labath   SymbolFile *symbols = GetSymbolFile();
37030fdc8d8SChris Lattner 
371b9c1b51eSKate Stone   for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++) {
37230fdc8d8SChris Lattner     sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
373863f8c18SZachary Turner     if (!sc.comp_unit)
374863f8c18SZachary Turner       continue;
375863f8c18SZachary Turner 
37630fdc8d8SChris Lattner     symbols->ParseVariablesForContext(sc);
37730fdc8d8SChris Lattner 
378863f8c18SZachary Turner     symbols->ParseFunctions(*sc.comp_unit);
37930fdc8d8SChris Lattner 
380a7f19e5fSRaphael Isemann     sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) {
381ffc1b8fdSZachary Turner       symbols->ParseBlocksRecursive(*f);
382ffc1b8fdSZachary Turner 
38330fdc8d8SChris Lattner       // Parse the variables for this function and all its blocks
384ffc1b8fdSZachary Turner       sc.function = f.get();
38530fdc8d8SChris Lattner       symbols->ParseVariablesForContext(sc);
386a7f19e5fSRaphael Isemann       return false;
387a7f19e5fSRaphael Isemann     });
38830fdc8d8SChris Lattner 
38930fdc8d8SChris Lattner     // Parse all types for this compile unit
390863f8c18SZachary Turner     symbols->ParseTypes(*sc.comp_unit);
39130fdc8d8SChris Lattner   }
39230fdc8d8SChris Lattner }
39330fdc8d8SChris Lattner 
394b9c1b51eSKate Stone void Module::CalculateSymbolContext(SymbolContext *sc) {
395e1cd1be6SGreg Clayton   sc->module_sp = shared_from_this();
39630fdc8d8SChris Lattner }
39730fdc8d8SChris Lattner 
398b9c1b51eSKate Stone ModuleSP Module::CalculateSymbolContextModule() { return shared_from_this(); }
3997e9b1fd0SGreg Clayton 
400b9c1b51eSKate Stone void Module::DumpSymbolContext(Stream *s) {
401324a1036SSaleem Abdulrasool   s->Printf(", Module{%p}", static_cast<void *>(this));
40230fdc8d8SChris Lattner }
40330fdc8d8SChris Lattner 
404b9c1b51eSKate Stone size_t Module::GetNumCompileUnits() {
40516ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
406f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
407f9d16476SPavel Labath   Timer scoped_timer(func_cat, "Module::GetNumCompileUnits (module = %p)",
408324a1036SSaleem Abdulrasool                      static_cast<void *>(this));
40923f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
41030fdc8d8SChris Lattner     return symbols->GetNumCompileUnits();
41130fdc8d8SChris Lattner   return 0;
41230fdc8d8SChris Lattner }
41330fdc8d8SChris Lattner 
414b9c1b51eSKate Stone CompUnitSP Module::GetCompileUnitAtIndex(size_t index) {
41516ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
416c7bece56SGreg Clayton   size_t num_comp_units = GetNumCompileUnits();
41730fdc8d8SChris Lattner   CompUnitSP cu_sp;
41830fdc8d8SChris Lattner 
419b9c1b51eSKate Stone   if (index < num_comp_units) {
42023f70e83SPavel Labath     if (SymbolFile *symbols = GetSymbolFile())
42130fdc8d8SChris Lattner       cu_sp = symbols->GetCompileUnitAtIndex(index);
42230fdc8d8SChris Lattner   }
42330fdc8d8SChris Lattner   return cu_sp;
42430fdc8d8SChris Lattner }
42530fdc8d8SChris Lattner 
426b9c1b51eSKate Stone bool Module::ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr) {
42716ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
428f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
429f9d16476SPavel Labath   Timer scoped_timer(func_cat,
430b9c1b51eSKate Stone                      "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")",
431b9c1b51eSKate Stone                      vm_addr);
4323046e668SGreg Clayton   SectionList *section_list = GetSectionList();
4333046e668SGreg Clayton   if (section_list)
4343046e668SGreg Clayton     return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
43530fdc8d8SChris Lattner   return false;
43630fdc8d8SChris Lattner }
43730fdc8d8SChris Lattner 
438b9c1b51eSKate Stone uint32_t Module::ResolveSymbolContextForAddress(
439991e4453SZachary Turner     const Address &so_addr, lldb::SymbolContextItem resolve_scope,
440991e4453SZachary Turner     SymbolContext &sc, bool resolve_tail_call_address) {
44116ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
44230fdc8d8SChris Lattner   uint32_t resolved_flags = 0;
44330fdc8d8SChris Lattner 
444b9c1b51eSKate Stone   // Clear the result symbol context in case we don't find anything, but don't
445b9c1b51eSKate Stone   // clear the target
44672310355SGreg Clayton   sc.Clear(false);
44730fdc8d8SChris Lattner 
44830fdc8d8SChris Lattner   // Get the section from the section/offset address.
449e72dfb32SGreg Clayton   SectionSP section_sp(so_addr.GetSection());
45030fdc8d8SChris Lattner 
45130fdc8d8SChris Lattner   // Make sure the section matches this module before we try and match anything
452b9c1b51eSKate Stone   if (section_sp && section_sp->GetModule().get() == this) {
45305097246SAdrian Prantl     // If the section offset based address resolved itself, then this is the
45405097246SAdrian Prantl     // right module.
455e1cd1be6SGreg Clayton     sc.module_sp = shared_from_this();
45630fdc8d8SChris Lattner     resolved_flags |= eSymbolContextModule;
45730fdc8d8SChris Lattner 
45823f70e83SPavel Labath     SymbolFile *symfile = GetSymbolFile();
45923f70e83SPavel Labath     if (!symfile)
46038807141SAshok Thirumurthi       return resolved_flags;
46138807141SAshok Thirumurthi 
46205097246SAdrian Prantl     // Resolve the compile unit, function, block, line table or line entry if
46305097246SAdrian Prantl     // requested.
46430fdc8d8SChris Lattner     if (resolve_scope & eSymbolContextCompUnit ||
46530fdc8d8SChris Lattner         resolve_scope & eSymbolContextFunction ||
46630fdc8d8SChris Lattner         resolve_scope & eSymbolContextBlock ||
4674c8e7828SGreg Clayton         resolve_scope & eSymbolContextLineEntry ||
468b9c1b51eSKate Stone         resolve_scope & eSymbolContextVariable) {
469b9c1b51eSKate Stone       resolved_flags |=
47023f70e83SPavel Labath           symfile->ResolveSymbolContext(so_addr, resolve_scope, sc);
47130fdc8d8SChris Lattner     }
47230fdc8d8SChris Lattner 
47305097246SAdrian Prantl     // Resolve the symbol if requested, but don't re-look it up if we've
47405097246SAdrian Prantl     // already found it.
475b9c1b51eSKate Stone     if (resolve_scope & eSymbolContextSymbol &&
476b9c1b51eSKate Stone         !(resolved_flags & eSymbolContextSymbol)) {
47723f70e83SPavel Labath       Symtab *symtab = symfile->GetSymtab();
478b9c1b51eSKate Stone       if (symtab && so_addr.IsSectionOffset()) {
4790d9dd7dfSMohit K. Bhakkad         Symbol *matching_symbol = nullptr;
480c35b91ceSAdrian McCarthy 
481b9c1b51eSKate Stone         symtab->ForEachSymbolContainingFileAddress(
482b9c1b51eSKate Stone             so_addr.GetFileAddress(),
483c35b91ceSAdrian McCarthy             [&matching_symbol](Symbol *symbol) -> bool {
484b9c1b51eSKate Stone               if (symbol->GetType() != eSymbolTypeInvalid) {
4850d9dd7dfSMohit K. Bhakkad                 matching_symbol = symbol;
4860d9dd7dfSMohit K. Bhakkad                 return false; // Stop iterating
4870d9dd7dfSMohit K. Bhakkad               }
4880d9dd7dfSMohit K. Bhakkad               return true; // Keep iterating
4890d9dd7dfSMohit K. Bhakkad             });
4900d9dd7dfSMohit K. Bhakkad         sc.symbol = matching_symbol;
491b9c1b51eSKate Stone         if (!sc.symbol && resolve_scope & eSymbolContextFunction &&
492b9c1b51eSKate Stone             !(resolved_flags & eSymbolContextFunction)) {
493b9c1b51eSKate Stone           bool verify_unique = false; // No need to check again since
494b9c1b51eSKate Stone                                       // ResolveSymbolContext failed to find a
495b9c1b51eSKate Stone                                       // symbol at this address.
49635729bb1SAshok Thirumurthi           if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
497b9c1b51eSKate Stone             sc.symbol =
498b9c1b51eSKate Stone                 obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
49935729bb1SAshok Thirumurthi         }
50035729bb1SAshok Thirumurthi 
501b9c1b51eSKate Stone         if (sc.symbol) {
502b9c1b51eSKate Stone           if (sc.symbol->IsSynthetic()) {
50305097246SAdrian Prantl             // We have a synthetic symbol so lets check if the object file from
50405097246SAdrian Prantl             // the symbol file in the symbol vendor is different than the
50505097246SAdrian Prantl             // object file for the module, and if so search its symbol table to
50605097246SAdrian Prantl             // see if we can come up with a better symbol. For example dSYM
50705097246SAdrian Prantl             // files on MacOSX have an unstripped symbol table inside of them.
50893e2861bSGreg Clayton             ObjectFile *symtab_objfile = symtab->GetObjectFile();
509b9c1b51eSKate Stone             if (symtab_objfile && symtab_objfile->IsStripped()) {
51093e2861bSGreg Clayton               ObjectFile *symfile_objfile = symfile->GetObjectFile();
511b9c1b51eSKate Stone               if (symfile_objfile != symtab_objfile) {
51293e2861bSGreg Clayton                 Symtab *symfile_symtab = symfile_objfile->GetSymtab();
513b9c1b51eSKate Stone                 if (symfile_symtab) {
514b9c1b51eSKate Stone                   Symbol *symbol =
515b9c1b51eSKate Stone                       symfile_symtab->FindSymbolContainingFileAddress(
516b9c1b51eSKate Stone                           so_addr.GetFileAddress());
517b9c1b51eSKate Stone                   if (symbol && !symbol->IsSynthetic()) {
51893e2861bSGreg Clayton                     sc.symbol = symbol;
51993e2861bSGreg Clayton                   }
52093e2861bSGreg Clayton                 }
52193e2861bSGreg Clayton               }
52293e2861bSGreg Clayton             }
52393e2861bSGreg Clayton           }
52430fdc8d8SChris Lattner           resolved_flags |= eSymbolContextSymbol;
52530fdc8d8SChris Lattner         }
52630fdc8d8SChris Lattner       }
52793e2861bSGreg Clayton     }
52838807141SAshok Thirumurthi 
529b9c1b51eSKate Stone     // For function symbols, so_addr may be off by one.  This is a convention
53005097246SAdrian Prantl     // consistent with FDE row indices in eh_frame sections, but requires extra
53105097246SAdrian Prantl     // logic here to permit symbol lookup for disassembly and unwind.
532b9c1b51eSKate Stone     if (resolve_scope & eSymbolContextSymbol &&
533b9c1b51eSKate Stone         !(resolved_flags & eSymbolContextSymbol) && resolve_tail_call_address &&
534b9c1b51eSKate Stone         so_addr.IsSectionOffset()) {
53538807141SAshok Thirumurthi       Address previous_addr = so_addr;
536edfaae39SGreg Clayton       previous_addr.Slide(-1);
53738807141SAshok Thirumurthi 
53835729bb1SAshok Thirumurthi       bool do_resolve_tail_call_address = false; // prevent recursion
539b9c1b51eSKate Stone       const uint32_t flags = ResolveSymbolContextForAddress(
540b9c1b51eSKate Stone           previous_addr, resolve_scope, sc, do_resolve_tail_call_address);
541b9c1b51eSKate Stone       if (flags & eSymbolContextSymbol) {
54238807141SAshok Thirumurthi         AddressRange addr_range;
543b9c1b51eSKate Stone         if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
544b9c1b51eSKate Stone                                false, addr_range)) {
545b9c1b51eSKate Stone           if (addr_range.GetBaseAddress().GetSection() ==
546b9c1b51eSKate Stone               so_addr.GetSection()) {
547b9c1b51eSKate Stone             // If the requested address is one past the address range of a
54805097246SAdrian Prantl             // function (i.e. a tail call), or the decremented address is the
54905097246SAdrian Prantl             // start of a function (i.e. some forms of trampoline), indicate
55005097246SAdrian Prantl             // that the symbol has been resolved.
551b9c1b51eSKate Stone             if (so_addr.GetOffset() ==
552b9c1b51eSKate Stone                     addr_range.GetBaseAddress().GetOffset() ||
553b9c1b51eSKate Stone                 so_addr.GetOffset() ==
554b9c1b51eSKate Stone                     addr_range.GetBaseAddress().GetOffset() +
555b9c1b51eSKate Stone                         addr_range.GetByteSize()) {
55638807141SAshok Thirumurthi               resolved_flags |= flags;
55738807141SAshok Thirumurthi             }
558b9c1b51eSKate Stone           } else {
559b9c1b51eSKate Stone             sc.symbol =
560b9c1b51eSKate Stone                 nullptr; // Don't trust the symbol if the sections didn't match.
56138807141SAshok Thirumurthi           }
56238807141SAshok Thirumurthi         }
56330fdc8d8SChris Lattner       }
56430fdc8d8SChris Lattner     }
56530fdc8d8SChris Lattner   }
56630fdc8d8SChris Lattner   return resolved_flags;
56730fdc8d8SChris Lattner }
56830fdc8d8SChris Lattner 
569991e4453SZachary Turner uint32_t Module::ResolveSymbolContextForFilePath(
570991e4453SZachary Turner     const char *file_path, uint32_t line, bool check_inlines,
571991e4453SZachary Turner     lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
5728f3be7a3SJonas Devlieghere   FileSpec file_spec(file_path);
573b9c1b51eSKate Stone   return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
574b9c1b51eSKate Stone                                           resolve_scope, sc_list);
57530fdc8d8SChris Lattner }
57630fdc8d8SChris Lattner 
577991e4453SZachary Turner uint32_t Module::ResolveSymbolContextsForFileSpec(
578991e4453SZachary Turner     const FileSpec &file_spec, uint32_t line, bool check_inlines,
579991e4453SZachary Turner     lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
58016ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
581f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
582f9d16476SPavel Labath   Timer scoped_timer(func_cat,
583b9c1b51eSKate Stone                      "Module::ResolveSymbolContextForFilePath (%s:%u, "
584b9c1b51eSKate Stone                      "check_inlines = %s, resolve_scope = 0x%8.8x)",
585b9c1b51eSKate Stone                      file_spec.GetPath().c_str(), line,
586b9c1b51eSKate Stone                      check_inlines ? "yes" : "no", resolve_scope);
58730fdc8d8SChris Lattner 
58830fdc8d8SChris Lattner   const uint32_t initial_count = sc_list.GetSize();
58930fdc8d8SChris Lattner 
59023f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
591b9c1b51eSKate Stone     symbols->ResolveSymbolContext(file_spec, line, check_inlines, resolve_scope,
592b9c1b51eSKate Stone                                   sc_list);
59330fdc8d8SChris Lattner 
59430fdc8d8SChris Lattner   return sc_list.GetSize() - initial_count;
59530fdc8d8SChris Lattner }
59630fdc8d8SChris Lattner 
5970e4c4821SAdrian Prantl size_t Module::FindGlobalVariables(ConstString name,
59899558cc4SGreg Clayton                                    const CompilerDeclContext *parent_decl_ctx,
599c7bece56SGreg Clayton                                    size_t max_matches,
600b9c1b51eSKate Stone                                    VariableList &variables) {
60123f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
60234cda14bSPavel Labath     return symbols->FindGlobalVariables(name, parent_decl_ctx, max_matches,
60334cda14bSPavel Labath                                         variables);
60434cda14bSPavel Labath   return 0;
60534cda14bSPavel Labath }
60634cda14bSPavel Labath 
60734cda14bSPavel Labath size_t Module::FindGlobalVariables(const RegularExpression &regex,
60834cda14bSPavel Labath                                    size_t max_matches,
60934cda14bSPavel Labath                                    VariableList &variables) {
61023f70e83SPavel Labath   SymbolFile *symbols = GetSymbolFile();
61134cda14bSPavel Labath   if (symbols)
61234cda14bSPavel Labath     return symbols->FindGlobalVariables(regex, max_matches, variables);
61330fdc8d8SChris Lattner   return 0;
61430fdc8d8SChris Lattner }
61530fdc8d8SChris Lattner 
616b9c1b51eSKate Stone size_t Module::FindCompileUnits(const FileSpec &path, bool append,
617b9c1b51eSKate Stone                                 SymbolContextList &sc_list) {
618644247c1SGreg Clayton   if (!append)
619644247c1SGreg Clayton     sc_list.Clear();
620644247c1SGreg Clayton 
621c7bece56SGreg Clayton   const size_t start_size = sc_list.GetSize();
622c7bece56SGreg Clayton   const size_t num_compile_units = GetNumCompileUnits();
623644247c1SGreg Clayton   SymbolContext sc;
624e1cd1be6SGreg Clayton   sc.module_sp = shared_from_this();
625ddd7a2a6SSean Callanan   const bool compare_directory = (bool)path.GetDirectory();
626b9c1b51eSKate Stone   for (size_t i = 0; i < num_compile_units; ++i) {
627644247c1SGreg Clayton     sc.comp_unit = GetCompileUnitAtIndex(i).get();
628b9c1b51eSKate Stone     if (sc.comp_unit) {
629644247c1SGreg Clayton       if (FileSpec::Equal(*sc.comp_unit, path, compare_directory))
630644247c1SGreg Clayton         sc_list.Append(sc);
631644247c1SGreg Clayton     }
6322dafd8edSGreg Clayton   }
633644247c1SGreg Clayton   return sc_list.GetSize() - start_size;
634644247c1SGreg Clayton }
635644247c1SGreg Clayton 
6360e4c4821SAdrian Prantl Module::LookupInfo::LookupInfo(ConstString name,
637117b1fa1SZachary Turner                                FunctionNameType name_type_mask,
638117b1fa1SZachary Turner                                LanguageType language)
639117b1fa1SZachary Turner     : m_name(name), m_lookup_name(), m_language(language),
640117b1fa1SZachary Turner       m_name_type_mask(eFunctionNameTypeNone),
641b9c1b51eSKate Stone       m_match_name_after_lookup(false) {
6426234a5c8SGreg Clayton   const char *name_cstr = name.GetCString();
6436234a5c8SGreg Clayton   llvm::StringRef basename;
6446234a5c8SGreg Clayton   llvm::StringRef context;
6456234a5c8SGreg Clayton 
646b9c1b51eSKate Stone   if (name_type_mask & eFunctionNameTypeAuto) {
6476234a5c8SGreg Clayton     if (CPlusPlusLanguage::IsCPPMangledName(name_cstr))
6486234a5c8SGreg Clayton       m_name_type_mask = eFunctionNameTypeFull;
6496234a5c8SGreg Clayton     else if ((language == eLanguageTypeUnknown ||
6506234a5c8SGreg Clayton               Language::LanguageIsObjC(language)) &&
6516234a5c8SGreg Clayton              ObjCLanguage::IsPossibleObjCMethodName(name_cstr))
6526234a5c8SGreg Clayton       m_name_type_mask = eFunctionNameTypeFull;
653b9c1b51eSKate Stone     else if (Language::LanguageIsC(language)) {
6546234a5c8SGreg Clayton       m_name_type_mask = eFunctionNameTypeFull;
655b9c1b51eSKate Stone     } else {
6566234a5c8SGreg Clayton       if ((language == eLanguageTypeUnknown ||
6576234a5c8SGreg Clayton            Language::LanguageIsObjC(language)) &&
6586234a5c8SGreg Clayton           ObjCLanguage::IsPossibleObjCSelector(name_cstr))
6596234a5c8SGreg Clayton         m_name_type_mask |= eFunctionNameTypeSelector;
6606234a5c8SGreg Clayton 
6616234a5c8SGreg Clayton       CPlusPlusLanguage::MethodName cpp_method(name);
6626234a5c8SGreg Clayton       basename = cpp_method.GetBasename();
663b9c1b51eSKate Stone       if (basename.empty()) {
664b9c1b51eSKate Stone         if (CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
665b9c1b51eSKate Stone                                                            basename))
6666234a5c8SGreg Clayton           m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
6676234a5c8SGreg Clayton         else
6686234a5c8SGreg Clayton           m_name_type_mask |= eFunctionNameTypeFull;
669b9c1b51eSKate Stone       } else {
6706234a5c8SGreg Clayton         m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
6716234a5c8SGreg Clayton       }
6726234a5c8SGreg Clayton     }
673b9c1b51eSKate Stone   } else {
6746234a5c8SGreg Clayton     m_name_type_mask = name_type_mask;
675b9c1b51eSKate Stone     if (name_type_mask & eFunctionNameTypeMethod ||
676b9c1b51eSKate Stone         name_type_mask & eFunctionNameTypeBase) {
677b9c1b51eSKate Stone       // If they've asked for a CPP method or function name and it can't be
67805097246SAdrian Prantl       // that, we don't even need to search for CPP methods or names.
6796234a5c8SGreg Clayton       CPlusPlusLanguage::MethodName cpp_method(name);
680b9c1b51eSKate Stone       if (cpp_method.IsValid()) {
6816234a5c8SGreg Clayton         basename = cpp_method.GetBasename();
6826234a5c8SGreg Clayton 
683b9c1b51eSKate Stone         if (!cpp_method.GetQualifiers().empty()) {
684b9c1b51eSKate Stone           // There is a "const" or other qualifier following the end of the
68505097246SAdrian Prantl           // function parens, this can't be a eFunctionNameTypeBase
6866234a5c8SGreg Clayton           m_name_type_mask &= ~(eFunctionNameTypeBase);
6876234a5c8SGreg Clayton           if (m_name_type_mask == eFunctionNameTypeNone)
6886234a5c8SGreg Clayton             return;
6896234a5c8SGreg Clayton         }
690b9c1b51eSKate Stone       } else {
691b9c1b51eSKate Stone         // If the CPP method parser didn't manage to chop this up, try to fill
69205097246SAdrian Prantl         // in the base name if we can. If a::b::c is passed in, we need to just
69305097246SAdrian Prantl         // look up "c", and then we'll filter the result later.
694b9c1b51eSKate Stone         CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
695b9c1b51eSKate Stone                                                        basename);
6966234a5c8SGreg Clayton       }
6976234a5c8SGreg Clayton     }
6986234a5c8SGreg Clayton 
699b9c1b51eSKate Stone     if (name_type_mask & eFunctionNameTypeSelector) {
700b9c1b51eSKate Stone       if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr)) {
7016234a5c8SGreg Clayton         m_name_type_mask &= ~(eFunctionNameTypeSelector);
7026234a5c8SGreg Clayton         if (m_name_type_mask == eFunctionNameTypeNone)
7036234a5c8SGreg Clayton           return;
7046234a5c8SGreg Clayton       }
7056234a5c8SGreg Clayton     }
7066234a5c8SGreg Clayton 
707b9c1b51eSKate Stone     // Still try and get a basename in case someone specifies a name type mask
7085d0c1146SGreg Clayton     // of eFunctionNameTypeFull and a name like "A::func"
709b9c1b51eSKate Stone     if (basename.empty()) {
7105d0c1146SGreg Clayton       if (name_type_mask & eFunctionNameTypeFull &&
7115d0c1146SGreg Clayton           !CPlusPlusLanguage::IsCPPMangledName(name_cstr)) {
7126234a5c8SGreg Clayton         CPlusPlusLanguage::MethodName cpp_method(name);
7136234a5c8SGreg Clayton         basename = cpp_method.GetBasename();
7146234a5c8SGreg Clayton         if (basename.empty())
715b9c1b51eSKate Stone           CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
716b9c1b51eSKate Stone                                                          basename);
7176234a5c8SGreg Clayton       }
7186234a5c8SGreg Clayton     }
7196234a5c8SGreg Clayton   }
7206234a5c8SGreg Clayton 
721b9c1b51eSKate Stone   if (!basename.empty()) {
72205097246SAdrian Prantl     // The name supplied was a partial C++ path like "a::count". In this case
72305097246SAdrian Prantl     // we want to do a lookup on the basename "count" and then make sure any
72405097246SAdrian Prantl     // matching results contain "a::count" so that it would match "b::a::count"
72505097246SAdrian Prantl     // and "a::count". This is why we set "match_name_after_lookup" to true
7266234a5c8SGreg Clayton     m_lookup_name.SetString(basename);
7276234a5c8SGreg Clayton     m_match_name_after_lookup = true;
728b9c1b51eSKate Stone   } else {
729b9c1b51eSKate Stone     // The name is already correct, just use the exact name as supplied, and we
73005097246SAdrian Prantl     // won't need to check if any matches contain "name"
7316234a5c8SGreg Clayton     m_lookup_name = name;
7326234a5c8SGreg Clayton     m_match_name_after_lookup = false;
7336234a5c8SGreg Clayton   }
7346234a5c8SGreg Clayton }
7356234a5c8SGreg Clayton 
736b9c1b51eSKate Stone void Module::LookupInfo::Prune(SymbolContextList &sc_list,
737b9c1b51eSKate Stone                                size_t start_idx) const {
738b9c1b51eSKate Stone   if (m_match_name_after_lookup && m_name) {
7396234a5c8SGreg Clayton     SymbolContext sc;
7406234a5c8SGreg Clayton     size_t i = start_idx;
741b9c1b51eSKate Stone     while (i < sc_list.GetSize()) {
7426234a5c8SGreg Clayton       if (!sc_list.GetContextAtIndex(i, sc))
7436234a5c8SGreg Clayton         break;
7446234a5c8SGreg Clayton       ConstString full_name(sc.GetFunctionName());
745b9c1b51eSKate Stone       if (full_name &&
746b9c1b51eSKate Stone           ::strstr(full_name.GetCString(), m_name.GetCString()) == nullptr) {
7476234a5c8SGreg Clayton         sc_list.RemoveContextAtIndex(i);
748b9c1b51eSKate Stone       } else {
7496234a5c8SGreg Clayton         ++i;
7506234a5c8SGreg Clayton       }
7516234a5c8SGreg Clayton     }
7526234a5c8SGreg Clayton   }
7536234a5c8SGreg Clayton 
754b9c1b51eSKate Stone   // If we have only full name matches we might have tried to set breakpoint on
7555d0c1146SGreg Clayton   // "func" and specified eFunctionNameTypeFull, but we might have found
7565d0c1146SGreg Clayton   // "a::func()", "a::b::func()", "c::func()", "func()" and "func". Only
7575d0c1146SGreg Clayton   // "func()" and "func" should end up matching.
758b9c1b51eSKate Stone   if (m_name_type_mask == eFunctionNameTypeFull) {
7596234a5c8SGreg Clayton     SymbolContext sc;
7606234a5c8SGreg Clayton     size_t i = start_idx;
761b9c1b51eSKate Stone     while (i < sc_list.GetSize()) {
7626234a5c8SGreg Clayton       if (!sc_list.GetContextAtIndex(i, sc))
7636234a5c8SGreg Clayton         break;
76405097246SAdrian Prantl       // Make sure the mangled and demangled names don't match before we try to
76505097246SAdrian Prantl       // pull anything out
7665d0c1146SGreg Clayton       ConstString mangled_name(sc.GetFunctionName(Mangled::ePreferMangled));
7676234a5c8SGreg Clayton       ConstString full_name(sc.GetFunctionName());
7685d0c1146SGreg Clayton       if (mangled_name != m_name && full_name != m_name)
7695d0c1146SGreg Clayton       {
7706234a5c8SGreg Clayton         CPlusPlusLanguage::MethodName cpp_method(full_name);
771b9c1b51eSKate Stone         if (cpp_method.IsValid()) {
772b9c1b51eSKate Stone           if (cpp_method.GetContext().empty()) {
773b9c1b51eSKate Stone             if (cpp_method.GetBasename().compare(m_name.GetStringRef()) != 0) {
7746234a5c8SGreg Clayton               sc_list.RemoveContextAtIndex(i);
7756234a5c8SGreg Clayton               continue;
7766234a5c8SGreg Clayton             }
777b9c1b51eSKate Stone           } else {
7785d0c1146SGreg Clayton             std::string qualified_name;
7795d0c1146SGreg Clayton             llvm::StringRef anon_prefix("(anonymous namespace)");
7805d0c1146SGreg Clayton             if (cpp_method.GetContext() == anon_prefix)
7815d0c1146SGreg Clayton               qualified_name = cpp_method.GetBasename().str();
7825d0c1146SGreg Clayton             else
7835d0c1146SGreg Clayton               qualified_name = cpp_method.GetScopeQualifiedName();
7848d20cfdfSJonas Devlieghere             if (qualified_name != m_name.GetCString()) {
7856234a5c8SGreg Clayton               sc_list.RemoveContextAtIndex(i);
7866234a5c8SGreg Clayton               continue;
7876234a5c8SGreg Clayton             }
7886234a5c8SGreg Clayton           }
7896234a5c8SGreg Clayton         }
7905d0c1146SGreg Clayton       }
7916234a5c8SGreg Clayton       ++i;
7926234a5c8SGreg Clayton     }
7936234a5c8SGreg Clayton   }
7946234a5c8SGreg Clayton }
7956234a5c8SGreg Clayton 
7960e4c4821SAdrian Prantl size_t Module::FindFunctions(ConstString name,
79799558cc4SGreg Clayton                              const CompilerDeclContext *parent_decl_ctx,
798117b1fa1SZachary Turner                              FunctionNameType name_type_mask,
799117b1fa1SZachary Turner                              bool include_symbols, bool include_inlines,
800117b1fa1SZachary Turner                              bool append, SymbolContextList &sc_list) {
801931180e6SGreg Clayton   if (!append)
802931180e6SGreg Clayton     sc_list.Clear();
803931180e6SGreg Clayton 
80443fe217bSGreg Clayton   const size_t old_size = sc_list.GetSize();
805931180e6SGreg Clayton 
806931180e6SGreg Clayton   // Find all the functions (not symbols, but debug information functions...
80723f70e83SPavel Labath   SymbolFile *symbols = GetSymbolFile();
80843fe217bSGreg Clayton 
809b9c1b51eSKate Stone   if (name_type_mask & eFunctionNameTypeAuto) {
8106234a5c8SGreg Clayton     LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
81143fe217bSGreg Clayton 
812b9c1b51eSKate Stone     if (symbols) {
813b9c1b51eSKate Stone       symbols->FindFunctions(lookup_info.GetLookupName(), parent_decl_ctx,
814b9c1b51eSKate Stone                              lookup_info.GetNameTypeMask(), include_inlines,
815b9c1b51eSKate Stone                              append, sc_list);
81643fe217bSGreg Clayton 
817b9c1b51eSKate Stone       // Now check our symbol table for symbols that are code symbols if
818b9c1b51eSKate Stone       // requested
819b9c1b51eSKate Stone       if (include_symbols) {
820a7499c98SMichael Sartain         Symtab *symtab = symbols->GetSymtab();
82143fe217bSGreg Clayton         if (symtab)
822b9c1b51eSKate Stone           symtab->FindFunctionSymbols(lookup_info.GetLookupName(),
823b9c1b51eSKate Stone                                       lookup_info.GetNameTypeMask(), sc_list);
82443fe217bSGreg Clayton       }
82543fe217bSGreg Clayton     }
82643fe217bSGreg Clayton 
8276234a5c8SGreg Clayton     const size_t new_size = sc_list.GetSize();
8286234a5c8SGreg Clayton 
8296234a5c8SGreg Clayton     if (old_size < new_size)
8306234a5c8SGreg Clayton       lookup_info.Prune(sc_list, old_size);
831b9c1b51eSKate Stone   } else {
832b9c1b51eSKate Stone     if (symbols) {
833b9c1b51eSKate Stone       symbols->FindFunctions(name, parent_decl_ctx, name_type_mask,
834b9c1b51eSKate Stone                              include_inlines, append, sc_list);
835931180e6SGreg Clayton 
836b9c1b51eSKate Stone       // Now check our symbol table for symbols that are code symbols if
837b9c1b51eSKate Stone       // requested
838b9c1b51eSKate Stone       if (include_symbols) {
839a7499c98SMichael Sartain         Symtab *symtab = symbols->GetSymtab();
840931180e6SGreg Clayton         if (symtab)
84143fe217bSGreg Clayton           symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
842931180e6SGreg Clayton       }
843931180e6SGreg Clayton     }
844931180e6SGreg Clayton   }
84543fe217bSGreg Clayton 
84643fe217bSGreg Clayton   return sc_list.GetSize() - old_size;
84730fdc8d8SChris Lattner }
84830fdc8d8SChris Lattner 
849b9c1b51eSKate Stone size_t Module::FindFunctions(const RegularExpression &regex,
850b9c1b51eSKate Stone                              bool include_symbols, bool include_inlines,
851b9c1b51eSKate Stone                              bool append, SymbolContextList &sc_list) {
852931180e6SGreg Clayton   if (!append)
853931180e6SGreg Clayton     sc_list.Clear();
854931180e6SGreg Clayton 
855c7bece56SGreg Clayton   const size_t start_size = sc_list.GetSize();
856931180e6SGreg Clayton 
85723f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile()) {
8589df05fbbSSean Callanan     symbols->FindFunctions(regex, include_inlines, append, sc_list);
859a7499c98SMichael Sartain 
86005097246SAdrian Prantl     // Now check our symbol table for symbols that are code symbols if
86105097246SAdrian Prantl     // requested
862b9c1b51eSKate Stone     if (include_symbols) {
863a7499c98SMichael Sartain       Symtab *symtab = symbols->GetSymtab();
864b9c1b51eSKate Stone       if (symtab) {
865931180e6SGreg Clayton         std::vector<uint32_t> symbol_indexes;
866b9c1b51eSKate Stone         symtab->AppendSymbolIndexesMatchingRegExAndType(
867b9c1b51eSKate Stone             regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny,
868b9c1b51eSKate Stone             symbol_indexes);
869c7bece56SGreg Clayton         const size_t num_matches = symbol_indexes.size();
870b9c1b51eSKate Stone         if (num_matches) {
871931180e6SGreg Clayton           SymbolContext sc(this);
872d8cf1a11SGreg Clayton           const size_t end_functions_added_index = sc_list.GetSize();
873b9c1b51eSKate Stone           size_t num_functions_added_to_sc_list =
874b9c1b51eSKate Stone               end_functions_added_index - start_size;
875b9c1b51eSKate Stone           if (num_functions_added_to_sc_list == 0) {
87605097246SAdrian Prantl             // No functions were added, just symbols, so we can just append
87705097246SAdrian Prantl             // them
878b9c1b51eSKate Stone             for (size_t i = 0; i < num_matches; ++i) {
879931180e6SGreg Clayton               sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
88000049b8bSMatt Kopec               SymbolType sym_type = sc.symbol->GetType();
88100049b8bSMatt Kopec               if (sc.symbol && (sym_type == eSymbolTypeCode ||
88200049b8bSMatt Kopec                                 sym_type == eSymbolTypeResolver))
883d8cf1a11SGreg Clayton                 sc_list.Append(sc);
884d8cf1a11SGreg Clayton             }
885b9c1b51eSKate Stone           } else {
886d8cf1a11SGreg Clayton             typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
887d8cf1a11SGreg Clayton             FileAddrToIndexMap file_addr_to_index;
888b9c1b51eSKate Stone             for (size_t i = start_size; i < end_functions_added_index; ++i) {
889d8cf1a11SGreg Clayton               const SymbolContext &sc = sc_list[i];
890d8cf1a11SGreg Clayton               if (sc.block)
891d8cf1a11SGreg Clayton                 continue;
892b9c1b51eSKate Stone               file_addr_to_index[sc.function->GetAddressRange()
893b9c1b51eSKate Stone                                      .GetBaseAddress()
894b9c1b51eSKate Stone                                      .GetFileAddress()] = i;
895d8cf1a11SGreg Clayton             }
896d8cf1a11SGreg Clayton 
897d8cf1a11SGreg Clayton             FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
898d8cf1a11SGreg Clayton             // Functions were added so we need to merge symbols into any
899d8cf1a11SGreg Clayton             // existing function symbol contexts
900b9c1b51eSKate Stone             for (size_t i = start_size; i < num_matches; ++i) {
901d8cf1a11SGreg Clayton               sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
902d8cf1a11SGreg Clayton               SymbolType sym_type = sc.symbol->GetType();
903b9c1b51eSKate Stone               if (sc.symbol && sc.symbol->ValueIsAddress() &&
904b9c1b51eSKate Stone                   (sym_type == eSymbolTypeCode ||
905b9c1b51eSKate Stone                    sym_type == eSymbolTypeResolver)) {
906b9c1b51eSKate Stone                 FileAddrToIndexMap::const_iterator pos =
907b9c1b51eSKate Stone                     file_addr_to_index.find(
908b9c1b51eSKate Stone                         sc.symbol->GetAddressRef().GetFileAddress());
909d8cf1a11SGreg Clayton                 if (pos == end)
910d8cf1a11SGreg Clayton                   sc_list.Append(sc);
911d8cf1a11SGreg Clayton                 else
912d8cf1a11SGreg Clayton                   sc_list[pos->second].symbol = sc.symbol;
913d8cf1a11SGreg Clayton               }
914d8cf1a11SGreg Clayton             }
915931180e6SGreg Clayton           }
916931180e6SGreg Clayton         }
917931180e6SGreg Clayton       }
918931180e6SGreg Clayton     }
919931180e6SGreg Clayton   }
920931180e6SGreg Clayton   return sc_list.GetSize() - start_size;
92130fdc8d8SChris Lattner }
92230fdc8d8SChris Lattner 
923b9c1b51eSKate Stone void Module::FindAddressesForLine(const lldb::TargetSP target_sp,
924f86248d9SRichard Mitton                                   const FileSpec &file, uint32_t line,
925f86248d9SRichard Mitton                                   Function *function,
926b9c1b51eSKate Stone                                   std::vector<Address> &output_local,
927b9c1b51eSKate Stone                                   std::vector<Address> &output_extern) {
928f86248d9SRichard Mitton   SearchFilterByModule filter(target_sp, m_file);
929f86248d9SRichard Mitton   AddressResolverFileLine resolver(file, line, true);
930f86248d9SRichard Mitton   resolver.ResolveAddress(filter);
931f86248d9SRichard Mitton 
932b9c1b51eSKate Stone   for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++) {
933f86248d9SRichard Mitton     Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
934f86248d9SRichard Mitton     Function *f = addr.CalculateSymbolContextFunction();
935f86248d9SRichard Mitton     if (f && f == function)
936f86248d9SRichard Mitton       output_local.push_back(addr);
937f86248d9SRichard Mitton     else
938f86248d9SRichard Mitton       output_extern.push_back(addr);
939f86248d9SRichard Mitton   }
940f86248d9SRichard Mitton }
941f86248d9SRichard Mitton 
942b9c1b51eSKate Stone size_t Module::FindTypes_Impl(
9430e4c4821SAdrian Prantl     ConstString name, const CompilerDeclContext *parent_decl_ctx,
944*d4d428efSAdrian Prantl     size_t max_matches,
945ae088e52SGreg Clayton     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
946b9c1b51eSKate Stone     TypeMap &types) {
947f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
948f9d16476SPavel Labath   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
94923f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
950*d4d428efSAdrian Prantl     return symbols->FindTypes(name, parent_decl_ctx, max_matches,
951b9c1b51eSKate Stone                               searched_symbol_files, types);
9523504eee8SGreg Clayton   return 0;
9533504eee8SGreg Clayton }
9543504eee8SGreg Clayton 
9550e4c4821SAdrian Prantl size_t Module::FindTypesInNamespace(ConstString type_name,
95699558cc4SGreg Clayton                                     const CompilerDeclContext *parent_decl_ctx,
957b9c1b51eSKate Stone                                     size_t max_matches, TypeList &type_list) {
9584069730cSRavitheja Addepally   TypeMap types_map;
959ae088e52SGreg Clayton   llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
960*d4d428efSAdrian Prantl   size_t num_types = FindTypes_Impl(type_name, parent_decl_ctx, max_matches,
961b9c1b51eSKate Stone                                     searched_symbol_files, types_map);
962576495e6SZachary Turner   if (num_types > 0) {
963576495e6SZachary Turner     SymbolContext sc;
964576495e6SZachary Turner     sc.module_sp = shared_from_this();
9654069730cSRavitheja Addepally     sc.SortTypeList(types_map, type_list);
966576495e6SZachary Turner   }
9674069730cSRavitheja Addepally   return num_types;
9686f3533fbSEnrico Granata }
9696f3533fbSEnrico Granata 
970b9c1b51eSKate Stone lldb::TypeSP Module::FindFirstType(const SymbolContext &sc,
9710e4c4821SAdrian Prantl                                    ConstString name, bool exact_match) {
972b43165b7SGreg Clayton   TypeList type_list;
973ae088e52SGreg Clayton   llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
974b9c1b51eSKate Stone   const size_t num_matches =
975576495e6SZachary Turner       FindTypes(name, exact_match, 1, searched_symbol_files, type_list);
976b43165b7SGreg Clayton   if (num_matches)
977b43165b7SGreg Clayton     return type_list.GetTypeAtIndex(0);
978b43165b7SGreg Clayton   return TypeSP();
979b43165b7SGreg Clayton }
980b43165b7SGreg Clayton 
981b9c1b51eSKate Stone size_t Module::FindTypes(
9820e4c4821SAdrian Prantl     ConstString name, bool exact_match, size_t max_matches,
983ae088e52SGreg Clayton     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
984b9c1b51eSKate Stone     TypeList &types) {
985c7bece56SGreg Clayton   size_t num_matches = 0;
98684db9105SGreg Clayton   const char *type_name_cstr = name.GetCString();
987556b1611STamas Berghammer   llvm::StringRef type_scope;
988556b1611STamas Berghammer   llvm::StringRef type_basename;
9897bc31332SGreg Clayton   TypeClass type_class = eTypeClassAny;
9904069730cSRavitheja Addepally   TypeMap typesmap;
9911739b7d0SFrederic Riss 
992b9c1b51eSKate Stone   if (Type::GetTypeScopeAndBasename(type_name_cstr, type_scope, type_basename,
993b9c1b51eSKate Stone                                     type_class)) {
99484db9105SGreg Clayton     // Check if "name" starts with "::" which means the qualified type starts
99584db9105SGreg Clayton     // from the root namespace and implies and exact match. The typenames we
99684db9105SGreg Clayton     // get back from clang do not start with "::" so we need to strip this off
997d93c4a33SBruce Mitchener     // in order to get the qualified names to match
998556b1611STamas Berghammer     exact_match = type_scope.consume_front("::");
9996f3533fbSEnrico Granata 
1000556b1611STamas Berghammer     ConstString type_basename_const_str(type_basename);
1001*d4d428efSAdrian Prantl     if (FindTypes_Impl(type_basename_const_str, nullptr, max_matches,
1002576495e6SZachary Turner                        searched_symbol_files, typesmap)) {
1003b9c1b51eSKate Stone       typesmap.RemoveMismatchedTypes(type_scope, type_basename, type_class,
1004b9c1b51eSKate Stone                                      exact_match);
10054069730cSRavitheja Addepally       num_matches = typesmap.GetSize();
10066f3533fbSEnrico Granata     }
1007b9c1b51eSKate Stone   } else {
1008b9c1b51eSKate Stone     // The type is not in a namespace/class scope, just search for it by
1009b9c1b51eSKate Stone     // basename
1010c485f056SGreg Clayton     if (type_class != eTypeClassAny && !type_basename.empty()) {
1011b9c1b51eSKate Stone       // The "type_name_cstr" will have been modified if we have a valid type
101205097246SAdrian Prantl       // class prefix (like "struct", "class", "union", "typedef" etc).
1013*d4d428efSAdrian Prantl       FindTypes_Impl(ConstString(type_basename), nullptr, UINT_MAX,
1014576495e6SZachary Turner                      searched_symbol_files, typesmap);
1015c485f056SGreg Clayton       typesmap.RemoveMismatchedTypes(type_scope, type_basename, type_class,
1016c485f056SGreg Clayton                                      exact_match);
10174069730cSRavitheja Addepally       num_matches = typesmap.GetSize();
1018b9c1b51eSKate Stone     } else {
1019*d4d428efSAdrian Prantl       num_matches = FindTypes_Impl(name, nullptr, UINT_MAX,
1020b9c1b51eSKate Stone                                    searched_symbol_files, typesmap);
1021c485f056SGreg Clayton       if (exact_match) {
1022c485f056SGreg Clayton         std::string name_str(name.AsCString(""));
1023c485f056SGreg Clayton         typesmap.RemoveMismatchedTypes(type_scope, name_str, type_class,
1024c485f056SGreg Clayton                                        exact_match);
10259cd4def1SFrederic Riss         num_matches = typesmap.GetSize();
1026c485f056SGreg Clayton       }
102784db9105SGreg Clayton     }
10287bc31332SGreg Clayton   }
1029576495e6SZachary Turner   if (num_matches > 0) {
1030576495e6SZachary Turner     SymbolContext sc;
1031576495e6SZachary Turner     sc.module_sp = shared_from_this();
10324069730cSRavitheja Addepally     sc.SortTypeList(typesmap, types);
1033576495e6SZachary Turner   }
103484db9105SGreg Clayton   return num_matches;
10356f3533fbSEnrico Granata }
10366f3533fbSEnrico Granata 
1037aa97a89dSAdrian Prantl size_t Module::FindTypes(llvm::ArrayRef<CompilerContext> pattern,
1038*d4d428efSAdrian Prantl                          LanguageSet languages, TypeMap &types) {
1039aa97a89dSAdrian Prantl   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1040aa97a89dSAdrian Prantl   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
1041aa97a89dSAdrian Prantl   if (SymbolFile *symbols = GetSymbolFile())
1042*d4d428efSAdrian Prantl     return symbols->FindTypes(pattern, languages, types);
1043aa97a89dSAdrian Prantl   return 0;
1044aa97a89dSAdrian Prantl }
1045aa97a89dSAdrian Prantl 
1046579d6d1aSPavel Labath SymbolFile *Module::GetSymbolFile(bool can_create, Stream *feedback_strm) {
1047579d6d1aSPavel Labath   if (!m_did_load_symfile.load()) {
104816ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1049579d6d1aSPavel Labath     if (!m_did_load_symfile.load() && can_create) {
105030fdc8d8SChris Lattner       ObjectFile *obj_file = GetObjectFile();
1051b9c1b51eSKate Stone       if (obj_file != nullptr) {
1052f9d16476SPavel Labath         static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1053f9d16476SPavel Labath         Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
1054d5b44036SJonas Devlieghere         m_symfile_up.reset(
1055b9c1b51eSKate Stone             SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
1056579d6d1aSPavel Labath         m_did_load_symfile = true;
105730fdc8d8SChris Lattner       }
105830fdc8d8SChris Lattner     }
105988c05f54SGreg Clayton   }
1060579d6d1aSPavel Labath   return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr;
106123f70e83SPavel Labath }
106223f70e83SPavel Labath 
1063d5d47a35SPavel Labath Symtab *Module::GetSymtab() {
1064d5d47a35SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
1065d5d47a35SPavel Labath     return symbols->GetSymtab();
1066d5d47a35SPavel Labath   return nullptr;
1067d5d47a35SPavel Labath }
1068d5d47a35SPavel Labath 
1069b9c1b51eSKate Stone void Module::SetFileSpecAndObjectName(const FileSpec &file,
10700e4c4821SAdrian Prantl                                       ConstString object_name) {
107105097246SAdrian Prantl   // Container objects whose paths do not specify a file directly can call this
107205097246SAdrian Prantl   // function to correct the file and object names.
107330fdc8d8SChris Lattner   m_file = file;
107446376966SJonas Devlieghere   m_mod_time = FileSystem::Instance().GetModificationTime(file);
107530fdc8d8SChris Lattner   m_object_name = object_name;
107630fdc8d8SChris Lattner }
107730fdc8d8SChris Lattner 
1078b9c1b51eSKate Stone const ArchSpec &Module::GetArchitecture() const { return m_arch; }
107930fdc8d8SChris Lattner 
1080b9c1b51eSKate Stone std::string Module::GetSpecificationDescription() const {
1081b5ad4ec7SGreg Clayton   std::string spec(GetFileSpec().GetPath());
1082b9c1b51eSKate Stone   if (m_object_name) {
1083b5ad4ec7SGreg Clayton     spec += '(';
1084b5ad4ec7SGreg Clayton     spec += m_object_name.GetCString();
1085b5ad4ec7SGreg Clayton     spec += ')';
1086b5ad4ec7SGreg Clayton   }
1087b5ad4ec7SGreg Clayton   return spec;
1088b5ad4ec7SGreg Clayton }
1089b5ad4ec7SGreg Clayton 
1090b9c1b51eSKate Stone void Module::GetDescription(Stream *s, lldb::DescriptionLevel level) {
109116ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1092ceb6b139SCaroline Tice 
1093b9c1b51eSKate Stone   if (level >= eDescriptionLevelFull) {
1094cfd1acedSGreg Clayton     if (m_arch.IsValid())
109564195a2cSGreg Clayton       s->Printf("(%s) ", m_arch.GetArchitectureName());
1096c982b3d6SGreg Clayton   }
1097ceb6b139SCaroline Tice 
1098b9c1b51eSKate Stone   if (level == eDescriptionLevelBrief) {
1099c982b3d6SGreg Clayton     const char *filename = m_file.GetFilename().GetCString();
1100c982b3d6SGreg Clayton     if (filename)
1101c982b3d6SGreg Clayton       s->PutCString(filename);
1102b9c1b51eSKate Stone   } else {
1103cfd1acedSGreg Clayton     char path[PATH_MAX];
1104cfd1acedSGreg Clayton     if (m_file.GetPath(path, sizeof(path)))
1105cfd1acedSGreg Clayton       s->PutCString(path);
1106c982b3d6SGreg Clayton   }
1107cfd1acedSGreg Clayton 
1108cfd1acedSGreg Clayton   const char *object_name = m_object_name.GetCString();
1109cfd1acedSGreg Clayton   if (object_name)
1110cfd1acedSGreg Clayton     s->Printf("(%s)", object_name);
1111ceb6b139SCaroline Tice }
1112ceb6b139SCaroline Tice 
1113b9c1b51eSKate Stone void Module::ReportError(const char *format, ...) {
1114b9c1b51eSKate Stone   if (format && format[0]) {
1115e38a5eddSGreg Clayton     StreamString strm;
1116e38a5eddSGreg Clayton     strm.PutCString("error: ");
1117e38a5eddSGreg Clayton     GetDescription(&strm, lldb::eDescriptionLevelBrief);
11188b35334eSGreg Clayton     strm.PutChar(' ');
1119c982b3d6SGreg Clayton     va_list args;
1120c982b3d6SGreg Clayton     va_start(args, format);
1121e38a5eddSGreg Clayton     strm.PrintfVarArg(format, args);
1122c982b3d6SGreg Clayton     va_end(args);
1123e38a5eddSGreg Clayton 
1124e38a5eddSGreg Clayton     const int format_len = strlen(format);
1125b9c1b51eSKate Stone     if (format_len > 0) {
1126e38a5eddSGreg Clayton       const char last_char = format[format_len - 1];
1127376230c9SRaphael Isemann       if (last_char != '\n' && last_char != '\r')
1128e38a5eddSGreg Clayton         strm.EOL();
1129e38a5eddSGreg Clayton     }
1130c156427dSZachary Turner     Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData());
1131e38a5eddSGreg Clayton   }
1132e38a5eddSGreg Clayton }
1133e38a5eddSGreg Clayton 
1134b9c1b51eSKate Stone bool Module::FileHasChanged() const {
1135c5dac77aSEugene Zelenko   if (!m_file_has_changed)
11361408bf72SPavel Labath     m_file_has_changed =
113746376966SJonas Devlieghere         (FileSystem::Instance().GetModificationTime(m_file) != m_mod_time);
11381d60909eSGreg Clayton   return m_file_has_changed;
11391d60909eSGreg Clayton }
11401d60909eSGreg Clayton 
1141b9c1b51eSKate Stone void Module::ReportErrorIfModifyDetected(const char *format, ...) {
1142b9c1b51eSKate Stone   if (!m_first_file_changed_log) {
1143b9c1b51eSKate Stone     if (FileHasChanged()) {
11441d60909eSGreg Clayton       m_first_file_changed_log = true;
1145b9c1b51eSKate Stone       if (format) {
1146e38a5eddSGreg Clayton         StreamString strm;
1147e38a5eddSGreg Clayton         strm.PutCString("error: the object file ");
1148e38a5eddSGreg Clayton         GetDescription(&strm, lldb::eDescriptionLevelFull);
1149e38a5eddSGreg Clayton         strm.PutCString(" has been modified\n");
1150e38a5eddSGreg Clayton 
1151e38a5eddSGreg Clayton         va_list args;
1152e38a5eddSGreg Clayton         va_start(args, format);
1153e38a5eddSGreg Clayton         strm.PrintfVarArg(format, args);
1154e38a5eddSGreg Clayton         va_end(args);
1155e38a5eddSGreg Clayton 
1156e38a5eddSGreg Clayton         const int format_len = strlen(format);
1157b9c1b51eSKate Stone         if (format_len > 0) {
1158e38a5eddSGreg Clayton           const char last_char = format[format_len - 1];
1159376230c9SRaphael Isemann           if (last_char != '\n' && last_char != '\r')
1160e38a5eddSGreg Clayton             strm.EOL();
1161e38a5eddSGreg Clayton         }
1162b9c1b51eSKate Stone         strm.PutCString("The debug session should be aborted as the original "
1163b9c1b51eSKate Stone                         "debug information has been overwritten.\n");
1164c156427dSZachary Turner         Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData());
1165e38a5eddSGreg Clayton       }
1166e38a5eddSGreg Clayton     }
1167c982b3d6SGreg Clayton   }
11681d60909eSGreg Clayton }
1169c982b3d6SGreg Clayton 
1170b9c1b51eSKate Stone void Module::ReportWarning(const char *format, ...) {
1171b9c1b51eSKate Stone   if (format && format[0]) {
1172e38a5eddSGreg Clayton     StreamString strm;
1173e38a5eddSGreg Clayton     strm.PutCString("warning: ");
11748b35334eSGreg Clayton     GetDescription(&strm, lldb::eDescriptionLevelFull);
11758b35334eSGreg Clayton     strm.PutChar(' ');
1176c982b3d6SGreg Clayton 
1177c982b3d6SGreg Clayton     va_list args;
1178c982b3d6SGreg Clayton     va_start(args, format);
1179e38a5eddSGreg Clayton     strm.PrintfVarArg(format, args);
1180c982b3d6SGreg Clayton     va_end(args);
1181e38a5eddSGreg Clayton 
1182e38a5eddSGreg Clayton     const int format_len = strlen(format);
1183b9c1b51eSKate Stone     if (format_len > 0) {
1184e38a5eddSGreg Clayton       const char last_char = format[format_len - 1];
1185376230c9SRaphael Isemann       if (last_char != '\n' && last_char != '\r')
1186e38a5eddSGreg Clayton         strm.EOL();
1187e38a5eddSGreg Clayton     }
1188c156427dSZachary Turner     Host::SystemLog(Host::eSystemLogWarning, "%s", strm.GetData());
1189e38a5eddSGreg Clayton   }
1190c982b3d6SGreg Clayton }
1191c982b3d6SGreg Clayton 
1192b9c1b51eSKate Stone void Module::LogMessage(Log *log, const char *format, ...) {
1193b9c1b51eSKate Stone   if (log != nullptr) {
1194c982b3d6SGreg Clayton     StreamString log_message;
11958b35334eSGreg Clayton     GetDescription(&log_message, lldb::eDescriptionLevelFull);
1196c982b3d6SGreg Clayton     log_message.PutCString(": ");
1197c982b3d6SGreg Clayton     va_list args;
1198c982b3d6SGreg Clayton     va_start(args, format);
1199c982b3d6SGreg Clayton     log_message.PrintfVarArg(format, args);
1200c982b3d6SGreg Clayton     va_end(args);
1201c156427dSZachary Turner     log->PutCString(log_message.GetData());
1202c982b3d6SGreg Clayton   }
1203c982b3d6SGreg Clayton }
1204c982b3d6SGreg Clayton 
1205b9c1b51eSKate Stone void Module::LogMessageVerboseBacktrace(Log *log, const char *format, ...) {
1206b9c1b51eSKate Stone   if (log != nullptr) {
1207d61c0fc0SGreg Clayton     StreamString log_message;
1208d61c0fc0SGreg Clayton     GetDescription(&log_message, lldb::eDescriptionLevelFull);
1209d61c0fc0SGreg Clayton     log_message.PutCString(": ");
1210d61c0fc0SGreg Clayton     va_list args;
1211d61c0fc0SGreg Clayton     va_start(args, format);
1212d61c0fc0SGreg Clayton     log_message.PrintfVarArg(format, args);
1213d61c0fc0SGreg Clayton     va_end(args);
1214b9c1b51eSKate Stone     if (log->GetVerbose()) {
1215a893d301SZachary Turner       std::string back_trace;
1216a893d301SZachary Turner       llvm::raw_string_ostream stream(back_trace);
1217a893d301SZachary Turner       llvm::sys::PrintStackTrace(stream);
1218771ef6d4SMalcolm Parsons       log_message.PutCString(back_trace);
1219a893d301SZachary Turner     }
1220c156427dSZachary Turner     log->PutCString(log_message.GetData());
1221d61c0fc0SGreg Clayton   }
1222d61c0fc0SGreg Clayton }
1223d61c0fc0SGreg Clayton 
1224b9c1b51eSKate Stone void Module::Dump(Stream *s) {
122516ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
12268941142aSGreg Clayton   // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
122730fdc8d8SChris Lattner   s->Indent();
1228b9c1b51eSKate Stone   s->Printf("Module %s%s%s%s\n", m_file.GetPath().c_str(),
122930fdc8d8SChris Lattner             m_object_name ? "(" : "",
123030fdc8d8SChris Lattner             m_object_name ? m_object_name.GetCString() : "",
123130fdc8d8SChris Lattner             m_object_name ? ")" : "");
123230fdc8d8SChris Lattner 
123330fdc8d8SChris Lattner   s->IndentMore();
123430fdc8d8SChris Lattner 
1235a7499c98SMichael Sartain   ObjectFile *objfile = GetObjectFile();
123630fdc8d8SChris Lattner   if (objfile)
123730fdc8d8SChris Lattner     objfile->Dump(s);
123830fdc8d8SChris Lattner 
1239d5d47a35SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
1240d5d47a35SPavel Labath     symbols->Dump(*s);
124130fdc8d8SChris Lattner 
124230fdc8d8SChris Lattner   s->IndentLess();
124330fdc8d8SChris Lattner }
124430fdc8d8SChris Lattner 
12450e4c4821SAdrian Prantl ConstString Module::GetObjectName() const { return m_object_name; }
124630fdc8d8SChris Lattner 
1247b9c1b51eSKate Stone ObjectFile *Module::GetObjectFile() {
1248b9c1b51eSKate Stone   if (!m_did_load_objfile.load()) {
124916ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1250b9c1b51eSKate Stone     if (!m_did_load_objfile.load()) {
1251f9d16476SPavel Labath       static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1252f9d16476SPavel Labath       Timer scoped_timer(func_cat, "Module::GetObjectFile () module = %s",
1253b9c1b51eSKate Stone                          GetFileSpec().GetFilename().AsCString(""));
12545ce9c565SGreg Clayton       DataBufferSP data_sp;
12555ce9c565SGreg Clayton       lldb::offset_t data_offset = 0;
125659b78bcbSJonas Devlieghere       const lldb::offset_t file_size =
125759b78bcbSJonas Devlieghere           FileSystem::Instance().GetByteSize(m_file);
1258b9c1b51eSKate Stone       if (file_size > m_object_offset) {
12592540a8a7SGreg Clayton         m_did_load_objfile = true;
1260b9c1b51eSKate Stone         m_objfile_sp = ObjectFile::FindPlugin(
1261b9c1b51eSKate Stone             shared_from_this(), &m_file, m_object_offset,
1262b9c1b51eSKate Stone             file_size - m_object_offset, data_sp, data_offset);
1263b9c1b51eSKate Stone         if (m_objfile_sp) {
1264b9c1b51eSKate Stone           // Once we get the object file, update our module with the object
126505097246SAdrian Prantl           // file's architecture since it might differ in vendor/os if some
126605097246SAdrian Prantl           // parts were unknown.  But since the matching arch might already be
126705097246SAdrian Prantl           // more specific than the generic COFF architecture, only merge in
126805097246SAdrian Prantl           // those values that overwrite unspecified unknown values.
1269f760f5aeSPavel Labath           m_arch.MergeFrom(m_objfile_sp->GetArchitecture());
1270b9c1b51eSKate Stone         } else {
1271b9c1b51eSKate Stone           ReportError("failed to load objfile for %s",
1272b9c1b51eSKate Stone                       GetFileSpec().GetPath().c_str());
12730ee56ce6STodd Fiala         }
127430fdc8d8SChris Lattner       }
12752540a8a7SGreg Clayton     }
127688c05f54SGreg Clayton   }
1277762f7135SGreg Clayton   return m_objfile_sp.get();
127830fdc8d8SChris Lattner }
127930fdc8d8SChris Lattner 
1280b9c1b51eSKate Stone SectionList *Module::GetSectionList() {
1281d5b44036SJonas Devlieghere   // Populate m_sections_up with sections from objfile.
1282d5b44036SJonas Devlieghere   if (!m_sections_up) {
12833046e668SGreg Clayton     ObjectFile *obj_file = GetObjectFile();
1284c5dac77aSEugene Zelenko     if (obj_file != nullptr)
12853046e668SGreg Clayton       obj_file->CreateSections(*GetUnifiedSectionList());
12863046e668SGreg Clayton   }
1287d5b44036SJonas Devlieghere   return m_sections_up.get();
12883046e668SGreg Clayton }
12893046e668SGreg Clayton 
1290b9c1b51eSKate Stone void Module::SectionFileAddressesChanged() {
129105a09c67SJason Molenda   ObjectFile *obj_file = GetObjectFile();
129205a09c67SJason Molenda   if (obj_file)
129305a09c67SJason Molenda     obj_file->SectionFileAddressesChanged();
129423f70e83SPavel Labath   if (SymbolFile *symbols = GetSymbolFile())
129523f70e83SPavel Labath     symbols->SectionFileAddressesChanged();
129605a09c67SJason Molenda }
129705a09c67SJason Molenda 
1298dec96392SPavel Labath UnwindTable &Module::GetUnwindTable() {
1299dec96392SPavel Labath   if (!m_unwind_table)
1300dec96392SPavel Labath     m_unwind_table.emplace(*this);
1301dec96392SPavel Labath   return *m_unwind_table;
1302dec96392SPavel Labath }
1303dec96392SPavel Labath 
1304b9c1b51eSKate Stone SectionList *Module::GetUnifiedSectionList() {
1305d5b44036SJonas Devlieghere   if (!m_sections_up)
1306a8f3ae7cSJonas Devlieghere     m_sections_up = std::make_unique<SectionList>();
1307d5b44036SJonas Devlieghere   return m_sections_up.get();
1308a7499c98SMichael Sartain }
130930fdc8d8SChris Lattner 
13100e4c4821SAdrian Prantl const Symbol *Module::FindFirstSymbolWithNameAndType(ConstString name,
1311b9c1b51eSKate Stone                                                      SymbolType symbol_type) {
1312f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1313b9c1b51eSKate Stone   Timer scoped_timer(
1314f9d16476SPavel Labath       func_cat, "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1315b9c1b51eSKate Stone       name.AsCString(), symbol_type);
1316d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab())
1317b9c1b51eSKate Stone     return symtab->FindFirstSymbolWithNameAndType(
1318b9c1b51eSKate Stone         name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
1319c5dac77aSEugene Zelenko   return nullptr;
132030fdc8d8SChris Lattner }
1321b9c1b51eSKate Stone void Module::SymbolIndicesToSymbolContextList(
1322b9c1b51eSKate Stone     Symtab *symtab, std::vector<uint32_t> &symbol_indexes,
1323b9c1b51eSKate Stone     SymbolContextList &sc_list) {
132430fdc8d8SChris Lattner   // No need to protect this call using m_mutex all other method calls are
132530fdc8d8SChris Lattner   // already thread safe.
132630fdc8d8SChris Lattner 
132730fdc8d8SChris Lattner   size_t num_indices = symbol_indexes.size();
1328b9c1b51eSKate Stone   if (num_indices > 0) {
132930fdc8d8SChris Lattner     SymbolContext sc;
133030fdc8d8SChris Lattner     CalculateSymbolContext(&sc);
1331b9c1b51eSKate Stone     for (size_t i = 0; i < num_indices; i++) {
133230fdc8d8SChris Lattner       sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
133330fdc8d8SChris Lattner       if (sc.symbol)
133430fdc8d8SChris Lattner         sc_list.Append(sc);
133530fdc8d8SChris Lattner     }
133630fdc8d8SChris Lattner   }
133730fdc8d8SChris Lattner }
133830fdc8d8SChris Lattner 
13390e4c4821SAdrian Prantl size_t Module::FindFunctionSymbols(ConstString name,
1340c1b2ccfdSGreg Clayton                                    uint32_t name_type_mask,
1341b9c1b51eSKate Stone                                    SymbolContextList &sc_list) {
1342f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1343f9d16476SPavel Labath   Timer scoped_timer(func_cat,
1344c1b2ccfdSGreg Clayton                      "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1345b9c1b51eSKate Stone                      name.AsCString(), name_type_mask);
1346d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab())
1347c1b2ccfdSGreg Clayton     return symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
1348c1b2ccfdSGreg Clayton   return 0;
1349c1b2ccfdSGreg Clayton }
1350c1b2ccfdSGreg Clayton 
13510e4c4821SAdrian Prantl size_t Module::FindSymbolsWithNameAndType(ConstString name,
1352b9c1b51eSKate Stone                                           SymbolType symbol_type,
1353b9c1b51eSKate Stone                                           SymbolContextList &sc_list) {
135430fdc8d8SChris Lattner   // No need to protect this call using m_mutex all other method calls are
135530fdc8d8SChris Lattner   // already thread safe.
135630fdc8d8SChris Lattner 
1357f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1358b9c1b51eSKate Stone   Timer scoped_timer(
1359f9d16476SPavel Labath       func_cat, "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1360b9c1b51eSKate Stone       name.AsCString(), symbol_type);
136130fdc8d8SChris Lattner   const size_t initial_size = sc_list.GetSize();
1362d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab()) {
136330fdc8d8SChris Lattner     std::vector<uint32_t> symbol_indexes;
136430fdc8d8SChris Lattner     symtab->FindAllSymbolsWithNameAndType(name, symbol_type, symbol_indexes);
136530fdc8d8SChris Lattner     SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
136630fdc8d8SChris Lattner   }
136730fdc8d8SChris Lattner   return sc_list.GetSize() - initial_size;
136830fdc8d8SChris Lattner }
136930fdc8d8SChris Lattner 
1370b9c1b51eSKate Stone size_t Module::FindSymbolsMatchingRegExAndType(const RegularExpression &regex,
1371b9c1b51eSKate Stone                                                SymbolType symbol_type,
1372b9c1b51eSKate Stone                                                SymbolContextList &sc_list) {
137330fdc8d8SChris Lattner   // No need to protect this call using m_mutex all other method calls are
137430fdc8d8SChris Lattner   // already thread safe.
137530fdc8d8SChris Lattner 
1376f9d16476SPavel Labath   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1377b9c1b51eSKate Stone   Timer scoped_timer(
1378f9d16476SPavel Labath       func_cat,
137930fdc8d8SChris Lattner       "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
138095eae423SZachary Turner       regex.GetText().str().c_str(), symbol_type);
138130fdc8d8SChris Lattner   const size_t initial_size = sc_list.GetSize();
1382d5d47a35SPavel Labath   if (Symtab *symtab = GetSymtab()) {
138330fdc8d8SChris Lattner     std::vector<uint32_t> symbol_indexes;
1384b9c1b51eSKate Stone     symtab->FindAllSymbolsMatchingRexExAndType(
1385b9c1b51eSKate Stone         regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny,
1386b9c1b51eSKate Stone         symbol_indexes);
138730fdc8d8SChris Lattner     SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
138830fdc8d8SChris Lattner   }
138930fdc8d8SChris Lattner   return sc_list.GetSize() - initial_size;
139030fdc8d8SChris Lattner }
139130fdc8d8SChris Lattner 
13927fca8c07SJim Ingham void Module::PreloadSymbols() {
13937fca8c07SJim Ingham   std::lock_guard<std::recursive_mutex> guard(m_mutex);
139423f70e83SPavel Labath   SymbolFile *sym_file = GetSymbolFile();
139523f70e83SPavel Labath   if (!sym_file)
13967fca8c07SJim Ingham     return;
139723f70e83SPavel Labath 
13987fca8c07SJim Ingham   // Prime the symbol file first, since it adds symbols to the symbol table.
139923f70e83SPavel Labath   sym_file->PreloadSymbols();
140023f70e83SPavel Labath 
14017fca8c07SJim Ingham   // Now we can prime the symbol table.
140223f70e83SPavel Labath   if (Symtab *symtab = sym_file->GetSymtab())
14037fca8c07SJim Ingham     symtab->PreloadSymbols();
14047fca8c07SJim Ingham }
14057fca8c07SJim Ingham 
1406b9c1b51eSKate Stone void Module::SetSymbolFileFileSpec(const FileSpec &file) {
1407dbd7fabaSJonas Devlieghere   if (!FileSystem::Instance().Exists(file))
140890271672SGreg Clayton     return;
1409d5b44036SJonas Devlieghere   if (m_symfile_up) {
1410b9c1b51eSKate Stone     // Remove any sections in the unified section list that come from the
1411b9c1b51eSKate Stone     // current symbol vendor.
14123046e668SGreg Clayton     SectionList *section_list = GetSectionList();
141323f70e83SPavel Labath     SymbolFile *symbol_file = GetSymbolFile();
1414b9c1b51eSKate Stone     if (section_list && symbol_file) {
1415a7499c98SMichael Sartain       ObjectFile *obj_file = symbol_file->GetObjectFile();
1416b9c1b51eSKate Stone       // Make sure we have an object file and that the symbol vendor's objfile
141705097246SAdrian Prantl       // isn't the same as the module's objfile before we remove any sections
141805097246SAdrian Prantl       // for it...
1419b9c1b51eSKate Stone       if (obj_file) {
1420b9c1b51eSKate Stone         // Check to make sure we aren't trying to specify the file we already
1421b9c1b51eSKate Stone         // have
1422b9c1b51eSKate Stone         if (obj_file->GetFileSpec() == file) {
142390271672SGreg Clayton           // We are being told to add the exact same file that we already have
142490271672SGreg Clayton           // we don't have to do anything.
142590271672SGreg Clayton           return;
142690271672SGreg Clayton         }
1427d00438e8STamas Berghammer 
1428b9c1b51eSKate Stone         // Cleare the current symtab as we are going to replace it with a new
1429b9c1b51eSKate Stone         // one
1430d00438e8STamas Berghammer         obj_file->ClearSymtab();
143190271672SGreg Clayton 
1432dec96392SPavel Labath         // Clear the unwind table too, as that may also be affected by the
1433dec96392SPavel Labath         // symbol file information.
1434dec96392SPavel Labath         m_unwind_table.reset();
1435dec96392SPavel Labath 
1436b9c1b51eSKate Stone         // The symbol file might be a directory bundle ("/tmp/a.out.dSYM")
143705097246SAdrian Prantl         // instead of a full path to the symbol file within the bundle
1438b9c1b51eSKate Stone         // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to
1439b9c1b51eSKate Stone         // check this
144090271672SGreg Clayton 
14413a58d898SJonas Devlieghere         if (FileSystem::Instance().IsDirectory(file)) {
144290271672SGreg Clayton           std::string new_path(file.GetPath());
144390271672SGreg Clayton           std::string old_path(obj_file->GetFileSpec().GetPath());
1444b9c1b51eSKate Stone           if (old_path.find(new_path) == 0) {
1445b9c1b51eSKate Stone             // We specified the same bundle as the symbol file that we already
1446b9c1b51eSKate Stone             // have
144790271672SGreg Clayton             return;
144890271672SGreg Clayton           }
144990271672SGreg Clayton         }
145090271672SGreg Clayton 
1451b9c1b51eSKate Stone         if (obj_file != m_objfile_sp.get()) {
1452a7499c98SMichael Sartain           size_t num_sections = section_list->GetNumSections(0);
1453b9c1b51eSKate Stone           for (size_t idx = num_sections; idx > 0; --idx) {
1454b9c1b51eSKate Stone             lldb::SectionSP section_sp(
1455b9c1b51eSKate Stone                 section_list->GetSectionAtIndex(idx - 1));
1456b9c1b51eSKate Stone             if (section_sp->GetObjectFile() == obj_file) {
14573046e668SGreg Clayton               section_list->DeleteSection(idx - 1);
1458a7499c98SMichael Sartain             }
1459a7499c98SMichael Sartain           }
1460a7499c98SMichael Sartain         }
1461a7499c98SMichael Sartain       }
1462a7499c98SMichael Sartain     }
1463b9c1b51eSKate Stone     // Keep all old symbol files around in case there are any lingering type
146405097246SAdrian Prantl     // references in any SBValue objects that might have been handed out.
1465d5b44036SJonas Devlieghere     m_old_symfiles.push_back(std::move(m_symfile_up));
146690271672SGreg Clayton   }
1467e01e07b6SGreg Clayton   m_symfile_spec = file;
1468d5b44036SJonas Devlieghere   m_symfile_up.reset();
1469579d6d1aSPavel Labath   m_did_load_symfile = false;
1470e01e07b6SGreg Clayton }
1471e01e07b6SGreg Clayton 
1472b9c1b51eSKate Stone bool Module::IsExecutable() {
1473c5dac77aSEugene Zelenko   if (GetObjectFile() == nullptr)
14745aee162fSJim Ingham     return false;
14755aee162fSJim Ingham   else
14765aee162fSJim Ingham     return GetObjectFile()->IsExecutable();
14775aee162fSJim Ingham }
14785aee162fSJim Ingham 
1479b9c1b51eSKate Stone bool Module::IsLoadedInTarget(Target *target) {
1480b53cb271SJim Ingham   ObjectFile *obj_file = GetObjectFile();
1481b9c1b51eSKate Stone   if (obj_file) {
14823046e668SGreg Clayton     SectionList *sections = GetSectionList();
1483b9c1b51eSKate Stone     if (sections != nullptr) {
1484b53cb271SJim Ingham       size_t num_sections = sections->GetSize();
1485b9c1b51eSKate Stone       for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++) {
1486b53cb271SJim Ingham         SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1487b9c1b51eSKate Stone         if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) {
1488b53cb271SJim Ingham           return true;
1489b53cb271SJim Ingham         }
1490b53cb271SJim Ingham       }
1491b53cb271SJim Ingham     }
1492b53cb271SJim Ingham   }
1493b53cb271SJim Ingham   return false;
1494b53cb271SJim Ingham }
14951759848bSEnrico Granata 
149697206d57SZachary Turner bool Module::LoadScriptingResourceInTarget(Target *target, Status &error,
1497b9c1b51eSKate Stone                                            Stream *feedback_stream) {
1498b9c1b51eSKate Stone   if (!target) {
14991759848bSEnrico Granata     error.SetErrorString("invalid destination Target");
15001759848bSEnrico Granata     return false;
15011759848bSEnrico Granata   }
15021759848bSEnrico Granata 
1503b9c1b51eSKate Stone   LoadScriptFromSymFile should_load =
1504b9c1b51eSKate Stone       target->TargetProperties::GetLoadScriptFromSymbolFile();
15052ea43cdcSEnrico Granata 
1506994740fbSGreg Clayton   if (should_load == eLoadScriptFromSymFileFalse)
1507994740fbSGreg Clayton     return false;
1508994740fbSGreg Clayton 
150991c0e749SGreg Clayton   Debugger &debugger = target->GetDebugger();
151091c0e749SGreg Clayton   const ScriptLanguage script_language = debugger.GetScriptLanguage();
1511b9c1b51eSKate Stone   if (script_language != eScriptLanguageNone) {
151291c0e749SGreg Clayton 
15131759848bSEnrico Granata     PlatformSP platform_sp(target->GetPlatform());
15141759848bSEnrico Granata 
1515b9c1b51eSKate Stone     if (!platform_sp) {
15161759848bSEnrico Granata       error.SetErrorString("invalid Platform");
15171759848bSEnrico Granata       return false;
15181759848bSEnrico Granata     }
15191759848bSEnrico Granata 
1520b9c1b51eSKate Stone     FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources(
1521b9c1b51eSKate Stone         target, *this, feedback_stream);
152291c0e749SGreg Clayton 
152391c0e749SGreg Clayton     const uint32_t num_specs = file_specs.GetSize();
1524b9c1b51eSKate Stone     if (num_specs) {
15252b29b432SJonas Devlieghere       ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
1526b9c1b51eSKate Stone       if (script_interpreter) {
1527b9c1b51eSKate Stone         for (uint32_t i = 0; i < num_specs; ++i) {
152891c0e749SGreg Clayton           FileSpec scripting_fspec(file_specs.GetFileSpecAtIndex(i));
1529dbd7fabaSJonas Devlieghere           if (scripting_fspec &&
1530dbd7fabaSJonas Devlieghere               FileSystem::Instance().Exists(scripting_fspec)) {
1531b9c1b51eSKate Stone             if (should_load == eLoadScriptFromSymFileWarn) {
1532397ddd5fSEnrico Granata               if (feedback_stream)
1533b9c1b51eSKate Stone                 feedback_stream->Printf(
1534b9c1b51eSKate Stone                     "warning: '%s' contains a debug script. To run this script "
1535b9c1b51eSKate Stone                     "in "
1536b9c1b51eSKate Stone                     "this debug session:\n\n    command script import "
1537b9c1b51eSKate Stone                     "\"%s\"\n\n"
1538d516deb4SJim Ingham                     "To run all discovered debug scripts in this session:\n\n"
1539b9c1b51eSKate Stone                     "    settings set target.load-script-from-symbol-file "
1540b9c1b51eSKate Stone                     "true\n",
1541d516deb4SJim Ingham                     GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1542d516deb4SJim Ingham                     scripting_fspec.GetPath().c_str());
15432ea43cdcSEnrico Granata               return false;
15442ea43cdcSEnrico Granata             }
15451759848bSEnrico Granata             StreamString scripting_stream;
15461759848bSEnrico Granata             scripting_fspec.Dump(&scripting_stream);
1547e0c70f1bSEnrico Granata             const bool can_reload = true;
15486a51085eSJim Ingham             const bool init_lldb_globals = false;
1549b9c1b51eSKate Stone             bool did_load = script_interpreter->LoadScriptingModule(
1550b9c1b51eSKate Stone                 scripting_stream.GetData(), can_reload, init_lldb_globals,
1551d516deb4SJim Ingham                 error);
15521759848bSEnrico Granata             if (!did_load)
15531759848bSEnrico Granata               return false;
15541759848bSEnrico Granata           }
155591c0e749SGreg Clayton         }
1556b9c1b51eSKate Stone       } else {
15571759848bSEnrico Granata         error.SetErrorString("invalid ScriptInterpreter");
15581759848bSEnrico Granata         return false;
15591759848bSEnrico Granata       }
15601759848bSEnrico Granata     }
1561b9d8890bSGreg Clayton   }
15621759848bSEnrico Granata   return true;
15631759848bSEnrico Granata }
15641759848bSEnrico Granata 
1565b9c1b51eSKate Stone bool Module::SetArchitecture(const ArchSpec &new_arch) {
1566b9c1b51eSKate Stone   if (!m_arch.IsValid()) {
15675aee162fSJim Ingham     m_arch = new_arch;
15685aee162fSJim Ingham     return true;
15695aee162fSJim Ingham   }
1570b6cd5fe9SChaoren Lin   return m_arch.IsCompatibleMatch(new_arch);
15715aee162fSJim Ingham }
15725aee162fSJim Ingham 
1573b9c1b51eSKate Stone bool Module::SetLoadAddress(Target &target, lldb::addr_t value,
1574b9c1b51eSKate Stone                             bool value_is_offset, bool &changed) {
15759e02dacdSSteve Pucci   ObjectFile *object_file = GetObjectFile();
1576b9c1b51eSKate Stone   if (object_file != nullptr) {
1577751caf65SGreg Clayton     changed = object_file->SetLoadAddress(target, value, value_is_offset);
15787524e090SGreg Clayton     return true;
1579b9c1b51eSKate Stone   } else {
15807524e090SGreg Clayton     changed = false;
1581c9660546SGreg Clayton   }
15829e02dacdSSteve Pucci   return false;
1583c9660546SGreg Clayton }
1584c9660546SGreg Clayton 
1585b9c1b51eSKate Stone bool Module::MatchesModuleSpec(const ModuleSpec &module_ref) {
1586b9a01b39SGreg Clayton   const UUID &uuid = module_ref.GetUUID();
1587b9a01b39SGreg Clayton 
1588b9c1b51eSKate Stone   if (uuid.IsValid()) {
1589b9a01b39SGreg Clayton     // If the UUID matches, then nothing more needs to match...
1590c5dac77aSEugene Zelenko     return (uuid == GetUUID());
1591b9a01b39SGreg Clayton   }
1592b9a01b39SGreg Clayton 
1593b9a01b39SGreg Clayton   const FileSpec &file_spec = module_ref.GetFileSpec();
1594b9c1b51eSKate Stone   if (file_spec) {
1595980662eeSTamas Berghammer     if (!FileSpec::Equal(file_spec, m_file, (bool)file_spec.GetDirectory()) &&
1596b9c1b51eSKate Stone         !FileSpec::Equal(file_spec, m_platform_file,
1597b9c1b51eSKate Stone                          (bool)file_spec.GetDirectory()))
1598b9a01b39SGreg Clayton       return false;
1599b9a01b39SGreg Clayton   }
1600b9a01b39SGreg Clayton 
1601b9a01b39SGreg Clayton   const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1602b9c1b51eSKate Stone   if (platform_file_spec) {
1603b9c1b51eSKate Stone     if (!FileSpec::Equal(platform_file_spec, GetPlatformFileSpec(),
1604b9c1b51eSKate Stone                          (bool)platform_file_spec.GetDirectory()))
1605b9a01b39SGreg Clayton       return false;
1606b9a01b39SGreg Clayton   }
1607b9a01b39SGreg Clayton 
1608b9a01b39SGreg Clayton   const ArchSpec &arch = module_ref.GetArchitecture();
1609b9c1b51eSKate Stone   if (arch.IsValid()) {
1610bf4b7be6SSean Callanan     if (!m_arch.IsCompatibleMatch(arch))
1611b9a01b39SGreg Clayton       return false;
1612b9a01b39SGreg Clayton   }
1613b9a01b39SGreg Clayton 
16140e4c4821SAdrian Prantl   ConstString object_name = module_ref.GetObjectName();
1615b9c1b51eSKate Stone   if (object_name) {
1616b9a01b39SGreg Clayton     if (object_name != GetObjectName())
1617b9a01b39SGreg Clayton       return false;
1618b9a01b39SGreg Clayton   }
1619b9a01b39SGreg Clayton   return true;
1620b9a01b39SGreg Clayton }
1621b9a01b39SGreg Clayton 
1622b9c1b51eSKate Stone bool Module::FindSourceFile(const FileSpec &orig_spec,
1623b9c1b51eSKate Stone                             FileSpec &new_spec) const {
162416ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1625d804d285SGreg Clayton   return m_source_mappings.FindFile(orig_spec, new_spec);
1626d804d285SGreg Clayton }
1627d804d285SGreg Clayton 
1628a498f0ecSZachary Turner bool Module::RemapSourceFile(llvm::StringRef path,
1629a498f0ecSZachary Turner                              std::string &new_path) const {
163016ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1631f9be6933SGreg Clayton   return m_source_mappings.RemapPath(path, new_path);
1632f9be6933SGreg Clayton }
1633f9be6933SGreg Clayton 
163424610611SAdrian Prantl bool Module::MergeArchitecture(const ArchSpec &arch_spec) {
163524610611SAdrian Prantl   if (!arch_spec.IsValid())
163624610611SAdrian Prantl     return false;
163724610611SAdrian Prantl   LLDB_LOG(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_MODULES),
163824610611SAdrian Prantl            "module has arch %s, merging/replacing with arch %s",
163924610611SAdrian Prantl            m_arch.GetTriple().getTriple().c_str(),
164024610611SAdrian Prantl            arch_spec.GetTriple().getTriple().c_str());
164124610611SAdrian Prantl   if (!m_arch.IsCompatibleMatch(arch_spec)) {
164224610611SAdrian Prantl     // The new architecture is different, we just need to replace it.
164324610611SAdrian Prantl     return SetArchitecture(arch_spec);
164424610611SAdrian Prantl   }
164524610611SAdrian Prantl 
164624610611SAdrian Prantl   // Merge bits from arch_spec into "merged_arch" and set our architecture.
164724610611SAdrian Prantl   ArchSpec merged_arch(m_arch);
164824610611SAdrian Prantl   merged_arch.MergeFrom(arch_spec);
164924610611SAdrian Prantl   // SetArchitecture() is a no-op if m_arch is already valid.
165024610611SAdrian Prantl   m_arch = ArchSpec();
165124610611SAdrian Prantl   return SetArchitecture(merged_arch);
165224610611SAdrian Prantl }
165324610611SAdrian Prantl 
16542272c481SPavel Labath llvm::VersionTuple Module::GetVersion() {
16552272c481SPavel Labath   if (ObjectFile *obj_file = GetObjectFile())
16562272c481SPavel Labath     return obj_file->GetVersion();
16572272c481SPavel Labath   return llvm::VersionTuple();
16583467d80bSEnrico Granata }
165943fe217bSGreg Clayton 
1660b9c1b51eSKate Stone bool Module::GetIsDynamicLinkEditor() {
166108928f30SGreg Clayton   ObjectFile *obj_file = GetObjectFile();
166208928f30SGreg Clayton 
166308928f30SGreg Clayton   if (obj_file)
166408928f30SGreg Clayton     return obj_file->GetIsDynamicLinkEditor();
166508928f30SGreg Clayton 
166608928f30SGreg Clayton   return false;
166708928f30SGreg Clayton }
1668