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 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Target/ThreadSpec.h"
15 #include "lldb/Target/Thread.h"
16 #include "lldb/Utility/StructuredData.h"
19 using namespace lldb_private;
21 const char *ThreadSpec::g_option_names[static_cast<uint32_t>(
22 ThreadSpec::OptionNames::LastOptionName)]{"Index", "ID", "Name",
25 ThreadSpec::ThreadSpec()
26 : m_index(UINT32_MAX), m_tid(LLDB_INVALID_THREAD_ID), m_name(),
29 ThreadSpec::ThreadSpec(const ThreadSpec &rhs)
30 : m_index(rhs.m_index), m_tid(rhs.m_tid), m_name(rhs.m_name),
31 m_queue_name(rhs.m_queue_name) {}
33 const ThreadSpec &ThreadSpec::operator=(const ThreadSpec &rhs) {
34 m_index = rhs.m_index;
37 m_queue_name = rhs.m_queue_name;
41 std::unique_ptr<ThreadSpec> ThreadSpec::CreateFromStructuredData(
42 const StructuredData::Dictionary &spec_dict, Status &error) {
43 uint32_t index = UINT32_MAX;
44 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
46 llvm::StringRef queue_name;
48 std::unique_ptr<ThreadSpec> thread_spec_up(new ThreadSpec());
49 bool success = spec_dict.GetValueForKeyAsInteger(
50 GetKey(OptionNames::ThreadIndex), index);
52 thread_spec_up->SetIndex(index);
55 spec_dict.GetValueForKeyAsInteger(GetKey(OptionNames::ThreadID), tid);
57 thread_spec_up->SetTID(tid);
60 spec_dict.GetValueForKeyAsString(GetKey(OptionNames::ThreadName), name);
62 thread_spec_up->SetName(name);
64 success = spec_dict.GetValueForKeyAsString(GetKey(OptionNames::ThreadName),
67 thread_spec_up->SetQueueName(queue_name);
69 return thread_spec_up;
72 StructuredData::ObjectSP ThreadSpec::SerializeToStructuredData() {
73 StructuredData::DictionarySP data_dict_sp(new StructuredData::Dictionary());
75 if (m_index != UINT32_MAX)
76 data_dict_sp->AddIntegerItem(GetKey(OptionNames::ThreadIndex), m_index);
77 if (m_tid != LLDB_INVALID_THREAD_ID)
78 data_dict_sp->AddIntegerItem(GetKey(OptionNames::ThreadID), m_tid);
80 data_dict_sp->AddStringItem(GetKey(OptionNames::ThreadName), m_name);
81 if (!m_queue_name.empty())
82 data_dict_sp->AddStringItem(GetKey(OptionNames::QueueName), m_queue_name);
87 const char *ThreadSpec::GetName() const {
88 return m_name.empty() ? nullptr : m_name.c_str();
91 const char *ThreadSpec::GetQueueName() const {
92 return m_queue_name.empty() ? nullptr : m_queue_name.c_str();
95 bool ThreadSpec::TIDMatches(Thread &thread) const {
96 if (m_tid == LLDB_INVALID_THREAD_ID)
99 lldb::tid_t thread_id = thread.GetID();
100 return TIDMatches(thread_id);
103 bool ThreadSpec::IndexMatches(Thread &thread) const {
104 if (m_index == UINT32_MAX)
106 uint32_t index = thread.GetIndexID();
107 return IndexMatches(index);
110 bool ThreadSpec::NameMatches(Thread &thread) const {
114 const char *name = thread.GetName();
115 return NameMatches(name);
118 bool ThreadSpec::QueueNameMatches(Thread &thread) const {
119 if (m_queue_name.empty())
122 const char *queue_name = thread.GetQueueName();
123 return QueueNameMatches(queue_name);
126 bool ThreadSpec::ThreadPassesBasicTests(Thread &thread) const {
127 if (!HasSpecification())
130 if (!TIDMatches(thread))
133 if (!IndexMatches(thread))
136 if (!NameMatches(thread))
139 if (!QueueNameMatches(thread))
145 bool ThreadSpec::HasSpecification() const {
146 return (m_index != UINT32_MAX || m_tid != LLDB_INVALID_THREAD_ID ||
147 !m_name.empty() || !m_queue_name.empty());
150 void ThreadSpec::GetDescription(Stream *s, lldb::DescriptionLevel level) const {
151 if (!HasSpecification()) {
152 if (level == eDescriptionLevelBrief) {
153 s->PutCString("thread spec: no ");
156 if (level == eDescriptionLevelBrief) {
157 s->PutCString("thread spec: yes ");
159 if (GetTID() != LLDB_INVALID_THREAD_ID)
160 s->Printf("tid: 0x%" PRIx64 " ", GetTID());
162 if (GetIndex() != UINT32_MAX)
163 s->Printf("index: %d ", GetIndex());
165 const char *name = GetName();
167 s->Printf("thread name: \"%s\" ", name);
169 const char *queue_name = GetQueueName();
171 s->Printf("queue name: \"%s\" ", queue_name);