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 (size_t idx) const
300 {
301     Mutex::Locker locker(m_modules_mutex);
302     return GetModulePointerAtIndexUnlocked(idx);
303 }
304 
305 Module*
306 ModuleList::GetModulePointerAtIndexUnlocked (size_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(size_t idx) const
315 {
316     Mutex::Locker locker(m_modules_mutex);
317     return GetModuleAtIndexUnlocked(idx);
318 }
319 
320 ModuleSP
321 ModuleList::GetModuleAtIndexUnlocked(size_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 size_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     const size_t old_size = sc_list.GetSize();
341 
342     if (name_type_mask & eFunctionNameTypeAuto)
343     {
344         ConstString lookup_name;
345         uint32_t lookup_name_type_mask = 0;
346         bool match_name_after_lookup = false;
347         Module::PrepareForFunctionNameLookup (name, name_type_mask,
348                                               lookup_name,
349                                               lookup_name_type_mask,
350                                               match_name_after_lookup);
351 
352         Mutex::Locker locker(m_modules_mutex);
353         collection::const_iterator pos, end = m_modules.end();
354         for (pos = m_modules.begin(); pos != end; ++pos)
355         {
356             (*pos)->FindFunctions (lookup_name,
357                                    NULL,
358                                    lookup_name_type_mask,
359                                    include_symbols,
360                                    include_inlines,
361                                    true,
362                                    sc_list);
363         }
364 
365         if (match_name_after_lookup)
366         {
367             SymbolContext sc;
368             size_t i = old_size;
369             while (i<sc_list.GetSize())
370             {
371                 if (sc_list.GetContextAtIndex(i, sc))
372                 {
373                     const char *func_name = sc.GetFunctionName().GetCString();
374                     if (func_name && strstr (func_name, name.GetCString()) == NULL)
375                     {
376                         // Remove the current context
377                         sc_list.RemoveContextAtIndex(i);
378                         // Don't increment i and continue in the loop
379                         continue;
380                     }
381                 }
382                 ++i;
383             }
384         }
385 
386     }
387     else
388     {
389 
390         Mutex::Locker locker(m_modules_mutex);
391         collection::const_iterator pos, end = m_modules.end();
392         for (pos = m_modules.begin(); pos != end; ++pos)
393         {
394             (*pos)->FindFunctions (name, NULL, name_type_mask, include_symbols, include_inlines, true, sc_list);
395         }
396     }
397     return sc_list.GetSize() - old_size;
398 }
399 
400 size_t
401 ModuleList::FindCompileUnits (const FileSpec &path,
402                               bool append,
403                               SymbolContextList &sc_list) const
404 {
405     if (!append)
406         sc_list.Clear();
407 
408     Mutex::Locker locker(m_modules_mutex);
409     collection::const_iterator pos, end = m_modules.end();
410     for (pos = m_modules.begin(); pos != end; ++pos)
411     {
412         (*pos)->FindCompileUnits (path, true, sc_list);
413     }
414 
415     return sc_list.GetSize();
416 }
417 
418 size_t
419 ModuleList::FindGlobalVariables (const ConstString &name,
420                                  bool append,
421                                  size_t max_matches,
422                                  VariableList& variable_list) const
423 {
424     size_t initial_size = variable_list.GetSize();
425     Mutex::Locker locker(m_modules_mutex);
426     collection::const_iterator pos, end = m_modules.end();
427     for (pos = m_modules.begin(); pos != end; ++pos)
428     {
429         (*pos)->FindGlobalVariables (name, NULL, append, max_matches, variable_list);
430     }
431     return variable_list.GetSize() - initial_size;
432 }
433 
434 
435 size_t
436 ModuleList::FindGlobalVariables (const RegularExpression& regex,
437                                  bool append,
438                                  size_t max_matches,
439                                  VariableList& variable_list) const
440 {
441     size_t initial_size = variable_list.GetSize();
442     Mutex::Locker locker(m_modules_mutex);
443     collection::const_iterator pos, end = m_modules.end();
444     for (pos = m_modules.begin(); pos != end; ++pos)
445     {
446         (*pos)->FindGlobalVariables (regex, append, max_matches, variable_list);
447     }
448     return variable_list.GetSize() - initial_size;
449 }
450 
451 
452 size_t
453 ModuleList::FindSymbolsWithNameAndType (const ConstString &name,
454                                         SymbolType symbol_type,
455                                         SymbolContextList &sc_list,
456                                         bool append) const
457 {
458     Mutex::Locker locker(m_modules_mutex);
459     if (!append)
460         sc_list.Clear();
461     size_t initial_size = sc_list.GetSize();
462 
463     collection::const_iterator pos, end = m_modules.end();
464     for (pos = m_modules.begin(); pos != end; ++pos)
465         (*pos)->FindSymbolsWithNameAndType (name, symbol_type, sc_list);
466     return sc_list.GetSize() - initial_size;
467 }
468 
469 size_t
470 ModuleList::FindSymbolsMatchingRegExAndType (const RegularExpression &regex,
471                                              lldb::SymbolType symbol_type,
472                                              SymbolContextList &sc_list,
473                                              bool append) const
474 {
475     Mutex::Locker locker(m_modules_mutex);
476     if (!append)
477         sc_list.Clear();
478     size_t initial_size = sc_list.GetSize();
479 
480     collection::const_iterator pos, end = m_modules.end();
481     for (pos = m_modules.begin(); pos != end; ++pos)
482         (*pos)->FindSymbolsMatchingRegExAndType (regex, symbol_type, sc_list);
483     return sc_list.GetSize() - initial_size;
484 }
485 
486 size_t
487 ModuleList::FindModules (const ModuleSpec &module_spec, ModuleList& matching_module_list) const
488 {
489     size_t existing_matches = matching_module_list.GetSize();
490 
491     Mutex::Locker locker(m_modules_mutex);
492     collection::const_iterator pos, end = m_modules.end();
493     for (pos = m_modules.begin(); pos != end; ++pos)
494     {
495         ModuleSP module_sp(*pos);
496         if (module_sp->MatchesModuleSpec (module_spec))
497             matching_module_list.Append(module_sp);
498     }
499     return matching_module_list.GetSize() - existing_matches;
500 }
501 
502 ModuleSP
503 ModuleList::FindModule (const Module *module_ptr) const
504 {
505     ModuleSP module_sp;
506 
507     // Scope for "locker"
508     {
509         Mutex::Locker locker(m_modules_mutex);
510         collection::const_iterator pos, end = m_modules.end();
511 
512         for (pos = m_modules.begin(); pos != end; ++pos)
513         {
514             if ((*pos).get() == module_ptr)
515             {
516                 module_sp = (*pos);
517                 break;
518             }
519         }
520     }
521     return module_sp;
522 
523 }
524 
525 ModuleSP
526 ModuleList::FindModule (const UUID &uuid) const
527 {
528     ModuleSP module_sp;
529 
530     if (uuid.IsValid())
531     {
532         Mutex::Locker locker(m_modules_mutex);
533         collection::const_iterator pos, end = m_modules.end();
534 
535         for (pos = m_modules.begin(); pos != end; ++pos)
536         {
537             if ((*pos)->GetUUID() == uuid)
538             {
539                 module_sp = (*pos);
540                 break;
541             }
542         }
543     }
544     return module_sp;
545 }
546 
547 
548 size_t
549 ModuleList::FindTypes (const SymbolContext& sc, const ConstString &name, bool name_is_fully_qualified, size_t max_matches, TypeList& types) const
550 {
551     Mutex::Locker locker(m_modules_mutex);
552 
553     size_t total_matches = 0;
554     collection::const_iterator pos, end = m_modules.end();
555     if (sc.module_sp)
556     {
557         // The symbol context "sc" contains a module so we want to search that
558         // one first if it is in our list...
559         for (pos = m_modules.begin(); pos != end; ++pos)
560         {
561             if (sc.module_sp.get() == (*pos).get())
562             {
563                 total_matches += (*pos)->FindTypes (sc, name, name_is_fully_qualified, max_matches, types);
564 
565                 if (total_matches >= max_matches)
566                     break;
567             }
568         }
569     }
570 
571     if (total_matches < max_matches)
572     {
573         SymbolContext world_sc;
574         for (pos = m_modules.begin(); pos != end; ++pos)
575         {
576             // Search the module if the module is not equal to the one in the symbol
577             // context "sc". If "sc" contains a empty module shared pointer, then
578             // the comparisong will always be true (valid_module_ptr != NULL).
579             if (sc.module_sp.get() != (*pos).get())
580                 total_matches += (*pos)->FindTypes (world_sc, name, name_is_fully_qualified, max_matches, types);
581 
582             if (total_matches >= max_matches)
583                 break;
584         }
585     }
586 
587     return total_matches;
588 }
589 
590 bool
591 ModuleList::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
592 {
593     Mutex::Locker locker(m_modules_mutex);
594     collection::const_iterator pos, end = m_modules.end();
595     for (pos = m_modules.begin(); pos != end; ++pos)
596     {
597         if ((*pos)->FindSourceFile (orig_spec, new_spec))
598             return true;
599     }
600     return false;
601 }
602 
603 
604 
605 ModuleSP
606 ModuleList::FindFirstModule (const ModuleSpec &module_spec) const
607 {
608     ModuleSP module_sp;
609     Mutex::Locker locker(m_modules_mutex);
610     collection::const_iterator pos, end = m_modules.end();
611     for (pos = m_modules.begin(); pos != end; ++pos)
612     {
613         ModuleSP module_sp(*pos);
614         if (module_sp->MatchesModuleSpec (module_spec))
615             return module_sp;
616     }
617     return module_sp;
618 
619 }
620 
621 size_t
622 ModuleList::GetSize() const
623 {
624     size_t size = 0;
625     {
626         Mutex::Locker locker(m_modules_mutex);
627         size = m_modules.size();
628     }
629     return size;
630 }
631 
632 
633 void
634 ModuleList::Dump(Stream *s) const
635 {
636 //  s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
637 //  s.Indent();
638 //  s << "ModuleList\n";
639 
640     Mutex::Locker locker(m_modules_mutex);
641     collection::const_iterator pos, end = m_modules.end();
642     for (pos = m_modules.begin(); pos != end; ++pos)
643     {
644         (*pos)->Dump(s);
645     }
646 }
647 
648 void
649 ModuleList::LogUUIDAndPaths (Log *log, const char *prefix_cstr)
650 {
651     if (log)
652     {
653         Mutex::Locker locker(m_modules_mutex);
654         char uuid_cstr[256];
655         collection::const_iterator pos, begin = m_modules.begin(), end = m_modules.end();
656         for (pos = begin; pos != end; ++pos)
657         {
658             Module *module = pos->get();
659             module->GetUUID().GetAsCString (uuid_cstr, sizeof(uuid_cstr));
660             const FileSpec &module_file_spec = module->GetFileSpec();
661             log->Printf ("%s[%u] %s (%s) \"%s\"",
662                          prefix_cstr ? prefix_cstr : "",
663                          (uint32_t)std::distance (begin, pos),
664                          uuid_cstr,
665                          module->GetArchitecture().GetArchitectureName(),
666                          module_file_spec.GetPath().c_str());
667         }
668     }
669 }
670 
671 bool
672 ModuleList::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr) const
673 {
674     Mutex::Locker locker(m_modules_mutex);
675     collection::const_iterator pos, end = m_modules.end();
676     for (pos = m_modules.begin(); pos != end; ++pos)
677     {
678         if ((*pos)->ResolveFileAddress (vm_addr, so_addr))
679             return true;
680     }
681 
682     return false;
683 }
684 
685 uint32_t
686 ModuleList::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) const
687 {
688     // The address is already section offset so it has a module
689     uint32_t resolved_flags = 0;
690     ModuleSP module_sp (so_addr.GetModule());
691     if (module_sp)
692     {
693         resolved_flags = module_sp->ResolveSymbolContextForAddress (so_addr,
694                                                                     resolve_scope,
695                                                                     sc);
696     }
697     else
698     {
699         Mutex::Locker locker(m_modules_mutex);
700         collection::const_iterator pos, end = m_modules.end();
701         for (pos = m_modules.begin(); pos != end; ++pos)
702         {
703             resolved_flags = (*pos)->ResolveSymbolContextForAddress (so_addr,
704                                                                      resolve_scope,
705                                                                      sc);
706             if (resolved_flags != 0)
707                 break;
708         }
709     }
710 
711     return resolved_flags;
712 }
713 
714 uint32_t
715 ModuleList::ResolveSymbolContextForFilePath
716 (
717     const char *file_path,
718     uint32_t line,
719     bool check_inlines,
720     uint32_t resolve_scope,
721     SymbolContextList& sc_list
722 )  const
723 {
724     FileSpec file_spec(file_path, false);
725     return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
726 }
727 
728 uint32_t
729 ModuleList::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) const
730 {
731     Mutex::Locker locker(m_modules_mutex);
732     collection::const_iterator pos, end = m_modules.end();
733     for (pos = m_modules.begin(); pos != end; ++pos)
734     {
735         (*pos)->ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
736     }
737 
738     return sc_list.GetSize();
739 }
740 
741 size_t
742 ModuleList::GetIndexForModule (const Module *module) const
743 {
744     if (module)
745     {
746         Mutex::Locker locker(m_modules_mutex);
747         collection::const_iterator pos;
748         collection::const_iterator begin = m_modules.begin();
749         collection::const_iterator end = m_modules.end();
750         for (pos = begin; pos != end; ++pos)
751         {
752             if ((*pos).get() == module)
753                 return std::distance (begin, pos);
754         }
755     }
756     return LLDB_INVALID_INDEX32;
757 }
758 
759 static ModuleList &
760 GetSharedModuleList ()
761 {
762     // NOTE: Intentionally leak the module list so a program doesn't have to
763     // cleanup all modules and object files as it exits. This just wastes time
764     // doing a bunch of cleanup that isn't required.
765     static ModuleList *g_shared_module_list = NULL;
766     if (g_shared_module_list == NULL)
767         g_shared_module_list = new ModuleList(); // <--- Intentional leak!!!
768 
769     return *g_shared_module_list;
770 }
771 
772 bool
773 ModuleList::ModuleIsInCache (const Module *module_ptr)
774 {
775     if (module_ptr)
776     {
777         ModuleList &shared_module_list = GetSharedModuleList ();
778         return shared_module_list.FindModule (module_ptr).get() != NULL;
779     }
780     return false;
781 }
782 
783 size_t
784 ModuleList::FindSharedModules (const ModuleSpec &module_spec, ModuleList &matching_module_list)
785 {
786     return GetSharedModuleList ().FindModules (module_spec, matching_module_list);
787 }
788 
789 size_t
790 ModuleList::RemoveOrphanSharedModules (bool mandatory)
791 {
792     return GetSharedModuleList ().RemoveOrphans(mandatory);
793 }
794 
795 Error
796 ModuleList::GetSharedModule
797 (
798     const ModuleSpec &module_spec,
799     ModuleSP &module_sp,
800     const FileSpecList *module_search_paths_ptr,
801     ModuleSP *old_module_sp_ptr,
802     bool *did_create_ptr,
803     bool always_create
804 )
805 {
806     ModuleList &shared_module_list = GetSharedModuleList ();
807     Mutex::Locker locker(shared_module_list.m_modules_mutex);
808     char path[PATH_MAX];
809     char uuid_cstr[64];
810 
811     Error error;
812 
813     module_sp.reset();
814 
815     if (did_create_ptr)
816         *did_create_ptr = false;
817     if (old_module_sp_ptr)
818         old_module_sp_ptr->reset();
819 
820     const UUID *uuid_ptr = module_spec.GetUUIDPtr();
821     const FileSpec &module_file_spec = module_spec.GetFileSpec();
822     const ArchSpec &arch = module_spec.GetArchitecture();
823 
824     // Make sure no one else can try and get or create a module while this
825     // function is actively working on it by doing an extra lock on the
826     // global mutex list.
827     if (always_create == false)
828     {
829         ModuleList matching_module_list;
830         const size_t num_matching_modules = shared_module_list.FindModules (module_spec, matching_module_list);
831         if (num_matching_modules > 0)
832         {
833             for (size_t module_idx = 0; module_idx < num_matching_modules; ++module_idx)
834             {
835                 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
836 
837                 // Make sure the file for the module hasn't been modified
838                 if (module_sp->FileHasChanged())
839                 {
840                     if (old_module_sp_ptr && !old_module_sp_ptr->get())
841                         *old_module_sp_ptr = module_sp;
842 
843                     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_MODULES));
844                     if (log)
845                         log->Printf("module changed: %p, removing from global module list", module_sp.get());
846 
847                     shared_module_list.Remove (module_sp);
848                     module_sp.reset();
849                 }
850                 else
851                 {
852                     // The module matches and the module was not modified from
853                     // when it was last loaded.
854                     return error;
855                 }
856             }
857         }
858     }
859 
860     if (module_sp)
861         return error;
862     else
863     {
864         module_sp.reset (new Module (module_spec));
865         // Make sure there are a module and an object file since we can specify
866         // a valid file path with an architecture that might not be in that file.
867         // By getting the object file we can guarantee that the architecture matches
868         if (module_sp)
869         {
870             if (module_sp->GetObjectFile())
871             {
872                 // If we get in here we got the correct arch, now we just need
873                 // to verify the UUID if one was given
874                 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID())
875                     module_sp.reset();
876                 else
877                 {
878                     if (did_create_ptr)
879                         *did_create_ptr = true;
880 
881                     shared_module_list.ReplaceEquivalent(module_sp);
882                     return error;
883                 }
884             }
885             else
886                 module_sp.reset();
887         }
888     }
889 
890     // Either the file didn't exist where at the path, or no path was given, so
891     // we now have to use more extreme measures to try and find the appropriate
892     // module.
893 
894     // Fixup the incoming path in case the path points to a valid file, yet
895     // the arch or UUID (if one was passed in) don't match.
896     FileSpec file_spec = Symbols::LocateExecutableObjectFile (module_spec);
897 
898     // Don't look for the file if it appears to be the same one we already
899     // checked for above...
900     if (file_spec != module_file_spec)
901     {
902         if (!file_spec.Exists())
903         {
904             file_spec.GetPath(path, sizeof(path));
905             if (path[0] == '\0')
906                 module_file_spec.GetPath(path, sizeof(path));
907             if (file_spec.Exists())
908             {
909                 if (uuid_ptr && uuid_ptr->IsValid())
910                     uuid_ptr->GetAsCString(uuid_cstr, sizeof (uuid_cstr));
911                 else
912                     uuid_cstr[0] = '\0';
913 
914 
915                 if (arch.IsValid())
916                 {
917                     if (uuid_cstr[0])
918                         error.SetErrorStringWithFormat("'%s' does not contain the %s architecture and UUID %s", path, arch.GetArchitectureName(), uuid_cstr);
919                     else
920                         error.SetErrorStringWithFormat("'%s' does not contain the %s architecture.", path, arch.GetArchitectureName());
921                 }
922             }
923             else
924             {
925                 error.SetErrorStringWithFormat("'%s' does not exist", path);
926             }
927             if (error.Fail())
928                 module_sp.reset();
929             return error;
930         }
931 
932 
933         // Make sure no one else can try and get or create a module while this
934         // function is actively working on it by doing an extra lock on the
935         // global mutex list.
936         ModuleSpec platform_module_spec(module_spec);
937         platform_module_spec.GetFileSpec() = file_spec;
938         platform_module_spec.GetPlatformFileSpec() = file_spec;
939         ModuleList matching_module_list;
940         if (shared_module_list.FindModules (platform_module_spec, matching_module_list) > 0)
941         {
942             module_sp = matching_module_list.GetModuleAtIndex(0);
943 
944             // If we didn't have a UUID in mind when looking for the object file,
945             // then we should make sure the modification time hasn't changed!
946             if (platform_module_spec.GetUUIDPtr() == NULL)
947             {
948                 TimeValue file_spec_mod_time(file_spec.GetModificationTime());
949                 if (file_spec_mod_time.IsValid())
950                 {
951                     if (file_spec_mod_time != module_sp->GetModificationTime())
952                     {
953                         if (old_module_sp_ptr)
954                             *old_module_sp_ptr = module_sp;
955                         shared_module_list.Remove (module_sp);
956                         module_sp.reset();
957                     }
958                 }
959             }
960         }
961 
962         if (module_sp.get() == NULL)
963         {
964             module_sp.reset (new Module (platform_module_spec));
965             // Make sure there are a module and an object file since we can specify
966             // a valid file path with an architecture that might not be in that file.
967             // By getting the object file we can guarantee that the architecture matches
968             if (module_sp && module_sp->GetObjectFile())
969             {
970                 if (did_create_ptr)
971                     *did_create_ptr = true;
972 
973                 shared_module_list.ReplaceEquivalent(module_sp);
974             }
975             else
976             {
977                 file_spec.GetPath(path, sizeof(path));
978 
979                 if (file_spec)
980                 {
981                     if (arch.IsValid())
982                         error.SetErrorStringWithFormat("unable to open %s architecture in '%s'", arch.GetArchitectureName(), path);
983                     else
984                         error.SetErrorStringWithFormat("unable to open '%s'", path);
985                 }
986                 else
987                 {
988                     if (uuid_ptr && uuid_ptr->IsValid())
989                         uuid_ptr->GetAsCString(uuid_cstr, sizeof (uuid_cstr));
990                     else
991                         uuid_cstr[0] = '\0';
992 
993                     if (uuid_cstr[0])
994                         error.SetErrorStringWithFormat("cannot locate a module for UUID '%s'", uuid_cstr);
995                     else
996                         error.SetErrorStringWithFormat("cannot locate a module");
997                 }
998             }
999         }
1000     }
1001 
1002     return error;
1003 }
1004 
1005 bool
1006 ModuleList::RemoveSharedModule (lldb::ModuleSP &module_sp)
1007 {
1008     return GetSharedModuleList ().Remove (module_sp);
1009 }
1010 
1011 bool
1012 ModuleList::RemoveSharedModuleIfOrphaned (const Module *module_ptr)
1013 {
1014     return GetSharedModuleList ().RemoveIfOrphaned (module_ptr);
1015 }
1016 
1017 
1018