blob: 444a5a5b262aa8330731edc9815c64267a94d5f7 [file] [log] [blame]
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001//===-- ThreadSpec.cpp ------------------------------------------*- C++ -*-===//
Jim Ingham1b54c882010-06-16 02:00:15 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Jim Ingham1b54c882010-06-16 02:00:15 +000014#include "lldb/Target/ThreadSpec.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000015#include "lldb/Target/Thread.h"
16#include "lldb/Utility/StructuredData.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000017
18using namespace lldb;
19using namespace lldb_private;
20
Jim Ingham778ef392016-09-22 22:00:59 +000021const char *ThreadSpec::g_option_names[static_cast<uint32_t>(
22 ThreadSpec::OptionNames::LastOptionName)]{"Index", "ID", "Name",
23 "QueueName"};
24
Kate Stoneb9c1b512016-09-06 20:57:50 +000025ThreadSpec::ThreadSpec()
26 : m_index(UINT32_MAX), m_tid(LLDB_INVALID_THREAD_ID), m_name(),
27 m_queue_name() {}
28
29ThreadSpec::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) {}
32
33const ThreadSpec &ThreadSpec::operator=(const ThreadSpec &rhs) {
34 m_index = rhs.m_index;
35 m_tid = rhs.m_tid;
36 m_name = rhs.m_name;
37 m_queue_name = rhs.m_queue_name;
38 return *this;
Jim Ingham1b54c882010-06-16 02:00:15 +000039}
40
Jim Ingham778ef392016-09-22 22:00:59 +000041std::unique_ptr<ThreadSpec> ThreadSpec::CreateFromStructuredData(
Zachary Turner97206d52017-05-12 04:51:55 +000042 const StructuredData::Dictionary &spec_dict, Status &error) {
Jim Ingham778ef392016-09-22 22:00:59 +000043 uint32_t index = UINT32_MAX;
44 lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
Zachary Turner28333212017-05-12 05:49:54 +000045 llvm::StringRef name;
46 llvm::StringRef queue_name;
Jim Ingham778ef392016-09-22 22:00:59 +000047
48 std::unique_ptr<ThreadSpec> thread_spec_up(new ThreadSpec());
49 bool success = spec_dict.GetValueForKeyAsInteger(
50 GetKey(OptionNames::ThreadIndex), index);
51 if (success)
52 thread_spec_up->SetIndex(index);
53
54 success =
55 spec_dict.GetValueForKeyAsInteger(GetKey(OptionNames::ThreadID), tid);
56 if (success)
57 thread_spec_up->SetTID(tid);
58
59 success =
60 spec_dict.GetValueForKeyAsString(GetKey(OptionNames::ThreadName), name);
61 if (success)
Zachary Turner28333212017-05-12 05:49:54 +000062 thread_spec_up->SetName(name);
Jim Ingham778ef392016-09-22 22:00:59 +000063
64 success = spec_dict.GetValueForKeyAsString(GetKey(OptionNames::ThreadName),
65 queue_name);
66 if (success)
Zachary Turner28333212017-05-12 05:49:54 +000067 thread_spec_up->SetQueueName(queue_name);
Jim Ingham778ef392016-09-22 22:00:59 +000068
69 return thread_spec_up;
70}
71
72StructuredData::ObjectSP ThreadSpec::SerializeToStructuredData() {
73 StructuredData::DictionarySP data_dict_sp(new StructuredData::Dictionary());
74
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);
79 if (!m_name.empty())
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);
83
84 return data_dict_sp;
85}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087const char *ThreadSpec::GetName() const {
88 return m_name.empty() ? nullptr : m_name.c_str();
Jim Ingham1b54c882010-06-16 02:00:15 +000089}
90
Kate Stoneb9c1b512016-09-06 20:57:50 +000091const char *ThreadSpec::GetQueueName() const {
92 return m_queue_name.empty() ? nullptr : m_queue_name.c_str();
Jim Ingham1b54c882010-06-16 02:00:15 +000093}
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095bool ThreadSpec::TIDMatches(Thread &thread) const {
96 if (m_tid == LLDB_INVALID_THREAD_ID)
Jim Ingham01363092010-06-18 01:00:58 +000097 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +000098
99 lldb::tid_t thread_id = thread.GetID();
100 return TIDMatches(thread_id);
Jim Ingham01363092010-06-18 01:00:58 +0000101}
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103bool ThreadSpec::IndexMatches(Thread &thread) const {
104 if (m_index == UINT32_MAX)
105 return true;
106 uint32_t index = thread.GetIndexID();
107 return IndexMatches(index);
Jim Ingham01363092010-06-18 01:00:58 +0000108}
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110bool ThreadSpec::NameMatches(Thread &thread) const {
111 if (m_name.empty())
112 return true;
Jim Ingham01363092010-06-18 01:00:58 +0000113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 const char *name = thread.GetName();
115 return NameMatches(name);
116}
117
118bool ThreadSpec::QueueNameMatches(Thread &thread) const {
119 if (m_queue_name.empty())
120 return true;
121
122 const char *queue_name = thread.GetQueueName();
123 return QueueNameMatches(queue_name);
124}
125
126bool ThreadSpec::ThreadPassesBasicTests(Thread &thread) const {
127 if (!HasSpecification())
128 return true;
129
130 if (!TIDMatches(thread))
131 return false;
132
133 if (!IndexMatches(thread))
134 return false;
135
136 if (!NameMatches(thread))
137 return false;
138
139 if (!QueueNameMatches(thread))
140 return false;
141
142 return true;
143}
144
145bool ThreadSpec::HasSpecification() const {
146 return (m_index != UINT32_MAX || m_tid != LLDB_INVALID_THREAD_ID ||
147 !m_name.empty() || !m_queue_name.empty());
148}
149
150void ThreadSpec::GetDescription(Stream *s, lldb::DescriptionLevel level) const {
151 if (!HasSpecification()) {
152 if (level == eDescriptionLevelBrief) {
153 s->PutCString("thread spec: no ");
Jim Ingham01363092010-06-18 01:00:58 +0000154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 } else {
156 if (level == eDescriptionLevelBrief) {
157 s->PutCString("thread spec: yes ");
158 } else {
159 if (GetTID() != LLDB_INVALID_THREAD_ID)
160 s->Printf("tid: 0x%" PRIx64 " ", GetTID());
161
162 if (GetIndex() != UINT32_MAX)
163 s->Printf("index: %d ", GetIndex());
164
165 const char *name = GetName();
166 if (name)
167 s->Printf("thread name: \"%s\" ", name);
168
169 const char *queue_name = GetQueueName();
170 if (queue_name)
171 s->Printf("queue name: \"%s\" ", queue_name);
172 }
173 }
Jim Ingham01363092010-06-18 01:00:58 +0000174}