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