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