1254721Semaste//===-- Module.cpp ----------------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/lldb-python.h"
11254721Semaste
12263363Semaste#include "lldb/Core/AddressResolverFileLine.h"
13254721Semaste#include "lldb/Core/Error.h"
14254721Semaste#include "lldb/Core/Module.h"
15254721Semaste#include "lldb/Core/DataBuffer.h"
16254721Semaste#include "lldb/Core/DataBufferHeap.h"
17254721Semaste#include "lldb/Core/Log.h"
18254721Semaste#include "lldb/Core/ModuleList.h"
19254721Semaste#include "lldb/Core/ModuleSpec.h"
20254721Semaste#include "lldb/Core/RegularExpression.h"
21254721Semaste#include "lldb/Core/Section.h"
22254721Semaste#include "lldb/Core/StreamString.h"
23254721Semaste#include "lldb/Core/Timer.h"
24254721Semaste#include "lldb/Host/Host.h"
25254721Semaste#include "lldb/Host/Symbols.h"
26254721Semaste#include "lldb/Interpreter/CommandInterpreter.h"
27254721Semaste#include "lldb/Interpreter/ScriptInterpreter.h"
28254721Semaste#include "lldb/lldb-private-log.h"
29254721Semaste#include "lldb/Symbol/CompileUnit.h"
30254721Semaste#include "lldb/Symbol/ObjectFile.h"
31254721Semaste#include "lldb/Symbol/SymbolContext.h"
32254721Semaste#include "lldb/Symbol/SymbolVendor.h"
33254721Semaste#include "lldb/Target/CPPLanguageRuntime.h"
34254721Semaste#include "lldb/Target/ObjCLanguageRuntime.h"
35254721Semaste#include "lldb/Target/Process.h"
36269024Semaste#include "lldb/Target/SectionLoadList.h"
37254721Semaste#include "lldb/Target/Target.h"
38254721Semaste#include "lldb/Symbol/SymbolFile.h"
39254721Semaste
40254721Semasteusing namespace lldb;
41254721Semasteusing namespace lldb_private;
42254721Semaste
43254721Semaste// Shared pointers to modules track module lifetimes in
44254721Semaste// targets and in the global module, but this collection
45254721Semaste// will track all module objects that are still alive
46254721Semastetypedef std::vector<Module *> ModuleCollection;
47254721Semaste
48254721Semastestatic ModuleCollection &
49254721SemasteGetModuleCollection()
50254721Semaste{
51254721Semaste    // This module collection needs to live past any module, so we could either make it a
52254721Semaste    // shared pointer in each module or just leak is.  Since it is only an empty vector by
53254721Semaste    // the time all the modules have gone away, we just leak it for now.  If we decide this
54254721Semaste    // is a big problem we can introduce a Finalize method that will tear everything down in
55254721Semaste    // a predictable order.
56254721Semaste
57254721Semaste    static ModuleCollection *g_module_collection = NULL;
58254721Semaste    if (g_module_collection == NULL)
59254721Semaste        g_module_collection = new ModuleCollection();
60254721Semaste
61254721Semaste    return *g_module_collection;
62254721Semaste}
63254721Semaste
64254721SemasteMutex *
65254721SemasteModule::GetAllocationModuleCollectionMutex()
66254721Semaste{
67254721Semaste    // NOTE: The mutex below must be leaked since the global module list in
68254721Semaste    // the ModuleList class will get torn at some point, and we can't know
69254721Semaste    // if it will tear itself down before the "g_module_collection_mutex" below
70254721Semaste    // will. So we leak a Mutex object below to safeguard against that
71254721Semaste
72254721Semaste    static Mutex *g_module_collection_mutex = NULL;
73254721Semaste    if (g_module_collection_mutex == NULL)
74254721Semaste        g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
75254721Semaste    return g_module_collection_mutex;
76254721Semaste}
77254721Semaste
78254721Semastesize_t
79254721SemasteModule::GetNumberAllocatedModules ()
80254721Semaste{
81254721Semaste    Mutex::Locker locker (GetAllocationModuleCollectionMutex());
82254721Semaste    return GetModuleCollection().size();
83254721Semaste}
84254721Semaste
85254721SemasteModule *
86254721SemasteModule::GetAllocatedModuleAtIndex (size_t idx)
87254721Semaste{
88254721Semaste    Mutex::Locker locker (GetAllocationModuleCollectionMutex());
89254721Semaste    ModuleCollection &modules = GetModuleCollection();
90254721Semaste    if (idx < modules.size())
91254721Semaste        return modules[idx];
92254721Semaste    return NULL;
93254721Semaste}
94254721Semaste#if 0
95254721Semaste
96254721Semaste// These functions help us to determine if modules are still loaded, yet don't require that
97254721Semaste// you have a command interpreter and can easily be called from an external debugger.
98254721Semastenamespace lldb {
99254721Semaste
100254721Semaste    void
101254721Semaste    ClearModuleInfo (void)
102254721Semaste    {
103254721Semaste        const bool mandatory = true;
104254721Semaste        ModuleList::RemoveOrphanSharedModules(mandatory);
105254721Semaste    }
106254721Semaste
107254721Semaste    void
108254721Semaste    DumpModuleInfo (void)
109254721Semaste    {
110254721Semaste        Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
111254721Semaste        ModuleCollection &modules = GetModuleCollection();
112254721Semaste        const size_t count = modules.size();
113254721Semaste        printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count);
114254721Semaste        for (size_t i=0; i<count; ++i)
115254721Semaste        {
116254721Semaste
117254721Semaste            StreamString strm;
118254721Semaste            Module *module = modules[i];
119254721Semaste            const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
120254721Semaste            module->GetDescription(&strm, eDescriptionLevelFull);
121254721Semaste            printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
122254721Semaste                    module,
123254721Semaste                    in_shared_module_list,
124254721Semaste                    (uint32_t)module->use_count(),
125254721Semaste                    strm.GetString().c_str());
126254721Semaste        }
127254721Semaste    }
128254721Semaste}
129254721Semaste
130254721Semaste#endif
131254721Semaste
132254721SemasteModule::Module (const ModuleSpec &module_spec) :
133254721Semaste    m_mutex (Mutex::eMutexTypeRecursive),
134254721Semaste    m_mod_time (module_spec.GetFileSpec().GetModificationTime()),
135254721Semaste    m_arch (module_spec.GetArchitecture()),
136254721Semaste    m_uuid (),
137254721Semaste    m_file (module_spec.GetFileSpec()),
138254721Semaste    m_platform_file(module_spec.GetPlatformFileSpec()),
139263367Semaste    m_remote_install_file(),
140254721Semaste    m_symfile_spec (module_spec.GetSymbolFileSpec()),
141254721Semaste    m_object_name (module_spec.GetObjectName()),
142254721Semaste    m_object_offset (module_spec.GetObjectOffset()),
143254721Semaste    m_object_mod_time (module_spec.GetObjectModificationTime()),
144254721Semaste    m_objfile_sp (),
145254721Semaste    m_symfile_ap (),
146254721Semaste    m_ast (),
147254721Semaste    m_source_mappings (),
148254721Semaste    m_did_load_objfile (false),
149254721Semaste    m_did_load_symbol_vendor (false),
150254721Semaste    m_did_parse_uuid (false),
151254721Semaste    m_did_init_ast (false),
152254721Semaste    m_is_dynamic_loader_module (false),
153254721Semaste    m_file_has_changed (false),
154254721Semaste    m_first_file_changed_log (false)
155254721Semaste{
156254721Semaste    // Scope for locker below...
157254721Semaste    {
158254721Semaste        Mutex::Locker locker (GetAllocationModuleCollectionMutex());
159254721Semaste        GetModuleCollection().push_back(this);
160254721Semaste    }
161254721Semaste
162254721Semaste    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
163254721Semaste    if (log)
164254721Semaste        log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
165254721Semaste                     this,
166254721Semaste                     m_arch.GetArchitectureName(),
167254721Semaste                     m_file.GetPath().c_str(),
168254721Semaste                     m_object_name.IsEmpty() ? "" : "(",
169254721Semaste                     m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
170254721Semaste                     m_object_name.IsEmpty() ? "" : ")");
171254721Semaste}
172254721Semaste
173254721SemasteModule::Module(const FileSpec& file_spec,
174254721Semaste               const ArchSpec& arch,
175254721Semaste               const ConstString *object_name,
176254721Semaste               off_t object_offset,
177254721Semaste               const TimeValue *object_mod_time_ptr) :
178254721Semaste    m_mutex (Mutex::eMutexTypeRecursive),
179254721Semaste    m_mod_time (file_spec.GetModificationTime()),
180254721Semaste    m_arch (arch),
181254721Semaste    m_uuid (),
182254721Semaste    m_file (file_spec),
183254721Semaste    m_platform_file(),
184263367Semaste    m_remote_install_file (),
185254721Semaste    m_symfile_spec (),
186254721Semaste    m_object_name (),
187254721Semaste    m_object_offset (object_offset),
188254721Semaste    m_object_mod_time (),
189254721Semaste    m_objfile_sp (),
190254721Semaste    m_symfile_ap (),
191254721Semaste    m_ast (),
192254721Semaste    m_source_mappings (),
193254721Semaste    m_did_load_objfile (false),
194254721Semaste    m_did_load_symbol_vendor (false),
195254721Semaste    m_did_parse_uuid (false),
196254721Semaste    m_did_init_ast (false),
197254721Semaste    m_is_dynamic_loader_module (false),
198254721Semaste    m_file_has_changed (false),
199254721Semaste    m_first_file_changed_log (false)
200254721Semaste{
201254721Semaste    // Scope for locker below...
202254721Semaste    {
203254721Semaste        Mutex::Locker locker (GetAllocationModuleCollectionMutex());
204254721Semaste        GetModuleCollection().push_back(this);
205254721Semaste    }
206254721Semaste
207254721Semaste    if (object_name)
208254721Semaste        m_object_name = *object_name;
209254721Semaste
210254721Semaste    if (object_mod_time_ptr)
211254721Semaste        m_object_mod_time = *object_mod_time_ptr;
212254721Semaste
213254721Semaste    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
214254721Semaste    if (log)
215254721Semaste        log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
216254721Semaste                     this,
217254721Semaste                     m_arch.GetArchitectureName(),
218254721Semaste                     m_file.GetPath().c_str(),
219254721Semaste                     m_object_name.IsEmpty() ? "" : "(",
220254721Semaste                     m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
221254721Semaste                     m_object_name.IsEmpty() ? "" : ")");
222254721Semaste}
223254721Semaste
224254721SemasteModule::~Module()
225254721Semaste{
226254721Semaste    // Lock our module down while we tear everything down to make sure
227254721Semaste    // we don't get any access to the module while it is being destroyed
228254721Semaste    Mutex::Locker locker (m_mutex);
229254721Semaste    // Scope for locker below...
230254721Semaste    {
231254721Semaste        Mutex::Locker locker (GetAllocationModuleCollectionMutex());
232254721Semaste        ModuleCollection &modules = GetModuleCollection();
233254721Semaste        ModuleCollection::iterator end = modules.end();
234254721Semaste        ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
235254721Semaste        assert (pos != end);
236254721Semaste        modules.erase(pos);
237254721Semaste    }
238254721Semaste    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
239254721Semaste    if (log)
240254721Semaste        log->Printf ("%p Module::~Module((%s) '%s%s%s%s')",
241254721Semaste                     this,
242254721Semaste                     m_arch.GetArchitectureName(),
243254721Semaste                     m_file.GetPath().c_str(),
244254721Semaste                     m_object_name.IsEmpty() ? "" : "(",
245254721Semaste                     m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
246254721Semaste                     m_object_name.IsEmpty() ? "" : ")");
247254721Semaste    // Release any auto pointers before we start tearing down our member
248254721Semaste    // variables since the object file and symbol files might need to make
249254721Semaste    // function calls back into this module object. The ordering is important
250254721Semaste    // here because symbol files can require the module object file. So we tear
251254721Semaste    // down the symbol file first, then the object file.
252254721Semaste    m_sections_ap.reset();
253254721Semaste    m_symfile_ap.reset();
254254721Semaste    m_objfile_sp.reset();
255254721Semaste}
256254721Semaste
257254721SemasteObjectFile *
258254721SemasteModule::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error)
259254721Semaste{
260254721Semaste    if (m_objfile_sp)
261254721Semaste    {
262254721Semaste        error.SetErrorString ("object file already exists");
263254721Semaste    }
264254721Semaste    else
265254721Semaste    {
266254721Semaste        Mutex::Locker locker (m_mutex);
267254721Semaste        if (process_sp)
268254721Semaste        {
269254721Semaste            m_did_load_objfile = true;
270254721Semaste            std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (512, 0));
271254721Semaste            Error readmem_error;
272254721Semaste            const size_t bytes_read = process_sp->ReadMemory (header_addr,
273254721Semaste                                                              data_ap->GetBytes(),
274254721Semaste                                                              data_ap->GetByteSize(),
275254721Semaste                                                              readmem_error);
276254721Semaste            if (bytes_read == 512)
277254721Semaste            {
278254721Semaste                DataBufferSP data_sp(data_ap.release());
279254721Semaste                m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
280254721Semaste                if (m_objfile_sp)
281254721Semaste                {
282254721Semaste                    StreamString s;
283254721Semaste                    s.Printf("0x%16.16" PRIx64, header_addr);
284254721Semaste                    m_object_name.SetCString (s.GetData());
285254721Semaste
286254721Semaste                    // Once we get the object file, update our module with the object file's
287254721Semaste                    // architecture since it might differ in vendor/os if some parts were
288254721Semaste                    // unknown.
289254721Semaste                    m_objfile_sp->GetArchitecture (m_arch);
290254721Semaste                }
291254721Semaste                else
292254721Semaste                {
293254721Semaste                    error.SetErrorString ("unable to find suitable object file plug-in");
294254721Semaste                }
295254721Semaste            }
296254721Semaste            else
297254721Semaste            {
298254721Semaste                error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
299254721Semaste            }
300254721Semaste        }
301254721Semaste        else
302254721Semaste        {
303254721Semaste            error.SetErrorString ("invalid process");
304254721Semaste        }
305254721Semaste    }
306254721Semaste    return m_objfile_sp.get();
307254721Semaste}
308254721Semaste
309254721Semaste
310254721Semasteconst lldb_private::UUID&
311254721SemasteModule::GetUUID()
312254721Semaste{
313254721Semaste    Mutex::Locker locker (m_mutex);
314254721Semaste    if (m_did_parse_uuid == false)
315254721Semaste    {
316254721Semaste        ObjectFile * obj_file = GetObjectFile ();
317254721Semaste
318254721Semaste        if (obj_file != NULL)
319254721Semaste        {
320254721Semaste            obj_file->GetUUID(&m_uuid);
321254721Semaste            m_did_parse_uuid = true;
322254721Semaste        }
323254721Semaste    }
324254721Semaste    return m_uuid;
325254721Semaste}
326254721Semaste
327254721SemasteClangASTContext &
328254721SemasteModule::GetClangASTContext ()
329254721Semaste{
330254721Semaste    Mutex::Locker locker (m_mutex);
331254721Semaste    if (m_did_init_ast == false)
332254721Semaste    {
333254721Semaste        ObjectFile * objfile = GetObjectFile();
334254721Semaste        ArchSpec object_arch;
335254721Semaste        if (objfile && objfile->GetArchitecture(object_arch))
336254721Semaste        {
337254721Semaste            m_did_init_ast = true;
338254721Semaste
339254721Semaste            // LLVM wants this to be set to iOS or MacOSX; if we're working on
340254721Semaste            // a bare-boards type image, change the triple for llvm's benefit.
341254721Semaste            if (object_arch.GetTriple().getVendor() == llvm::Triple::Apple
342254721Semaste                && object_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
343254721Semaste            {
344254721Semaste                if (object_arch.GetTriple().getArch() == llvm::Triple::arm ||
345254721Semaste                    object_arch.GetTriple().getArch() == llvm::Triple::thumb)
346254721Semaste                {
347254721Semaste                    object_arch.GetTriple().setOS(llvm::Triple::IOS);
348254721Semaste                }
349254721Semaste                else
350254721Semaste                {
351254721Semaste                    object_arch.GetTriple().setOS(llvm::Triple::MacOSX);
352254721Semaste                }
353254721Semaste            }
354254721Semaste            m_ast.SetArchitecture (object_arch);
355254721Semaste        }
356254721Semaste    }
357254721Semaste    return m_ast;
358254721Semaste}
359254721Semaste
360254721Semastevoid
361254721SemasteModule::ParseAllDebugSymbols()
362254721Semaste{
363254721Semaste    Mutex::Locker locker (m_mutex);
364254721Semaste    size_t num_comp_units = GetNumCompileUnits();
365254721Semaste    if (num_comp_units == 0)
366254721Semaste        return;
367254721Semaste
368254721Semaste    SymbolContext sc;
369254721Semaste    sc.module_sp = shared_from_this();
370254721Semaste    SymbolVendor *symbols = GetSymbolVendor ();
371254721Semaste
372254721Semaste    for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
373254721Semaste    {
374254721Semaste        sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
375254721Semaste        if (sc.comp_unit)
376254721Semaste        {
377254721Semaste            sc.function = NULL;
378254721Semaste            symbols->ParseVariablesForContext(sc);
379254721Semaste
380254721Semaste            symbols->ParseCompileUnitFunctions(sc);
381254721Semaste
382254721Semaste            for (size_t func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx)
383254721Semaste            {
384254721Semaste                symbols->ParseFunctionBlocks(sc);
385254721Semaste
386254721Semaste                // Parse the variables for this function and all its blocks
387254721Semaste                symbols->ParseVariablesForContext(sc);
388254721Semaste            }
389254721Semaste
390254721Semaste
391254721Semaste            // Parse all types for this compile unit
392254721Semaste            sc.function = NULL;
393254721Semaste            symbols->ParseTypes(sc);
394254721Semaste        }
395254721Semaste    }
396254721Semaste}
397254721Semaste
398254721Semastevoid
399254721SemasteModule::CalculateSymbolContext(SymbolContext* sc)
400254721Semaste{
401254721Semaste    sc->module_sp = shared_from_this();
402254721Semaste}
403254721Semaste
404254721SemasteModuleSP
405254721SemasteModule::CalculateSymbolContextModule ()
406254721Semaste{
407254721Semaste    return shared_from_this();
408254721Semaste}
409254721Semaste
410254721Semastevoid
411254721SemasteModule::DumpSymbolContext(Stream *s)
412254721Semaste{
413254721Semaste    s->Printf(", Module{%p}", this);
414254721Semaste}
415254721Semaste
416254721Semastesize_t
417254721SemasteModule::GetNumCompileUnits()
418254721Semaste{
419254721Semaste    Mutex::Locker locker (m_mutex);
420254721Semaste    Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
421254721Semaste    SymbolVendor *symbols = GetSymbolVendor ();
422254721Semaste    if (symbols)
423254721Semaste        return symbols->GetNumCompileUnits();
424254721Semaste    return 0;
425254721Semaste}
426254721Semaste
427254721SemasteCompUnitSP
428254721SemasteModule::GetCompileUnitAtIndex (size_t index)
429254721Semaste{
430254721Semaste    Mutex::Locker locker (m_mutex);
431254721Semaste    size_t num_comp_units = GetNumCompileUnits ();
432254721Semaste    CompUnitSP cu_sp;
433254721Semaste
434254721Semaste    if (index < num_comp_units)
435254721Semaste    {
436254721Semaste        SymbolVendor *symbols = GetSymbolVendor ();
437254721Semaste        if (symbols)
438254721Semaste            cu_sp = symbols->GetCompileUnitAtIndex(index);
439254721Semaste    }
440254721Semaste    return cu_sp;
441254721Semaste}
442254721Semaste
443254721Semastebool
444254721SemasteModule::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
445254721Semaste{
446254721Semaste    Mutex::Locker locker (m_mutex);
447254721Semaste    Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
448254721Semaste    SectionList *section_list = GetSectionList();
449254721Semaste    if (section_list)
450254721Semaste        return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
451254721Semaste    return false;
452254721Semaste}
453254721Semaste
454254721Semasteuint32_t
455263363SemasteModule::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc,
456263363Semaste                                        bool resolve_tail_call_address)
457254721Semaste{
458254721Semaste    Mutex::Locker locker (m_mutex);
459254721Semaste    uint32_t resolved_flags = 0;
460254721Semaste
461254721Semaste    // Clear the result symbol context in case we don't find anything, but don't clear the target
462254721Semaste    sc.Clear(false);
463254721Semaste
464254721Semaste    // Get the section from the section/offset address.
465254721Semaste    SectionSP section_sp (so_addr.GetSection());
466254721Semaste
467254721Semaste    // Make sure the section matches this module before we try and match anything
468254721Semaste    if (section_sp && section_sp->GetModule().get() == this)
469254721Semaste    {
470254721Semaste        // If the section offset based address resolved itself, then this
471254721Semaste        // is the right module.
472254721Semaste        sc.module_sp = shared_from_this();
473254721Semaste        resolved_flags |= eSymbolContextModule;
474254721Semaste
475263363Semaste        SymbolVendor* sym_vendor = GetSymbolVendor();
476263363Semaste        if (!sym_vendor)
477263363Semaste            return resolved_flags;
478263363Semaste
479254721Semaste        // Resolve the compile unit, function, block, line table or line
480254721Semaste        // entry if requested.
481254721Semaste        if (resolve_scope & eSymbolContextCompUnit    ||
482254721Semaste            resolve_scope & eSymbolContextFunction    ||
483254721Semaste            resolve_scope & eSymbolContextBlock       ||
484254721Semaste            resolve_scope & eSymbolContextLineEntry   )
485254721Semaste        {
486263363Semaste            resolved_flags |= sym_vendor->ResolveSymbolContext (so_addr, resolve_scope, sc);
487254721Semaste        }
488254721Semaste
489254721Semaste        // Resolve the symbol if requested, but don't re-look it up if we've already found it.
490254721Semaste        if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
491254721Semaste        {
492263363Semaste            Symtab *symtab = sym_vendor->GetSymtab();
493263363Semaste            if (symtab && so_addr.IsSectionOffset())
494254721Semaste            {
495263363Semaste                sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
496263363Semaste                if (!sc.symbol &&
497263363Semaste                    resolve_scope & eSymbolContextFunction && !(resolved_flags & eSymbolContextFunction))
498254721Semaste                {
499263363Semaste                    bool verify_unique = false; // No need to check again since ResolveSymbolContext failed to find a symbol at this address.
500263363Semaste                    if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
501263363Semaste                        sc.symbol = obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
502263363Semaste                }
503263363Semaste
504263363Semaste                if (sc.symbol)
505263363Semaste                {
506263363Semaste                    if (sc.symbol->IsSynthetic())
507254721Semaste                    {
508263363Semaste                        // We have a synthetic symbol so lets check if the object file
509263363Semaste                        // from the symbol file in the symbol vendor is different than
510263363Semaste                        // the object file for the module, and if so search its symbol
511263363Semaste                        // table to see if we can come up with a better symbol. For example
512263363Semaste                        // dSYM files on MacOSX have an unstripped symbol table inside of
513263363Semaste                        // them.
514263363Semaste                        ObjectFile *symtab_objfile = symtab->GetObjectFile();
515263363Semaste                        if (symtab_objfile && symtab_objfile->IsStripped())
516263363Semaste                        {
517263363Semaste                            SymbolFile *symfile = sym_vendor->GetSymbolFile();
518263363Semaste                            if (symfile)
519263363Semaste                            {
520263363Semaste                                ObjectFile *symfile_objfile = symfile->GetObjectFile();
521263363Semaste                                if (symfile_objfile != symtab_objfile)
522263363Semaste                                {
523263363Semaste                                    Symtab *symfile_symtab = symfile_objfile->GetSymtab();
524263363Semaste                                    if (symfile_symtab)
525263363Semaste                                    {
526263363Semaste                                        Symbol *symbol = symfile_symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
527263363Semaste                                        if (symbol && !symbol->IsSynthetic())
528263363Semaste                                        {
529263363Semaste                                            sc.symbol = symbol;
530263363Semaste                                        }
531263363Semaste                                    }
532263363Semaste                                }
533263363Semaste                            }
534263363Semaste                        }
535254721Semaste                    }
536263363Semaste                    resolved_flags |= eSymbolContextSymbol;
537254721Semaste                }
538254721Semaste            }
539254721Semaste        }
540263363Semaste
541263363Semaste        // For function symbols, so_addr may be off by one.  This is a convention consistent
542263363Semaste        // with FDE row indices in eh_frame sections, but requires extra logic here to permit
543263363Semaste        // symbol lookup for disassembly and unwind.
544263363Semaste        if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol) &&
545263363Semaste            resolve_tail_call_address && so_addr.IsSectionOffset())
546263363Semaste        {
547263363Semaste            Address previous_addr = so_addr;
548263363Semaste            previous_addr.Slide(-1);
549263363Semaste
550263363Semaste            bool do_resolve_tail_call_address = false; // prevent recursion
551263363Semaste            const uint32_t flags = ResolveSymbolContextForAddress(previous_addr, resolve_scope, sc,
552263363Semaste                                                                  do_resolve_tail_call_address);
553263363Semaste            if (flags & eSymbolContextSymbol)
554263363Semaste            {
555263363Semaste                AddressRange addr_range;
556263363Semaste                if (sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
557263363Semaste                {
558263363Semaste                    if (addr_range.GetBaseAddress().GetSection() == so_addr.GetSection())
559263363Semaste                    {
560263363Semaste                        // If the requested address is one past the address range of a function (i.e. a tail call),
561263363Semaste                        // or the decremented address is the start of a function (i.e. some forms of trampoline),
562263363Semaste                        // indicate that the symbol has been resolved.
563263363Semaste                        if (so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() ||
564263363Semaste                            so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() + addr_range.GetByteSize())
565263363Semaste                        {
566263363Semaste                            resolved_flags |= flags;
567263363Semaste                        }
568263363Semaste                    }
569263363Semaste                    else
570263363Semaste                    {
571263363Semaste                        sc.symbol = nullptr; // Don't trust the symbol if the sections didn't match.
572263363Semaste                    }
573263363Semaste                }
574263363Semaste            }
575263363Semaste        }
576254721Semaste    }
577254721Semaste    return resolved_flags;
578254721Semaste}
579254721Semaste
580254721Semasteuint32_t
581254721SemasteModule::ResolveSymbolContextForFilePath
582254721Semaste(
583254721Semaste    const char *file_path,
584254721Semaste    uint32_t line,
585254721Semaste    bool check_inlines,
586254721Semaste    uint32_t resolve_scope,
587254721Semaste    SymbolContextList& sc_list
588254721Semaste)
589254721Semaste{
590254721Semaste    FileSpec file_spec(file_path, false);
591254721Semaste    return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
592254721Semaste}
593254721Semaste
594254721Semasteuint32_t
595254721SemasteModule::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
596254721Semaste{
597254721Semaste    Mutex::Locker locker (m_mutex);
598254721Semaste    Timer scoped_timer(__PRETTY_FUNCTION__,
599254721Semaste                       "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
600254721Semaste                       file_spec.GetPath().c_str(),
601254721Semaste                       line,
602254721Semaste                       check_inlines ? "yes" : "no",
603254721Semaste                       resolve_scope);
604254721Semaste
605254721Semaste    const uint32_t initial_count = sc_list.GetSize();
606254721Semaste
607254721Semaste    SymbolVendor *symbols = GetSymbolVendor  ();
608254721Semaste    if (symbols)
609254721Semaste        symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
610254721Semaste
611254721Semaste    return sc_list.GetSize() - initial_count;
612254721Semaste}
613254721Semaste
614254721Semaste
615254721Semastesize_t
616254721SemasteModule::FindGlobalVariables (const ConstString &name,
617254721Semaste                             const ClangNamespaceDecl *namespace_decl,
618254721Semaste                             bool append,
619254721Semaste                             size_t max_matches,
620254721Semaste                             VariableList& variables)
621254721Semaste{
622254721Semaste    SymbolVendor *symbols = GetSymbolVendor ();
623254721Semaste    if (symbols)
624254721Semaste        return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
625254721Semaste    return 0;
626254721Semaste}
627254721Semaste
628254721Semastesize_t
629254721SemasteModule::FindGlobalVariables (const RegularExpression& regex,
630254721Semaste                             bool append,
631254721Semaste                             size_t max_matches,
632254721Semaste                             VariableList& variables)
633254721Semaste{
634254721Semaste    SymbolVendor *symbols = GetSymbolVendor ();
635254721Semaste    if (symbols)
636254721Semaste        return symbols->FindGlobalVariables(regex, append, max_matches, variables);
637254721Semaste    return 0;
638254721Semaste}
639254721Semaste
640254721Semastesize_t
641254721SemasteModule::FindCompileUnits (const FileSpec &path,
642254721Semaste                          bool append,
643254721Semaste                          SymbolContextList &sc_list)
644254721Semaste{
645254721Semaste    if (!append)
646254721Semaste        sc_list.Clear();
647254721Semaste
648254721Semaste    const size_t start_size = sc_list.GetSize();
649254721Semaste    const size_t num_compile_units = GetNumCompileUnits();
650254721Semaste    SymbolContext sc;
651254721Semaste    sc.module_sp = shared_from_this();
652263363Semaste    const bool compare_directory = (bool)path.GetDirectory();
653254721Semaste    for (size_t i=0; i<num_compile_units; ++i)
654254721Semaste    {
655254721Semaste        sc.comp_unit = GetCompileUnitAtIndex(i).get();
656254721Semaste        if (sc.comp_unit)
657254721Semaste        {
658254721Semaste            if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
659254721Semaste                sc_list.Append(sc);
660254721Semaste        }
661254721Semaste    }
662254721Semaste    return sc_list.GetSize() - start_size;
663254721Semaste}
664254721Semaste
665254721Semastesize_t
666254721SemasteModule::FindFunctions (const ConstString &name,
667254721Semaste                       const ClangNamespaceDecl *namespace_decl,
668254721Semaste                       uint32_t name_type_mask,
669254721Semaste                       bool include_symbols,
670254721Semaste                       bool include_inlines,
671254721Semaste                       bool append,
672254721Semaste                       SymbolContextList& sc_list)
673254721Semaste{
674254721Semaste    if (!append)
675254721Semaste        sc_list.Clear();
676254721Semaste
677254721Semaste    const size_t old_size = sc_list.GetSize();
678254721Semaste
679254721Semaste    // Find all the functions (not symbols, but debug information functions...
680254721Semaste    SymbolVendor *symbols = GetSymbolVendor ();
681254721Semaste
682254721Semaste    if (name_type_mask & eFunctionNameTypeAuto)
683254721Semaste    {
684254721Semaste        ConstString lookup_name;
685254721Semaste        uint32_t lookup_name_type_mask = 0;
686254721Semaste        bool match_name_after_lookup = false;
687254721Semaste        Module::PrepareForFunctionNameLookup (name,
688254721Semaste                                              name_type_mask,
689254721Semaste                                              lookup_name,
690254721Semaste                                              lookup_name_type_mask,
691254721Semaste                                              match_name_after_lookup);
692254721Semaste
693254721Semaste        if (symbols)
694254721Semaste        {
695254721Semaste            symbols->FindFunctions(lookup_name,
696254721Semaste                                   namespace_decl,
697254721Semaste                                   lookup_name_type_mask,
698254721Semaste                                   include_inlines,
699254721Semaste                                   append,
700254721Semaste                                   sc_list);
701254721Semaste
702254721Semaste            // Now check our symbol table for symbols that are code symbols if requested
703254721Semaste            if (include_symbols)
704254721Semaste            {
705254721Semaste                Symtab *symtab = symbols->GetSymtab();
706254721Semaste                if (symtab)
707254721Semaste                    symtab->FindFunctionSymbols(lookup_name, lookup_name_type_mask, sc_list);
708254721Semaste            }
709254721Semaste        }
710254721Semaste
711254721Semaste        if (match_name_after_lookup)
712254721Semaste        {
713254721Semaste            SymbolContext sc;
714254721Semaste            size_t i = old_size;
715254721Semaste            while (i<sc_list.GetSize())
716254721Semaste            {
717254721Semaste                if (sc_list.GetContextAtIndex(i, sc))
718254721Semaste                {
719254721Semaste                    const char *func_name = sc.GetFunctionName().GetCString();
720254721Semaste                    if (func_name && strstr (func_name, name.GetCString()) == NULL)
721254721Semaste                    {
722254721Semaste                        // Remove the current context
723254721Semaste                        sc_list.RemoveContextAtIndex(i);
724254721Semaste                        // Don't increment i and continue in the loop
725254721Semaste                        continue;
726254721Semaste                    }
727254721Semaste                }
728254721Semaste                ++i;
729254721Semaste            }
730254721Semaste        }
731254721Semaste    }
732254721Semaste    else
733254721Semaste    {
734254721Semaste        if (symbols)
735254721Semaste        {
736254721Semaste            symbols->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list);
737254721Semaste
738254721Semaste            // Now check our symbol table for symbols that are code symbols if requested
739254721Semaste            if (include_symbols)
740254721Semaste            {
741254721Semaste                Symtab *symtab = symbols->GetSymtab();
742254721Semaste                if (symtab)
743254721Semaste                    symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
744254721Semaste            }
745254721Semaste        }
746254721Semaste    }
747254721Semaste
748254721Semaste    return sc_list.GetSize() - old_size;
749254721Semaste}
750254721Semaste
751254721Semastesize_t
752254721SemasteModule::FindFunctions (const RegularExpression& regex,
753254721Semaste                       bool include_symbols,
754254721Semaste                       bool include_inlines,
755254721Semaste                       bool append,
756254721Semaste                       SymbolContextList& sc_list)
757254721Semaste{
758254721Semaste    if (!append)
759254721Semaste        sc_list.Clear();
760254721Semaste
761254721Semaste    const size_t start_size = sc_list.GetSize();
762254721Semaste
763254721Semaste    SymbolVendor *symbols = GetSymbolVendor ();
764254721Semaste    if (symbols)
765254721Semaste    {
766254721Semaste        symbols->FindFunctions(regex, include_inlines, append, sc_list);
767254721Semaste
768254721Semaste        // Now check our symbol table for symbols that are code symbols if requested
769254721Semaste        if (include_symbols)
770254721Semaste        {
771254721Semaste            Symtab *symtab = symbols->GetSymtab();
772254721Semaste            if (symtab)
773254721Semaste            {
774254721Semaste                std::vector<uint32_t> symbol_indexes;
775254721Semaste                symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
776254721Semaste                const size_t num_matches = symbol_indexes.size();
777254721Semaste                if (num_matches)
778254721Semaste                {
779254721Semaste                    SymbolContext sc(this);
780254721Semaste                    const size_t end_functions_added_index = sc_list.GetSize();
781254721Semaste                    size_t num_functions_added_to_sc_list = end_functions_added_index - start_size;
782254721Semaste                    if (num_functions_added_to_sc_list == 0)
783254721Semaste                    {
784254721Semaste                        // No functions were added, just symbols, so we can just append them
785254721Semaste                        for (size_t i=0; i<num_matches; ++i)
786254721Semaste                        {
787254721Semaste                            sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
788254721Semaste                            SymbolType sym_type = sc.symbol->GetType();
789254721Semaste                            if (sc.symbol && (sym_type == eSymbolTypeCode ||
790254721Semaste                                              sym_type == eSymbolTypeResolver))
791254721Semaste                                sc_list.Append(sc);
792254721Semaste                        }
793254721Semaste                    }
794254721Semaste                    else
795254721Semaste                    {
796254721Semaste                        typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
797254721Semaste                        FileAddrToIndexMap file_addr_to_index;
798254721Semaste                        for (size_t i=start_size; i<end_functions_added_index; ++i)
799254721Semaste                        {
800254721Semaste                            const SymbolContext &sc = sc_list[i];
801254721Semaste                            if (sc.block)
802254721Semaste                                continue;
803254721Semaste                            file_addr_to_index[sc.function->GetAddressRange().GetBaseAddress().GetFileAddress()] = i;
804254721Semaste                        }
805254721Semaste
806254721Semaste                        FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
807254721Semaste                        // Functions were added so we need to merge symbols into any
808254721Semaste                        // existing function symbol contexts
809254721Semaste                        for (size_t i=start_size; i<num_matches; ++i)
810254721Semaste                        {
811254721Semaste                            sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
812254721Semaste                            SymbolType sym_type = sc.symbol->GetType();
813254721Semaste                            if (sc.symbol && (sym_type == eSymbolTypeCode ||
814254721Semaste                                              sym_type == eSymbolTypeResolver))
815254721Semaste                            {
816254721Semaste                                FileAddrToIndexMap::const_iterator pos = file_addr_to_index.find(sc.symbol->GetAddress().GetFileAddress());
817254721Semaste                                if (pos == end)
818254721Semaste                                    sc_list.Append(sc);
819254721Semaste                                else
820254721Semaste                                    sc_list[pos->second].symbol = sc.symbol;
821254721Semaste                            }
822254721Semaste                        }
823254721Semaste                    }
824254721Semaste                }
825254721Semaste            }
826254721Semaste        }
827254721Semaste    }
828254721Semaste    return sc_list.GetSize() - start_size;
829254721Semaste}
830254721Semaste
831263363Semastevoid
832263363SemasteModule::FindAddressesForLine (const lldb::TargetSP target_sp,
833263363Semaste                              const FileSpec &file, uint32_t line,
834263363Semaste                              Function *function,
835263363Semaste                              std::vector<Address> &output_local, std::vector<Address> &output_extern)
836263363Semaste{
837263363Semaste    SearchFilterByModule filter(target_sp, m_file);
838263363Semaste    AddressResolverFileLine resolver(file, line, true);
839263363Semaste    resolver.ResolveAddress (filter);
840263363Semaste
841263363Semaste    for (size_t n=0;n<resolver.GetNumberOfAddresses();n++)
842263363Semaste    {
843263363Semaste        Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
844263363Semaste        Function *f = addr.CalculateSymbolContextFunction();
845263363Semaste        if (f && f == function)
846263363Semaste            output_local.push_back (addr);
847263363Semaste        else
848263363Semaste            output_extern.push_back (addr);
849263363Semaste    }
850263363Semaste}
851263363Semaste
852254721Semastesize_t
853254721SemasteModule::FindTypes_Impl (const SymbolContext& sc,
854254721Semaste                        const ConstString &name,
855254721Semaste                        const ClangNamespaceDecl *namespace_decl,
856254721Semaste                        bool append,
857254721Semaste                        size_t max_matches,
858254721Semaste                        TypeList& types)
859254721Semaste{
860254721Semaste    Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
861254721Semaste    if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
862254721Semaste    {
863254721Semaste        SymbolVendor *symbols = GetSymbolVendor ();
864254721Semaste        if (symbols)
865254721Semaste            return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
866254721Semaste    }
867254721Semaste    return 0;
868254721Semaste}
869254721Semaste
870254721Semastesize_t
871254721SemasteModule::FindTypesInNamespace (const SymbolContext& sc,
872254721Semaste                              const ConstString &type_name,
873254721Semaste                              const ClangNamespaceDecl *namespace_decl,
874254721Semaste                              size_t max_matches,
875254721Semaste                              TypeList& type_list)
876254721Semaste{
877254721Semaste    const bool append = true;
878254721Semaste    return FindTypes_Impl(sc, type_name, namespace_decl, append, max_matches, type_list);
879254721Semaste}
880254721Semaste
881254721Semastelldb::TypeSP
882254721SemasteModule::FindFirstType (const SymbolContext& sc,
883254721Semaste                       const ConstString &name,
884254721Semaste                       bool exact_match)
885254721Semaste{
886254721Semaste    TypeList type_list;
887254721Semaste    const size_t num_matches = FindTypes (sc, name, exact_match, 1, type_list);
888254721Semaste    if (num_matches)
889254721Semaste        return type_list.GetTypeAtIndex(0);
890254721Semaste    return TypeSP();
891254721Semaste}
892254721Semaste
893254721Semaste
894254721Semastesize_t
895254721SemasteModule::FindTypes (const SymbolContext& sc,
896254721Semaste                   const ConstString &name,
897254721Semaste                   bool exact_match,
898254721Semaste                   size_t max_matches,
899254721Semaste                   TypeList& types)
900254721Semaste{
901254721Semaste    size_t num_matches = 0;
902254721Semaste    const char *type_name_cstr = name.GetCString();
903254721Semaste    std::string type_scope;
904254721Semaste    std::string type_basename;
905254721Semaste    const bool append = true;
906254721Semaste    TypeClass type_class = eTypeClassAny;
907254721Semaste    if (Type::GetTypeScopeAndBasename (type_name_cstr, type_scope, type_basename, type_class))
908254721Semaste    {
909254721Semaste        // Check if "name" starts with "::" which means the qualified type starts
910254721Semaste        // from the root namespace and implies and exact match. The typenames we
911254721Semaste        // get back from clang do not start with "::" so we need to strip this off
912254721Semaste        // in order to get the qualfied names to match
913254721Semaste
914254721Semaste        if (type_scope.size() >= 2 && type_scope[0] == ':' && type_scope[1] == ':')
915254721Semaste        {
916254721Semaste            type_scope.erase(0,2);
917254721Semaste            exact_match = true;
918254721Semaste        }
919254721Semaste        ConstString type_basename_const_str (type_basename.c_str());
920254721Semaste        if (FindTypes_Impl(sc, type_basename_const_str, NULL, append, max_matches, types))
921254721Semaste        {
922254721Semaste            types.RemoveMismatchedTypes (type_scope, type_basename, type_class, exact_match);
923254721Semaste            num_matches = types.GetSize();
924254721Semaste        }
925254721Semaste    }
926254721Semaste    else
927254721Semaste    {
928254721Semaste        // The type is not in a namespace/class scope, just search for it by basename
929254721Semaste        if (type_class != eTypeClassAny)
930254721Semaste        {
931254721Semaste            // The "type_name_cstr" will have been modified if we have a valid type class
932254721Semaste            // prefix (like "struct", "class", "union", "typedef" etc).
933254721Semaste            num_matches = FindTypes_Impl(sc, ConstString(type_name_cstr), NULL, append, max_matches, types);
934254721Semaste            types.RemoveMismatchedTypes (type_class);
935254721Semaste            num_matches = types.GetSize();
936254721Semaste        }
937254721Semaste        else
938254721Semaste        {
939254721Semaste            num_matches = FindTypes_Impl(sc, name, NULL, append, max_matches, types);
940254721Semaste        }
941254721Semaste    }
942254721Semaste
943254721Semaste    return num_matches;
944254721Semaste
945254721Semaste}
946254721Semaste
947254721SemasteSymbolVendor*
948254721SemasteModule::GetSymbolVendor (bool can_create, lldb_private::Stream *feedback_strm)
949254721Semaste{
950254721Semaste    Mutex::Locker locker (m_mutex);
951254721Semaste    if (m_did_load_symbol_vendor == false && can_create)
952254721Semaste    {
953254721Semaste        ObjectFile *obj_file = GetObjectFile ();
954254721Semaste        if (obj_file != NULL)
955254721Semaste        {
956254721Semaste            Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
957254721Semaste            m_symfile_ap.reset(SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
958254721Semaste            m_did_load_symbol_vendor = true;
959254721Semaste        }
960254721Semaste    }
961254721Semaste    return m_symfile_ap.get();
962254721Semaste}
963254721Semaste
964254721Semastevoid
965254721SemasteModule::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
966254721Semaste{
967254721Semaste    // Container objects whose paths do not specify a file directly can call
968254721Semaste    // this function to correct the file and object names.
969254721Semaste    m_file = file;
970254721Semaste    m_mod_time = file.GetModificationTime();
971254721Semaste    m_object_name = object_name;
972254721Semaste}
973254721Semaste
974254721Semasteconst ArchSpec&
975254721SemasteModule::GetArchitecture () const
976254721Semaste{
977254721Semaste    return m_arch;
978254721Semaste}
979254721Semaste
980254721Semastestd::string
981254721SemasteModule::GetSpecificationDescription () const
982254721Semaste{
983254721Semaste    std::string spec(GetFileSpec().GetPath());
984254721Semaste    if (m_object_name)
985254721Semaste    {
986254721Semaste        spec += '(';
987254721Semaste        spec += m_object_name.GetCString();
988254721Semaste        spec += ')';
989254721Semaste    }
990254721Semaste    return spec;
991254721Semaste}
992254721Semaste
993254721Semastevoid
994254721SemasteModule::GetDescription (Stream *s, lldb::DescriptionLevel level)
995254721Semaste{
996254721Semaste    Mutex::Locker locker (m_mutex);
997254721Semaste
998254721Semaste    if (level >= eDescriptionLevelFull)
999254721Semaste    {
1000254721Semaste        if (m_arch.IsValid())
1001254721Semaste            s->Printf("(%s) ", m_arch.GetArchitectureName());
1002254721Semaste    }
1003254721Semaste
1004254721Semaste    if (level == eDescriptionLevelBrief)
1005254721Semaste    {
1006254721Semaste        const char *filename = m_file.GetFilename().GetCString();
1007254721Semaste        if (filename)
1008254721Semaste            s->PutCString (filename);
1009254721Semaste    }
1010254721Semaste    else
1011254721Semaste    {
1012254721Semaste        char path[PATH_MAX];
1013254721Semaste        if (m_file.GetPath(path, sizeof(path)))
1014254721Semaste            s->PutCString(path);
1015254721Semaste    }
1016254721Semaste
1017254721Semaste    const char *object_name = m_object_name.GetCString();
1018254721Semaste    if (object_name)
1019254721Semaste        s->Printf("(%s)", object_name);
1020254721Semaste}
1021254721Semaste
1022254721Semastevoid
1023254721SemasteModule::ReportError (const char *format, ...)
1024254721Semaste{
1025254721Semaste    if (format && format[0])
1026254721Semaste    {
1027254721Semaste        StreamString strm;
1028254721Semaste        strm.PutCString("error: ");
1029254721Semaste        GetDescription(&strm, lldb::eDescriptionLevelBrief);
1030254721Semaste        strm.PutChar (' ');
1031254721Semaste        va_list args;
1032254721Semaste        va_start (args, format);
1033254721Semaste        strm.PrintfVarArg(format, args);
1034254721Semaste        va_end (args);
1035254721Semaste
1036254721Semaste        const int format_len = strlen(format);
1037254721Semaste        if (format_len > 0)
1038254721Semaste        {
1039254721Semaste            const char last_char = format[format_len-1];
1040254721Semaste            if (last_char != '\n' || last_char != '\r')
1041254721Semaste                strm.EOL();
1042254721Semaste        }
1043254721Semaste        Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1044254721Semaste
1045254721Semaste    }
1046254721Semaste}
1047254721Semaste
1048254721Semastebool
1049254721SemasteModule::FileHasChanged () const
1050254721Semaste{
1051254721Semaste    if (m_file_has_changed == false)
1052254721Semaste        m_file_has_changed = (m_file.GetModificationTime() != m_mod_time);
1053254721Semaste    return m_file_has_changed;
1054254721Semaste}
1055254721Semaste
1056254721Semastevoid
1057254721SemasteModule::ReportErrorIfModifyDetected (const char *format, ...)
1058254721Semaste{
1059254721Semaste    if (m_first_file_changed_log == false)
1060254721Semaste    {
1061254721Semaste        if (FileHasChanged ())
1062254721Semaste        {
1063254721Semaste            m_first_file_changed_log = true;
1064254721Semaste            if (format)
1065254721Semaste            {
1066254721Semaste                StreamString strm;
1067254721Semaste                strm.PutCString("error: the object file ");
1068254721Semaste                GetDescription(&strm, lldb::eDescriptionLevelFull);
1069254721Semaste                strm.PutCString (" has been modified\n");
1070254721Semaste
1071254721Semaste                va_list args;
1072254721Semaste                va_start (args, format);
1073254721Semaste                strm.PrintfVarArg(format, args);
1074254721Semaste                va_end (args);
1075254721Semaste
1076254721Semaste                const int format_len = strlen(format);
1077254721Semaste                if (format_len > 0)
1078254721Semaste                {
1079254721Semaste                    const char last_char = format[format_len-1];
1080254721Semaste                    if (last_char != '\n' || last_char != '\r')
1081254721Semaste                        strm.EOL();
1082254721Semaste                }
1083254721Semaste                strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
1084254721Semaste                Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
1085254721Semaste            }
1086254721Semaste        }
1087254721Semaste    }
1088254721Semaste}
1089254721Semaste
1090254721Semastevoid
1091254721SemasteModule::ReportWarning (const char *format, ...)
1092254721Semaste{
1093254721Semaste    if (format && format[0])
1094254721Semaste    {
1095254721Semaste        StreamString strm;
1096254721Semaste        strm.PutCString("warning: ");
1097254721Semaste        GetDescription(&strm, lldb::eDescriptionLevelFull);
1098254721Semaste        strm.PutChar (' ');
1099254721Semaste
1100254721Semaste        va_list args;
1101254721Semaste        va_start (args, format);
1102254721Semaste        strm.PrintfVarArg(format, args);
1103254721Semaste        va_end (args);
1104254721Semaste
1105254721Semaste        const int format_len = strlen(format);
1106254721Semaste        if (format_len > 0)
1107254721Semaste        {
1108254721Semaste            const char last_char = format[format_len-1];
1109254721Semaste            if (last_char != '\n' || last_char != '\r')
1110254721Semaste                strm.EOL();
1111254721Semaste        }
1112254721Semaste        Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
1113254721Semaste    }
1114254721Semaste}
1115254721Semaste
1116254721Semastevoid
1117254721SemasteModule::LogMessage (Log *log, const char *format, ...)
1118254721Semaste{
1119254721Semaste    if (log)
1120254721Semaste    {
1121254721Semaste        StreamString log_message;
1122254721Semaste        GetDescription(&log_message, lldb::eDescriptionLevelFull);
1123254721Semaste        log_message.PutCString (": ");
1124254721Semaste        va_list args;
1125254721Semaste        va_start (args, format);
1126254721Semaste        log_message.PrintfVarArg (format, args);
1127254721Semaste        va_end (args);
1128254721Semaste        log->PutCString(log_message.GetString().c_str());
1129254721Semaste    }
1130254721Semaste}
1131254721Semaste
1132254721Semastevoid
1133254721SemasteModule::LogMessageVerboseBacktrace (Log *log, const char *format, ...)
1134254721Semaste{
1135254721Semaste    if (log)
1136254721Semaste    {
1137254721Semaste        StreamString log_message;
1138254721Semaste        GetDescription(&log_message, lldb::eDescriptionLevelFull);
1139254721Semaste        log_message.PutCString (": ");
1140254721Semaste        va_list args;
1141254721Semaste        va_start (args, format);
1142254721Semaste        log_message.PrintfVarArg (format, args);
1143254721Semaste        va_end (args);
1144254721Semaste        if (log->GetVerbose())
1145254721Semaste            Host::Backtrace (log_message, 1024);
1146254721Semaste        log->PutCString(log_message.GetString().c_str());
1147254721Semaste    }
1148254721Semaste}
1149254721Semaste
1150254721Semastevoid
1151254721SemasteModule::Dump(Stream *s)
1152254721Semaste{
1153254721Semaste    Mutex::Locker locker (m_mutex);
1154254721Semaste    //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1155254721Semaste    s->Indent();
1156254721Semaste    s->Printf("Module %s%s%s%s\n",
1157254721Semaste              m_file.GetPath().c_str(),
1158254721Semaste              m_object_name ? "(" : "",
1159254721Semaste              m_object_name ? m_object_name.GetCString() : "",
1160254721Semaste              m_object_name ? ")" : "");
1161254721Semaste
1162254721Semaste    s->IndentMore();
1163254721Semaste
1164254721Semaste    ObjectFile *objfile = GetObjectFile ();
1165254721Semaste    if (objfile)
1166254721Semaste        objfile->Dump(s);
1167254721Semaste
1168254721Semaste    SymbolVendor *symbols = GetSymbolVendor ();
1169254721Semaste    if (symbols)
1170254721Semaste        symbols->Dump(s);
1171254721Semaste
1172254721Semaste    s->IndentLess();
1173254721Semaste}
1174254721Semaste
1175254721Semaste
1176254721SemasteTypeList*
1177254721SemasteModule::GetTypeList ()
1178254721Semaste{
1179254721Semaste    SymbolVendor *symbols = GetSymbolVendor ();
1180254721Semaste    if (symbols)
1181254721Semaste        return &symbols->GetTypeList();
1182254721Semaste    return NULL;
1183254721Semaste}
1184254721Semaste
1185254721Semasteconst ConstString &
1186254721SemasteModule::GetObjectName() const
1187254721Semaste{
1188254721Semaste    return m_object_name;
1189254721Semaste}
1190254721Semaste
1191254721SemasteObjectFile *
1192254721SemasteModule::GetObjectFile()
1193254721Semaste{
1194254721Semaste    Mutex::Locker locker (m_mutex);
1195254721Semaste    if (m_did_load_objfile == false)
1196254721Semaste    {
1197254721Semaste        Timer scoped_timer(__PRETTY_FUNCTION__,
1198254721Semaste                           "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
1199254721Semaste        DataBufferSP data_sp;
1200254721Semaste        lldb::offset_t data_offset = 0;
1201254721Semaste        const lldb::offset_t file_size = m_file.GetByteSize();
1202254721Semaste        if (file_size > m_object_offset)
1203254721Semaste        {
1204254721Semaste            m_did_load_objfile = true;
1205254721Semaste            m_objfile_sp = ObjectFile::FindPlugin (shared_from_this(),
1206254721Semaste                                                   &m_file,
1207254721Semaste                                                   m_object_offset,
1208254721Semaste                                                   file_size - m_object_offset,
1209254721Semaste                                                   data_sp,
1210254721Semaste                                                   data_offset);
1211254721Semaste            if (m_objfile_sp)
1212254721Semaste            {
1213254721Semaste                // Once we get the object file, update our module with the object file's
1214254721Semaste                // architecture since it might differ in vendor/os if some parts were
1215254721Semaste                // unknown.
1216254721Semaste                m_objfile_sp->GetArchitecture (m_arch);
1217254721Semaste            }
1218254721Semaste        }
1219254721Semaste    }
1220254721Semaste    return m_objfile_sp.get();
1221254721Semaste}
1222254721Semaste
1223254721SemasteSectionList *
1224254721SemasteModule::GetSectionList()
1225254721Semaste{
1226254721Semaste    // Populate m_unified_sections_ap with sections from objfile.
1227254721Semaste    if (m_sections_ap.get() == NULL)
1228254721Semaste    {
1229254721Semaste        ObjectFile *obj_file = GetObjectFile();
1230254721Semaste        if (obj_file)
1231254721Semaste            obj_file->CreateSections(*GetUnifiedSectionList());
1232254721Semaste    }
1233254721Semaste    return m_sections_ap.get();
1234254721Semaste}
1235254721Semaste
1236254721SemasteSectionList *
1237254721SemasteModule::GetUnifiedSectionList()
1238254721Semaste{
1239254721Semaste    // Populate m_unified_sections_ap with sections from objfile.
1240254721Semaste    if (m_sections_ap.get() == NULL)
1241254721Semaste        m_sections_ap.reset(new SectionList());
1242254721Semaste    return m_sections_ap.get();
1243254721Semaste}
1244254721Semaste
1245254721Semasteconst Symbol *
1246254721SemasteModule::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
1247254721Semaste{
1248254721Semaste    Timer scoped_timer(__PRETTY_FUNCTION__,
1249254721Semaste                       "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1250254721Semaste                       name.AsCString(),
1251254721Semaste                       symbol_type);
1252254721Semaste    SymbolVendor* sym_vendor = GetSymbolVendor();
1253254721Semaste    if (sym_vendor)
1254254721Semaste    {
1255254721Semaste        Symtab *symtab = sym_vendor->GetSymtab();
1256254721Semaste        if (symtab)
1257254721Semaste            return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
1258254721Semaste    }
1259254721Semaste    return NULL;
1260254721Semaste}
1261254721Semastevoid
1262254721SemasteModule::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1263254721Semaste{
1264254721Semaste    // No need to protect this call using m_mutex all other method calls are
1265254721Semaste    // already thread safe.
1266254721Semaste
1267254721Semaste    size_t num_indices = symbol_indexes.size();
1268254721Semaste    if (num_indices > 0)
1269254721Semaste    {
1270254721Semaste        SymbolContext sc;
1271254721Semaste        CalculateSymbolContext (&sc);
1272254721Semaste        for (size_t i = 0; i < num_indices; i++)
1273254721Semaste        {
1274254721Semaste            sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
1275254721Semaste            if (sc.symbol)
1276254721Semaste                sc_list.Append (sc);
1277254721Semaste        }
1278254721Semaste    }
1279254721Semaste}
1280254721Semaste
1281254721Semastesize_t
1282254721SemasteModule::FindFunctionSymbols (const ConstString &name,
1283254721Semaste                             uint32_t name_type_mask,
1284254721Semaste                             SymbolContextList& sc_list)
1285254721Semaste{
1286254721Semaste    Timer scoped_timer(__PRETTY_FUNCTION__,
1287254721Semaste                       "Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1288254721Semaste                       name.AsCString(),
1289254721Semaste                       name_type_mask);
1290254721Semaste    SymbolVendor* sym_vendor = GetSymbolVendor();
1291254721Semaste    if (sym_vendor)
1292254721Semaste    {
1293254721Semaste        Symtab *symtab = sym_vendor->GetSymtab();
1294254721Semaste        if (symtab)
1295254721Semaste            return symtab->FindFunctionSymbols (name, name_type_mask, sc_list);
1296254721Semaste    }
1297254721Semaste    return 0;
1298254721Semaste}
1299254721Semaste
1300254721Semastesize_t
1301254721SemasteModule::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
1302254721Semaste{
1303254721Semaste    // No need to protect this call using m_mutex all other method calls are
1304254721Semaste    // already thread safe.
1305254721Semaste
1306254721Semaste
1307254721Semaste    Timer scoped_timer(__PRETTY_FUNCTION__,
1308254721Semaste                       "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
1309254721Semaste                       name.AsCString(),
1310254721Semaste                       symbol_type);
1311254721Semaste    const size_t initial_size = sc_list.GetSize();
1312254721Semaste    SymbolVendor* sym_vendor = GetSymbolVendor();
1313254721Semaste    if (sym_vendor)
1314254721Semaste    {
1315254721Semaste        Symtab *symtab = sym_vendor->GetSymtab();
1316254721Semaste        if (symtab)
1317254721Semaste        {
1318254721Semaste            std::vector<uint32_t> symbol_indexes;
1319254721Semaste            symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
1320254721Semaste            SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1321254721Semaste        }
1322254721Semaste    }
1323254721Semaste    return sc_list.GetSize() - initial_size;
1324254721Semaste}
1325254721Semaste
1326254721Semastesize_t
1327254721SemasteModule::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
1328254721Semaste{
1329254721Semaste    // No need to protect this call using m_mutex all other method calls are
1330254721Semaste    // already thread safe.
1331254721Semaste
1332254721Semaste    Timer scoped_timer(__PRETTY_FUNCTION__,
1333254721Semaste                       "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1334254721Semaste                       regex.GetText(),
1335254721Semaste                       symbol_type);
1336254721Semaste    const size_t initial_size = sc_list.GetSize();
1337254721Semaste    SymbolVendor* sym_vendor = GetSymbolVendor();
1338254721Semaste    if (sym_vendor)
1339254721Semaste    {
1340254721Semaste        Symtab *symtab = sym_vendor->GetSymtab();
1341254721Semaste        if (symtab)
1342254721Semaste        {
1343254721Semaste            std::vector<uint32_t> symbol_indexes;
1344254721Semaste            symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
1345254721Semaste            SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
1346254721Semaste        }
1347254721Semaste    }
1348254721Semaste    return sc_list.GetSize() - initial_size;
1349254721Semaste}
1350254721Semaste
1351254721Semastevoid
1352254721SemasteModule::SetSymbolFileFileSpec (const FileSpec &file)
1353254721Semaste{
1354254721Semaste    // Remove any sections in the unified section list that come from the current symbol vendor.
1355254721Semaste    if (m_symfile_ap)
1356254721Semaste    {
1357254721Semaste        SectionList *section_list = GetSectionList();
1358254721Semaste        SymbolFile *symbol_file = m_symfile_ap->GetSymbolFile();
1359254721Semaste        if (section_list && symbol_file)
1360254721Semaste        {
1361254721Semaste            ObjectFile *obj_file = symbol_file->GetObjectFile();
1362254721Semaste            // Make sure we have an object file and that the symbol vendor's objfile isn't
1363254721Semaste            // the same as the module's objfile before we remove any sections for it...
1364254721Semaste            if (obj_file && obj_file != m_objfile_sp.get())
1365254721Semaste            {
1366254721Semaste                size_t num_sections = section_list->GetNumSections (0);
1367254721Semaste                for (size_t idx = num_sections; idx > 0; --idx)
1368254721Semaste                {
1369254721Semaste                    lldb::SectionSP section_sp (section_list->GetSectionAtIndex (idx - 1));
1370254721Semaste                    if (section_sp->GetObjectFile() == obj_file)
1371254721Semaste                    {
1372254721Semaste                        section_list->DeleteSection (idx - 1);
1373254721Semaste                    }
1374254721Semaste                }
1375254721Semaste            }
1376254721Semaste        }
1377254721Semaste    }
1378254721Semaste
1379254721Semaste    m_symfile_spec = file;
1380254721Semaste    m_symfile_ap.reset();
1381254721Semaste    m_did_load_symbol_vendor = false;
1382254721Semaste}
1383254721Semaste
1384254721Semastebool
1385254721SemasteModule::IsExecutable ()
1386254721Semaste{
1387254721Semaste    if (GetObjectFile() == NULL)
1388254721Semaste        return false;
1389254721Semaste    else
1390254721Semaste        return GetObjectFile()->IsExecutable();
1391254721Semaste}
1392254721Semaste
1393254721Semastebool
1394254721SemasteModule::IsLoadedInTarget (Target *target)
1395254721Semaste{
1396254721Semaste    ObjectFile *obj_file = GetObjectFile();
1397254721Semaste    if (obj_file)
1398254721Semaste    {
1399254721Semaste        SectionList *sections = GetSectionList();
1400254721Semaste        if (sections != NULL)
1401254721Semaste        {
1402254721Semaste            size_t num_sections = sections->GetSize();
1403254721Semaste            for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
1404254721Semaste            {
1405254721Semaste                SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1406254721Semaste                if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
1407254721Semaste                {
1408254721Semaste                    return true;
1409254721Semaste                }
1410254721Semaste            }
1411254721Semaste        }
1412254721Semaste    }
1413254721Semaste    return false;
1414254721Semaste}
1415254721Semaste
1416254721Semastebool
1417254721SemasteModule::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* feedback_stream)
1418254721Semaste{
1419254721Semaste    if (!target)
1420254721Semaste    {
1421254721Semaste        error.SetErrorString("invalid destination Target");
1422254721Semaste        return false;
1423254721Semaste    }
1424254721Semaste
1425254721Semaste    LoadScriptFromSymFile shoud_load = target->TargetProperties::GetLoadScriptFromSymbolFile();
1426254721Semaste
1427254721Semaste    Debugger &debugger = target->GetDebugger();
1428254721Semaste    const ScriptLanguage script_language = debugger.GetScriptLanguage();
1429254721Semaste    if (script_language != eScriptLanguageNone)
1430254721Semaste    {
1431254721Semaste
1432254721Semaste        PlatformSP platform_sp(target->GetPlatform());
1433254721Semaste
1434254721Semaste        if (!platform_sp)
1435254721Semaste        {
1436254721Semaste            error.SetErrorString("invalid Platform");
1437254721Semaste            return false;
1438254721Semaste        }
1439254721Semaste
1440254721Semaste        FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources (target,
1441254721Semaste                                                                                   *this);
1442254721Semaste
1443254721Semaste
1444254721Semaste        const uint32_t num_specs = file_specs.GetSize();
1445254721Semaste        if (num_specs)
1446254721Semaste        {
1447254721Semaste            ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1448254721Semaste            if (script_interpreter)
1449254721Semaste            {
1450254721Semaste                for (uint32_t i=0; i<num_specs; ++i)
1451254721Semaste                {
1452254721Semaste                    FileSpec scripting_fspec (file_specs.GetFileSpecAtIndex(i));
1453254721Semaste                    if (scripting_fspec && scripting_fspec.Exists())
1454254721Semaste                    {
1455254721Semaste                        if (shoud_load == eLoadScriptFromSymFileFalse)
1456254721Semaste                            return false;
1457254721Semaste                        if (shoud_load == eLoadScriptFromSymFileWarn)
1458254721Semaste                        {
1459254721Semaste                            if (feedback_stream)
1460254721Semaste                                feedback_stream->Printf("warning: '%s' contains a debug script. To run this script in "
1461254721Semaste                                                        "this debug session:\n\n    command script import \"%s\"\n\n"
1462254721Semaste                                                        "To run all discovered debug scripts in this session:\n\n"
1463254721Semaste                                                        "    settings set target.load-script-from-symbol-file true\n",
1464254721Semaste                                                        GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1465254721Semaste                                                        scripting_fspec.GetPath().c_str());
1466254721Semaste                            return false;
1467254721Semaste                        }
1468254721Semaste                        StreamString scripting_stream;
1469254721Semaste                        scripting_fspec.Dump(&scripting_stream);
1470254721Semaste                        const bool can_reload = true;
1471254721Semaste                        const bool init_lldb_globals = false;
1472254721Semaste                        bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(),
1473254721Semaste                                                                                can_reload,
1474254721Semaste                                                                                init_lldb_globals,
1475254721Semaste                                                                                error);
1476254721Semaste                        if (!did_load)
1477254721Semaste                            return false;
1478254721Semaste                    }
1479254721Semaste                }
1480254721Semaste            }
1481254721Semaste            else
1482254721Semaste            {
1483254721Semaste                error.SetErrorString("invalid ScriptInterpreter");
1484254721Semaste                return false;
1485254721Semaste            }
1486254721Semaste        }
1487254721Semaste    }
1488254721Semaste    return true;
1489254721Semaste}
1490254721Semaste
1491254721Semastebool
1492254721SemasteModule::SetArchitecture (const ArchSpec &new_arch)
1493254721Semaste{
1494254721Semaste    if (!m_arch.IsValid())
1495254721Semaste    {
1496254721Semaste        m_arch = new_arch;
1497254721Semaste        return true;
1498254721Semaste    }
1499254721Semaste    return m_arch.IsExactMatch(new_arch);
1500254721Semaste}
1501254721Semaste
1502254721Semastebool
1503269024SemasteModule::SetLoadAddress (Target &target, lldb::addr_t value, bool value_is_offset, bool &changed)
1504254721Semaste{
1505269024Semaste    ObjectFile *object_file = GetObjectFile();
1506269024Semaste    if (object_file)
1507254721Semaste    {
1508269024Semaste        changed = object_file->SetLoadAddress(target, value, value_is_offset);
1509269024Semaste        return true;
1510254721Semaste    }
1511269024Semaste    else
1512269024Semaste    {
1513269024Semaste        changed = false;
1514269024Semaste    }
1515269024Semaste    return false;
1516254721Semaste}
1517254721Semaste
1518254721Semaste
1519254721Semastebool
1520254721SemasteModule::MatchesModuleSpec (const ModuleSpec &module_ref)
1521254721Semaste{
1522254721Semaste    const UUID &uuid = module_ref.GetUUID();
1523254721Semaste
1524254721Semaste    if (uuid.IsValid())
1525254721Semaste    {
1526254721Semaste        // If the UUID matches, then nothing more needs to match...
1527254721Semaste        if (uuid == GetUUID())
1528254721Semaste            return true;
1529254721Semaste        else
1530254721Semaste            return false;
1531254721Semaste    }
1532254721Semaste
1533254721Semaste    const FileSpec &file_spec = module_ref.GetFileSpec();
1534254721Semaste    if (file_spec)
1535254721Semaste    {
1536263363Semaste        if (!FileSpec::Equal (file_spec, m_file, (bool)file_spec.GetDirectory()))
1537254721Semaste            return false;
1538254721Semaste    }
1539254721Semaste
1540254721Semaste    const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1541254721Semaste    if (platform_file_spec)
1542254721Semaste    {
1543263363Semaste        if (!FileSpec::Equal (platform_file_spec, GetPlatformFileSpec (), (bool)platform_file_spec.GetDirectory()))
1544254721Semaste            return false;
1545254721Semaste    }
1546254721Semaste
1547254721Semaste    const ArchSpec &arch = module_ref.GetArchitecture();
1548254721Semaste    if (arch.IsValid())
1549254721Semaste    {
1550254721Semaste        if (!m_arch.IsCompatibleMatch(arch))
1551254721Semaste            return false;
1552254721Semaste    }
1553254721Semaste
1554254721Semaste    const ConstString &object_name = module_ref.GetObjectName();
1555254721Semaste    if (object_name)
1556254721Semaste    {
1557254721Semaste        if (object_name != GetObjectName())
1558254721Semaste            return false;
1559254721Semaste    }
1560254721Semaste    return true;
1561254721Semaste}
1562254721Semaste
1563254721Semastebool
1564254721SemasteModule::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
1565254721Semaste{
1566254721Semaste    Mutex::Locker locker (m_mutex);
1567254721Semaste    return m_source_mappings.FindFile (orig_spec, new_spec);
1568254721Semaste}
1569254721Semaste
1570254721Semastebool
1571254721SemasteModule::RemapSourceFile (const char *path, std::string &new_path) const
1572254721Semaste{
1573254721Semaste    Mutex::Locker locker (m_mutex);
1574254721Semaste    return m_source_mappings.RemapPath(path, new_path);
1575254721Semaste}
1576254721Semaste
1577254721Semasteuint32_t
1578254721SemasteModule::GetVersion (uint32_t *versions, uint32_t num_versions)
1579254721Semaste{
1580254721Semaste    ObjectFile *obj_file = GetObjectFile();
1581254721Semaste    if (obj_file)
1582254721Semaste        return obj_file->GetVersion (versions, num_versions);
1583254721Semaste
1584254721Semaste    if (versions && num_versions)
1585254721Semaste    {
1586254721Semaste        for (uint32_t i=0; i<num_versions; ++i)
1587254721Semaste            versions[i] = UINT32_MAX;
1588254721Semaste    }
1589254721Semaste    return 0;
1590254721Semaste}
1591254721Semaste
1592254721Semastevoid
1593254721SemasteModule::PrepareForFunctionNameLookup (const ConstString &name,
1594254721Semaste                                      uint32_t name_type_mask,
1595254721Semaste                                      ConstString &lookup_name,
1596254721Semaste                                      uint32_t &lookup_name_type_mask,
1597254721Semaste                                      bool &match_name_after_lookup)
1598254721Semaste{
1599254721Semaste    const char *name_cstr = name.GetCString();
1600254721Semaste    lookup_name_type_mask = eFunctionNameTypeNone;
1601254721Semaste    match_name_after_lookup = false;
1602254721Semaste    const char *base_name_start = NULL;
1603254721Semaste    const char *base_name_end = NULL;
1604254721Semaste
1605254721Semaste    if (name_type_mask & eFunctionNameTypeAuto)
1606254721Semaste    {
1607254721Semaste        if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
1608254721Semaste            lookup_name_type_mask = eFunctionNameTypeFull;
1609254721Semaste        else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
1610254721Semaste            lookup_name_type_mask = eFunctionNameTypeFull;
1611254721Semaste        else
1612254721Semaste        {
1613254721Semaste            if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1614254721Semaste                lookup_name_type_mask |= eFunctionNameTypeSelector;
1615254721Semaste
1616254721Semaste            CPPLanguageRuntime::MethodName cpp_method (name);
1617254721Semaste            llvm::StringRef basename (cpp_method.GetBasename());
1618254721Semaste            if (basename.empty())
1619254721Semaste            {
1620254721Semaste                if (CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1621254721Semaste                    lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
1622254721Semaste            }
1623254721Semaste            else
1624254721Semaste            {
1625254721Semaste                base_name_start = basename.data();
1626254721Semaste                base_name_end = base_name_start + basename.size();
1627254721Semaste                lookup_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
1628254721Semaste            }
1629254721Semaste        }
1630254721Semaste    }
1631254721Semaste    else
1632254721Semaste    {
1633254721Semaste        lookup_name_type_mask = name_type_mask;
1634254721Semaste        if (lookup_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
1635254721Semaste        {
1636254721Semaste            // If they've asked for a CPP method or function name and it can't be that, we don't
1637254721Semaste            // even need to search for CPP methods or names.
1638254721Semaste            CPPLanguageRuntime::MethodName cpp_method (name);
1639254721Semaste            if (cpp_method.IsValid())
1640254721Semaste            {
1641254721Semaste                llvm::StringRef basename (cpp_method.GetBasename());
1642254721Semaste                base_name_start = basename.data();
1643254721Semaste                base_name_end = base_name_start + basename.size();
1644254721Semaste
1645254721Semaste                if (!cpp_method.GetQualifiers().empty())
1646254721Semaste                {
1647254721Semaste                    // There is a "const" or other qualifer following the end of the fucntion parens,
1648254721Semaste                    // this can't be a eFunctionNameTypeBase
1649254721Semaste                    lookup_name_type_mask &= ~(eFunctionNameTypeBase);
1650254721Semaste                    if (lookup_name_type_mask == eFunctionNameTypeNone)
1651254721Semaste                        return;
1652254721Semaste                }
1653254721Semaste            }
1654254721Semaste            else
1655254721Semaste            {
1656254721Semaste                if (!CPPLanguageRuntime::StripNamespacesFromVariableName (name_cstr, base_name_start, base_name_end))
1657254721Semaste                {
1658254721Semaste                    lookup_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
1659254721Semaste                    if (lookup_name_type_mask == eFunctionNameTypeNone)
1660254721Semaste                        return;
1661254721Semaste                }
1662254721Semaste            }
1663254721Semaste        }
1664254721Semaste
1665254721Semaste        if (lookup_name_type_mask & eFunctionNameTypeSelector)
1666254721Semaste        {
1667254721Semaste            if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
1668254721Semaste            {
1669254721Semaste                lookup_name_type_mask &= ~(eFunctionNameTypeSelector);
1670254721Semaste                if (lookup_name_type_mask == eFunctionNameTypeNone)
1671254721Semaste                    return;
1672254721Semaste            }
1673254721Semaste        }
1674254721Semaste    }
1675254721Semaste
1676254721Semaste    if (base_name_start &&
1677254721Semaste        base_name_end &&
1678254721Semaste        base_name_start != name_cstr &&
1679254721Semaste        base_name_start < base_name_end)
1680254721Semaste    {
1681254721Semaste        // The name supplied was a partial C++ path like "a::count". In this case we want to do a
1682254721Semaste        // lookup on the basename "count" and then make sure any matching results contain "a::count"
1683254721Semaste        // so that it would match "b::a::count" and "a::count". This is why we set "match_name_after_lookup"
1684254721Semaste        // to true
1685254721Semaste        lookup_name.SetCStringWithLength(base_name_start, base_name_end - base_name_start);
1686254721Semaste        match_name_after_lookup = true;
1687254721Semaste    }
1688254721Semaste    else
1689254721Semaste    {
1690254721Semaste        // The name is already correct, just use the exact name as supplied, and we won't need
1691254721Semaste        // to check if any matches contain "name"
1692254721Semaste        lookup_name = name;
1693254721Semaste        match_name_after_lookup = false;
1694254721Semaste    }
1695263363Semaste}
1696