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