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