LLDB  mainline
ThreadSpec.cpp
Go to the documentation of this file.
1 //===-- ThreadSpec.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 
10 #include "lldb/Target/Thread.h"
12 
13 using namespace lldb;
14 using namespace lldb_private;
15 
16 const char *ThreadSpec::g_option_names[static_cast<uint32_t>(
17  ThreadSpec::OptionNames::LastOptionName)]{"Index", "ID", "Name",
18  "QueueName"};
19 
20 ThreadSpec::ThreadSpec()
21  : m_index(UINT32_MAX), m_tid(LLDB_INVALID_THREAD_ID), m_name(),
22  m_queue_name() {}
23 
25  : m_index(rhs.m_index), m_tid(rhs.m_tid), m_name(rhs.m_name),
26  m_queue_name(rhs.m_queue_name) {}
27 
29  m_index = rhs.m_index;
30  m_tid = rhs.m_tid;
31  m_name = rhs.m_name;
32  m_queue_name = rhs.m_queue_name;
33  return *this;
34 }
35 
36 std::unique_ptr<ThreadSpec> ThreadSpec::CreateFromStructuredData(
37  const StructuredData::Dictionary &spec_dict, Status &error) {
38  uint32_t index = UINT32_MAX;
40  llvm::StringRef name;
41  llvm::StringRef queue_name;
42 
43  std::unique_ptr<ThreadSpec> thread_spec_up(new ThreadSpec());
44  bool success = spec_dict.GetValueForKeyAsInteger(
45  GetKey(OptionNames::ThreadIndex), index);
46  if (success)
47  thread_spec_up->SetIndex(index);
48 
49  success =
50  spec_dict.GetValueForKeyAsInteger(GetKey(OptionNames::ThreadID), tid);
51  if (success)
52  thread_spec_up->SetTID(tid);
53 
54  success =
55  spec_dict.GetValueForKeyAsString(GetKey(OptionNames::ThreadName), name);
56  if (success)
57  thread_spec_up->SetName(name);
58 
59  success = spec_dict.GetValueForKeyAsString(GetKey(OptionNames::ThreadName),
60  queue_name);
61  if (success)
62  thread_spec_up->SetQueueName(queue_name);
63 
64  return thread_spec_up;
65 }
66 
69 
70  if (m_index != UINT32_MAX)
71  data_dict_sp->AddIntegerItem(GetKey(OptionNames::ThreadIndex), m_index);
72  if (m_tid != LLDB_INVALID_THREAD_ID)
73  data_dict_sp->AddIntegerItem(GetKey(OptionNames::ThreadID), m_tid);
74  if (!m_name.empty())
75  data_dict_sp->AddStringItem(GetKey(OptionNames::ThreadName), m_name);
76  if (!m_queue_name.empty())
77  data_dict_sp->AddStringItem(GetKey(OptionNames::QueueName), m_queue_name);
78 
79  return data_dict_sp;
80 }
81 
82 const char *ThreadSpec::GetName() const {
83  return m_name.empty() ? nullptr : m_name.c_str();
84 }
85 
86 const char *ThreadSpec::GetQueueName() const {
87  return m_queue_name.empty() ? nullptr : m_queue_name.c_str();
88 }
89 
90 bool ThreadSpec::TIDMatches(Thread &thread) const {
91  if (m_tid == LLDB_INVALID_THREAD_ID)
92  return true;
93 
94  lldb::tid_t thread_id = thread.GetID();
95  return TIDMatches(thread_id);
96 }
97 
98 bool ThreadSpec::IndexMatches(Thread &thread) const {
99  if (m_index == UINT32_MAX)
100  return true;
101  uint32_t index = thread.GetIndexID();
102  return IndexMatches(index);
103 }
104 
105 bool ThreadSpec::NameMatches(Thread &thread) const {
106  if (m_name.empty())
107  return true;
108 
109  const char *name = thread.GetName();
110  return NameMatches(name);
111 }
112 
114  if (m_queue_name.empty())
115  return true;
116 
117  const char *queue_name = thread.GetQueueName();
118  return QueueNameMatches(queue_name);
119 }
120 
122  if (!HasSpecification())
123  return true;
124 
125  if (!TIDMatches(thread))
126  return false;
127 
128  if (!IndexMatches(thread))
129  return false;
130 
131  if (!NameMatches(thread))
132  return false;
133 
134  if (!QueueNameMatches(thread))
135  return false;
136 
137  return true;
138 }
139 
141  return (m_index != UINT32_MAX || m_tid != LLDB_INVALID_THREAD_ID ||
142  !m_name.empty() || !m_queue_name.empty());
143 }
144 
146  if (!HasSpecification()) {
147  if (level == eDescriptionLevelBrief) {
148  s->PutCString("thread spec: no ");
149  }
150  } else {
151  if (level == eDescriptionLevelBrief) {
152  s->PutCString("thread spec: yes ");
153  } else {
155  s->Printf("tid: 0x%" PRIx64 " ", GetTID());
156 
157  if (GetIndex() != UINT32_MAX)
158  s->Printf("index: %d ", GetIndex());
159 
160  const char *name = GetName();
161  if (name)
162  s->Printf("thread name: \"%s\" ", name);
163 
164  const char *queue_name = GetQueueName();
165  if (queue_name)
166  s->Printf("queue name: \"%s\" ", queue_name);
167  }
168  }
169 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
bool NameMatches(const char *name) const
Definition: ThreadSpec.h:83
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:49
bool TIDMatches(lldb::tid_t thread_id) const
Definition: ThreadSpec.h:65
bool HasSpecification() const
Definition: ThreadSpec.cpp:140
StructuredData::ObjectSP SerializeToStructuredData()
Definition: ThreadSpec.cpp:67
bool IndexMatches(uint32_t index) const
Definition: ThreadSpec.h:74
const char * GetName() const
Definition: ThreadSpec.cpp:82
const char * GetQueueName() const
Definition: ThreadSpec.cpp:86
std::shared_ptr< Dictionary > DictionarySP
virtual const char * GetQueueName()
Retrieve the Queue name for the queue currently using this Thread.
Definition: Thread.h:329
#define UINT32_MAX
Definition: lldb-defines.h:31
static std::unique_ptr< ThreadSpec > CreateFromStructuredData(const StructuredData::Dictionary &data_dict, Status &error)
Definition: ThreadSpec.cpp:36
lldb::tid_t GetTID() const
Definition: ThreadSpec.h:59
virtual const char * GetName()
Definition: Thread.h:277
uint64_t tid_t
Definition: lldb-types.h:86
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
const ThreadSpec & operator=(const ThreadSpec &rhs)
Definition: ThreadSpec.cpp:28
bool QueueNameMatches(const char *queue_name) const
Definition: ThreadSpec.h:94
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
#define LLDB_INVALID_THREAD_ID
Definition: lldb-defines.h:93
bool ThreadPassesBasicTests(Thread &thread) const
Definition: ThreadSpec.cpp:121
void GetDescription(Stream *s, lldb::DescriptionLevel level) const
Definition: ThreadSpec.cpp:145
Definition: SBAddress.h:15
uint32_t GetIndex() const
Definition: ThreadSpec.h:57
std::shared_ptr< Object > ObjectSP
uint32_t GetIndexID() const
Definition: Thread.cpp:1515
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
An error handling class.
Definition: Status.h:44