]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/lldb/source/Core/ModuleList.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / tools / lldb / source / Core / ModuleList.cpp
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     {
155         if (AppendIfNeeded(pos))
156             any_in = true;
157     }
158     return any_in;
159 }
160
161 bool
162 ModuleList::RemoveImpl (const ModuleSP &module_sp, bool use_notifier)
163 {
164     if (module_sp)
165     {
166         Mutex::Locker locker(m_modules_mutex);
167         collection::iterator pos, end = m_modules.end();
168         for (pos = m_modules.begin(); pos != end; ++pos)
169         {
170             if (pos->get() == module_sp.get())
171             {
172                 m_modules.erase (pos);
173                 if (use_notifier && m_notifier)
174                     m_notifier->ModuleRemoved(*this, module_sp);
175                 return true;
176             }
177         }
178     }
179     return false;
180 }
181
182 ModuleList::collection::iterator
183 ModuleList::RemoveImpl (ModuleList::collection::iterator pos, bool use_notifier)
184 {
185     ModuleSP module_sp(*pos);
186     collection::iterator retval = m_modules.erase(pos);
187     if (use_notifier && m_notifier)
188         m_notifier->ModuleRemoved(*this, module_sp);
189     return retval;
190 }
191
192 bool
193 ModuleList::Remove (const ModuleSP &module_sp)
194 {
195     return RemoveImpl (module_sp);
196 }
197
198 bool
199 ModuleList::ReplaceModule (const lldb::ModuleSP &old_module_sp, const lldb::ModuleSP &new_module_sp)
200 {
201     if (!RemoveImpl(old_module_sp, false))
202         return false;
203     AppendImpl (new_module_sp, false);
204     if (m_notifier)
205         m_notifier->ModuleUpdated(*this, old_module_sp,new_module_sp);
206     return true;
207 }
208
209 bool
210 ModuleList::RemoveIfOrphaned (const Module *module_ptr)
211 {
212     if (module_ptr)
213     {
214         Mutex::Locker locker(m_modules_mutex);
215         collection::iterator pos, end = m_modules.end();
216         for (pos = m_modules.begin(); pos != end; ++pos)
217         {
218             if (pos->get() == module_ptr)
219             {
220                 if (pos->unique())
221                 {
222                     pos = RemoveImpl(pos);
223                     return true;
224                 }
225                 else
226                     return false;
227             }
228         }
229     }
230     return false;
231 }
232
233 size_t
234 ModuleList::RemoveOrphans (bool mandatory)
235 {
236     Mutex::Locker locker;
237     
238     if (mandatory)
239     {
240         locker.Lock (m_modules_mutex);
241     }
242     else
243     {
244         // Not mandatory, remove orphans if we can get the mutex
245         if (!locker.TryLock(m_modules_mutex))
246             return 0;
247     }
248     collection::iterator pos = m_modules.begin();
249     size_t remove_count = 0;
250     while (pos != m_modules.end())
251     {
252         if (pos->unique())
253         {
254             pos = RemoveImpl(pos);
255             ++remove_count;
256         }
257         else
258         {
259             ++pos;
260         }
261     }
262     return remove_count;
263 }
264
265 size_t
266 ModuleList::Remove (ModuleList &module_list)
267 {
268     Mutex::Locker locker(m_modules_mutex);
269     size_t num_removed = 0;
270     collection::iterator pos, end = module_list.m_modules.end();
271     for (pos = module_list.m_modules.begin(); pos != end; ++pos)
272     {
273         if (Remove (*pos))
274             ++num_removed;
275     }
276     return num_removed;
277 }
278
279
280 void
281 ModuleList::Clear()
282 {
283     ClearImpl();
284 }
285
286 void
287 ModuleList::Destroy()
288 {
289     ClearImpl();
290 }
291
292 void
293 ModuleList::ClearImpl (bool use_notifier)
294 {
295     Mutex::Locker locker(m_modules_mutex);
296     if (use_notifier && m_notifier)
297         m_notifier->WillClearList(*this);
298     m_modules.clear();
299 }
300
301 Module*
302 ModuleList::GetModulePointerAtIndex (size_t idx) const
303 {
304     Mutex::Locker locker(m_modules_mutex);
305     return GetModulePointerAtIndexUnlocked(idx);
306 }
307
308 Module*
309 ModuleList::GetModulePointerAtIndexUnlocked (size_t idx) const
310 {
311     if (idx < m_modules.size())
312         return m_modules[idx].get();
313     return NULL;
314 }
315
316 ModuleSP
317 ModuleList::GetModuleAtIndex(size_t idx) const
318 {
319     Mutex::Locker locker(m_modules_mutex);
320     return GetModuleAtIndexUnlocked(idx);
321 }
322
323 ModuleSP
324 ModuleList::GetModuleAtIndexUnlocked(size_t idx) const
325 {
326     ModuleSP module_sp;
327     if (idx < m_modules.size())
328         module_sp = m_modules[idx];
329     return module_sp;
330 }
331
332 size_t
333 ModuleList::FindFunctions (const ConstString &name, 
334                            uint32_t name_type_mask, 
335                            bool include_symbols,
336                            bool include_inlines,
337                            bool append, 
338                            SymbolContextList &sc_list) const
339 {
340     if (!append)
341         sc_list.Clear();
342     
343     const size_t old_size = sc_list.GetSize();
344     
345     if (name_type_mask & eFunctionNameTypeAuto)
346     {
347         ConstString lookup_name;
348         uint32_t lookup_name_type_mask = 0;
349         bool match_name_after_lookup = false;
350         Module::PrepareForFunctionNameLookup (name, name_type_mask,
351                                               lookup_name,
352                                               lookup_name_type_mask,
353                                               match_name_after_lookup);
354     
355         Mutex::Locker locker(m_modules_mutex);
356         collection::const_iterator pos, end = m_modules.end();
357         for (pos = m_modules.begin(); pos != end; ++pos)
358         {
359             (*pos)->FindFunctions (lookup_name,
360                                    NULL,
361                                    lookup_name_type_mask,
362                                    include_symbols,
363                                    include_inlines,
364                                    true,
365                                    sc_list);
366         }
367         
368         if (match_name_after_lookup)
369         {
370             SymbolContext sc;
371             size_t i = old_size;
372             while (i<sc_list.GetSize())
373             {
374                 if (sc_list.GetContextAtIndex(i, sc))
375                 {
376                     const char *func_name = sc.GetFunctionName().GetCString();
377                     if (func_name && strstr (func_name, name.GetCString()) == NULL)
378                     {
379                         // Remove the current context
380                         sc_list.RemoveContextAtIndex(i);
381                         // Don't increment i and continue in the loop
382                         continue;
383                     }
384                 }
385                 ++i;
386             }
387         }
388
389     }
390     else
391     {
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)->FindFunctions (name, NULL, name_type_mask, include_symbols, include_inlines, true, sc_list);
397         }
398     }
399     return sc_list.GetSize() - old_size;
400 }
401
402 size_t
403 ModuleList::FindFunctionSymbols (const ConstString &name,
404                                  uint32_t name_type_mask,
405                                  SymbolContextList& sc_list)
406 {
407     const size_t old_size = sc_list.GetSize();
408
409     if (name_type_mask & eFunctionNameTypeAuto)
410     {
411         ConstString lookup_name;
412         uint32_t lookup_name_type_mask = 0;
413         bool match_name_after_lookup = false;
414         Module::PrepareForFunctionNameLookup (name, name_type_mask,
415                                               lookup_name,
416                                               lookup_name_type_mask,
417                                               match_name_after_lookup);
418     
419         Mutex::Locker locker(m_modules_mutex);
420         collection::const_iterator pos, end = m_modules.end();
421         for (pos = m_modules.begin(); pos != end; ++pos)
422         {
423             (*pos)->FindFunctionSymbols (lookup_name,
424                                    lookup_name_type_mask,
425                                    sc_list);
426         }
427         
428         if (match_name_after_lookup)
429         {
430             SymbolContext sc;
431             size_t i = old_size;
432             while (i<sc_list.GetSize())
433             {
434                 if (sc_list.GetContextAtIndex(i, sc))
435                 {
436                     const char *func_name = sc.GetFunctionName().GetCString();
437                     if (func_name && strstr (func_name, name.GetCString()) == NULL)
438                     {
439                         // Remove the current context
440                         sc_list.RemoveContextAtIndex(i);
441                         // Don't increment i and continue in the loop
442                         continue;
443                     }
444                 }
445                 ++i;
446             }
447         }
448
449     }
450     else
451     {
452         Mutex::Locker locker(m_modules_mutex);
453         collection::const_iterator pos, end = m_modules.end();
454         for (pos = m_modules.begin(); pos != end; ++pos)
455         {
456             (*pos)->FindFunctionSymbols (name, name_type_mask, sc_list);
457         }
458     }
459
460     return sc_list.GetSize() - old_size;
461 }
462
463 size_t
464 ModuleList::FindCompileUnits (const FileSpec &path, 
465                               bool append, 
466                               SymbolContextList &sc_list) const
467 {
468     if (!append)
469         sc_list.Clear();
470     
471     Mutex::Locker locker(m_modules_mutex);
472     collection::const_iterator pos, end = m_modules.end();
473     for (pos = m_modules.begin(); pos != end; ++pos)
474     {
475         (*pos)->FindCompileUnits (path, true, sc_list);
476     }
477     
478     return sc_list.GetSize();
479 }
480
481 size_t
482 ModuleList::FindGlobalVariables (const ConstString &name, 
483                                  bool append, 
484                                  size_t max_matches,
485                                  VariableList& variable_list) const
486 {
487     size_t initial_size = variable_list.GetSize();
488     Mutex::Locker locker(m_modules_mutex);
489     collection::const_iterator pos, end = m_modules.end();
490     for (pos = m_modules.begin(); pos != end; ++pos)
491     {
492         (*pos)->FindGlobalVariables (name, NULL, append, max_matches, variable_list);
493     }
494     return variable_list.GetSize() - initial_size;
495 }
496
497
498 size_t
499 ModuleList::FindGlobalVariables (const RegularExpression& regex, 
500                                  bool append, 
501                                  size_t max_matches,
502                                  VariableList& variable_list) const
503 {
504     size_t initial_size = variable_list.GetSize();
505     Mutex::Locker locker(m_modules_mutex);
506     collection::const_iterator pos, end = m_modules.end();
507     for (pos = m_modules.begin(); pos != end; ++pos)
508     {
509         (*pos)->FindGlobalVariables (regex, append, max_matches, variable_list);
510     }
511     return variable_list.GetSize() - initial_size;
512 }
513
514
515 size_t
516 ModuleList::FindSymbolsWithNameAndType (const ConstString &name, 
517                                         SymbolType symbol_type, 
518                                         SymbolContextList &sc_list,
519                                         bool append) const
520 {
521     Mutex::Locker locker(m_modules_mutex);
522     if (!append)
523         sc_list.Clear();
524     size_t initial_size = sc_list.GetSize();
525     
526     collection::const_iterator pos, end = m_modules.end();
527     for (pos = m_modules.begin(); pos != end; ++pos)
528         (*pos)->FindSymbolsWithNameAndType (name, symbol_type, sc_list);
529     return sc_list.GetSize() - initial_size;
530 }
531
532 size_t
533 ModuleList::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, 
534                                              lldb::SymbolType symbol_type, 
535                                              SymbolContextList &sc_list,
536                                              bool append) const
537 {
538     Mutex::Locker locker(m_modules_mutex);
539     if (!append)
540         sc_list.Clear();
541     size_t initial_size = sc_list.GetSize();
542     
543     collection::const_iterator pos, end = m_modules.end();
544     for (pos = m_modules.begin(); pos != end; ++pos)
545         (*pos)->FindSymbolsMatchingRegExAndType (regex, symbol_type, sc_list);
546     return sc_list.GetSize() - initial_size;
547 }
548
549 size_t
550 ModuleList::FindModules (const ModuleSpec &module_spec, ModuleList& matching_module_list) const
551 {
552     size_t existing_matches = matching_module_list.GetSize();
553
554     Mutex::Locker locker(m_modules_mutex);
555     collection::const_iterator pos, end = m_modules.end();
556     for (pos = m_modules.begin(); pos != end; ++pos)
557     {
558         ModuleSP module_sp(*pos);
559         if (module_sp->MatchesModuleSpec (module_spec))
560             matching_module_list.Append(module_sp);
561     }
562     return matching_module_list.GetSize() - existing_matches;
563 }
564
565 ModuleSP
566 ModuleList::FindModule (const Module *module_ptr) const
567 {
568     ModuleSP module_sp;
569
570     // Scope for "locker"
571     {
572         Mutex::Locker locker(m_modules_mutex);
573         collection::const_iterator pos, end = m_modules.end();
574
575         for (pos = m_modules.begin(); pos != end; ++pos)
576         {
577             if ((*pos).get() == module_ptr)
578             {
579                 module_sp = (*pos);
580                 break;
581             }
582         }
583     }
584     return module_sp;
585
586 }
587
588 ModuleSP
589 ModuleList::FindModule (const UUID &uuid) const
590 {
591     ModuleSP module_sp;
592     
593     if (uuid.IsValid())
594     {
595         Mutex::Locker locker(m_modules_mutex);
596         collection::const_iterator pos, end = m_modules.end();
597         
598         for (pos = m_modules.begin(); pos != end; ++pos)
599         {
600             if ((*pos)->GetUUID() == uuid)
601             {
602                 module_sp = (*pos);
603                 break;
604             }
605         }
606     }
607     return module_sp;
608 }
609
610
611 size_t
612 ModuleList::FindTypes (const SymbolContext& sc, const ConstString &name, bool name_is_fully_qualified, size_t max_matches, TypeList& types) const
613 {
614     Mutex::Locker locker(m_modules_mutex);
615
616     size_t total_matches = 0;
617     collection::const_iterator pos, end = m_modules.end();
618     if (sc.module_sp)
619     {
620         // The symbol context "sc" contains a module so we want to search that
621         // one first if it is in our list...
622         for (pos = m_modules.begin(); pos != end; ++pos)
623         {
624             if (sc.module_sp.get() == (*pos).get())
625             {
626                 total_matches += (*pos)->FindTypes (sc, name, name_is_fully_qualified, max_matches, types);
627
628                 if (total_matches >= max_matches)
629                     break;
630             }
631         }
632     }
633     
634     if (total_matches < max_matches)
635     {
636         SymbolContext world_sc;
637         for (pos = m_modules.begin(); pos != end; ++pos)
638         {
639             // Search the module if the module is not equal to the one in the symbol
640             // context "sc". If "sc" contains a empty module shared pointer, then
641             // the comparisong will always be true (valid_module_ptr != NULL).
642             if (sc.module_sp.get() != (*pos).get())
643                 total_matches += (*pos)->FindTypes (world_sc, name, name_is_fully_qualified, max_matches, types);
644             
645             if (total_matches >= max_matches)
646                 break;
647         }
648     }
649     
650     return total_matches;
651 }
652
653 bool
654 ModuleList::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const
655 {
656     Mutex::Locker locker(m_modules_mutex);
657     collection::const_iterator pos, end = m_modules.end();
658     for (pos = m_modules.begin(); pos != end; ++pos)
659     {
660         if ((*pos)->FindSourceFile (orig_spec, new_spec))
661             return true;
662     }
663     return false;
664 }
665
666
667
668 ModuleSP
669 ModuleList::FindFirstModule (const ModuleSpec &module_spec) const
670 {
671     ModuleSP module_sp;
672     Mutex::Locker locker(m_modules_mutex);
673     collection::const_iterator pos, end = m_modules.end();
674     for (pos = m_modules.begin(); pos != end; ++pos)
675     {
676         ModuleSP module_sp(*pos);
677         if (module_sp->MatchesModuleSpec (module_spec))
678             return module_sp;
679     }
680     return module_sp;
681
682 }
683
684 size_t
685 ModuleList::GetSize() const
686 {
687     size_t size = 0;
688     {
689         Mutex::Locker locker(m_modules_mutex);
690         size = m_modules.size();
691     }
692     return size;
693 }
694
695
696 void
697 ModuleList::Dump(Stream *s) const
698 {
699 //  s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
700 //  s.Indent();
701 //  s << "ModuleList\n";
702
703     Mutex::Locker locker(m_modules_mutex);
704     collection::const_iterator pos, end = m_modules.end();
705     for (pos = m_modules.begin(); pos != end; ++pos)
706     {
707         (*pos)->Dump(s);
708     }
709 }
710
711 void
712 ModuleList::LogUUIDAndPaths (Log *log, const char *prefix_cstr)
713 {
714     if (log)
715     {   
716         Mutex::Locker locker(m_modules_mutex);
717         collection::const_iterator pos, begin = m_modules.begin(), end = m_modules.end();
718         for (pos = begin; pos != end; ++pos)
719         {
720             Module *module = pos->get();
721             const FileSpec &module_file_spec = module->GetFileSpec();
722             log->Printf ("%s[%u] %s (%s) \"%s\"",
723                          prefix_cstr ? prefix_cstr : "",
724                          (uint32_t)std::distance (begin, pos),
725                          module->GetUUID().GetAsString().c_str(),
726                          module->GetArchitecture().GetArchitectureName(),
727                          module_file_spec.GetPath().c_str());
728         }
729     }
730 }
731
732 bool
733 ModuleList::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr) const
734 {
735     Mutex::Locker locker(m_modules_mutex);
736     collection::const_iterator pos, end = m_modules.end();
737     for (pos = m_modules.begin(); pos != end; ++pos)
738     {
739         if ((*pos)->ResolveFileAddress (vm_addr, so_addr))
740             return true;
741     }
742
743     return false;
744 }
745
746 uint32_t
747 ModuleList::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) const
748 {
749     // The address is already section offset so it has a module
750     uint32_t resolved_flags = 0;
751     ModuleSP module_sp (so_addr.GetModule());
752     if (module_sp)
753     {
754         resolved_flags = module_sp->ResolveSymbolContextForAddress (so_addr,
755                                                                     resolve_scope,
756                                                                     sc);
757     }
758     else
759     {
760         Mutex::Locker locker(m_modules_mutex);
761         collection::const_iterator pos, end = m_modules.end();
762         for (pos = m_modules.begin(); pos != end; ++pos)
763         {
764             resolved_flags = (*pos)->ResolveSymbolContextForAddress (so_addr,
765                                                                      resolve_scope,
766                                                                      sc);
767             if (resolved_flags != 0)
768                 break;
769         }
770     }
771
772     return resolved_flags;
773 }
774
775 uint32_t
776 ModuleList::ResolveSymbolContextForFilePath 
777 (
778     const char *file_path, 
779     uint32_t line, 
780     bool check_inlines, 
781     uint32_t resolve_scope, 
782     SymbolContextList& sc_list
783 )  const
784 {
785     FileSpec file_spec(file_path, false);
786     return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
787 }
788
789 uint32_t
790 ModuleList::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) const
791 {
792     Mutex::Locker locker(m_modules_mutex);
793     collection::const_iterator pos, end = m_modules.end();
794     for (pos = m_modules.begin(); pos != end; ++pos)
795     {
796         (*pos)->ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
797     }
798
799     return sc_list.GetSize();
800 }
801
802 size_t
803 ModuleList::GetIndexForModule (const Module *module) const
804 {
805     if (module)
806     {
807         Mutex::Locker locker(m_modules_mutex);
808         collection::const_iterator pos;
809         collection::const_iterator begin = m_modules.begin();
810         collection::const_iterator end = m_modules.end();
811         for (pos = begin; pos != end; ++pos)
812         {
813             if ((*pos).get() == module)
814                 return std::distance (begin, pos);
815         }
816     }
817     return LLDB_INVALID_INDEX32;
818 }
819
820 static ModuleList &
821 GetSharedModuleList ()
822 {
823     // NOTE: Intentionally leak the module list so a program doesn't have to
824     // cleanup all modules and object files as it exits. This just wastes time
825     // doing a bunch of cleanup that isn't required.
826     static ModuleList *g_shared_module_list = NULL;
827     if (g_shared_module_list == NULL)
828         g_shared_module_list = new ModuleList(); // <--- Intentional leak!!!
829     
830     return *g_shared_module_list;
831 }
832
833 bool
834 ModuleList::ModuleIsInCache (const Module *module_ptr)
835 {
836     if (module_ptr)
837     {
838         ModuleList &shared_module_list = GetSharedModuleList ();
839         return shared_module_list.FindModule (module_ptr).get() != NULL;
840     }
841     return false;
842 }
843
844 size_t
845 ModuleList::FindSharedModules (const ModuleSpec &module_spec, ModuleList &matching_module_list)
846 {
847     return GetSharedModuleList ().FindModules (module_spec, matching_module_list);
848 }
849
850 size_t
851 ModuleList::RemoveOrphanSharedModules (bool mandatory)
852 {
853     return GetSharedModuleList ().RemoveOrphans(mandatory);
854 }
855
856 Error
857 ModuleList::GetSharedModule
858 (
859     const ModuleSpec &module_spec,
860     ModuleSP &module_sp,
861     const FileSpecList *module_search_paths_ptr,
862     ModuleSP *old_module_sp_ptr,
863     bool *did_create_ptr,
864     bool always_create
865 )
866 {
867     ModuleList &shared_module_list = GetSharedModuleList ();
868     Mutex::Locker locker(shared_module_list.m_modules_mutex);
869     char path[PATH_MAX];
870
871     Error error;
872
873     module_sp.reset();
874
875     if (did_create_ptr)
876         *did_create_ptr = false;
877     if (old_module_sp_ptr)
878         old_module_sp_ptr->reset();
879
880     const UUID *uuid_ptr = module_spec.GetUUIDPtr();
881     const FileSpec &module_file_spec = module_spec.GetFileSpec();
882     const ArchSpec &arch = module_spec.GetArchitecture();
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     if (always_create == false)
888     {
889         ModuleList matching_module_list;
890         const size_t num_matching_modules = shared_module_list.FindModules (module_spec, matching_module_list);
891         if (num_matching_modules > 0)
892         {
893             for (size_t module_idx = 0; module_idx < num_matching_modules; ++module_idx)
894             {
895                 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
896                 
897                 // Make sure the file for the module hasn't been modified
898                 if (module_sp->FileHasChanged())
899                 {
900                     if (old_module_sp_ptr && !old_module_sp_ptr->get())
901                         *old_module_sp_ptr = module_sp;
902
903                     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_MODULES));
904                     if (log)
905                         log->Printf("module changed: %p, removing from global module list", module_sp.get());
906
907                     shared_module_list.Remove (module_sp);
908                     module_sp.reset();
909                 }
910                 else
911                 {
912                     // The module matches and the module was not modified from
913                     // when it was last loaded.
914                     return error;
915                 }
916             }
917         }
918     }
919
920     if (module_sp)
921         return error;
922     else
923     {
924         module_sp.reset (new Module (module_spec));
925         // Make sure there are a module and an object file since we can specify
926         // a valid file path with an architecture that might not be in that file.
927         // By getting the object file we can guarantee that the architecture matches
928         if (module_sp)
929         {
930             if (module_sp->GetObjectFile())
931             {
932                 // If we get in here we got the correct arch, now we just need
933                 // to verify the UUID if one was given
934                 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID())
935                     module_sp.reset();
936                 else
937                 {
938                     if (did_create_ptr)
939                         *did_create_ptr = true;
940                     
941                     shared_module_list.ReplaceEquivalent(module_sp);
942                     return error;
943                 }
944             }
945             else
946                 module_sp.reset();
947         }
948     }
949
950     // Either the file didn't exist where at the path, or no path was given, so
951     // we now have to use more extreme measures to try and find the appropriate
952     // module.
953
954     // Fixup the incoming path in case the path points to a valid file, yet
955     // the arch or UUID (if one was passed in) don't match.
956     FileSpec file_spec = Symbols::LocateExecutableObjectFile (module_spec);
957
958     // Don't look for the file if it appears to be the same one we already
959     // checked for above...
960     if (file_spec != module_file_spec)
961     {
962         if (!file_spec.Exists())
963         {
964             file_spec.GetPath(path, sizeof(path));
965             if (path[0] == '\0')
966                 module_file_spec.GetPath(path, sizeof(path));
967             if (file_spec.Exists())
968             {
969                 std::string uuid_str;
970                 if (uuid_ptr && uuid_ptr->IsValid())
971                     uuid_str = uuid_ptr->GetAsString();
972
973                 if (arch.IsValid())
974                 {
975                     if (!uuid_str.empty())
976                         error.SetErrorStringWithFormat("'%s' does not contain the %s architecture and UUID %s", path, arch.GetArchitectureName(), uuid_str.c_str());
977                     else
978                         error.SetErrorStringWithFormat("'%s' does not contain the %s architecture.", path, arch.GetArchitectureName());
979                 }
980             }
981             else
982             {
983                 error.SetErrorStringWithFormat("'%s' does not exist", path);
984             }
985             if (error.Fail())
986                 module_sp.reset();
987             return error;
988         }
989
990
991         // Make sure no one else can try and get or create a module while this
992         // function is actively working on it by doing an extra lock on the
993         // global mutex list.
994         ModuleSpec platform_module_spec(module_spec);
995         platform_module_spec.GetFileSpec() = file_spec;
996         platform_module_spec.GetPlatformFileSpec() = file_spec;
997         ModuleList matching_module_list;
998         if (shared_module_list.FindModules (platform_module_spec, matching_module_list) > 0)
999         {
1000             module_sp = matching_module_list.GetModuleAtIndex(0);
1001
1002             // If we didn't have a UUID in mind when looking for the object file,
1003             // then we should make sure the modification time hasn't changed!
1004             if (platform_module_spec.GetUUIDPtr() == NULL)
1005             {
1006                 TimeValue file_spec_mod_time(file_spec.GetModificationTime());
1007                 if (file_spec_mod_time.IsValid())
1008                 {
1009                     if (file_spec_mod_time != module_sp->GetModificationTime())
1010                     {
1011                         if (old_module_sp_ptr)
1012                             *old_module_sp_ptr = module_sp;
1013                         shared_module_list.Remove (module_sp);
1014                         module_sp.reset();
1015                     }
1016                 }
1017             }
1018         }
1019
1020         if (module_sp.get() == NULL)
1021         {
1022             module_sp.reset (new Module (platform_module_spec));
1023             // Make sure there are a module and an object file since we can specify
1024             // a valid file path with an architecture that might not be in that file.
1025             // By getting the object file we can guarantee that the architecture matches
1026             if (module_sp && module_sp->GetObjectFile())
1027             {
1028                 if (did_create_ptr)
1029                     *did_create_ptr = true;
1030
1031                 shared_module_list.ReplaceEquivalent(module_sp);
1032             }
1033             else
1034             {
1035                 file_spec.GetPath(path, sizeof(path));
1036
1037                 if (file_spec)
1038                 {
1039                     if (arch.IsValid())
1040                         error.SetErrorStringWithFormat("unable to open %s architecture in '%s'", arch.GetArchitectureName(), path);
1041                     else
1042                         error.SetErrorStringWithFormat("unable to open '%s'", path);
1043                 }
1044                 else
1045                 {
1046                     std::string uuid_str;
1047                     if (uuid_ptr && uuid_ptr->IsValid())
1048                         uuid_str = uuid_ptr->GetAsString();
1049
1050                     if (!uuid_str.empty())
1051                         error.SetErrorStringWithFormat("cannot locate a module for UUID '%s'", uuid_str.c_str());
1052                     else
1053                         error.SetErrorStringWithFormat("cannot locate a module");
1054                 }
1055             }
1056         }
1057     }
1058
1059     return error;
1060 }
1061
1062 bool
1063 ModuleList::RemoveSharedModule (lldb::ModuleSP &module_sp)
1064 {
1065     return GetSharedModuleList ().Remove (module_sp);
1066 }
1067
1068 bool
1069 ModuleList::RemoveSharedModuleIfOrphaned (const Module *module_ptr)
1070 {
1071     return GetSharedModuleList ().RemoveIfOrphaned (module_ptr);
1072 }
1073
1074 bool
1075 ModuleList::LoadScriptingResourcesInTarget (Target *target,
1076                                             std::list<Error>& errors,
1077                                             Stream *feedback_stream,
1078                                             bool continue_on_error)
1079 {
1080     if (!target)
1081         return false;
1082     Mutex::Locker locker(m_modules_mutex);
1083     for (auto module : m_modules)
1084     {
1085         Error error;
1086         if (module)
1087         {
1088             if (!module->LoadScriptingResourceInTarget(target, error, feedback_stream))
1089             {
1090                 if (error.Fail() && error.AsCString())
1091                 {
1092                     error.SetErrorStringWithFormat("unable to load scripting data for module %s - error reported was %s",
1093                                                    module->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1094                                                    error.AsCString());
1095                     errors.push_back(error);
1096                 }
1097                 if (!continue_on_error)
1098                     return false;
1099             }
1100         }
1101     }
1102     return errors.size() == 0;
1103 }