1 //===-- ThreadSpec.cpp ------------------------------------------*- C++ -*-===// 2 // 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 10 // C Includes 11 // C++ Includes 12 // Other libraries and framework includes 13 // Project includes 14 #include "lldb/Target/ThreadSpec.h" 15 #include "lldb/Target/Thread.h" 16 #include "lldb/Utility/StructuredData.h" 17 18 using namespace lldb; 19 using namespace lldb_private; 20 21 const char *ThreadSpec::g_option_names[static_cast<uint32_t>( 22 ThreadSpec::OptionNames::LastOptionName)]{"Index", "ID", "Name", 23 "QueueName"}; 24 25 ThreadSpec::ThreadSpec() 26 : m_index(UINT32_MAX), m_tid(LLDB_INVALID_THREAD_ID), m_name(), 27 m_queue_name() {} 28 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) {} 32 33 const 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; 39 } 40 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; 45 llvm::StringRef name; 46 llvm::StringRef queue_name; 47 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) 62 thread_spec_up->SetName(name); 63 64 success = spec_dict.GetValueForKeyAsString(GetKey(OptionNames::ThreadName), 65 queue_name); 66 if (success) 67 thread_spec_up->SetQueueName(queue_name); 68 69 return thread_spec_up; 70 } 71 72 StructuredData::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 87 const char *ThreadSpec::GetName() const { 88 return m_name.empty() ? nullptr : m_name.c_str(); 89 } 90 91 const char *ThreadSpec::GetQueueName() const { 92 return m_queue_name.empty() ? nullptr : m_queue_name.c_str(); 93 } 94 95 bool ThreadSpec::TIDMatches(Thread &thread) const { 96 if (m_tid == LLDB_INVALID_THREAD_ID) 97 return true; 98 99 lldb::tid_t thread_id = thread.GetID(); 100 return TIDMatches(thread_id); 101 } 102 103 bool ThreadSpec::IndexMatches(Thread &thread) const { 104 if (m_index == UINT32_MAX) 105 return true; 106 uint32_t index = thread.GetIndexID(); 107 return IndexMatches(index); 108 } 109 110 bool ThreadSpec::NameMatches(Thread &thread) const { 111 if (m_name.empty()) 112 return true; 113 114 const char *name = thread.GetName(); 115 return NameMatches(name); 116 } 117 118 bool 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 126 bool 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 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()); 148 } 149 150 void ThreadSpec::GetDescription(Stream *s, lldb::DescriptionLevel level) const { 151 if (!HasSpecification()) { 152 if (level == eDescriptionLevelBrief) { 153 s->PutCString("thread spec: no "); 154 } 155 } 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 } 174 } 175