1 //===-- ModuleList.cpp ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Core/ModuleList.h"
10 #include "lldb/Core/FileSpecList.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/ModuleSpec.h"
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Interpreter/OptionValueFileSpec.h"
15 #include "lldb/Interpreter/OptionValueProperties.h"
16 #include "lldb/Interpreter/Property.h"
17 #include "lldb/Symbol/LocateSymbolFile.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Symbol/SymbolContext.h"
20 #include "lldb/Symbol/TypeList.h"
21 #include "lldb/Symbol/VariableList.h"
22 #include "lldb/Utility/ArchSpec.h"
23 #include "lldb/Utility/ConstString.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/Logging.h"
26 #include "lldb/Utility/UUID.h"
27 #include "lldb/lldb-defines.h"
28
29 #if defined(_WIN32)
30 #include "lldb/Host/windows/PosixApi.h"
31 #endif
32
33 #include "clang/Driver/Driver.h"
34 #include "llvm/ADT/StringRef.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/Threading.h"
37 #include "llvm/Support/raw_ostream.h"
38
39 #include <chrono>
40 #include <memory>
41 #include <mutex>
42 #include <string>
43 #include <utility>
44
45 namespace lldb_private {
46 class Function;
47 }
48 namespace lldb_private {
49 class RegularExpression;
50 }
51 namespace lldb_private {
52 class Stream;
53 }
54 namespace lldb_private {
55 class SymbolFile;
56 }
57 namespace lldb_private {
58 class Target;
59 }
60
61 using namespace lldb;
62 using namespace lldb_private;
63
64 namespace {
65
66 #define LLDB_PROPERTIES_modulelist
67 #include "CoreProperties.inc"
68
69 enum {
70 #define LLDB_PROPERTIES_modulelist
71 #include "CorePropertiesEnum.inc"
72 };
73
74 } // namespace
75
ModuleListProperties()76 ModuleListProperties::ModuleListProperties() {
77 m_collection_sp =
78 std::make_shared<OptionValueProperties>(ConstString("symbols"));
79 m_collection_sp->Initialize(g_modulelist_properties);
80
81 llvm::SmallString<128> path;
82 clang::driver::Driver::getDefaultModuleCachePath(path);
83 SetClangModulesCachePath(path);
84 }
85
GetEnableExternalLookup() const86 bool ModuleListProperties::GetEnableExternalLookup() const {
87 const uint32_t idx = ePropertyEnableExternalLookup;
88 return m_collection_sp->GetPropertyAtIndexAsBoolean(
89 nullptr, idx, g_modulelist_properties[idx].default_uint_value != 0);
90 }
91
SetEnableExternalLookup(bool new_value)92 bool ModuleListProperties::SetEnableExternalLookup(bool new_value) {
93 return m_collection_sp->SetPropertyAtIndexAsBoolean(
94 nullptr, ePropertyEnableExternalLookup, new_value);
95 }
96
GetClangModulesCachePath() const97 FileSpec ModuleListProperties::GetClangModulesCachePath() const {
98 return m_collection_sp
99 ->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false,
100 ePropertyClangModulesCachePath)
101 ->GetCurrentValue();
102 }
103
SetClangModulesCachePath(llvm::StringRef path)104 bool ModuleListProperties::SetClangModulesCachePath(llvm::StringRef path) {
105 return m_collection_sp->SetPropertyAtIndexAsString(
106 nullptr, ePropertyClangModulesCachePath, path);
107 }
108
ModuleList()109 ModuleList::ModuleList()
110 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {}
111
ModuleList(const ModuleList & rhs)112 ModuleList::ModuleList(const ModuleList &rhs)
113 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {
114 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
115 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
116 m_modules = rhs.m_modules;
117 }
118
ModuleList(ModuleList::Notifier * notifier)119 ModuleList::ModuleList(ModuleList::Notifier *notifier)
120 : m_modules(), m_modules_mutex(), m_notifier(notifier) {}
121
operator =(const ModuleList & rhs)122 const ModuleList &ModuleList::operator=(const ModuleList &rhs) {
123 if (this != &rhs) {
124 std::lock(m_modules_mutex, rhs.m_modules_mutex);
125 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex,
126 std::adopt_lock);
127 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex,
128 std::adopt_lock);
129 m_modules = rhs.m_modules;
130 }
131 return *this;
132 }
133
134 ModuleList::~ModuleList() = default;
135
AppendImpl(const ModuleSP & module_sp,bool use_notifier)136 void ModuleList::AppendImpl(const ModuleSP &module_sp, bool use_notifier) {
137 if (module_sp) {
138 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
139 m_modules.push_back(module_sp);
140 if (use_notifier && m_notifier)
141 m_notifier->NotifyModuleAdded(*this, module_sp);
142 }
143 }
144
Append(const ModuleSP & module_sp,bool notify)145 void ModuleList::Append(const ModuleSP &module_sp, bool notify) {
146 AppendImpl(module_sp, notify);
147 }
148
ReplaceEquivalent(const ModuleSP & module_sp)149 void ModuleList::ReplaceEquivalent(const ModuleSP &module_sp) {
150 if (module_sp) {
151 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
152
153 // First remove any equivalent modules. Equivalent modules are modules
154 // whose path, platform path and architecture match.
155 ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
156 module_sp->GetArchitecture());
157 equivalent_module_spec.GetPlatformFileSpec() =
158 module_sp->GetPlatformFileSpec();
159
160 size_t idx = 0;
161 while (idx < m_modules.size()) {
162 ModuleSP module_sp(m_modules[idx]);
163 if (module_sp->MatchesModuleSpec(equivalent_module_spec))
164 RemoveImpl(m_modules.begin() + idx);
165 else
166 ++idx;
167 }
168 // Now add the new module to the list
169 Append(module_sp);
170 }
171 }
172
AppendIfNeeded(const ModuleSP & module_sp,bool notify)173 bool ModuleList::AppendIfNeeded(const ModuleSP &module_sp, bool notify) {
174 if (module_sp) {
175 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
176 collection::iterator pos, end = m_modules.end();
177 for (pos = m_modules.begin(); pos != end; ++pos) {
178 if (pos->get() == module_sp.get())
179 return false; // Already in the list
180 }
181 // Only push module_sp on the list if it wasn't already in there.
182 Append(module_sp, notify);
183 return true;
184 }
185 return false;
186 }
187
Append(const ModuleList & module_list)188 void ModuleList::Append(const ModuleList &module_list) {
189 for (auto pos : module_list.m_modules)
190 Append(pos);
191 }
192
AppendIfNeeded(const ModuleList & module_list)193 bool ModuleList::AppendIfNeeded(const ModuleList &module_list) {
194 bool any_in = false;
195 for (auto pos : module_list.m_modules) {
196 if (AppendIfNeeded(pos))
197 any_in = true;
198 }
199 return any_in;
200 }
201
RemoveImpl(const ModuleSP & module_sp,bool use_notifier)202 bool ModuleList::RemoveImpl(const ModuleSP &module_sp, bool use_notifier) {
203 if (module_sp) {
204 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
205 collection::iterator pos, end = m_modules.end();
206 for (pos = m_modules.begin(); pos != end; ++pos) {
207 if (pos->get() == module_sp.get()) {
208 m_modules.erase(pos);
209 if (use_notifier && m_notifier)
210 m_notifier->NotifyModuleRemoved(*this, module_sp);
211 return true;
212 }
213 }
214 }
215 return false;
216 }
217
218 ModuleList::collection::iterator
RemoveImpl(ModuleList::collection::iterator pos,bool use_notifier)219 ModuleList::RemoveImpl(ModuleList::collection::iterator pos,
220 bool use_notifier) {
221 ModuleSP module_sp(*pos);
222 collection::iterator retval = m_modules.erase(pos);
223 if (use_notifier && m_notifier)
224 m_notifier->NotifyModuleRemoved(*this, module_sp);
225 return retval;
226 }
227
Remove(const ModuleSP & module_sp,bool notify)228 bool ModuleList::Remove(const ModuleSP &module_sp, bool notify) {
229 return RemoveImpl(module_sp, notify);
230 }
231
ReplaceModule(const lldb::ModuleSP & old_module_sp,const lldb::ModuleSP & new_module_sp)232 bool ModuleList::ReplaceModule(const lldb::ModuleSP &old_module_sp,
233 const lldb::ModuleSP &new_module_sp) {
234 if (!RemoveImpl(old_module_sp, false))
235 return false;
236 AppendImpl(new_module_sp, false);
237 if (m_notifier)
238 m_notifier->NotifyModuleUpdated(*this, old_module_sp, new_module_sp);
239 return true;
240 }
241
RemoveIfOrphaned(const Module * module_ptr)242 bool ModuleList::RemoveIfOrphaned(const Module *module_ptr) {
243 if (module_ptr) {
244 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
245 collection::iterator pos, end = m_modules.end();
246 for (pos = m_modules.begin(); pos != end; ++pos) {
247 if (pos->get() == module_ptr) {
248 if (pos->unique()) {
249 pos = RemoveImpl(pos);
250 return true;
251 } else
252 return false;
253 }
254 }
255 }
256 return false;
257 }
258
RemoveOrphans(bool mandatory)259 size_t ModuleList::RemoveOrphans(bool mandatory) {
260 std::unique_lock<std::recursive_mutex> lock(m_modules_mutex, std::defer_lock);
261
262 if (mandatory) {
263 lock.lock();
264 } else {
265 // Not mandatory, remove orphans if we can get the mutex
266 if (!lock.try_lock())
267 return 0;
268 }
269 collection::iterator pos = m_modules.begin();
270 size_t remove_count = 0;
271 while (pos != m_modules.end()) {
272 if (pos->unique()) {
273 pos = RemoveImpl(pos);
274 ++remove_count;
275 } else {
276 ++pos;
277 }
278 }
279 return remove_count;
280 }
281
Remove(ModuleList & module_list)282 size_t ModuleList::Remove(ModuleList &module_list) {
283 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
284 size_t num_removed = 0;
285 collection::iterator pos, end = module_list.m_modules.end();
286 for (pos = module_list.m_modules.begin(); pos != end; ++pos) {
287 if (Remove(*pos, false /* notify */))
288 ++num_removed;
289 }
290 if (m_notifier)
291 m_notifier->NotifyModulesRemoved(module_list);
292 return num_removed;
293 }
294
Clear()295 void ModuleList::Clear() { ClearImpl(); }
296
Destroy()297 void ModuleList::Destroy() { ClearImpl(); }
298
ClearImpl(bool use_notifier)299 void ModuleList::ClearImpl(bool use_notifier) {
300 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
301 if (use_notifier && m_notifier)
302 m_notifier->NotifyWillClearList(*this);
303 m_modules.clear();
304 }
305
GetModulePointerAtIndex(size_t idx) const306 Module *ModuleList::GetModulePointerAtIndex(size_t idx) const {
307 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
308 return GetModulePointerAtIndexUnlocked(idx);
309 }
310
GetModulePointerAtIndexUnlocked(size_t idx) const311 Module *ModuleList::GetModulePointerAtIndexUnlocked(size_t idx) const {
312 if (idx < m_modules.size())
313 return m_modules[idx].get();
314 return nullptr;
315 }
316
GetModuleAtIndex(size_t idx) const317 ModuleSP ModuleList::GetModuleAtIndex(size_t idx) const {
318 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
319 return GetModuleAtIndexUnlocked(idx);
320 }
321
GetModuleAtIndexUnlocked(size_t idx) const322 ModuleSP ModuleList::GetModuleAtIndexUnlocked(size_t idx) const {
323 ModuleSP module_sp;
324 if (idx < m_modules.size())
325 module_sp = m_modules[idx];
326 return module_sp;
327 }
328
FindFunctions(ConstString name,FunctionNameType name_type_mask,bool include_symbols,bool include_inlines,SymbolContextList & sc_list) const329 void ModuleList::FindFunctions(ConstString name,
330 FunctionNameType name_type_mask,
331 bool include_symbols, bool include_inlines,
332 SymbolContextList &sc_list) const {
333 const size_t old_size = sc_list.GetSize();
334
335 if (name_type_mask & eFunctionNameTypeAuto) {
336 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
337
338 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
339 collection::const_iterator pos, end = m_modules.end();
340 for (pos = m_modules.begin(); pos != end; ++pos) {
341 (*pos)->FindFunctions(lookup_info.GetLookupName(), nullptr,
342 lookup_info.GetNameTypeMask(), include_symbols,
343 include_inlines, sc_list);
344 }
345
346 const size_t new_size = sc_list.GetSize();
347
348 if (old_size < new_size)
349 lookup_info.Prune(sc_list, old_size);
350 } else {
351 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
352 collection::const_iterator pos, end = m_modules.end();
353 for (pos = m_modules.begin(); pos != end; ++pos) {
354 (*pos)->FindFunctions(name, nullptr, name_type_mask, include_symbols,
355 include_inlines, sc_list);
356 }
357 }
358 }
359
FindFunctionSymbols(ConstString name,lldb::FunctionNameType name_type_mask,SymbolContextList & sc_list)360 void ModuleList::FindFunctionSymbols(ConstString name,
361 lldb::FunctionNameType name_type_mask,
362 SymbolContextList &sc_list) {
363 const size_t old_size = sc_list.GetSize();
364
365 if (name_type_mask & eFunctionNameTypeAuto) {
366 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
367
368 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
369 collection::const_iterator pos, end = m_modules.end();
370 for (pos = m_modules.begin(); pos != end; ++pos) {
371 (*pos)->FindFunctionSymbols(lookup_info.GetLookupName(),
372 lookup_info.GetNameTypeMask(), sc_list);
373 }
374
375 const size_t new_size = sc_list.GetSize();
376
377 if (old_size < new_size)
378 lookup_info.Prune(sc_list, old_size);
379 } else {
380 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
381 collection::const_iterator pos, end = m_modules.end();
382 for (pos = m_modules.begin(); pos != end; ++pos) {
383 (*pos)->FindFunctionSymbols(name, name_type_mask, sc_list);
384 }
385 }
386 }
387
FindFunctions(const RegularExpression & name,bool include_symbols,bool include_inlines,SymbolContextList & sc_list)388 void ModuleList::FindFunctions(const RegularExpression &name,
389 bool include_symbols, bool include_inlines,
390 SymbolContextList &sc_list) {
391 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
392 collection::const_iterator pos, end = m_modules.end();
393 for (pos = m_modules.begin(); pos != end; ++pos) {
394 (*pos)->FindFunctions(name, include_symbols, include_inlines, sc_list);
395 }
396 }
397
FindCompileUnits(const FileSpec & path,SymbolContextList & sc_list) const398 void ModuleList::FindCompileUnits(const FileSpec &path,
399 SymbolContextList &sc_list) const {
400 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
401 collection::const_iterator pos, end = m_modules.end();
402 for (pos = m_modules.begin(); pos != end; ++pos) {
403 (*pos)->FindCompileUnits(path, sc_list);
404 }
405 }
406
FindGlobalVariables(ConstString name,size_t max_matches,VariableList & variable_list) const407 void ModuleList::FindGlobalVariables(ConstString name, size_t max_matches,
408 VariableList &variable_list) const {
409 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
410 collection::const_iterator pos, end = m_modules.end();
411 for (pos = m_modules.begin(); pos != end; ++pos) {
412 (*pos)->FindGlobalVariables(name, nullptr, max_matches, variable_list);
413 }
414 }
415
FindGlobalVariables(const RegularExpression & regex,size_t max_matches,VariableList & variable_list) const416 void ModuleList::FindGlobalVariables(const RegularExpression ®ex,
417 size_t max_matches,
418 VariableList &variable_list) const {
419 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
420 collection::const_iterator pos, end = m_modules.end();
421 for (pos = m_modules.begin(); pos != end; ++pos) {
422 (*pos)->FindGlobalVariables(regex, max_matches, variable_list);
423 }
424 }
425
FindSymbolsWithNameAndType(ConstString name,SymbolType symbol_type,SymbolContextList & sc_list) const426 void ModuleList::FindSymbolsWithNameAndType(ConstString name,
427 SymbolType symbol_type,
428 SymbolContextList &sc_list) const {
429 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
430 collection::const_iterator pos, end = m_modules.end();
431 for (pos = m_modules.begin(); pos != end; ++pos)
432 (*pos)->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
433 }
434
FindSymbolsMatchingRegExAndType(const RegularExpression & regex,lldb::SymbolType symbol_type,SymbolContextList & sc_list) const435 void ModuleList::FindSymbolsMatchingRegExAndType(
436 const RegularExpression ®ex, lldb::SymbolType symbol_type,
437 SymbolContextList &sc_list) const {
438 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
439 collection::const_iterator pos, end = m_modules.end();
440 for (pos = m_modules.begin(); pos != end; ++pos)
441 (*pos)->FindSymbolsMatchingRegExAndType(regex, symbol_type, sc_list);
442 }
443
FindModules(const ModuleSpec & module_spec,ModuleList & matching_module_list) const444 void ModuleList::FindModules(const ModuleSpec &module_spec,
445 ModuleList &matching_module_list) const {
446 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
447 collection::const_iterator pos, end = m_modules.end();
448 for (pos = m_modules.begin(); pos != end; ++pos) {
449 ModuleSP module_sp(*pos);
450 if (module_sp->MatchesModuleSpec(module_spec))
451 matching_module_list.Append(module_sp);
452 }
453 }
454
FindModule(const Module * module_ptr) const455 ModuleSP ModuleList::FindModule(const Module *module_ptr) const {
456 ModuleSP module_sp;
457
458 // Scope for "locker"
459 {
460 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
461 collection::const_iterator pos, end = m_modules.end();
462
463 for (pos = m_modules.begin(); pos != end; ++pos) {
464 if ((*pos).get() == module_ptr) {
465 module_sp = (*pos);
466 break;
467 }
468 }
469 }
470 return module_sp;
471 }
472
FindModule(const UUID & uuid) const473 ModuleSP ModuleList::FindModule(const UUID &uuid) const {
474 ModuleSP module_sp;
475
476 if (uuid.IsValid()) {
477 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
478 collection::const_iterator pos, end = m_modules.end();
479
480 for (pos = m_modules.begin(); pos != end; ++pos) {
481 if ((*pos)->GetUUID() == uuid) {
482 module_sp = (*pos);
483 break;
484 }
485 }
486 }
487 return module_sp;
488 }
489
FindTypes(Module * search_first,ConstString name,bool name_is_fully_qualified,size_t max_matches,llvm::DenseSet<SymbolFile * > & searched_symbol_files,TypeList & types) const490 void ModuleList::FindTypes(Module *search_first, ConstString name,
491 bool name_is_fully_qualified, size_t max_matches,
492 llvm::DenseSet<SymbolFile *> &searched_symbol_files,
493 TypeList &types) const {
494 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
495
496 collection::const_iterator pos, end = m_modules.end();
497 if (search_first) {
498 for (pos = m_modules.begin(); pos != end; ++pos) {
499 if (search_first == pos->get()) {
500 search_first->FindTypes(name, name_is_fully_qualified, max_matches,
501 searched_symbol_files, types);
502
503 if (types.GetSize() >= max_matches)
504 return;
505 }
506 }
507 }
508
509 for (pos = m_modules.begin(); pos != end; ++pos) {
510 // Search the module if the module is not equal to the one in the symbol
511 // context "sc". If "sc" contains a empty module shared pointer, then the
512 // comparison will always be true (valid_module_ptr != nullptr).
513 if (search_first != pos->get())
514 (*pos)->FindTypes(name, name_is_fully_qualified, max_matches,
515 searched_symbol_files, types);
516
517 if (types.GetSize() >= max_matches)
518 return;
519 }
520 }
521
FindSourceFile(const FileSpec & orig_spec,FileSpec & new_spec) const522 bool ModuleList::FindSourceFile(const FileSpec &orig_spec,
523 FileSpec &new_spec) const {
524 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
525 collection::const_iterator pos, end = m_modules.end();
526 for (pos = m_modules.begin(); pos != end; ++pos) {
527 if ((*pos)->FindSourceFile(orig_spec, new_spec))
528 return true;
529 }
530 return false;
531 }
532
FindAddressesForLine(const lldb::TargetSP target_sp,const FileSpec & file,uint32_t line,Function * function,std::vector<Address> & output_local,std::vector<Address> & output_extern)533 void ModuleList::FindAddressesForLine(const lldb::TargetSP target_sp,
534 const FileSpec &file, uint32_t line,
535 Function *function,
536 std::vector<Address> &output_local,
537 std::vector<Address> &output_extern) {
538 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
539 collection::const_iterator pos, end = m_modules.end();
540 for (pos = m_modules.begin(); pos != end; ++pos) {
541 (*pos)->FindAddressesForLine(target_sp, file, line, function, output_local,
542 output_extern);
543 }
544 }
545
FindFirstModule(const ModuleSpec & module_spec) const546 ModuleSP ModuleList::FindFirstModule(const ModuleSpec &module_spec) const {
547 ModuleSP module_sp;
548 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
549 collection::const_iterator pos, end = m_modules.end();
550 for (pos = m_modules.begin(); pos != end; ++pos) {
551 ModuleSP module_sp(*pos);
552 if (module_sp->MatchesModuleSpec(module_spec))
553 return module_sp;
554 }
555 return module_sp;
556 }
557
GetSize() const558 size_t ModuleList::GetSize() const {
559 size_t size = 0;
560 {
561 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
562 size = m_modules.size();
563 }
564 return size;
565 }
566
Dump(Stream * s) const567 void ModuleList::Dump(Stream *s) const {
568 // s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
569 // s.Indent();
570 // s << "ModuleList\n";
571
572 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
573 collection::const_iterator pos, end = m_modules.end();
574 for (pos = m_modules.begin(); pos != end; ++pos) {
575 (*pos)->Dump(s);
576 }
577 }
578
LogUUIDAndPaths(Log * log,const char * prefix_cstr)579 void ModuleList::LogUUIDAndPaths(Log *log, const char *prefix_cstr) {
580 if (log != nullptr) {
581 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
582 collection::const_iterator pos, begin = m_modules.begin(),
583 end = m_modules.end();
584 for (pos = begin; pos != end; ++pos) {
585 Module *module = pos->get();
586 const FileSpec &module_file_spec = module->GetFileSpec();
587 LLDB_LOGF(log, "%s[%u] %s (%s) \"%s\"", prefix_cstr ? prefix_cstr : "",
588 (uint32_t)std::distance(begin, pos),
589 module->GetUUID().GetAsString().c_str(),
590 module->GetArchitecture().GetArchitectureName(),
591 module_file_spec.GetPath().c_str());
592 }
593 }
594 }
595
ResolveFileAddress(lldb::addr_t vm_addr,Address & so_addr) const596 bool ModuleList::ResolveFileAddress(lldb::addr_t vm_addr,
597 Address &so_addr) const {
598 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
599 collection::const_iterator pos, end = m_modules.end();
600 for (pos = m_modules.begin(); pos != end; ++pos) {
601 if ((*pos)->ResolveFileAddress(vm_addr, so_addr))
602 return true;
603 }
604
605 return false;
606 }
607
608 uint32_t
ResolveSymbolContextForAddress(const Address & so_addr,SymbolContextItem resolve_scope,SymbolContext & sc) const609 ModuleList::ResolveSymbolContextForAddress(const Address &so_addr,
610 SymbolContextItem resolve_scope,
611 SymbolContext &sc) const {
612 // The address is already section offset so it has a module
613 uint32_t resolved_flags = 0;
614 ModuleSP module_sp(so_addr.GetModule());
615 if (module_sp) {
616 resolved_flags =
617 module_sp->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
618 } else {
619 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
620 collection::const_iterator pos, end = m_modules.end();
621 for (pos = m_modules.begin(); pos != end; ++pos) {
622 resolved_flags =
623 (*pos)->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
624 if (resolved_flags != 0)
625 break;
626 }
627 }
628
629 return resolved_flags;
630 }
631
ResolveSymbolContextForFilePath(const char * file_path,uint32_t line,bool check_inlines,SymbolContextItem resolve_scope,SymbolContextList & sc_list) const632 uint32_t ModuleList::ResolveSymbolContextForFilePath(
633 const char *file_path, uint32_t line, bool check_inlines,
634 SymbolContextItem resolve_scope, SymbolContextList &sc_list) const {
635 FileSpec file_spec(file_path);
636 return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
637 resolve_scope, sc_list);
638 }
639
ResolveSymbolContextsForFileSpec(const FileSpec & file_spec,uint32_t line,bool check_inlines,SymbolContextItem resolve_scope,SymbolContextList & sc_list) const640 uint32_t ModuleList::ResolveSymbolContextsForFileSpec(
641 const FileSpec &file_spec, uint32_t line, bool check_inlines,
642 SymbolContextItem resolve_scope, SymbolContextList &sc_list) const {
643 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
644 collection::const_iterator pos, end = m_modules.end();
645 for (pos = m_modules.begin(); pos != end; ++pos) {
646 (*pos)->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
647 resolve_scope, sc_list);
648 }
649
650 return sc_list.GetSize();
651 }
652
GetIndexForModule(const Module * module) const653 size_t ModuleList::GetIndexForModule(const Module *module) const {
654 if (module) {
655 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
656 collection::const_iterator pos;
657 collection::const_iterator begin = m_modules.begin();
658 collection::const_iterator end = m_modules.end();
659 for (pos = begin; pos != end; ++pos) {
660 if ((*pos).get() == module)
661 return std::distance(begin, pos);
662 }
663 }
664 return LLDB_INVALID_INDEX32;
665 }
666
667 namespace {
668 struct SharedModuleListInfo {
669 ModuleList module_list;
670 ModuleListProperties module_list_properties;
671 };
672 }
GetSharedModuleListInfo()673 static SharedModuleListInfo &GetSharedModuleListInfo()
674 {
675 static SharedModuleListInfo *g_shared_module_list_info = nullptr;
676 static llvm::once_flag g_once_flag;
677 llvm::call_once(g_once_flag, []() {
678 // NOTE: Intentionally leak the module list so a program doesn't have to
679 // cleanup all modules and object files as it exits. This just wastes time
680 // doing a bunch of cleanup that isn't required.
681 if (g_shared_module_list_info == nullptr)
682 g_shared_module_list_info = new SharedModuleListInfo();
683 });
684 return *g_shared_module_list_info;
685 }
686
GetSharedModuleList()687 static ModuleList &GetSharedModuleList() {
688 return GetSharedModuleListInfo().module_list;
689 }
690
GetGlobalModuleListProperties()691 ModuleListProperties &ModuleList::GetGlobalModuleListProperties() {
692 return GetSharedModuleListInfo().module_list_properties;
693 }
694
ModuleIsInCache(const Module * module_ptr)695 bool ModuleList::ModuleIsInCache(const Module *module_ptr) {
696 if (module_ptr) {
697 ModuleList &shared_module_list = GetSharedModuleList();
698 return shared_module_list.FindModule(module_ptr).get() != nullptr;
699 }
700 return false;
701 }
702
FindSharedModules(const ModuleSpec & module_spec,ModuleList & matching_module_list)703 void ModuleList::FindSharedModules(const ModuleSpec &module_spec,
704 ModuleList &matching_module_list) {
705 GetSharedModuleList().FindModules(module_spec, matching_module_list);
706 }
707
RemoveOrphanSharedModules(bool mandatory)708 size_t ModuleList::RemoveOrphanSharedModules(bool mandatory) {
709 return GetSharedModuleList().RemoveOrphans(mandatory);
710 }
711
GetSharedModule(const ModuleSpec & module_spec,ModuleSP & module_sp,const FileSpecList * module_search_paths_ptr,ModuleSP * old_module_sp_ptr,bool * did_create_ptr,bool always_create)712 Status ModuleList::GetSharedModule(const ModuleSpec &module_spec,
713 ModuleSP &module_sp,
714 const FileSpecList *module_search_paths_ptr,
715 ModuleSP *old_module_sp_ptr,
716 bool *did_create_ptr, bool always_create) {
717 ModuleList &shared_module_list = GetSharedModuleList();
718 std::lock_guard<std::recursive_mutex> guard(
719 shared_module_list.m_modules_mutex);
720 char path[PATH_MAX];
721
722 Status error;
723
724 module_sp.reset();
725
726 if (did_create_ptr)
727 *did_create_ptr = false;
728 if (old_module_sp_ptr)
729 old_module_sp_ptr->reset();
730
731 const UUID *uuid_ptr = module_spec.GetUUIDPtr();
732 const FileSpec &module_file_spec = module_spec.GetFileSpec();
733 const ArchSpec &arch = module_spec.GetArchitecture();
734
735 // Make sure no one else can try and get or create a module while this
736 // function is actively working on it by doing an extra lock on the global
737 // mutex list.
738 if (!always_create) {
739 ModuleList matching_module_list;
740 shared_module_list.FindModules(module_spec, matching_module_list);
741 const size_t num_matching_modules = matching_module_list.GetSize();
742
743 if (num_matching_modules > 0) {
744 for (size_t module_idx = 0; module_idx < num_matching_modules;
745 ++module_idx) {
746 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
747
748 // Make sure the file for the module hasn't been modified
749 if (module_sp->FileHasChanged()) {
750 if (old_module_sp_ptr && !*old_module_sp_ptr)
751 *old_module_sp_ptr = module_sp;
752
753 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_MODULES));
754 if (log != nullptr)
755 LLDB_LOGF(
756 log, "%p '%s' module changed: removing from global module list",
757 static_cast<void *>(module_sp.get()),
758 module_sp->GetFileSpec().GetFilename().GetCString());
759
760 shared_module_list.Remove(module_sp);
761 module_sp.reset();
762 } else {
763 // The module matches and the module was not modified from when it
764 // was last loaded.
765 return error;
766 }
767 }
768 }
769 }
770
771 if (module_sp)
772 return error;
773
774 module_sp = std::make_shared<Module>(module_spec);
775 // Make sure there are a module and an object file since we can specify a
776 // valid file path with an architecture that might not be in that file. By
777 // getting the object file we can guarantee that the architecture matches
778 if (module_sp->GetObjectFile()) {
779 // If we get in here we got the correct arch, now we just need to verify
780 // the UUID if one was given
781 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
782 module_sp.reset();
783 } else {
784 if (module_sp->GetObjectFile() &&
785 module_sp->GetObjectFile()->GetType() ==
786 ObjectFile::eTypeStubLibrary) {
787 module_sp.reset();
788 } else {
789 if (did_create_ptr) {
790 *did_create_ptr = true;
791 }
792
793 shared_module_list.ReplaceEquivalent(module_sp);
794 return error;
795 }
796 }
797 } else {
798 module_sp.reset();
799 }
800
801 if (module_search_paths_ptr) {
802 const auto num_directories = module_search_paths_ptr->GetSize();
803 for (size_t idx = 0; idx < num_directories; ++idx) {
804 auto search_path_spec = module_search_paths_ptr->GetFileSpecAtIndex(idx);
805 FileSystem::Instance().Resolve(search_path_spec);
806 namespace fs = llvm::sys::fs;
807 if (!FileSystem::Instance().IsDirectory(search_path_spec))
808 continue;
809 search_path_spec.AppendPathComponent(
810 module_spec.GetFileSpec().GetFilename().AsCString());
811 if (!FileSystem::Instance().Exists(search_path_spec))
812 continue;
813
814 auto resolved_module_spec(module_spec);
815 resolved_module_spec.GetFileSpec() = search_path_spec;
816 module_sp = std::make_shared<Module>(resolved_module_spec);
817 if (module_sp->GetObjectFile()) {
818 // If we get in here we got the correct arch, now we just need to
819 // verify the UUID if one was given
820 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
821 module_sp.reset();
822 } else {
823 if (module_sp->GetObjectFile()->GetType() ==
824 ObjectFile::eTypeStubLibrary) {
825 module_sp.reset();
826 } else {
827 if (did_create_ptr)
828 *did_create_ptr = true;
829
830 shared_module_list.ReplaceEquivalent(module_sp);
831 return Status();
832 }
833 }
834 } else {
835 module_sp.reset();
836 }
837 }
838 }
839
840 // Either the file didn't exist where at the path, or no path was given, so
841 // we now have to use more extreme measures to try and find the appropriate
842 // module.
843
844 // Fixup the incoming path in case the path points to a valid file, yet the
845 // arch or UUID (if one was passed in) don't match.
846 ModuleSpec located_binary_modulespec =
847 Symbols::LocateExecutableObjectFile(module_spec);
848
849 // Don't look for the file if it appears to be the same one we already
850 // checked for above...
851 if (located_binary_modulespec.GetFileSpec() != module_file_spec) {
852 if (!FileSystem::Instance().Exists(
853 located_binary_modulespec.GetFileSpec())) {
854 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
855 if (path[0] == '\0')
856 module_file_spec.GetPath(path, sizeof(path));
857 // How can this check ever be true? This branch it is false, and we
858 // haven't modified file_spec.
859 if (FileSystem::Instance().Exists(
860 located_binary_modulespec.GetFileSpec())) {
861 std::string uuid_str;
862 if (uuid_ptr && uuid_ptr->IsValid())
863 uuid_str = uuid_ptr->GetAsString();
864
865 if (arch.IsValid()) {
866 if (!uuid_str.empty())
867 error.SetErrorStringWithFormat(
868 "'%s' does not contain the %s architecture and UUID %s", path,
869 arch.GetArchitectureName(), uuid_str.c_str());
870 else
871 error.SetErrorStringWithFormat(
872 "'%s' does not contain the %s architecture.", path,
873 arch.GetArchitectureName());
874 }
875 } else {
876 error.SetErrorStringWithFormat("'%s' does not exist", path);
877 }
878 if (error.Fail())
879 module_sp.reset();
880 return error;
881 }
882
883 // Make sure no one else can try and get or create a module while this
884 // function is actively working on it by doing an extra lock on the global
885 // mutex list.
886 ModuleSpec platform_module_spec(module_spec);
887 platform_module_spec.GetFileSpec() =
888 located_binary_modulespec.GetFileSpec();
889 platform_module_spec.GetPlatformFileSpec() =
890 located_binary_modulespec.GetFileSpec();
891 platform_module_spec.GetSymbolFileSpec() =
892 located_binary_modulespec.GetSymbolFileSpec();
893 ModuleList matching_module_list;
894 shared_module_list.FindModules(platform_module_spec, matching_module_list);
895 if (!matching_module_list.IsEmpty()) {
896 module_sp = matching_module_list.GetModuleAtIndex(0);
897
898 // If we didn't have a UUID in mind when looking for the object file,
899 // then we should make sure the modification time hasn't changed!
900 if (platform_module_spec.GetUUIDPtr() == nullptr) {
901 auto file_spec_mod_time = FileSystem::Instance().GetModificationTime(
902 located_binary_modulespec.GetFileSpec());
903 if (file_spec_mod_time != llvm::sys::TimePoint<>()) {
904 if (file_spec_mod_time != module_sp->GetModificationTime()) {
905 if (old_module_sp_ptr)
906 *old_module_sp_ptr = module_sp;
907 shared_module_list.Remove(module_sp);
908 module_sp.reset();
909 }
910 }
911 }
912 }
913
914 if (!module_sp) {
915 module_sp = std::make_shared<Module>(platform_module_spec);
916 // Make sure there are a module and an object file since we can specify a
917 // 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
919 // matches
920 if (module_sp && module_sp->GetObjectFile()) {
921 if (module_sp->GetObjectFile()->GetType() ==
922 ObjectFile::eTypeStubLibrary) {
923 module_sp.reset();
924 } else {
925 if (did_create_ptr)
926 *did_create_ptr = true;
927
928 shared_module_list.ReplaceEquivalent(module_sp);
929 }
930 } else {
931 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
932
933 if (located_binary_modulespec.GetFileSpec()) {
934 if (arch.IsValid())
935 error.SetErrorStringWithFormat(
936 "unable to open %s architecture in '%s'",
937 arch.GetArchitectureName(), path);
938 else
939 error.SetErrorStringWithFormat("unable to open '%s'", path);
940 } else {
941 std::string uuid_str;
942 if (uuid_ptr && uuid_ptr->IsValid())
943 uuid_str = uuid_ptr->GetAsString();
944
945 if (!uuid_str.empty())
946 error.SetErrorStringWithFormat(
947 "cannot locate a module for UUID '%s'", uuid_str.c_str());
948 else
949 error.SetErrorStringWithFormat("cannot locate a module");
950 }
951 }
952 }
953 }
954
955 return error;
956 }
957
RemoveSharedModule(lldb::ModuleSP & module_sp)958 bool ModuleList::RemoveSharedModule(lldb::ModuleSP &module_sp) {
959 return GetSharedModuleList().Remove(module_sp);
960 }
961
RemoveSharedModuleIfOrphaned(const Module * module_ptr)962 bool ModuleList::RemoveSharedModuleIfOrphaned(const Module *module_ptr) {
963 return GetSharedModuleList().RemoveIfOrphaned(module_ptr);
964 }
965
LoadScriptingResourcesInTarget(Target * target,std::list<Status> & errors,Stream * feedback_stream,bool continue_on_error)966 bool ModuleList::LoadScriptingResourcesInTarget(Target *target,
967 std::list<Status> &errors,
968 Stream *feedback_stream,
969 bool continue_on_error) {
970 if (!target)
971 return false;
972 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
973 for (auto module : m_modules) {
974 Status error;
975 if (module) {
976 if (!module->LoadScriptingResourceInTarget(target, error,
977 feedback_stream)) {
978 if (error.Fail() && error.AsCString()) {
979 error.SetErrorStringWithFormat("unable to load scripting data for "
980 "module %s - error reported was %s",
981 module->GetFileSpec()
982 .GetFileNameStrippingExtension()
983 .GetCString(),
984 error.AsCString());
985 errors.push_back(error);
986
987 if (!continue_on_error)
988 return false;
989 }
990 }
991 }
992 }
993 return errors.empty();
994 }
995
ForEach(std::function<bool (const ModuleSP & module_sp)> const & callback) const996 void ModuleList::ForEach(
997 std::function<bool(const ModuleSP &module_sp)> const &callback) const {
998 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
999 for (const auto &module : m_modules) {
1000 // If the callback returns false, then stop iterating and break out
1001 if (!callback(module))
1002 break;
1003 }
1004 }
1005