130fdc8d8SChris Lattner //===-- Module.cpp ----------------------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
10c5dac77aSEugene Zelenko #include "lldb/Core/Module.h"
11c5dac77aSEugene Zelenko 
12c5dac77aSEugene Zelenko // C Includes
13c5dac77aSEugene Zelenko // C++ Includes
14c5dac77aSEugene Zelenko // Other libraries and framework includes
15c5dac77aSEugene Zelenko #include "llvm/Support/raw_os_ostream.h"
16c5dac77aSEugene Zelenko #include "llvm/Support/Signals.h"
17c5dac77aSEugene Zelenko 
18c5dac77aSEugene Zelenko // Project includes
19f86248d9SRichard Mitton #include "lldb/Core/AddressResolverFileLine.h"
201759848bSEnrico Granata #include "lldb/Core/Error.h"
21c9660546SGreg Clayton #include "lldb/Core/DataBuffer.h"
22c9660546SGreg Clayton #include "lldb/Core/DataBufferHeap.h"
2330fdc8d8SChris Lattner #include "lldb/Core/Log.h"
2430fdc8d8SChris Lattner #include "lldb/Core/ModuleList.h"
251f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
2656939cb3SGreg Clayton #include "lldb/Core/PluginManager.h"
2730fdc8d8SChris Lattner #include "lldb/Core/RegularExpression.h"
281f746071SGreg Clayton #include "lldb/Core/Section.h"
29c982b3d6SGreg Clayton #include "lldb/Core/StreamString.h"
3030fdc8d8SChris Lattner #include "lldb/Core/Timer.h"
31e38a5eddSGreg Clayton #include "lldb/Host/Host.h"
321759848bSEnrico Granata #include "lldb/Host/Symbols.h"
331759848bSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h"
341759848bSEnrico Granata #include "lldb/Interpreter/ScriptInterpreter.h"
351f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
3630fdc8d8SChris Lattner #include "lldb/Symbol/ObjectFile.h"
3730fdc8d8SChris Lattner #include "lldb/Symbol/SymbolContext.h"
3856939cb3SGreg Clayton #include "lldb/Symbol/SymbolFile.h"
3930fdc8d8SChris Lattner #include "lldb/Symbol/SymbolVendor.h"
4056939cb3SGreg Clayton #include "lldb/Symbol/TypeSystem.h"
410e0984eeSJim Ingham #include "lldb/Target/Language.h"
42c9660546SGreg Clayton #include "lldb/Target/Process.h"
43d5944cd1SGreg Clayton #include "lldb/Target/SectionLoadList.h"
44c9660546SGreg Clayton #include "lldb/Target/Target.h"
45aa816b8fSJim Ingham #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
46aa816b8fSJim Ingham #include "Plugins/Language/ObjC/ObjCLanguage.h"
474069730cSRavitheja Addepally #include "lldb/Symbol/TypeMap.h"
4830fdc8d8SChris Lattner 
4923f8c95aSGreg Clayton #include "Plugins/ObjectFile/JIT/ObjectFileJIT.h"
5023f8c95aSGreg Clayton 
5130fdc8d8SChris Lattner using namespace lldb;
5230fdc8d8SChris Lattner using namespace lldb_private;
5330fdc8d8SChris Lattner 
5465a03991SGreg Clayton // Shared pointers to modules track module lifetimes in
5565a03991SGreg Clayton // targets and in the global module, but this collection
5665a03991SGreg Clayton // will track all module objects that are still alive
5765a03991SGreg Clayton typedef std::vector<Module *> ModuleCollection;
5865a03991SGreg Clayton 
5965a03991SGreg Clayton static ModuleCollection &
6065a03991SGreg Clayton GetModuleCollection()
6165a03991SGreg Clayton {
62549f7374SJim Ingham     // This module collection needs to live past any module, so we could either make it a
63549f7374SJim Ingham     // shared pointer in each module or just leak is.  Since it is only an empty vector by
64549f7374SJim Ingham     // the time all the modules have gone away, we just leak it for now.  If we decide this
65549f7374SJim Ingham     // is a big problem we can introduce a Finalize method that will tear everything down in
66549f7374SJim Ingham     // a predictable order.
67549f7374SJim Ingham 
68c5dac77aSEugene Zelenko     static ModuleCollection *g_module_collection = nullptr;
69c5dac77aSEugene Zelenko     if (g_module_collection == nullptr)
70549f7374SJim Ingham         g_module_collection = new ModuleCollection();
71549f7374SJim Ingham 
72549f7374SJim Ingham     return *g_module_collection;
7365a03991SGreg Clayton }
7465a03991SGreg Clayton 
7516ff8604SSaleem Abdulrasool std::recursive_mutex &
7665a03991SGreg Clayton Module::GetAllocationModuleCollectionMutex()
7765a03991SGreg Clayton {
78b26e6bebSGreg Clayton     // NOTE: The mutex below must be leaked since the global module list in
79b26e6bebSGreg Clayton     // the ModuleList class will get torn at some point, and we can't know
80b26e6bebSGreg Clayton     // if it will tear itself down before the "g_module_collection_mutex" below
81b26e6bebSGreg Clayton     // will. So we leak a Mutex object below to safeguard against that
82b26e6bebSGreg Clayton 
8316ff8604SSaleem Abdulrasool     static std::recursive_mutex *g_module_collection_mutex = nullptr;
84c5dac77aSEugene Zelenko     if (g_module_collection_mutex == nullptr)
8516ff8604SSaleem Abdulrasool         g_module_collection_mutex = new std::recursive_mutex; // NOTE: known leak
8616ff8604SSaleem Abdulrasool     return *g_module_collection_mutex;
8765a03991SGreg Clayton }
8865a03991SGreg Clayton 
8965a03991SGreg Clayton size_t
9065a03991SGreg Clayton Module::GetNumberAllocatedModules ()
9165a03991SGreg Clayton {
9216ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetAllocationModuleCollectionMutex());
9365a03991SGreg Clayton     return GetModuleCollection().size();
9465a03991SGreg Clayton }
9565a03991SGreg Clayton 
9665a03991SGreg Clayton Module *
9765a03991SGreg Clayton Module::GetAllocatedModuleAtIndex (size_t idx)
9865a03991SGreg Clayton {
9916ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetAllocationModuleCollectionMutex());
10065a03991SGreg Clayton     ModuleCollection &modules = GetModuleCollection();
10165a03991SGreg Clayton     if (idx < modules.size())
10265a03991SGreg Clayton         return modules[idx];
103c5dac77aSEugene Zelenko     return nullptr;
10465a03991SGreg Clayton }
10565a03991SGreg Clayton 
106c5dac77aSEugene Zelenko #if 0
10729ad7b91SGreg Clayton // These functions help us to determine if modules are still loaded, yet don't require that
10829ad7b91SGreg Clayton // you have a command interpreter and can easily be called from an external debugger.
10929ad7b91SGreg Clayton namespace lldb {
11065a03991SGreg Clayton 
11129ad7b91SGreg Clayton     void
11229ad7b91SGreg Clayton     ClearModuleInfo (void)
11329ad7b91SGreg Clayton     {
1140cd70866SGreg Clayton         const bool mandatory = true;
1150cd70866SGreg Clayton         ModuleList::RemoveOrphanSharedModules(mandatory);
11629ad7b91SGreg Clayton     }
11729ad7b91SGreg Clayton 
11829ad7b91SGreg Clayton     void
11929ad7b91SGreg Clayton     DumpModuleInfo (void)
12029ad7b91SGreg Clayton     {
12129ad7b91SGreg Clayton         Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
12229ad7b91SGreg Clayton         ModuleCollection &modules = GetModuleCollection();
12329ad7b91SGreg Clayton         const size_t count = modules.size();
124*f343968fSZachary Turner         printf ("%s: %" PRIu64 " modules:\n", LLVM_PRETTY_FUNCTION, (uint64_t)count);
12529ad7b91SGreg Clayton         for (size_t i = 0; i < count; ++i)
12629ad7b91SGreg Clayton         {
12729ad7b91SGreg Clayton 
12829ad7b91SGreg Clayton             StreamString strm;
12929ad7b91SGreg Clayton             Module *module = modules[i];
13029ad7b91SGreg Clayton             const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
13129ad7b91SGreg Clayton             module->GetDescription(&strm, eDescriptionLevelFull);
13229ad7b91SGreg Clayton             printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
13329ad7b91SGreg Clayton                     module,
13429ad7b91SGreg Clayton                     in_shared_module_list,
13529ad7b91SGreg Clayton                     (uint32_t)module->use_count(),
13629ad7b91SGreg Clayton                     strm.GetString().c_str());
13729ad7b91SGreg Clayton         }
13829ad7b91SGreg Clayton     }
13929ad7b91SGreg Clayton }
14029ad7b91SGreg Clayton 
14129ad7b91SGreg Clayton #endif
14265a03991SGreg Clayton 
14316ff8604SSaleem Abdulrasool Module::Module(const ModuleSpec &module_spec)
14416ff8604SSaleem Abdulrasool     : m_mutex(),
14534f1159bSGreg Clayton       m_mod_time(),
14634f1159bSGreg Clayton       m_arch(),
147b9a01b39SGreg Clayton       m_uuid(),
14834f1159bSGreg Clayton       m_file(),
14934f1159bSGreg Clayton       m_platform_file(),
150fbb76349SGreg Clayton       m_remote_install_file(),
15134f1159bSGreg Clayton       m_symfile_spec(),
15234f1159bSGreg Clayton       m_object_name(),
15334f1159bSGreg Clayton       m_object_offset(),
15434f1159bSGreg Clayton       m_object_mod_time(),
155b9a01b39SGreg Clayton       m_objfile_sp(),
156b9a01b39SGreg Clayton       m_symfile_ap(),
15756939cb3SGreg Clayton       m_type_system_map(),
158d804d285SGreg Clayton       m_source_mappings(),
15923f8c95aSGreg Clayton       m_sections_ap(),
160b9a01b39SGreg Clayton       m_did_load_objfile(false),
161b9a01b39SGreg Clayton       m_did_load_symbol_vendor(false),
162b9a01b39SGreg Clayton       m_did_parse_uuid(false),
1631d60909eSGreg Clayton       m_file_has_changed(false),
1641d60909eSGreg Clayton       m_first_file_changed_log(false)
165b9a01b39SGreg Clayton {
166b9a01b39SGreg Clayton     // Scope for locker below...
167b9a01b39SGreg Clayton     {
16816ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(GetAllocationModuleCollectionMutex());
169b9a01b39SGreg Clayton         GetModuleCollection().push_back(this);
170b9a01b39SGreg Clayton     }
171b9a01b39SGreg Clayton 
1725160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_MODULES));
173c5dac77aSEugene Zelenko     if (log != nullptr)
17416ff8604SSaleem Abdulrasool         log->Printf("%p Module::Module((%s) '%s%s%s%s')", static_cast<void *>(this),
17516ff8604SSaleem Abdulrasool                     module_spec.GetArchitecture().GetArchitectureName(), module_spec.GetFileSpec().GetPath().c_str(),
17634f1159bSGreg Clayton                     module_spec.GetObjectName().IsEmpty() ? "" : "(",
17734f1159bSGreg Clayton                     module_spec.GetObjectName().IsEmpty() ? "" : module_spec.GetObjectName().AsCString(""),
17834f1159bSGreg Clayton                     module_spec.GetObjectName().IsEmpty() ? "" : ")");
17934f1159bSGreg Clayton 
18034f1159bSGreg Clayton     // First extract all module specifications from the file using the local
18134f1159bSGreg Clayton     // file path. If there are no specifications, then don't fill anything in
18234f1159bSGreg Clayton     ModuleSpecList modules_specs;
18334f1159bSGreg Clayton     if (ObjectFile::GetModuleSpecifications(module_spec.GetFileSpec(), 0, 0, modules_specs) == 0)
18434f1159bSGreg Clayton         return;
18534f1159bSGreg Clayton 
18634f1159bSGreg Clayton     // Now make sure that one of the module specifications matches what we just
18734f1159bSGreg Clayton     // extract. We might have a module specification that specifies a file "/usr/lib/dyld"
18834f1159bSGreg Clayton     // with UUID XXX, but we might have a local version of "/usr/lib/dyld" that has
18934f1159bSGreg Clayton     // UUID YYY and we don't want those to match. If they don't match, just don't
19034f1159bSGreg Clayton     // fill any ivars in so we don't accidentally grab the wrong file later since
19134f1159bSGreg Clayton     // they don't match...
19234f1159bSGreg Clayton     ModuleSpec matching_module_spec;
19334f1159bSGreg Clayton     if (modules_specs.FindMatchingModuleSpec(module_spec, matching_module_spec) == 0)
19434f1159bSGreg Clayton         return;
1957ab7f89aSGreg Clayton 
1967ab7f89aSGreg Clayton     if (module_spec.GetFileSpec())
19734f1159bSGreg Clayton         m_mod_time = module_spec.GetFileSpec().GetModificationTime();
1987ab7f89aSGreg Clayton     else if (matching_module_spec.GetFileSpec())
1997ab7f89aSGreg Clayton         m_mod_time = matching_module_spec.GetFileSpec().GetModificationTime();
2007ab7f89aSGreg Clayton 
2017ab7f89aSGreg Clayton     // Copy the architecture from the actual spec if we got one back, else use the one that was specified
2027ab7f89aSGreg Clayton     if (matching_module_spec.GetArchitecture().IsValid())
20334f1159bSGreg Clayton         m_arch = matching_module_spec.GetArchitecture();
2047ab7f89aSGreg Clayton     else if (module_spec.GetArchitecture().IsValid())
2057ab7f89aSGreg Clayton         m_arch = module_spec.GetArchitecture();
2067ab7f89aSGreg Clayton 
207d93c4a33SBruce Mitchener     // Copy the file spec over and use the specified one (if there was one) so we
2087ab7f89aSGreg Clayton     // don't use a path that might have gotten resolved a path in 'matching_module_spec'
2097ab7f89aSGreg Clayton     if (module_spec.GetFileSpec())
21034f1159bSGreg Clayton         m_file = module_spec.GetFileSpec();
2117ab7f89aSGreg Clayton     else if (matching_module_spec.GetFileSpec())
2127ab7f89aSGreg Clayton         m_file = matching_module_spec.GetFileSpec();
2137ab7f89aSGreg Clayton 
2147ab7f89aSGreg Clayton     // Copy the platform file spec over
2157ab7f89aSGreg Clayton     if (module_spec.GetPlatformFileSpec())
21634f1159bSGreg Clayton         m_platform_file = module_spec.GetPlatformFileSpec();
2177ab7f89aSGreg Clayton     else if (matching_module_spec.GetPlatformFileSpec())
2187ab7f89aSGreg Clayton         m_platform_file = matching_module_spec.GetPlatformFileSpec();
2197ab7f89aSGreg Clayton 
2207ab7f89aSGreg Clayton     // Copy the symbol file spec over
2217ab7f89aSGreg Clayton     if (module_spec.GetSymbolFileSpec())
22234f1159bSGreg Clayton         m_symfile_spec = module_spec.GetSymbolFileSpec();
2237ab7f89aSGreg Clayton     else if (matching_module_spec.GetSymbolFileSpec())
2247ab7f89aSGreg Clayton         m_symfile_spec = matching_module_spec.GetSymbolFileSpec();
2257ab7f89aSGreg Clayton 
2267ab7f89aSGreg Clayton     // Copy the object name over
2277ab7f89aSGreg Clayton     if (matching_module_spec.GetObjectName())
2287ab7f89aSGreg Clayton         m_object_name = matching_module_spec.GetObjectName();
2297ab7f89aSGreg Clayton     else
23034f1159bSGreg Clayton         m_object_name = module_spec.GetObjectName();
2317ab7f89aSGreg Clayton 
2327ab7f89aSGreg Clayton     // Always trust the object offset (file offset) and object modification
2337ab7f89aSGreg Clayton     // time (for mod time in a BSD static archive) of from the matching
2347ab7f89aSGreg Clayton     // module specification
23536d7c894SGreg Clayton     m_object_offset = matching_module_spec.GetObjectOffset();
23636d7c894SGreg Clayton     m_object_mod_time = matching_module_spec.GetObjectModificationTime();
237b9a01b39SGreg Clayton }
238b9a01b39SGreg Clayton 
23916ff8604SSaleem Abdulrasool Module::Module(const FileSpec &file_spec, const ArchSpec &arch, const ConstString *object_name,
24016ff8604SSaleem Abdulrasool                lldb::offset_t object_offset, const TimeValue *object_mod_time_ptr)
24116ff8604SSaleem Abdulrasool     : m_mutex(),
24230fdc8d8SChris Lattner       m_mod_time(file_spec.GetModificationTime()),
24330fdc8d8SChris Lattner       m_arch(arch),
24430fdc8d8SChris Lattner       m_uuid(),
24530fdc8d8SChris Lattner       m_file(file_spec),
24632e0a750SGreg Clayton       m_platform_file(),
247fbb76349SGreg Clayton       m_remote_install_file(),
248e72dfb32SGreg Clayton       m_symfile_spec(),
24930fdc8d8SChris Lattner       m_object_name(),
2508b82f087SGreg Clayton       m_object_offset(object_offset),
25157abc5d6SGreg Clayton       m_object_mod_time(),
252762f7135SGreg Clayton       m_objfile_sp(),
253e83e731eSGreg Clayton       m_symfile_ap(),
25456939cb3SGreg Clayton       m_type_system_map(),
255d804d285SGreg Clayton       m_source_mappings(),
25623f8c95aSGreg Clayton       m_sections_ap(),
257e83e731eSGreg Clayton       m_did_load_objfile(false),
258e83e731eSGreg Clayton       m_did_load_symbol_vendor(false),
259e83e731eSGreg Clayton       m_did_parse_uuid(false),
2601d60909eSGreg Clayton       m_file_has_changed(false),
2611d60909eSGreg Clayton       m_first_file_changed_log(false)
26230fdc8d8SChris Lattner {
26365a03991SGreg Clayton     // Scope for locker below...
26465a03991SGreg Clayton     {
26516ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(GetAllocationModuleCollectionMutex());
26665a03991SGreg Clayton         GetModuleCollection().push_back(this);
26765a03991SGreg Clayton     }
26865a03991SGreg Clayton 
26930fdc8d8SChris Lattner     if (object_name)
27030fdc8d8SChris Lattner         m_object_name = *object_name;
27157abc5d6SGreg Clayton 
27257abc5d6SGreg Clayton     if (object_mod_time_ptr)
27357abc5d6SGreg Clayton         m_object_mod_time = *object_mod_time_ptr;
27457abc5d6SGreg Clayton 
2755160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_MODULES));
276c5dac77aSEugene Zelenko     if (log != nullptr)
27716ff8604SSaleem Abdulrasool         log->Printf("%p Module::Module((%s) '%s%s%s%s')", static_cast<void *>(this), m_arch.GetArchitectureName(),
27816ff8604SSaleem Abdulrasool                     m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(",
27916ff8604SSaleem Abdulrasool                     m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), m_object_name.IsEmpty() ? "" : ")");
28030fdc8d8SChris Lattner }
28130fdc8d8SChris Lattner 
28216ff8604SSaleem Abdulrasool Module::Module()
28316ff8604SSaleem Abdulrasool     : m_mutex(),
28423f8c95aSGreg Clayton       m_mod_time(),
28523f8c95aSGreg Clayton       m_arch(),
28623f8c95aSGreg Clayton       m_uuid(),
28723f8c95aSGreg Clayton       m_file(),
28823f8c95aSGreg Clayton       m_platform_file(),
28923f8c95aSGreg Clayton       m_remote_install_file(),
29023f8c95aSGreg Clayton       m_symfile_spec(),
29123f8c95aSGreg Clayton       m_object_name(),
29223f8c95aSGreg Clayton       m_object_offset(0),
29323f8c95aSGreg Clayton       m_object_mod_time(),
29423f8c95aSGreg Clayton       m_objfile_sp(),
29523f8c95aSGreg Clayton       m_symfile_ap(),
29656939cb3SGreg Clayton       m_type_system_map(),
29723f8c95aSGreg Clayton       m_source_mappings(),
29823f8c95aSGreg Clayton       m_sections_ap(),
29923f8c95aSGreg Clayton       m_did_load_objfile(false),
30023f8c95aSGreg Clayton       m_did_load_symbol_vendor(false),
30123f8c95aSGreg Clayton       m_did_parse_uuid(false),
30223f8c95aSGreg Clayton       m_file_has_changed(false),
30323f8c95aSGreg Clayton       m_first_file_changed_log(false)
30423f8c95aSGreg Clayton {
30516ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetAllocationModuleCollectionMutex());
30623f8c95aSGreg Clayton     GetModuleCollection().push_back(this);
30723f8c95aSGreg Clayton }
30823f8c95aSGreg Clayton 
30930fdc8d8SChris Lattner Module::~Module()
31030fdc8d8SChris Lattner {
311217b28baSGreg Clayton     // Lock our module down while we tear everything down to make sure
312217b28baSGreg Clayton     // we don't get any access to the module while it is being destroyed
31316ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
31465a03991SGreg Clayton     // Scope for locker below...
31565a03991SGreg Clayton     {
31616ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(GetAllocationModuleCollectionMutex());
31765a03991SGreg Clayton         ModuleCollection &modules = GetModuleCollection();
31865a03991SGreg Clayton         ModuleCollection::iterator end = modules.end();
31965a03991SGreg Clayton         ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
3203a18e319SGreg Clayton         assert (pos != end);
32165a03991SGreg Clayton         modules.erase(pos);
32265a03991SGreg Clayton     }
3235160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
324c5dac77aSEugene Zelenko     if (log != nullptr)
325b5ad4ec7SGreg Clayton         log->Printf ("%p Module::~Module((%s) '%s%s%s%s')",
326324a1036SSaleem Abdulrasool                      static_cast<void*>(this),
32764195a2cSGreg Clayton                      m_arch.GetArchitectureName(),
328b5ad4ec7SGreg Clayton                      m_file.GetPath().c_str(),
32930fdc8d8SChris Lattner                      m_object_name.IsEmpty() ? "" : "(",
33030fdc8d8SChris Lattner                      m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
33130fdc8d8SChris Lattner                      m_object_name.IsEmpty() ? "" : ")");
3326beaaa68SGreg Clayton     // Release any auto pointers before we start tearing down our member
3336beaaa68SGreg Clayton     // variables since the object file and symbol files might need to make
3346beaaa68SGreg Clayton     // function calls back into this module object. The ordering is important
3356beaaa68SGreg Clayton     // here because symbol files can require the module object file. So we tear
3366beaaa68SGreg Clayton     // down the symbol file first, then the object file.
3373046e668SGreg Clayton     m_sections_ap.reset();
3386beaaa68SGreg Clayton     m_symfile_ap.reset();
339762f7135SGreg Clayton     m_objfile_sp.reset();
34030fdc8d8SChris Lattner }
34130fdc8d8SChris Lattner 
342c7f09ccaSGreg Clayton ObjectFile *
34317220c18SAndrew MacPherson Module::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error, size_t size_to_read)
344c7f09ccaSGreg Clayton {
345c7f09ccaSGreg Clayton     if (m_objfile_sp)
346c7f09ccaSGreg Clayton     {
347c7f09ccaSGreg Clayton         error.SetErrorString ("object file already exists");
348c7f09ccaSGreg Clayton     }
349c7f09ccaSGreg Clayton     else
350c7f09ccaSGreg Clayton     {
35116ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(m_mutex);
352c7f09ccaSGreg Clayton         if (process_sp)
353c7f09ccaSGreg Clayton         {
354c7f09ccaSGreg Clayton             m_did_load_objfile = true;
35517220c18SAndrew MacPherson             std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (size_to_read, 0));
356c7f09ccaSGreg Clayton             Error readmem_error;
357c7f09ccaSGreg Clayton             const size_t bytes_read = process_sp->ReadMemory (header_addr,
358c7f09ccaSGreg Clayton                                                               data_ap->GetBytes(),
359c7f09ccaSGreg Clayton                                                               data_ap->GetByteSize(),
360c7f09ccaSGreg Clayton                                                               readmem_error);
36117220c18SAndrew MacPherson             if (bytes_read == size_to_read)
362c7f09ccaSGreg Clayton             {
363c7f09ccaSGreg Clayton                 DataBufferSP data_sp(data_ap.release());
364c7f09ccaSGreg Clayton                 m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
365c7f09ccaSGreg Clayton                 if (m_objfile_sp)
366c7f09ccaSGreg Clayton                 {
3673e10cf3bSGreg Clayton                     StreamString s;
368d01b2953SDaniel Malea                     s.Printf("0x%16.16" PRIx64, header_addr);
3693e10cf3bSGreg Clayton                     m_object_name.SetCString (s.GetData());
3703e10cf3bSGreg Clayton 
371c7f09ccaSGreg Clayton                     // Once we get the object file, update our module with the object file's
372c7f09ccaSGreg Clayton                     // architecture since it might differ in vendor/os if some parts were
373c7f09ccaSGreg Clayton                     // unknown.
374c7f09ccaSGreg Clayton                     m_objfile_sp->GetArchitecture (m_arch);
375c7f09ccaSGreg Clayton                 }
376c7f09ccaSGreg Clayton                 else
377c7f09ccaSGreg Clayton                 {
378c7f09ccaSGreg Clayton                     error.SetErrorString ("unable to find suitable object file plug-in");
379c7f09ccaSGreg Clayton                 }
380c7f09ccaSGreg Clayton             }
381c7f09ccaSGreg Clayton             else
382c7f09ccaSGreg Clayton             {
383c7f09ccaSGreg Clayton                 error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
384c7f09ccaSGreg Clayton             }
385c7f09ccaSGreg Clayton         }
386c7f09ccaSGreg Clayton         else
387c7f09ccaSGreg Clayton         {
388c7f09ccaSGreg Clayton             error.SetErrorString ("invalid process");
389c7f09ccaSGreg Clayton         }
390c7f09ccaSGreg Clayton     }
391c7f09ccaSGreg Clayton     return m_objfile_sp.get();
392c7f09ccaSGreg Clayton }
393c7f09ccaSGreg Clayton 
39460830268SGreg Clayton const lldb_private::UUID&
39530fdc8d8SChris Lattner Module::GetUUID()
39630fdc8d8SChris Lattner {
397c5dac77aSEugene Zelenko     if (!m_did_parse_uuid.load())
39888c05f54SGreg Clayton     {
39916ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(m_mutex);
400c5dac77aSEugene Zelenko         if (!m_did_parse_uuid.load())
40130fdc8d8SChris Lattner         {
40230fdc8d8SChris Lattner             ObjectFile * obj_file = GetObjectFile ();
40330fdc8d8SChris Lattner 
404c5dac77aSEugene Zelenko             if (obj_file != nullptr)
40530fdc8d8SChris Lattner             {
40630fdc8d8SChris Lattner                 obj_file->GetUUID(&m_uuid);
407e83e731eSGreg Clayton                 m_did_parse_uuid = true;
40830fdc8d8SChris Lattner             }
40930fdc8d8SChris Lattner         }
41088c05f54SGreg Clayton     }
41130fdc8d8SChris Lattner     return m_uuid;
41230fdc8d8SChris Lattner }
41330fdc8d8SChris Lattner 
4148b4edba9SGreg Clayton TypeSystem *
4158b4edba9SGreg Clayton Module::GetTypeSystemForLanguage (LanguageType language)
4168b4edba9SGreg Clayton {
4175beec213SGreg Clayton     return m_type_system_map.GetTypeSystemForLanguage(language, this, true);
4186beaaa68SGreg Clayton }
4196beaaa68SGreg Clayton 
42030fdc8d8SChris Lattner void
42130fdc8d8SChris Lattner Module::ParseAllDebugSymbols()
42230fdc8d8SChris Lattner {
42316ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
424c7bece56SGreg Clayton     size_t num_comp_units = GetNumCompileUnits();
42530fdc8d8SChris Lattner     if (num_comp_units == 0)
42630fdc8d8SChris Lattner         return;
42730fdc8d8SChris Lattner 
428a2eee184SGreg Clayton     SymbolContext sc;
429e1cd1be6SGreg Clayton     sc.module_sp = shared_from_this();
43030fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
43130fdc8d8SChris Lattner 
432c7bece56SGreg Clayton     for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
43330fdc8d8SChris Lattner     {
43430fdc8d8SChris Lattner         sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
43530fdc8d8SChris Lattner         if (sc.comp_unit)
43630fdc8d8SChris Lattner         {
437c5dac77aSEugene Zelenko             sc.function = nullptr;
43830fdc8d8SChris Lattner             symbols->ParseVariablesForContext(sc);
43930fdc8d8SChris Lattner 
44030fdc8d8SChris Lattner             symbols->ParseCompileUnitFunctions(sc);
44130fdc8d8SChris Lattner 
442c5dac77aSEugene Zelenko             for (size_t func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != nullptr; ++func_idx)
44330fdc8d8SChris Lattner             {
44430fdc8d8SChris Lattner                 symbols->ParseFunctionBlocks(sc);
44530fdc8d8SChris Lattner 
44630fdc8d8SChris Lattner                 // Parse the variables for this function and all its blocks
44730fdc8d8SChris Lattner                 symbols->ParseVariablesForContext(sc);
44830fdc8d8SChris Lattner             }
44930fdc8d8SChris Lattner 
45030fdc8d8SChris Lattner             // Parse all types for this compile unit
451c5dac77aSEugene Zelenko             sc.function = nullptr;
45230fdc8d8SChris Lattner             symbols->ParseTypes(sc);
45330fdc8d8SChris Lattner         }
45430fdc8d8SChris Lattner     }
45530fdc8d8SChris Lattner }
45630fdc8d8SChris Lattner 
45730fdc8d8SChris Lattner void
45830fdc8d8SChris Lattner Module::CalculateSymbolContext(SymbolContext* sc)
45930fdc8d8SChris Lattner {
460e1cd1be6SGreg Clayton     sc->module_sp = shared_from_this();
46130fdc8d8SChris Lattner }
46230fdc8d8SChris Lattner 
463e72dfb32SGreg Clayton ModuleSP
4647e9b1fd0SGreg Clayton Module::CalculateSymbolContextModule ()
4657e9b1fd0SGreg Clayton {
466e72dfb32SGreg Clayton     return shared_from_this();
4677e9b1fd0SGreg Clayton }
4687e9b1fd0SGreg Clayton 
46930fdc8d8SChris Lattner void
47030fdc8d8SChris Lattner Module::DumpSymbolContext(Stream *s)
47130fdc8d8SChris Lattner {
472324a1036SSaleem Abdulrasool     s->Printf(", Module{%p}", static_cast<void*>(this));
47330fdc8d8SChris Lattner }
47430fdc8d8SChris Lattner 
475c7bece56SGreg Clayton size_t
47630fdc8d8SChris Lattner Module::GetNumCompileUnits()
47730fdc8d8SChris Lattner {
47816ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
479*f343968fSZachary Turner     Timer scoped_timer(LLVM_PRETTY_FUNCTION,
480324a1036SSaleem Abdulrasool                        "Module::GetNumCompileUnits (module = %p)",
481324a1036SSaleem Abdulrasool                        static_cast<void*>(this));
48230fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
48330fdc8d8SChris Lattner     if (symbols)
48430fdc8d8SChris Lattner         return symbols->GetNumCompileUnits();
48530fdc8d8SChris Lattner     return 0;
48630fdc8d8SChris Lattner }
48730fdc8d8SChris Lattner 
48830fdc8d8SChris Lattner CompUnitSP
489c7bece56SGreg Clayton Module::GetCompileUnitAtIndex (size_t index)
49030fdc8d8SChris Lattner {
49116ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
492c7bece56SGreg Clayton     size_t num_comp_units = GetNumCompileUnits ();
49330fdc8d8SChris Lattner     CompUnitSP cu_sp;
49430fdc8d8SChris Lattner 
49530fdc8d8SChris Lattner     if (index < num_comp_units)
49630fdc8d8SChris Lattner     {
49730fdc8d8SChris Lattner         SymbolVendor *symbols = GetSymbolVendor ();
49830fdc8d8SChris Lattner         if (symbols)
49930fdc8d8SChris Lattner             cu_sp = symbols->GetCompileUnitAtIndex(index);
50030fdc8d8SChris Lattner     }
50130fdc8d8SChris Lattner     return cu_sp;
50230fdc8d8SChris Lattner }
50330fdc8d8SChris Lattner 
50430fdc8d8SChris Lattner bool
50530fdc8d8SChris Lattner Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
50630fdc8d8SChris Lattner {
50716ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
508*f343968fSZachary Turner     Timer scoped_timer(LLVM_PRETTY_FUNCTION, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
5093046e668SGreg Clayton     SectionList *section_list = GetSectionList();
5103046e668SGreg Clayton     if (section_list)
5113046e668SGreg Clayton         return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
51230fdc8d8SChris Lattner     return false;
51330fdc8d8SChris Lattner }
51430fdc8d8SChris Lattner 
51530fdc8d8SChris Lattner uint32_t
51635729bb1SAshok Thirumurthi Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc,
51735729bb1SAshok Thirumurthi                                         bool resolve_tail_call_address)
51830fdc8d8SChris Lattner {
51916ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
52030fdc8d8SChris Lattner     uint32_t resolved_flags = 0;
52130fdc8d8SChris Lattner 
52272310355SGreg Clayton     // Clear the result symbol context in case we don't find anything, but don't clear the target
52372310355SGreg Clayton     sc.Clear(false);
52430fdc8d8SChris Lattner 
52530fdc8d8SChris Lattner     // Get the section from the section/offset address.
526e72dfb32SGreg Clayton     SectionSP section_sp (so_addr.GetSection());
52730fdc8d8SChris Lattner 
52830fdc8d8SChris Lattner     // Make sure the section matches this module before we try and match anything
529e72dfb32SGreg Clayton     if (section_sp && section_sp->GetModule().get() == this)
53030fdc8d8SChris Lattner     {
53130fdc8d8SChris Lattner         // If the section offset based address resolved itself, then this
53230fdc8d8SChris Lattner         // is the right module.
533e1cd1be6SGreg Clayton         sc.module_sp = shared_from_this();
53430fdc8d8SChris Lattner         resolved_flags |= eSymbolContextModule;
53530fdc8d8SChris Lattner 
53638807141SAshok Thirumurthi         SymbolVendor* sym_vendor = GetSymbolVendor();
53738807141SAshok Thirumurthi         if (!sym_vendor)
53838807141SAshok Thirumurthi             return resolved_flags;
53938807141SAshok Thirumurthi 
54030fdc8d8SChris Lattner         // Resolve the compile unit, function, block, line table or line
54130fdc8d8SChris Lattner         // entry if requested.
54230fdc8d8SChris Lattner         if (resolve_scope & eSymbolContextCompUnit    ||
54330fdc8d8SChris Lattner             resolve_scope & eSymbolContextFunction    ||
54430fdc8d8SChris Lattner             resolve_scope & eSymbolContextBlock       ||
5454c8e7828SGreg Clayton             resolve_scope & eSymbolContextLineEntry   ||
5464c8e7828SGreg Clayton             resolve_scope & eSymbolContextVariable    )
54730fdc8d8SChris Lattner         {
54838807141SAshok Thirumurthi             resolved_flags |= sym_vendor->ResolveSymbolContext (so_addr, resolve_scope, sc);
54930fdc8d8SChris Lattner         }
55030fdc8d8SChris Lattner 
551680e1778SJim Ingham         // Resolve the symbol if requested, but don't re-look it up if we've already found it.
552680e1778SJim Ingham         if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
55330fdc8d8SChris Lattner         {
554a7499c98SMichael Sartain             Symtab *symtab = sym_vendor->GetSymtab();
55538807141SAshok Thirumurthi             if (symtab && so_addr.IsSectionOffset())
55630fdc8d8SChris Lattner             {
5570d9dd7dfSMohit K. Bhakkad                 Symbol *matching_symbol = nullptr;
558c35b91ceSAdrian McCarthy 
559c35b91ceSAdrian McCarthy                 symtab->ForEachSymbolContainingFileAddress(so_addr.GetFileAddress(),
560c35b91ceSAdrian McCarthy                                                            [&matching_symbol](Symbol *symbol) -> bool {
5610d9dd7dfSMohit K. Bhakkad                                                                if (symbol->GetType() != eSymbolTypeInvalid)
5620d9dd7dfSMohit K. Bhakkad                                                                {
5630d9dd7dfSMohit K. Bhakkad                                                                    matching_symbol = symbol;
5640d9dd7dfSMohit K. Bhakkad                                                                    return false; // Stop iterating
5650d9dd7dfSMohit K. Bhakkad                                                                }
5660d9dd7dfSMohit K. Bhakkad                                                                return true; // Keep iterating
5670d9dd7dfSMohit K. Bhakkad                                                            });
5680d9dd7dfSMohit K. Bhakkad                 sc.symbol = matching_symbol;
56935729bb1SAshok Thirumurthi                 if (!sc.symbol &&
57035729bb1SAshok Thirumurthi                     resolve_scope & eSymbolContextFunction && !(resolved_flags & eSymbolContextFunction))
57135729bb1SAshok Thirumurthi                 {
57235729bb1SAshok Thirumurthi                     bool verify_unique = false; // No need to check again since ResolveSymbolContext failed to find a symbol at this address.
57335729bb1SAshok Thirumurthi                     if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
57435729bb1SAshok Thirumurthi                         sc.symbol = obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
57535729bb1SAshok Thirumurthi                 }
57635729bb1SAshok Thirumurthi 
57730fdc8d8SChris Lattner                 if (sc.symbol)
57893e2861bSGreg Clayton                 {
57993e2861bSGreg Clayton                     if (sc.symbol->IsSynthetic())
58093e2861bSGreg Clayton                     {
58193e2861bSGreg Clayton                         // We have a synthetic symbol so lets check if the object file
58293e2861bSGreg Clayton                         // from the symbol file in the symbol vendor is different than
58393e2861bSGreg Clayton                         // the object file for the module, and if so search its symbol
58493e2861bSGreg Clayton                         // table to see if we can come up with a better symbol. For example
58593e2861bSGreg Clayton                         // dSYM files on MacOSX have an unstripped symbol table inside of
58693e2861bSGreg Clayton                         // them.
58793e2861bSGreg Clayton                         ObjectFile *symtab_objfile = symtab->GetObjectFile();
58893e2861bSGreg Clayton                         if (symtab_objfile && symtab_objfile->IsStripped())
58993e2861bSGreg Clayton                         {
59093e2861bSGreg Clayton                             SymbolFile *symfile = sym_vendor->GetSymbolFile();
59193e2861bSGreg Clayton                             if (symfile)
59293e2861bSGreg Clayton                             {
59393e2861bSGreg Clayton                                 ObjectFile *symfile_objfile = symfile->GetObjectFile();
59493e2861bSGreg Clayton                                 if (symfile_objfile != symtab_objfile)
59593e2861bSGreg Clayton                                 {
59693e2861bSGreg Clayton                                     Symtab *symfile_symtab = symfile_objfile->GetSymtab();
59793e2861bSGreg Clayton                                     if (symfile_symtab)
59893e2861bSGreg Clayton                                     {
59993e2861bSGreg Clayton                                         Symbol *symbol = symfile_symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
60093e2861bSGreg Clayton                                         if (symbol && !symbol->IsSynthetic())
60193e2861bSGreg Clayton                                         {
60293e2861bSGreg Clayton                                             sc.symbol = symbol;
60393e2861bSGreg Clayton                                         }
60493e2861bSGreg Clayton                                     }
60593e2861bSGreg Clayton                                 }
60693e2861bSGreg Clayton                             }
60793e2861bSGreg Clayton                         }
60893e2861bSGreg Clayton                     }
60930fdc8d8SChris Lattner                     resolved_flags |= eSymbolContextSymbol;
61030fdc8d8SChris Lattner                 }
61130fdc8d8SChris Lattner             }
61293e2861bSGreg Clayton         }
61338807141SAshok Thirumurthi 
61438807141SAshok Thirumurthi         // For function symbols, so_addr may be off by one.  This is a convention consistent
61538807141SAshok Thirumurthi         // with FDE row indices in eh_frame sections, but requires extra logic here to permit
61638807141SAshok Thirumurthi         // symbol lookup for disassembly and unwind.
61738807141SAshok Thirumurthi         if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol) &&
61835729bb1SAshok Thirumurthi             resolve_tail_call_address && so_addr.IsSectionOffset())
61938807141SAshok Thirumurthi         {
62038807141SAshok Thirumurthi             Address previous_addr = so_addr;
621edfaae39SGreg Clayton             previous_addr.Slide(-1);
62238807141SAshok Thirumurthi 
62335729bb1SAshok Thirumurthi             bool do_resolve_tail_call_address = false; // prevent recursion
62435729bb1SAshok Thirumurthi             const uint32_t flags = ResolveSymbolContextForAddress(previous_addr, resolve_scope, sc,
62535729bb1SAshok Thirumurthi                                                                   do_resolve_tail_call_address);
62638807141SAshok Thirumurthi             if (flags & eSymbolContextSymbol)
62738807141SAshok Thirumurthi             {
62838807141SAshok Thirumurthi                 AddressRange addr_range;
62938807141SAshok Thirumurthi                 if (sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
63038807141SAshok Thirumurthi                 {
63138807141SAshok Thirumurthi                     if (addr_range.GetBaseAddress().GetSection() == so_addr.GetSection())
63238807141SAshok Thirumurthi                     {
63338807141SAshok Thirumurthi                         // If the requested address is one past the address range of a function (i.e. a tail call),
63438807141SAshok Thirumurthi                         // or the decremented address is the start of a function (i.e. some forms of trampoline),
63538807141SAshok Thirumurthi                         // indicate that the symbol has been resolved.
63638807141SAshok Thirumurthi                         if (so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() ||
63738807141SAshok Thirumurthi                             so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() + addr_range.GetByteSize())
63838807141SAshok Thirumurthi                         {
63938807141SAshok Thirumurthi                             resolved_flags |= flags;
64038807141SAshok Thirumurthi                         }
64138807141SAshok Thirumurthi                     }
64238807141SAshok Thirumurthi                     else
64338807141SAshok Thirumurthi                     {
64438807141SAshok Thirumurthi                         sc.symbol = nullptr; // Don't trust the symbol if the sections didn't match.
64538807141SAshok Thirumurthi                     }
64638807141SAshok Thirumurthi                 }
64730fdc8d8SChris Lattner             }
64830fdc8d8SChris Lattner         }
64930fdc8d8SChris Lattner     }
65030fdc8d8SChris Lattner     return resolved_flags;
65130fdc8d8SChris Lattner }
65230fdc8d8SChris Lattner 
65330fdc8d8SChris Lattner uint32_t
654274060b6SGreg Clayton Module::ResolveSymbolContextForFilePath
655274060b6SGreg Clayton (
656274060b6SGreg Clayton     const char *file_path,
657274060b6SGreg Clayton     uint32_t line,
658274060b6SGreg Clayton     bool check_inlines,
659274060b6SGreg Clayton     uint32_t resolve_scope,
660274060b6SGreg Clayton     SymbolContextList& sc_list
661274060b6SGreg Clayton )
66230fdc8d8SChris Lattner {
663274060b6SGreg Clayton     FileSpec file_spec(file_path, false);
66430fdc8d8SChris Lattner     return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
66530fdc8d8SChris Lattner }
66630fdc8d8SChris Lattner 
66730fdc8d8SChris Lattner uint32_t
66830fdc8d8SChris Lattner Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
66930fdc8d8SChris Lattner {
67016ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
671*f343968fSZachary Turner     Timer scoped_timer(LLVM_PRETTY_FUNCTION,
672b5ad4ec7SGreg Clayton                        "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
673b5ad4ec7SGreg Clayton                        file_spec.GetPath().c_str(),
67430fdc8d8SChris Lattner                        line,
67530fdc8d8SChris Lattner                        check_inlines ? "yes" : "no",
67630fdc8d8SChris Lattner                        resolve_scope);
67730fdc8d8SChris Lattner 
67830fdc8d8SChris Lattner     const uint32_t initial_count = sc_list.GetSize();
67930fdc8d8SChris Lattner 
68030fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor  ();
68130fdc8d8SChris Lattner     if (symbols)
68230fdc8d8SChris Lattner         symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
68330fdc8d8SChris Lattner 
68430fdc8d8SChris Lattner     return sc_list.GetSize() - initial_count;
68530fdc8d8SChris Lattner }
68630fdc8d8SChris Lattner 
687c7bece56SGreg Clayton size_t
688c7bece56SGreg Clayton Module::FindGlobalVariables (const ConstString &name,
68999558cc4SGreg Clayton                              const CompilerDeclContext *parent_decl_ctx,
690c7bece56SGreg Clayton                              bool append,
691c7bece56SGreg Clayton                              size_t max_matches,
692c7bece56SGreg Clayton                              VariableList& variables)
69330fdc8d8SChris Lattner {
69430fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
69530fdc8d8SChris Lattner     if (symbols)
69699558cc4SGreg Clayton         return symbols->FindGlobalVariables(name, parent_decl_ctx, append, max_matches, variables);
69730fdc8d8SChris Lattner     return 0;
69830fdc8d8SChris Lattner }
699c7bece56SGreg Clayton 
700c7bece56SGreg Clayton size_t
701c7bece56SGreg Clayton Module::FindGlobalVariables (const RegularExpression& regex,
702c7bece56SGreg Clayton                              bool append,
703c7bece56SGreg Clayton                              size_t max_matches,
704c7bece56SGreg Clayton                              VariableList& variables)
70530fdc8d8SChris Lattner {
70630fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
70730fdc8d8SChris Lattner     if (symbols)
70830fdc8d8SChris Lattner         return symbols->FindGlobalVariables(regex, append, max_matches, variables);
70930fdc8d8SChris Lattner     return 0;
71030fdc8d8SChris Lattner }
71130fdc8d8SChris Lattner 
712c7bece56SGreg Clayton size_t
713644247c1SGreg Clayton Module::FindCompileUnits (const FileSpec &path,
714644247c1SGreg Clayton                           bool append,
715644247c1SGreg Clayton                           SymbolContextList &sc_list)
716644247c1SGreg Clayton {
717644247c1SGreg Clayton     if (!append)
718644247c1SGreg Clayton         sc_list.Clear();
719644247c1SGreg Clayton 
720c7bece56SGreg Clayton     const size_t start_size = sc_list.GetSize();
721c7bece56SGreg Clayton     const size_t num_compile_units = GetNumCompileUnits();
722644247c1SGreg Clayton     SymbolContext sc;
723e1cd1be6SGreg Clayton     sc.module_sp = shared_from_this();
724ddd7a2a6SSean Callanan     const bool compare_directory = (bool)path.GetDirectory();
725c7bece56SGreg Clayton     for (size_t i = 0; i < num_compile_units; ++i)
726644247c1SGreg Clayton     {
727644247c1SGreg Clayton         sc.comp_unit = GetCompileUnitAtIndex(i).get();
7282dafd8edSGreg Clayton         if (sc.comp_unit)
7292dafd8edSGreg Clayton         {
730644247c1SGreg Clayton             if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
731644247c1SGreg Clayton                 sc_list.Append(sc);
732644247c1SGreg Clayton         }
7332dafd8edSGreg Clayton     }
734644247c1SGreg Clayton     return sc_list.GetSize() - start_size;
735644247c1SGreg Clayton }
736644247c1SGreg Clayton 
7376234a5c8SGreg Clayton Module::LookupInfo::LookupInfo(const ConstString &name, uint32_t name_type_mask, lldb::LanguageType language) :
7386234a5c8SGreg Clayton     m_name(name),
7396234a5c8SGreg Clayton     m_lookup_name(),
7406234a5c8SGreg Clayton     m_language(language),
7416234a5c8SGreg Clayton     m_name_type_mask(0),
7426234a5c8SGreg Clayton     m_match_name_after_lookup(false)
7436234a5c8SGreg Clayton {
7446234a5c8SGreg Clayton     const char *name_cstr = name.GetCString();
7456234a5c8SGreg Clayton     llvm::StringRef basename;
7466234a5c8SGreg Clayton     llvm::StringRef context;
7476234a5c8SGreg Clayton 
7486234a5c8SGreg Clayton     if (name_type_mask & eFunctionNameTypeAuto)
7496234a5c8SGreg Clayton     {
7506234a5c8SGreg Clayton         if (CPlusPlusLanguage::IsCPPMangledName (name_cstr))
7516234a5c8SGreg Clayton             m_name_type_mask = eFunctionNameTypeFull;
7526234a5c8SGreg Clayton         else if ((language == eLanguageTypeUnknown ||
7536234a5c8SGreg Clayton                   Language::LanguageIsObjC(language)) &&
7546234a5c8SGreg Clayton                  ObjCLanguage::IsPossibleObjCMethodName (name_cstr))
7556234a5c8SGreg Clayton             m_name_type_mask = eFunctionNameTypeFull;
7566234a5c8SGreg Clayton         else if (Language::LanguageIsC(language))
7576234a5c8SGreg Clayton         {
7586234a5c8SGreg Clayton             m_name_type_mask = eFunctionNameTypeFull;
7596234a5c8SGreg Clayton         }
7606234a5c8SGreg Clayton         else
7616234a5c8SGreg Clayton         {
7626234a5c8SGreg Clayton             if ((language == eLanguageTypeUnknown ||
7636234a5c8SGreg Clayton                  Language::LanguageIsObjC(language)) &&
7646234a5c8SGreg Clayton                 ObjCLanguage::IsPossibleObjCSelector(name_cstr))
7656234a5c8SGreg Clayton                 m_name_type_mask |= eFunctionNameTypeSelector;
7666234a5c8SGreg Clayton 
7676234a5c8SGreg Clayton             CPlusPlusLanguage::MethodName cpp_method (name);
7686234a5c8SGreg Clayton             basename = cpp_method.GetBasename();
7696234a5c8SGreg Clayton             if (basename.empty())
7706234a5c8SGreg Clayton             {
7716234a5c8SGreg Clayton                 if (CPlusPlusLanguage::ExtractContextAndIdentifier (name_cstr, context, basename))
7726234a5c8SGreg Clayton                     m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
7736234a5c8SGreg Clayton                 else
7746234a5c8SGreg Clayton                     m_name_type_mask |= eFunctionNameTypeFull;
7756234a5c8SGreg Clayton             }
7766234a5c8SGreg Clayton             else
7776234a5c8SGreg Clayton             {
7786234a5c8SGreg Clayton                 m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
7796234a5c8SGreg Clayton             }
7806234a5c8SGreg Clayton         }
7816234a5c8SGreg Clayton     }
7826234a5c8SGreg Clayton     else
7836234a5c8SGreg Clayton     {
7846234a5c8SGreg Clayton         m_name_type_mask = name_type_mask;
7856234a5c8SGreg Clayton         if (name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
7866234a5c8SGreg Clayton         {
7876234a5c8SGreg Clayton             // If they've asked for a CPP method or function name and it can't be that, we don't
7886234a5c8SGreg Clayton             // even need to search for CPP methods or names.
7896234a5c8SGreg Clayton             CPlusPlusLanguage::MethodName cpp_method (name);
7906234a5c8SGreg Clayton             if (cpp_method.IsValid())
7916234a5c8SGreg Clayton             {
7926234a5c8SGreg Clayton                 basename = cpp_method.GetBasename();
7936234a5c8SGreg Clayton 
7946234a5c8SGreg Clayton                 if (!cpp_method.GetQualifiers().empty())
7956234a5c8SGreg Clayton                 {
7966234a5c8SGreg Clayton                     // There is a "const" or other qualifier following the end of the function parens,
7976234a5c8SGreg Clayton                     // this can't be a eFunctionNameTypeBase
7986234a5c8SGreg Clayton                     m_name_type_mask &= ~(eFunctionNameTypeBase);
7996234a5c8SGreg Clayton                     if (m_name_type_mask == eFunctionNameTypeNone)
8006234a5c8SGreg Clayton                         return;
8016234a5c8SGreg Clayton                 }
8026234a5c8SGreg Clayton             }
8036234a5c8SGreg Clayton             else
8046234a5c8SGreg Clayton             {
8056234a5c8SGreg Clayton                 // If the CPP method parser didn't manage to chop this up, try to fill in the base name if we can.
8066234a5c8SGreg Clayton                 // If a::b::c is passed in, we need to just look up "c", and then we'll filter the result later.
8076234a5c8SGreg Clayton                 CPlusPlusLanguage::ExtractContextAndIdentifier (name_cstr, context, basename);
8086234a5c8SGreg Clayton             }
8096234a5c8SGreg Clayton         }
8106234a5c8SGreg Clayton 
8116234a5c8SGreg Clayton         if (name_type_mask & eFunctionNameTypeSelector)
8126234a5c8SGreg Clayton         {
8136234a5c8SGreg Clayton             if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr))
8146234a5c8SGreg Clayton             {
8156234a5c8SGreg Clayton                 m_name_type_mask &= ~(eFunctionNameTypeSelector);
8166234a5c8SGreg Clayton                 if (m_name_type_mask == eFunctionNameTypeNone)
8176234a5c8SGreg Clayton                     return;
8186234a5c8SGreg Clayton             }
8196234a5c8SGreg Clayton         }
8206234a5c8SGreg Clayton 
8216234a5c8SGreg Clayton         // Still try and get a basename in case someone specifies a name type mask of
8226234a5c8SGreg Clayton         // eFunctionNameTypeFull and a name like "A::func"
8236234a5c8SGreg Clayton         if (basename.empty())
8246234a5c8SGreg Clayton         {
8256234a5c8SGreg Clayton             if (name_type_mask & eFunctionNameTypeFull)
8266234a5c8SGreg Clayton             {
8276234a5c8SGreg Clayton                 CPlusPlusLanguage::MethodName cpp_method (name);
8286234a5c8SGreg Clayton                 basename = cpp_method.GetBasename();
8296234a5c8SGreg Clayton                 if (basename.empty())
8306234a5c8SGreg Clayton                     CPlusPlusLanguage::ExtractContextAndIdentifier (name_cstr, context, basename);
8316234a5c8SGreg Clayton             }
8326234a5c8SGreg Clayton         }
8336234a5c8SGreg Clayton     }
8346234a5c8SGreg Clayton 
8356234a5c8SGreg Clayton     if (!basename.empty())
8366234a5c8SGreg Clayton     {
8376234a5c8SGreg Clayton         // The name supplied was a partial C++ path like "a::count". In this case we want to do a
8386234a5c8SGreg Clayton         // lookup on the basename "count" and then make sure any matching results contain "a::count"
8396234a5c8SGreg Clayton         // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
8406234a5c8SGreg Clayton         // to true
8416234a5c8SGreg Clayton         m_lookup_name.SetString(basename);
8426234a5c8SGreg Clayton         m_match_name_after_lookup = true;
8436234a5c8SGreg Clayton     }
8446234a5c8SGreg Clayton     else
8456234a5c8SGreg Clayton     {
8466234a5c8SGreg Clayton         // The name is already correct, just use the exact name as supplied, and we won't need
8476234a5c8SGreg Clayton         // to check if any matches contain "name"
8486234a5c8SGreg Clayton         m_lookup_name = name;
8496234a5c8SGreg Clayton         m_match_name_after_lookup = false;
8506234a5c8SGreg Clayton     }
8516234a5c8SGreg Clayton 
8526234a5c8SGreg Clayton }
8536234a5c8SGreg Clayton 
8546234a5c8SGreg Clayton void
8556234a5c8SGreg Clayton Module::LookupInfo::Prune(SymbolContextList &sc_list, size_t start_idx) const
8566234a5c8SGreg Clayton {
8576234a5c8SGreg Clayton     if (m_match_name_after_lookup && m_name)
8586234a5c8SGreg Clayton     {
8596234a5c8SGreg Clayton         SymbolContext sc;
8606234a5c8SGreg Clayton         size_t i = start_idx;
8616234a5c8SGreg Clayton         while (i < sc_list.GetSize())
8626234a5c8SGreg Clayton         {
8636234a5c8SGreg Clayton             if (!sc_list.GetContextAtIndex(i, sc))
8646234a5c8SGreg Clayton                 break;
8656234a5c8SGreg Clayton             ConstString full_name(sc.GetFunctionName());
8666234a5c8SGreg Clayton             if (full_name && ::strstr(full_name.GetCString(), m_name.GetCString()) == nullptr)
8676234a5c8SGreg Clayton             {
8686234a5c8SGreg Clayton                 sc_list.RemoveContextAtIndex(i);
8696234a5c8SGreg Clayton             }
8706234a5c8SGreg Clayton             else
8716234a5c8SGreg Clayton             {
8726234a5c8SGreg Clayton                 ++i;
8736234a5c8SGreg Clayton             }
8746234a5c8SGreg Clayton         }
8756234a5c8SGreg Clayton     }
8766234a5c8SGreg Clayton 
8776234a5c8SGreg Clayton     // If we have only full name matches we might have tried to set breakpoint on "func"
8786234a5c8SGreg Clayton     // and specified eFunctionNameTypeFull, but we might have found "a::func()",
8796234a5c8SGreg Clayton     // "a::b::func()", "c::func()", "func()" and "func". Only "func()" and "func" should
8806234a5c8SGreg Clayton     // end up matching.
8816234a5c8SGreg Clayton     if (m_name_type_mask == eFunctionNameTypeFull)
8826234a5c8SGreg Clayton     {
8836234a5c8SGreg Clayton         SymbolContext sc;
8846234a5c8SGreg Clayton         size_t i = start_idx;
8856234a5c8SGreg Clayton         while (i < sc_list.GetSize())
8866234a5c8SGreg Clayton         {
8876234a5c8SGreg Clayton             if (!sc_list.GetContextAtIndex(i, sc))
8886234a5c8SGreg Clayton                 break;
8896234a5c8SGreg Clayton             ConstString full_name(sc.GetFunctionName());
8906234a5c8SGreg Clayton             CPlusPlusLanguage::MethodName cpp_method(full_name);
8916234a5c8SGreg Clayton             if (cpp_method.IsValid())
8926234a5c8SGreg Clayton             {
8936234a5c8SGreg Clayton                 if (cpp_method.GetContext().empty())
8946234a5c8SGreg Clayton                 {
8956234a5c8SGreg Clayton                     if (cpp_method.GetBasename().compare(m_name.GetStringRef()) != 0)
8966234a5c8SGreg Clayton                     {
8976234a5c8SGreg Clayton                         sc_list.RemoveContextAtIndex(i);
8986234a5c8SGreg Clayton                         continue;
8996234a5c8SGreg Clayton                     }
9006234a5c8SGreg Clayton                 }
9016234a5c8SGreg Clayton                 else
9026234a5c8SGreg Clayton                 {
9036234a5c8SGreg Clayton                     std::string qualified_name = cpp_method.GetScopeQualifiedName();
9046234a5c8SGreg Clayton                     if (qualified_name.compare(m_name.GetCString()) != 0)
9056234a5c8SGreg Clayton                     {
9066234a5c8SGreg Clayton                         sc_list.RemoveContextAtIndex(i);
9076234a5c8SGreg Clayton                         continue;
9086234a5c8SGreg Clayton                     }
9096234a5c8SGreg Clayton                 }
9106234a5c8SGreg Clayton             }
9116234a5c8SGreg Clayton             ++i;
9126234a5c8SGreg Clayton         }
9136234a5c8SGreg Clayton     }
9146234a5c8SGreg Clayton }
9156234a5c8SGreg Clayton 
9166234a5c8SGreg Clayton 
917c7bece56SGreg Clayton size_t
918931180e6SGreg Clayton Module::FindFunctions (const ConstString &name,
91999558cc4SGreg Clayton                        const CompilerDeclContext *parent_decl_ctx,
920931180e6SGreg Clayton                        uint32_t name_type_mask,
921931180e6SGreg Clayton                        bool include_symbols,
9229df05fbbSSean Callanan                        bool include_inlines,
923931180e6SGreg Clayton                        bool append,
924931180e6SGreg Clayton                        SymbolContextList& sc_list)
92530fdc8d8SChris Lattner {
926931180e6SGreg Clayton     if (!append)
927931180e6SGreg Clayton         sc_list.Clear();
928931180e6SGreg Clayton 
92943fe217bSGreg Clayton     const size_t old_size = sc_list.GetSize();
930931180e6SGreg Clayton 
931931180e6SGreg Clayton     // Find all the functions (not symbols, but debug information functions...
93230fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
93343fe217bSGreg Clayton 
93443fe217bSGreg Clayton     if (name_type_mask & eFunctionNameTypeAuto)
93543fe217bSGreg Clayton     {
9366234a5c8SGreg Clayton         LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
93743fe217bSGreg Clayton 
93843fe217bSGreg Clayton         if (symbols)
939a7499c98SMichael Sartain         {
9406234a5c8SGreg Clayton             symbols->FindFunctions(lookup_info.GetLookupName(),
94199558cc4SGreg Clayton                                    parent_decl_ctx,
9426234a5c8SGreg Clayton                                    lookup_info.GetNameTypeMask(),
94343fe217bSGreg Clayton                                    include_inlines,
94443fe217bSGreg Clayton                                    append,
94543fe217bSGreg Clayton                                    sc_list);
94643fe217bSGreg Clayton 
94743fe217bSGreg Clayton             // Now check our symbol table for symbols that are code symbols if requested
94843fe217bSGreg Clayton             if (include_symbols)
94943fe217bSGreg Clayton             {
950a7499c98SMichael Sartain                 Symtab *symtab = symbols->GetSymtab();
95143fe217bSGreg Clayton                 if (symtab)
9526234a5c8SGreg Clayton                     symtab->FindFunctionSymbols(lookup_info.GetLookupName(), lookup_info.GetNameTypeMask(), sc_list);
95343fe217bSGreg Clayton             }
95443fe217bSGreg Clayton         }
95543fe217bSGreg Clayton 
9566234a5c8SGreg Clayton         const size_t new_size = sc_list.GetSize();
9576234a5c8SGreg Clayton 
9586234a5c8SGreg Clayton         if (old_size < new_size)
9596234a5c8SGreg Clayton             lookup_info.Prune (sc_list, old_size);
96043fe217bSGreg Clayton     }
96143fe217bSGreg Clayton     else
96243fe217bSGreg Clayton     {
96330fdc8d8SChris Lattner         if (symbols)
964a7499c98SMichael Sartain         {
96599558cc4SGreg Clayton             symbols->FindFunctions(name, parent_decl_ctx, name_type_mask, include_inlines, append, sc_list);
966931180e6SGreg Clayton 
967931180e6SGreg Clayton             // Now check our symbol table for symbols that are code symbols if requested
968931180e6SGreg Clayton             if (include_symbols)
969931180e6SGreg Clayton             {
970a7499c98SMichael Sartain                 Symtab *symtab = symbols->GetSymtab();
971931180e6SGreg Clayton                 if (symtab)
97243fe217bSGreg Clayton                     symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
973931180e6SGreg Clayton             }
974931180e6SGreg Clayton         }
975931180e6SGreg Clayton     }
97643fe217bSGreg Clayton 
97743fe217bSGreg Clayton     return sc_list.GetSize() - old_size;
97830fdc8d8SChris Lattner }
97930fdc8d8SChris Lattner 
980c7bece56SGreg Clayton size_t
981931180e6SGreg Clayton Module::FindFunctions (const RegularExpression& regex,
982931180e6SGreg Clayton                        bool include_symbols,
9839df05fbbSSean Callanan                        bool include_inlines,
984931180e6SGreg Clayton                        bool append,
985931180e6SGreg Clayton                        SymbolContextList& sc_list)
98630fdc8d8SChris Lattner {
987931180e6SGreg Clayton     if (!append)
988931180e6SGreg Clayton         sc_list.Clear();
989931180e6SGreg Clayton 
990c7bece56SGreg Clayton     const size_t start_size = sc_list.GetSize();
991931180e6SGreg Clayton 
99230fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
99330fdc8d8SChris Lattner     if (symbols)
994a7499c98SMichael Sartain     {
9959df05fbbSSean Callanan         symbols->FindFunctions(regex, include_inlines, append, sc_list);
996a7499c98SMichael Sartain 
997931180e6SGreg Clayton         // Now check our symbol table for symbols that are code symbols if requested
998931180e6SGreg Clayton         if (include_symbols)
999931180e6SGreg Clayton         {
1000a7499c98SMichael Sartain             Symtab *symtab = symbols->GetSymtab();
1001931180e6SGreg Clayton             if (symtab)
1002931180e6SGreg Clayton             {
1003931180e6SGreg Clayton                 std::vector<uint32_t> symbol_indexes;
100400049b8bSMatt Kopec                 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
1005c7bece56SGreg Clayton                 const size_t num_matches = symbol_indexes.size();
1006931180e6SGreg Clayton                 if (num_matches)
1007931180e6SGreg Clayton                 {
1008931180e6SGreg Clayton                     SymbolContext sc(this);
1009d8cf1a11SGreg Clayton                     const size_t end_functions_added_index = sc_list.GetSize();
1010d8cf1a11SGreg Clayton                     size_t num_functions_added_to_sc_list = end_functions_added_index - start_size;
1011d8cf1a11SGreg Clayton                     if (num_functions_added_to_sc_list == 0)
1012d8cf1a11SGreg Clayton                     {
1013d8cf1a11SGreg Clayton                         // No functions were added, just symbols, so we can just append them
1014d8cf1a11SGreg Clayton                         for (size_t i = 0; i < num_matches; ++i)
1015931180e6SGreg Clayton                         {
1016931180e6SGreg Clayton                             sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
101700049b8bSMatt Kopec                             SymbolType sym_type = sc.symbol->GetType();
101800049b8bSMatt Kopec                             if (sc.symbol && (sym_type == eSymbolTypeCode ||
101900049b8bSMatt Kopec                                               sym_type == eSymbolTypeResolver))
1020d8cf1a11SGreg Clayton                                 sc_list.Append(sc);
1021d8cf1a11SGreg Clayton                         }
1022d8cf1a11SGreg Clayton                     }
1023d8cf1a11SGreg Clayton                     else
1024d8cf1a11SGreg Clayton                     {
1025d8cf1a11SGreg Clayton                         typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
1026d8cf1a11SGreg Clayton                         FileAddrToIndexMap file_addr_to_index;
1027d8cf1a11SGreg Clayton                         for (size_t i = start_size; i < end_functions_added_index; ++i)
1028d8cf1a11SGreg Clayton                         {
1029d8cf1a11SGreg Clayton                             const SymbolContext &sc = sc_list[i];
1030d8cf1a11SGreg Clayton                             if (sc.block)
1031d8cf1a11SGreg Clayton                                 continue;
1032d8cf1a11SGreg Clayton                             file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i;
1033d8cf1a11SGreg Clayton                         }
1034d8cf1a11SGreg Clayton 
1035d8cf1a11SGreg Clayton                         FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
1036d8cf1a11SGreg Clayton                         // Functions were added so we need to merge symbols into any
1037d8cf1a11SGreg Clayton                         // existing function symbol contexts
1038d8cf1a11SGreg Clayton                         for (size_t i = start_size; i < num_matches; ++i)
1039d8cf1a11SGreg Clayton                         {
1040d8cf1a11SGreg Clayton                             sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
1041d8cf1a11SGreg Clayton                             SymbolType sym_type = sc.symbol->GetType();
1042358cf1eaSGreg Clayton                             if (sc.symbol && sc.symbol->ValueIsAddress() && (sym_type == eSymbolTypeCode || sym_type == eSymbolTypeResolver))
1043d8cf1a11SGreg Clayton                             {
1044358cf1eaSGreg Clayton                                 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddressRef().GetFileAddress());
1045d8cf1a11SGreg Clayton                                 if (pos == end)
1046d8cf1a11SGreg Clayton                                     sc_list.Append(sc);
1047d8cf1a11SGreg Clayton                                 else
1048d8cf1a11SGreg Clayton                                     sc_list[pos->second].symbol = sc.symbol;
1049d8cf1a11SGreg Clayton                             }
1050d8cf1a11SGreg Clayton                         }
1051931180e6SGreg Clayton                     }
1052931180e6SGreg Clayton                 }
1053931180e6SGreg Clayton             }
1054931180e6SGreg Clayton         }
1055931180e6SGreg Clayton     }
1056931180e6SGreg Clayton     return sc_list.GetSize() - start_size;
105730fdc8d8SChris Lattner }
105830fdc8d8SChris Lattner 
1059f86248d9SRichard Mitton void
1060f86248d9SRichard Mitton Module::FindAddressesForLine (const lldb::TargetSP target_sp,
1061f86248d9SRichard Mitton                               const FileSpec &file, uint32_t line,
1062f86248d9SRichard Mitton                               Function *function,
1063f86248d9SRichard Mitton                               std::vector<Address> &output_local, std::vector<Address> &output_extern)
1064f86248d9SRichard Mitton {
1065f86248d9SRichard Mitton     SearchFilterByModule filter(target_sp, m_file);
1066f86248d9SRichard Mitton     AddressResolverFileLine resolver(file, line, true);
1067f86248d9SRichard Mitton     resolver.ResolveAddress (filter);
1068f86248d9SRichard Mitton 
1069f86248d9SRichard Mitton     for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++)
1070f86248d9SRichard Mitton     {
1071f86248d9SRichard Mitton         Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
1072f86248d9SRichard Mitton         Function *f = addr.CalculateSymbolContextFunction();
1073f86248d9SRichard Mitton         if (f && f == function)
1074f86248d9SRichard Mitton             output_local.push_back (addr);
1075f86248d9SRichard Mitton         else
1076f86248d9SRichard Mitton             output_extern.push_back (addr);
1077f86248d9SRichard Mitton     }
1078f86248d9SRichard Mitton }
1079f86248d9SRichard Mitton 
1080c7bece56SGreg Clayton size_t
108184db9105SGreg Clayton Module::FindTypes_Impl (const SymbolContext& sc,
108284db9105SGreg Clayton                         const ConstString &name,
108399558cc4SGreg Clayton                         const CompilerDeclContext *parent_decl_ctx,
108484db9105SGreg Clayton                         bool append,
1085c7bece56SGreg Clayton                         size_t max_matches,
1086ae088e52SGreg Clayton                         llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
10874069730cSRavitheja Addepally                         TypeMap& types)
10883504eee8SGreg Clayton {
1089*f343968fSZachary Turner     Timer scoped_timer(LLVM_PRETTY_FUNCTION, LLVM_PRETTY_FUNCTION);
1090c5dac77aSEugene Zelenko     if (!sc.module_sp || sc.module_sp.get() == this)
10913504eee8SGreg Clayton     {
10923504eee8SGreg Clayton         SymbolVendor *symbols = GetSymbolVendor ();
10933504eee8SGreg Clayton         if (symbols)
1094ae088e52SGreg Clayton             return symbols->FindTypes(sc, name, parent_decl_ctx, append, max_matches, searched_symbol_files, types);
10953504eee8SGreg Clayton     }
10963504eee8SGreg Clayton     return 0;
10973504eee8SGreg Clayton }
10983504eee8SGreg Clayton 
1099c7bece56SGreg Clayton size_t
110084db9105SGreg Clayton Module::FindTypesInNamespace (const SymbolContext& sc,
110184db9105SGreg Clayton                               const ConstString &type_name,
110299558cc4SGreg Clayton                               const CompilerDeclContext *parent_decl_ctx,
1103c7bece56SGreg Clayton                               size_t max_matches,
110484db9105SGreg Clayton                               TypeList& type_list)
11056f3533fbSEnrico Granata {
110684db9105SGreg Clayton     const bool append = true;
11074069730cSRavitheja Addepally     TypeMap types_map;
1108ae088e52SGreg Clayton     llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
1109ae088e52SGreg Clayton     size_t num_types = FindTypes_Impl(sc, type_name, parent_decl_ctx, append, max_matches, searched_symbol_files, types_map);
11104069730cSRavitheja Addepally     if (num_types > 0)
11114069730cSRavitheja Addepally         sc.SortTypeList(types_map, type_list);
11124069730cSRavitheja Addepally     return num_types;
11136f3533fbSEnrico Granata }
11146f3533fbSEnrico Granata 
1115b43165b7SGreg Clayton lldb::TypeSP
1116b43165b7SGreg Clayton Module::FindFirstType (const SymbolContext& sc,
1117b43165b7SGreg Clayton                        const ConstString &name,
1118b43165b7SGreg Clayton                        bool exact_match)
1119b43165b7SGreg Clayton {
1120b43165b7SGreg Clayton     TypeList type_list;
1121ae088e52SGreg Clayton     llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
1122ae088e52SGreg Clayton     const size_t num_matches = FindTypes (sc, name, exact_match, 1, searched_symbol_files, type_list);
1123b43165b7SGreg Clayton     if (num_matches)
1124b43165b7SGreg Clayton         return type_list.GetTypeAtIndex(0);
1125b43165b7SGreg Clayton     return TypeSP();
1126b43165b7SGreg Clayton }
1127b43165b7SGreg Clayton 
1128c7bece56SGreg Clayton size_t
112984db9105SGreg Clayton Module::FindTypes (const SymbolContext& sc,
113084db9105SGreg Clayton                    const ConstString &name,
113184db9105SGreg Clayton                    bool exact_match,
1132c7bece56SGreg Clayton                    size_t max_matches,
1133ae088e52SGreg Clayton                    llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
113484db9105SGreg Clayton                    TypeList& types)
11356f3533fbSEnrico Granata {
1136c7bece56SGreg Clayton     size_t num_matches = 0;
113784db9105SGreg Clayton     const char *type_name_cstr = name.GetCString();
113884db9105SGreg Clayton     std::string type_scope;
113984db9105SGreg Clayton     std::string type_basename;
114084db9105SGreg Clayton     const bool append = true;
11417bc31332SGreg Clayton     TypeClass type_class = eTypeClassAny;
11424069730cSRavitheja Addepally     TypeMap typesmap;
11437bc31332SGreg Clayton     if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
114484db9105SGreg Clayton     {
114584db9105SGreg Clayton         // Check if "name" starts with "::" which means the qualified type starts
114684db9105SGreg Clayton         // from the root namespace and implies and exact match. The typenames we
114784db9105SGreg Clayton         // get back from clang do not start with "::" so we need to strip this off
1148d93c4a33SBruce Mitchener         // in order to get the qualified names to match
11496f3533fbSEnrico Granata 
115084db9105SGreg Clayton         if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
11516f3533fbSEnrico Granata         {
115284db9105SGreg Clayton             type_scope.erase(0,2);
115384db9105SGreg Clayton             exact_match = true;
115484db9105SGreg Clayton         }
115584db9105SGreg Clayton         ConstString type_basename_const_str (type_basename.c_str());
1156c5dac77aSEugene Zelenko         if (FindTypes_Impl(sc, type_basename_const_str, nullptr, append, max_matches, searched_symbol_files, typesmap))
115784db9105SGreg Clayton         {
11584069730cSRavitheja Addepally             typesmap.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
11594069730cSRavitheja Addepally             num_matches = typesmap.GetSize();
11606f3533fbSEnrico Granata         }
116184db9105SGreg Clayton     }
116284db9105SGreg Clayton     else
116384db9105SGreg Clayton     {
116484db9105SGreg Clayton         // The type is not in a namespace/class scope, just search for it by basename
11657bc31332SGreg Clayton         if (type_class != eTypeClassAny)
11667bc31332SGreg Clayton         {
11677bc31332SGreg Clayton             // The "type_name_cstr" will have been modified if we have a valid type class
11687bc31332SGreg Clayton             // prefix (like "struct", "class", "union", "typedef" etc).
1169c5dac77aSEugene Zelenko             FindTypes_Impl(sc, ConstString(type_name_cstr), nullptr, append, max_matches, searched_symbol_files, typesmap);
11704069730cSRavitheja Addepally             typesmap.RemoveMismatchedTypes (type_class);
11714069730cSRavitheja Addepally             num_matches = typesmap.GetSize();
11727bc31332SGreg Clayton         }
11737bc31332SGreg Clayton         else
11747bc31332SGreg Clayton         {
1175c5dac77aSEugene Zelenko             num_matches = FindTypes_Impl(sc, name, nullptr, append, max_matches, searched_symbol_files, typesmap);
117684db9105SGreg Clayton         }
11777bc31332SGreg Clayton     }
11784069730cSRavitheja Addepally     if (num_matches > 0)
11794069730cSRavitheja Addepally         sc.SortTypeList(typesmap, types);
118084db9105SGreg Clayton     return num_matches;
11816f3533fbSEnrico Granata }
11826f3533fbSEnrico Granata 
118330fdc8d8SChris Lattner SymbolVendor*
1184136dff87SGreg Clayton Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
118530fdc8d8SChris Lattner {
1186c5dac77aSEugene Zelenko     if (!m_did_load_symbol_vendor.load())
118788c05f54SGreg Clayton     {
118816ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(m_mutex);
1189c5dac77aSEugene Zelenko         if (!m_did_load_symbol_vendor.load() && can_create)
119030fdc8d8SChris Lattner         {
119130fdc8d8SChris Lattner             ObjectFile *obj_file = GetObjectFile ();
1192c5dac77aSEugene Zelenko             if (obj_file != nullptr)
119330fdc8d8SChris Lattner             {
1194*f343968fSZachary Turner                 Timer scoped_timer(LLVM_PRETTY_FUNCTION, LLVM_PRETTY_FUNCTION);
1195136dff87SGreg Clayton                 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
1196e83e731eSGreg Clayton                 m_did_load_symbol_vendor = true;
119730fdc8d8SChris Lattner             }
119830fdc8d8SChris Lattner         }
119988c05f54SGreg Clayton     }
120030fdc8d8SChris Lattner     return m_symfile_ap.get();
120130fdc8d8SChris Lattner }
120230fdc8d8SChris Lattner 
120330fdc8d8SChris Lattner void
120430fdc8d8SChris Lattner Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
120530fdc8d8SChris Lattner {
120630fdc8d8SChris Lattner     // Container objects whose paths do not specify a file directly can call
120730fdc8d8SChris Lattner     // this function to correct the file and object names.
120830fdc8d8SChris Lattner     m_file = file;
120930fdc8d8SChris Lattner     m_mod_time = file.GetModificationTime();
121030fdc8d8SChris Lattner     m_object_name = object_name;
121130fdc8d8SChris Lattner }
121230fdc8d8SChris Lattner 
121330fdc8d8SChris Lattner const ArchSpec&
121430fdc8d8SChris Lattner Module::GetArchitecture () const
121530fdc8d8SChris Lattner {
121630fdc8d8SChris Lattner     return m_arch;
121730fdc8d8SChris Lattner }
121830fdc8d8SChris Lattner 
1219b5ad4ec7SGreg Clayton std::string
1220b5ad4ec7SGreg Clayton Module::GetSpecificationDescription () const
1221b5ad4ec7SGreg Clayton {
1222b5ad4ec7SGreg Clayton     std::string spec(GetFileSpec().GetPath());
1223b5ad4ec7SGreg Clayton     if (m_object_name)
1224b5ad4ec7SGreg Clayton     {
1225b5ad4ec7SGreg Clayton         spec += '(';
1226b5ad4ec7SGreg Clayton         spec += m_object_name.GetCString();
1227b5ad4ec7SGreg Clayton         spec += ')';
1228b5ad4ec7SGreg Clayton     }
1229b5ad4ec7SGreg Clayton     return spec;
1230b5ad4ec7SGreg Clayton }
1231b5ad4ec7SGreg Clayton 
123230fdc8d8SChris Lattner void
1233c982b3d6SGreg Clayton Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
1234ceb6b139SCaroline Tice {
123516ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1236ceb6b139SCaroline Tice 
1237c982b3d6SGreg Clayton     if (level >= eDescriptionLevelFull)
1238c982b3d6SGreg Clayton     {
1239cfd1acedSGreg Clayton         if (m_arch.IsValid())
124064195a2cSGreg Clayton             s->Printf("(%s) ", m_arch.GetArchitectureName());
1241c982b3d6SGreg Clayton     }
1242ceb6b139SCaroline Tice 
1243c982b3d6SGreg Clayton     if (level == eDescriptionLevelBrief)
1244c982b3d6SGreg Clayton     {
1245c982b3d6SGreg Clayton         const char *filename = m_file.GetFilename().GetCString();
1246c982b3d6SGreg Clayton         if (filename)
1247c982b3d6SGreg Clayton             s->PutCString (filename);
1248c982b3d6SGreg Clayton     }
1249c982b3d6SGreg Clayton     else
1250c982b3d6SGreg Clayton     {
1251cfd1acedSGreg Clayton         char path[PATH_MAX];
1252cfd1acedSGreg Clayton         if (m_file.GetPath(path, sizeof(path)))
1253cfd1acedSGreg Clayton             s->PutCString(path);
1254c982b3d6SGreg Clayton     }
1255cfd1acedSGreg Clayton 
1256cfd1acedSGreg Clayton     const char *object_name = m_object_name.GetCString();
1257cfd1acedSGreg Clayton     if (object_name)
1258cfd1acedSGreg Clayton         s->Printf("(%s)", object_name);
1259ceb6b139SCaroline Tice }
1260ceb6b139SCaroline Tice 
1261ceb6b139SCaroline Tice void
1262c982b3d6SGreg Clayton Module::ReportError (const char *format, ...)
1263c982b3d6SGreg Clayton {
1264e38a5eddSGreg Clayton     if (format && format[0])
1265e38a5eddSGreg Clayton     {
1266e38a5eddSGreg Clayton         StreamString strm;
1267e38a5eddSGreg Clayton         strm.PutCString("error: ");
1268e38a5eddSGreg Clayton         GetDescription(&strm, lldb::eDescriptionLevelBrief);
12698b35334eSGreg Clayton         strm.PutChar (' ');
1270c982b3d6SGreg Clayton         va_list args;
1271c982b3d6SGreg Clayton         va_start (args, format);
1272e38a5eddSGreg Clayton         strm.PrintfVarArg(format, args);
1273c982b3d6SGreg Clayton         va_end (args);
1274e38a5eddSGreg Clayton 
1275e38a5eddSGreg Clayton         const int format_len = strlen(format);
1276e38a5eddSGreg Clayton         if (format_len > 0)
1277e38a5eddSGreg Clayton         {
1278e38a5eddSGreg Clayton             const char last_char = format[format_len-1];
1279e38a5eddSGreg Clayton             if (last_char != '\n' || last_char != '\r')
1280e38a5eddSGreg Clayton                 strm.EOL();
1281e38a5eddSGreg Clayton         }
1282e38a5eddSGreg Clayton         Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1283e38a5eddSGreg Clayton     }
1284e38a5eddSGreg Clayton }
1285e38a5eddSGreg Clayton 
12861d60909eSGreg Clayton bool
12871d60909eSGreg Clayton Module::FileHasChanged () const
12881d60909eSGreg Clayton {
1289c5dac77aSEugene Zelenko     if (!m_file_has_changed)
12901d60909eSGreg Clayton         m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
12911d60909eSGreg Clayton     return m_file_has_changed;
12921d60909eSGreg Clayton }
12931d60909eSGreg Clayton 
1294e38a5eddSGreg Clayton void
1295e38a5eddSGreg Clayton Module::ReportErrorIfModifyDetected (const char *format, ...)
1296e38a5eddSGreg Clayton {
1297c5dac77aSEugene Zelenko     if (!m_first_file_changed_log)
1298e38a5eddSGreg Clayton     {
12991d60909eSGreg Clayton         if (FileHasChanged ())
13001d60909eSGreg Clayton         {
13011d60909eSGreg Clayton             m_first_file_changed_log = true;
1302e38a5eddSGreg Clayton             if (format)
1303e38a5eddSGreg Clayton             {
1304e38a5eddSGreg Clayton                 StreamString strm;
1305e38a5eddSGreg Clayton                 strm.PutCString("error: the object file ");
1306e38a5eddSGreg Clayton                 GetDescription(&strm, lldb::eDescriptionLevelFull);
1307e38a5eddSGreg Clayton                 strm.PutCString (" has been modified\n");
1308e38a5eddSGreg Clayton 
1309e38a5eddSGreg Clayton                 va_list args;
1310e38a5eddSGreg Clayton                 va_start (args, format);
1311e38a5eddSGreg Clayton                 strm.PrintfVarArg(format, args);
1312e38a5eddSGreg Clayton                 va_end (args);
1313e38a5eddSGreg Clayton 
1314e38a5eddSGreg Clayton                 const int format_len = strlen(format);
1315e38a5eddSGreg Clayton                 if (format_len > 0)
1316e38a5eddSGreg Clayton                 {
1317e38a5eddSGreg Clayton                     const char last_char = format[format_len-1];
1318e38a5eddSGreg Clayton                     if (last_char != '\n' || last_char != '\r')
1319e38a5eddSGreg Clayton                         strm.EOL();
1320e38a5eddSGreg Clayton                 }
1321e38a5eddSGreg Clayton                 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
1322e38a5eddSGreg Clayton                 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1323e38a5eddSGreg Clayton             }
1324e38a5eddSGreg Clayton         }
1325c982b3d6SGreg Clayton     }
13261d60909eSGreg Clayton }
1327c982b3d6SGreg Clayton 
1328c982b3d6SGreg Clayton void
1329c982b3d6SGreg Clayton Module::ReportWarning (const char *format, ...)
1330c982b3d6SGreg Clayton {
1331e38a5eddSGreg Clayton     if (format && format[0])
1332e38a5eddSGreg Clayton     {
1333e38a5eddSGreg Clayton         StreamString strm;
1334e38a5eddSGreg Clayton         strm.PutCString("warning: ");
13358b35334eSGreg Clayton         GetDescription(&strm, lldb::eDescriptionLevelFull);
13368b35334eSGreg Clayton         strm.PutChar (' ');
1337c982b3d6SGreg Clayton 
1338c982b3d6SGreg Clayton         va_list args;
1339c982b3d6SGreg Clayton         va_start (args, format);
1340e38a5eddSGreg Clayton         strm.PrintfVarArg(format, args);
1341c982b3d6SGreg Clayton         va_end (args);
1342e38a5eddSGreg Clayton 
1343e38a5eddSGreg Clayton         const int format_len = strlen(format);
1344e38a5eddSGreg Clayton         if (format_len > 0)
1345e38a5eddSGreg Clayton         {
1346e38a5eddSGreg Clayton             const char last_char = format[format_len-1];
1347e38a5eddSGreg Clayton             if (last_char != '\n' || last_char != '\r')
1348e38a5eddSGreg Clayton                 strm.EOL();
1349e38a5eddSGreg Clayton         }
1350e38a5eddSGreg Clayton         Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
1351e38a5eddSGreg Clayton     }
1352c982b3d6SGreg Clayton }
1353c982b3d6SGreg Clayton 
1354c982b3d6SGreg Clayton void
1355c982b3d6SGreg Clayton Module::LogMessage (Log *log, const char *format, ...)
1356c982b3d6SGreg Clayton {
1357c5dac77aSEugene Zelenko     if (log != nullptr)
1358c982b3d6SGreg Clayton     {
1359c982b3d6SGreg Clayton         StreamString log_message;
13608b35334eSGreg Clayton         GetDescription(&log_message, lldb::eDescriptionLevelFull);
1361c982b3d6SGreg Clayton         log_message.PutCString (": ");
1362c982b3d6SGreg Clayton         va_list args;
1363c982b3d6SGreg Clayton         va_start (args, format);
1364c982b3d6SGreg Clayton         log_message.PrintfVarArg (format, args);
1365c982b3d6SGreg Clayton         va_end (args);
1366c982b3d6SGreg Clayton         log->PutCString(log_message.GetString().c_str());
1367c982b3d6SGreg Clayton     }
1368c982b3d6SGreg Clayton }
1369c982b3d6SGreg Clayton 
1370d61c0fc0SGreg Clayton void
1371d61c0fc0SGreg Clayton Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
1372d61c0fc0SGreg Clayton {
1373c5dac77aSEugene Zelenko     if (log != nullptr)
1374d61c0fc0SGreg Clayton     {
1375d61c0fc0SGreg Clayton         StreamString log_message;
1376d61c0fc0SGreg Clayton         GetDescription(&log_message, lldb::eDescriptionLevelFull);
1377d61c0fc0SGreg Clayton         log_message.PutCString (": ");
1378d61c0fc0SGreg Clayton         va_list args;
1379d61c0fc0SGreg Clayton         va_start (args, format);
1380d61c0fc0SGreg Clayton         log_message.PrintfVarArg (format, args);
1381d61c0fc0SGreg Clayton         va_end (args);
1382d61c0fc0SGreg Clayton         if (log->GetVerbose())
1383a893d301SZachary Turner         {
1384a893d301SZachary Turner             std::string back_trace;
1385a893d301SZachary Turner             llvm::raw_string_ostream stream(back_trace);
1386a893d301SZachary Turner             llvm::sys::PrintStackTrace(stream);
1387a893d301SZachary Turner             log_message.PutCString(back_trace.c_str());
1388a893d301SZachary Turner         }
1389d61c0fc0SGreg Clayton         log->PutCString(log_message.GetString().c_str());
1390d61c0fc0SGreg Clayton     }
1391d61c0fc0SGreg Clayton }
1392d61c0fc0SGreg Clayton 
1393c982b3d6SGreg Clayton void
139430fdc8d8SChris Lattner Module::Dump(Stream *s)
139530fdc8d8SChris Lattner {
139616ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
13978941142aSGreg Clayton     //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
139830fdc8d8SChris Lattner     s->Indent();
1399b5ad4ec7SGreg Clayton     s->Printf("Module %s%s%s%s\n",
1400b5ad4ec7SGreg Clayton               m_file.GetPath().c_str(),
140130fdc8d8SChris Lattner               m_object_name ? "(" : "",
140230fdc8d8SChris Lattner               m_object_name ? m_object_name.GetCString() : "",
140330fdc8d8SChris Lattner               m_object_name ? ")" : "");
140430fdc8d8SChris Lattner 
140530fdc8d8SChris Lattner     s->IndentMore();
140630fdc8d8SChris Lattner 
1407a7499c98SMichael Sartain     ObjectFile *objfile = GetObjectFile ();
140830fdc8d8SChris Lattner     if (objfile)
140930fdc8d8SChris Lattner         objfile->Dump(s);
141030fdc8d8SChris Lattner 
141130fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
141230fdc8d8SChris Lattner     if (symbols)
141330fdc8d8SChris Lattner         symbols->Dump(s);
141430fdc8d8SChris Lattner 
141530fdc8d8SChris Lattner     s->IndentLess();
141630fdc8d8SChris Lattner }
141730fdc8d8SChris Lattner 
141830fdc8d8SChris Lattner TypeList*
141930fdc8d8SChris Lattner Module::GetTypeList ()
142030fdc8d8SChris Lattner {
142130fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
142230fdc8d8SChris Lattner     if (symbols)
142330fdc8d8SChris Lattner         return &symbols->GetTypeList();
1424c5dac77aSEugene Zelenko     return nullptr;
142530fdc8d8SChris Lattner }
142630fdc8d8SChris Lattner 
142730fdc8d8SChris Lattner const ConstString &
142830fdc8d8SChris Lattner Module::GetObjectName() const
142930fdc8d8SChris Lattner {
143030fdc8d8SChris Lattner     return m_object_name;
143130fdc8d8SChris Lattner }
143230fdc8d8SChris Lattner 
143330fdc8d8SChris Lattner ObjectFile *
143430fdc8d8SChris Lattner Module::GetObjectFile()
143530fdc8d8SChris Lattner {
1436c5dac77aSEugene Zelenko     if (!m_did_load_objfile.load())
143788c05f54SGreg Clayton     {
143816ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(m_mutex);
1439c5dac77aSEugene Zelenko         if (!m_did_load_objfile.load())
144030fdc8d8SChris Lattner         {
1441*f343968fSZachary Turner             Timer scoped_timer(LLVM_PRETTY_FUNCTION,
144230fdc8d8SChris Lattner                                "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
14435ce9c565SGreg Clayton             DataBufferSP data_sp;
14445ce9c565SGreg Clayton             lldb::offset_t data_offset = 0;
14452540a8a7SGreg Clayton             const lldb::offset_t file_size = m_file.GetByteSize();
14462540a8a7SGreg Clayton             if (file_size > m_object_offset)
14472540a8a7SGreg Clayton             {
14482540a8a7SGreg Clayton                 m_did_load_objfile = true;
1449e72dfb32SGreg Clayton                 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
1450e72dfb32SGreg Clayton                                                        &m_file,
1451e72dfb32SGreg Clayton                                                        m_object_offset,
14522540a8a7SGreg Clayton                                                        file_size - m_object_offset,
14535ce9c565SGreg Clayton                                                        data_sp,
14545ce9c565SGreg Clayton                                                        data_offset);
1455593577a1SGreg Clayton                 if (m_objfile_sp)
1456593577a1SGreg Clayton                 {
1457593577a1SGreg Clayton                     // Once we get the object file, update our module with the object file's
1458593577a1SGreg Clayton                     // architecture since it might differ in vendor/os if some parts were
14595e6f4520SZachary Turner                     // unknown.  But since the matching arch might already be more specific
14605e6f4520SZachary Turner                     // than the generic COFF architecture, only merge in those values that
14615e6f4520SZachary Turner                     // overwrite unspecified unknown values.
14625e6f4520SZachary Turner                     ArchSpec new_arch;
14635e6f4520SZachary Turner                     m_objfile_sp->GetArchitecture(new_arch);
14645e6f4520SZachary Turner                     m_arch.MergeFrom(new_arch);
1465593577a1SGreg Clayton                 }
14660ee56ce6STodd Fiala                 else
14670ee56ce6STodd Fiala                 {
14680ee56ce6STodd Fiala                     ReportError ("failed to load objfile for %s", GetFileSpec().GetPath().c_str());
14690ee56ce6STodd Fiala                 }
147030fdc8d8SChris Lattner             }
14712540a8a7SGreg Clayton         }
147288c05f54SGreg Clayton     }
1473762f7135SGreg Clayton     return m_objfile_sp.get();
147430fdc8d8SChris Lattner }
147530fdc8d8SChris Lattner 
1476a7499c98SMichael Sartain SectionList *
14773046e668SGreg Clayton Module::GetSectionList()
14783046e668SGreg Clayton {
14793046e668SGreg Clayton     // Populate m_unified_sections_ap with sections from objfile.
1480c5dac77aSEugene Zelenko     if (!m_sections_ap)
14813046e668SGreg Clayton     {
14823046e668SGreg Clayton         ObjectFile *obj_file = GetObjectFile();
1483c5dac77aSEugene Zelenko         if (obj_file != nullptr)
14843046e668SGreg Clayton             obj_file->CreateSections(*GetUnifiedSectionList());
14853046e668SGreg Clayton     }
14863046e668SGreg Clayton     return m_sections_ap.get();
14873046e668SGreg Clayton }
14883046e668SGreg Clayton 
148905a09c67SJason Molenda void
149005a09c67SJason Molenda Module::SectionFileAddressesChanged ()
149105a09c67SJason Molenda {
149205a09c67SJason Molenda     ObjectFile *obj_file = GetObjectFile ();
149305a09c67SJason Molenda     if (obj_file)
149405a09c67SJason Molenda         obj_file->SectionFileAddressesChanged ();
149505a09c67SJason Molenda     SymbolVendor* sym_vendor = GetSymbolVendor();
1496c5dac77aSEugene Zelenko     if (sym_vendor != nullptr)
149705a09c67SJason Molenda         sym_vendor->SectionFileAddressesChanged ();
149805a09c67SJason Molenda }
149905a09c67SJason Molenda 
15003046e668SGreg Clayton SectionList *
1501a7499c98SMichael Sartain Module::GetUnifiedSectionList()
1502a7499c98SMichael Sartain {
15033046e668SGreg Clayton     // Populate m_unified_sections_ap with sections from objfile.
1504c5dac77aSEugene Zelenko     if (!m_sections_ap)
15053046e668SGreg Clayton         m_sections_ap.reset(new SectionList());
15063046e668SGreg Clayton     return m_sections_ap.get();
1507a7499c98SMichael Sartain }
150830fdc8d8SChris Lattner 
150930fdc8d8SChris Lattner const Symbol *
151030fdc8d8SChris Lattner Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
151130fdc8d8SChris Lattner {
1512*f343968fSZachary Turner     Timer scoped_timer(LLVM_PRETTY_FUNCTION,
151330fdc8d8SChris Lattner                        "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
151430fdc8d8SChris Lattner                        name.AsCString(),
151530fdc8d8SChris Lattner                        symbol_type);
1516a7499c98SMichael Sartain     SymbolVendor* sym_vendor = GetSymbolVendor();
1517a7499c98SMichael Sartain     if (sym_vendor)
151830fdc8d8SChris Lattner     {
1519a7499c98SMichael Sartain         Symtab *symtab = sym_vendor->GetSymtab();
152030fdc8d8SChris Lattner         if (symtab)
1521bcf2cfbdSGreg Clayton             return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
152230fdc8d8SChris Lattner     }
1523c5dac77aSEugene Zelenko     return nullptr;
152430fdc8d8SChris Lattner }
152530fdc8d8SChris Lattner void
152630fdc8d8SChris Lattner Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
152730fdc8d8SChris Lattner {
152830fdc8d8SChris Lattner     // No need to protect this call using m_mutex all other method calls are
152930fdc8d8SChris Lattner     // already thread safe.
153030fdc8d8SChris Lattner 
153130fdc8d8SChris Lattner     size_t num_indices = symbol_indexes.size();
153230fdc8d8SChris Lattner     if (num_indices > 0)
153330fdc8d8SChris Lattner     {
153430fdc8d8SChris Lattner         SymbolContext sc;
153530fdc8d8SChris Lattner         CalculateSymbolContext (&sc);
153630fdc8d8SChris Lattner         for (size_t i = 0; i < num_indices; i++)
153730fdc8d8SChris Lattner         {
153830fdc8d8SChris Lattner             sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
153930fdc8d8SChris Lattner             if (sc.symbol)
154030fdc8d8SChris Lattner                 sc_list.Append (sc);
154130fdc8d8SChris Lattner         }
154230fdc8d8SChris Lattner     }
154330fdc8d8SChris Lattner }
154430fdc8d8SChris Lattner 
154530fdc8d8SChris Lattner size_t
1546c1b2ccfdSGreg Clayton Module::FindFunctionSymbols (const ConstString &name,
1547c1b2ccfdSGreg Clayton                              uint32_t name_type_mask,
1548c1b2ccfdSGreg Clayton                              SymbolContextList& sc_list)
1549c1b2ccfdSGreg Clayton {
1550*f343968fSZachary Turner     Timer scoped_timer(LLVM_PRETTY_FUNCTION,
1551c1b2ccfdSGreg Clayton                        "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1552c1b2ccfdSGreg Clayton                        name.AsCString(),
1553c1b2ccfdSGreg Clayton                        name_type_mask);
1554a7499c98SMichael Sartain     SymbolVendor* sym_vendor = GetSymbolVendor();
1555a7499c98SMichael Sartain     if (sym_vendor)
1556c1b2ccfdSGreg Clayton     {
1557a7499c98SMichael Sartain         Symtab *symtab = sym_vendor->GetSymtab();
1558c1b2ccfdSGreg Clayton         if (symtab)
1559c1b2ccfdSGreg Clayton             return symtab->FindFunctionSymbols (name, name_type_mask, sc_list);
1560c1b2ccfdSGreg Clayton     }
1561c1b2ccfdSGreg Clayton     return 0;
1562c1b2ccfdSGreg Clayton }
1563c1b2ccfdSGreg Clayton 
1564c1b2ccfdSGreg Clayton size_t
1565b96ff33bSSean Callanan Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
156630fdc8d8SChris Lattner {
156730fdc8d8SChris Lattner     // No need to protect this call using m_mutex all other method calls are
156830fdc8d8SChris Lattner     // already thread safe.
156930fdc8d8SChris Lattner 
1570*f343968fSZachary Turner     Timer scoped_timer(LLVM_PRETTY_FUNCTION,
157130fdc8d8SChris Lattner                        "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
157230fdc8d8SChris Lattner                        name.AsCString(),
157330fdc8d8SChris Lattner                        symbol_type);
157430fdc8d8SChris Lattner     const size_t initial_size = sc_list.GetSize();
1575a7499c98SMichael Sartain     SymbolVendor* sym_vendor = GetSymbolVendor();
1576a7499c98SMichael Sartain     if (sym_vendor)
157730fdc8d8SChris Lattner     {
1578a7499c98SMichael Sartain         Symtab *symtab = sym_vendor->GetSymtab();
157930fdc8d8SChris Lattner         if (symtab)
158030fdc8d8SChris Lattner         {
158130fdc8d8SChris Lattner             std::vector<uint32_t> symbol_indexes;
158230fdc8d8SChris Lattner             symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
158330fdc8d8SChris Lattner             SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
158430fdc8d8SChris Lattner         }
158530fdc8d8SChris Lattner     }
158630fdc8d8SChris Lattner     return sc_list.GetSize() - initial_size;
158730fdc8d8SChris Lattner }
158830fdc8d8SChris Lattner 
158930fdc8d8SChris Lattner size_t
159030fdc8d8SChris Lattner Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
159130fdc8d8SChris Lattner {
159230fdc8d8SChris Lattner     // No need to protect this call using m_mutex all other method calls are
159330fdc8d8SChris Lattner     // already thread safe.
159430fdc8d8SChris Lattner 
1595*f343968fSZachary Turner     Timer scoped_timer(LLVM_PRETTY_FUNCTION,
159630fdc8d8SChris Lattner                        "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
159730fdc8d8SChris Lattner                        regex.GetText(),
159830fdc8d8SChris Lattner                        symbol_type);
159930fdc8d8SChris Lattner     const size_t initial_size = sc_list.GetSize();
1600a7499c98SMichael Sartain     SymbolVendor* sym_vendor = GetSymbolVendor();
1601a7499c98SMichael Sartain     if (sym_vendor)
160230fdc8d8SChris Lattner     {
1603a7499c98SMichael Sartain         Symtab *symtab = sym_vendor->GetSymtab();
160430fdc8d8SChris Lattner         if (symtab)
160530fdc8d8SChris Lattner         {
160630fdc8d8SChris Lattner             std::vector<uint32_t> symbol_indexes;
1607bcf2cfbdSGreg Clayton             symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
160830fdc8d8SChris Lattner             SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
160930fdc8d8SChris Lattner         }
161030fdc8d8SChris Lattner     }
161130fdc8d8SChris Lattner     return sc_list.GetSize() - initial_size;
161230fdc8d8SChris Lattner }
161330fdc8d8SChris Lattner 
1614e01e07b6SGreg Clayton void
1615e01e07b6SGreg Clayton Module::SetSymbolFileFileSpec (const FileSpec &file)
1616e01e07b6SGreg Clayton {
161790271672SGreg Clayton     if (!file.Exists())
161890271672SGreg Clayton         return;
1619a7499c98SMichael Sartain     if (m_symfile_ap)
1620a7499c98SMichael Sartain     {
162190271672SGreg Clayton         // Remove any sections in the unified section list that come from the current symbol vendor.
16223046e668SGreg Clayton         SectionList *section_list = GetSectionList();
1623a7499c98SMichael Sartain         SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile();
1624a7499c98SMichael Sartain         if (section_list && symbol_file)
1625a7499c98SMichael Sartain         {
1626a7499c98SMichael Sartain             ObjectFile *obj_file = symbol_file->GetObjectFile();
1627540fbbfaSGreg Clayton             // Make sure we have an object file and that the symbol vendor's objfile isn't
1628540fbbfaSGreg Clayton             // the same as the module's objfile before we remove any sections for it...
162990271672SGreg Clayton             if (obj_file)
163090271672SGreg Clayton             {
163190271672SGreg Clayton                 // Check to make sure we aren't trying to specify the file we already have
163290271672SGreg Clayton                 if (obj_file->GetFileSpec() == file)
163390271672SGreg Clayton                 {
163490271672SGreg Clayton                     // We are being told to add the exact same file that we already have
163590271672SGreg Clayton                     // we don't have to do anything.
163690271672SGreg Clayton                     return;
163790271672SGreg Clayton                 }
1638d00438e8STamas Berghammer 
1639d00438e8STamas Berghammer                 // Cleare the current symtab as we are going to replace it with a new one
1640d00438e8STamas Berghammer                 obj_file->ClearSymtab();
164190271672SGreg Clayton 
164290271672SGreg Clayton                 // The symbol file might be a directory bundle ("/tmp/a.out.dSYM") instead
164390271672SGreg Clayton                 // of a full path to the symbol file within the bundle
164490271672SGreg Clayton                 // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to check this
164590271672SGreg Clayton 
164690271672SGreg Clayton                 if (file.IsDirectory())
164790271672SGreg Clayton                 {
164890271672SGreg Clayton                     std::string new_path(file.GetPath());
164990271672SGreg Clayton                     std::string old_path(obj_file->GetFileSpec().GetPath());
165090271672SGreg Clayton                     if (old_path.find(new_path) == 0)
165190271672SGreg Clayton                     {
165290271672SGreg Clayton                         // We specified the same bundle as the symbol file that we already have
165390271672SGreg Clayton                         return;
165490271672SGreg Clayton                     }
165590271672SGreg Clayton                 }
165690271672SGreg Clayton 
165790271672SGreg Clayton                 if (obj_file != m_objfile_sp.get())
1658a7499c98SMichael Sartain                 {
1659a7499c98SMichael Sartain                     size_t num_sections = section_list->GetNumSections (0);
1660a7499c98SMichael Sartain                     for (size_t idx = num_sections; idx > 0; --idx)
1661a7499c98SMichael Sartain                     {
1662a7499c98SMichael Sartain                         lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1));
1663a7499c98SMichael Sartain                         if (section_sp->GetObjectFile() == obj_file)
1664a7499c98SMichael Sartain                         {
16653046e668SGreg Clayton                             section_list->DeleteSection (idx - 1);
1666a7499c98SMichael Sartain                         }
1667a7499c98SMichael Sartain                     }
1668a7499c98SMichael Sartain                 }
1669a7499c98SMichael Sartain             }
1670a7499c98SMichael Sartain         }
167190271672SGreg Clayton         // Keep all old symbol files around in case there are any lingering type references in
167290271672SGreg Clayton         // any SBValue objects that might have been handed out.
167390271672SGreg Clayton         m_old_symfiles.push_back(std::move(m_symfile_ap));
167490271672SGreg Clayton     }
1675e01e07b6SGreg Clayton     m_symfile_spec = file;
1676e01e07b6SGreg Clayton     m_symfile_ap.reset();
1677e01e07b6SGreg Clayton     m_did_load_symbol_vendor = false;
1678e01e07b6SGreg Clayton }
1679e01e07b6SGreg Clayton 
16805aee162fSJim Ingham bool
16815aee162fSJim Ingham Module::IsExecutable ()
16825aee162fSJim Ingham {
1683c5dac77aSEugene Zelenko     if (GetObjectFile() == nullptr)
16845aee162fSJim Ingham         return false;
16855aee162fSJim Ingham     else
16865aee162fSJim Ingham         return GetObjectFile()->IsExecutable();
16875aee162fSJim Ingham }
16885aee162fSJim Ingham 
16895aee162fSJim Ingham bool
1690b53cb271SJim Ingham Module::IsLoadedInTarget (Target *target)
1691b53cb271SJim Ingham {
1692b53cb271SJim Ingham     ObjectFile *obj_file = GetObjectFile();
1693b53cb271SJim Ingham     if (obj_file)
1694b53cb271SJim Ingham     {
16953046e668SGreg Clayton         SectionList *sections = GetSectionList();
1696c5dac77aSEugene Zelenko         if (sections != nullptr)
1697b53cb271SJim Ingham         {
1698b53cb271SJim Ingham             size_t num_sections = sections->GetSize();
1699b53cb271SJim Ingham             for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1700b53cb271SJim Ingham             {
1701b53cb271SJim Ingham                 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1702b53cb271SJim Ingham                 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1703b53cb271SJim Ingham                 {
1704b53cb271SJim Ingham                     return true;
1705b53cb271SJim Ingham                 }
1706b53cb271SJim Ingham             }
1707b53cb271SJim Ingham         }
1708b53cb271SJim Ingham     }
1709b53cb271SJim Ingham     return false;
1710b53cb271SJim Ingham }
17111759848bSEnrico Granata 
17121759848bSEnrico Granata bool
17139730339bSEnrico Granata Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream)
17141759848bSEnrico Granata {
17151759848bSEnrico Granata     if (!target)
17161759848bSEnrico Granata     {
17171759848bSEnrico Granata         error.SetErrorString("invalid destination Target");
17181759848bSEnrico Granata         return false;
17191759848bSEnrico Granata     }
17201759848bSEnrico Granata 
1721d93c4a33SBruce Mitchener     LoadScriptFromSymFile should_load = target->TargetProperties::GetLoadScriptFromSymbolFile();
17222ea43cdcSEnrico Granata 
1723994740fbSGreg Clayton     if (should_load == eLoadScriptFromSymFileFalse)
1724994740fbSGreg Clayton         return false;
1725994740fbSGreg Clayton 
172691c0e749SGreg Clayton     Debugger &debugger = target->GetDebugger();
172791c0e749SGreg Clayton     const ScriptLanguage script_language = debugger.GetScriptLanguage();
172891c0e749SGreg Clayton     if (script_language != eScriptLanguageNone)
172991c0e749SGreg Clayton     {
173091c0e749SGreg Clayton 
17311759848bSEnrico Granata         PlatformSP platform_sp(target->GetPlatform());
17321759848bSEnrico Granata 
17331759848bSEnrico Granata         if (!platform_sp)
17341759848bSEnrico Granata         {
17351759848bSEnrico Granata             error.SetErrorString("invalid Platform");
17361759848bSEnrico Granata             return false;
17371759848bSEnrico Granata         }
17381759848bSEnrico Granata 
173991c0e749SGreg Clayton         FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
1740fe7295dcSEnrico Granata                                                                                    *this,
1741fe7295dcSEnrico Granata                                                                                    feedback_stream);
174291c0e749SGreg Clayton 
174391c0e749SGreg Clayton         const uint32_t num_specs = file_specs.GetSize();
174491c0e749SGreg Clayton         if (num_specs)
174591c0e749SGreg Clayton         {
1746b9d8890bSGreg Clayton             ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1747b9d8890bSGreg Clayton             if (script_interpreter)
1748b9d8890bSGreg Clayton             {
174991c0e749SGreg Clayton                 for (uint32_t i = 0; i < num_specs; ++i)
175091c0e749SGreg Clayton                 {
175191c0e749SGreg Clayton                     FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
175291c0e749SGreg Clayton                     if (scripting_fspec && scripting_fspec.Exists())
175391c0e749SGreg Clayton                     {
1754d93c4a33SBruce Mitchener                         if (should_load == eLoadScriptFromSymFileWarn)
17552ea43cdcSEnrico Granata                         {
1756397ddd5fSEnrico Granata                             if (feedback_stream)
1757d516deb4SJim Ingham                                 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in "
1758d516deb4SJim Ingham                                                         "this debug session:\n\n    command script import \"%s\"\n\n"
1759d516deb4SJim Ingham                                                         "To run all discovered debug scripts in this session:\n\n"
1760d516deb4SJim Ingham                                                         "    settings set target.load-script-from-symbol-file true\n",
1761d516deb4SJim Ingham                                                         GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1762d516deb4SJim Ingham                                                         scripting_fspec.GetPath().c_str());
17632ea43cdcSEnrico Granata                             return false;
17642ea43cdcSEnrico Granata                         }
17651759848bSEnrico Granata                         StreamString scripting_stream;
17661759848bSEnrico Granata                         scripting_fspec.Dump(&scripting_stream);
1767e0c70f1bSEnrico Granata                         const bool can_reload = true;
17686a51085eSJim Ingham                         const bool init_lldb_globals = false;
1769d516deb4SJim Ingham                         bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(),
1770d516deb4SJim Ingham                                                                                 can_reload,
1771d516deb4SJim Ingham                                                                                 init_lldb_globals,
1772d516deb4SJim Ingham                                                                                 error);
17731759848bSEnrico Granata                         if (!did_load)
17741759848bSEnrico Granata                             return false;
17751759848bSEnrico Granata                     }
177691c0e749SGreg Clayton                 }
177791c0e749SGreg Clayton             }
17781759848bSEnrico Granata             else
17791759848bSEnrico Granata             {
17801759848bSEnrico Granata                 error.SetErrorString("invalid ScriptInterpreter");
17811759848bSEnrico Granata                 return false;
17821759848bSEnrico Granata             }
17831759848bSEnrico Granata         }
1784b9d8890bSGreg Clayton     }
17851759848bSEnrico Granata     return true;
17861759848bSEnrico Granata }
17871759848bSEnrico Granata 
1788b53cb271SJim Ingham bool
17895aee162fSJim Ingham Module::SetArchitecture (const ArchSpec &new_arch)
17905aee162fSJim Ingham {
179164195a2cSGreg Clayton     if (!m_arch.IsValid())
17925aee162fSJim Ingham     {
17935aee162fSJim Ingham         m_arch = new_arch;
17945aee162fSJim Ingham         return true;
17955aee162fSJim Ingham     }
1796b6cd5fe9SChaoren Lin     return m_arch.IsCompatibleMatch(new_arch);
17975aee162fSJim Ingham }
17985aee162fSJim Ingham 
1799c9660546SGreg Clayton bool
1800751caf65SGreg Clayton Module::SetLoadAddress (Target &target, lldb::addr_t value, bool value_is_offset, bool &changed)
1801c9660546SGreg Clayton {
18029e02dacdSSteve Pucci     ObjectFile *object_file = GetObjectFile();
1803c5dac77aSEugene Zelenko     if (object_file != nullptr)
1804c9660546SGreg Clayton     {
1805751caf65SGreg Clayton         changed = object_file->SetLoadAddress(target, value, value_is_offset);
18067524e090SGreg Clayton         return true;
18077524e090SGreg Clayton     }
18087524e090SGreg Clayton     else
18097524e090SGreg Clayton     {
18107524e090SGreg Clayton         changed = false;
1811c9660546SGreg Clayton     }
18129e02dacdSSteve Pucci     return false;
1813c9660546SGreg Clayton }
1814c9660546SGreg Clayton 
1815b9a01b39SGreg Clayton 
1816b9a01b39SGreg Clayton bool
1817b9a01b39SGreg Clayton Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1818b9a01b39SGreg Clayton {
1819b9a01b39SGreg Clayton     const UUID &uuid = module_ref.GetUUID();
1820b9a01b39SGreg Clayton 
1821b9a01b39SGreg Clayton     if (uuid.IsValid())
1822b9a01b39SGreg Clayton     {
1823b9a01b39SGreg Clayton         // If the UUID matches, then nothing more needs to match...
1824c5dac77aSEugene Zelenko         return (uuid == GetUUID());
1825b9a01b39SGreg Clayton     }
1826b9a01b39SGreg Clayton 
1827b9a01b39SGreg Clayton     const FileSpec &file_spec = module_ref.GetFileSpec();
1828b9a01b39SGreg Clayton     if (file_spec)
1829b9a01b39SGreg Clayton     {
1830980662eeSTamas Berghammer         if (!FileSpec::Equal (file_spec, m_file, (bool)file_spec.GetDirectory()) &&
1831980662eeSTamas Berghammer             !FileSpec::Equal (file_spec, m_platform_file, (bool)file_spec.GetDirectory()))
1832b9a01b39SGreg Clayton             return false;
1833b9a01b39SGreg Clayton     }
1834b9a01b39SGreg Clayton 
1835b9a01b39SGreg Clayton     const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1836b9a01b39SGreg Clayton     if (platform_file_spec)
1837b9a01b39SGreg Clayton     {
1838ddd7a2a6SSean Callanan         if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), (bool)platform_file_spec.GetDirectory()))
1839b9a01b39SGreg Clayton             return false;
1840b9a01b39SGreg Clayton     }
1841b9a01b39SGreg Clayton 
1842b9a01b39SGreg Clayton     const ArchSpec &arch = module_ref.GetArchitecture();
1843b9a01b39SGreg Clayton     if (arch.IsValid())
1844b9a01b39SGreg Clayton     {
1845bf4b7be6SSean Callanan         if (!m_arch.IsCompatibleMatch(arch))
1846b9a01b39SGreg Clayton             return false;
1847b9a01b39SGreg Clayton     }
1848b9a01b39SGreg Clayton 
1849b9a01b39SGreg Clayton     const ConstString &object_name = module_ref.GetObjectName();
1850b9a01b39SGreg Clayton     if (object_name)
1851b9a01b39SGreg Clayton     {
1852b9a01b39SGreg Clayton         if (object_name != GetObjectName())
1853b9a01b39SGreg Clayton             return false;
1854b9a01b39SGreg Clayton     }
1855b9a01b39SGreg Clayton     return true;
1856b9a01b39SGreg Clayton }
1857b9a01b39SGreg Clayton 
1858d804d285SGreg Clayton bool
1859d804d285SGreg Clayton Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1860d804d285SGreg Clayton {
186116ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1862d804d285SGreg Clayton     return m_source_mappings.FindFile (orig_spec, new_spec);
1863d804d285SGreg Clayton }
1864d804d285SGreg Clayton 
1865f9be6933SGreg Clayton bool
1866f9be6933SGreg Clayton Module::RemapSourceFile (const char *path, std::string &new_path) const
1867f9be6933SGreg Clayton {
186816ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1869f9be6933SGreg Clayton     return m_source_mappings.RemapPath(path, new_path);
1870f9be6933SGreg Clayton }
1871f9be6933SGreg Clayton 
18723467d80bSEnrico Granata uint32_t
18733467d80bSEnrico Granata Module::GetVersion (uint32_t *versions, uint32_t num_versions)
18743467d80bSEnrico Granata {
18753467d80bSEnrico Granata     ObjectFile *obj_file = GetObjectFile();
18763467d80bSEnrico Granata     if (obj_file)
18773467d80bSEnrico Granata         return obj_file->GetVersion (versions, num_versions);
18783467d80bSEnrico Granata 
1879c5dac77aSEugene Zelenko     if (versions != nullptr && num_versions != 0)
18803467d80bSEnrico Granata     {
18813467d80bSEnrico Granata         for (uint32_t i = 0; i < num_versions; ++i)
1882afcbdb15SEnrico Granata             versions[i] = LLDB_INVALID_MODULE_VERSION;
18833467d80bSEnrico Granata     }
18843467d80bSEnrico Granata     return 0;
18853467d80bSEnrico Granata }
188643fe217bSGreg Clayton 
188723f8c95aSGreg Clayton ModuleSP
188823f8c95aSGreg Clayton Module::CreateJITModule (const lldb::ObjectFileJITDelegateSP &delegate_sp)
188923f8c95aSGreg Clayton {
189023f8c95aSGreg Clayton     if (delegate_sp)
189123f8c95aSGreg Clayton     {
189223f8c95aSGreg Clayton         // Must create a module and place it into a shared pointer before
189323f8c95aSGreg Clayton         // we can create an object file since it has a std::weak_ptr back
189423f8c95aSGreg Clayton         // to the module, so we need to control the creation carefully in
189523f8c95aSGreg Clayton         // this static function
189623f8c95aSGreg Clayton         ModuleSP module_sp(new Module());
189723f8c95aSGreg Clayton         module_sp->m_objfile_sp.reset (new ObjectFileJIT (module_sp, delegate_sp));
189823f8c95aSGreg Clayton         if (module_sp->m_objfile_sp)
189923f8c95aSGreg Clayton         {
190023f8c95aSGreg Clayton             // Once we get the object file, update our module with the object file's
190123f8c95aSGreg Clayton             // architecture since it might differ in vendor/os if some parts were
190223f8c95aSGreg Clayton             // unknown.
190323f8c95aSGreg Clayton             module_sp->m_objfile_sp->GetArchitecture (module_sp->m_arch);
190423f8c95aSGreg Clayton         }
190523f8c95aSGreg Clayton         return module_sp;
190623f8c95aSGreg Clayton     }
190723f8c95aSGreg Clayton     return ModuleSP();
190823f8c95aSGreg Clayton }
190923f8c95aSGreg Clayton 
191008928f30SGreg Clayton bool
191108928f30SGreg Clayton Module::GetIsDynamicLinkEditor()
191208928f30SGreg Clayton {
191308928f30SGreg Clayton     ObjectFile * obj_file = GetObjectFile ();
191408928f30SGreg Clayton 
191508928f30SGreg Clayton     if (obj_file)
191608928f30SGreg Clayton         return obj_file->GetIsDynamicLinkEditor();
191708928f30SGreg Clayton 
191808928f30SGreg Clayton     return false;
191908928f30SGreg Clayton }
1920