1 //===-- Module.cpp ----------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Core/Module.h"
11 #include "lldb/Core/Log.h"
12 #include "lldb/Core/ModuleList.h"
13 #include "lldb/Core/RegularExpression.h"
14 #include "lldb/Core/StreamString.h"
15 #include "lldb/Core/Timer.h"
16 #include "lldb/lldb-private-log.h"
17 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Symbol/SymbolContext.h"
19 #include "lldb/Symbol/SymbolVendor.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 // Shared pointers to modules track module lifetimes in
25 // targets and in the global module, but this collection
26 // will track all module objects that are still alive
27 typedef std::vector<Module *> ModuleCollection;
28 
29 static ModuleCollection &
30 GetModuleCollection()
31 {
32     // This module collection needs to live past any module, so we could either make it a
33     // shared pointer in each module or just leak is.  Since it is only an empty vector by
34     // the time all the modules have gone away, we just leak it for now.  If we decide this
35     // is a big problem we can introduce a Finalize method that will tear everything down in
36     // a predictable order.
37 
38     static ModuleCollection *g_module_collection = NULL;
39     if (g_module_collection == NULL)
40         g_module_collection = new ModuleCollection();
41 
42     return *g_module_collection;
43 }
44 
45 Mutex &
46 Module::GetAllocationModuleCollectionMutex()
47 {
48     static Mutex g_module_collection_mutex(Mutex::eMutexTypeRecursive);
49     return g_module_collection_mutex;
50 }
51 
52 size_t
53 Module::GetNumberAllocatedModules ()
54 {
55     Mutex::Locker locker (GetAllocationModuleCollectionMutex());
56     return GetModuleCollection().size();
57 }
58 
59 Module *
60 Module::GetAllocatedModuleAtIndex (size_t idx)
61 {
62     Mutex::Locker locker (GetAllocationModuleCollectionMutex());
63     ModuleCollection &modules = GetModuleCollection();
64     if (idx < modules.size())
65         return modules[idx];
66     return NULL;
67 }
68 
69 
70 
71 
72 Module::Module(const FileSpec& file_spec, const ArchSpec& arch, const ConstString *object_name, off_t object_offset) :
73     m_mutex (Mutex::eMutexTypeRecursive),
74     m_mod_time (file_spec.GetModificationTime()),
75     m_arch (arch),
76     m_uuid (),
77     m_file (file_spec),
78     m_platform_file(),
79     m_object_name (),
80     m_object_offset (object_offset),
81     m_objfile_sp (),
82     m_symfile_ap (),
83     m_ast (),
84     m_did_load_objfile (false),
85     m_did_load_symbol_vendor (false),
86     m_did_parse_uuid (false),
87     m_did_init_ast (false),
88     m_is_dynamic_loader_module (false)
89 {
90     // Scope for locker below...
91     {
92         Mutex::Locker locker (GetAllocationModuleCollectionMutex());
93         GetModuleCollection().push_back(this);
94     }
95 
96     if (object_name)
97         m_object_name = *object_name;
98     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
99     if (log)
100         log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')",
101                      this,
102                      m_arch.GetArchitectureName(),
103                      m_file.GetDirectory().AsCString(""),
104                      m_file.GetFilename().AsCString(""),
105                      m_object_name.IsEmpty() ? "" : "(",
106                      m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
107                      m_object_name.IsEmpty() ? "" : ")");
108 }
109 
110 Module::~Module()
111 {
112     // Scope for locker below...
113     {
114         Mutex::Locker locker (GetAllocationModuleCollectionMutex());
115         ModuleCollection &modules = GetModuleCollection();
116         ModuleCollection::iterator end = modules.end();
117         ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
118         if (pos != end)
119             modules.erase(pos);
120     }
121     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
122     if (log)
123         log->Printf ("%p Module::~Module((%s) '%s/%s%s%s%s')",
124                      this,
125                      m_arch.GetArchitectureName(),
126                      m_file.GetDirectory().AsCString(""),
127                      m_file.GetFilename().AsCString(""),
128                      m_object_name.IsEmpty() ? "" : "(",
129                      m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
130                      m_object_name.IsEmpty() ? "" : ")");
131     // Release any auto pointers before we start tearing down our member
132     // variables since the object file and symbol files might need to make
133     // function calls back into this module object. The ordering is important
134     // here because symbol files can require the module object file. So we tear
135     // down the symbol file first, then the object file.
136     m_symfile_ap.reset();
137     m_objfile_sp.reset();
138 }
139 
140 
141 const lldb_private::UUID&
142 Module::GetUUID()
143 {
144     Mutex::Locker locker (m_mutex);
145     if (m_did_parse_uuid == false)
146     {
147         ObjectFile * obj_file = GetObjectFile ();
148 
149         if (obj_file != NULL)
150         {
151             obj_file->GetUUID(&m_uuid);
152             m_did_parse_uuid = true;
153         }
154     }
155     return m_uuid;
156 }
157 
158 ClangASTContext &
159 Module::GetClangASTContext ()
160 {
161     Mutex::Locker locker (m_mutex);
162     if (m_did_init_ast == false)
163     {
164         ObjectFile * objfile = GetObjectFile();
165         ArchSpec object_arch;
166         if (objfile && objfile->GetArchitecture(object_arch))
167         {
168             m_did_init_ast = true;
169             m_ast.SetArchitecture (object_arch);
170         }
171     }
172     return m_ast;
173 }
174 
175 void
176 Module::ParseAllDebugSymbols()
177 {
178     Mutex::Locker locker (m_mutex);
179     uint32_t num_comp_units = GetNumCompileUnits();
180     if (num_comp_units == 0)
181         return;
182 
183     SymbolContext sc;
184     sc.module_sp = this;
185     uint32_t cu_idx;
186     SymbolVendor *symbols = GetSymbolVendor ();
187 
188     for (cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
189     {
190         sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
191         if (sc.comp_unit)
192         {
193             sc.function = NULL;
194             symbols->ParseVariablesForContext(sc);
195 
196             symbols->ParseCompileUnitFunctions(sc);
197 
198             uint32_t func_idx;
199             for (func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx)
200             {
201                 symbols->ParseFunctionBlocks(sc);
202 
203                 // Parse the variables for this function and all its blocks
204                 symbols->ParseVariablesForContext(sc);
205             }
206 
207 
208             // Parse all types for this compile unit
209             sc.function = NULL;
210             symbols->ParseTypes(sc);
211         }
212     }
213 }
214 
215 void
216 Module::CalculateSymbolContext(SymbolContext* sc)
217 {
218     sc->module_sp = this;
219 }
220 
221 Module *
222 Module::CalculateSymbolContextModule ()
223 {
224     return this;
225 }
226 
227 void
228 Module::DumpSymbolContext(Stream *s)
229 {
230     s->Printf(", Module{%p}", this);
231 }
232 
233 uint32_t
234 Module::GetNumCompileUnits()
235 {
236     Mutex::Locker locker (m_mutex);
237     Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
238     SymbolVendor *symbols = GetSymbolVendor ();
239     if (symbols)
240         return symbols->GetNumCompileUnits();
241     return 0;
242 }
243 
244 CompUnitSP
245 Module::GetCompileUnitAtIndex (uint32_t index)
246 {
247     Mutex::Locker locker (m_mutex);
248     uint32_t num_comp_units = GetNumCompileUnits ();
249     CompUnitSP cu_sp;
250 
251     if (index < num_comp_units)
252     {
253         SymbolVendor *symbols = GetSymbolVendor ();
254         if (symbols)
255             cu_sp = symbols->GetCompileUnitAtIndex(index);
256     }
257     return cu_sp;
258 }
259 
260 bool
261 Module::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
262 {
263     Mutex::Locker locker (m_mutex);
264     Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%llx)", vm_addr);
265     ObjectFile* ofile = GetObjectFile();
266     if (ofile)
267         return so_addr.ResolveAddressUsingFileSections(vm_addr, ofile->GetSectionList());
268     return false;
269 }
270 
271 uint32_t
272 Module::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
273 {
274     Mutex::Locker locker (m_mutex);
275     uint32_t resolved_flags = 0;
276 
277     // Clear the result symbol context in case we don't find anything
278     sc.Clear();
279 
280     // Get the section from the section/offset address.
281     const Section *section = so_addr.GetSection();
282 
283     // Make sure the section matches this module before we try and match anything
284     if (section && section->GetModule() == this)
285     {
286         // If the section offset based address resolved itself, then this
287         // is the right module.
288         sc.module_sp = this;
289         resolved_flags |= eSymbolContextModule;
290 
291         // Resolve the compile unit, function, block, line table or line
292         // entry if requested.
293         if (resolve_scope & eSymbolContextCompUnit    ||
294             resolve_scope & eSymbolContextFunction    ||
295             resolve_scope & eSymbolContextBlock       ||
296             resolve_scope & eSymbolContextLineEntry   )
297         {
298             SymbolVendor *symbols = GetSymbolVendor ();
299             if (symbols)
300                 resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc);
301         }
302 
303         // Resolve the symbol if requested, but don't re-look it up if we've already found it.
304         if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
305         {
306             ObjectFile* ofile = GetObjectFile();
307             if (ofile)
308             {
309                 Symtab *symtab = ofile->GetSymtab();
310                 if (symtab)
311                 {
312                     if (so_addr.IsSectionOffset())
313                     {
314                         sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
315                         if (sc.symbol)
316                             resolved_flags |= eSymbolContextSymbol;
317                     }
318                 }
319             }
320         }
321     }
322     return resolved_flags;
323 }
324 
325 uint32_t
326 Module::ResolveSymbolContextForFilePath
327 (
328     const char *file_path,
329     uint32_t line,
330     bool check_inlines,
331     uint32_t resolve_scope,
332     SymbolContextList& sc_list
333 )
334 {
335     FileSpec file_spec(file_path, false);
336     return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
337 }
338 
339 uint32_t
340 Module::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
341 {
342     Mutex::Locker locker (m_mutex);
343     Timer scoped_timer(__PRETTY_FUNCTION__,
344                        "Module::ResolveSymbolContextForFilePath (%s%s%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
345                        file_spec.GetDirectory().AsCString(""),
346                        file_spec.GetDirectory() ? "/" : "",
347                        file_spec.GetFilename().AsCString(""),
348                        line,
349                        check_inlines ? "yes" : "no",
350                        resolve_scope);
351 
352     const uint32_t initial_count = sc_list.GetSize();
353 
354     SymbolVendor *symbols = GetSymbolVendor  ();
355     if (symbols)
356         symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
357 
358     return sc_list.GetSize() - initial_count;
359 }
360 
361 
362 uint32_t
363 Module::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
364 {
365     SymbolVendor *symbols = GetSymbolVendor ();
366     if (symbols)
367         return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
368     return 0;
369 }
370 uint32_t
371 Module::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
372 {
373     SymbolVendor *symbols = GetSymbolVendor ();
374     if (symbols)
375         return symbols->FindGlobalVariables(regex, append, max_matches, variables);
376     return 0;
377 }
378 
379 uint32_t
380 Module::FindCompileUnits (const FileSpec &path,
381                           bool append,
382                           SymbolContextList &sc_list)
383 {
384     if (!append)
385         sc_list.Clear();
386 
387     const uint32_t start_size = sc_list.GetSize();
388     const uint32_t num_compile_units = GetNumCompileUnits();
389     SymbolContext sc;
390     sc.module_sp = this;
391     const bool compare_directory = path.GetDirectory();
392     for (uint32_t i=0; i<num_compile_units; ++i)
393     {
394         sc.comp_unit = GetCompileUnitAtIndex(i).get();
395         if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
396             sc_list.Append(sc);
397     }
398     return sc_list.GetSize() - start_size;
399 }
400 
401 uint32_t
402 Module::FindFunctions (const ConstString &name,
403                        const ClangNamespaceDecl *namespace_decl,
404                        uint32_t name_type_mask,
405                        bool include_symbols,
406                        bool append,
407                        SymbolContextList& sc_list)
408 {
409     if (!append)
410         sc_list.Clear();
411 
412     const uint32_t start_size = sc_list.GetSize();
413 
414     // Find all the functions (not symbols, but debug information functions...
415     SymbolVendor *symbols = GetSymbolVendor ();
416     if (symbols)
417         symbols->FindFunctions(name, namespace_decl, name_type_mask, append, sc_list);
418 
419     // Now check our symbol table for symbols that are code symbols if requested
420     if (include_symbols)
421     {
422         ObjectFile *objfile = GetObjectFile();
423         if (objfile)
424         {
425             Symtab *symtab = objfile->GetSymtab();
426             if (symtab)
427             {
428                 std::vector<uint32_t> symbol_indexes;
429                 symtab->FindAllSymbolsWithNameAndType (name, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
430                 const uint32_t num_matches = symbol_indexes.size();
431                 if (num_matches)
432                 {
433                     const bool merge_symbol_into_function = true;
434                     SymbolContext sc(this);
435                     for (uint32_t i=0; i<num_matches; i++)
436                     {
437                         sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
438                         sc_list.AppendIfUnique (sc, merge_symbol_into_function);
439                     }
440                 }
441             }
442         }
443     }
444     return sc_list.GetSize() - start_size;
445 }
446 
447 uint32_t
448 Module::FindFunctions (const RegularExpression& regex,
449                        bool include_symbols,
450                        bool append,
451                        SymbolContextList& sc_list)
452 {
453     if (!append)
454         sc_list.Clear();
455 
456     const uint32_t start_size = sc_list.GetSize();
457 
458     SymbolVendor *symbols = GetSymbolVendor ();
459     if (symbols)
460         symbols->FindFunctions(regex, append, sc_list);
461     // Now check our symbol table for symbols that are code symbols if requested
462     if (include_symbols)
463     {
464         ObjectFile *objfile = GetObjectFile();
465         if (objfile)
466         {
467             Symtab *symtab = objfile->GetSymtab();
468             if (symtab)
469             {
470                 std::vector<uint32_t> symbol_indexes;
471                 symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
472                 const uint32_t num_matches = symbol_indexes.size();
473                 if (num_matches)
474                 {
475                     const bool merge_symbol_into_function = true;
476                     SymbolContext sc(this);
477                     for (uint32_t i=0; i<num_matches; i++)
478                     {
479                         sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
480                         sc_list.AppendIfUnique (sc, merge_symbol_into_function);
481                     }
482                 }
483             }
484         }
485     }
486     return sc_list.GetSize() - start_size;
487 }
488 
489 uint32_t
490 Module::FindTypes_Impl (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types)
491 {
492     Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
493     if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
494     {
495         SymbolVendor *symbols = GetSymbolVendor ();
496         if (symbols)
497             return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
498     }
499     return 0;
500 }
501 
502 // depending on implementation details, type lookup might fail because of
503 // embedded spurious namespace:: prefixes. this call strips them, paying
504 // attention to the fact that a type might have namespace'd type names as
505 // arguments to templates, and those must not be stripped off
506 static const char*
507 StripTypeName(const char* name_cstr)
508 {
509     // Protect against null c string.
510     if (!name_cstr)
511         return name_cstr;
512     const char* skip_namespace = strstr(name_cstr, "::");
513     const char* template_arg_char = strchr(name_cstr, '<');
514     while (skip_namespace != NULL)
515     {
516         if (template_arg_char != NULL &&
517             skip_namespace > template_arg_char) // but namespace'd template arguments are still good to go
518             break;
519         name_cstr = skip_namespace+2;
520         skip_namespace = strstr(name_cstr, "::");
521     }
522     return name_cstr;
523 }
524 
525 uint32_t
526 Module::FindTypes (const SymbolContext& sc,  const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types)
527 {
528     uint32_t retval = FindTypes_Impl(sc, name, namespace_decl, append, max_matches, types);
529 
530     if (retval == 0)
531     {
532         const char *stripped = StripTypeName(name.GetCString());
533         return FindTypes_Impl(sc, ConstString(stripped), namespace_decl, append, max_matches, types);
534     }
535     else
536         return retval;
537 
538 }
539 
540 //uint32_t
541 //Module::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, const char *udt_name, TypeList& types)
542 //{
543 //  Timer scoped_timer(__PRETTY_FUNCTION__);
544 //  SymbolVendor *symbols = GetSymbolVendor ();
545 //  if (symbols)
546 //      return symbols->FindTypes(sc, regex, append, max_matches, encoding, udt_name, types);
547 //  return 0;
548 //
549 //}
550 
551 SymbolVendor*
552 Module::GetSymbolVendor (bool can_create)
553 {
554     Mutex::Locker locker (m_mutex);
555     if (m_did_load_symbol_vendor == false && can_create)
556     {
557         ObjectFile *obj_file = GetObjectFile ();
558         if (obj_file != NULL)
559         {
560             Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
561             m_symfile_ap.reset(SymbolVendor::FindPlugin(this));
562             m_did_load_symbol_vendor = true;
563         }
564     }
565     return m_symfile_ap.get();
566 }
567 
568 void
569 Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
570 {
571     // Container objects whose paths do not specify a file directly can call
572     // this function to correct the file and object names.
573     m_file = file;
574     m_mod_time = file.GetModificationTime();
575     m_object_name = object_name;
576 }
577 
578 const ArchSpec&
579 Module::GetArchitecture () const
580 {
581     return m_arch;
582 }
583 
584 void
585 Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
586 {
587     Mutex::Locker locker (m_mutex);
588 
589     if (level >= eDescriptionLevelFull)
590     {
591         if (m_arch.IsValid())
592             s->Printf("(%s) ", m_arch.GetArchitectureName());
593     }
594 
595     if (level == eDescriptionLevelBrief)
596     {
597         const char *filename = m_file.GetFilename().GetCString();
598         if (filename)
599             s->PutCString (filename);
600     }
601     else
602     {
603         char path[PATH_MAX];
604         if (m_file.GetPath(path, sizeof(path)))
605             s->PutCString(path);
606     }
607 
608     const char *object_name = m_object_name.GetCString();
609     if (object_name)
610         s->Printf("(%s)", object_name);
611 }
612 
613 void
614 Module::ReportError (const char *format, ...)
615 {
616     StreamString module_description;
617     GetDescription(&module_description, lldb::eDescriptionLevelBrief);
618     ::fprintf (stderr, "error: %s ", module_description.GetString().c_str());
619 
620     va_list args;
621     va_start (args, format);
622     vfprintf (stderr, format, args);
623     va_end (args);
624 }
625 
626 void
627 Module::ReportWarning (const char *format, ...)
628 {
629     StreamString module_description;
630     GetDescription(&module_description, lldb::eDescriptionLevelBrief);
631     ::fprintf (stderr, "warning: %s ", module_description.GetString().c_str());
632 
633     va_list args;
634     va_start (args, format);
635     vfprintf (stderr, format, args);
636     va_end (args);
637 }
638 
639 void
640 Module::LogMessage (Log *log, const char *format, ...)
641 {
642     if (log)
643     {
644         StreamString log_message;
645         GetDescription(&log_message, lldb::eDescriptionLevelBrief);
646         log_message.PutCString (": ");
647         va_list args;
648         va_start (args, format);
649         log_message.PrintfVarArg (format, args);
650         va_end (args);
651         log->PutCString(log_message.GetString().c_str());
652     }
653 }
654 
655 void
656 Module::Dump(Stream *s)
657 {
658     Mutex::Locker locker (m_mutex);
659     //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
660     s->Indent();
661     s->Printf("Module %s/%s%s%s%s\n",
662               m_file.GetDirectory().AsCString(),
663               m_file.GetFilename().AsCString(),
664               m_object_name ? "(" : "",
665               m_object_name ? m_object_name.GetCString() : "",
666               m_object_name ? ")" : "");
667 
668     s->IndentMore();
669     ObjectFile *objfile = GetObjectFile ();
670 
671     if (objfile)
672         objfile->Dump(s);
673 
674     SymbolVendor *symbols = GetSymbolVendor ();
675 
676     if (symbols)
677         symbols->Dump(s);
678 
679     s->IndentLess();
680 }
681 
682 
683 TypeList*
684 Module::GetTypeList ()
685 {
686     SymbolVendor *symbols = GetSymbolVendor ();
687     if (symbols)
688         return &symbols->GetTypeList();
689     return NULL;
690 }
691 
692 const ConstString &
693 Module::GetObjectName() const
694 {
695     return m_object_name;
696 }
697 
698 ObjectFile *
699 Module::GetObjectFile()
700 {
701     Mutex::Locker locker (m_mutex);
702     if (m_did_load_objfile == false)
703     {
704         m_did_load_objfile = true;
705         Timer scoped_timer(__PRETTY_FUNCTION__,
706                            "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
707         m_objfile_sp = ObjectFile::FindPlugin(this, &m_file, m_object_offset, m_file.GetByteSize());
708         if (m_objfile_sp)
709         {
710 			// Once we get the object file, update our module with the object file's
711 			// architecture since it might differ in vendor/os if some parts were
712 			// unknown.
713             m_objfile_sp->GetArchitecture (m_arch);
714         }
715     }
716     return m_objfile_sp.get();
717 }
718 
719 
720 const Symbol *
721 Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
722 {
723     Timer scoped_timer(__PRETTY_FUNCTION__,
724                        "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
725                        name.AsCString(),
726                        symbol_type);
727     ObjectFile *objfile = GetObjectFile();
728     if (objfile)
729     {
730         Symtab *symtab = objfile->GetSymtab();
731         if (symtab)
732             return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
733     }
734     return NULL;
735 }
736 void
737 Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
738 {
739     // No need to protect this call using m_mutex all other method calls are
740     // already thread safe.
741 
742     size_t num_indices = symbol_indexes.size();
743     if (num_indices > 0)
744     {
745         SymbolContext sc;
746         CalculateSymbolContext (&sc);
747         for (size_t i = 0; i < num_indices; i++)
748         {
749             sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
750             if (sc.symbol)
751                 sc_list.Append (sc);
752         }
753     }
754 }
755 
756 size_t
757 Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
758 {
759     // No need to protect this call using m_mutex all other method calls are
760     // already thread safe.
761 
762 
763     Timer scoped_timer(__PRETTY_FUNCTION__,
764                        "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
765                        name.AsCString(),
766                        symbol_type);
767     const size_t initial_size = sc_list.GetSize();
768     ObjectFile *objfile = GetObjectFile ();
769     if (objfile)
770     {
771         Symtab *symtab = objfile->GetSymtab();
772         if (symtab)
773         {
774             std::vector<uint32_t> symbol_indexes;
775             symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
776             SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
777         }
778     }
779     return sc_list.GetSize() - initial_size;
780 }
781 
782 size_t
783 Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
784 {
785     // No need to protect this call using m_mutex all other method calls are
786     // already thread safe.
787 
788     Timer scoped_timer(__PRETTY_FUNCTION__,
789                        "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
790                        regex.GetText(),
791                        symbol_type);
792     const size_t initial_size = sc_list.GetSize();
793     ObjectFile *objfile = GetObjectFile ();
794     if (objfile)
795     {
796         Symtab *symtab = objfile->GetSymtab();
797         if (symtab)
798         {
799             std::vector<uint32_t> symbol_indexes;
800             symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
801             SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
802         }
803     }
804     return sc_list.GetSize() - initial_size;
805 }
806 
807 const TimeValue &
808 Module::GetModificationTime () const
809 {
810     return m_mod_time;
811 }
812 
813 bool
814 Module::IsExecutable ()
815 {
816     if (GetObjectFile() == NULL)
817         return false;
818     else
819         return GetObjectFile()->IsExecutable();
820 }
821 
822 bool
823 Module::IsLoadedInTarget (Target *target)
824 {
825     ObjectFile *obj_file = GetObjectFile();
826     if (obj_file)
827     {
828         SectionList *sections = obj_file->GetSectionList();
829         if (sections != NULL)
830         {
831             size_t num_sections = sections->GetSize();
832             for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
833             {
834                 SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
835                 if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
836                 {
837                     return true;
838                 }
839             }
840         }
841     }
842     return false;
843 }
844 bool
845 Module::SetArchitecture (const ArchSpec &new_arch)
846 {
847     if (!m_arch.IsValid())
848     {
849         m_arch = new_arch;
850         return true;
851     }
852     return m_arch == new_arch;
853 }
854 
855