LLDB  mainline
ThreadSpec.cpp
Go to the documentation of this file.
1 //===-- ThreadSpec.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/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() : m_name(), m_queue_name() {}
21 
22 std::unique_ptr<ThreadSpec> ThreadSpec::CreateFromStructuredData(
23  const StructuredData::Dictionary &spec_dict, Status &error) {
24  uint32_t index = UINT32_MAX;
26  llvm::StringRef name;
27  llvm::StringRef queue_name;
28 
29  std::unique_ptr<ThreadSpec> thread_spec_up(new ThreadSpec());
30  bool success = spec_dict.GetValueForKeyAsInteger(
32  if (success)
33  thread_spec_up->SetIndex(index);
34 
35  success =
37  if (success)
38  thread_spec_up->SetTID(tid);
39 
40  success =
42  if (success)
43  thread_spec_up->SetName(name);
44 
46  queue_name);
47  if (success)
48  thread_spec_up->SetQueueName(queue_name);
49 
50  return thread_spec_up;
51 }
52 
55 
56  if (m_index != UINT32_MAX)
57  data_dict_sp->AddIntegerItem(GetKey(OptionNames::ThreadIndex), m_index);
59  data_dict_sp->AddIntegerItem(GetKey(OptionNames::ThreadID), m_tid);
60  if (!m_name.empty())
61  data_dict_sp->AddStringItem(GetKey(OptionNames::ThreadName), m_name);
62  if (!m_queue_name.empty())
63  data_dict_sp->AddStringItem(GetKey(OptionNames::QueueName), m_queue_name);
64 
65  return data_dict_sp;
66 }
67 
68 const char *ThreadSpec::GetName() const {
69  return m_name.empty() ? nullptr : m_name.c_str();
70 }
71 
72 const char *ThreadSpec::GetQueueName() const {
73  return m_queue_name.empty() ? nullptr : m_queue_name.c_str();
74 }
75 
76 bool ThreadSpec::TIDMatches(Thread &thread) const {
78  return true;
79 
80  lldb::tid_t thread_id = thread.GetID();
81  return TIDMatches(thread_id);
82 }
83 
84 bool ThreadSpec::IndexMatches(Thread &thread) const {
85  if (m_index == UINT32_MAX)
86  return true;
87  uint32_t index = thread.GetIndexID();
88  return IndexMatches(index);
89 }
90 
91 bool ThreadSpec::NameMatches(Thread &thread) const {
92  if (m_name.empty())
93  return true;
94 
95  const char *name = thread.GetName();
96  return NameMatches(name);
97 }
98 
99 bool ThreadSpec::QueueNameMatches(Thread &thread) const {
100  if (m_queue_name.empty())
101  return true;
102 
103  const char *queue_name = thread.GetQueueName();
104  return QueueNameMatches(queue_name);
105 }
106 
108  if (!HasSpecification())
109  return true;
110 
111  if (!TIDMatches(thread))
112  return false;
113 
114  if (!IndexMatches(thread))
115  return false;
116 
117  if (!NameMatches(thread))
118  return false;
119 
120  if (!QueueNameMatches(thread))
121  return false;
122 
123  return true;
124 }
125 
127  return (m_index != UINT32_MAX || m_tid != LLDB_INVALID_THREAD_ID ||
128  !m_name.empty() || !m_queue_name.empty());
129 }
130 
132  if (!HasSpecification()) {
133  if (level == eDescriptionLevelBrief) {
134  s->PutCString("thread spec: no ");
135  }
136  } else {
137  if (level == eDescriptionLevelBrief) {
138  s->PutCString("thread spec: yes ");
139  } else {
141  s->Printf("tid: 0x%" PRIx64 " ", GetTID());
142 
143  if (GetIndex() != UINT32_MAX)
144  s->Printf("index: %d ", GetIndex());
145 
146  const char *name = GetName();
147  if (name)
148  s->Printf("thread name: \"%s\" ", name);
149 
150  const char *queue_name = GetQueueName();
151  if (queue_name)
152  s->Printf("queue name: \"%s\" ", queue_name);
153  }
154  }
155 }
lldb_private::ThreadSpec::HasSpecification
bool HasSpecification() const
Definition: ThreadSpec.cpp:126
lldb_private::StructuredData::Dictionary
Definition: StructuredData.h:352
lldb_private::ThreadSpec::NameMatches
bool NameMatches(const char *name) const
Definition: ThreadSpec.h:81
lldb_private::Thread::GetIndexID
uint32_t GetIndexID() const
Definition: Thread.cpp:1387
StructuredData.h
lldb_private::ThreadSpec::SerializeToStructuredData
StructuredData::ObjectSP SerializeToStructuredData()
Definition: ThreadSpec.cpp:53
lldb_private::ThreadSpec::OptionNames::ThreadName
@ ThreadName
lldb_private::ThreadSpec::ThreadPassesBasicTests
bool ThreadPassesBasicTests(Thread &thread) const
Definition: ThreadSpec.cpp:107
lldb_private::Stream
Definition: Stream.h:28
lldb_private::ThreadSpec::TIDMatches
bool TIDMatches(lldb::tid_t thread_id) const
Definition: ThreadSpec.h:63
lldb_private::StructuredData::Dictionary::GetValueForKeyAsInteger
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
Definition: StructuredData.h:403
lldb_private::Thread::GetName
virtual const char * GetName()
Definition: Thread.h:279
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::ThreadSpec::GetKey
static const char * GetKey(OptionNames enum_value)
Definition: ThreadSpec.h:119
lldb_private::ThreadSpec::GetDescription
void GetDescription(Stream *s, lldb::DescriptionLevel level) const
Definition: ThreadSpec.cpp:131
lldb_private::ThreadSpec::m_tid
lldb::tid_t m_tid
Definition: ThreadSpec.h:124
lldb_private::Thread
Definition: Thread.h:60
ThreadSpec.h
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
lldb_private::ThreadSpec::IndexMatches
bool IndexMatches(uint32_t index) const
Definition: ThreadSpec.h:72
lldb_private::ThreadSpec::m_index
uint32_t m_index
Definition: ThreadSpec.h:123
lldb_private::ThreadSpec::QueueNameMatches
bool QueueNameMatches(const char *queue_name) const
Definition: ThreadSpec.h:92
lldb_private::StructuredData::DictionarySP
std::shared_ptr< Dictionary > DictionarySP
Definition: StructuredData.h:67
lldb_private::ThreadSpec::OptionNames::ThreadIndex
@ ThreadIndex
lldb_private::ThreadSpec::ThreadSpec
ThreadSpec()
Definition: ThreadSpec.cpp:20
Thread.h
lldb_private::UserID::GetID
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47
lldb_private::ThreadSpec::OptionNames::QueueName
@ QueueName
lldb_private::ThreadSpec::GetName
const char * GetName() const
Definition: ThreadSpec.cpp:68
lldb_private::ThreadSpec::GetIndex
uint32_t GetIndex() const
Definition: ThreadSpec.h:55
lldb_private::ThreadSpec::m_queue_name
std::string m_queue_name
Definition: ThreadSpec.h:126
lldb_private::Status
Definition: Status.h:44
lldb_private::ThreadSpec::GetQueueName
const char * GetQueueName() const
Definition: ThreadSpec.cpp:72
uint32_t
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ThreadSpec::m_name
std::string m_name
Definition: ThreadSpec.h:125
lldb_private::ThreadSpec::CreateFromStructuredData
static std::unique_ptr< ThreadSpec > CreateFromStructuredData(const StructuredData::Dictionary &data_dict, Status &error)
Definition: ThreadSpec.cpp:22
lldb_private::ThreadSpec::GetTID
lldb::tid_t GetTID() const
Definition: ThreadSpec.h:57
LLDB_INVALID_THREAD_ID
#define LLDB_INVALID_THREAD_ID
Definition: lldb-defines.h:94
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb_private::StructuredData::Dictionary::GetValueForKeyAsString
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
Definition: StructuredData.h:423
lldb_private::ThreadSpec::OptionNames::ThreadID
@ ThreadID
lldb
Definition: SBAddress.h:15
lldb::eDescriptionLevelBrief
@ eDescriptionLevelBrief
Definition: lldb-enumerations.h:208
lldb::DescriptionLevel
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
Definition: lldb-enumerations.h:207
lldb_private::Thread::GetQueueName
virtual const char * GetQueueName()
Retrieve the Queue name for the queue currently using this Thread.
Definition: Thread.h:331
lldb::tid_t
uint64_t tid_t
Definition: lldb-types.h:86