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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Jim Ingham | 1b54c88 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Jim Ingham | 1b54c88 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 9 | #include "lldb/Target/ThreadSpec.h" |
Pavel Labath | f2a8bcc | 2017-06-27 10:45:31 +0000 | [diff] [blame] | 10 | #include "lldb/Target/Thread.h" |
| 11 | #include "lldb/Utility/StructuredData.h" |
Jim Ingham | 1b54c88 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 12 | |
| 13 | using namespace lldb; |
| 14 | using namespace lldb_private; |
| 15 | |
Jim Ingham | 778ef39 | 2016-09-22 22:00:59 +0000 | [diff] [blame] | 16 | const char *ThreadSpec::g_option_names[static_cast<uint32_t>( |
| 17 | ThreadSpec::OptionNames::LastOptionName)]{"Index", "ID", "Name", |
| 18 | "QueueName"}; |
| 19 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | ThreadSpec::ThreadSpec() |
| 21 | : m_index(UINT32_MAX), m_tid(LLDB_INVALID_THREAD_ID), m_name(), |
| 22 | m_queue_name() {} |
| 23 | |
Jim Ingham | 778ef39 | 2016-09-22 22:00:59 +0000 | [diff] [blame] | 24 | std::unique_ptr<ThreadSpec> ThreadSpec::CreateFromStructuredData( |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 25 | const StructuredData::Dictionary &spec_dict, Status &error) { |
Jim Ingham | 778ef39 | 2016-09-22 22:00:59 +0000 | [diff] [blame] | 26 | uint32_t index = UINT32_MAX; |
| 27 | lldb::tid_t tid = LLDB_INVALID_THREAD_ID; |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 28 | llvm::StringRef name; |
| 29 | llvm::StringRef queue_name; |
Jim Ingham | 778ef39 | 2016-09-22 22:00:59 +0000 | [diff] [blame] | 30 | |
| 31 | std::unique_ptr<ThreadSpec> thread_spec_up(new ThreadSpec()); |
| 32 | bool success = spec_dict.GetValueForKeyAsInteger( |
| 33 | GetKey(OptionNames::ThreadIndex), index); |
| 34 | if (success) |
| 35 | thread_spec_up->SetIndex(index); |
| 36 | |
| 37 | success = |
| 38 | spec_dict.GetValueForKeyAsInteger(GetKey(OptionNames::ThreadID), tid); |
| 39 | if (success) |
| 40 | thread_spec_up->SetTID(tid); |
| 41 | |
| 42 | success = |
| 43 | spec_dict.GetValueForKeyAsString(GetKey(OptionNames::ThreadName), name); |
| 44 | if (success) |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 45 | thread_spec_up->SetName(name); |
Jim Ingham | 778ef39 | 2016-09-22 22:00:59 +0000 | [diff] [blame] | 46 | |
| 47 | success = spec_dict.GetValueForKeyAsString(GetKey(OptionNames::ThreadName), |
| 48 | queue_name); |
| 49 | if (success) |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 50 | thread_spec_up->SetQueueName(queue_name); |
Jim Ingham | 778ef39 | 2016-09-22 22:00:59 +0000 | [diff] [blame] | 51 | |
| 52 | return thread_spec_up; |
| 53 | } |
| 54 | |
| 55 | StructuredData::ObjectSP ThreadSpec::SerializeToStructuredData() { |
| 56 | StructuredData::DictionarySP data_dict_sp(new StructuredData::Dictionary()); |
| 57 | |
| 58 | if (m_index != UINT32_MAX) |
| 59 | data_dict_sp->AddIntegerItem(GetKey(OptionNames::ThreadIndex), m_index); |
| 60 | if (m_tid != LLDB_INVALID_THREAD_ID) |
| 61 | data_dict_sp->AddIntegerItem(GetKey(OptionNames::ThreadID), m_tid); |
| 62 | if (!m_name.empty()) |
| 63 | data_dict_sp->AddStringItem(GetKey(OptionNames::ThreadName), m_name); |
| 64 | if (!m_queue_name.empty()) |
| 65 | data_dict_sp->AddStringItem(GetKey(OptionNames::QueueName), m_queue_name); |
| 66 | |
| 67 | return data_dict_sp; |
| 68 | } |
| 69 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | const char *ThreadSpec::GetName() const { |
| 71 | return m_name.empty() ? nullptr : m_name.c_str(); |
Jim Ingham | 1b54c88 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | const char *ThreadSpec::GetQueueName() const { |
| 75 | return m_queue_name.empty() ? nullptr : m_queue_name.c_str(); |
Jim Ingham | 1b54c88 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | bool ThreadSpec::TIDMatches(Thread &thread) const { |
| 79 | if (m_tid == LLDB_INVALID_THREAD_ID) |
Jim Ingham | 0136309 | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 80 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | |
| 82 | lldb::tid_t thread_id = thread.GetID(); |
| 83 | return TIDMatches(thread_id); |
Jim Ingham | 0136309 | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | bool ThreadSpec::IndexMatches(Thread &thread) const { |
| 87 | if (m_index == UINT32_MAX) |
| 88 | return true; |
| 89 | uint32_t index = thread.GetIndexID(); |
| 90 | return IndexMatches(index); |
Jim Ingham | 0136309 | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 91 | } |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 +0000 | [diff] [blame] | 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | bool ThreadSpec::NameMatches(Thread &thread) const { |
| 94 | if (m_name.empty()) |
| 95 | return true; |
Jim Ingham | 0136309 | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 96 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | const char *name = thread.GetName(); |
| 98 | return NameMatches(name); |
| 99 | } |
| 100 | |
| 101 | bool ThreadSpec::QueueNameMatches(Thread &thread) const { |
| 102 | if (m_queue_name.empty()) |
| 103 | return true; |
| 104 | |
| 105 | const char *queue_name = thread.GetQueueName(); |
| 106 | return QueueNameMatches(queue_name); |
| 107 | } |
| 108 | |
| 109 | bool ThreadSpec::ThreadPassesBasicTests(Thread &thread) const { |
| 110 | if (!HasSpecification()) |
| 111 | return true; |
| 112 | |
| 113 | if (!TIDMatches(thread)) |
| 114 | return false; |
| 115 | |
| 116 | if (!IndexMatches(thread)) |
| 117 | return false; |
| 118 | |
| 119 | if (!NameMatches(thread)) |
| 120 | return false; |
| 121 | |
| 122 | if (!QueueNameMatches(thread)) |
| 123 | return false; |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | bool ThreadSpec::HasSpecification() const { |
| 129 | return (m_index != UINT32_MAX || m_tid != LLDB_INVALID_THREAD_ID || |
| 130 | !m_name.empty() || !m_queue_name.empty()); |
| 131 | } |
| 132 | |
| 133 | void ThreadSpec::GetDescription(Stream *s, lldb::DescriptionLevel level) const { |
| 134 | if (!HasSpecification()) { |
| 135 | if (level == eDescriptionLevelBrief) { |
| 136 | s->PutCString("thread spec: no "); |
Jim Ingham | 0136309 | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 137 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | } else { |
| 139 | if (level == eDescriptionLevelBrief) { |
| 140 | s->PutCString("thread spec: yes "); |
| 141 | } else { |
| 142 | if (GetTID() != LLDB_INVALID_THREAD_ID) |
| 143 | s->Printf("tid: 0x%" PRIx64 " ", GetTID()); |
| 144 | |
| 145 | if (GetIndex() != UINT32_MAX) |
| 146 | s->Printf("index: %d ", GetIndex()); |
| 147 | |
| 148 | const char *name = GetName(); |
| 149 | if (name) |
| 150 | s->Printf("thread name: \"%s\" ", name); |
| 151 | |
| 152 | const char *queue_name = GetQueueName(); |
| 153 | if (queue_name) |
| 154 | s->Printf("queue name: \"%s\" ", queue_name); |
| 155 | } |
| 156 | } |
Jim Ingham | 0136309 | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 157 | } |