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         for (pos = m_modules.begin(); pos != end; ++pos)
458         {
459             // Search the module if the module is not equal to the one in the symbol
460             // context "sc". If "sc" contains a empty module shared pointer, then
461             // the comparisong will always be true (valid_module_ptr != NULL).
462             if (sc.module_sp.get() != (*pos).get())
463                 total_matches += (*pos)->FindTypes (sc, name, name_is_fully_qualified, max_matches, types);
464 
465             if (total_matches >= max_matches)
466                 break;
467         }
468     }
469 
470     return total_matches;
471 }
472 
473 bool
474 ModuleList::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
475 {
476     Mutex::Locker locker(m_modules_mutex);
477     collection::const_iterator pos, end = m_modules.end();
478     for (pos = m_modules.begin(); pos != end; ++pos)
479     {
480         if ((*pos)->FindSourceFile (orig_spec, new_spec))
481             return true;
482     }
483     return false;
484 }
485 
486 
487 
488 ModuleSP
489 ModuleList::FindFirstModule (const ModuleSpec &module_spec)
490 {
491     ModuleSP module_sp;
492     Mutex::Locker locker(m_modules_mutex);
493     collection::const_iterator pos, end = m_modules.end();
494     for (pos = m_modules.begin(); pos != end; ++pos)
495     {
496         ModuleSP module_sp(*pos);
497         if (module_sp->MatchesModuleSpec (module_spec))
498             return module_sp;
499     }
500     return module_sp;
501 
502 }
503 
504 size_t
505 ModuleList::GetSize() const
506 {
507     size_t size = 0;
508     {
509         Mutex::Locker locker(m_modules_mutex);
510         size = m_modules.size();
511     }
512     return size;
513 }
514 
515 
516 void
517 ModuleList::Dump(Stream *s) const
518 {
519 //  s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
520 //  s.Indent();
521 //  s << "ModuleList\n";
522 
523     Mutex::Locker locker(m_modules_mutex);
524     collection::const_iterator pos, end = m_modules.end();
525     for (pos = m_modules.begin(); pos != end; ++pos)
526     {
527         (*pos)->Dump(s);
528     }
529 }
530 
531 void
532 ModuleList::LogUUIDAndPaths (LogSP &log_sp, const char *prefix_cstr)
533 {
534     if (log_sp)
535     {
536         Mutex::Locker locker(m_modules_mutex);
537         char uuid_cstr[256];
538         collection::const_iterator pos, begin = m_modules.begin(), end = m_modules.end();
539         for (pos = begin; pos != end; ++pos)
540         {
541             Module *module = pos->get();
542             module->GetUUID().GetAsCString (uuid_cstr, sizeof(uuid_cstr));
543             const FileSpec &module_file_spec = module->GetFileSpec();
544             log_sp->Printf ("%s[%u] %s (%s) \"%s/%s\"",
545                             prefix_cstr ? prefix_cstr : "",
546                             (uint32_t)std::distance (begin, pos),
547                             uuid_cstr,
548                             module->GetArchitecture().GetArchitectureName(),
549                             module_file_spec.GetDirectory().GetCString(),
550                             module_file_spec.GetFilename().GetCString());
551         }
552     }
553 }
554 
555 bool
556 ModuleList::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
557 {
558     Mutex::Locker locker(m_modules_mutex);
559     collection::const_iterator pos, end = m_modules.end();
560     for (pos = m_modules.begin(); pos != end; ++pos)
561     {
562         if ((*pos)->ResolveFileAddress (vm_addr, so_addr))
563             return true;
564     }
565 
566     return false;
567 }
568 
569 uint32_t
570 ModuleList::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
571 {
572     // The address is already section offset so it has a module
573     uint32_t resolved_flags = 0;
574     ModuleSP module_sp (so_addr.GetModule());
575     if (module_sp)
576     {
577         resolved_flags = module_sp->ResolveSymbolContextForAddress (so_addr,
578                                                                     resolve_scope,
579                                                                     sc);
580     }
581     else
582     {
583         Mutex::Locker locker(m_modules_mutex);
584         collection::const_iterator pos, end = m_modules.end();
585         for (pos = m_modules.begin(); pos != end; ++pos)
586         {
587             resolved_flags = (*pos)->ResolveSymbolContextForAddress (so_addr,
588                                                                      resolve_scope,
589                                                                      sc);
590             if (resolved_flags != 0)
591                 break;
592         }
593     }
594 
595     return resolved_flags;
596 }
597 
598 uint32_t
599 ModuleList::ResolveSymbolContextForFilePath
600 (
601     const char *file_path,
602     uint32_t line,
603     bool check_inlines,
604     uint32_t resolve_scope,
605     SymbolContextList& sc_list
606 )
607 {
608     FileSpec file_spec(file_path, false);
609     return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
610 }
611 
612 uint32_t
613 ModuleList::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
614 {
615     Mutex::Locker locker(m_modules_mutex);
616     collection::const_iterator pos, end = m_modules.end();
617     for (pos = m_modules.begin(); pos != end; ++pos)
618     {
619         (*pos)->ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
620     }
621 
622     return sc_list.GetSize();
623 }
624 
625 uint32_t
626 ModuleList::GetIndexForModule (const Module *module) const
627 {
628     if (module)
629     {
630         Mutex::Locker locker(m_modules_mutex);
631         collection::const_iterator pos;
632         collection::const_iterator begin = m_modules.begin();
633         collection::const_iterator end = m_modules.end();
634         for (pos = begin; pos != end; ++pos)
635         {
636             if ((*pos).get() == module)
637                 return std::distance (begin, pos);
638         }
639     }
640     return LLDB_INVALID_INDEX32;
641 }
642 
643 static ModuleList &
644 GetSharedModuleList ()
645 {
646     // NOTE: Intentionally leak the module list so a program doesn't have to
647     // cleanup all modules and object files as it exits. This just wastes time
648     // doing a bunch of cleanup that isn't required.
649     static ModuleList *g_shared_module_list = NULL;
650     if (g_shared_module_list == NULL)
651         g_shared_module_list = new ModuleList(); // <--- Intentional leak!!!
652 
653     return *g_shared_module_list;
654 }
655 
656 bool
657 ModuleList::ModuleIsInCache (const Module *module_ptr)
658 {
659     if (module_ptr)
660     {
661         ModuleList &shared_module_list = GetSharedModuleList ();
662         return shared_module_list.FindModule (module_ptr).get() != NULL;
663     }
664     return false;
665 }
666 
667 size_t
668 ModuleList::FindSharedModules (const ModuleSpec &module_spec, ModuleList &matching_module_list)
669 {
670     return GetSharedModuleList ().FindModules (module_spec, matching_module_list);
671 }
672 
673 uint32_t
674 ModuleList::RemoveOrphanSharedModules (bool mandatory)
675 {
676     return GetSharedModuleList ().RemoveOrphans(mandatory);
677 }
678 
679 Error
680 ModuleList::GetSharedModule
681 (
682     const ModuleSpec &module_spec,
683     ModuleSP &module_sp,
684     const FileSpecList *module_search_paths_ptr,
685     ModuleSP *old_module_sp_ptr,
686     bool *did_create_ptr,
687     bool always_create
688 )
689 {
690     ModuleList &shared_module_list = GetSharedModuleList ();
691     Mutex::Locker locker(shared_module_list.m_modules_mutex);
692     char path[PATH_MAX];
693     char uuid_cstr[64];
694 
695     Error error;
696 
697     module_sp.reset();
698 
699     if (did_create_ptr)
700         *did_create_ptr = false;
701     if (old_module_sp_ptr)
702         old_module_sp_ptr->reset();
703 
704     const UUID *uuid_ptr = module_spec.GetUUIDPtr();
705     const FileSpec &module_file_spec = module_spec.GetFileSpec();
706     const ArchSpec &arch = module_spec.GetArchitecture();
707 
708     // Make sure no one else can try and get or create a module while this
709     // function is actively working on it by doing an extra lock on the
710     // global mutex list.
711     if (always_create == false)
712     {
713         ModuleList matching_module_list;
714         const size_t num_matching_modules = shared_module_list.FindModules (module_spec, matching_module_list);
715         if (num_matching_modules > 0)
716         {
717             for (uint32_t module_idx = 0; module_idx < num_matching_modules; ++module_idx)
718             {
719                 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
720 
721                 // Make sure the file for the module hasn't been modified
722                 if (module_sp->FileHasChanged())
723                 {
724                     if (old_module_sp_ptr && !old_module_sp_ptr->get())
725                         *old_module_sp_ptr = module_sp;
726                     shared_module_list.Remove (module_sp);
727                     module_sp.reset();
728                 }
729                 else
730                 {
731                     // The module matches and the module was not modified from
732                     // when it was last loaded.
733                     return error;
734                 }
735             }
736         }
737     }
738 
739     if (module_sp)
740         return error;
741     else
742     {
743         module_sp.reset (new Module (module_spec));
744         // Make sure there are a module and an object file since we can specify
745         // a valid file path with an architecture that might not be in that file.
746         // By getting the object file we can guarantee that the architecture matches
747         if (module_sp)
748         {
749             if (module_sp->GetObjectFile())
750             {
751                 // If we get in here we got the correct arch, now we just need
752                 // to verify the UUID if one was given
753                 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID())
754                     module_sp.reset();
755                 else
756                 {
757                     if (did_create_ptr)
758                         *did_create_ptr = true;
759 
760                     shared_module_list.ReplaceEquivalent(module_sp);
761                     return error;
762                 }
763             }
764             else
765                 module_sp.reset();
766         }
767     }
768 
769     // Either the file didn't exist where at the path, or no path was given, so
770     // we now have to use more extreme measures to try and find the appropriate
771     // module.
772 
773     // Fixup the incoming path in case the path points to a valid file, yet
774     // the arch or UUID (if one was passed in) don't match.
775     FileSpec file_spec = Symbols::LocateExecutableObjectFile (module_spec);
776 
777     // Don't look for the file if it appears to be the same one we already
778     // checked for above...
779     if (file_spec != module_file_spec)
780     {
781         if (!file_spec.Exists())
782         {
783             file_spec.GetPath(path, sizeof(path));
784             if (path[0] == '\0')
785                 module_file_spec.GetPath(path, sizeof(path));
786             if (file_spec.Exists())
787             {
788                 if (uuid_ptr && uuid_ptr->IsValid())
789                     uuid_ptr->GetAsCString(uuid_cstr, sizeof (uuid_cstr));
790                 else
791                     uuid_cstr[0] = '\0';
792 
793 
794                 if (arch.IsValid())
795                 {
796                     if (uuid_cstr[0])
797                         error.SetErrorStringWithFormat("'%s' does not contain the %s architecture and UUID %s", path, arch.GetArchitectureName(), uuid_cstr);
798                     else
799                         error.SetErrorStringWithFormat("'%s' does not contain the %s architecture.", path, arch.GetArchitectureName());
800                 }
801             }
802             else
803             {
804                 error.SetErrorStringWithFormat("'%s' does not exist", path);
805             }
806             if (error.Fail())
807                 module_sp.reset();
808             return error;
809         }
810 
811 
812         // Make sure no one else can try and get or create a module while this
813         // function is actively working on it by doing an extra lock on the
814         // global mutex list.
815         ModuleSpec platform_module_spec(module_spec);
816         platform_module_spec.GetFileSpec() = file_spec;
817         platform_module_spec.GetPlatformFileSpec() = file_spec;
818         ModuleList matching_module_list;
819         if (shared_module_list.FindModules (platform_module_spec, matching_module_list) > 0)
820         {
821             module_sp = matching_module_list.GetModuleAtIndex(0);
822 
823             // If we didn't have a UUID in mind when looking for the object file,
824             // then we should make sure the modification time hasn't changed!
825             if (platform_module_spec.GetUUIDPtr() == NULL)
826             {
827                 TimeValue file_spec_mod_time(file_spec.GetModificationTime());
828                 if (file_spec_mod_time.IsValid())
829                 {
830                     if (file_spec_mod_time != module_sp->GetModificationTime())
831                     {
832                         if (old_module_sp_ptr)
833                             *old_module_sp_ptr = module_sp;
834                         shared_module_list.Remove (module_sp);
835                         module_sp.reset();
836                     }
837                 }
838             }
839         }
840 
841         if (module_sp.get() == NULL)
842         {
843             module_sp.reset (new Module (platform_module_spec));
844             // Make sure there are a module and an object file since we can specify
845             // a valid file path with an architecture that might not be in that file.
846             // By getting the object file we can guarantee that the architecture matches
847             if (module_sp && module_sp->GetObjectFile())
848             {
849                 if (did_create_ptr)
850                     *did_create_ptr = true;
851 
852                 shared_module_list.ReplaceEquivalent(module_sp);
853             }
854             else
855             {
856                 file_spec.GetPath(path, sizeof(path));
857 
858                 if (file_spec)
859                 {
860                     if (arch.IsValid())
861                         error.SetErrorStringWithFormat("unable to open %s architecture in '%s'", arch.GetArchitectureName(), path);
862                     else
863                         error.SetErrorStringWithFormat("unable to open '%s'", path);
864                 }
865                 else
866                 {
867                     if (uuid_ptr && uuid_ptr->IsValid())
868                         uuid_ptr->GetAsCString(uuid_cstr, sizeof (uuid_cstr));
869                     else
870                         uuid_cstr[0] = '\0';
871 
872                     if (uuid_cstr[0])
873                         error.SetErrorStringWithFormat("cannot locate a module for UUID '%s'", uuid_cstr);
874                     else
875                         error.SetErrorStringWithFormat("cannot locate a module");
876                 }
877             }
878         }
879     }
880 
881     return error;
882 }
883 
884 bool
885 ModuleList::RemoveSharedModule (lldb::ModuleSP &module_sp)
886 {
887     return GetSharedModuleList ().Remove (module_sp);
888 }
889 
890 bool
891 ModuleList::RemoveSharedModuleIfOrphaned (const Module *module_ptr)
892 {
893     return GetSharedModuleList ().RemoveIfOrphaned (module_ptr);
894 }
895 
896 
897