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     static ModuleList g_shared_module_list;
647     return g_shared_module_list;
648 }
649 
650 bool
651 ModuleList::ModuleIsInCache (const Module *module_ptr)
652 {
653     if (module_ptr)
654     {
655         ModuleList &shared_module_list = GetSharedModuleList ();
656         return shared_module_list.FindModule (module_ptr).get() != NULL;
657     }
658     return false;
659 }
660 
661 size_t
662 ModuleList::FindSharedModules (const ModuleSpec &module_spec, ModuleList &matching_module_list)
663 {
664     return GetSharedModuleList ().FindModules (module_spec, matching_module_list);
665 }
666 
667 uint32_t
668 ModuleList::RemoveOrphanSharedModules (bool mandatory)
669 {
670     return GetSharedModuleList ().RemoveOrphans(mandatory);
671 }
672 
673 Error
674 ModuleList::GetSharedModule
675 (
676     const ModuleSpec &module_spec,
677     ModuleSP &module_sp,
678     const FileSpecList *module_search_paths_ptr,
679     ModuleSP *old_module_sp_ptr,
680     bool *did_create_ptr,
681     bool always_create
682 )
683 {
684     ModuleList &shared_module_list = GetSharedModuleList ();
685     Mutex::Locker locker(shared_module_list.m_modules_mutex);
686     char path[PATH_MAX];
687     char uuid_cstr[64];
688 
689     Error error;
690 
691     module_sp.reset();
692 
693     if (did_create_ptr)
694         *did_create_ptr = false;
695     if (old_module_sp_ptr)
696         old_module_sp_ptr->reset();
697 
698     const UUID *uuid_ptr = module_spec.GetUUIDPtr();
699     const FileSpec &module_file_spec = module_spec.GetFileSpec();
700     const ArchSpec &arch = module_spec.GetArchitecture();
701 
702     // Make sure no one else can try and get or create a module while this
703     // function is actively working on it by doing an extra lock on the
704     // global mutex list.
705     if (always_create == false)
706     {
707         ModuleList matching_module_list;
708         const size_t num_matching_modules = shared_module_list.FindModules (module_spec, matching_module_list);
709         if (num_matching_modules > 0)
710         {
711             for (uint32_t module_idx = 0; module_idx < num_matching_modules; ++module_idx)
712             {
713                 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
714 
715                 // Make sure the file for the module hasn't been modified
716                 if (module_sp->FileHasChanged())
717                 {
718                     if (old_module_sp_ptr && !old_module_sp_ptr->get())
719                         *old_module_sp_ptr = module_sp;
720                     shared_module_list.Remove (module_sp);
721                     module_sp.reset();
722                 }
723                 else
724                 {
725                     // The module matches and the module was not modified from
726                     // when it was last loaded.
727                     return error;
728                 }
729             }
730         }
731     }
732 
733     if (module_sp)
734         return error;
735     else
736     {
737         module_sp.reset (new Module (module_spec));
738         // Make sure there are a module and an object file since we can specify
739         // a valid file path with an architecture that might not be in that file.
740         // By getting the object file we can guarantee that the architecture matches
741         if (module_sp)
742         {
743             if (module_sp->GetObjectFile())
744             {
745                 // If we get in here we got the correct arch, now we just need
746                 // to verify the UUID if one was given
747                 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID())
748                     module_sp.reset();
749                 else
750                 {
751                     if (did_create_ptr)
752                         *did_create_ptr = true;
753 
754                     shared_module_list.ReplaceEquivalent(module_sp);
755                     return error;
756                 }
757             }
758             else
759                 module_sp.reset();
760         }
761     }
762 
763     // Either the file didn't exist where at the path, or no path was given, so
764     // we now have to use more extreme measures to try and find the appropriate
765     // module.
766 
767     // Fixup the incoming path in case the path points to a valid file, yet
768     // the arch or UUID (if one was passed in) don't match.
769     FileSpec file_spec = Symbols::LocateExecutableObjectFile (module_spec);
770 
771     // Don't look for the file if it appears to be the same one we already
772     // checked for above...
773     if (file_spec != module_file_spec)
774     {
775         if (!file_spec.Exists())
776         {
777             file_spec.GetPath(path, sizeof(path));
778             if (path[0] == '\0')
779                 module_file_spec.GetPath(path, sizeof(path));
780             if (file_spec.Exists())
781             {
782                 if (uuid_ptr && uuid_ptr->IsValid())
783                     uuid_ptr->GetAsCString(uuid_cstr, sizeof (uuid_cstr));
784                 else
785                     uuid_cstr[0] = '\0';
786 
787 
788                 if (arch.IsValid())
789                 {
790                     if (uuid_cstr[0])
791                         error.SetErrorStringWithFormat("'%s' does not contain the %s architecture and UUID %s", path, arch.GetArchitectureName(), uuid_cstr);
792                     else
793                         error.SetErrorStringWithFormat("'%s' does not contain the %s architecture.", path, arch.GetArchitectureName());
794                 }
795             }
796             else
797             {
798                 error.SetErrorStringWithFormat("'%s' does not exist", path);
799             }
800             if (error.Fail())
801                 module_sp.reset();
802             return error;
803         }
804 
805 
806         // Make sure no one else can try and get or create a module while this
807         // function is actively working on it by doing an extra lock on the
808         // global mutex list.
809         ModuleSpec platform_module_spec(module_spec);
810         platform_module_spec.GetFileSpec() = file_spec;
811         platform_module_spec.GetPlatformFileSpec() = file_spec;
812         ModuleList matching_module_list;
813         if (shared_module_list.FindModules (platform_module_spec, matching_module_list) > 0)
814         {
815             module_sp = matching_module_list.GetModuleAtIndex(0);
816 
817             // If we didn't have a UUID in mind when looking for the object file,
818             // then we should make sure the modification time hasn't changed!
819             if (platform_module_spec.GetUUIDPtr() == NULL)
820             {
821                 TimeValue file_spec_mod_time(file_spec.GetModificationTime());
822                 if (file_spec_mod_time.IsValid())
823                 {
824                     if (file_spec_mod_time != module_sp->GetModificationTime())
825                     {
826                         if (old_module_sp_ptr)
827                             *old_module_sp_ptr = module_sp;
828                         shared_module_list.Remove (module_sp);
829                         module_sp.reset();
830                     }
831                 }
832             }
833         }
834 
835         if (module_sp.get() == NULL)
836         {
837             module_sp.reset (new Module (platform_module_spec));
838             // Make sure there are a module and an object file since we can specify
839             // a valid file path with an architecture that might not be in that file.
840             // By getting the object file we can guarantee that the architecture matches
841             if (module_sp && module_sp->GetObjectFile())
842             {
843                 if (did_create_ptr)
844                     *did_create_ptr = true;
845 
846                 shared_module_list.ReplaceEquivalent(module_sp);
847             }
848             else
849             {
850                 file_spec.GetPath(path, sizeof(path));
851 
852                 if (file_spec)
853                 {
854                     if (arch.IsValid())
855                         error.SetErrorStringWithFormat("unable to open %s architecture in '%s'", arch.GetArchitectureName(), path);
856                     else
857                         error.SetErrorStringWithFormat("unable to open '%s'", path);
858                 }
859                 else
860                 {
861                     if (uuid_ptr && uuid_ptr->IsValid())
862                         uuid_ptr->GetAsCString(uuid_cstr, sizeof (uuid_cstr));
863                     else
864                         uuid_cstr[0] = '\0';
865 
866                     if (uuid_cstr[0])
867                         error.SetErrorStringWithFormat("cannot locate a module for UUID '%s'", uuid_cstr);
868                     else
869                         error.SetErrorStringWithFormat("cannot locate a module");
870                 }
871             }
872         }
873     }
874 
875     return error;
876 }
877 
878 bool
879 ModuleList::RemoveSharedModule (lldb::ModuleSP &module_sp)
880 {
881     return GetSharedModuleList ().Remove (module_sp);
882 }
883 
884 bool
885 ModuleList::RemoveSharedModuleIfOrphaned (const Module *module_ptr)
886 {
887     return GetSharedModuleList ().RemoveIfOrphaned (module_ptr);
888 }
889 
890 
891