blob: f2f392046880d0b14cd01ea81d2297066db802d4 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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
Greg Clayton427f2902010-12-14 02:59:59 +000016#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Module.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000018#include "lldb/Core/ModuleSpec.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000019#include "lldb/Host/Host.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Host/Symbols.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000021#include "lldb/Symbol/ClangNamespaceDecl.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Symbol/VariableList.h"
24
25using namespace lldb;
26using namespace lldb_private;
27
28//----------------------------------------------------------------------
29// ModuleList constructor
30//----------------------------------------------------------------------
31ModuleList::ModuleList() :
32 m_modules(),
Enrico Granata146d9522012-11-08 02:22:02 +000033 m_modules_mutex (Mutex::eMutexTypeRecursive),
34 m_notifier(NULL)
Chris Lattner24943d22010-06-08 16:52:24 +000035{
36}
37
38//----------------------------------------------------------------------
39// Copy constructor
40//----------------------------------------------------------------------
41ModuleList::ModuleList(const ModuleList& rhs) :
Greg Claytone8fc0362012-06-27 19:59:26 +000042 m_modules(),
43 m_modules_mutex (Mutex::eMutexTypeRecursive)
Chris Lattner24943d22010-06-08 16:52:24 +000044{
Greg Claytone8fc0362012-06-27 19:59:26 +000045 Mutex::Locker lhs_locker(m_modules_mutex);
46 Mutex::Locker rhs_locker(rhs.m_modules_mutex);
47 m_modules = rhs.m_modules;
Enrico Granata146d9522012-11-08 02:22:02 +000048}
49
50ModuleList::ModuleList (ModuleList::Notifier* notifier) :
51 m_modules(),
52 m_modules_mutex (Mutex::eMutexTypeRecursive),
53 m_notifier(notifier)
54{
Chris Lattner24943d22010-06-08 16:52:24 +000055}
56
57//----------------------------------------------------------------------
58// Assignment operator
59//----------------------------------------------------------------------
60const ModuleList&
61ModuleList::operator= (const ModuleList& rhs)
62{
63 if (this != &rhs)
64 {
Greg Claytone8fc0362012-06-27 19:59:26 +000065 Mutex::Locker lhs_locker(m_modules_mutex);
66 Mutex::Locker rhs_locker(rhs.m_modules_mutex);
Chris Lattner24943d22010-06-08 16:52:24 +000067 m_modules = rhs.m_modules;
68 }
69 return *this;
70}
71
72//----------------------------------------------------------------------
73// Destructor
74//----------------------------------------------------------------------
75ModuleList::~ModuleList()
76{
77}
78
79void
Enrico Granata146d9522012-11-08 02:22:02 +000080ModuleList::AppendImpl (const ModuleSP &module_sp, bool use_notifier)
Chris Lattner24943d22010-06-08 16:52:24 +000081{
Greg Claytonb72d0f02011-04-12 05:54:46 +000082 if (module_sp)
83 {
84 Mutex::Locker locker(m_modules_mutex);
85 m_modules.push_back(module_sp);
Enrico Granata146d9522012-11-08 02:22:02 +000086 if (use_notifier && m_notifier)
Enrico Granata0b2f5cc2012-11-08 19:16:03 +000087 m_notifier->ModuleAdded(*this, module_sp);
Greg Claytonb72d0f02011-04-12 05:54:46 +000088 }
Chris Lattner24943d22010-06-08 16:52:24 +000089}
90
Greg Claytonbcaf99a2012-07-12 20:32:19 +000091void
Enrico Granata146d9522012-11-08 02:22:02 +000092ModuleList::Append (const ModuleSP &module_sp)
93{
94 AppendImpl (module_sp);
95}
96
97void
Greg Claytonbcaf99a2012-07-12 20:32:19 +000098ModuleList::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))
Enrico Granata146d9522012-11-08 02:22:02 +0000114 RemoveImpl(m_modules.begin() + idx);
Greg Claytonbcaf99a2012-07-12 20:32:19 +0000115 else
116 ++idx;
117 }
118 // Now add the new module to the list
Enrico Granata146d9522012-11-08 02:22:02 +0000119 Append(module_sp);
Greg Claytonbcaf99a2012-07-12 20:32:19 +0000120 }
121}
122
Chris Lattner24943d22010-06-08 16:52:24 +0000123bool
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000124ModuleList::AppendIfNeeded (const ModuleSP &module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000125{
Greg Claytonb72d0f02011-04-12 05:54:46 +0000126 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000127 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000128 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.
Enrico Granata146d9522012-11-08 02:22:02 +0000136 Append(module_sp);
Greg Claytonb72d0f02011-04-12 05:54:46 +0000137 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000138 }
Greg Claytonb72d0f02011-04-12 05:54:46 +0000139 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000140}
141
Enrico Granata146d9522012-11-08 02:22:02 +0000142void
143ModuleList::Append (const ModuleList& module_list)
144{
145 for (auto pos : module_list.m_modules)
146 Append(pos);
147}
148
Chris Lattner24943d22010-06-08 16:52:24 +0000149bool
Enrico Granata146d9522012-11-08 02:22:02 +0000150ModuleList::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
158bool
159ModuleList::RemoveImpl (const ModuleSP &module_sp, bool use_notifier)
Chris Lattner24943d22010-06-08 16:52:24 +0000160{
Greg Claytonb72d0f02011-04-12 05:54:46 +0000161 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000162 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000163 Mutex::Locker locker(m_modules_mutex);
164 collection::iterator pos, end = m_modules.end();
165 for (pos = m_modules.begin(); pos != end; ++pos)
Chris Lattner24943d22010-06-08 16:52:24 +0000166 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000167 if (pos->get() == module_sp.get())
168 {
169 m_modules.erase (pos);
Enrico Granata146d9522012-11-08 02:22:02 +0000170 if (use_notifier && m_notifier)
Enrico Granata0b2f5cc2012-11-08 19:16:03 +0000171 m_notifier->ModuleRemoved(*this, module_sp);
Greg Claytonb72d0f02011-04-12 05:54:46 +0000172 return true;
173 }
Chris Lattner24943d22010-06-08 16:52:24 +0000174 }
175 }
176 return false;
177}
178
Enrico Granata146d9522012-11-08 02:22:02 +0000179ModuleList::collection::iterator
180ModuleList::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)
Enrico Granata0b2f5cc2012-11-08 19:16:03 +0000185 m_notifier->ModuleRemoved(*this, module_sp);
Enrico Granata146d9522012-11-08 02:22:02 +0000186 return retval;
187}
188
189bool
190ModuleList::Remove (const ModuleSP &module_sp)
191{
192 return RemoveImpl (module_sp);
193}
194
195bool
196ModuleList::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)
Enrico Granata0b2f5cc2012-11-08 19:16:03 +0000202 m_notifier->ModuleUpdated(*this, old_module_sp,new_module_sp);
Enrico Granata146d9522012-11-08 02:22:02 +0000203 return true;
204}
205
Jim Ingham03e5e512012-05-17 18:38:42 +0000206bool
207ModuleList::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 {
Enrico Granata146d9522012-11-08 02:22:02 +0000219 pos = RemoveImpl(pos);
Jim Ingham03e5e512012-05-17 18:38:42 +0000220 return true;
221 }
222 else
223 return false;
224 }
225 }
226 }
227 return false;
228}
Greg Clayton5beb99d2011-08-11 02:48:45 +0000229
230size_t
Greg Clayton860b9ea2012-04-09 20:22:01 +0000231ModuleList::RemoveOrphans (bool mandatory)
Greg Clayton5beb99d2011-08-11 02:48:45 +0000232{
Greg Clayton860b9ea2012-04-09 20:22:01 +0000233 Mutex::Locker locker;
234
235 if (mandatory)
236 {
Jim Ingham1b584eb2012-05-04 23:02:50 +0000237 locker.Lock (m_modules_mutex);
Greg Clayton860b9ea2012-04-09 20:22:01 +0000238 }
239 else
240 {
241 // Not mandatory, remove orphans if we can get the mutex
Jim Ingham1b584eb2012-05-04 23:02:50 +0000242 if (!locker.TryLock(m_modules_mutex))
Greg Clayton860b9ea2012-04-09 20:22:01 +0000243 return 0;
244 }
Greg Claytonb01760c2011-08-11 04:30:39 +0000245 collection::iterator pos = m_modules.begin();
Greg Clayton5beb99d2011-08-11 02:48:45 +0000246 size_t remove_count = 0;
Greg Claytonb01760c2011-08-11 04:30:39 +0000247 while (pos != m_modules.end())
Greg Clayton5beb99d2011-08-11 02:48:45 +0000248 {
249 if (pos->unique())
250 {
Enrico Granata146d9522012-11-08 02:22:02 +0000251 pos = RemoveImpl(pos);
Greg Clayton5beb99d2011-08-11 02:48:45 +0000252 ++remove_count;
253 }
254 else
255 {
256 ++pos;
257 }
258 }
259 return remove_count;
260}
261
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000262size_t
263ModuleList::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
Chris Lattner24943d22010-06-08 16:52:24 +0000276
Chris Lattner24943d22010-06-08 16:52:24 +0000277void
278ModuleList::Clear()
279{
Enrico Granata146d9522012-11-08 02:22:02 +0000280 ClearImpl();
Chris Lattner24943d22010-06-08 16:52:24 +0000281}
282
Greg Clayton08205a62012-01-27 18:45:39 +0000283void
284ModuleList::Destroy()
285{
Enrico Granata146d9522012-11-08 02:22:02 +0000286 ClearImpl();
287}
288
289void
290ModuleList::ClearImpl (bool use_notifier)
291{
Greg Clayton08205a62012-01-27 18:45:39 +0000292 Mutex::Locker locker(m_modules_mutex);
Enrico Granata146d9522012-11-08 02:22:02 +0000293 if (use_notifier && m_notifier)
Enrico Granata0b2f5cc2012-11-08 19:16:03 +0000294 m_notifier->WillClearList(*this);
Enrico Granata146d9522012-11-08 02:22:02 +0000295 m_modules.clear();
Greg Clayton08205a62012-01-27 18:45:39 +0000296}
297
Chris Lattner24943d22010-06-08 16:52:24 +0000298Module*
299ModuleList::GetModulePointerAtIndex (uint32_t idx) const
300{
301 Mutex::Locker locker(m_modules_mutex);
Jim Ingham93367902012-05-30 02:19:25 +0000302 return GetModulePointerAtIndexUnlocked(idx);
303}
304
305Module*
306ModuleList::GetModulePointerAtIndexUnlocked (uint32_t idx) const
307{
Chris Lattner24943d22010-06-08 16:52:24 +0000308 if (idx < m_modules.size())
309 return m_modules[idx].get();
310 return NULL;
311}
312
313ModuleSP
Enrico Granata146d9522012-11-08 02:22:02 +0000314ModuleList::GetModuleAtIndex(uint32_t idx) const
Chris Lattner24943d22010-06-08 16:52:24 +0000315{
316 Mutex::Locker locker(m_modules_mutex);
Jim Ingham93367902012-05-30 02:19:25 +0000317 return GetModuleAtIndexUnlocked(idx);
318}
319
320ModuleSP
Enrico Granata146d9522012-11-08 02:22:02 +0000321ModuleList::GetModuleAtIndexUnlocked(uint32_t idx) const
Jim Ingham93367902012-05-30 02:19:25 +0000322{
Chris Lattner24943d22010-06-08 16:52:24 +0000323 ModuleSP module_sp;
324 if (idx < m_modules.size())
325 module_sp = m_modules[idx];
326 return module_sp;
327}
328
Greg Clayton801417e2011-07-07 01:59:51 +0000329uint32_t
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000330ModuleList::FindFunctions (const ConstString &name,
331 uint32_t name_type_mask,
332 bool include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +0000333 bool include_inlines,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000334 bool append,
Enrico Granata146d9522012-11-08 02:22:02 +0000335 SymbolContextList &sc_list) const
Chris Lattner24943d22010-06-08 16:52:24 +0000336{
Sean Callanan0fc73582010-07-27 00:55:47 +0000337 if (!append)
338 sc_list.Clear();
339
Chris Lattner24943d22010-06-08 16:52:24 +0000340 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 {
Sean Callanan302d78c2012-02-10 22:52:19 +0000344 (*pos)->FindFunctions (name, NULL, name_type_mask, include_symbols, include_inlines, true, sc_list);
Chris Lattner24943d22010-06-08 16:52:24 +0000345 }
Sean Callanan0fc73582010-07-27 00:55:47 +0000346
Chris Lattner24943d22010-06-08 16:52:24 +0000347 return sc_list.GetSize();
348}
349
350uint32_t
Greg Clayton801417e2011-07-07 01:59:51 +0000351ModuleList::FindCompileUnits (const FileSpec &path,
352 bool append,
Enrico Granata146d9522012-11-08 02:22:02 +0000353 SymbolContextList &sc_list) const
Greg Clayton801417e2011-07-07 01:59:51 +0000354{
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
368uint32_t
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000369ModuleList::FindGlobalVariables (const ConstString &name,
370 bool append,
371 uint32_t max_matches,
Enrico Granata146d9522012-11-08 02:22:02 +0000372 VariableList& variable_list) const
Chris Lattner24943d22010-06-08 16:52:24 +0000373{
374 size_t initial_size = variable_list.GetSize();
375 Mutex::Locker locker(m_modules_mutex);
Enrico Granata146d9522012-11-08 02:22:02 +0000376 collection::const_iterator pos, end = m_modules.end();
Chris Lattner24943d22010-06-08 16:52:24 +0000377 for (pos = m_modules.begin(); pos != end; ++pos)
378 {
Sean Callanan3e80cd92011-10-12 02:08:07 +0000379 (*pos)->FindGlobalVariables (name, NULL, append, max_matches, variable_list);
Chris Lattner24943d22010-06-08 16:52:24 +0000380 }
381 return variable_list.GetSize() - initial_size;
382}
383
384
385uint32_t
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000386ModuleList::FindGlobalVariables (const RegularExpression& regex,
387 bool append,
388 uint32_t max_matches,
Enrico Granata146d9522012-11-08 02:22:02 +0000389 VariableList& variable_list) const
Chris Lattner24943d22010-06-08 16:52:24 +0000390{
391 size_t initial_size = variable_list.GetSize();
392 Mutex::Locker locker(m_modules_mutex);
Enrico Granata146d9522012-11-08 02:22:02 +0000393 collection::const_iterator pos, end = m_modules.end();
Chris Lattner24943d22010-06-08 16:52:24 +0000394 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
402size_t
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000403ModuleList::FindSymbolsWithNameAndType (const ConstString &name,
404 SymbolType symbol_type,
Jim Ingham323ce422011-11-19 00:19:25 +0000405 SymbolContextList &sc_list,
Enrico Granata146d9522012-11-08 02:22:02 +0000406 bool append) const
Chris Lattner24943d22010-06-08 16:52:24 +0000407{
408 Mutex::Locker locker(m_modules_mutex);
Jim Ingham323ce422011-11-19 00:19:25 +0000409 if (!append)
410 sc_list.Clear();
411 size_t initial_size = sc_list.GetSize();
412
Enrico Granata146d9522012-11-08 02:22:02 +0000413 collection::const_iterator pos, end = m_modules.end();
Chris Lattner24943d22010-06-08 16:52:24 +0000414 for (pos = m_modules.begin(); pos != end; ++pos)
Sean Callananaa4a5532011-10-13 16:49:47 +0000415 (*pos)->FindSymbolsWithNameAndType (name, symbol_type, sc_list);
Jim Ingham323ce422011-11-19 00:19:25 +0000416 return sc_list.GetSize() - initial_size;
417}
418
Greg Claytonbcaf99a2012-07-12 20:32:19 +0000419size_t
Jim Ingham323ce422011-11-19 00:19:25 +0000420ModuleList::FindSymbolsMatchingRegExAndType (const RegularExpression &regex,
421 lldb::SymbolType symbol_type,
422 SymbolContextList &sc_list,
Enrico Granata146d9522012-11-08 02:22:02 +0000423 bool append) const
Jim Ingham323ce422011-11-19 00:19:25 +0000424{
425 Mutex::Locker locker(m_modules_mutex);
426 if (!append)
427 sc_list.Clear();
428 size_t initial_size = sc_list.GetSize();
429
Enrico Granata146d9522012-11-08 02:22:02 +0000430 collection::const_iterator pos, end = m_modules.end();
Jim Ingham323ce422011-11-19 00:19:25 +0000431 for (pos = m_modules.begin(); pos != end; ++pos)
432 (*pos)->FindSymbolsMatchingRegExAndType (regex, symbol_type, sc_list);
433 return sc_list.GetSize() - initial_size;
Chris Lattner24943d22010-06-08 16:52:24 +0000434}
435
Chris Lattner24943d22010-06-08 16:52:24 +0000436size_t
Greg Clayton444fe992012-02-26 05:51:37 +0000437ModuleList::FindModules (const ModuleSpec &module_spec, ModuleList& matching_module_list) const
Chris Lattner24943d22010-06-08 16:52:24 +0000438{
439 size_t existing_matches = matching_module_list.GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000440
441 Mutex::Locker locker(m_modules_mutex);
Greg Clayton444fe992012-02-26 05:51:37 +0000442 collection::const_iterator pos, end = m_modules.end();
443 for (pos = m_modules.begin(); pos != end; ++pos)
Chris Lattner24943d22010-06-08 16:52:24 +0000444 {
445 ModuleSP module_sp(*pos);
Greg Clayton444fe992012-02-26 05:51:37 +0000446 if (module_sp->MatchesModuleSpec (module_spec))
447 matching_module_list.Append(module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000448 }
449 return matching_module_list.GetSize() - existing_matches;
450}
451
452ModuleSP
Enrico Granata146d9522012-11-08 02:22:02 +0000453ModuleList::FindModule (const Module *module_ptr) const
Chris Lattner24943d22010-06-08 16:52:24 +0000454{
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
Greg Clayton24bc5d92011-03-30 18:16:51 +0000475ModuleSP
Enrico Granata146d9522012-11-08 02:22:02 +0000476ModuleList::FindModule (const UUID &uuid) const
Greg Clayton24bc5d92011-03-30 18:16:51 +0000477{
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
Chris Lattner24943d22010-06-08 16:52:24 +0000497
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000498uint32_t
Enrico Granata146d9522012-11-08 02:22:02 +0000499ModuleList::FindTypes (const SymbolContext& sc, const ConstString &name, bool name_is_fully_qualified, uint32_t max_matches, TypeList& types) const
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000500{
501 Mutex::Locker locker(m_modules_mutex);
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000502
503 uint32_t total_matches = 0;
504 collection::const_iterator pos, end = m_modules.end();
Greg Clayton72c17bf2012-04-06 18:09:43 +0000505 if (sc.module_sp)
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000506 {
Greg Clayton72c17bf2012-04-06 18:09:43 +0000507 // 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);
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000514
Greg Clayton72c17bf2012-04-06 18:09:43 +0000515 if (total_matches >= max_matches)
516 break;
517 }
518 }
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000519 }
Greg Clayton72c17bf2012-04-06 18:09:43 +0000520
521 if (total_matches < max_matches)
522 {
Enrico Granatad9c37692012-10-03 21:31:35 +0000523 SymbolContext world_sc;
Greg Clayton72c17bf2012-04-06 18:09:43 +0000524 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())
Enrico Granatad9c37692012-10-03 21:31:35 +0000530 total_matches += (*pos)->FindTypes (world_sc, name, name_is_fully_qualified, max_matches, types);
Greg Clayton72c17bf2012-04-06 18:09:43 +0000531
532 if (total_matches >= max_matches)
533 break;
534 }
535 }
536
Greg Clayton7aff9ff2010-08-03 01:26:16 +0000537 return total_matches;
538}
539
Greg Clayton964deba2012-03-15 21:01:31 +0000540bool
541ModuleList::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
Chris Lattner24943d22010-06-08 16:52:24 +0000555ModuleSP
Enrico Granata146d9522012-11-08 02:22:02 +0000556ModuleList::FindFirstModule (const ModuleSpec &module_spec) const
Chris Lattner24943d22010-06-08 16:52:24 +0000557{
558 ModuleSP module_sp;
Greg Clayton444fe992012-02-26 05:51:37 +0000559 Mutex::Locker locker(m_modules_mutex);
560 collection::const_iterator pos, end = m_modules.end();
561 for (pos = m_modules.begin(); pos != end; ++pos)
Chris Lattner24943d22010-06-08 16:52:24 +0000562 {
Greg Clayton444fe992012-02-26 05:51:37 +0000563 ModuleSP module_sp(*pos);
564 if (module_sp->MatchesModuleSpec (module_spec))
565 return module_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000566 }
567 return module_sp;
568
569}
570
Chris Lattner24943d22010-06-08 16:52:24 +0000571size_t
572ModuleList::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
583void
584ModuleList::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
Greg Clayton427f2902010-12-14 02:59:59 +0000598void
599ModuleList::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,
Greg Clayton940b1032011-02-23 00:35:02 +0000615 module->GetArchitecture().GetArchitectureName(),
Greg Clayton427f2902010-12-14 02:59:59 +0000616 module_file_spec.GetDirectory().GetCString(),
617 module_file_spec.GetFilename().GetCString());
618 }
619 }
620}
Chris Lattner24943d22010-06-08 16:52:24 +0000621
622bool
Enrico Granata146d9522012-11-08 02:22:02 +0000623ModuleList::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr) const
Chris Lattner24943d22010-06-08 16:52:24 +0000624{
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
636uint32_t
Enrico Granata146d9522012-11-08 02:22:02 +0000637ModuleList::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) const
Chris Lattner24943d22010-06-08 16:52:24 +0000638{
639 // The address is already section offset so it has a module
640 uint32_t resolved_flags = 0;
Greg Clayton3508c382012-02-24 01:59:29 +0000641 ModuleSP module_sp (so_addr.GetModule());
642 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000643 {
Greg Clayton3508c382012-02-24 01:59:29 +0000644 resolved_flags = module_sp->ResolveSymbolContextForAddress (so_addr,
645 resolve_scope,
646 sc);
Chris Lattner24943d22010-06-08 16:52:24 +0000647 }
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
665uint32_t
Greg Clayton537a7a82010-10-20 20:54:39 +0000666ModuleList::ResolveSymbolContextForFilePath
667(
668 const char *file_path,
669 uint32_t line,
670 bool check_inlines,
671 uint32_t resolve_scope,
672 SymbolContextList& sc_list
Enrico Granata146d9522012-11-08 02:22:02 +0000673) const
Chris Lattner24943d22010-06-08 16:52:24 +0000674{
Greg Clayton537a7a82010-10-20 20:54:39 +0000675 FileSpec file_spec(file_path, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000676 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
677}
678
679uint32_t
Enrico Granata146d9522012-11-08 02:22:02 +0000680ModuleList::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) const
Chris Lattner24943d22010-06-08 16:52:24 +0000681{
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
692uint32_t
693ModuleList::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
710static ModuleList &
711GetSharedModuleList ()
712{
Greg Claytonaf059722012-09-18 23:50:22 +0000713 // 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;
Chris Lattner24943d22010-06-08 16:52:24 +0000721}
722
Greg Clayton08205a62012-01-27 18:45:39 +0000723bool
724ModuleList::ModuleIsInCache (const Module *module_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000725{
Chris Lattner24943d22010-06-08 16:52:24 +0000726 if (module_ptr)
727 {
728 ModuleList &shared_module_list = GetSharedModuleList ();
Greg Clayton08205a62012-01-27 18:45:39 +0000729 return shared_module_list.FindModule (module_ptr).get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000730 }
Greg Clayton08205a62012-01-27 18:45:39 +0000731 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000732}
733
Greg Clayton661825b2010-06-28 23:51:11 +0000734size_t
Greg Clayton444fe992012-02-26 05:51:37 +0000735ModuleList::FindSharedModules (const ModuleSpec &module_spec, ModuleList &matching_module_list)
Greg Clayton661825b2010-06-28 23:51:11 +0000736{
Greg Clayton444fe992012-02-26 05:51:37 +0000737 return GetSharedModuleList ().FindModules (module_spec, matching_module_list);
Greg Clayton661825b2010-06-28 23:51:11 +0000738}
Chris Lattner24943d22010-06-08 16:52:24 +0000739
Greg Clayton5beb99d2011-08-11 02:48:45 +0000740uint32_t
Greg Clayton860b9ea2012-04-09 20:22:01 +0000741ModuleList::RemoveOrphanSharedModules (bool mandatory)
Greg Clayton5beb99d2011-08-11 02:48:45 +0000742{
Greg Clayton860b9ea2012-04-09 20:22:01 +0000743 return GetSharedModuleList ().RemoveOrphans(mandatory);
Greg Clayton5beb99d2011-08-11 02:48:45 +0000744}
745
Chris Lattner24943d22010-06-08 16:52:24 +0000746Error
747ModuleList::GetSharedModule
748(
Greg Clayton444fe992012-02-26 05:51:37 +0000749 const ModuleSpec &module_spec,
Chris Lattner24943d22010-06-08 16:52:24 +0000750 ModuleSP &module_sp,
Greg Clayton9ce95382012-02-13 23:10:39 +0000751 const FileSpecList *module_search_paths_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000752 ModuleSP *old_module_sp_ptr,
Greg Claytondd506e12011-03-15 03:56:33 +0000753 bool *did_create_ptr,
754 bool always_create
Chris Lattner24943d22010-06-08 16:52:24 +0000755)
756{
757 ModuleList &shared_module_list = GetSharedModuleList ();
Greg Claytondd506e12011-03-15 03:56:33 +0000758 Mutex::Locker locker(shared_module_list.m_modules_mutex);
Chris Lattner24943d22010-06-08 16:52:24 +0000759 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
Greg Clayton444fe992012-02-26 05:51:37 +0000771 const UUID *uuid_ptr = module_spec.GetUUIDPtr();
772 const FileSpec &module_file_spec = module_spec.GetFileSpec();
773 const ArchSpec &arch = module_spec.GetArchitecture();
Chris Lattner24943d22010-06-08 16:52:24 +0000774
Greg Clayton444fe992012-02-26 05:51:37 +0000775 // 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)
Chris Lattner24943d22010-06-08 16:52:24 +0000779 {
Greg Clayton444fe992012-02-26 05:51:37 +0000780 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)
Chris Lattner24943d22010-06-08 16:52:24 +0000783 {
Greg Clayton444fe992012-02-26 05:51:37 +0000784 for (uint32_t module_idx = 0; module_idx < num_matching_modules; ++module_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000785 {
Greg Clayton444fe992012-02-26 05:51:37 +0000786 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
Greg Claytoncbacba12012-07-12 22:51:12 +0000787
788 // Make sure the file for the module hasn't been modified
789 if (module_sp->FileHasChanged())
Chris Lattner24943d22010-06-08 16:52:24 +0000790 {
Greg Claytoncbacba12012-07-12 22:51:12 +0000791 if (old_module_sp_ptr && !old_module_sp_ptr->get())
792 *old_module_sp_ptr = module_sp;
Greg Claytonf737d372012-10-08 22:41:53 +0000793
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
Greg Claytoncbacba12012-07-12 22:51:12 +0000798 shared_module_list.Remove (module_sp);
799 module_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000800 }
Greg Claytoncbacba12012-07-12 22:51:12 +0000801 else
802 {
803 // The module matches and the module was not modified from
804 // when it was last loaded.
805 return error;
806 }
Chris Lattner24943d22010-06-08 16:52:24 +0000807 }
808 }
Greg Clayton444fe992012-02-26 05:51:37 +0000809 }
Chris Lattner24943d22010-06-08 16:52:24 +0000810
Greg Clayton444fe992012-02-26 05:51:37 +0000811 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
Greg Claytondd506e12011-03-15 03:56:33 +0000819 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000820 {
Greg Clayton444fe992012-02-26 05:51:37 +0000821 if (module_sp->GetObjectFile())
Chris Lattner24943d22010-06-08 16:52:24 +0000822 {
Greg Clayton444fe992012-02-26 05:51:37 +0000823 // 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())
Greg Clayton46c9a352012-02-09 06:16:32 +0000826 module_sp.reset();
Greg Clayton444fe992012-02-26 05:51:37 +0000827 else
828 {
829 if (did_create_ptr)
830 *did_create_ptr = true;
831
Greg Claytonbcaf99a2012-07-12 20:32:19 +0000832 shared_module_list.ReplaceEquivalent(module_sp);
Greg Clayton444fe992012-02-26 05:51:37 +0000833 return error;
834 }
Chris Lattner24943d22010-06-08 16:52:24 +0000835 }
Greg Clayton444fe992012-02-26 05:51:37 +0000836 else
837 module_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000838 }
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.
Greg Clayton444fe992012-02-26 05:51:37 +0000847 FileSpec file_spec = Symbols::LocateExecutableObjectFile (module_spec);
Chris Lattner24943d22010-06-08 16:52:24 +0000848
849 // Don't look for the file if it appears to be the same one we already
850 // checked for above...
Greg Clayton444fe992012-02-26 05:51:37 +0000851 if (file_spec != module_file_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000852 {
853 if (!file_spec.Exists())
854 {
855 file_spec.GetPath(path, sizeof(path));
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000856 if (path[0] == '\0')
Greg Clayton444fe992012-02-26 05:51:37 +0000857 module_file_spec.GetPath(path, sizeof(path));
Chris Lattner24943d22010-06-08 16:52:24 +0000858 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])
Greg Clayton9c236732011-10-26 00:56:27 +0000869 error.SetErrorStringWithFormat("'%s' does not contain the %s architecture and UUID %s", path, arch.GetArchitectureName(), uuid_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +0000870 else
Greg Clayton9c236732011-10-26 00:56:27 +0000871 error.SetErrorStringWithFormat("'%s' does not contain the %s architecture.", path, arch.GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000872 }
873 }
874 else
875 {
Greg Clayton9c236732011-10-26 00:56:27 +0000876 error.SetErrorStringWithFormat("'%s' does not exist", path);
Chris Lattner24943d22010-06-08 16:52:24 +0000877 }
Greg Claytonb5a8f142012-02-05 02:38:54 +0000878 if (error.Fail())
879 module_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000880 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.
Greg Claytonaa206302012-03-21 04:25:00 +0000887 ModuleSpec platform_module_spec(module_spec);
888 platform_module_spec.GetFileSpec() = file_spec;
889 platform_module_spec.GetPlatformFileSpec() = file_spec;
Chris Lattner24943d22010-06-08 16:52:24 +0000890 ModuleList matching_module_list;
Greg Claytonaa206302012-03-21 04:25:00 +0000891 if (shared_module_list.FindModules (platform_module_spec, matching_module_list) > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000892 {
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!
Greg Claytonaa206302012-03-21 04:25:00 +0000897 if (platform_module_spec.GetUUIDPtr() == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000898 {
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 {
Greg Claytonaa206302012-03-21 04:25:00 +0000915 module_sp.reset (new Module (platform_module_spec));
Greg Clayton153ccd72011-08-10 02:10:13 +0000916 // 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())
Chris Lattner24943d22010-06-08 16:52:24 +0000920 {
921 if (did_create_ptr)
922 *did_create_ptr = true;
923
Greg Claytonbcaf99a2012-07-12 20:32:19 +0000924 shared_module_list.ReplaceEquivalent(module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000925 }
926 else
927 {
928 file_spec.GetPath(path, sizeof(path));
929
930 if (file_spec)
931 {
932 if (arch.IsValid())
Greg Clayton9c236732011-10-26 00:56:27 +0000933 error.SetErrorStringWithFormat("unable to open %s architecture in '%s'", arch.GetArchitectureName(), path);
Chris Lattner24943d22010-06-08 16:52:24 +0000934 else
Greg Clayton9c236732011-10-26 00:56:27 +0000935 error.SetErrorStringWithFormat("unable to open '%s'", path);
Chris Lattner24943d22010-06-08 16:52:24 +0000936 }
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])
Greg Clayton9c236732011-10-26 00:56:27 +0000945 error.SetErrorStringWithFormat("cannot locate a module for UUID '%s'", uuid_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +0000946 else
Greg Clayton9c236732011-10-26 00:56:27 +0000947 error.SetErrorStringWithFormat("cannot locate a module");
Chris Lattner24943d22010-06-08 16:52:24 +0000948 }
949 }
950 }
951 }
952
953 return error;
954}
955
Greg Claytondd506e12011-03-15 03:56:33 +0000956bool
957ModuleList::RemoveSharedModule (lldb::ModuleSP &module_sp)
958{
959 return GetSharedModuleList ().Remove (module_sp);
960}
961
Jim Ingham03e5e512012-05-17 18:38:42 +0000962bool
963ModuleList::RemoveSharedModuleIfOrphaned (const Module *module_ptr)
964{
965 return GetSharedModuleList ().RemoveIfOrphaned (module_ptr);
966}
967
Greg Claytondd506e12011-03-15 03:56:33 +0000968