1 //===-- ModuleList.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/ModuleList.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Log.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Host/Host.h"
19 #include "lldb/Host/Symbols.h"
20 #include "lldb/Symbol/ClangNamespaceDecl.h"
21 #include "lldb/Symbol/ObjectFile.h"
22 #include "lldb/Symbol/VariableList.h"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 //----------------------------------------------------------------------
28 // ModuleList constructor
29 //----------------------------------------------------------------------
30 ModuleList::ModuleList() :
31     m_modules(),
32     m_modules_mutex (Mutex::eMutexTypeRecursive)
33 {
34 }
35 
36 //----------------------------------------------------------------------
37 // Copy constructor
38 //----------------------------------------------------------------------
39 ModuleList::ModuleList(const ModuleList& rhs) :
40     m_modules(),
41     m_modules_mutex (Mutex::eMutexTypeRecursive)
42 {
43     Mutex::Locker lhs_locker(m_modules_mutex);
44     Mutex::Locker rhs_locker(rhs.m_modules_mutex);
45     m_modules = rhs.m_modules;
46 }
47 
48 //----------------------------------------------------------------------
49 // Assignment operator
50 //----------------------------------------------------------------------
51 const ModuleList&
52 ModuleList::operator= (const ModuleList& rhs)
53 {
54     if (this != &rhs)
55     {
56         Mutex::Locker lhs_locker(m_modules_mutex);
57         Mutex::Locker rhs_locker(rhs.m_modules_mutex);
58         m_modules = rhs.m_modules;
59     }
60     return *this;
61 }
62 
63 //----------------------------------------------------------------------
64 // Destructor
65 //----------------------------------------------------------------------
66 ModuleList::~ModuleList()
67 {
68 }
69 
70 void
71 ModuleList::Append (const ModuleSP &module_sp)
72 {
73     if (module_sp)
74     {
75         Mutex::Locker locker(m_modules_mutex);
76         m_modules.push_back(module_sp);
77     }
78 }
79 
80 void
81 ModuleList::ReplaceEquivalent (const ModuleSP &module_sp)
82 {
83     if (module_sp)
84     {
85         Mutex::Locker locker(m_modules_mutex);
86 
87         // First remove any equivalent modules. Equivalent modules are modules
88         // whose path, platform path and architecture match.
89         ModuleSpec equivalent_module_spec (module_sp->GetFileSpec(), module_sp->GetArchitecture());
90         equivalent_module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
91 
92         size_t idx = 0;
93         while (idx < m_modules.size())
94         {
95             ModuleSP module_sp (m_modules[idx]);
96             if (module_sp->MatchesModuleSpec (equivalent_module_spec))
97                 m_modules.erase(m_modules.begin() + idx);
98             else
99                 ++idx;
100         }
101         // Now add the new module to the list
102         m_modules.push_back(module_sp);
103     }
104 }
105 
106 bool
107 ModuleList::AppendIfNeeded (const ModuleSP &module_sp)
108 {
109     if (module_sp)
110     {
111         Mutex::Locker locker(m_modules_mutex);
112         collection::iterator pos, end = m_modules.end();
113         for (pos = m_modules.begin(); pos != end; ++pos)
114         {
115             if (pos->get() == module_sp.get())
116                 return false; // Already in the list
117         }
118         // Only push module_sp on the list if it wasn't already in there.
119         m_modules.push_back(module_sp);
120         return true;
121     }
122     return false;
123 }
124 
125 bool
126 ModuleList::Remove (const ModuleSP &module_sp)
127 {
128     if (module_sp)
129     {
130         Mutex::Locker locker(m_modules_mutex);
131         collection::iterator pos, end = m_modules.end();
132         for (pos = m_modules.begin(); pos != end; ++pos)
133         {
134             if (pos->get() == module_sp.get())
135             {
136                 m_modules.erase (pos);
137                 return true;
138             }
139         }
140     }
141     return false;
142 }
143 
144 bool
145 ModuleList::RemoveIfOrphaned (const Module *module_ptr)
146 {
147     if (module_ptr)
148     {
149         Mutex::Locker locker(m_modules_mutex);
150         collection::iterator pos, end = m_modules.end();
151         for (pos = m_modules.begin(); pos != end; ++pos)
152         {
153             if (pos->get() == module_ptr)
154             {
155                 if (pos->unique())
156                 {
157                     pos = m_modules.erase (pos);
158                     return true;
159                 }
160                 else
161                     return false;
162             }
163         }
164     }
165     return false;
166 }
167 
168 size_t
169 ModuleList::RemoveOrphans (bool mandatory)
170 {
171     Mutex::Locker locker;
172 
173     if (mandatory)
174     {
175         locker.Lock (m_modules_mutex);
176     }
177     else
178     {
179         // Not mandatory, remove orphans if we can get the mutex
180         if (!locker.TryLock(m_modules_mutex))
181             return 0;
182     }
183     collection::iterator pos = m_modules.begin();
184     size_t remove_count = 0;
185     while (pos != m_modules.end())
186     {
187         if (pos->unique())
188         {
189             pos = m_modules.erase (pos);
190             ++remove_count;
191         }
192         else
193         {
194             ++pos;
195         }
196     }
197     return remove_count;
198 }
199 
200 size_t
201 ModuleList::Remove (ModuleList &module_list)
202 {
203     Mutex::Locker locker(m_modules_mutex);
204     size_t num_removed = 0;
205     collection::iterator pos, end = module_list.m_modules.end();
206     for (pos = module_list.m_modules.begin(); pos != end; ++pos)
207     {
208         if (Remove (*pos))
209             ++num_removed;
210     }
211     return num_removed;
212 }
213 
214 
215 
216 void
217 ModuleList::Clear()
218 {
219     Mutex::Locker locker(m_modules_mutex);
220     m_modules.clear();
221 }
222 
223 void
224 ModuleList::Destroy()
225 {
226     Mutex::Locker locker(m_modules_mutex);
227     collection empty;
228     m_modules.swap(empty);
229 }
230 
231 Module*
232 ModuleList::GetModulePointerAtIndex (uint32_t idx) const
233 {
234     Mutex::Locker locker(m_modules_mutex);
235     return GetModulePointerAtIndexUnlocked(idx);
236 }
237 
238 Module*
239 ModuleList::GetModulePointerAtIndexUnlocked (uint32_t idx) const
240 {
241     if (idx < m_modules.size())
242         return m_modules[idx].get();
243     return NULL;
244 }
245 
246 ModuleSP
247 ModuleList::GetModuleAtIndex(uint32_t idx)
248 {
249     Mutex::Locker locker(m_modules_mutex);
250     return GetModuleAtIndexUnlocked(idx);
251 }
252 
253 ModuleSP
254 ModuleList::GetModuleAtIndexUnlocked(uint32_t idx)
255 {
256     ModuleSP module_sp;
257     if (idx < m_modules.size())
258         module_sp = m_modules[idx];
259     return module_sp;
260 }
261 
262 uint32_t
263 ModuleList::FindFunctions (const ConstString &name,
264                            uint32_t name_type_mask,
265                            bool include_symbols,
266                            bool include_inlines,
267                            bool append,
268                            SymbolContextList &sc_list)
269 {
270     if (!append)
271         sc_list.Clear();
272 
273     Mutex::Locker locker(m_modules_mutex);
274     collection::const_iterator pos, end = m_modules.end();
275     for (pos = m_modules.begin(); pos != end; ++pos)
276     {
277         (*pos)->FindFunctions (name, NULL, name_type_mask, include_symbols, include_inlines, true, sc_list);
278     }
279 
280     return sc_list.GetSize();
281 }
282 
283 uint32_t
284 ModuleList::FindCompileUnits (const FileSpec &path,
285                               bool append,
286                               SymbolContextList &sc_list)
287 {
288     if (!append)
289         sc_list.Clear();
290 
291     Mutex::Locker locker(m_modules_mutex);
292     collection::const_iterator pos, end = m_modules.end();
293     for (pos = m_modules.begin(); pos != end; ++pos)
294     {
295         (*pos)->FindCompileUnits (path, true, sc_list);
296     }
297 
298     return sc_list.GetSize();
299 }
300 
301 uint32_t
302 ModuleList::FindGlobalVariables (const ConstString &name,
303                                  bool append,
304                                  uint32_t max_matches,
305                                  VariableList& variable_list)
306 {
307     size_t initial_size = variable_list.GetSize();
308     Mutex::Locker locker(m_modules_mutex);
309     collection::iterator pos, end = m_modules.end();
310     for (pos = m_modules.begin(); pos != end; ++pos)
311     {
312         (*pos)->FindGlobalVariables (name, NULL, append, max_matches, variable_list);
313     }
314     return variable_list.GetSize() - initial_size;
315 }
316 
317 
318 uint32_t
319 ModuleList::FindGlobalVariables (const RegularExpression& regex,
320                                  bool append,
321                                  uint32_t max_matches,
322                                  VariableList& variable_list)
323 {
324     size_t initial_size = variable_list.GetSize();
325     Mutex::Locker locker(m_modules_mutex);
326     collection::iterator pos, end = m_modules.end();
327     for (pos = m_modules.begin(); pos != end; ++pos)
328     {
329         (*pos)->FindGlobalVariables (regex, append, max_matches, variable_list);
330     }
331     return variable_list.GetSize() - initial_size;
332 }
333 
334 
335 size_t
336 ModuleList::FindSymbolsWithNameAndType (const ConstString &name,
337                                         SymbolType symbol_type,
338                                         SymbolContextList &sc_list,
339                                         bool append)
340 {
341     Mutex::Locker locker(m_modules_mutex);
342     if (!append)
343         sc_list.Clear();
344     size_t initial_size = sc_list.GetSize();
345 
346     collection::iterator pos, end = m_modules.end();
347     for (pos = m_modules.begin(); pos != end; ++pos)
348         (*pos)->FindSymbolsWithNameAndType (name, symbol_type, sc_list);
349     return sc_list.GetSize() - initial_size;
350 }
351 
352 size_t
353 ModuleList::FindSymbolsMatchingRegExAndType (const RegularExpression &regex,
354                                              lldb::SymbolType symbol_type,
355                                              SymbolContextList &sc_list,
356                                              bool append)
357 {
358     Mutex::Locker locker(m_modules_mutex);
359     if (!append)
360         sc_list.Clear();
361     size_t initial_size = sc_list.GetSize();
362 
363     collection::iterator pos, end = m_modules.end();
364     for (pos = m_modules.begin(); pos != end; ++pos)
365         (*pos)->FindSymbolsMatchingRegExAndType (regex, symbol_type, sc_list);
366     return sc_list.GetSize() - initial_size;
367 }
368 
369 size_t
370 ModuleList::FindModules (const ModuleSpec &module_spec, ModuleList& matching_module_list) const
371 {
372     size_t existing_matches = matching_module_list.GetSize();
373 
374     Mutex::Locker locker(m_modules_mutex);
375     collection::const_iterator pos, end = m_modules.end();
376     for (pos = m_modules.begin(); pos != end; ++pos)
377     {
378         ModuleSP module_sp(*pos);
379         if (module_sp->MatchesModuleSpec (module_spec))
380             matching_module_list.Append(module_sp);
381     }
382     return matching_module_list.GetSize() - existing_matches;
383 }
384 
385 ModuleSP
386 ModuleList::FindModule (const Module *module_ptr)
387 {
388     ModuleSP module_sp;
389 
390     // Scope for "locker"
391     {
392         Mutex::Locker locker(m_modules_mutex);
393         collection::const_iterator pos, end = m_modules.end();
394 
395         for (pos = m_modules.begin(); pos != end; ++pos)
396         {
397             if ((*pos).get() == module_ptr)
398             {
399                 module_sp = (*pos);
400                 break;
401             }
402         }
403     }
404     return module_sp;
405 
406 }
407 
408 ModuleSP
409 ModuleList::FindModule (const UUID &uuid)
410 {
411     ModuleSP module_sp;
412 
413     if (uuid.IsValid())
414     {
415         Mutex::Locker locker(m_modules_mutex);
416         collection::const_iterator pos, end = m_modules.end();
417 
418         for (pos = m_modules.begin(); pos != end; ++pos)
419         {
420             if ((*pos)->GetUUID() == uuid)
421             {
422                 module_sp = (*pos);
423                 break;
424             }
425         }
426     }
427     return module_sp;
428 }
429 
430 
431 uint32_t
432 ModuleList::FindTypes (const SymbolContext& sc, const ConstString &name, bool name_is_fully_qualified, uint32_t max_matches, TypeList& types)
433 {
434     Mutex::Locker locker(m_modules_mutex);
435 
436     uint32_t total_matches = 0;
437     collection::const_iterator pos, end = m_modules.end();
438     if (sc.module_sp)
439     {
440         // The symbol context "sc" contains a module so we want to search that
441         // one first if it is in our list...
442         for (pos = m_modules.begin(); pos != end; ++pos)
443         {
444             if (sc.module_sp.get() == (*pos).get())
445             {
446                 total_matches += (*pos)->FindTypes (sc, name, name_is_fully_qualified, max_matches, types);
447 
448                 if (total_matches >= max_matches)
449                     break;
450             }
451         }
452     }
453 
454     if (total_matches < max_matches)
455     {
456         for (pos = m_modules.begin(); pos != end; ++pos)
457         {
458             // Search the module if the module is not equal to the one in the symbol
459             // context "sc". If "sc" contains a empty module shared pointer, then
460             // the comparisong will always be true (valid_module_ptr != NULL).
461             if (sc.module_sp.get() != (*pos).get())
462                 total_matches += (*pos)->FindTypes (sc, name, name_is_fully_qualified, max_matches, types);
463 
464             if (total_matches >= max_matches)
465                 break;
466         }
467     }
468 
469     return total_matches;
470 }
471 
472 bool
473 ModuleList::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
474 {
475     Mutex::Locker locker(m_modules_mutex);
476     collection::const_iterator pos, end = m_modules.end();
477     for (pos = m_modules.begin(); pos != end; ++pos)
478     {
479         if ((*pos)->FindSourceFile (orig_spec, new_spec))
480             return true;
481     }
482     return false;
483 }
484 
485 
486 
487 ModuleSP
488 ModuleList::FindFirstModule (const ModuleSpec &module_spec)
489 {
490     ModuleSP module_sp;
491     Mutex::Locker locker(m_modules_mutex);
492     collection::const_iterator pos, end = m_modules.end();
493     for (pos = m_modules.begin(); pos != end; ++pos)
494     {
495         ModuleSP module_sp(*pos);
496         if (module_sp->MatchesModuleSpec (module_spec))
497             return module_sp;
498     }
499     return module_sp;
500 
501 }
502 
503 size_t
504 ModuleList::GetSize() const
505 {
506     size_t size = 0;
507     {
508         Mutex::Locker locker(m_modules_mutex);
509         size = m_modules.size();
510     }
511     return size;
512 }
513 
514 
515 void
516 ModuleList::Dump(Stream *s) const
517 {
518 //  s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
519 //  s.Indent();
520 //  s << "ModuleList\n";
521 
522     Mutex::Locker locker(m_modules_mutex);
523     collection::const_iterator pos, end = m_modules.end();
524     for (pos = m_modules.begin(); pos != end; ++pos)
525     {
526         (*pos)->Dump(s);
527     }
528 }
529 
530 void
531 ModuleList::LogUUIDAndPaths (LogSP &log_sp, const char *prefix_cstr)
532 {
533     if (log_sp)
534     {
535         Mutex::Locker locker(m_modules_mutex);
536         char uuid_cstr[256];
537         collection::const_iterator pos, begin = m_modules.begin(), end = m_modules.end();
538         for (pos = begin; pos != end; ++pos)
539         {
540             Module *module = pos->get();
541             module->GetUUID().GetAsCString (uuid_cstr, sizeof(uuid_cstr));
542             const FileSpec &module_file_spec = module->GetFileSpec();
543             log_sp->Printf ("%s[%u] %s (%s) \"%s/%s\"",
544                             prefix_cstr ? prefix_cstr : "",
545                             (uint32_t)std::distance (begin, pos),
546                             uuid_cstr,
547                             module->GetArchitecture().GetArchitectureName(),
548                             module_file_spec.GetDirectory().GetCString(),
549                             module_file_spec.GetFilename().GetCString());
550         }
551     }
552 }
553 
554 bool
555 ModuleList::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
556 {
557     Mutex::Locker locker(m_modules_mutex);
558     collection::const_iterator pos, end = m_modules.end();
559     for (pos = m_modules.begin(); pos != end; ++pos)
560     {
561         if ((*pos)->ResolveFileAddress (vm_addr, so_addr))
562             return true;
563     }
564 
565     return false;
566 }
567 
568 uint32_t
569 ModuleList::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
570 {
571     // The address is already section offset so it has a module
572     uint32_t resolved_flags = 0;
573     ModuleSP module_sp (so_addr.GetModule());
574     if (module_sp)
575     {
576         resolved_flags = module_sp->ResolveSymbolContextForAddress (so_addr,
577                                                                     resolve_scope,
578                                                                     sc);
579     }
580     else
581     {
582         Mutex::Locker locker(m_modules_mutex);
583         collection::const_iterator pos, end = m_modules.end();
584         for (pos = m_modules.begin(); pos != end; ++pos)
585         {
586             resolved_flags = (*pos)->ResolveSymbolContextForAddress (so_addr,
587                                                                      resolve_scope,
588                                                                      sc);
589             if (resolved_flags != 0)
590                 break;
591         }
592     }
593 
594     return resolved_flags;
595 }
596 
597 uint32_t
598 ModuleList::ResolveSymbolContextForFilePath
599 (
600     const char *file_path,
601     uint32_t line,
602     bool check_inlines,
603     uint32_t resolve_scope,
604     SymbolContextList& sc_list
605 )
606 {
607     FileSpec file_spec(file_path, false);
608     return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
609 }
610 
611 uint32_t
612 ModuleList::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
613 {
614     Mutex::Locker locker(m_modules_mutex);
615     collection::const_iterator pos, end = m_modules.end();
616     for (pos = m_modules.begin(); pos != end; ++pos)
617     {
618         (*pos)->ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
619     }
620 
621     return sc_list.GetSize();
622 }
623 
624 uint32_t
625 ModuleList::GetIndexForModule (const Module *module) const
626 {
627     if (module)
628     {
629         Mutex::Locker locker(m_modules_mutex);
630         collection::const_iterator pos;
631         collection::const_iterator begin = m_modules.begin();
632         collection::const_iterator end = m_modules.end();
633         for (pos = begin; pos != end; ++pos)
634         {
635             if ((*pos).get() == module)
636                 return std::distance (begin, pos);
637         }
638     }
639     return LLDB_INVALID_INDEX32;
640 }
641 
642 static ModuleList &
643 GetSharedModuleList ()
644 {
645     static ModuleList g_shared_module_list;
646     return g_shared_module_list;
647 }
648 
649 bool
650 ModuleList::ModuleIsInCache (const Module *module_ptr)
651 {
652     if (module_ptr)
653     {
654         ModuleList &shared_module_list = GetSharedModuleList ();
655         return shared_module_list.FindModule (module_ptr).get() != NULL;
656     }
657     return false;
658 }
659 
660 size_t
661 ModuleList::FindSharedModules (const ModuleSpec &module_spec, ModuleList &matching_module_list)
662 {
663     return GetSharedModuleList ().FindModules (module_spec, matching_module_list);
664 }
665 
666 uint32_t
667 ModuleList::RemoveOrphanSharedModules (bool mandatory)
668 {
669     return GetSharedModuleList ().RemoveOrphans(mandatory);
670 }
671 
672 Error
673 ModuleList::GetSharedModule
674 (
675     const ModuleSpec &module_spec,
676     ModuleSP &module_sp,
677     const FileSpecList *module_search_paths_ptr,
678     ModuleSP *old_module_sp_ptr,
679     bool *did_create_ptr,
680     bool always_create
681 )
682 {
683     ModuleList &shared_module_list = GetSharedModuleList ();
684     Mutex::Locker locker(shared_module_list.m_modules_mutex);
685     char path[PATH_MAX];
686     char uuid_cstr[64];
687 
688     Error error;
689 
690     module_sp.reset();
691 
692     if (did_create_ptr)
693         *did_create_ptr = false;
694     if (old_module_sp_ptr)
695         old_module_sp_ptr->reset();
696 
697     const UUID *uuid_ptr = module_spec.GetUUIDPtr();
698     const FileSpec &module_file_spec = module_spec.GetFileSpec();
699     const ArchSpec &arch = module_spec.GetArchitecture();
700 
701     // Make sure no one else can try and get or create a module while this
702     // function is actively working on it by doing an extra lock on the
703     // global mutex list.
704     if (always_create == false)
705     {
706         ModuleList matching_module_list;
707         const size_t num_matching_modules = shared_module_list.FindModules (module_spec, matching_module_list);
708         if (num_matching_modules > 0)
709         {
710             for (uint32_t module_idx = 0; module_idx < num_matching_modules; ++module_idx)
711             {
712                 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
713 
714                 // Make sure the file for the module hasn't been modified
715                 if (module_sp->FileHasChanged())
716                 {
717                     if (old_module_sp_ptr && !old_module_sp_ptr->get())
718                         *old_module_sp_ptr = module_sp;
719                     shared_module_list.Remove (module_sp);
720                     module_sp.reset();
721                 }
722                 else
723                 {
724                     // The module matches and the module was not modified from
725                     // when it was last loaded.
726                     return error;
727                 }
728             }
729         }
730     }
731 
732     if (module_sp)
733         return error;
734     else
735     {
736         module_sp.reset (new Module (module_spec));
737         // Make sure there are a module and an object file since we can specify
738         // a valid file path with an architecture that might not be in that file.
739         // By getting the object file we can guarantee that the architecture matches
740         if (module_sp)
741         {
742             if (module_sp->GetObjectFile())
743             {
744                 // If we get in here we got the correct arch, now we just need
745                 // to verify the UUID if one was given
746                 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID())
747                     module_sp.reset();
748                 else
749                 {
750                     if (did_create_ptr)
751                         *did_create_ptr = true;
752 
753                     shared_module_list.ReplaceEquivalent(module_sp);
754                     return error;
755                 }
756             }
757             else
758                 module_sp.reset();
759         }
760     }
761 
762     // Either the file didn't exist where at the path, or no path was given, so
763     // we now have to use more extreme measures to try and find the appropriate
764     // module.
765 
766     // Fixup the incoming path in case the path points to a valid file, yet
767     // the arch or UUID (if one was passed in) don't match.
768     FileSpec file_spec = Symbols::LocateExecutableObjectFile (module_spec);
769 
770     // Don't look for the file if it appears to be the same one we already
771     // checked for above...
772     if (file_spec != module_file_spec)
773     {
774         if (!file_spec.Exists())
775         {
776             file_spec.GetPath(path, sizeof(path));
777             if (path[0] == '\0')
778                 module_file_spec.GetPath(path, sizeof(path));
779             if (file_spec.Exists())
780             {
781                 if (uuid_ptr && uuid_ptr->IsValid())
782                     uuid_ptr->GetAsCString(uuid_cstr, sizeof (uuid_cstr));
783                 else
784                     uuid_cstr[0] = '\0';
785 
786 
787                 if (arch.IsValid())
788                 {
789                     if (uuid_cstr[0])
790                         error.SetErrorStringWithFormat("'%s' does not contain the %s architecture and UUID %s", path, arch.GetArchitectureName(), uuid_cstr);
791                     else
792                         error.SetErrorStringWithFormat("'%s' does not contain the %s architecture.", path, arch.GetArchitectureName());
793                 }
794             }
795             else
796             {
797                 error.SetErrorStringWithFormat("'%s' does not exist", path);
798             }
799             if (error.Fail())
800                 module_sp.reset();
801             return error;
802         }
803 
804 
805         // Make sure no one else can try and get or create a module while this
806         // function is actively working on it by doing an extra lock on the
807         // global mutex list.
808         ModuleSpec platform_module_spec(module_spec);
809         platform_module_spec.GetFileSpec() = file_spec;
810         platform_module_spec.GetPlatformFileSpec() = file_spec;
811         ModuleList matching_module_list;
812         if (shared_module_list.FindModules (platform_module_spec, matching_module_list) > 0)
813         {
814             module_sp = matching_module_list.GetModuleAtIndex(0);
815 
816             // If we didn't have a UUID in mind when looking for the object file,
817             // then we should make sure the modification time hasn't changed!
818             if (platform_module_spec.GetUUIDPtr() == NULL)
819             {
820                 TimeValue file_spec_mod_time(file_spec.GetModificationTime());
821                 if (file_spec_mod_time.IsValid())
822                 {
823                     if (file_spec_mod_time != module_sp->GetModificationTime())
824                     {
825                         if (old_module_sp_ptr)
826                             *old_module_sp_ptr = module_sp;
827                         shared_module_list.Remove (module_sp);
828                         module_sp.reset();
829                     }
830                 }
831             }
832         }
833 
834         if (module_sp.get() == NULL)
835         {
836             module_sp.reset (new Module (platform_module_spec));
837             // Make sure there are a module and an object file since we can specify
838             // a valid file path with an architecture that might not be in that file.
839             // By getting the object file we can guarantee that the architecture matches
840             if (module_sp && module_sp->GetObjectFile())
841             {
842                 if (did_create_ptr)
843                     *did_create_ptr = true;
844 
845                 shared_module_list.ReplaceEquivalent(module_sp);
846             }
847             else
848             {
849                 file_spec.GetPath(path, sizeof(path));
850 
851                 if (file_spec)
852                 {
853                     if (arch.IsValid())
854                         error.SetErrorStringWithFormat("unable to open %s architecture in '%s'", arch.GetArchitectureName(), path);
855                     else
856                         error.SetErrorStringWithFormat("unable to open '%s'", path);
857                 }
858                 else
859                 {
860                     if (uuid_ptr && uuid_ptr->IsValid())
861                         uuid_ptr->GetAsCString(uuid_cstr, sizeof (uuid_cstr));
862                     else
863                         uuid_cstr[0] = '\0';
864 
865                     if (uuid_cstr[0])
866                         error.SetErrorStringWithFormat("cannot locate a module for UUID '%s'", uuid_cstr);
867                     else
868                         error.SetErrorStringWithFormat("cannot locate a module");
869                 }
870             }
871         }
872     }
873 
874     return error;
875 }
876 
877 bool
878 ModuleList::RemoveSharedModule (lldb::ModuleSP &module_sp)
879 {
880     return GetSharedModuleList ().Remove (module_sp);
881 }
882 
883 bool
884 ModuleList::RemoveSharedModuleIfOrphaned (const Module *module_ptr)
885 {
886     return GetSharedModuleList ().RemoveIfOrphaned (module_ptr);
887 }
888 
889 
890