1 //===-- ThreadSpec.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ThreadSpec.h"
11 #include "lldb/Target/Thread.h"
12 #include "lldb/Utility/StructuredData.h"
15 using namespace lldb_private;
17 const char *ThreadSpec::g_option_names[static_cast<uint32_t>(
18 ThreadSpec::OptionNames::LastOptionName)]{"Index", "ID", "Name",
21 ThreadSpec::ThreadSpec()
22 : m_index(UINT32_MAX), m_tid(LLDB_INVALID_THREAD_ID), m_name(),
25 ThreadSpec::ThreadSpec(const ThreadSpec &rhs)
26 : m_index(rhs.m_index), m_tid(rhs.m_tid), m_name(rhs.m_name),
27 m_queue_name(rhs.m_queue_name) {}
29 const ThreadSpec &ThreadSpec::operator=(const ThreadSpec &rhs) {
30 m_index = rhs.m_index;
33 m_queue_name = rhs.m_queue_name;
37 std::unique_ptr<ThreadSpec> ThreadSpec::CreateFromStructuredData(
38 const StructuredData::Dictionary &spec_dict, Status &error) {
39 uint32_t index = UINT32_MAX;
40 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
42 llvm::StringRef queue_name;
44 std::unique_ptr<ThreadSpec> thread_spec_up(new ThreadSpec());
45 bool success = spec_dict.GetValueForKeyAsInteger(
46 GetKey(OptionNames::ThreadIndex), index);
48 thread_spec_up->SetIndex(index);
51 spec_dict.GetValueForKeyAsInteger(GetKey(OptionNames::ThreadID), tid);
53 thread_spec_up->SetTID(tid);
56 spec_dict.GetValueForKeyAsString(GetKey(OptionNames::ThreadName), name);
58 thread_spec_up->SetName(name);
60 success = spec_dict.GetValueForKeyAsString(GetKey(OptionNames::ThreadName),
63 thread_spec_up->SetQueueName(queue_name);
65 return thread_spec_up;
68 StructuredData::ObjectSP ThreadSpec::SerializeToStructuredData() {
69 StructuredData::DictionarySP data_dict_sp(new StructuredData::Dictionary());
71 if (m_index != UINT32_MAX)
72 data_dict_sp->AddIntegerItem(GetKey(OptionNames::ThreadIndex), m_index);
73 if (m_tid != LLDB_INVALID_THREAD_ID)
74 data_dict_sp->AddIntegerItem(GetKey(OptionNames::ThreadID), m_tid);
76 data_dict_sp->AddStringItem(GetKey(OptionNames::ThreadName), m_name);
77 if (!m_queue_name.empty())
78 data_dict_sp->AddStringItem(GetKey(OptionNames::QueueName), m_queue_name);
83 const char *ThreadSpec::GetName() const {
84 return m_name.empty() ? nullptr : m_name.c_str();
87 const char *ThreadSpec::GetQueueName() const {
88 return m_queue_name.empty() ? nullptr : m_queue_name.c_str();
91 bool ThreadSpec::TIDMatches(Thread &thread) const {
92 if (m_tid == LLDB_INVALID_THREAD_ID)
95 lldb::tid_t thread_id = thread.GetID();
96 return TIDMatches(thread_id);
99 bool ThreadSpec::IndexMatches(Thread &thread) const {
100 if (m_index == UINT32_MAX)
102 uint32_t index = thread.GetIndexID();
103 return IndexMatches(index);
106 bool ThreadSpec::NameMatches(Thread &thread) const {
110 const char *name = thread.GetName();
111 return NameMatches(name);
114 bool ThreadSpec::QueueNameMatches(Thread &thread) const {
115 if (m_queue_name.empty())
118 const char *queue_name = thread.GetQueueName();
119 return QueueNameMatches(queue_name);
122 bool ThreadSpec::ThreadPassesBasicTests(Thread &thread) const {
123 if (!HasSpecification())
126 if (!TIDMatches(thread))
129 if (!IndexMatches(thread))
132 if (!NameMatches(thread))
135 if (!QueueNameMatches(thread))
141 bool ThreadSpec::HasSpecification() const {
142 return (m_index != UINT32_MAX || m_tid != LLDB_INVALID_THREAD_ID ||
143 !m_name.empty() || !m_queue_name.empty());
146 void ThreadSpec::GetDescription(Stream *s, lldb::DescriptionLevel level) const {
147 if (!HasSpecification()) {
148 if (level == eDescriptionLevelBrief) {
149 s->PutCString("thread spec: no ");
152 if (level == eDescriptionLevelBrief) {
153 s->PutCString("thread spec: yes ");
155 if (GetTID() != LLDB_INVALID_THREAD_ID)
156 s->Printf("tid: 0x%" PRIx64 " ", GetTID());
158 if (GetIndex() != UINT32_MAX)
159 s->Printf("index: %d ", GetIndex());
161 const char *name = GetName();
163 s->Printf("thread name: \"%s\" ", name);
165 const char *queue_name = GetQueueName();
167 s->Printf("queue name: \"%s\" ", queue_name);