LLDB mainline
ModuleList.cpp
Go to the documentation of this file.
1//===-- ModuleList.cpp ----------------------------------------------------===//
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
10#include "lldb/Core/Module.h"
26#include "lldb/Utility/Log.h"
27#include "lldb/Utility/UUID.h"
28#include "lldb/lldb-defines.h"
29
30#if defined(_WIN32)
32#endif
33
34#include "clang/Driver/Driver.h"
35#include "llvm/ADT/StringRef.h"
36#include "llvm/Support/FileSystem.h"
37#include "llvm/Support/Threading.h"
38#include "llvm/Support/raw_ostream.h"
39
40#include <chrono>
41#include <memory>
42#include <mutex>
43#include <string>
44#include <utility>
45
46namespace lldb_private {
47class Function;
48}
49namespace lldb_private {
51}
52namespace lldb_private {
53class Stream;
54}
55namespace lldb_private {
56class SymbolFile;
57}
58namespace lldb_private {
59class Target;
60}
61
62using namespace lldb;
63using namespace lldb_private;
64
65namespace {
66
67#define LLDB_PROPERTIES_modulelist
68#include "CoreProperties.inc"
69
70enum {
71#define LLDB_PROPERTIES_modulelist
72#include "CorePropertiesEnum.inc"
73};
74
75} // namespace
76
79 std::make_shared<OptionValueProperties>(ConstString("symbols"));
80 m_collection_sp->Initialize(g_modulelist_properties);
81 m_collection_sp->SetValueChangedCallback(ePropertySymLinkPaths,
82 [this] { UpdateSymlinkMappings(); });
83
84 llvm::SmallString<128> path;
85 if (clang::driver::Driver::getDefaultModuleCachePath(path)) {
87 }
88
89 path.clear();
90 if (llvm::sys::path::cache_directory(path)) {
91 llvm::sys::path::append(path, "lldb");
92 llvm::sys::path::append(path, "IndexCache");
94 }
95
96}
97
99 const uint32_t idx = ePropertyEnableExternalLookup;
100 return GetPropertyAtIndexAs<bool>(
101 idx, g_modulelist_properties[idx].default_uint_value != 0);
102}
103
105 return SetPropertyAtIndex(ePropertyEnableExternalLookup, new_value);
106}
107
109 const uint32_t idx = ePropertyEnableBackgroundLookup;
110 return GetPropertyAtIndexAs<bool>(
111 idx, g_modulelist_properties[idx].default_uint_value != 0);
112}
113
115 const uint32_t idx = ePropertyClangModulesCachePath;
116 return GetPropertyAtIndexAs<FileSpec>(idx, {});
117}
118
120 const uint32_t idx = ePropertyClangModulesCachePath;
121 return SetPropertyAtIndex(idx, path);
122}
123
125 const uint32_t idx = ePropertyLLDBIndexCachePath;
126 return GetPropertyAtIndexAs<FileSpec>(idx, {});
127}
128
130 const uint32_t idx = ePropertyLLDBIndexCachePath;
131 return SetPropertyAtIndex(idx, path);
132}
133
135 const uint32_t idx = ePropertyEnableLLDBIndexCache;
136 return GetPropertyAtIndexAs<bool>(
137 idx, g_modulelist_properties[idx].default_uint_value != 0);
138}
139
141 return SetPropertyAtIndex(ePropertyEnableLLDBIndexCache, new_value);
142}
143
145 const uint32_t idx = ePropertyLLDBIndexCacheMaxByteSize;
146 return GetPropertyAtIndexAs<uint64_t>(
147 idx, g_modulelist_properties[idx].default_uint_value);
148}
149
151 const uint32_t idx = ePropertyLLDBIndexCacheMaxPercent;
152 return GetPropertyAtIndexAs<uint64_t>(
153 idx, g_modulelist_properties[idx].default_uint_value);
154}
155
157 const uint32_t idx = ePropertyLLDBIndexCacheExpirationDays;
158 return GetPropertyAtIndexAs<uint64_t>(
159 idx, g_modulelist_properties[idx].default_uint_value);
160}
161
163 FileSpecList list =
164 GetPropertyAtIndexAs<FileSpecList>(ePropertySymLinkPaths, {});
165 llvm::sys::ScopedWriter lock(m_symlink_paths_mutex);
166 const bool notify = false;
167 m_symlink_paths.Clear(notify);
168 for (FileSpec symlink : list) {
169 FileSpec resolved;
170 Status status = FileSystem::Instance().Readlink(symlink, resolved);
171 if (status.Success())
172 m_symlink_paths.Append(symlink.GetPath(), resolved.GetPath(), notify);
173 }
174}
175
177 llvm::sys::ScopedReader lock(m_symlink_paths_mutex);
178 return m_symlink_paths;
179}
180
182 const uint32_t idx = ePropertyLoadSymbolOnDemand;
183 return GetPropertyAtIndexAs<bool>(
184 idx, g_modulelist_properties[idx].default_uint_value != 0);
185}
186
187ModuleList::ModuleList() : m_modules(), m_modules_mutex() {}
188
189ModuleList::ModuleList(const ModuleList &rhs) : m_modules(), m_modules_mutex() {
190 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
191 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
192 m_modules = rhs.m_modules;
193}
194
196 : m_modules(), m_modules_mutex(), m_notifier(notifier) {}
197
199 if (this != &rhs) {
200 std::lock(m_modules_mutex, rhs.m_modules_mutex);
201 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex,
202 std::adopt_lock);
203 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex,
204 std::adopt_lock);
205 m_modules = rhs.m_modules;
206 }
207 return *this;
208}
209
210ModuleList::~ModuleList() = default;
211
212void ModuleList::AppendImpl(const ModuleSP &module_sp, bool use_notifier) {
213 if (module_sp) {
214 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
215 m_modules.push_back(module_sp);
216 if (use_notifier && m_notifier)
217 m_notifier->NotifyModuleAdded(*this, module_sp);
218 }
219}
220
221void ModuleList::Append(const ModuleSP &module_sp, bool notify) {
222 AppendImpl(module_sp, notify);
223}
224
226 const ModuleSP &module_sp,
228 if (module_sp) {
229 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
230
231 // First remove any equivalent modules. Equivalent modules are modules
232 // whose path, platform path and architecture match.
233 ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
234 module_sp->GetArchitecture());
235 equivalent_module_spec.GetPlatformFileSpec() =
236 module_sp->GetPlatformFileSpec();
237
238 size_t idx = 0;
239 while (idx < m_modules.size()) {
240 ModuleSP test_module_sp(m_modules[idx]);
241 if (test_module_sp->MatchesModuleSpec(equivalent_module_spec)) {
242 if (old_modules)
243 old_modules->push_back(test_module_sp);
244 RemoveImpl(m_modules.begin() + idx);
245 } else {
246 ++idx;
247 }
248 }
249 // Now add the new module to the list
250 Append(module_sp);
251 }
252}
253
254bool ModuleList::AppendIfNeeded(const ModuleSP &new_module, bool notify) {
255 if (new_module) {
256 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
257 for (const ModuleSP &module_sp : m_modules) {
258 if (module_sp.get() == new_module.get())
259 return false; // Already in the list
260 }
261 // Only push module_sp on the list if it wasn't already in there.
262 Append(new_module, notify);
263 return true;
264 }
265 return false;
266}
267
268void ModuleList::Append(const ModuleList &module_list) {
269 for (auto pos : module_list.m_modules)
270 Append(pos);
271}
272
273bool ModuleList::AppendIfNeeded(const ModuleList &module_list) {
274 bool any_in = false;
275 for (auto pos : module_list.m_modules) {
276 if (AppendIfNeeded(pos))
277 any_in = true;
278 }
279 return any_in;
280}
281
282bool ModuleList::RemoveImpl(const ModuleSP &module_sp, bool use_notifier) {
283 if (module_sp) {
284 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
285 collection::iterator pos, end = m_modules.end();
286 for (pos = m_modules.begin(); pos != end; ++pos) {
287 if (pos->get() == module_sp.get()) {
288 m_modules.erase(pos);
289 if (use_notifier && m_notifier)
290 m_notifier->NotifyModuleRemoved(*this, module_sp);
291 return true;
292 }
293 }
294 }
295 return false;
296}
297
298ModuleList::collection::iterator
299ModuleList::RemoveImpl(ModuleList::collection::iterator pos,
300 bool use_notifier) {
301 ModuleSP module_sp(*pos);
302 collection::iterator retval = m_modules.erase(pos);
303 if (use_notifier && m_notifier)
304 m_notifier->NotifyModuleRemoved(*this, module_sp);
305 return retval;
306}
307
308bool ModuleList::Remove(const ModuleSP &module_sp, bool notify) {
309 return RemoveImpl(module_sp, notify);
310}
311
312bool ModuleList::ReplaceModule(const lldb::ModuleSP &old_module_sp,
313 const lldb::ModuleSP &new_module_sp) {
314 if (!RemoveImpl(old_module_sp, false))
315 return false;
316 AppendImpl(new_module_sp, false);
317 if (m_notifier)
318 m_notifier->NotifyModuleUpdated(*this, old_module_sp, new_module_sp);
319 return true;
320}
321
322bool ModuleList::RemoveIfOrphaned(const Module *module_ptr) {
323 if (module_ptr) {
324 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
325 collection::iterator pos, end = m_modules.end();
326 for (pos = m_modules.begin(); pos != end; ++pos) {
327 if (pos->get() == module_ptr) {
328 if (pos->unique()) {
329 pos = RemoveImpl(pos);
330 return true;
331 } else
332 return false;
333 }
334 }
335 }
336 return false;
337}
338
339size_t ModuleList::RemoveOrphans(bool mandatory) {
340 std::unique_lock<std::recursive_mutex> lock(m_modules_mutex, std::defer_lock);
341
342 if (mandatory) {
343 lock.lock();
344 } else {
345 // Not mandatory, remove orphans if we can get the mutex
346 if (!lock.try_lock())
347 return 0;
348 }
349 size_t remove_count = 0;
350 // Modules might hold shared pointers to other modules, so removing one
351 // module might make other other modules orphans. Keep removing modules until
352 // there are no further modules that can be removed.
353 bool made_progress = true;
354 while (made_progress) {
355 // Keep track if we make progress this iteration.
356 made_progress = false;
357 collection::iterator pos = m_modules.begin();
358 while (pos != m_modules.end()) {
359 if (pos->unique()) {
360 pos = RemoveImpl(pos);
361 ++remove_count;
362 // We did make progress.
363 made_progress = true;
364 } else {
365 ++pos;
366 }
367 }
368 }
369 return remove_count;
370}
371
372size_t ModuleList::Remove(ModuleList &module_list) {
373 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
374 size_t num_removed = 0;
375 collection::iterator pos, end = module_list.m_modules.end();
376 for (pos = module_list.m_modules.begin(); pos != end; ++pos) {
377 if (Remove(*pos, false /* notify */))
378 ++num_removed;
379 }
380 if (m_notifier)
381 m_notifier->NotifyModulesRemoved(module_list);
382 return num_removed;
383}
384
386
388
389void ModuleList::ClearImpl(bool use_notifier) {
390 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
391 if (use_notifier && m_notifier)
393 m_modules.clear();
394}
395
397 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
398 if (idx < m_modules.size())
399 return m_modules[idx].get();
400 return nullptr;
401}
402
403ModuleSP ModuleList::GetModuleAtIndex(size_t idx) const {
404 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
405 return GetModuleAtIndexUnlocked(idx);
406}
407
408ModuleSP ModuleList::GetModuleAtIndexUnlocked(size_t idx) const {
409 ModuleSP module_sp;
410 if (idx < m_modules.size())
411 module_sp = m_modules[idx];
412 return module_sp;
413}
414
416 FunctionNameType name_type_mask,
417 const ModuleFunctionSearchOptions &options,
418 SymbolContextList &sc_list) const {
419 const size_t old_size = sc_list.GetSize();
420
421 if (name_type_mask & eFunctionNameTypeAuto) {
422 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
423
424 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
425 for (const ModuleSP &module_sp : m_modules) {
426 module_sp->FindFunctions(lookup_info, CompilerDeclContext(), options,
427 sc_list);
428 }
429
430 const size_t new_size = sc_list.GetSize();
431
432 if (old_size < new_size)
433 lookup_info.Prune(sc_list, old_size);
434 } else {
435 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
436 for (const ModuleSP &module_sp : m_modules) {
437 module_sp->FindFunctions(name, CompilerDeclContext(), name_type_mask,
438 options, sc_list);
439 }
440 }
441}
442
444 lldb::FunctionNameType name_type_mask,
445 SymbolContextList &sc_list) {
446 const size_t old_size = sc_list.GetSize();
447
448 if (name_type_mask & eFunctionNameTypeAuto) {
449 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
450
451 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
452 for (const ModuleSP &module_sp : m_modules) {
453 module_sp->FindFunctionSymbols(lookup_info.GetLookupName(),
454 lookup_info.GetNameTypeMask(), sc_list);
455 }
456
457 const size_t new_size = sc_list.GetSize();
458
459 if (old_size < new_size)
460 lookup_info.Prune(sc_list, old_size);
461 } else {
462 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
463 for (const ModuleSP &module_sp : m_modules) {
464 module_sp->FindFunctionSymbols(name, name_type_mask, sc_list);
465 }
466 }
467}
468
470 const ModuleFunctionSearchOptions &options,
471 SymbolContextList &sc_list) {
472 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
473 for (const ModuleSP &module_sp : m_modules)
474 module_sp->FindFunctions(name, options, sc_list);
475}
476
478 SymbolContextList &sc_list) const {
479 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
480 for (const ModuleSP &module_sp : m_modules)
481 module_sp->FindCompileUnits(path, sc_list);
482}
483
484void ModuleList::FindGlobalVariables(ConstString name, size_t max_matches,
485 VariableList &variable_list) const {
486 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
487 for (const ModuleSP &module_sp : m_modules) {
488 module_sp->FindGlobalVariables(name, CompilerDeclContext(), max_matches,
489 variable_list);
490 }
491}
492
494 size_t max_matches,
495 VariableList &variable_list) const {
496 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
497 for (const ModuleSP &module_sp : m_modules)
498 module_sp->FindGlobalVariables(regex, max_matches, variable_list);
499}
500
502 SymbolType symbol_type,
503 SymbolContextList &sc_list) const {
504 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
505 for (const ModuleSP &module_sp : m_modules)
506 module_sp->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
507}
508
510 const RegularExpression &regex, lldb::SymbolType symbol_type,
511 SymbolContextList &sc_list) const {
512 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
513 for (const ModuleSP &module_sp : m_modules)
514 module_sp->FindSymbolsMatchingRegExAndType(regex, symbol_type, sc_list);
515}
516
517void ModuleList::FindModules(const ModuleSpec &module_spec,
518 ModuleList &matching_module_list) const {
519 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
520 for (const ModuleSP &module_sp : m_modules) {
521 if (module_sp->MatchesModuleSpec(module_spec))
522 matching_module_list.Append(module_sp);
523 }
524}
525
526ModuleSP ModuleList::FindModule(const Module *module_ptr) const {
527 ModuleSP module_sp;
528
529 // Scope for "locker"
530 {
531 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
532 collection::const_iterator pos, end = m_modules.end();
533
534 for (pos = m_modules.begin(); pos != end; ++pos) {
535 if ((*pos).get() == module_ptr) {
536 module_sp = (*pos);
537 break;
538 }
539 }
540 }
541 return module_sp;
542}
543
544ModuleSP ModuleList::FindModule(const UUID &uuid) const {
545 ModuleSP module_sp;
546
547 if (uuid.IsValid()) {
548 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
549 collection::const_iterator pos, end = m_modules.end();
550
551 for (pos = m_modules.begin(); pos != end; ++pos) {
552 if ((*pos)->GetUUID() == uuid) {
553 module_sp = (*pos);
554 break;
555 }
556 }
557 }
558 return module_sp;
559}
560
561void ModuleList::FindTypes(Module *search_first, ConstString name,
562 bool name_is_fully_qualified, size_t max_matches,
563 llvm::DenseSet<SymbolFile *> &searched_symbol_files,
564 TypeList &types) const {
565 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
566
567 collection::const_iterator pos, end = m_modules.end();
568 if (search_first) {
569 for (pos = m_modules.begin(); pos != end; ++pos) {
570 if (search_first == pos->get()) {
571 search_first->FindTypes(name, name_is_fully_qualified, max_matches,
572 searched_symbol_files, types);
573
574 if (types.GetSize() >= max_matches)
575 return;
576 }
577 }
578 }
579
580 for (pos = m_modules.begin(); pos != end; ++pos) {
581 // Search the module if the module is not equal to the one in the symbol
582 // context "sc". If "sc" contains a empty module shared pointer, then the
583 // comparison will always be true (valid_module_ptr != nullptr).
584 if (search_first != pos->get())
585 (*pos)->FindTypes(name, name_is_fully_qualified, max_matches,
586 searched_symbol_files, types);
587
588 if (types.GetSize() >= max_matches)
589 return;
590 }
591}
592
594 FileSpec &new_spec) const {
595 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
596 for (const ModuleSP &module_sp : m_modules) {
597 if (module_sp->FindSourceFile(orig_spec, new_spec))
598 return true;
599 }
600 return false;
601}
602
603void ModuleList::FindAddressesForLine(const lldb::TargetSP target_sp,
604 const FileSpec &file, uint32_t line,
605 Function *function,
606 std::vector<Address> &output_local,
607 std::vector<Address> &output_extern) {
608 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
609 for (const ModuleSP &module_sp : m_modules) {
610 module_sp->FindAddressesForLine(target_sp, file, line, function,
611 output_local, output_extern);
612 }
613}
614
615ModuleSP ModuleList::FindFirstModule(const ModuleSpec &module_spec) const {
616 ModuleSP module_sp;
617 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
618 collection::const_iterator pos, end = m_modules.end();
619 for (pos = m_modules.begin(); pos != end; ++pos) {
620 ModuleSP module_sp(*pos);
621 if (module_sp->MatchesModuleSpec(module_spec))
622 return module_sp;
623 }
624 return module_sp;
625}
626
627size_t ModuleList::GetSize() const {
628 size_t size = 0;
629 {
630 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
631 size = m_modules.size();
632 }
633 return size;
634}
635
636void ModuleList::Dump(Stream *s) const {
637 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
638 for (const ModuleSP &module_sp : m_modules)
639 module_sp->Dump(s);
640}
641
642void ModuleList::LogUUIDAndPaths(Log *log, const char *prefix_cstr) {
643 if (log != nullptr) {
644 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
645 collection::const_iterator pos, begin = m_modules.begin(),
646 end = m_modules.end();
647 for (pos = begin; pos != end; ++pos) {
648 Module *module = pos->get();
649 const FileSpec &module_file_spec = module->GetFileSpec();
650 LLDB_LOGF(log, "%s[%u] %s (%s) \"%s\"", prefix_cstr ? prefix_cstr : "",
651 (uint32_t)std::distance(begin, pos),
652 module->GetUUID().GetAsString().c_str(),
654 module_file_spec.GetPath().c_str());
655 }
656 }
657}
658
660 Address &so_addr) const {
661 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
662 for (const ModuleSP &module_sp : m_modules) {
663 if (module_sp->ResolveFileAddress(vm_addr, so_addr))
664 return true;
665 }
666
667 return false;
668}
669
672 SymbolContextItem resolve_scope,
673 SymbolContext &sc) const {
674 // The address is already section offset so it has a module
675 uint32_t resolved_flags = 0;
676 ModuleSP module_sp(so_addr.GetModule());
677 if (module_sp) {
678 resolved_flags =
679 module_sp->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
680 } else {
681 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
682 collection::const_iterator pos, end = m_modules.end();
683 for (pos = m_modules.begin(); pos != end; ++pos) {
684 resolved_flags =
685 (*pos)->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
686 if (resolved_flags != 0)
687 break;
688 }
689 }
690
691 return resolved_flags;
692}
693
695 const char *file_path, uint32_t line, bool check_inlines,
696 SymbolContextItem resolve_scope, SymbolContextList &sc_list) const {
697 FileSpec file_spec(file_path);
698 return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
699 resolve_scope, sc_list);
700}
701
703 const FileSpec &file_spec, uint32_t line, bool check_inlines,
704 SymbolContextItem resolve_scope, SymbolContextList &sc_list) const {
705 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
706 for (const ModuleSP &module_sp : m_modules) {
707 module_sp->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
708 resolve_scope, sc_list);
709 }
710
711 return sc_list.GetSize();
712}
713
714size_t ModuleList::GetIndexForModule(const Module *module) const {
715 if (module) {
716 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
717 collection::const_iterator pos;
718 collection::const_iterator begin = m_modules.begin();
719 collection::const_iterator end = m_modules.end();
720 for (pos = begin; pos != end; ++pos) {
721 if ((*pos).get() == module)
722 return std::distance(begin, pos);
723 }
724 }
726}
727
728namespace {
729struct SharedModuleListInfo {
730 ModuleList module_list;
731 ModuleListProperties module_list_properties;
732};
733}
734static SharedModuleListInfo &GetSharedModuleListInfo()
735{
736 static SharedModuleListInfo *g_shared_module_list_info = nullptr;
737 static llvm::once_flag g_once_flag;
738 llvm::call_once(g_once_flag, []() {
739 // NOTE: Intentionally leak the module list so a program doesn't have to
740 // cleanup all modules and object files as it exits. This just wastes time
741 // doing a bunch of cleanup that isn't required.
742 if (g_shared_module_list_info == nullptr)
743 g_shared_module_list_info = new SharedModuleListInfo();
744 });
745 return *g_shared_module_list_info;
746}
747
749 return GetSharedModuleListInfo().module_list;
750}
751
753 return GetSharedModuleListInfo().module_list_properties;
754}
755
756bool ModuleList::ModuleIsInCache(const Module *module_ptr) {
757 if (module_ptr) {
758 ModuleList &shared_module_list = GetSharedModuleList();
759 return shared_module_list.FindModule(module_ptr).get() != nullptr;
760 }
761 return false;
762}
763
765 ModuleList &matching_module_list) {
766 GetSharedModuleList().FindModules(module_spec, matching_module_list);
767}
768
769lldb::ModuleSP ModuleList::FindSharedModule(const UUID &uuid) {
770 return GetSharedModuleList().FindModule(uuid);
771}
772
774 return GetSharedModuleList().RemoveOrphans(mandatory);
775}
776
777Status
778ModuleList::GetSharedModule(const ModuleSpec &module_spec, ModuleSP &module_sp,
779 const FileSpecList *module_search_paths_ptr,
781 bool *did_create_ptr, bool always_create) {
782 ModuleList &shared_module_list = GetSharedModuleList();
783 std::lock_guard<std::recursive_mutex> guard(
784 shared_module_list.m_modules_mutex);
785 char path[PATH_MAX];
786
788
789 module_sp.reset();
790
791 if (did_create_ptr)
792 *did_create_ptr = false;
793
794 const UUID *uuid_ptr = module_spec.GetUUIDPtr();
795 const FileSpec &module_file_spec = module_spec.GetFileSpec();
796 const ArchSpec &arch = module_spec.GetArchitecture();
797
798 // Make sure no one else can try and get or create a module while this
799 // function is actively working on it by doing an extra lock on the global
800 // mutex list.
801 if (!always_create) {
802 ModuleList matching_module_list;
803 shared_module_list.FindModules(module_spec, matching_module_list);
804 const size_t num_matching_modules = matching_module_list.GetSize();
805
806 if (num_matching_modules > 0) {
807 for (size_t module_idx = 0; module_idx < num_matching_modules;
808 ++module_idx) {
809 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
810
811 // Make sure the file for the module hasn't been modified
812 if (module_sp->FileHasChanged()) {
813 if (old_modules)
814 old_modules->push_back(module_sp);
815
817 if (log != nullptr)
818 LLDB_LOGF(
819 log, "%p '%s' module changed: removing from global module list",
820 static_cast<void *>(module_sp.get()),
821 module_sp->GetFileSpec().GetFilename().GetCString());
822
823 shared_module_list.Remove(module_sp);
824 module_sp.reset();
825 } else {
826 // The module matches and the module was not modified from when it
827 // was last loaded.
828 return error;
829 }
830 }
831 }
832 }
833
834 if (module_sp)
835 return error;
836
837 module_sp = std::make_shared<Module>(module_spec);
838 // Make sure there are a module and an object file since we can specify a
839 // valid file path with an architecture that might not be in that file. By
840 // getting the object file we can guarantee that the architecture matches
841 if (module_sp->GetObjectFile()) {
842 // If we get in here we got the correct arch, now we just need to verify
843 // the UUID if one was given
844 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
845 module_sp.reset();
846 } else {
847 if (module_sp->GetObjectFile() &&
848 module_sp->GetObjectFile()->GetType() ==
850 module_sp.reset();
851 } else {
852 if (did_create_ptr) {
853 *did_create_ptr = true;
854 }
855
856 shared_module_list.ReplaceEquivalent(module_sp, old_modules);
857 return error;
858 }
859 }
860 } else {
861 module_sp.reset();
862 }
863
864 if (module_search_paths_ptr) {
865 const auto num_directories = module_search_paths_ptr->GetSize();
866 for (size_t idx = 0; idx < num_directories; ++idx) {
867 auto search_path_spec = module_search_paths_ptr->GetFileSpecAtIndex(idx);
868 FileSystem::Instance().Resolve(search_path_spec);
869 namespace fs = llvm::sys::fs;
870 if (!FileSystem::Instance().IsDirectory(search_path_spec))
871 continue;
872 search_path_spec.AppendPathComponent(
873 module_spec.GetFileSpec().GetFilename().GetStringRef());
874 if (!FileSystem::Instance().Exists(search_path_spec))
875 continue;
876
877 auto resolved_module_spec(module_spec);
878 resolved_module_spec.GetFileSpec() = search_path_spec;
879 module_sp = std::make_shared<Module>(resolved_module_spec);
880 if (module_sp->GetObjectFile()) {
881 // If we get in here we got the correct arch, now we just need to
882 // verify the UUID if one was given
883 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
884 module_sp.reset();
885 } else {
886 if (module_sp->GetObjectFile()->GetType() ==
888 module_sp.reset();
889 } else {
890 if (did_create_ptr)
891 *did_create_ptr = true;
892
893 shared_module_list.ReplaceEquivalent(module_sp, old_modules);
894 return Status();
895 }
896 }
897 } else {
898 module_sp.reset();
899 }
900 }
901 }
902
903 // Either the file didn't exist where at the path, or no path was given, so
904 // we now have to use more extreme measures to try and find the appropriate
905 // module.
906
907 // Fixup the incoming path in case the path points to a valid file, yet the
908 // arch or UUID (if one was passed in) don't match.
909 ModuleSpec located_binary_modulespec =
911
912 // Don't look for the file if it appears to be the same one we already
913 // checked for above...
914 if (located_binary_modulespec.GetFileSpec() != module_file_spec) {
915 if (!FileSystem::Instance().Exists(
916 located_binary_modulespec.GetFileSpec())) {
917 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
918 if (path[0] == '\0')
919 module_file_spec.GetPath(path, sizeof(path));
920 // How can this check ever be true? This branch it is false, and we
921 // haven't modified file_spec.
922 if (FileSystem::Instance().Exists(
923 located_binary_modulespec.GetFileSpec())) {
924 std::string uuid_str;
925 if (uuid_ptr && uuid_ptr->IsValid())
926 uuid_str = uuid_ptr->GetAsString();
927
928 if (arch.IsValid()) {
929 if (!uuid_str.empty())
930 error.SetErrorStringWithFormat(
931 "'%s' does not contain the %s architecture and UUID %s", path,
932 arch.GetArchitectureName(), uuid_str.c_str());
933 else
934 error.SetErrorStringWithFormat(
935 "'%s' does not contain the %s architecture.", path,
936 arch.GetArchitectureName());
937 }
938 } else {
939 error.SetErrorStringWithFormat("'%s' does not exist", path);
940 }
941 if (error.Fail())
942 module_sp.reset();
943 return error;
944 }
945
946 // Make sure no one else can try and get or create a module while this
947 // function is actively working on it by doing an extra lock on the global
948 // mutex list.
949 ModuleSpec platform_module_spec(module_spec);
950 platform_module_spec.GetFileSpec() =
951 located_binary_modulespec.GetFileSpec();
952 platform_module_spec.GetPlatformFileSpec() =
953 located_binary_modulespec.GetFileSpec();
954 platform_module_spec.GetSymbolFileSpec() =
955 located_binary_modulespec.GetSymbolFileSpec();
956 ModuleList matching_module_list;
957 shared_module_list.FindModules(platform_module_spec, matching_module_list);
958 if (!matching_module_list.IsEmpty()) {
959 module_sp = matching_module_list.GetModuleAtIndex(0);
960
961 // If we didn't have a UUID in mind when looking for the object file,
962 // then we should make sure the modification time hasn't changed!
963 if (platform_module_spec.GetUUIDPtr() == nullptr) {
964 auto file_spec_mod_time = FileSystem::Instance().GetModificationTime(
965 located_binary_modulespec.GetFileSpec());
966 if (file_spec_mod_time != llvm::sys::TimePoint<>()) {
967 if (file_spec_mod_time != module_sp->GetModificationTime()) {
968 if (old_modules)
969 old_modules->push_back(module_sp);
970 shared_module_list.Remove(module_sp);
971 module_sp.reset();
972 }
973 }
974 }
975 }
976
977 if (!module_sp) {
978 module_sp = std::make_shared<Module>(platform_module_spec);
979 // Make sure there are a module and an object file since we can specify a
980 // valid file path with an architecture that might not be in that file.
981 // By getting the object file we can guarantee that the architecture
982 // matches
983 if (module_sp && module_sp->GetObjectFile()) {
984 if (module_sp->GetObjectFile()->GetType() ==
986 module_sp.reset();
987 } else {
988 if (did_create_ptr)
989 *did_create_ptr = true;
990
991 shared_module_list.ReplaceEquivalent(module_sp, old_modules);
992 }
993 } else {
994 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
995
996 if (located_binary_modulespec.GetFileSpec()) {
997 if (arch.IsValid())
998 error.SetErrorStringWithFormat(
999 "unable to open %s architecture in '%s'",
1000 arch.GetArchitectureName(), path);
1001 else
1002 error.SetErrorStringWithFormat("unable to open '%s'", path);
1003 } else {
1004 std::string uuid_str;
1005 if (uuid_ptr && uuid_ptr->IsValid())
1006 uuid_str = uuid_ptr->GetAsString();
1007
1008 if (!uuid_str.empty())
1009 error.SetErrorStringWithFormat(
1010 "cannot locate a module for UUID '%s'", uuid_str.c_str());
1011 else
1012 error.SetErrorString("cannot locate a module");
1013 }
1014 }
1015 }
1016 }
1017
1018 return error;
1019}
1020
1021bool ModuleList::RemoveSharedModule(lldb::ModuleSP &module_sp) {
1022 return GetSharedModuleList().Remove(module_sp);
1023}
1024
1026 return GetSharedModuleList().RemoveIfOrphaned(module_ptr);
1027}
1028
1030 std::list<Status> &errors,
1031 Stream *feedback_stream,
1032 bool continue_on_error) {
1033 if (!target)
1034 return false;
1035 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
1036 for (auto module : m_modules) {
1037 Status error;
1038 if (module) {
1039 if (!module->LoadScriptingResourceInTarget(target, error,
1040 feedback_stream)) {
1041 if (error.Fail() && error.AsCString()) {
1042 error.SetErrorStringWithFormat("unable to load scripting data for "
1043 "module %s - error reported was %s",
1044 module->GetFileSpec()
1045 .GetFileNameStrippingExtension()
1046 .GetCString(),
1047 error.AsCString());
1048 errors.push_back(error);
1049
1050 if (!continue_on_error)
1051 return false;
1052 }
1053 }
1054 }
1055 }
1056 return errors.empty();
1057}
1058
1060 std::function<bool(const ModuleSP &module_sp)> const &callback) const {
1061 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
1062 for (const auto &module_sp : m_modules) {
1063 assert(module_sp != nullptr);
1064 // If the callback returns false, then stop iterating and break out
1065 if (!callback(module_sp))
1066 break;
1067 }
1068}
1069
1071 std::function<bool(lldb_private::Module &module_sp)> const &callback)
1072 const {
1073 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
1074 for (const auto &module_sp : m_modules) {
1075 assert(module_sp != nullptr);
1076 if (callback(*module_sp))
1077 return true;
1078 }
1079
1080 return false;
1081}
static llvm::raw_ostream & error(Stream &strm)
#define lldbassert(x)
Definition: LLDBAssert.h:13
#define LLDB_LOGF(log,...)
Definition: Log.h:344
static SharedModuleListInfo & GetSharedModuleListInfo()
Definition: ModuleList.cpp:734
static ModuleList & GetSharedModuleList()
Definition: ModuleList.cpp:748
A section + offset based address class.
Definition: Address.h:59
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:283
An architecture specification class.
Definition: ArchSpec.h:31
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:348
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
Definition: ArchSpec.cpp:552
Represents a generic declaration context in a program.
A uniqued constant string class.
Definition: ConstString.h:39
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:204
A file collection class.
Definition: FileSpecList.h:24
const FileSpec & GetFileSpecAtIndex(size_t idx) const
Get file at index.
size_t GetSize() const
Get the number of files in the file list.
A file utility class.
Definition: FileSpec.h:56
const ConstString & GetFilename() const
Filename string const get accessor.
Definition: FileSpec.h:240
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:366
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
llvm::sys::TimePoint GetModificationTime(const FileSpec &file_spec) const
Returns the modification time of the given file.
Status Readlink(const FileSpec &src, FileSpec &dst)
static FileSystem & Instance()
A class that describes a function.
Definition: Function.h:409
bool SetClangModulesCachePath(const FileSpec &path)
Definition: ModuleList.cpp:119
bool SetLLDBIndexCachePath(const FileSpec &path)
Definition: ModuleList.cpp:129
bool SetEnableExternalLookup(bool new_value)
Definition: ModuleList.cpp:104
FileSpec GetLLDBIndexCachePath() const
Definition: ModuleList.cpp:124
PathMappingList GetSymlinkMappings() const
Definition: ModuleList.cpp:176
FileSpec GetClangModulesCachePath() const
Definition: ModuleList.cpp:114
llvm::sys::RWMutex m_symlink_paths_mutex
Definition: ModuleList.h:51
bool SetEnableLLDBIndexCache(bool new_value)
Definition: ModuleList.cpp:140
virtual void NotifyModuleAdded(const ModuleList &module_list, const lldb::ModuleSP &module_sp)=0
virtual void NotifyModuleUpdated(const ModuleList &module_list, const lldb::ModuleSP &old_module_sp, const lldb::ModuleSP &new_module_sp)=0
virtual void NotifyModuleRemoved(const ModuleList &module_list, const lldb::ModuleSP &module_sp)=0
virtual void NotifyWillClearList(const ModuleList &module_list)=0
virtual void NotifyModulesRemoved(lldb_private::ModuleList &module_list)=0
A collection class for Module objects.
Definition: ModuleList.h:82
bool ReplaceModule(const lldb::ModuleSP &old_module_sp, const lldb::ModuleSP &new_module_sp)
Definition: ModuleList.cpp:312
collection m_modules
The collection of modules.
Definition: ModuleList.h:498
void ClearImpl(bool use_notifier=true)
Definition: ModuleList.cpp:389
void FindTypes(Module *search_first, ConstString name, bool name_is_fully_qualified, size_t max_matches, llvm::DenseSet< SymbolFile * > &searched_symbol_files, TypeList &types) const
Find types by name.
Definition: ModuleList.cpp:561
bool RemoveImpl(const lldb::ModuleSP &module_sp, bool use_notifier=true)
void ForEach(std::function< bool(const lldb::ModuleSP &module_sp)> const &callback) const
Applies 'callback' to each module in this ModuleList.
uint32_t ResolveSymbolContextsForFileSpec(const FileSpec &file_spec, uint32_t line, bool check_inlines, lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) const
Resolve items in the symbol context for a given file and line. (const FileSpec&,...
Definition: ModuleList.cpp:702
static bool RemoveSharedModule(lldb::ModuleSP &module_sp)
void FindFunctions(ConstString name, lldb::FunctionNameType name_type_mask, const ModuleFunctionSearchOptions &options, SymbolContextList &sc_list) const
bool FindSourceFile(const FileSpec &orig_spec, FileSpec &new_spec) const
Definition: ModuleList.cpp:593
bool AnyOf(std::function< bool(lldb_private::Module &module)> const &callback) const
Returns true if 'callback' returns true for one of the modules in this ModuleList.
static bool ModuleIsInCache(const Module *module_ptr)
Definition: ModuleList.cpp:756
void FindGlobalVariables(ConstString name, size_t max_matches, VariableList &variable_list) const
Find global and static variables by name.
Definition: ModuleList.cpp:484
size_t GetIndexForModule(const Module *module) const
Definition: ModuleList.cpp:714
lldb::ModuleSP FindFirstModule(const ModuleSpec &module_spec) const
Definition: ModuleList.cpp:615
void Clear()
Clear the object's state.
Definition: ModuleList.cpp:385
bool Remove(const lldb::ModuleSP &module_sp, bool notify=true)
Remove a module from the module list.
ModuleList()
Default constructor.
Definition: ModuleList.cpp:187
void Dump(Stream *s) const
Dump the description of each module contained in this list.
Definition: ModuleList.cpp:636
bool LoadScriptingResourcesInTarget(Target *target, std::list< Status > &errors, Stream *feedback_stream=nullptr, bool continue_on_error=true)
uint32_t ResolveSymbolContextForFilePath(const char *file_path, uint32_t line, bool check_inlines, lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) const
Resolve items in the symbol context for a given file and line. (const char*,uint32_t,...
Definition: ModuleList.cpp:694
static bool RemoveSharedModuleIfOrphaned(const Module *module_ptr)
lldb::ModuleSP GetModuleAtIndexUnlocked(size_t idx) const
Get the module shared pointer for the module at index idx without acquiring the ModuleList mutex.
Definition: ModuleList.cpp:408
void FindCompileUnits(const FileSpec &path, SymbolContextList &sc_list) const
Find compile units by partial or full path.
Definition: ModuleList.cpp:477
const ModuleList & operator=(const ModuleList &rhs)
Assignment operator.
Definition: ModuleList.cpp:198
std::recursive_mutex m_modules_mutex
Definition: ModuleList.h:499
static Status GetSharedModule(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp, const FileSpecList *module_search_paths_ptr, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr, bool always_create=false)
Definition: ModuleList.cpp:778
Module * GetModulePointerAtIndex(size_t idx) const
Get the module pointer for the module at index idx.
Definition: ModuleList.cpp:396
void FindSymbolsWithNameAndType(ConstString name, lldb::SymbolType symbol_type, SymbolContextList &sc_list) const
Definition: ModuleList.cpp:501
static lldb::ModuleSP FindSharedModule(const UUID &uuid)
Definition: ModuleList.cpp:769
lldb::ModuleSP FindModule(const Module *module_ptr) const
Definition: ModuleList.cpp:526
static ModuleListProperties & GetGlobalModuleListProperties()
Definition: ModuleList.cpp:752
void FindModules(const ModuleSpec &module_spec, ModuleList &matching_module_list) const
Finds the first module whose file specification matches file_spec.
Definition: ModuleList.cpp:517
void FindAddressesForLine(const lldb::TargetSP target_sp, const FileSpec &file, uint32_t line, Function *function, std::vector< Address > &output_local, std::vector< Address > &output_extern)
Find addresses by file/line.
Definition: ModuleList.cpp:603
uint32_t ResolveSymbolContextForAddress(const Address &so_addr, lldb::SymbolContextItem resolve_scope, SymbolContext &sc) const
Resolve the symbol context for the given address. (const Address&,uint32_t,SymbolContext&)
Definition: ModuleList.cpp:671
size_t RemoveOrphans(bool mandatory)
Definition: ModuleList.cpp:339
void Append(const lldb::ModuleSP &module_sp, bool notify=true)
Append a module to the module list.
lldb::ModuleSP GetModuleAtIndex(size_t idx) const
Get the module shared pointer for the module at index idx.
Definition: ModuleList.cpp:403
bool AppendIfNeeded(const lldb::ModuleSP &new_module, bool notify=true)
Append a module to the module list, if it is not already there.
static size_t RemoveOrphanSharedModules(bool mandatory)
Definition: ModuleList.cpp:773
void Destroy()
Clear the object's state.
Definition: ModuleList.cpp:387
bool RemoveIfOrphaned(const Module *module_ptr)
Definition: ModuleList.cpp:322
void AppendImpl(const lldb::ModuleSP &module_sp, bool use_notifier=true)
Definition: ModuleList.cpp:212
void ReplaceEquivalent(const lldb::ModuleSP &module_sp, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules=nullptr)
Append a module to the module list and remove any equivalent modules.
Definition: ModuleList.cpp:225
void FindFunctionSymbols(ConstString name, lldb::FunctionNameType name_type_mask, SymbolContextList &sc_list)
Definition: ModuleList.cpp:443
static void FindSharedModules(const ModuleSpec &module_spec, ModuleList &matching_module_list)
Definition: ModuleList.cpp:764
void FindSymbolsMatchingRegExAndType(const RegularExpression &regex, lldb::SymbolType symbol_type, SymbolContextList &sc_list) const
Definition: ModuleList.cpp:509
bool ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr) const
Definition: ModuleList.cpp:659
size_t GetSize() const
Gets the size of the module list.
Definition: ModuleList.cpp:627
void LogUUIDAndPaths(Log *log, const char *prefix_cstr)
Definition: ModuleList.cpp:642
FileSpec & GetPlatformFileSpec()
Definition: ModuleSpec.h:65
FileSpec & GetFileSpec()
Definition: ModuleSpec.h:53
ArchSpec & GetArchitecture()
Definition: ModuleSpec.h:89
FileSpec & GetSymbolFileSpec()
Definition: ModuleSpec.h:77
A class that encapsulates name lookup information.
Definition: Module.h:950
lldb::FunctionNameType GetNameTypeMask() const
Definition: Module.h:965
ConstString GetLookupName() const
Definition: Module.h:961
void Prune(SymbolContextList &sc_list, size_t start_idx) const
Definition: Module.cpp:768
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:88
const lldb_private::UUID & GetUUID()
Get a reference to the UUID value contained in this object.
Definition: Module.cpp:343
void FindTypes(ConstString type_name, bool exact_match, size_t max_matches, llvm::DenseSet< lldb_private::SymbolFile * > &searched_symbol_files, TypeList &types)
Find types by name.
Definition: Module.cpp:989
const ArchSpec & GetArchitecture() const
Get const accessor for the module architecture.
Definition: Module.cpp:1081
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition: Module.h:498
@ eTypeStubLibrary
A library that can be linked against but not used for execution.
Definition: ObjectFile.h:62
void Append(llvm::StringRef path, llvm::StringRef replacement, bool notify)
lldb::OptionValuePropertiesSP m_collection_sp
bool SetPropertyAtIndex(uint32_t idx, T t, const ExecutionContext *exe_ctx=nullptr) const
An error handling class.
Definition: Status.h:44
bool Success() const
Test for success condition.
Definition: Status.cpp:287
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
Defines a list of symbol context objects.
uint32_t GetSize() const
Get accessor for a symbol context list size.
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
Provides public interface for all SymbolFiles.
Definition: SymbolFile.h:49
static ModuleSpec LocateExecutableObjectFile(const ModuleSpec &module_spec)
uint32_t GetSize() const
Definition: TypeList.cpp:60
std::string GetAsString(llvm::StringRef separator="-") const
Definition: UUID.cpp:49
bool IsValid() const
Definition: UUID.h:69
#define LLDB_INVALID_INDEX32
Definition: lldb-defines.h:75
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:309
Definition: SBAddress.h:15
@ eLanguageTypeUnknown
Unknown or invalid language value.
SymbolType
Symbol types.
uint64_t addr_t
Definition: lldb-types.h:79
Options used by Module::FindFunctions.
Definition: Module.h:65
#define PATH_MAX