blob: 31867b0dedf79fc07a55acfbbf2477a39e8614e4 [file] [log] [blame]
Jason Molenda5e8dce42013-12-13 00:29:16 +00001//===-- SBQueue.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/lldb-python.h"
11
Virgile Belloda0fc762014-03-08 16:22:55 +000012#include <inttypes.h>
13
Jason Molenda5e8dce42013-12-13 00:29:16 +000014#include "lldb/API/SBQueue.h"
15
16#include "lldb/API/SBProcess.h"
17#include "lldb/API/SBThread.h"
18#include "lldb/Core/Log.h"
19#include "lldb/Target/Process.h"
20#include "lldb/Target/Queue.h"
21#include "lldb/Target/QueueItem.h"
22#include "lldb/Target/Thread.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
Jason Molendac8064ac2013-12-14 01:14:45 +000027namespace lldb_private
28{
29
30 class QueueImpl
31 {
32 public:
33 QueueImpl () :
34 m_queue_wp(),
35 m_threads(),
36 m_thread_list_fetched(false),
Jason Molenda2fd83352014-02-05 05:44:54 +000037 m_pending_items(),
38 m_pending_items_fetched(false)
Jason Molendac8064ac2013-12-14 01:14:45 +000039 {
40 }
41
42 QueueImpl (const lldb::QueueSP &queue_sp) :
Jason Molendab97f44d2013-12-18 00:58:23 +000043 m_queue_wp(),
Jason Molendac8064ac2013-12-14 01:14:45 +000044 m_threads(),
45 m_thread_list_fetched(false),
Jason Molenda2fd83352014-02-05 05:44:54 +000046 m_pending_items(),
47 m_pending_items_fetched(false)
Jason Molendac8064ac2013-12-14 01:14:45 +000048 {
Jason Molendab97f44d2013-12-18 00:58:23 +000049 m_queue_wp = queue_sp;
Jason Molendac8064ac2013-12-14 01:14:45 +000050 }
51
52 QueueImpl (const QueueImpl &rhs)
53 {
54 if (&rhs == this)
55 return;
56 m_queue_wp = rhs.m_queue_wp;
57 m_threads = rhs.m_threads;
58 m_thread_list_fetched = rhs.m_thread_list_fetched;
Jason Molenda2fd83352014-02-05 05:44:54 +000059 m_pending_items = rhs.m_pending_items;
60 m_pending_items_fetched = rhs.m_pending_items_fetched;
Jason Molendac8064ac2013-12-14 01:14:45 +000061 }
62
63 ~QueueImpl ()
64 {
65 }
66
67 bool
68 IsValid ()
69 {
70 return m_queue_wp.lock() != NULL;
71 }
72
73 void
74 Clear ()
75 {
76 m_queue_wp.reset();
77 m_thread_list_fetched = false;
78 m_threads.clear();
Jason Molenda2fd83352014-02-05 05:44:54 +000079 m_pending_items_fetched = false;
80 m_pending_items.clear();
Jason Molendac8064ac2013-12-14 01:14:45 +000081 }
82
83 void
84 SetQueue (const lldb::QueueSP &queue_sp)
85 {
86 Clear();
87 m_queue_wp = queue_sp;
88 }
89
90 lldb::queue_id_t
91 GetQueueID () const
92 {
93 lldb::queue_id_t result = LLDB_INVALID_QUEUE_ID;
94 lldb::QueueSP queue_sp = m_queue_wp.lock();
95 if (queue_sp)
96 {
97 result = queue_sp->GetID();
98 }
99 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
100 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000101 log->Printf ("SBQueue(%p)::GetQueueID () => 0x%" PRIx64,
102 static_cast<const void*>(this), result);
Jason Molendac8064ac2013-12-14 01:14:45 +0000103 return result;
104 }
105
106 uint32_t
107 GetIndexID () const
108 {
109 uint32_t result = LLDB_INVALID_INDEX32;
110 lldb::QueueSP queue_sp = m_queue_wp.lock();
111 if (queue_sp)
112 {
113 result = queue_sp->GetIndexID();
114 }
115 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
116 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000117 log->Printf ("SBQueueImpl(%p)::GetIndexID () => %d",
118 static_cast<const void*>(this), result);
Jason Molendac8064ac2013-12-14 01:14:45 +0000119 return result;
120 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000121
Jason Molendac8064ac2013-12-14 01:14:45 +0000122 const char *
123 GetName () const
124 {
125 const char *name = NULL;
126 lldb::QueueSP queue_sp = m_queue_wp.lock ();
127 if (queue_sp.get())
128 {
129 name = queue_sp->GetName();
130 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000131
Jason Molendac8064ac2013-12-14 01:14:45 +0000132 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
133 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000134 log->Printf ("SBQueueImpl(%p)::GetName () => %s",
135 static_cast<const void*>(this),
136 name ? name : "NULL");
137
Jason Molendac8064ac2013-12-14 01:14:45 +0000138 return name;
139 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000140
Jason Molendac8064ac2013-12-14 01:14:45 +0000141 void
142 FetchThreads ()
143 {
144 if (m_thread_list_fetched == false)
145 {
146 lldb::QueueSP queue_sp = m_queue_wp.lock();
147 if (queue_sp)
148 {
149 Process::StopLocker stop_locker;
150 if (stop_locker.TryLock (&queue_sp->GetProcess()->GetRunLock()))
151 {
152 const std::vector<ThreadSP> thread_list(queue_sp->GetThreads());
153 m_thread_list_fetched = true;
154 const uint32_t num_threads = thread_list.size();
155 for (uint32_t idx = 0; idx < num_threads; ++idx)
156 {
157 ThreadSP thread_sp = thread_list[idx];
158 if (thread_sp && thread_sp->IsValid())
159 {
160 m_threads.push_back (thread_sp);
161 }
162 }
163 }
164 }
165 }
166 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000167
Jason Molendac8064ac2013-12-14 01:14:45 +0000168 void
169 FetchItems ()
170 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000171 if (m_pending_items_fetched == false)
Jason Molendac8064ac2013-12-14 01:14:45 +0000172 {
173 QueueSP queue_sp = m_queue_wp.lock();
174 if (queue_sp)
175 {
176 Process::StopLocker stop_locker;
177 if (stop_locker.TryLock (&queue_sp->GetProcess()->GetRunLock()))
178 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000179 const std::vector<QueueItemSP> queue_items(queue_sp->GetPendingItems());
180 m_pending_items_fetched = true;
181 const uint32_t num_pending_items = queue_items.size();
182 for (uint32_t idx = 0; idx < num_pending_items; ++idx)
Jason Molendac8064ac2013-12-14 01:14:45 +0000183 {
184 QueueItemSP item = queue_items[idx];
185 if (item && item->IsValid())
186 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000187 m_pending_items.push_back (item);
Jason Molendac8064ac2013-12-14 01:14:45 +0000188 }
189 }
190 }
191 }
192 }
193 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000194
Jason Molendac8064ac2013-12-14 01:14:45 +0000195 uint32_t
196 GetNumThreads ()
197 {
198 uint32_t result = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000199
Jason Molendac8064ac2013-12-14 01:14:45 +0000200 FetchThreads();
201 if (m_thread_list_fetched)
202 {
203 result = m_threads.size();
204 }
205 return result;
206 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000207
Jason Molendac8064ac2013-12-14 01:14:45 +0000208 lldb::SBThread
209 GetThreadAtIndex (uint32_t idx)
210 {
211 FetchThreads();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000212
Jason Molendac8064ac2013-12-14 01:14:45 +0000213 SBThread sb_thread;
214 QueueSP queue_sp = m_queue_wp.lock();
215 if (queue_sp && idx < m_threads.size())
216 {
217 ProcessSP process_sp = queue_sp->GetProcess();
218 if (process_sp)
219 {
220 ThreadSP thread_sp = m_threads[idx].lock();
221 if (thread_sp)
222 {
223 sb_thread.SetThread (thread_sp);
224 }
225 }
226 }
227 return sb_thread;
228 }
Jason Molendafe95dc92014-03-09 19:41:30 +0000229
Jason Molendac8064ac2013-12-14 01:14:45 +0000230 uint32_t
Jason Molenda2fd83352014-02-05 05:44:54 +0000231 GetNumPendingItems ()
Jason Molendac8064ac2013-12-14 01:14:45 +0000232 {
233 uint32_t result = 0;
Jason Molendafe95dc92014-03-09 19:41:30 +0000234
235 QueueSP queue_sp = m_queue_wp.lock();
236 if (m_pending_items_fetched == false && queue_sp)
237 {
238 result = queue_sp->GetNumPendingWorkItems();
239 }
240 else
Jason Molendac8064ac2013-12-14 01:14:45 +0000241 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000242 result = m_pending_items.size();
Jason Molendac8064ac2013-12-14 01:14:45 +0000243 }
244 return result;
245 }
Jason Molendafe95dc92014-03-09 19:41:30 +0000246
Jason Molendac8064ac2013-12-14 01:14:45 +0000247 lldb::SBQueueItem
Jason Molenda2fd83352014-02-05 05:44:54 +0000248 GetPendingItemAtIndex (uint32_t idx)
Jason Molendac8064ac2013-12-14 01:14:45 +0000249 {
250 SBQueueItem result;
251 FetchItems();
Jason Molenda2fd83352014-02-05 05:44:54 +0000252 if (m_pending_items_fetched && idx < m_pending_items.size())
Jason Molendac8064ac2013-12-14 01:14:45 +0000253 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000254 result.SetQueueItem (m_pending_items[idx]);
Jason Molendac8064ac2013-12-14 01:14:45 +0000255 }
256 return result;
257 }
Jason Molendafe95dc92014-03-09 19:41:30 +0000258
259 uint32_t
260 GetNumRunningItems ()
261 {
262 uint32_t result = 0;
263 QueueSP queue_sp = m_queue_wp.lock();
264 if (queue_sp)
265 result = queue_sp->GetNumRunningWorkItems();
266 return result;
267 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000268
Jason Molendac8064ac2013-12-14 01:14:45 +0000269 lldb::SBProcess
270 GetProcess ()
271 {
272 SBProcess result;
273 QueueSP queue_sp = m_queue_wp.lock();
274 if (queue_sp)
275 {
276 result.SetSP (queue_sp->GetProcess());
277 }
278 return result;
279 }
280
Jason Molendaaac16e02014-03-13 02:54:54 +0000281 lldb::QueueKind
282 GetKind ()
283 {
284 lldb::QueueKind kind = eQueueKindUnknown;
285 QueueSP queue_sp = m_queue_wp.lock();
286 if (queue_sp)
287 kind = queue_sp->GetKind();
288
289 return kind;
290 }
291
Jason Molendac8064ac2013-12-14 01:14:45 +0000292 private:
293 lldb::QueueWP m_queue_wp;
294 std::vector<lldb::ThreadWP> m_threads; // threads currently executing this queue's items
295 bool m_thread_list_fetched; // have we tried to fetch the threads list already?
Jason Molenda2fd83352014-02-05 05:44:54 +0000296 std::vector<lldb::QueueItemSP> m_pending_items; // items currently enqueued
297 bool m_pending_items_fetched; // have we tried to fetch the item list already?
Jason Molendac8064ac2013-12-14 01:14:45 +0000298 };
299
300}
301
Jason Molenda5e8dce42013-12-13 00:29:16 +0000302SBQueue::SBQueue () :
Jason Molendac8064ac2013-12-14 01:14:45 +0000303 m_opaque_sp (new QueueImpl())
Jason Molenda5e8dce42013-12-13 00:29:16 +0000304{
305}
306
307SBQueue::SBQueue (const QueueSP& queue_sp) :
Jason Molendac8064ac2013-12-14 01:14:45 +0000308 m_opaque_sp (new QueueImpl (queue_sp))
Jason Molenda5e8dce42013-12-13 00:29:16 +0000309{
310}
311
Jason Molendac8064ac2013-12-14 01:14:45 +0000312SBQueue::SBQueue (const SBQueue &rhs)
313{
314 if (&rhs == this)
315 return;
316
317 m_opaque_sp = rhs.m_opaque_sp;
318}
319
320const lldb::SBQueue &
321SBQueue::operator = (const lldb::SBQueue &rhs)
322{
323 m_opaque_sp = rhs.m_opaque_sp;
324 return *this;
325}
326
Jason Molenda5e8dce42013-12-13 00:29:16 +0000327SBQueue::~SBQueue()
328{
Jason Molenda5e8dce42013-12-13 00:29:16 +0000329}
330
331bool
332SBQueue::IsValid() const
333{
Jason Molendaac605f42014-03-08 01:34:55 +0000334 bool is_valid = m_opaque_sp->IsValid ();
335 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
336 if (log)
337 log->Printf("SBQueue(0x%" PRIx64 ")::IsValid() == %s", m_opaque_sp->GetQueueID(),
338 is_valid ? "true" : "false");
339 return is_valid;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000340}
341
342
343void
344SBQueue::Clear ()
345{
Jason Molendaac605f42014-03-08 01:34:55 +0000346 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
347 if (log)
348 log->Printf("SBQueue(0x%" PRIx64 ")::Clear()", m_opaque_sp->GetQueueID());
Jason Molendac8064ac2013-12-14 01:14:45 +0000349 m_opaque_sp->Clear();
Jason Molenda5e8dce42013-12-13 00:29:16 +0000350}
351
352
353void
354SBQueue::SetQueue (const QueueSP& queue_sp)
355{
Jason Molendac8064ac2013-12-14 01:14:45 +0000356 m_opaque_sp->SetQueue (queue_sp);
Jason Molenda5e8dce42013-12-13 00:29:16 +0000357}
358
359lldb::queue_id_t
360SBQueue::GetQueueID () const
361{
Jason Molendaac605f42014-03-08 01:34:55 +0000362 lldb::queue_id_t qid = m_opaque_sp->GetQueueID ();
363 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
364 if (log)
365 log->Printf("SBQueue(0x%" PRIx64 ")::GetQueueID() == 0x%" PRIx64, m_opaque_sp->GetQueueID(), (uint64_t) qid);
366 return qid;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000367}
368
369uint32_t
370SBQueue::GetIndexID () const
371{
Jason Molendaac605f42014-03-08 01:34:55 +0000372 uint32_t index_id = m_opaque_sp->GetIndexID ();
373 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
374 if (log)
375 log->Printf("SBQueue(0x%" PRIx64 ")::GetIndexID() == 0x%" PRIx32, m_opaque_sp->GetQueueID(), index_id);
376 return index_id;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000377}
378
379const char *
380SBQueue::GetName () const
381{
Jason Molendaac605f42014-03-08 01:34:55 +0000382 const char *name = m_opaque_sp->GetName ();
383 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
384 if (log)
385 log->Printf("SBQueue(0x%" PRIx64 ")::GetName() == %s", m_opaque_sp->GetQueueID(),
386 name ? name : "");
387 return name;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000388}
389
390uint32_t
391SBQueue::GetNumThreads ()
392{
Jason Molendaac605f42014-03-08 01:34:55 +0000393 uint32_t numthreads = m_opaque_sp->GetNumThreads ();
394 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
395 if (log)
396 log->Printf("SBQueue(0x%" PRIx64 ")::GetNumThreads() == %d", m_opaque_sp->GetQueueID(), numthreads);
397 return numthreads;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000398}
399
400SBThread
401SBQueue::GetThreadAtIndex (uint32_t idx)
402{
Jason Molendaac605f42014-03-08 01:34:55 +0000403 SBThread th = m_opaque_sp->GetThreadAtIndex (idx);
404 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
405 if (log)
406 log->Printf("SBQueue(0x%" PRIx64 ")::GetThreadAtIndex(%d)", m_opaque_sp->GetQueueID(), idx);
407 return th;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000408}
409
410
411uint32_t
Jason Molenda2fd83352014-02-05 05:44:54 +0000412SBQueue::GetNumPendingItems ()
Jason Molenda5e8dce42013-12-13 00:29:16 +0000413{
Jason Molendaac605f42014-03-08 01:34:55 +0000414 uint32_t pending_items = m_opaque_sp->GetNumPendingItems ();
415 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
416 if (log)
417 log->Printf("SBQueue(0x%" PRIx64 ")::GetNumPendingItems() == %d", m_opaque_sp->GetQueueID(), pending_items);
418 return pending_items;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000419}
420
421SBQueueItem
Jason Molenda2fd83352014-02-05 05:44:54 +0000422SBQueue::GetPendingItemAtIndex (uint32_t idx)
Jason Molenda5e8dce42013-12-13 00:29:16 +0000423{
Jason Molendaac605f42014-03-08 01:34:55 +0000424 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
425 if (log)
426 log->Printf("SBQueue(0x%" PRIx64 ")::GetPendingItemAtIndex(%d)", m_opaque_sp->GetQueueID(), idx);
Jason Molenda2fd83352014-02-05 05:44:54 +0000427 return m_opaque_sp->GetPendingItemAtIndex (idx);
Jason Molenda5e8dce42013-12-13 00:29:16 +0000428}
429
Jason Molendafe95dc92014-03-09 19:41:30 +0000430uint32_t
431SBQueue::GetNumRunningItems ()
432{
433 uint32_t running_items = m_opaque_sp->GetNumRunningItems ();
434 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
435 if (log)
436 log->Printf("SBQueue(0x%" PRIx64 ")::GetNumRunningItems() == %d", m_opaque_sp->GetQueueID(), running_items);
437 return running_items;
438}
439
Jason Molenda5e8dce42013-12-13 00:29:16 +0000440SBProcess
441SBQueue::GetProcess ()
442{
Jason Molendac8064ac2013-12-14 01:14:45 +0000443 return m_opaque_sp->GetProcess();
Jason Molenda5e8dce42013-12-13 00:29:16 +0000444}
Jason Molendaaac16e02014-03-13 02:54:54 +0000445
446lldb::QueueKind
447SBQueue::GetKind ()
448{
449 return m_opaque_sp->GetKind();
450}