Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 +0000 | [diff] [blame] | 1 | //===-- ThreadSpec.cpp ------------------------------------------*- C++ -*-===// |
Jim Ingham | 1b54c88 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 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 | |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
| 13 | // Project includes |
Jim Ingham | 1b54c88 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 14 | #include "lldb/Target/Thread.h" |
Jim Ingham | 778ef39 | 2016-09-22 22:00:59 +0000 | [diff] [blame] | 15 | #include "lldb/Core/StructuredData.h" |
Jim Ingham | 1b54c88 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 16 | #include "lldb/Target/ThreadSpec.h" |
| 17 | |
| 18 | using namespace lldb; |
| 19 | using namespace lldb_private; |
| 20 | |
Jim Ingham | 778ef39 | 2016-09-22 22:00:59 +0000 | [diff] [blame] | 21 | const char *ThreadSpec::g_option_names[static_cast<uint32_t>( |
| 22 | ThreadSpec::OptionNames::LastOptionName)]{"Index", "ID", "Name", |
| 23 | "QueueName"}; |
| 24 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Jim Ingham | 1b54c88 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Jim Ingham | 778ef39 | 2016-09-22 22:00:59 +0000 | [diff] [blame] | 41 | std::unique_ptr<ThreadSpec> ThreadSpec::CreateFromStructuredData( |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame^] | 42 | const StructuredData::Dictionary &spec_dict, Status &error) { |
Jim Ingham | 778ef39 | 2016-09-22 22:00:59 +0000 | [diff] [blame] | 43 | uint32_t index = UINT32_MAX; |
| 44 | lldb::tid_t tid = LLDB_INVALID_THREAD_ID; |
| 45 | std::string name; |
| 46 | std::string 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.c_str()); |
| 63 | |
| 64 | success = spec_dict.GetValueForKeyAsString(GetKey(OptionNames::ThreadName), |
| 65 | queue_name); |
| 66 | if (success) |
| 67 | thread_spec_up->SetQueueName(queue_name.c_str()); |
| 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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | const char *ThreadSpec::GetName() const { |
| 88 | return m_name.empty() ? nullptr : m_name.c_str(); |
Jim Ingham | 1b54c88 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | const char *ThreadSpec::GetQueueName() const { |
| 92 | return m_queue_name.empty() ? nullptr : m_queue_name.c_str(); |
Jim Ingham | 1b54c88 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | bool ThreadSpec::TIDMatches(Thread &thread) const { |
| 96 | if (m_tid == LLDB_INVALID_THREAD_ID) |
Jim Ingham | 0136309 | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 97 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | |
| 99 | lldb::tid_t thread_id = thread.GetID(); |
| 100 | return TIDMatches(thread_id); |
Jim Ingham | 0136309 | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Jim Ingham | 0136309 | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 108 | } |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 +0000 | [diff] [blame] | 109 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | bool ThreadSpec::NameMatches(Thread &thread) const { |
| 111 | if (m_name.empty()) |
| 112 | return true; |
Jim Ingham | 0136309 | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 "); |
Jim Ingham | 0136309 | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 154 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 | } |
Jim Ingham | 0136309 | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 174 | } |