ThreadSpec.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //===-- ThreadSpec.h --------------------------------------------*- 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. #ifndef LLDB_TARGET_THREADSPEC_H
  9. #define LLDB_TARGET_THREADSPEC_H
  10. #include "lldb/Utility/StructuredData.h"
  11. #include "lldb/lldb-private.h"
  12. #include <string>
  13. namespace lldb_private {
  14. // Note: For now the thread spec has only fixed elements -
  15. // Thread ID
  16. // Thread Index
  17. // Thread Name
  18. // Thread Queue Name
  19. //
  20. // But if we need more generality, we can hang a key/value map off of this
  21. // structure.
  22. // That's why the thread matches spec test is done as a virtual method in
  23. // Thread::MatchesSpec,
  24. // since it is the native thread that would know how to interpret the keys.
  25. // I was going to do the Queue Name this way out of sheer orneriness, but that
  26. // seems a
  27. // sufficiently general concept, so I put it in here on its own.
  28. class ThreadSpec {
  29. public:
  30. ThreadSpec();
  31. static std::unique_ptr<ThreadSpec>
  32. CreateFromStructuredData(const StructuredData::Dictionary &data_dict,
  33. Status &error);
  34. StructuredData::ObjectSP SerializeToStructuredData();
  35. static const char *GetSerializationKey() { return "ThreadSpec"; }
  36. void SetIndex(uint32_t index) { m_index = index; }
  37. void SetTID(lldb::tid_t tid) { m_tid = tid; }
  38. void SetName(llvm::StringRef name) { m_name = std::string(name); }
  39. void SetQueueName(llvm::StringRef queue_name) {
  40. m_queue_name = std::string(queue_name);
  41. }
  42. uint32_t GetIndex() const { return m_index; }
  43. lldb::tid_t GetTID() const { return m_tid; }
  44. const char *GetName() const;
  45. const char *GetQueueName() const;
  46. bool TIDMatches(lldb::tid_t thread_id) const {
  47. if (m_tid == LLDB_INVALID_THREAD_ID || thread_id == LLDB_INVALID_THREAD_ID)
  48. return true;
  49. else
  50. return thread_id == m_tid;
  51. }
  52. bool TIDMatches(Thread &thread) const;
  53. bool IndexMatches(uint32_t index) const {
  54. if (m_index == UINT32_MAX || index == UINT32_MAX)
  55. return true;
  56. else
  57. return index == m_index;
  58. }
  59. bool IndexMatches(Thread &thread) const;
  60. bool NameMatches(const char *name) const {
  61. if (m_name.empty())
  62. return true;
  63. else if (name == nullptr)
  64. return false;
  65. else
  66. return m_name == name;
  67. }
  68. bool NameMatches(Thread &thread) const;
  69. bool QueueNameMatches(const char *queue_name) const {
  70. if (m_queue_name.empty())
  71. return true;
  72. else if (queue_name == nullptr)
  73. return false;
  74. else
  75. return m_queue_name == queue_name;
  76. }
  77. bool QueueNameMatches(Thread &thread) const;
  78. bool ThreadPassesBasicTests(Thread &thread) const;
  79. bool HasSpecification() const;
  80. void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
  81. private:
  82. enum class OptionNames {
  83. ThreadIndex = 0,
  84. ThreadID,
  85. ThreadName,
  86. QueueName,
  87. LastOptionName
  88. };
  89. static const char *g_option_names[(size_t)OptionNames::LastOptionName];
  90. static const char *GetKey(OptionNames enum_value) {
  91. return g_option_names[(size_t) enum_value];
  92. }
  93. uint32_t m_index;
  94. lldb::tid_t m_tid;
  95. std::string m_name;
  96. std::string m_queue_name;
  97. };
  98. } // namespace lldb_private
  99. #endif // LLDB_TARGET_THREADSPEC_H