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