1 //===-- SBQueue.cpp ---------------------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include <inttypes.h> 10 11 #include "SBReproducerPrivate.h" 12 #include "lldb/API/SBQueue.h" 13 14 #include "lldb/API/SBProcess.h" 15 #include "lldb/API/SBQueueItem.h" 16 #include "lldb/API/SBThread.h" 17 18 #include "lldb/Target/Process.h" 19 #include "lldb/Target/Queue.h" 20 #include "lldb/Target/QueueItem.h" 21 #include "lldb/Target/Thread.h" 22 23 using namespace lldb; 24 using namespace lldb_private; 25 26 namespace lldb_private { 27 28 class QueueImpl { 29 public: 30 QueueImpl() 31 : m_queue_wp(), m_threads(), m_thread_list_fetched(false), 32 m_pending_items(), m_pending_items_fetched(false) {} 33 34 QueueImpl(const lldb::QueueSP &queue_sp) 35 : m_queue_wp(), m_threads(), m_thread_list_fetched(false), 36 m_pending_items(), m_pending_items_fetched(false) { 37 m_queue_wp = queue_sp; 38 } 39 40 QueueImpl(const QueueImpl &rhs) { 41 if (&rhs == this) 42 return; 43 m_queue_wp = rhs.m_queue_wp; 44 m_threads = rhs.m_threads; 45 m_thread_list_fetched = rhs.m_thread_list_fetched; 46 m_pending_items = rhs.m_pending_items; 47 m_pending_items_fetched = rhs.m_pending_items_fetched; 48 } 49 50 ~QueueImpl() {} 51 52 bool IsValid() { return m_queue_wp.lock() != NULL; } 53 54 void Clear() { 55 m_queue_wp.reset(); 56 m_thread_list_fetched = false; 57 m_threads.clear(); 58 m_pending_items_fetched = false; 59 m_pending_items.clear(); 60 } 61 62 void SetQueue(const lldb::QueueSP &queue_sp) { 63 Clear(); 64 m_queue_wp = queue_sp; 65 } 66 67 lldb::queue_id_t GetQueueID() const { 68 lldb::queue_id_t result = LLDB_INVALID_QUEUE_ID; 69 lldb::QueueSP queue_sp = m_queue_wp.lock(); 70 if (queue_sp) { 71 result = queue_sp->GetID(); 72 } 73 return result; 74 } 75 76 uint32_t GetIndexID() const { 77 uint32_t result = LLDB_INVALID_INDEX32; 78 lldb::QueueSP queue_sp = m_queue_wp.lock(); 79 if (queue_sp) { 80 result = queue_sp->GetIndexID(); 81 } 82 return result; 83 } 84 85 const char *GetName() const { 86 const char *name = NULL; 87 lldb::QueueSP queue_sp = m_queue_wp.lock(); 88 if (queue_sp.get()) { 89 name = queue_sp->GetName(); 90 } 91 return name; 92 } 93 94 void FetchThreads() { 95 if (!m_thread_list_fetched) { 96 lldb::QueueSP queue_sp = m_queue_wp.lock(); 97 if (queue_sp) { 98 Process::StopLocker stop_locker; 99 if (stop_locker.TryLock(&queue_sp->GetProcess()->GetRunLock())) { 100 const std::vector<ThreadSP> thread_list(queue_sp->GetThreads()); 101 m_thread_list_fetched = true; 102 const uint32_t num_threads = thread_list.size(); 103 for (uint32_t idx = 0; idx < num_threads; ++idx) { 104 ThreadSP thread_sp = thread_list[idx]; 105 if (thread_sp && thread_sp->IsValid()) { 106 m_threads.push_back(thread_sp); 107 } 108 } 109 } 110 } 111 } 112 } 113 114 void FetchItems() { 115 if (!m_pending_items_fetched) { 116 QueueSP queue_sp = m_queue_wp.lock(); 117 if (queue_sp) { 118 Process::StopLocker stop_locker; 119 if (stop_locker.TryLock(&queue_sp->GetProcess()->GetRunLock())) { 120 const std::vector<QueueItemSP> queue_items( 121 queue_sp->GetPendingItems()); 122 m_pending_items_fetched = true; 123 const uint32_t num_pending_items = queue_items.size(); 124 for (uint32_t idx = 0; idx < num_pending_items; ++idx) { 125 QueueItemSP item = queue_items[idx]; 126 if (item && item->IsValid()) { 127 m_pending_items.push_back(item); 128 } 129 } 130 } 131 } 132 } 133 } 134 135 uint32_t GetNumThreads() { 136 uint32_t result = 0; 137 138 FetchThreads(); 139 if (m_thread_list_fetched) { 140 result = m_threads.size(); 141 } 142 return result; 143 } 144 145 lldb::SBThread GetThreadAtIndex(uint32_t idx) { 146 FetchThreads(); 147 148 SBThread sb_thread; 149 QueueSP queue_sp = m_queue_wp.lock(); 150 if (queue_sp && idx < m_threads.size()) { 151 ProcessSP process_sp = queue_sp->GetProcess(); 152 if (process_sp) { 153 ThreadSP thread_sp = m_threads[idx].lock(); 154 if (thread_sp) { 155 sb_thread.SetThread(thread_sp); 156 } 157 } 158 } 159 return sb_thread; 160 } 161 162 uint32_t GetNumPendingItems() { 163 uint32_t result = 0; 164 165 QueueSP queue_sp = m_queue_wp.lock(); 166 if (!m_pending_items_fetched && queue_sp) { 167 result = queue_sp->GetNumPendingWorkItems(); 168 } else { 169 result = m_pending_items.size(); 170 } 171 return result; 172 } 173 174 lldb::SBQueueItem GetPendingItemAtIndex(uint32_t idx) { 175 SBQueueItem result; 176 FetchItems(); 177 if (m_pending_items_fetched && idx < m_pending_items.size()) { 178 result.SetQueueItem(m_pending_items[idx]); 179 } 180 return result; 181 } 182 183 uint32_t GetNumRunningItems() { 184 uint32_t result = 0; 185 QueueSP queue_sp = m_queue_wp.lock(); 186 if (queue_sp) 187 result = queue_sp->GetNumRunningWorkItems(); 188 return result; 189 } 190 191 lldb::SBProcess GetProcess() { 192 SBProcess result; 193 QueueSP queue_sp = m_queue_wp.lock(); 194 if (queue_sp) { 195 result.SetSP(queue_sp->GetProcess()); 196 } 197 return result; 198 } 199 200 lldb::QueueKind GetKind() { 201 lldb::QueueKind kind = eQueueKindUnknown; 202 QueueSP queue_sp = m_queue_wp.lock(); 203 if (queue_sp) 204 kind = queue_sp->GetKind(); 205 206 return kind; 207 } 208 209 private: 210 lldb::QueueWP m_queue_wp; 211 std::vector<lldb::ThreadWP> 212 m_threads; // threads currently executing this queue's items 213 bool 214 m_thread_list_fetched; // have we tried to fetch the threads list already? 215 std::vector<lldb::QueueItemSP> m_pending_items; // items currently enqueued 216 bool m_pending_items_fetched; // have we tried to fetch the item list already? 217 }; 218 } 219 220 SBQueue::SBQueue() : m_opaque_sp(new QueueImpl()) { 221 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBQueue); 222 } 223 224 SBQueue::SBQueue(const QueueSP &queue_sp) 225 : m_opaque_sp(new QueueImpl(queue_sp)) { 226 LLDB_RECORD_CONSTRUCTOR(SBQueue, (const lldb::QueueSP &), queue_sp); 227 } 228 229 SBQueue::SBQueue(const SBQueue &rhs) { 230 LLDB_RECORD_CONSTRUCTOR(SBQueue, (const lldb::SBQueue &), rhs); 231 232 if (&rhs == this) 233 return; 234 235 m_opaque_sp = rhs.m_opaque_sp; 236 } 237 238 const lldb::SBQueue &SBQueue::operator=(const lldb::SBQueue &rhs) { 239 LLDB_RECORD_METHOD(const lldb::SBQueue &, 240 SBQueue, operator=,(const lldb::SBQueue &), rhs); 241 242 m_opaque_sp = rhs.m_opaque_sp; 243 return *this; 244 } 245 246 SBQueue::~SBQueue() {} 247 248 bool SBQueue::IsValid() const { 249 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBQueue, IsValid); 250 251 return m_opaque_sp->IsValid(); 252 } 253 254 void SBQueue::Clear() { 255 LLDB_RECORD_METHOD_NO_ARGS(void, SBQueue, Clear); 256 257 m_opaque_sp->Clear(); 258 } 259 260 void SBQueue::SetQueue(const QueueSP &queue_sp) { 261 m_opaque_sp->SetQueue(queue_sp); 262 } 263 264 lldb::queue_id_t SBQueue::GetQueueID() const { 265 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::queue_id_t, SBQueue, GetQueueID); 266 267 return m_opaque_sp->GetQueueID(); 268 } 269 270 uint32_t SBQueue::GetIndexID() const { 271 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBQueue, GetIndexID); 272 273 uint32_t index_id = m_opaque_sp->GetIndexID(); 274 return index_id; 275 } 276 277 const char *SBQueue::GetName() const { 278 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBQueue, GetName); 279 280 return m_opaque_sp->GetName(); 281 } 282 283 uint32_t SBQueue::GetNumThreads() { 284 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBQueue, GetNumThreads); 285 286 return m_opaque_sp->GetNumThreads(); 287 } 288 289 SBThread SBQueue::GetThreadAtIndex(uint32_t idx) { 290 LLDB_RECORD_METHOD(lldb::SBThread, SBQueue, GetThreadAtIndex, (uint32_t), 291 idx); 292 293 SBThread th = m_opaque_sp->GetThreadAtIndex(idx); 294 return LLDB_RECORD_RESULT(th); 295 } 296 297 uint32_t SBQueue::GetNumPendingItems() { 298 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBQueue, GetNumPendingItems); 299 300 return m_opaque_sp->GetNumPendingItems(); 301 } 302 303 SBQueueItem SBQueue::GetPendingItemAtIndex(uint32_t idx) { 304 LLDB_RECORD_METHOD(lldb::SBQueueItem, SBQueue, GetPendingItemAtIndex, 305 (uint32_t), idx); 306 307 return LLDB_RECORD_RESULT(m_opaque_sp->GetPendingItemAtIndex(idx)); 308 } 309 310 uint32_t SBQueue::GetNumRunningItems() { 311 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBQueue, GetNumRunningItems); 312 313 return m_opaque_sp->GetNumRunningItems(); 314 } 315 316 SBProcess SBQueue::GetProcess() { 317 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBProcess, SBQueue, GetProcess); 318 319 return LLDB_RECORD_RESULT(m_opaque_sp->GetProcess()); 320 } 321 322 lldb::QueueKind SBQueue::GetKind() { 323 LLDB_RECORD_METHOD_NO_ARGS(lldb::QueueKind, SBQueue, GetKind); 324 325 return m_opaque_sp->GetKind(); 326 } 327