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