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