1 //===-- ModuleSpec.h --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ModuleSpec_h_
11 #define liblldb_ModuleSpec_h_
17 // Other libraries and framework includes
19 #include "lldb/Core/ArchSpec.h"
20 #include "lldb/Core/Stream.h"
21 #include "lldb/Core/UUID.h"
22 #include "lldb/Host/FileSpec.h"
23 #include "lldb/Host/Mutex.h"
24 #include "lldb/Target/PathMappingList.h"
26 namespace lldb_private {
45 ModuleSpec (const FileSpec &file_spec) :
53 m_object_size (file_spec.GetByteSize ()),
59 ModuleSpec (const FileSpec &file_spec, const ArchSpec &arch) :
67 m_object_size (file_spec.GetByteSize ()),
73 ModuleSpec (const ModuleSpec &rhs) :
75 m_platform_file (rhs.m_platform_file),
76 m_symbol_file (rhs.m_symbol_file),
79 m_object_name (rhs.m_object_name),
80 m_object_offset (rhs.m_object_offset),
81 m_object_size (rhs.m_object_size),
82 m_object_mod_time (rhs.m_object_mod_time),
83 m_source_mappings (rhs.m_source_mappings)
88 operator = (const ModuleSpec &rhs)
93 m_platform_file = rhs.m_platform_file;
94 m_symbol_file = rhs.m_symbol_file;
97 m_object_name = rhs.m_object_name;
98 m_object_offset = rhs.m_object_offset;
99 m_object_size = rhs.m_object_size;
100 m_object_mod_time = rhs.m_object_mod_time;
101 m_source_mappings = rhs.m_source_mappings;
109 return (m_file ? &m_file : nullptr);
113 GetFileSpecPtr () const
115 return (m_file ? &m_file : nullptr);
131 GetPlatformFileSpecPtr ()
133 return (m_platform_file ? &m_platform_file : nullptr);
137 GetPlatformFileSpecPtr () const
139 return (m_platform_file ? &m_platform_file : nullptr);
143 GetPlatformFileSpec ()
145 return m_platform_file;
149 GetPlatformFileSpec () const
151 return m_platform_file;
155 GetSymbolFileSpecPtr ()
157 return (m_symbol_file ? &m_symbol_file : nullptr);
161 GetSymbolFileSpecPtr () const
163 return (m_symbol_file ? &m_symbol_file : nullptr);
169 return m_symbol_file;
173 GetSymbolFileSpec () const
175 return m_symbol_file;
179 GetArchitecturePtr ()
181 return (m_arch.IsValid() ? &m_arch : nullptr);
185 GetArchitecturePtr () const
187 return (m_arch.IsValid() ? &m_arch : nullptr);
197 GetArchitecture () const
205 return (m_uuid.IsValid() ? &m_uuid : nullptr);
211 return (m_uuid.IsValid() ? &m_uuid : nullptr);
229 return m_object_name;
233 GetObjectName () const
235 return m_object_name;
239 GetObjectOffset () const
241 return m_object_offset;
245 SetObjectOffset (uint64_t object_offset)
247 m_object_offset = object_offset;
251 GetObjectSize () const
253 return m_object_size;
257 SetObjectSize (uint64_t object_size)
259 m_object_size = object_size;
263 GetObjectModificationTime ()
265 return m_object_mod_time;
269 GetObjectModificationTime () const
271 return m_object_mod_time;
275 GetSourceMappingList () const
277 return m_source_mappings;
284 m_platform_file.Clear();
285 m_symbol_file.Clear();
288 m_object_name.Clear();
291 m_source_mappings.Clear(false);
292 m_object_mod_time.Clear();
295 explicit operator bool () const
303 if (m_arch.IsValid())
305 if (m_uuid.IsValid())
311 if (m_object_mod_time.IsValid())
317 Dump (Stream &strm) const
319 bool dumped_something = false;
322 strm.PutCString("file = '");
324 strm.PutCString("'");
325 dumped_something = true;
329 if (dumped_something)
330 strm.PutCString(", ");
331 strm.PutCString("platform_file = '");
332 strm << m_platform_file;
333 strm.PutCString("'");
334 dumped_something = true;
338 if (dumped_something)
339 strm.PutCString(", ");
340 strm.PutCString("symbol_file = '");
341 strm << m_symbol_file;
342 strm.PutCString("'");
343 dumped_something = true;
345 if (m_arch.IsValid())
347 if (dumped_something)
348 strm.PutCString(", ");
349 strm.Printf("arch = ");
350 m_arch.DumpTriple(strm);
351 dumped_something = true;
353 if (m_uuid.IsValid())
355 if (dumped_something)
356 strm.PutCString(", ");
357 strm.PutCString("uuid = ");
359 dumped_something = true;
363 if (dumped_something)
364 strm.PutCString(", ");
365 strm.Printf("object_name = %s", m_object_name.GetCString());
366 dumped_something = true;
368 if (m_object_offset > 0)
370 if (dumped_something)
371 strm.PutCString(", ");
372 strm.Printf("object_offset = %" PRIu64, m_object_offset);
373 dumped_something = true;
375 if (m_object_size > 0)
377 if (dumped_something)
378 strm.PutCString(", ");
379 strm.Printf("object size = %" PRIu64, m_object_size);
380 dumped_something = true;
382 if (m_object_mod_time.IsValid())
384 if (dumped_something)
385 strm.PutCString(", ");
386 strm.Printf("object_mod_time = 0x%" PRIx64, m_object_mod_time.GetAsSecondsSinceJan1_1970());
391 Matches (const ModuleSpec &match_module_spec, bool exact_arch_match) const
393 if (match_module_spec.GetUUIDPtr() && match_module_spec.GetUUID() != GetUUID())
395 if (match_module_spec.GetObjectName() && match_module_spec.GetObjectName() != GetObjectName())
397 if (match_module_spec.GetFileSpecPtr())
399 const FileSpec &fspec = match_module_spec.GetFileSpec();
400 if (!FileSpec::Equal(fspec, GetFileSpec(), fspec.GetDirectory().IsEmpty() == false))
403 if (GetPlatformFileSpec() && match_module_spec.GetPlatformFileSpecPtr())
405 const FileSpec &fspec = match_module_spec.GetPlatformFileSpec();
406 if (!FileSpec::Equal(fspec, GetPlatformFileSpec(), fspec.GetDirectory().IsEmpty() == false))
410 // Only match the symbol file spec if there is one in this ModuleSpec
411 if (GetSymbolFileSpec() && match_module_spec.GetSymbolFileSpecPtr())
413 const FileSpec &fspec = match_module_spec.GetSymbolFileSpec();
414 if (!FileSpec::Equal(fspec, GetSymbolFileSpec(), fspec.GetDirectory().IsEmpty() == false))
418 if (match_module_spec.GetArchitecturePtr())
420 if (exact_arch_match)
422 if (!GetArchitecture().IsExactMatch(match_module_spec.GetArchitecture()))
427 if (!GetArchitecture().IsCompatibleMatch(match_module_spec.GetArchitecture()))
436 FileSpec m_platform_file;
437 FileSpec m_symbol_file;
440 ConstString m_object_name;
441 uint64_t m_object_offset;
442 uint64_t m_object_size;
443 TimeValue m_object_mod_time;
444 mutable PathMappingList m_source_mappings;
452 m_mutex(Mutex::eMutexTypeRecursive)
456 ModuleSpecList (const ModuleSpecList &rhs) :
458 m_mutex(Mutex::eMutexTypeRecursive)
460 Mutex::Locker lhs_locker(m_mutex);
461 Mutex::Locker rhs_locker(rhs.m_mutex);
462 m_specs = rhs.m_specs;
465 ~ModuleSpecList() = default;
468 operator = (const ModuleSpecList &rhs)
472 Mutex::Locker lhs_locker(m_mutex);
473 Mutex::Locker rhs_locker(rhs.m_mutex);
474 m_specs = rhs.m_specs;
482 Mutex::Locker locker(m_mutex);
483 return m_specs.size();
489 Mutex::Locker locker(m_mutex);
494 Append (const ModuleSpec &spec)
496 Mutex::Locker locker(m_mutex);
497 m_specs.push_back (spec);
501 Append (const ModuleSpecList &rhs)
503 Mutex::Locker lhs_locker(m_mutex);
504 Mutex::Locker rhs_locker(rhs.m_mutex);
505 m_specs.insert(m_specs.end(), rhs.m_specs.begin(), rhs.m_specs.end());
508 // The index "i" must be valid and this can't be used in
509 // multi-threaded code as no mutex lock is taken.
511 GetModuleSpecRefAtIndex (size_t i)
517 GetModuleSpecAtIndex (size_t i, ModuleSpec &module_spec) const
519 Mutex::Locker locker(m_mutex);
520 if (i < m_specs.size())
522 module_spec = m_specs[i];
530 FindMatchingModuleSpec (const ModuleSpec &module_spec, ModuleSpec &match_module_spec) const
532 Mutex::Locker locker(m_mutex);
533 bool exact_arch_match = true;
534 for (auto spec: m_specs)
536 if (spec.Matches(module_spec, exact_arch_match))
538 match_module_spec = spec;
543 // If there was an architecture, retry with a compatible arch
544 if (module_spec.GetArchitecturePtr())
546 exact_arch_match = false;
547 for (auto spec: m_specs)
549 if (spec.Matches(module_spec, exact_arch_match))
551 match_module_spec = spec;
556 match_module_spec.Clear();
561 FindMatchingModuleSpecs (const ModuleSpec &module_spec, ModuleSpecList &matching_list) const
563 Mutex::Locker locker(m_mutex);
564 bool exact_arch_match = true;
565 const size_t initial_match_count = matching_list.GetSize();
566 for (auto spec: m_specs)
568 if (spec.Matches(module_spec, exact_arch_match))
569 matching_list.Append (spec);
572 // If there was an architecture, retry with a compatible arch if no matches were found
573 if (module_spec.GetArchitecturePtr() && (initial_match_count == matching_list.GetSize()))
575 exact_arch_match = false;
576 for (auto spec: m_specs)
578 if (spec.Matches(module_spec, exact_arch_match))
579 matching_list.Append (spec);
582 return matching_list.GetSize() - initial_match_count;
588 Mutex::Locker locker(m_mutex);
590 for (auto spec: m_specs)
592 strm.Printf("[%u] ", idx);
600 typedef std::vector<ModuleSpec> collection; ///< The module collection type.
601 collection m_specs; ///< The collection of modules.
602 mutable Mutex m_mutex;
605 } // namespace lldb_private
607 #endif // liblldb_ModuleSpec_h_