1 //===-- ThreadSpec.h --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_ThreadSpec_h_
11 #define liblldb_ThreadSpec_h_
12 
13 #include "lldb/Utility/StructuredData.h"
14 #include "lldb/lldb-private.h"
15 #include <string>
16 
17 namespace lldb_private {
18 
19 // Note: For now the thread spec has only fixed elements -
20 //   Thread ID
21 //   Thread Index
22 //   Thread Name
23 //   Thread Queue Name
24 //
25 //  But if we need more generality, we can hang a key/value map off of this
26 //  structure.
27 //  That's why the thread matches spec test is done as a virtual method in
28 //  Thread::MatchesSpec,
29 //  since it is the native thread that would know how to interpret the keys.
30 //  I was going to do the Queue Name this way out of sheer orneriness, but that
31 //  seems a
32 //  sufficiently general concept, so I put it in here on its own.
33 
34 class ThreadSpec {
35 public:
36   ThreadSpec();
37 
38   ThreadSpec(const ThreadSpec &rhs);
39 
40   const ThreadSpec &operator=(const ThreadSpec &rhs);
41 
42   static std::unique_ptr<ThreadSpec>
43   CreateFromStructuredData(const StructuredData::Dictionary &data_dict,
44                            Status &error);
45 
46   StructuredData::ObjectSP SerializeToStructuredData();
47 
GetSerializationKey()48   static const char *GetSerializationKey() { return "ThreadSpec"; }
49 
SetIndex(uint32_t index)50   void SetIndex(uint32_t index) { m_index = index; }
51 
SetTID(lldb::tid_t tid)52   void SetTID(lldb::tid_t tid) { m_tid = tid; }
53 
SetName(llvm::StringRef name)54   void SetName(llvm::StringRef name) { m_name = name; }
55 
SetQueueName(llvm::StringRef queue_name)56   void SetQueueName(llvm::StringRef queue_name) { m_queue_name = queue_name; }
57 
GetIndex()58   uint32_t GetIndex() const { return m_index; }
59 
GetTID()60   lldb::tid_t GetTID() const { return m_tid; }
61 
62   const char *GetName() const;
63 
64   const char *GetQueueName() const;
65 
TIDMatches(lldb::tid_t thread_id)66   bool TIDMatches(lldb::tid_t thread_id) const {
67     if (m_tid == LLDB_INVALID_THREAD_ID || thread_id == LLDB_INVALID_THREAD_ID)
68       return true;
69     else
70       return thread_id == m_tid;
71   }
72 
73   bool TIDMatches(Thread &thread) const;
74 
IndexMatches(uint32_t index)75   bool IndexMatches(uint32_t index) const {
76     if (m_index == UINT32_MAX || index == UINT32_MAX)
77       return true;
78     else
79       return index == m_index;
80   }
81 
82   bool IndexMatches(Thread &thread) const;
83 
NameMatches(const char * name)84   bool NameMatches(const char *name) const {
85     if (m_name.empty())
86       return true;
87     else if (name == nullptr)
88       return false;
89     else
90       return m_name == name;
91   }
92 
93   bool NameMatches(Thread &thread) const;
94 
QueueNameMatches(const char * queue_name)95   bool QueueNameMatches(const char *queue_name) const {
96     if (m_queue_name.empty())
97       return true;
98     else if (queue_name == nullptr)
99       return false;
100     else
101       return m_queue_name == queue_name;
102   }
103 
104   bool QueueNameMatches(Thread &thread) const;
105 
106   bool ThreadPassesBasicTests(Thread &thread) const;
107 
108   bool HasSpecification() const;
109 
110   void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
111 
112 private:
113   enum class OptionNames {
114     ThreadIndex = 0,
115     ThreadID,
116     ThreadName,
117     QueueName,
118     LastOptionName
119   };
120   static const char *g_option_names[(size_t)OptionNames::LastOptionName];
121 
GetKey(OptionNames enum_value)122   static const char *GetKey(OptionNames enum_value) {
123     return g_option_names[(size_t) enum_value];
124   }
125 
126   uint32_t m_index;
127   lldb::tid_t m_tid;
128   std::string m_name;
129   std::string m_queue_name;
130 };
131 
132 } // namespace lldb_private
133 
134 #endif // liblldb_ThreadSpec_h_
135