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 
75*16ff8604SSaleem 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 
83*16ff8604SSaleem Abdulrasool     static std::recursive_mutex *g_module_collection_mutex = nullptr;
84c5dac77aSEugene Zelenko     if (g_module_collection_mutex == nullptr)
85*16ff8604SSaleem Abdulrasool         g_module_collection_mutex = new std::recursive_mutex; // NOTE: known leak
86*16ff8604SSaleem Abdulrasool     return *g_module_collection_mutex;
8765a03991SGreg Clayton }
8865a03991SGreg Clayton 
8965a03991SGreg Clayton size_t
9065a03991SGreg Clayton Module::GetNumberAllocatedModules ()
9165a03991SGreg Clayton {
92*16ff8604SSaleem 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 {
99*16ff8604SSaleem 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();
124d01b2953SDaniel Malea         printf ("%s: %" PRIu64 " modules:\n", __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 
143*16ff8604SSaleem Abdulrasool Module::Module(const ModuleSpec &module_spec)
144*16ff8604SSaleem 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     {
168*16ff8604SSaleem 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)
174*16ff8604SSaleem Abdulrasool         log->Printf("%p Module::Module((%s) '%s%s%s%s')", static_cast<void *>(this),
175*16ff8604SSaleem 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 
239*16ff8604SSaleem Abdulrasool Module::Module(const FileSpec &file_spec, const ArchSpec &arch, const ConstString *object_name,
240*16ff8604SSaleem Abdulrasool                lldb::offset_t object_offset, const TimeValue *object_mod_time_ptr)
241*16ff8604SSaleem 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     {
265*16ff8604SSaleem 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)
277*16ff8604SSaleem Abdulrasool         log->Printf("%p Module::Module((%s) '%s%s%s%s')", static_cast<void *>(this), m_arch.GetArchitectureName(),
278*16ff8604SSaleem Abdulrasool                     m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(",
279*16ff8604SSaleem Abdulrasool                     m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""), m_object_name.IsEmpty() ? "" : ")");
28030fdc8d8SChris Lattner }
28130fdc8d8SChris Lattner 
282*16ff8604SSaleem Abdulrasool Module::Module()
283*16ff8604SSaleem 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 {
305*16ff8604SSaleem 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
313*16ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
31465a03991SGreg Clayton     // Scope for locker below...
31565a03991SGreg Clayton     {
316*16ff8604SSaleem 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     {
351*16ff8604SSaleem 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     {
399*16ff8604SSaleem 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 {
423*16ff8604SSaleem 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 {
478*16ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
479324a1036SSaleem Abdulrasool     Timer scoped_timer(__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 {
491*16ff8604SSaleem 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 {
507*16ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
508d01b2953SDaniel Malea     Timer scoped_timer(__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 {
519*16ff8604SSaleem 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       ||
54530fdc8d8SChris Lattner             resolve_scope & eSymbolContextLineEntry   )
54630fdc8d8SChris Lattner         {
54738807141SAshok Thirumurthi             resolved_flags |= sym_vendor->ResolveSymbolContext (so_addr, resolve_scope, sc);
54830fdc8d8SChris Lattner         }
54930fdc8d8SChris Lattner 
550680e1778SJim Ingham         // Resolve the symbol if requested, but don't re-look it up if we've already found it.
551680e1778SJim Ingham         if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
55230fdc8d8SChris Lattner         {
553a7499c98SMichael Sartain             Symtab *symtab = sym_vendor->GetSymtab();
55438807141SAshok Thirumurthi             if (symtab && so_addr.IsSectionOffset())
55530fdc8d8SChris Lattner             {
5560d9dd7dfSMohit K. Bhakkad                 Symbol *matching_symbol = nullptr;
557c35b91ceSAdrian McCarthy 
558c35b91ceSAdrian McCarthy                 symtab->ForEachSymbolContainingFileAddress(so_addr.GetFileAddress(),
559c35b91ceSAdrian McCarthy                                                            [&matching_symbol](Symbol *symbol) -> bool {
5600d9dd7dfSMohit K. Bhakkad                                                                if (symbol->GetType() != eSymbolTypeInvalid)
5610d9dd7dfSMohit K. Bhakkad                                                                {
5620d9dd7dfSMohit K. Bhakkad                                                                    matching_symbol = symbol;
5630d9dd7dfSMohit K. Bhakkad                                                                    return false; // Stop iterating
5640d9dd7dfSMohit K. Bhakkad                                                                }
5650d9dd7dfSMohit K. Bhakkad                                                                return true; // Keep iterating
5660d9dd7dfSMohit K. Bhakkad                                                            });
5670d9dd7dfSMohit K. Bhakkad                 sc.symbol = matching_symbol;
56835729bb1SAshok Thirumurthi                 if (!sc.symbol &&
56935729bb1SAshok Thirumurthi                     resolve_scope & eSymbolContextFunction && !(resolved_flags & eSymbolContextFunction))
57035729bb1SAshok Thirumurthi                 {
57135729bb1SAshok Thirumurthi                     bool verify_unique = false; // No need to check again since ResolveSymbolContext failed to find a symbol at this address.
57235729bb1SAshok Thirumurthi                     if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
57335729bb1SAshok Thirumurthi                         sc.symbol = obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
57435729bb1SAshok Thirumurthi                 }
57535729bb1SAshok Thirumurthi 
57630fdc8d8SChris Lattner                 if (sc.symbol)
57793e2861bSGreg Clayton                 {
57893e2861bSGreg Clayton                     if (sc.symbol->IsSynthetic())
57993e2861bSGreg Clayton                     {
58093e2861bSGreg Clayton                         // We have a synthetic symbol so lets check if the object file
58193e2861bSGreg Clayton                         // from the symbol file in the symbol vendor is different than
58293e2861bSGreg Clayton                         // the object file for the module, and if so search its symbol
58393e2861bSGreg Clayton                         // table to see if we can come up with a better symbol. For example
58493e2861bSGreg Clayton                         // dSYM files on MacOSX have an unstripped symbol table inside of
58593e2861bSGreg Clayton                         // them.
58693e2861bSGreg Clayton                         ObjectFile *symtab_objfile = symtab->GetObjectFile();
58793e2861bSGreg Clayton                         if (symtab_objfile && symtab_objfile->IsStripped())
58893e2861bSGreg Clayton                         {
58993e2861bSGreg Clayton                             SymbolFile *symfile = sym_vendor->GetSymbolFile();
59093e2861bSGreg Clayton                             if (symfile)
59193e2861bSGreg Clayton                             {
59293e2861bSGreg Clayton                                 ObjectFile *symfile_objfile = symfile->GetObjectFile();
59393e2861bSGreg Clayton                                 if (symfile_objfile != symtab_objfile)
59493e2861bSGreg Clayton                                 {
59593e2861bSGreg Clayton                                     Symtab *symfile_symtab = symfile_objfile->GetSymtab();
59693e2861bSGreg Clayton                                     if (symfile_symtab)
59793e2861bSGreg Clayton                                     {
59893e2861bSGreg Clayton                                         Symbol *symbol = symfile_symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
59993e2861bSGreg Clayton                                         if (symbol && !symbol->IsSynthetic())
60093e2861bSGreg Clayton                                         {
60193e2861bSGreg Clayton                                             sc.symbol = symbol;
60293e2861bSGreg Clayton                                         }
60393e2861bSGreg Clayton                                     }
60493e2861bSGreg Clayton                                 }
60593e2861bSGreg Clayton                             }
60693e2861bSGreg Clayton                         }
60793e2861bSGreg Clayton                     }
60830fdc8d8SChris Lattner                     resolved_flags |= eSymbolContextSymbol;
60930fdc8d8SChris Lattner                 }
61030fdc8d8SChris Lattner             }
61193e2861bSGreg Clayton         }
61238807141SAshok Thirumurthi 
61338807141SAshok Thirumurthi         // For function symbols, so_addr may be off by one.  This is a convention consistent
61438807141SAshok Thirumurthi         // with FDE row indices in eh_frame sections, but requires extra logic here to permit
61538807141SAshok Thirumurthi         // symbol lookup for disassembly and unwind.
61638807141SAshok Thirumurthi         if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol) &&
61735729bb1SAshok Thirumurthi             resolve_tail_call_address && so_addr.IsSectionOffset())
61838807141SAshok Thirumurthi         {
61938807141SAshok Thirumurthi             Address previous_addr = so_addr;
620edfaae39SGreg Clayton             previous_addr.Slide(-1);
62138807141SAshok Thirumurthi 
62235729bb1SAshok Thirumurthi             bool do_resolve_tail_call_address = false; // prevent recursion
62335729bb1SAshok Thirumurthi             const uint32_t flags = ResolveSymbolContextForAddress(previous_addr, resolve_scope, sc,
62435729bb1SAshok Thirumurthi                                                                   do_resolve_tail_call_address);
62538807141SAshok Thirumurthi             if (flags & eSymbolContextSymbol)
62638807141SAshok Thirumurthi             {
62738807141SAshok Thirumurthi                 AddressRange addr_range;
62838807141SAshok Thirumurthi                 if (sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
62938807141SAshok Thirumurthi                 {
63038807141SAshok Thirumurthi                     if (addr_range.GetBaseAddress().GetSection() == so_addr.GetSection())
63138807141SAshok Thirumurthi                     {
63238807141SAshok Thirumurthi                         // If the requested address is one past the address range of a function (i.e. a tail call),
63338807141SAshok Thirumurthi                         // or the decremented address is the start of a function (i.e. some forms of trampoline),
63438807141SAshok Thirumurthi                         // indicate that the symbol has been resolved.
63538807141SAshok Thirumurthi                         if (so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() ||
63638807141SAshok Thirumurthi                             so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() + addr_range.GetByteSize())
63738807141SAshok Thirumurthi                         {
63838807141SAshok Thirumurthi                             resolved_flags |= flags;
63938807141SAshok Thirumurthi                         }
64038807141SAshok Thirumurthi                     }
64138807141SAshok Thirumurthi                     else
64238807141SAshok Thirumurthi                     {
64338807141SAshok Thirumurthi                         sc.symbol = nullptr; // Don't trust the symbol if the sections didn't match.
64438807141SAshok Thirumurthi                     }
64538807141SAshok Thirumurthi                 }
64630fdc8d8SChris Lattner             }
64730fdc8d8SChris Lattner         }
64830fdc8d8SChris Lattner     }
64930fdc8d8SChris Lattner     return resolved_flags;
65030fdc8d8SChris Lattner }
65130fdc8d8SChris Lattner 
65230fdc8d8SChris Lattner uint32_t
653274060b6SGreg Clayton Module::ResolveSymbolContextForFilePath
654274060b6SGreg Clayton (
655274060b6SGreg Clayton     const char *file_path,
656274060b6SGreg Clayton     uint32_t line,
657274060b6SGreg Clayton     bool check_inlines,
658274060b6SGreg Clayton     uint32_t resolve_scope,
659274060b6SGreg Clayton     SymbolContextList& sc_list
660274060b6SGreg Clayton )
66130fdc8d8SChris Lattner {
662274060b6SGreg Clayton     FileSpec file_spec(file_path, false);
66330fdc8d8SChris Lattner     return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
66430fdc8d8SChris Lattner }
66530fdc8d8SChris Lattner 
66630fdc8d8SChris Lattner uint32_t
66730fdc8d8SChris Lattner Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
66830fdc8d8SChris Lattner {
669*16ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
67030fdc8d8SChris Lattner     Timer scoped_timer(__PRETTY_FUNCTION__,
671b5ad4ec7SGreg Clayton                        "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
672b5ad4ec7SGreg Clayton                        file_spec.GetPath().c_str(),
67330fdc8d8SChris Lattner                        line,
67430fdc8d8SChris Lattner                        check_inlines ? "yes" : "no",
67530fdc8d8SChris Lattner                        resolve_scope);
67630fdc8d8SChris Lattner 
67730fdc8d8SChris Lattner     const uint32_t initial_count = sc_list.GetSize();
67830fdc8d8SChris Lattner 
67930fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor  ();
68030fdc8d8SChris Lattner     if (symbols)
68130fdc8d8SChris Lattner         symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
68230fdc8d8SChris Lattner 
68330fdc8d8SChris Lattner     return sc_list.GetSize() - initial_count;
68430fdc8d8SChris Lattner }
68530fdc8d8SChris Lattner 
686c7bece56SGreg Clayton size_t
687c7bece56SGreg Clayton Module::FindGlobalVariables (const ConstString &name,
68899558cc4SGreg Clayton                              const CompilerDeclContext *parent_decl_ctx,
689c7bece56SGreg Clayton                              bool append,
690c7bece56SGreg Clayton                              size_t max_matches,
691c7bece56SGreg Clayton                              VariableList& variables)
69230fdc8d8SChris Lattner {
69330fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
69430fdc8d8SChris Lattner     if (symbols)
69599558cc4SGreg Clayton         return symbols->FindGlobalVariables(name, parent_decl_ctx, append, max_matches, variables);
69630fdc8d8SChris Lattner     return 0;
69730fdc8d8SChris Lattner }
698c7bece56SGreg Clayton 
699c7bece56SGreg Clayton size_t
700c7bece56SGreg Clayton Module::FindGlobalVariables (const RegularExpression& regex,
701c7bece56SGreg Clayton                              bool append,
702c7bece56SGreg Clayton                              size_t max_matches,
703c7bece56SGreg Clayton                              VariableList& variables)
70430fdc8d8SChris Lattner {
70530fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
70630fdc8d8SChris Lattner     if (symbols)
70730fdc8d8SChris Lattner         return symbols->FindGlobalVariables(regex, append, max_matches, variables);
70830fdc8d8SChris Lattner     return 0;
70930fdc8d8SChris Lattner }
71030fdc8d8SChris Lattner 
711c7bece56SGreg Clayton size_t
712644247c1SGreg Clayton Module::FindCompileUnits (const FileSpec &path,
713644247c1SGreg Clayton                           bool append,
714644247c1SGreg Clayton                           SymbolContextList &sc_list)
715644247c1SGreg Clayton {
716644247c1SGreg Clayton     if (!append)
717644247c1SGreg Clayton         sc_list.Clear();
718644247c1SGreg Clayton 
719c7bece56SGreg Clayton     const size_t start_size = sc_list.GetSize();
720c7bece56SGreg Clayton     const size_t num_compile_units = GetNumCompileUnits();
721644247c1SGreg Clayton     SymbolContext sc;
722e1cd1be6SGreg Clayton     sc.module_sp = shared_from_this();
723ddd7a2a6SSean Callanan     const bool compare_directory = (bool)path.GetDirectory();
724c7bece56SGreg Clayton     for (size_t i = 0; i < num_compile_units; ++i)
725644247c1SGreg Clayton     {
726644247c1SGreg Clayton         sc.comp_unit = GetCompileUnitAtIndex(i).get();
7272dafd8edSGreg Clayton         if (sc.comp_unit)
7282dafd8edSGreg Clayton         {
729644247c1SGreg Clayton             if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
730644247c1SGreg Clayton                 sc_list.Append(sc);
731644247c1SGreg Clayton         }
7322dafd8edSGreg Clayton     }
733644247c1SGreg Clayton     return sc_list.GetSize() - start_size;
734644247c1SGreg Clayton }
735644247c1SGreg Clayton 
736c7bece56SGreg Clayton size_t
737931180e6SGreg Clayton Module::FindFunctions (const ConstString &name,
73899558cc4SGreg Clayton                        const CompilerDeclContext *parent_decl_ctx,
739931180e6SGreg Clayton                        uint32_t name_type_mask,
740931180e6SGreg Clayton                        bool include_symbols,
7419df05fbbSSean Callanan                        bool include_inlines,
742931180e6SGreg Clayton                        bool append,
743931180e6SGreg Clayton                        SymbolContextList& sc_list)
74430fdc8d8SChris Lattner {
745931180e6SGreg Clayton     if (!append)
746931180e6SGreg Clayton         sc_list.Clear();
747931180e6SGreg Clayton 
74843fe217bSGreg Clayton     const size_t old_size = sc_list.GetSize();
749931180e6SGreg Clayton 
750931180e6SGreg Clayton     // Find all the functions (not symbols, but debug information functions...
75130fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
75243fe217bSGreg Clayton 
75343fe217bSGreg Clayton     if (name_type_mask & eFunctionNameTypeAuto)
75443fe217bSGreg Clayton     {
75543fe217bSGreg Clayton         ConstString lookup_name;
75643fe217bSGreg Clayton         uint32_t lookup_name_type_mask = 0;
75743fe217bSGreg Clayton         bool match_name_after_lookup = false;
75843fe217bSGreg Clayton         Module::PrepareForFunctionNameLookup (name,
75943fe217bSGreg Clayton                                               name_type_mask,
76023b1decbSDawn Perchik                                               eLanguageTypeUnknown, // TODO: add support
76143fe217bSGreg Clayton                                               lookup_name,
76243fe217bSGreg Clayton                                               lookup_name_type_mask,
76343fe217bSGreg Clayton                                               match_name_after_lookup);
76443fe217bSGreg Clayton 
76543fe217bSGreg Clayton         if (symbols)
766a7499c98SMichael Sartain         {
76743fe217bSGreg Clayton             symbols->FindFunctions(lookup_name,
76899558cc4SGreg Clayton                                    parent_decl_ctx,
76943fe217bSGreg Clayton                                    lookup_name_type_mask,
77043fe217bSGreg Clayton                                    include_inlines,
77143fe217bSGreg Clayton                                    append,
77243fe217bSGreg Clayton                                    sc_list);
77343fe217bSGreg Clayton 
77443fe217bSGreg Clayton             // Now check our symbol table for symbols that are code symbols if requested
77543fe217bSGreg Clayton             if (include_symbols)
77643fe217bSGreg Clayton             {
777a7499c98SMichael Sartain                 Symtab *symtab = symbols->GetSymtab();
77843fe217bSGreg Clayton                 if (symtab)
77943fe217bSGreg Clayton                     symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list);
78043fe217bSGreg Clayton             }
78143fe217bSGreg Clayton         }
78243fe217bSGreg Clayton 
78343fe217bSGreg Clayton         if (match_name_after_lookup)
78443fe217bSGreg Clayton         {
78543fe217bSGreg Clayton             SymbolContext sc;
78643fe217bSGreg Clayton             size_t i = old_size;
78743fe217bSGreg Clayton             while (i < sc_list.GetSize())
78843fe217bSGreg Clayton             {
78943fe217bSGreg Clayton                 if (sc_list.GetContextAtIndex(i, sc))
79043fe217bSGreg Clayton                 {
79143fe217bSGreg Clayton                     const char *func_name = sc.GetFunctionName().GetCString();
792c5dac77aSEugene Zelenko                     if (func_name && strstr(func_name, name.GetCString()) == nullptr)
79343fe217bSGreg Clayton                     {
79443fe217bSGreg Clayton                         // Remove the current context
79543fe217bSGreg Clayton                         sc_list.RemoveContextAtIndex(i);
79643fe217bSGreg Clayton                         // Don't increment i and continue in the loop
79743fe217bSGreg Clayton                         continue;
79843fe217bSGreg Clayton                     }
79943fe217bSGreg Clayton                 }
80043fe217bSGreg Clayton                 ++i;
80143fe217bSGreg Clayton             }
80243fe217bSGreg Clayton         }
80343fe217bSGreg Clayton     }
80443fe217bSGreg Clayton     else
80543fe217bSGreg Clayton     {
80630fdc8d8SChris Lattner         if (symbols)
807a7499c98SMichael Sartain         {
80899558cc4SGreg Clayton             symbols->FindFunctions(name, parent_decl_ctx, name_type_mask, include_inlines, append, sc_list);
809931180e6SGreg Clayton 
810931180e6SGreg Clayton             // Now check our symbol table for symbols that are code symbols if requested
811931180e6SGreg Clayton             if (include_symbols)
812931180e6SGreg Clayton             {
813a7499c98SMichael Sartain                 Symtab *symtab = symbols->GetSymtab();
814931180e6SGreg Clayton                 if (symtab)
81543fe217bSGreg Clayton                     symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
816931180e6SGreg Clayton             }
817931180e6SGreg Clayton         }
818931180e6SGreg Clayton     }
81943fe217bSGreg Clayton 
82043fe217bSGreg Clayton     return sc_list.GetSize() - old_size;
82130fdc8d8SChris Lattner }
82230fdc8d8SChris Lattner 
823c7bece56SGreg Clayton size_t
824931180e6SGreg Clayton Module::FindFunctions (const RegularExpression& regex,
825931180e6SGreg Clayton                        bool include_symbols,
8269df05fbbSSean Callanan                        bool include_inlines,
827931180e6SGreg Clayton                        bool append,
828931180e6SGreg Clayton                        SymbolContextList& sc_list)
82930fdc8d8SChris Lattner {
830931180e6SGreg Clayton     if (!append)
831931180e6SGreg Clayton         sc_list.Clear();
832931180e6SGreg Clayton 
833c7bece56SGreg Clayton     const size_t start_size = sc_list.GetSize();
834931180e6SGreg Clayton 
83530fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
83630fdc8d8SChris Lattner     if (symbols)
837a7499c98SMichael Sartain     {
8389df05fbbSSean Callanan         symbols->FindFunctions(regex, include_inlines, append, sc_list);
839a7499c98SMichael Sartain 
840931180e6SGreg Clayton         // Now check our symbol table for symbols that are code symbols if requested
841931180e6SGreg Clayton         if (include_symbols)
842931180e6SGreg Clayton         {
843a7499c98SMichael Sartain             Symtab *symtab = symbols->GetSymtab();
844931180e6SGreg Clayton             if (symtab)
845931180e6SGreg Clayton             {
846931180e6SGreg Clayton                 std::vector<uint32_t> symbol_indexes;
84700049b8bSMatt Kopec                 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
848c7bece56SGreg Clayton                 const size_t num_matches = symbol_indexes.size();
849931180e6SGreg Clayton                 if (num_matches)
850931180e6SGreg Clayton                 {
851931180e6SGreg Clayton                     SymbolContext sc(this);
852d8cf1a11SGreg Clayton                     const size_t end_functions_added_index = sc_list.GetSize();
853d8cf1a11SGreg Clayton                     size_t num_functions_added_to_sc_list = end_functions_added_index - start_size;
854d8cf1a11SGreg Clayton                     if (num_functions_added_to_sc_list == 0)
855d8cf1a11SGreg Clayton                     {
856d8cf1a11SGreg Clayton                         // No functions were added, just symbols, so we can just append them
857d8cf1a11SGreg Clayton                         for (size_t i = 0; i < num_matches; ++i)
858931180e6SGreg Clayton                         {
859931180e6SGreg Clayton                             sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
86000049b8bSMatt Kopec                             SymbolType sym_type = sc.symbol->GetType();
86100049b8bSMatt Kopec                             if (sc.symbol && (sym_type == eSymbolTypeCode ||
86200049b8bSMatt Kopec                                               sym_type == eSymbolTypeResolver))
863d8cf1a11SGreg Clayton                                 sc_list.Append(sc);
864d8cf1a11SGreg Clayton                         }
865d8cf1a11SGreg Clayton                     }
866d8cf1a11SGreg Clayton                     else
867d8cf1a11SGreg Clayton                     {
868d8cf1a11SGreg Clayton                         typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
869d8cf1a11SGreg Clayton                         FileAddrToIndexMap file_addr_to_index;
870d8cf1a11SGreg Clayton                         for (size_t i = start_size; i < end_functions_added_index; ++i)
871d8cf1a11SGreg Clayton                         {
872d8cf1a11SGreg Clayton                             const SymbolContext &sc = sc_list[i];
873d8cf1a11SGreg Clayton                             if (sc.block)
874d8cf1a11SGreg Clayton                                 continue;
875d8cf1a11SGreg Clayton                             file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i;
876d8cf1a11SGreg Clayton                         }
877d8cf1a11SGreg Clayton 
878d8cf1a11SGreg Clayton                         FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
879d8cf1a11SGreg Clayton                         // Functions were added so we need to merge symbols into any
880d8cf1a11SGreg Clayton                         // existing function symbol contexts
881d8cf1a11SGreg Clayton                         for (size_t i = start_size; i < num_matches; ++i)
882d8cf1a11SGreg Clayton                         {
883d8cf1a11SGreg Clayton                             sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
884d8cf1a11SGreg Clayton                             SymbolType sym_type = sc.symbol->GetType();
885358cf1eaSGreg Clayton                             if (sc.symbol && sc.symbol->ValueIsAddress() && (sym_type == eSymbolTypeCode || sym_type == eSymbolTypeResolver))
886d8cf1a11SGreg Clayton                             {
887358cf1eaSGreg Clayton                                 FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddressRef().GetFileAddress());
888d8cf1a11SGreg Clayton                                 if (pos == end)
889d8cf1a11SGreg Clayton                                     sc_list.Append(sc);
890d8cf1a11SGreg Clayton                                 else
891d8cf1a11SGreg Clayton                                     sc_list[pos->second].symbol = sc.symbol;
892d8cf1a11SGreg Clayton                             }
893d8cf1a11SGreg Clayton                         }
894931180e6SGreg Clayton                     }
895931180e6SGreg Clayton                 }
896931180e6SGreg Clayton             }
897931180e6SGreg Clayton         }
898931180e6SGreg Clayton     }
899931180e6SGreg Clayton     return sc_list.GetSize() - start_size;
90030fdc8d8SChris Lattner }
90130fdc8d8SChris Lattner 
902f86248d9SRichard Mitton void
903f86248d9SRichard Mitton Module::FindAddressesForLine (const lldb::TargetSP target_sp,
904f86248d9SRichard Mitton                               const FileSpec &file, uint32_t line,
905f86248d9SRichard Mitton                               Function *function,
906f86248d9SRichard Mitton                               std::vector<Address> &output_local, std::vector<Address> &output_extern)
907f86248d9SRichard Mitton {
908f86248d9SRichard Mitton     SearchFilterByModule filter(target_sp, m_file);
909f86248d9SRichard Mitton     AddressResolverFileLine resolver(file, line, true);
910f86248d9SRichard Mitton     resolver.ResolveAddress (filter);
911f86248d9SRichard Mitton 
912f86248d9SRichard Mitton     for (size_t n = 0; n < resolver.GetNumberOfAddresses(); n++)
913f86248d9SRichard Mitton     {
914f86248d9SRichard Mitton         Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
915f86248d9SRichard Mitton         Function *f = addr.CalculateSymbolContextFunction();
916f86248d9SRichard Mitton         if (f && f == function)
917f86248d9SRichard Mitton             output_local.push_back (addr);
918f86248d9SRichard Mitton         else
919f86248d9SRichard Mitton             output_extern.push_back (addr);
920f86248d9SRichard Mitton     }
921f86248d9SRichard Mitton }
922f86248d9SRichard Mitton 
923c7bece56SGreg Clayton size_t
92484db9105SGreg Clayton Module::FindTypes_Impl (const SymbolContext& sc,
92584db9105SGreg Clayton                         const ConstString &name,
92699558cc4SGreg Clayton                         const CompilerDeclContext *parent_decl_ctx,
92784db9105SGreg Clayton                         bool append,
928c7bece56SGreg Clayton                         size_t max_matches,
929ae088e52SGreg Clayton                         llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
9304069730cSRavitheja Addepally                         TypeMap& types)
9313504eee8SGreg Clayton {
9323504eee8SGreg Clayton     Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
933c5dac77aSEugene Zelenko     if (!sc.module_sp || sc.module_sp.get() == this)
9343504eee8SGreg Clayton     {
9353504eee8SGreg Clayton         SymbolVendor *symbols = GetSymbolVendor ();
9363504eee8SGreg Clayton         if (symbols)
937ae088e52SGreg Clayton             return symbols->FindTypes(sc, name, parent_decl_ctx, append, max_matches, searched_symbol_files, types);
9383504eee8SGreg Clayton     }
9393504eee8SGreg Clayton     return 0;
9403504eee8SGreg Clayton }
9413504eee8SGreg Clayton 
942c7bece56SGreg Clayton size_t
94384db9105SGreg Clayton Module::FindTypesInNamespace (const SymbolContext& sc,
94484db9105SGreg Clayton                               const ConstString &type_name,
94599558cc4SGreg Clayton                               const CompilerDeclContext *parent_decl_ctx,
946c7bece56SGreg Clayton                               size_t max_matches,
94784db9105SGreg Clayton                               TypeList& type_list)
9486f3533fbSEnrico Granata {
94984db9105SGreg Clayton     const bool append = true;
9504069730cSRavitheja Addepally     TypeMap types_map;
951ae088e52SGreg Clayton     llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
952ae088e52SGreg Clayton     size_t num_types = FindTypes_Impl(sc, type_name, parent_decl_ctx, append, max_matches, searched_symbol_files, types_map);
9534069730cSRavitheja Addepally     if (num_types > 0)
9544069730cSRavitheja Addepally         sc.SortTypeList(types_map, type_list);
9554069730cSRavitheja Addepally     return num_types;
9566f3533fbSEnrico Granata }
9576f3533fbSEnrico Granata 
958b43165b7SGreg Clayton lldb::TypeSP
959b43165b7SGreg Clayton Module::FindFirstType (const SymbolContext& sc,
960b43165b7SGreg Clayton                        const ConstString &name,
961b43165b7SGreg Clayton                        bool exact_match)
962b43165b7SGreg Clayton {
963b43165b7SGreg Clayton     TypeList type_list;
964ae088e52SGreg Clayton     llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
965ae088e52SGreg Clayton     const size_t num_matches = FindTypes (sc, name, exact_match, 1, searched_symbol_files, type_list);
966b43165b7SGreg Clayton     if (num_matches)
967b43165b7SGreg Clayton         return type_list.GetTypeAtIndex(0);
968b43165b7SGreg Clayton     return TypeSP();
969b43165b7SGreg Clayton }
970b43165b7SGreg Clayton 
971c7bece56SGreg Clayton size_t
97284db9105SGreg Clayton Module::FindTypes (const SymbolContext& sc,
97384db9105SGreg Clayton                    const ConstString &name,
97484db9105SGreg Clayton                    bool exact_match,
975c7bece56SGreg Clayton                    size_t max_matches,
976ae088e52SGreg Clayton                    llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
97784db9105SGreg Clayton                    TypeList& types)
9786f3533fbSEnrico Granata {
979c7bece56SGreg Clayton     size_t num_matches = 0;
98084db9105SGreg Clayton     const char *type_name_cstr = name.GetCString();
98184db9105SGreg Clayton     std::string type_scope;
98284db9105SGreg Clayton     std::string type_basename;
98384db9105SGreg Clayton     const bool append = true;
9847bc31332SGreg Clayton     TypeClass type_class = eTypeClassAny;
9854069730cSRavitheja Addepally     TypeMap typesmap;
9867bc31332SGreg Clayton     if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
98784db9105SGreg Clayton     {
98884db9105SGreg Clayton         // Check if "name" starts with "::" which means the qualified type starts
98984db9105SGreg Clayton         // from the root namespace and implies and exact match. The typenames we
99084db9105SGreg Clayton         // get back from clang do not start with "::" so we need to strip this off
991d93c4a33SBruce Mitchener         // in order to get the qualified names to match
9926f3533fbSEnrico Granata 
99384db9105SGreg Clayton         if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
9946f3533fbSEnrico Granata         {
99584db9105SGreg Clayton             type_scope.erase(0,2);
99684db9105SGreg Clayton             exact_match = true;
99784db9105SGreg Clayton         }
99884db9105SGreg Clayton         ConstString type_basename_const_str (type_basename.c_str());
999c5dac77aSEugene Zelenko         if (FindTypes_Impl(sc, type_basename_const_str, nullptr, append, max_matches, searched_symbol_files, typesmap))
100084db9105SGreg Clayton         {
10014069730cSRavitheja Addepally             typesmap.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
10024069730cSRavitheja Addepally             num_matches = typesmap.GetSize();
10036f3533fbSEnrico Granata         }
100484db9105SGreg Clayton     }
100584db9105SGreg Clayton     else
100684db9105SGreg Clayton     {
100784db9105SGreg Clayton         // The type is not in a namespace/class scope, just search for it by basename
10087bc31332SGreg Clayton         if (type_class != eTypeClassAny)
10097bc31332SGreg Clayton         {
10107bc31332SGreg Clayton             // The "type_name_cstr" will have been modified if we have a valid type class
10117bc31332SGreg Clayton             // prefix (like "struct", "class", "union", "typedef" etc).
1012c5dac77aSEugene Zelenko             FindTypes_Impl(sc, ConstString(type_name_cstr), nullptr, append, max_matches, searched_symbol_files, typesmap);
10134069730cSRavitheja Addepally             typesmap.RemoveMismatchedTypes (type_class);
10144069730cSRavitheja Addepally             num_matches = typesmap.GetSize();
10157bc31332SGreg Clayton         }
10167bc31332SGreg Clayton         else
10177bc31332SGreg Clayton         {
1018c5dac77aSEugene Zelenko             num_matches = FindTypes_Impl(sc, name, nullptr, append, max_matches, searched_symbol_files, typesmap);
101984db9105SGreg Clayton         }
10207bc31332SGreg Clayton     }
10214069730cSRavitheja Addepally     if (num_matches > 0)
10224069730cSRavitheja Addepally         sc.SortTypeList(typesmap, types);
102384db9105SGreg Clayton     return num_matches;
10246f3533fbSEnrico Granata }
10256f3533fbSEnrico Granata 
102630fdc8d8SChris Lattner SymbolVendor*
1027136dff87SGreg Clayton Module::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
102830fdc8d8SChris Lattner {
1029c5dac77aSEugene Zelenko     if (!m_did_load_symbol_vendor.load())
103088c05f54SGreg Clayton     {
1031*16ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(m_mutex);
1032c5dac77aSEugene Zelenko         if (!m_did_load_symbol_vendor.load() && can_create)
103330fdc8d8SChris Lattner         {
103430fdc8d8SChris Lattner             ObjectFile *obj_file = GetObjectFile ();
1035c5dac77aSEugene Zelenko             if (obj_file != nullptr)
103630fdc8d8SChris Lattner             {
103730fdc8d8SChris Lattner                 Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
1038136dff87SGreg Clayton                 m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
1039e83e731eSGreg Clayton                 m_did_load_symbol_vendor = true;
104030fdc8d8SChris Lattner             }
104130fdc8d8SChris Lattner         }
104288c05f54SGreg Clayton     }
104330fdc8d8SChris Lattner     return m_symfile_ap.get();
104430fdc8d8SChris Lattner }
104530fdc8d8SChris Lattner 
104630fdc8d8SChris Lattner void
104730fdc8d8SChris Lattner Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
104830fdc8d8SChris Lattner {
104930fdc8d8SChris Lattner     // Container objects whose paths do not specify a file directly can call
105030fdc8d8SChris Lattner     // this function to correct the file and object names.
105130fdc8d8SChris Lattner     m_file = file;
105230fdc8d8SChris Lattner     m_mod_time = file.GetModificationTime();
105330fdc8d8SChris Lattner     m_object_name = object_name;
105430fdc8d8SChris Lattner }
105530fdc8d8SChris Lattner 
105630fdc8d8SChris Lattner const ArchSpec&
105730fdc8d8SChris Lattner Module::GetArchitecture () const
105830fdc8d8SChris Lattner {
105930fdc8d8SChris Lattner     return m_arch;
106030fdc8d8SChris Lattner }
106130fdc8d8SChris Lattner 
1062b5ad4ec7SGreg Clayton std::string
1063b5ad4ec7SGreg Clayton Module::GetSpecificationDescription () const
1064b5ad4ec7SGreg Clayton {
1065b5ad4ec7SGreg Clayton     std::string spec(GetFileSpec().GetPath());
1066b5ad4ec7SGreg Clayton     if (m_object_name)
1067b5ad4ec7SGreg Clayton     {
1068b5ad4ec7SGreg Clayton         spec += '(';
1069b5ad4ec7SGreg Clayton         spec += m_object_name.GetCString();
1070b5ad4ec7SGreg Clayton         spec += ')';
1071b5ad4ec7SGreg Clayton     }
1072b5ad4ec7SGreg Clayton     return spec;
1073b5ad4ec7SGreg Clayton }
1074b5ad4ec7SGreg Clayton 
107530fdc8d8SChris Lattner void
1076c982b3d6SGreg Clayton Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
1077ceb6b139SCaroline Tice {
1078*16ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1079ceb6b139SCaroline Tice 
1080c982b3d6SGreg Clayton     if (level >= eDescriptionLevelFull)
1081c982b3d6SGreg Clayton     {
1082cfd1acedSGreg Clayton         if (m_arch.IsValid())
108364195a2cSGreg Clayton             s->Printf("(%s) ", m_arch.GetArchitectureName());
1084c982b3d6SGreg Clayton     }
1085ceb6b139SCaroline Tice 
1086c982b3d6SGreg Clayton     if (level == eDescriptionLevelBrief)
1087c982b3d6SGreg Clayton     {
1088c982b3d6SGreg Clayton         const char *filename = m_file.GetFilename().GetCString();
1089c982b3d6SGreg Clayton         if (filename)
1090c982b3d6SGreg Clayton             s->PutCString (filename);
1091c982b3d6SGreg Clayton     }
1092c982b3d6SGreg Clayton     else
1093c982b3d6SGreg Clayton     {
1094cfd1acedSGreg Clayton         char path[PATH_MAX];
1095cfd1acedSGreg Clayton         if (m_file.GetPath(path, sizeof(path)))
1096cfd1acedSGreg Clayton             s->PutCString(path);
1097c982b3d6SGreg Clayton     }
1098cfd1acedSGreg Clayton 
1099cfd1acedSGreg Clayton     const char *object_name = m_object_name.GetCString();
1100cfd1acedSGreg Clayton     if (object_name)
1101cfd1acedSGreg Clayton         s->Printf("(%s)", object_name);
1102ceb6b139SCaroline Tice }
1103ceb6b139SCaroline Tice 
1104ceb6b139SCaroline Tice void
1105c982b3d6SGreg Clayton Module::ReportError (const char *format, ...)
1106c982b3d6SGreg Clayton {
1107e38a5eddSGreg Clayton     if (format && format[0])
1108e38a5eddSGreg Clayton     {
1109e38a5eddSGreg Clayton         StreamString strm;
1110e38a5eddSGreg Clayton         strm.PutCString("error: ");
1111e38a5eddSGreg Clayton         GetDescription(&strm, lldb::eDescriptionLevelBrief);
11128b35334eSGreg Clayton         strm.PutChar (' ');
1113c982b3d6SGreg Clayton         va_list args;
1114c982b3d6SGreg Clayton         va_start (args, format);
1115e38a5eddSGreg Clayton         strm.PrintfVarArg(format, args);
1116c982b3d6SGreg Clayton         va_end (args);
1117e38a5eddSGreg Clayton 
1118e38a5eddSGreg Clayton         const int format_len = strlen(format);
1119e38a5eddSGreg Clayton         if (format_len > 0)
1120e38a5eddSGreg Clayton         {
1121e38a5eddSGreg Clayton             const char last_char = format[format_len-1];
1122e38a5eddSGreg Clayton             if (last_char != '\n' || last_char != '\r')
1123e38a5eddSGreg Clayton                 strm.EOL();
1124e38a5eddSGreg Clayton         }
1125e38a5eddSGreg Clayton         Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1126e38a5eddSGreg Clayton     }
1127e38a5eddSGreg Clayton }
1128e38a5eddSGreg Clayton 
11291d60909eSGreg Clayton bool
11301d60909eSGreg Clayton Module::FileHasChanged () const
11311d60909eSGreg Clayton {
1132c5dac77aSEugene Zelenko     if (!m_file_has_changed)
11331d60909eSGreg Clayton         m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
11341d60909eSGreg Clayton     return m_file_has_changed;
11351d60909eSGreg Clayton }
11361d60909eSGreg Clayton 
1137e38a5eddSGreg Clayton void
1138e38a5eddSGreg Clayton Module::ReportErrorIfModifyDetected (const char *format, ...)
1139e38a5eddSGreg Clayton {
1140c5dac77aSEugene Zelenko     if (!m_first_file_changed_log)
1141e38a5eddSGreg Clayton     {
11421d60909eSGreg Clayton         if (FileHasChanged ())
11431d60909eSGreg Clayton         {
11441d60909eSGreg Clayton             m_first_file_changed_log = true;
1145e38a5eddSGreg Clayton             if (format)
1146e38a5eddSGreg Clayton             {
1147e38a5eddSGreg Clayton                 StreamString strm;
1148e38a5eddSGreg Clayton                 strm.PutCString("error: the object file ");
1149e38a5eddSGreg Clayton                 GetDescription(&strm, lldb::eDescriptionLevelFull);
1150e38a5eddSGreg Clayton                 strm.PutCString (" has been modified\n");
1151e38a5eddSGreg Clayton 
1152e38a5eddSGreg Clayton                 va_list args;
1153e38a5eddSGreg Clayton                 va_start (args, format);
1154e38a5eddSGreg Clayton                 strm.PrintfVarArg(format, args);
1155e38a5eddSGreg Clayton                 va_end (args);
1156e38a5eddSGreg Clayton 
1157e38a5eddSGreg Clayton                 const int format_len = strlen(format);
1158e38a5eddSGreg Clayton                 if (format_len > 0)
1159e38a5eddSGreg Clayton                 {
1160e38a5eddSGreg Clayton                     const char last_char = format[format_len-1];
1161e38a5eddSGreg Clayton                     if (last_char != '\n' || last_char != '\r')
1162e38a5eddSGreg Clayton                         strm.EOL();
1163e38a5eddSGreg Clayton                 }
1164e38a5eddSGreg Clayton                 strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
1165e38a5eddSGreg Clayton                 Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1166e38a5eddSGreg Clayton             }
1167e38a5eddSGreg Clayton         }
1168c982b3d6SGreg Clayton     }
11691d60909eSGreg Clayton }
1170c982b3d6SGreg Clayton 
1171c982b3d6SGreg Clayton void
1172c982b3d6SGreg Clayton Module::ReportWarning (const char *format, ...)
1173c982b3d6SGreg Clayton {
1174e38a5eddSGreg Clayton     if (format && format[0])
1175e38a5eddSGreg Clayton     {
1176e38a5eddSGreg Clayton         StreamString strm;
1177e38a5eddSGreg Clayton         strm.PutCString("warning: ");
11788b35334eSGreg Clayton         GetDescription(&strm, lldb::eDescriptionLevelFull);
11798b35334eSGreg Clayton         strm.PutChar (' ');
1180c982b3d6SGreg Clayton 
1181c982b3d6SGreg Clayton         va_list args;
1182c982b3d6SGreg Clayton         va_start (args, format);
1183e38a5eddSGreg Clayton         strm.PrintfVarArg(format, args);
1184c982b3d6SGreg Clayton         va_end (args);
1185e38a5eddSGreg Clayton 
1186e38a5eddSGreg Clayton         const int format_len = strlen(format);
1187e38a5eddSGreg Clayton         if (format_len > 0)
1188e38a5eddSGreg Clayton         {
1189e38a5eddSGreg Clayton             const char last_char = format[format_len-1];
1190e38a5eddSGreg Clayton             if (last_char != '\n' || last_char != '\r')
1191e38a5eddSGreg Clayton                 strm.EOL();
1192e38a5eddSGreg Clayton         }
1193e38a5eddSGreg Clayton         Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
1194e38a5eddSGreg Clayton     }
1195c982b3d6SGreg Clayton }
1196c982b3d6SGreg Clayton 
1197c982b3d6SGreg Clayton void
1198c982b3d6SGreg Clayton Module::LogMessage (Log *log, const char *format, ...)
1199c982b3d6SGreg Clayton {
1200c5dac77aSEugene Zelenko     if (log != nullptr)
1201c982b3d6SGreg Clayton     {
1202c982b3d6SGreg Clayton         StreamString log_message;
12038b35334eSGreg Clayton         GetDescription(&log_message, lldb::eDescriptionLevelFull);
1204c982b3d6SGreg Clayton         log_message.PutCString (": ");
1205c982b3d6SGreg Clayton         va_list args;
1206c982b3d6SGreg Clayton         va_start (args, format);
1207c982b3d6SGreg Clayton         log_message.PrintfVarArg (format, args);
1208c982b3d6SGreg Clayton         va_end (args);
1209c982b3d6SGreg Clayton         log->PutCString(log_message.GetString().c_str());
1210c982b3d6SGreg Clayton     }
1211c982b3d6SGreg Clayton }
1212c982b3d6SGreg Clayton 
1213d61c0fc0SGreg Clayton void
1214d61c0fc0SGreg Clayton Module::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
1215d61c0fc0SGreg Clayton {
1216c5dac77aSEugene Zelenko     if (log != nullptr)
1217d61c0fc0SGreg Clayton     {
1218d61c0fc0SGreg Clayton         StreamString log_message;
1219d61c0fc0SGreg Clayton         GetDescription(&log_message, lldb::eDescriptionLevelFull);
1220d61c0fc0SGreg Clayton         log_message.PutCString (": ");
1221d61c0fc0SGreg Clayton         va_list args;
1222d61c0fc0SGreg Clayton         va_start (args, format);
1223d61c0fc0SGreg Clayton         log_message.PrintfVarArg (format, args);
1224d61c0fc0SGreg Clayton         va_end (args);
1225d61c0fc0SGreg Clayton         if (log->GetVerbose())
1226a893d301SZachary Turner         {
1227a893d301SZachary Turner             std::string back_trace;
1228a893d301SZachary Turner             llvm::raw_string_ostream stream(back_trace);
1229a893d301SZachary Turner             llvm::sys::PrintStackTrace(stream);
1230a893d301SZachary Turner             log_message.PutCString(back_trace.c_str());
1231a893d301SZachary Turner         }
1232d61c0fc0SGreg Clayton         log->PutCString(log_message.GetString().c_str());
1233d61c0fc0SGreg Clayton     }
1234d61c0fc0SGreg Clayton }
1235d61c0fc0SGreg Clayton 
1236c982b3d6SGreg Clayton void
123730fdc8d8SChris Lattner Module::Dump(Stream *s)
123830fdc8d8SChris Lattner {
1239*16ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
12408941142aSGreg Clayton     //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
124130fdc8d8SChris Lattner     s->Indent();
1242b5ad4ec7SGreg Clayton     s->Printf("Module %s%s%s%s\n",
1243b5ad4ec7SGreg Clayton               m_file.GetPath().c_str(),
124430fdc8d8SChris Lattner               m_object_name ? "(" : "",
124530fdc8d8SChris Lattner               m_object_name ? m_object_name.GetCString() : "",
124630fdc8d8SChris Lattner               m_object_name ? ")" : "");
124730fdc8d8SChris Lattner 
124830fdc8d8SChris Lattner     s->IndentMore();
124930fdc8d8SChris Lattner 
1250a7499c98SMichael Sartain     ObjectFile *objfile = GetObjectFile ();
125130fdc8d8SChris Lattner     if (objfile)
125230fdc8d8SChris Lattner         objfile->Dump(s);
125330fdc8d8SChris Lattner 
125430fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
125530fdc8d8SChris Lattner     if (symbols)
125630fdc8d8SChris Lattner         symbols->Dump(s);
125730fdc8d8SChris Lattner 
125830fdc8d8SChris Lattner     s->IndentLess();
125930fdc8d8SChris Lattner }
126030fdc8d8SChris Lattner 
126130fdc8d8SChris Lattner TypeList*
126230fdc8d8SChris Lattner Module::GetTypeList ()
126330fdc8d8SChris Lattner {
126430fdc8d8SChris Lattner     SymbolVendor *symbols = GetSymbolVendor ();
126530fdc8d8SChris Lattner     if (symbols)
126630fdc8d8SChris Lattner         return &symbols->GetTypeList();
1267c5dac77aSEugene Zelenko     return nullptr;
126830fdc8d8SChris Lattner }
126930fdc8d8SChris Lattner 
127030fdc8d8SChris Lattner const ConstString &
127130fdc8d8SChris Lattner Module::GetObjectName() const
127230fdc8d8SChris Lattner {
127330fdc8d8SChris Lattner     return m_object_name;
127430fdc8d8SChris Lattner }
127530fdc8d8SChris Lattner 
127630fdc8d8SChris Lattner ObjectFile *
127730fdc8d8SChris Lattner Module::GetObjectFile()
127830fdc8d8SChris Lattner {
1279c5dac77aSEugene Zelenko     if (!m_did_load_objfile.load())
128088c05f54SGreg Clayton     {
1281*16ff8604SSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(m_mutex);
1282c5dac77aSEugene Zelenko         if (!m_did_load_objfile.load())
128330fdc8d8SChris Lattner         {
128430fdc8d8SChris Lattner             Timer scoped_timer(__PRETTY_FUNCTION__,
128530fdc8d8SChris Lattner                                "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
12865ce9c565SGreg Clayton             DataBufferSP data_sp;
12875ce9c565SGreg Clayton             lldb::offset_t data_offset = 0;
12882540a8a7SGreg Clayton             const lldb::offset_t file_size = m_file.GetByteSize();
12892540a8a7SGreg Clayton             if (file_size > m_object_offset)
12902540a8a7SGreg Clayton             {
12912540a8a7SGreg Clayton                 m_did_load_objfile = true;
1292e72dfb32SGreg Clayton                 m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
1293e72dfb32SGreg Clayton                                                        &m_file,
1294e72dfb32SGreg Clayton                                                        m_object_offset,
12952540a8a7SGreg Clayton                                                        file_size - m_object_offset,
12965ce9c565SGreg Clayton                                                        data_sp,
12975ce9c565SGreg Clayton                                                        data_offset);
1298593577a1SGreg Clayton                 if (m_objfile_sp)
1299593577a1SGreg Clayton                 {
1300593577a1SGreg Clayton                     // Once we get the object file, update our module with the object file's
1301593577a1SGreg Clayton                     // architecture since it might differ in vendor/os if some parts were
13025e6f4520SZachary Turner                     // unknown.  But since the matching arch might already be more specific
13035e6f4520SZachary Turner                     // than the generic COFF architecture, only merge in those values that
13045e6f4520SZachary Turner                     // overwrite unspecified unknown values.
13055e6f4520SZachary Turner                     ArchSpec new_arch;
13065e6f4520SZachary Turner                     m_objfile_sp->GetArchitecture(new_arch);
13075e6f4520SZachary Turner                     m_arch.MergeFrom(new_arch);
1308593577a1SGreg Clayton                 }
13090ee56ce6STodd Fiala                 else
13100ee56ce6STodd Fiala                 {
13110ee56ce6STodd Fiala                     ReportError ("failed to load objfile for %s", GetFileSpec().GetPath().c_str());
13120ee56ce6STodd Fiala                 }
131330fdc8d8SChris Lattner             }
13142540a8a7SGreg Clayton         }
131588c05f54SGreg Clayton     }
1316762f7135SGreg Clayton     return m_objfile_sp.get();
131730fdc8d8SChris Lattner }
131830fdc8d8SChris Lattner 
1319a7499c98SMichael Sartain SectionList *
13203046e668SGreg Clayton Module::GetSectionList()
13213046e668SGreg Clayton {
13223046e668SGreg Clayton     // Populate m_unified_sections_ap with sections from objfile.
1323c5dac77aSEugene Zelenko     if (!m_sections_ap)
13243046e668SGreg Clayton     {
13253046e668SGreg Clayton         ObjectFile *obj_file = GetObjectFile();
1326c5dac77aSEugene Zelenko         if (obj_file != nullptr)
13273046e668SGreg Clayton             obj_file->CreateSections(*GetUnifiedSectionList());
13283046e668SGreg Clayton     }
13293046e668SGreg Clayton     return m_sections_ap.get();
13303046e668SGreg Clayton }
13313046e668SGreg Clayton 
133205a09c67SJason Molenda void
133305a09c67SJason Molenda Module::SectionFileAddressesChanged ()
133405a09c67SJason Molenda {
133505a09c67SJason Molenda     ObjectFile *obj_file = GetObjectFile ();
133605a09c67SJason Molenda     if (obj_file)
133705a09c67SJason Molenda         obj_file->SectionFileAddressesChanged ();
133805a09c67SJason Molenda     SymbolVendor* sym_vendor = GetSymbolVendor();
1339c5dac77aSEugene Zelenko     if (sym_vendor != nullptr)
134005a09c67SJason Molenda         sym_vendor->SectionFileAddressesChanged ();
134105a09c67SJason Molenda }
134205a09c67SJason Molenda 
13433046e668SGreg Clayton SectionList *
1344a7499c98SMichael Sartain Module::GetUnifiedSectionList()
1345a7499c98SMichael Sartain {
13463046e668SGreg Clayton     // Populate m_unified_sections_ap with sections from objfile.
1347c5dac77aSEugene Zelenko     if (!m_sections_ap)
13483046e668SGreg Clayton         m_sections_ap.reset(new SectionList());
13493046e668SGreg Clayton     return m_sections_ap.get();
1350a7499c98SMichael Sartain }
135130fdc8d8SChris Lattner 
135230fdc8d8SChris Lattner const Symbol *
135330fdc8d8SChris Lattner Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
135430fdc8d8SChris Lattner {
135530fdc8d8SChris Lattner     Timer scoped_timer(__PRETTY_FUNCTION__,
135630fdc8d8SChris Lattner                        "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
135730fdc8d8SChris Lattner                        name.AsCString(),
135830fdc8d8SChris Lattner                        symbol_type);
1359a7499c98SMichael Sartain     SymbolVendor* sym_vendor = GetSymbolVendor();
1360a7499c98SMichael Sartain     if (sym_vendor)
136130fdc8d8SChris Lattner     {
1362a7499c98SMichael Sartain         Symtab *symtab = sym_vendor->GetSymtab();
136330fdc8d8SChris Lattner         if (symtab)
1364bcf2cfbdSGreg Clayton             return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
136530fdc8d8SChris Lattner     }
1366c5dac77aSEugene Zelenko     return nullptr;
136730fdc8d8SChris Lattner }
136830fdc8d8SChris Lattner void
136930fdc8d8SChris Lattner Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
137030fdc8d8SChris Lattner {
137130fdc8d8SChris Lattner     // No need to protect this call using m_mutex all other method calls are
137230fdc8d8SChris Lattner     // already thread safe.
137330fdc8d8SChris Lattner 
137430fdc8d8SChris Lattner     size_t num_indices = symbol_indexes.size();
137530fdc8d8SChris Lattner     if (num_indices > 0)
137630fdc8d8SChris Lattner     {
137730fdc8d8SChris Lattner         SymbolContext sc;
137830fdc8d8SChris Lattner         CalculateSymbolContext (&sc);
137930fdc8d8SChris Lattner         for (size_t i = 0; i < num_indices; i++)
138030fdc8d8SChris Lattner         {
138130fdc8d8SChris Lattner             sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
138230fdc8d8SChris Lattner             if (sc.symbol)
138330fdc8d8SChris Lattner                 sc_list.Append (sc);
138430fdc8d8SChris Lattner         }
138530fdc8d8SChris Lattner     }
138630fdc8d8SChris Lattner }
138730fdc8d8SChris Lattner 
138830fdc8d8SChris Lattner size_t
1389c1b2ccfdSGreg Clayton Module::FindFunctionSymbols (const ConstString &name,
1390c1b2ccfdSGreg Clayton                              uint32_t name_type_mask,
1391c1b2ccfdSGreg Clayton                              SymbolContextList& sc_list)
1392c1b2ccfdSGreg Clayton {
1393c1b2ccfdSGreg Clayton     Timer scoped_timer(__PRETTY_FUNCTION__,
1394c1b2ccfdSGreg Clayton                        "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1395c1b2ccfdSGreg Clayton                        name.AsCString(),
1396c1b2ccfdSGreg Clayton                        name_type_mask);
1397a7499c98SMichael Sartain     SymbolVendor* sym_vendor = GetSymbolVendor();
1398a7499c98SMichael Sartain     if (sym_vendor)
1399c1b2ccfdSGreg Clayton     {
1400a7499c98SMichael Sartain         Symtab *symtab = sym_vendor->GetSymtab();
1401c1b2ccfdSGreg Clayton         if (symtab)
1402c1b2ccfdSGreg Clayton             return symtab->FindFunctionSymbols (name, name_type_mask, sc_list);
1403c1b2ccfdSGreg Clayton     }
1404c1b2ccfdSGreg Clayton     return 0;
1405c1b2ccfdSGreg Clayton }
1406c1b2ccfdSGreg Clayton 
1407c1b2ccfdSGreg Clayton size_t
1408b96ff33bSSean Callanan Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
140930fdc8d8SChris Lattner {
141030fdc8d8SChris Lattner     // No need to protect this call using m_mutex all other method calls are
141130fdc8d8SChris Lattner     // already thread safe.
141230fdc8d8SChris Lattner 
141330fdc8d8SChris Lattner     Timer scoped_timer(__PRETTY_FUNCTION__,
141430fdc8d8SChris Lattner                        "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
141530fdc8d8SChris Lattner                        name.AsCString(),
141630fdc8d8SChris Lattner                        symbol_type);
141730fdc8d8SChris Lattner     const size_t initial_size = sc_list.GetSize();
1418a7499c98SMichael Sartain     SymbolVendor* sym_vendor = GetSymbolVendor();
1419a7499c98SMichael Sartain     if (sym_vendor)
142030fdc8d8SChris Lattner     {
1421a7499c98SMichael Sartain         Symtab *symtab = sym_vendor->GetSymtab();
142230fdc8d8SChris Lattner         if (symtab)
142330fdc8d8SChris Lattner         {
142430fdc8d8SChris Lattner             std::vector<uint32_t> symbol_indexes;
142530fdc8d8SChris Lattner             symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
142630fdc8d8SChris Lattner             SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
142730fdc8d8SChris Lattner         }
142830fdc8d8SChris Lattner     }
142930fdc8d8SChris Lattner     return sc_list.GetSize() - initial_size;
143030fdc8d8SChris Lattner }
143130fdc8d8SChris Lattner 
143230fdc8d8SChris Lattner size_t
143330fdc8d8SChris Lattner Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
143430fdc8d8SChris Lattner {
143530fdc8d8SChris Lattner     // No need to protect this call using m_mutex all other method calls are
143630fdc8d8SChris Lattner     // already thread safe.
143730fdc8d8SChris Lattner 
143830fdc8d8SChris Lattner     Timer scoped_timer(__PRETTY_FUNCTION__,
143930fdc8d8SChris Lattner                        "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
144030fdc8d8SChris Lattner                        regex.GetText(),
144130fdc8d8SChris Lattner                        symbol_type);
144230fdc8d8SChris Lattner     const size_t initial_size = sc_list.GetSize();
1443a7499c98SMichael Sartain     SymbolVendor* sym_vendor = GetSymbolVendor();
1444a7499c98SMichael Sartain     if (sym_vendor)
144530fdc8d8SChris Lattner     {
1446a7499c98SMichael Sartain         Symtab *symtab = sym_vendor->GetSymtab();
144730fdc8d8SChris Lattner         if (symtab)
144830fdc8d8SChris Lattner         {
144930fdc8d8SChris Lattner             std::vector<uint32_t> symbol_indexes;
1450bcf2cfbdSGreg Clayton             symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
145130fdc8d8SChris Lattner             SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
145230fdc8d8SChris Lattner         }
145330fdc8d8SChris Lattner     }
145430fdc8d8SChris Lattner     return sc_list.GetSize() - initial_size;
145530fdc8d8SChris Lattner }
145630fdc8d8SChris Lattner 
1457e01e07b6SGreg Clayton void
1458e01e07b6SGreg Clayton Module::SetSymbolFileFileSpec (const FileSpec &file)
1459e01e07b6SGreg Clayton {
146090271672SGreg Clayton     if (!file.Exists())
146190271672SGreg Clayton         return;
1462a7499c98SMichael Sartain     if (m_symfile_ap)
1463a7499c98SMichael Sartain     {
146490271672SGreg Clayton         // Remove any sections in the unified section list that come from the current symbol vendor.
14653046e668SGreg Clayton         SectionList *section_list = GetSectionList();
1466a7499c98SMichael Sartain         SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile();
1467a7499c98SMichael Sartain         if (section_list && symbol_file)
1468a7499c98SMichael Sartain         {
1469a7499c98SMichael Sartain             ObjectFile *obj_file = symbol_file->GetObjectFile();
1470540fbbfaSGreg Clayton             // Make sure we have an object file and that the symbol vendor's objfile isn't
1471540fbbfaSGreg Clayton             // the same as the module's objfile before we remove any sections for it...
147290271672SGreg Clayton             if (obj_file)
147390271672SGreg Clayton             {
147490271672SGreg Clayton                 // Check to make sure we aren't trying to specify the file we already have
147590271672SGreg Clayton                 if (obj_file->GetFileSpec() == file)
147690271672SGreg Clayton                 {
147790271672SGreg Clayton                     // We are being told to add the exact same file that we already have
147890271672SGreg Clayton                     // we don't have to do anything.
147990271672SGreg Clayton                     return;
148090271672SGreg Clayton                 }
1481d00438e8STamas Berghammer 
1482d00438e8STamas Berghammer                 // Cleare the current symtab as we are going to replace it with a new one
1483d00438e8STamas Berghammer                 obj_file->ClearSymtab();
148490271672SGreg Clayton 
148590271672SGreg Clayton                 // The symbol file might be a directory bundle ("/tmp/a.out.dSYM") instead
148690271672SGreg Clayton                 // of a full path to the symbol file within the bundle
148790271672SGreg Clayton                 // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to check this
148890271672SGreg Clayton 
148990271672SGreg Clayton                 if (file.IsDirectory())
149090271672SGreg Clayton                 {
149190271672SGreg Clayton                     std::string new_path(file.GetPath());
149290271672SGreg Clayton                     std::string old_path(obj_file->GetFileSpec().GetPath());
149390271672SGreg Clayton                     if (old_path.find(new_path) == 0)
149490271672SGreg Clayton                     {
149590271672SGreg Clayton                         // We specified the same bundle as the symbol file that we already have
149690271672SGreg Clayton                         return;
149790271672SGreg Clayton                     }
149890271672SGreg Clayton                 }
149990271672SGreg Clayton 
150090271672SGreg Clayton                 if (obj_file != m_objfile_sp.get())
1501a7499c98SMichael Sartain                 {
1502a7499c98SMichael Sartain                     size_t num_sections = section_list->GetNumSections (0);
1503a7499c98SMichael Sartain                     for (size_t idx = num_sections; idx > 0; --idx)
1504a7499c98SMichael Sartain                     {
1505a7499c98SMichael Sartain                         lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1));
1506a7499c98SMichael Sartain                         if (section_sp->GetObjectFile() == obj_file)
1507a7499c98SMichael Sartain                         {
15083046e668SGreg Clayton                             section_list->DeleteSection (idx - 1);
1509a7499c98SMichael Sartain                         }
1510a7499c98SMichael Sartain                     }
1511a7499c98SMichael Sartain                 }
1512a7499c98SMichael Sartain             }
1513a7499c98SMichael Sartain         }
151490271672SGreg Clayton         // Keep all old symbol files around in case there are any lingering type references in
151590271672SGreg Clayton         // any SBValue objects that might have been handed out.
151690271672SGreg Clayton         m_old_symfiles.push_back(std::move(m_symfile_ap));
151790271672SGreg Clayton     }
1518e01e07b6SGreg Clayton     m_symfile_spec = file;
1519e01e07b6SGreg Clayton     m_symfile_ap.reset();
1520e01e07b6SGreg Clayton     m_did_load_symbol_vendor = false;
1521e01e07b6SGreg Clayton }
1522e01e07b6SGreg Clayton 
15235aee162fSJim Ingham bool
15245aee162fSJim Ingham Module::IsExecutable ()
15255aee162fSJim Ingham {
1526c5dac77aSEugene Zelenko     if (GetObjectFile() == nullptr)
15275aee162fSJim Ingham         return false;
15285aee162fSJim Ingham     else
15295aee162fSJim Ingham         return GetObjectFile()->IsExecutable();
15305aee162fSJim Ingham }
15315aee162fSJim Ingham 
15325aee162fSJim Ingham bool
1533b53cb271SJim Ingham Module::IsLoadedInTarget (Target *target)
1534b53cb271SJim Ingham {
1535b53cb271SJim Ingham     ObjectFile *obj_file = GetObjectFile();
1536b53cb271SJim Ingham     if (obj_file)
1537b53cb271SJim Ingham     {
15383046e668SGreg Clayton         SectionList *sections = GetSectionList();
1539c5dac77aSEugene Zelenko         if (sections != nullptr)
1540b53cb271SJim Ingham         {
1541b53cb271SJim Ingham             size_t num_sections = sections->GetSize();
1542b53cb271SJim Ingham             for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1543b53cb271SJim Ingham             {
1544b53cb271SJim Ingham                 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1545b53cb271SJim Ingham                 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1546b53cb271SJim Ingham                 {
1547b53cb271SJim Ingham                     return true;
1548b53cb271SJim Ingham                 }
1549b53cb271SJim Ingham             }
1550b53cb271SJim Ingham         }
1551b53cb271SJim Ingham     }
1552b53cb271SJim Ingham     return false;
1553b53cb271SJim Ingham }
15541759848bSEnrico Granata 
15551759848bSEnrico Granata bool
15569730339bSEnrico Granata Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream)
15571759848bSEnrico Granata {
15581759848bSEnrico Granata     if (!target)
15591759848bSEnrico Granata     {
15601759848bSEnrico Granata         error.SetErrorString("invalid destination Target");
15611759848bSEnrico Granata         return false;
15621759848bSEnrico Granata     }
15631759848bSEnrico Granata 
1564d93c4a33SBruce Mitchener     LoadScriptFromSymFile should_load = target->TargetProperties::GetLoadScriptFromSymbolFile();
15652ea43cdcSEnrico Granata 
1566994740fbSGreg Clayton     if (should_load == eLoadScriptFromSymFileFalse)
1567994740fbSGreg Clayton         return false;
1568994740fbSGreg Clayton 
156991c0e749SGreg Clayton     Debugger &debugger = target->GetDebugger();
157091c0e749SGreg Clayton     const ScriptLanguage script_language = debugger.GetScriptLanguage();
157191c0e749SGreg Clayton     if (script_language != eScriptLanguageNone)
157291c0e749SGreg Clayton     {
157391c0e749SGreg Clayton 
15741759848bSEnrico Granata         PlatformSP platform_sp(target->GetPlatform());
15751759848bSEnrico Granata 
15761759848bSEnrico Granata         if (!platform_sp)
15771759848bSEnrico Granata         {
15781759848bSEnrico Granata             error.SetErrorString("invalid Platform");
15791759848bSEnrico Granata             return false;
15801759848bSEnrico Granata         }
15811759848bSEnrico Granata 
158291c0e749SGreg Clayton         FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
1583fe7295dcSEnrico Granata                                                                                    *this,
1584fe7295dcSEnrico Granata                                                                                    feedback_stream);
158591c0e749SGreg Clayton 
158691c0e749SGreg Clayton         const uint32_t num_specs = file_specs.GetSize();
158791c0e749SGreg Clayton         if (num_specs)
158891c0e749SGreg Clayton         {
1589b9d8890bSGreg Clayton             ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1590b9d8890bSGreg Clayton             if (script_interpreter)
1591b9d8890bSGreg Clayton             {
159291c0e749SGreg Clayton                 for (uint32_t i = 0; i < num_specs; ++i)
159391c0e749SGreg Clayton                 {
159491c0e749SGreg Clayton                     FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
159591c0e749SGreg Clayton                     if (scripting_fspec && scripting_fspec.Exists())
159691c0e749SGreg Clayton                     {
1597d93c4a33SBruce Mitchener                         if (should_load == eLoadScriptFromSymFileWarn)
15982ea43cdcSEnrico Granata                         {
1599397ddd5fSEnrico Granata                             if (feedback_stream)
1600d516deb4SJim Ingham                                 feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in "
1601d516deb4SJim Ingham                                                         "this debug session:\n\n    command script import \"%s\"\n\n"
1602d516deb4SJim Ingham                                                         "To run all discovered debug scripts in this session:\n\n"
1603d516deb4SJim Ingham                                                         "    settings set target.load-script-from-symbol-file true\n",
1604d516deb4SJim Ingham                                                         GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1605d516deb4SJim Ingham                                                         scripting_fspec.GetPath().c_str());
16062ea43cdcSEnrico Granata                             return false;
16072ea43cdcSEnrico Granata                         }
16081759848bSEnrico Granata                         StreamString scripting_stream;
16091759848bSEnrico Granata                         scripting_fspec.Dump(&scripting_stream);
1610e0c70f1bSEnrico Granata                         const bool can_reload = true;
16116a51085eSJim Ingham                         const bool init_lldb_globals = false;
1612d516deb4SJim Ingham                         bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(),
1613d516deb4SJim Ingham                                                                                 can_reload,
1614d516deb4SJim Ingham                                                                                 init_lldb_globals,
1615d516deb4SJim Ingham                                                                                 error);
16161759848bSEnrico Granata                         if (!did_load)
16171759848bSEnrico Granata                             return false;
16181759848bSEnrico Granata                     }
161991c0e749SGreg Clayton                 }
162091c0e749SGreg Clayton             }
16211759848bSEnrico Granata             else
16221759848bSEnrico Granata             {
16231759848bSEnrico Granata                 error.SetErrorString("invalid ScriptInterpreter");
16241759848bSEnrico Granata                 return false;
16251759848bSEnrico Granata             }
16261759848bSEnrico Granata         }
1627b9d8890bSGreg Clayton     }
16281759848bSEnrico Granata     return true;
16291759848bSEnrico Granata }
16301759848bSEnrico Granata 
1631b53cb271SJim Ingham bool
16325aee162fSJim Ingham Module::SetArchitecture (const ArchSpec &new_arch)
16335aee162fSJim Ingham {
163464195a2cSGreg Clayton     if (!m_arch.IsValid())
16355aee162fSJim Ingham     {
16365aee162fSJim Ingham         m_arch = new_arch;
16375aee162fSJim Ingham         return true;
16385aee162fSJim Ingham     }
1639b6cd5fe9SChaoren Lin     return m_arch.IsCompatibleMatch(new_arch);
16405aee162fSJim Ingham }
16415aee162fSJim Ingham 
1642c9660546SGreg Clayton bool
1643751caf65SGreg Clayton Module::SetLoadAddress (Target &target, lldb::addr_t value, bool value_is_offset, bool &changed)
1644c9660546SGreg Clayton {
16459e02dacdSSteve Pucci     ObjectFile *object_file = GetObjectFile();
1646c5dac77aSEugene Zelenko     if (object_file != nullptr)
1647c9660546SGreg Clayton     {
1648751caf65SGreg Clayton         changed = object_file->SetLoadAddress(target, value, value_is_offset);
16497524e090SGreg Clayton         return true;
16507524e090SGreg Clayton     }
16517524e090SGreg Clayton     else
16527524e090SGreg Clayton     {
16537524e090SGreg Clayton         changed = false;
1654c9660546SGreg Clayton     }
16559e02dacdSSteve Pucci     return false;
1656c9660546SGreg Clayton }
1657c9660546SGreg Clayton 
1658b9a01b39SGreg Clayton 
1659b9a01b39SGreg Clayton bool
1660b9a01b39SGreg Clayton Module::MatchesModuleSpec (const ModuleSpec &module_ref)
1661b9a01b39SGreg Clayton {
1662b9a01b39SGreg Clayton     const UUID &uuid = module_ref.GetUUID();
1663b9a01b39SGreg Clayton 
1664b9a01b39SGreg Clayton     if (uuid.IsValid())
1665b9a01b39SGreg Clayton     {
1666b9a01b39SGreg Clayton         // If the UUID matches, then nothing more needs to match...
1667c5dac77aSEugene Zelenko         return (uuid == GetUUID());
1668b9a01b39SGreg Clayton     }
1669b9a01b39SGreg Clayton 
1670b9a01b39SGreg Clayton     const FileSpec &file_spec = module_ref.GetFileSpec();
1671b9a01b39SGreg Clayton     if (file_spec)
1672b9a01b39SGreg Clayton     {
1673980662eeSTamas Berghammer         if (!FileSpec::Equal (file_spec, m_file, (bool)file_spec.GetDirectory()) &&
1674980662eeSTamas Berghammer             !FileSpec::Equal (file_spec, m_platform_file, (bool)file_spec.GetDirectory()))
1675b9a01b39SGreg Clayton             return false;
1676b9a01b39SGreg Clayton     }
1677b9a01b39SGreg Clayton 
1678b9a01b39SGreg Clayton     const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1679b9a01b39SGreg Clayton     if (platform_file_spec)
1680b9a01b39SGreg Clayton     {
1681ddd7a2a6SSean Callanan         if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), (bool)platform_file_spec.GetDirectory()))
1682b9a01b39SGreg Clayton             return false;
1683b9a01b39SGreg Clayton     }
1684b9a01b39SGreg Clayton 
1685b9a01b39SGreg Clayton     const ArchSpec &arch = module_ref.GetArchitecture();
1686b9a01b39SGreg Clayton     if (arch.IsValid())
1687b9a01b39SGreg Clayton     {
1688bf4b7be6SSean Callanan         if (!m_arch.IsCompatibleMatch(arch))
1689b9a01b39SGreg Clayton             return false;
1690b9a01b39SGreg Clayton     }
1691b9a01b39SGreg Clayton 
1692b9a01b39SGreg Clayton     const ConstString &object_name = module_ref.GetObjectName();
1693b9a01b39SGreg Clayton     if (object_name)
1694b9a01b39SGreg Clayton     {
1695b9a01b39SGreg Clayton         if (object_name != GetObjectName())
1696b9a01b39SGreg Clayton             return false;
1697b9a01b39SGreg Clayton     }
1698b9a01b39SGreg Clayton     return true;
1699b9a01b39SGreg Clayton }
1700b9a01b39SGreg Clayton 
1701d804d285SGreg Clayton bool
1702d804d285SGreg Clayton Module::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1703d804d285SGreg Clayton {
1704*16ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1705d804d285SGreg Clayton     return m_source_mappings.FindFile (orig_spec, new_spec);
1706d804d285SGreg Clayton }
1707d804d285SGreg Clayton 
1708f9be6933SGreg Clayton bool
1709f9be6933SGreg Clayton Module::RemapSourceFile (const char *path, std::string &new_path) const
1710f9be6933SGreg Clayton {
1711*16ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
1712f9be6933SGreg Clayton     return m_source_mappings.RemapPath(path, new_path);
1713f9be6933SGreg Clayton }
1714f9be6933SGreg Clayton 
17153467d80bSEnrico Granata uint32_t
17163467d80bSEnrico Granata Module::GetVersion (uint32_t *versions, uint32_t num_versions)
17173467d80bSEnrico Granata {
17183467d80bSEnrico Granata     ObjectFile *obj_file = GetObjectFile();
17193467d80bSEnrico Granata     if (obj_file)
17203467d80bSEnrico Granata         return obj_file->GetVersion (versions, num_versions);
17213467d80bSEnrico Granata 
1722c5dac77aSEugene Zelenko     if (versions != nullptr && num_versions != 0)
17233467d80bSEnrico Granata     {
17243467d80bSEnrico Granata         for (uint32_t i = 0; i < num_versions; ++i)
1725afcbdb15SEnrico Granata             versions[i] = LLDB_INVALID_MODULE_VERSION;
17263467d80bSEnrico Granata     }
17273467d80bSEnrico Granata     return 0;
17283467d80bSEnrico Granata }
172943fe217bSGreg Clayton 
173043fe217bSGreg Clayton void
173143fe217bSGreg Clayton Module::PrepareForFunctionNameLookup (const ConstString &name,
173243fe217bSGreg Clayton                                       uint32_t name_type_mask,
173323b1decbSDawn Perchik                                       LanguageType language,
173443fe217bSGreg Clayton                                       ConstString &lookup_name,
173543fe217bSGreg Clayton                                       uint32_t &lookup_name_type_mask,
173643fe217bSGreg Clayton                                       bool &match_name_after_lookup)
173743fe217bSGreg Clayton {
173843fe217bSGreg Clayton     const char *name_cstr = name.GetCString();
173943fe217bSGreg Clayton     lookup_name_type_mask = eFunctionNameTypeNone;
174043fe217bSGreg Clayton     match_name_after_lookup = false;
1741fa39bb4aSJim Ingham 
1742fa39bb4aSJim Ingham     llvm::StringRef basename;
1743fa39bb4aSJim Ingham     llvm::StringRef context;
174443fe217bSGreg Clayton 
174543fe217bSGreg Clayton     if (name_type_mask & eFunctionNameTypeAuto)
174643fe217bSGreg Clayton     {
1747aa816b8fSJim Ingham         if (CPlusPlusLanguage::IsCPPMangledName (name_cstr))
174843fe217bSGreg Clayton             lookup_name_type_mask = eFunctionNameTypeFull;
174923b1decbSDawn Perchik         else if ((language == eLanguageTypeUnknown ||
17500e0984eeSJim Ingham                   Language::LanguageIsObjC(language)) &&
1751aa816b8fSJim Ingham                  ObjCLanguage::IsPossibleObjCMethodName (name_cstr))
175243fe217bSGreg Clayton             lookup_name_type_mask = eFunctionNameTypeFull;
17530e0984eeSJim Ingham         else if (Language::LanguageIsC(language))
175423b1decbSDawn Perchik         {
175523b1decbSDawn Perchik             lookup_name_type_mask = eFunctionNameTypeFull;
175623b1decbSDawn Perchik         }
175743fe217bSGreg Clayton         else
175843fe217bSGreg Clayton         {
175923b1decbSDawn Perchik             if ((language == eLanguageTypeUnknown ||
17600e0984eeSJim Ingham                  Language::LanguageIsObjC(language)) &&
1761aa816b8fSJim Ingham                 ObjCLanguage::IsPossibleObjCSelector(name_cstr))
176243fe217bSGreg Clayton                 lookup_name_type_mask |= eFunctionNameTypeSelector;
176343fe217bSGreg Clayton 
1764aa816b8fSJim Ingham             CPlusPlusLanguage::MethodName cpp_method (name);
1765fa39bb4aSJim Ingham             basename = cpp_method.GetBasename();
17666ecb232bSGreg Clayton             if (basename.empty())
17676ecb232bSGreg Clayton             {
1768aa816b8fSJim Ingham                 if (CPlusPlusLanguage::ExtractContextAndIdentifier (name_cstr, context, basename))
176943fe217bSGreg Clayton                     lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
1770c3795409SGreg Clayton                 else
177165b0e763SGreg Clayton                     lookup_name_type_mask |= eFunctionNameTypeFull;
177243fe217bSGreg Clayton             }
17736ecb232bSGreg Clayton             else
17746ecb232bSGreg Clayton             {
17756ecb232bSGreg Clayton                 lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
17766ecb232bSGreg Clayton             }
17776ecb232bSGreg Clayton         }
177843fe217bSGreg Clayton     }
177943fe217bSGreg Clayton     else
178043fe217bSGreg Clayton     {
178143fe217bSGreg Clayton         lookup_name_type_mask = name_type_mask;
178243fe217bSGreg Clayton         if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
178343fe217bSGreg Clayton         {
178443fe217bSGreg Clayton             // If they've asked for a CPP method or function name and it can't be that, we don't
178543fe217bSGreg Clayton             // even need to search for CPP methods or names.
1786aa816b8fSJim Ingham             CPlusPlusLanguage::MethodName cpp_method (name);
17876ecb232bSGreg Clayton             if (cpp_method.IsValid())
17886ecb232bSGreg Clayton             {
1789fa39bb4aSJim Ingham                 basename = cpp_method.GetBasename();
17906ecb232bSGreg Clayton 
17916ecb232bSGreg Clayton                 if (!cpp_method.GetQualifiers().empty())
17926ecb232bSGreg Clayton                 {
1793d93c4a33SBruce Mitchener                     // There is a "const" or other qualifier following the end of the function parens,
17946ecb232bSGreg Clayton                     // this can't be a eFunctionNameTypeBase
17956ecb232bSGreg Clayton                     lookup_name_type_mask &= ~(eFunctionNameTypeBase);
17966ecb232bSGreg Clayton                     if (lookup_name_type_mask == eFunctionNameTypeNone)
17976ecb232bSGreg Clayton                         return;
17986ecb232bSGreg Clayton                 }
17996ecb232bSGreg Clayton             }
18006ecb232bSGreg Clayton             else
18016ecb232bSGreg Clayton             {
1802fa39bb4aSJim Ingham                 // If the CPP method parser didn't manage to chop this up, try to fill in the base name if we can.
1803fa39bb4aSJim Ingham                 // If a::b::c is passed in, we need to just look up "c", and then we'll filter the result later.
1804aa816b8fSJim Ingham                 CPlusPlusLanguage::ExtractContextAndIdentifier (name_cstr, context, basename);
180543fe217bSGreg Clayton             }
18066ecb232bSGreg Clayton         }
180743fe217bSGreg Clayton 
180843fe217bSGreg Clayton         if (lookup_name_type_mask & eFunctionNameTypeSelector)
180943fe217bSGreg Clayton         {
1810aa816b8fSJim Ingham             if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr))
181143fe217bSGreg Clayton             {
181243fe217bSGreg Clayton                 lookup_name_type_mask &= ~(eFunctionNameTypeSelector);
181343fe217bSGreg Clayton                 if (lookup_name_type_mask == eFunctionNameTypeNone)
181443fe217bSGreg Clayton                     return;
181543fe217bSGreg Clayton             }
181643fe217bSGreg Clayton         }
181743fe217bSGreg Clayton     }
181843fe217bSGreg Clayton 
1819fa39bb4aSJim Ingham     if (!basename.empty())
182043fe217bSGreg Clayton     {
182143fe217bSGreg Clayton         // The name supplied was a partial C++ path like "a::count". In this case we want to do a
182243fe217bSGreg Clayton         // lookup on the basename "count" and then make sure any matching results contain "a::count"
182343fe217bSGreg Clayton         // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
182443fe217bSGreg Clayton         // to true
1825fa39bb4aSJim Ingham         lookup_name.SetString(basename);
182643fe217bSGreg Clayton         match_name_after_lookup = true;
182743fe217bSGreg Clayton     }
182843fe217bSGreg Clayton     else
182943fe217bSGreg Clayton     {
183043fe217bSGreg Clayton         // The name is already correct, just use the exact name as supplied, and we won't need
183143fe217bSGreg Clayton         // to check if any matches contain "name"
183243fe217bSGreg Clayton         lookup_name = name;
183343fe217bSGreg Clayton         match_name_after_lookup = false;
183443fe217bSGreg Clayton     }
183543fe217bSGreg Clayton }
183623f8c95aSGreg Clayton 
183723f8c95aSGreg Clayton ModuleSP
183823f8c95aSGreg Clayton Module::CreateJITModule (const lldb::ObjectFileJITDelegateSP &delegate_sp)
183923f8c95aSGreg Clayton {
184023f8c95aSGreg Clayton     if (delegate_sp)
184123f8c95aSGreg Clayton     {
184223f8c95aSGreg Clayton         // Must create a module and place it into a shared pointer before
184323f8c95aSGreg Clayton         // we can create an object file since it has a std::weak_ptr back
184423f8c95aSGreg Clayton         // to the module, so we need to control the creation carefully in
184523f8c95aSGreg Clayton         // this static function
184623f8c95aSGreg Clayton         ModuleSP module_sp(new Module());
184723f8c95aSGreg Clayton         module_sp->m_objfile_sp.reset (new ObjectFileJIT (module_sp, delegate_sp));
184823f8c95aSGreg Clayton         if (module_sp->m_objfile_sp)
184923f8c95aSGreg Clayton         {
185023f8c95aSGreg Clayton             // Once we get the object file, update our module with the object file's
185123f8c95aSGreg Clayton             // architecture since it might differ in vendor/os if some parts were
185223f8c95aSGreg Clayton             // unknown.
185323f8c95aSGreg Clayton             module_sp->m_objfile_sp->GetArchitecture (module_sp->m_arch);
185423f8c95aSGreg Clayton         }
185523f8c95aSGreg Clayton         return module_sp;
185623f8c95aSGreg Clayton     }
185723f8c95aSGreg Clayton     return ModuleSP();
185823f8c95aSGreg Clayton }
185923f8c95aSGreg Clayton 
186008928f30SGreg Clayton bool
186108928f30SGreg Clayton Module::GetIsDynamicLinkEditor()
186208928f30SGreg Clayton {
186308928f30SGreg Clayton     ObjectFile * obj_file = GetObjectFile ();
186408928f30SGreg Clayton 
186508928f30SGreg Clayton     if (obj_file)
186608928f30SGreg Clayton         return obj_file->GetIsDynamicLinkEditor();
186708928f30SGreg Clayton 
186808928f30SGreg Clayton     return false;
186908928f30SGreg Clayton }
1870