blob: b19ed72543c2593293affd4dae28d72b4a177e94 [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"
Jason Molendab9ffa982014-04-25 00:01:15 +000018#include "lldb/API/SBQueueItem.h"
19
Jason Molenda5e8dce42013-12-13 00:29:16 +000020#include "lldb/Core/Log.h"
21#include "lldb/Target/Process.h"
22#include "lldb/Target/Queue.h"
23#include "lldb/Target/QueueItem.h"
24#include "lldb/Target/Thread.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
Jason Molendac8064ac2013-12-14 01:14:45 +000029namespace lldb_private
30{
31
32 class QueueImpl
33 {
34 public:
35 QueueImpl () :
36 m_queue_wp(),
37 m_threads(),
38 m_thread_list_fetched(false),
Jason Molenda2fd83352014-02-05 05:44:54 +000039 m_pending_items(),
40 m_pending_items_fetched(false)
Jason Molendac8064ac2013-12-14 01:14:45 +000041 {
42 }
43
44 QueueImpl (const lldb::QueueSP &queue_sp) :
Jason Molendab97f44d2013-12-18 00:58:23 +000045 m_queue_wp(),
Jason Molendac8064ac2013-12-14 01:14:45 +000046 m_threads(),
47 m_thread_list_fetched(false),
Jason Molenda2fd83352014-02-05 05:44:54 +000048 m_pending_items(),
49 m_pending_items_fetched(false)
Jason Molendac8064ac2013-12-14 01:14:45 +000050 {
Jason Molendab97f44d2013-12-18 00:58:23 +000051 m_queue_wp = queue_sp;
Jason Molendac8064ac2013-12-14 01:14:45 +000052 }
53
54 QueueImpl (const QueueImpl &rhs)
55 {
56 if (&rhs == this)
57 return;
58 m_queue_wp = rhs.m_queue_wp;
59 m_threads = rhs.m_threads;
60 m_thread_list_fetched = rhs.m_thread_list_fetched;
Jason Molenda2fd83352014-02-05 05:44:54 +000061 m_pending_items = rhs.m_pending_items;
62 m_pending_items_fetched = rhs.m_pending_items_fetched;
Jason Molendac8064ac2013-12-14 01:14:45 +000063 }
64
65 ~QueueImpl ()
66 {
67 }
68
69 bool
70 IsValid ()
71 {
72 return m_queue_wp.lock() != NULL;
73 }
74
75 void
76 Clear ()
77 {
78 m_queue_wp.reset();
79 m_thread_list_fetched = false;
80 m_threads.clear();
Jason Molenda2fd83352014-02-05 05:44:54 +000081 m_pending_items_fetched = false;
82 m_pending_items.clear();
Jason Molendac8064ac2013-12-14 01:14:45 +000083 }
84
85 void
86 SetQueue (const lldb::QueueSP &queue_sp)
87 {
88 Clear();
89 m_queue_wp = queue_sp;
90 }
91
92 lldb::queue_id_t
93 GetQueueID () const
94 {
95 lldb::queue_id_t result = LLDB_INVALID_QUEUE_ID;
96 lldb::QueueSP queue_sp = m_queue_wp.lock();
97 if (queue_sp)
98 {
99 result = queue_sp->GetID();
100 }
101 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
102 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000103 log->Printf ("SBQueue(%p)::GetQueueID () => 0x%" PRIx64,
104 static_cast<const void*>(this), result);
Jason Molendac8064ac2013-12-14 01:14:45 +0000105 return result;
106 }
107
108 uint32_t
109 GetIndexID () const
110 {
111 uint32_t result = LLDB_INVALID_INDEX32;
112 lldb::QueueSP queue_sp = m_queue_wp.lock();
113 if (queue_sp)
114 {
115 result = queue_sp->GetIndexID();
116 }
117 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
118 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000119 log->Printf ("SBQueueImpl(%p)::GetIndexID () => %d",
120 static_cast<const void*>(this), result);
Jason Molendac8064ac2013-12-14 01:14:45 +0000121 return result;
122 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000123
Jason Molendac8064ac2013-12-14 01:14:45 +0000124 const char *
125 GetName () const
126 {
127 const char *name = NULL;
128 lldb::QueueSP queue_sp = m_queue_wp.lock ();
129 if (queue_sp.get())
130 {
131 name = queue_sp->GetName();
132 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000133
Jason Molendac8064ac2013-12-14 01:14:45 +0000134 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
135 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000136 log->Printf ("SBQueueImpl(%p)::GetName () => %s",
137 static_cast<const void*>(this),
138 name ? name : "NULL");
139
Jason Molendac8064ac2013-12-14 01:14:45 +0000140 return name;
141 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000142
Jason Molendac8064ac2013-12-14 01:14:45 +0000143 void
144 FetchThreads ()
145 {
146 if (m_thread_list_fetched == false)
147 {
148 lldb::QueueSP queue_sp = m_queue_wp.lock();
149 if (queue_sp)
150 {
151 Process::StopLocker stop_locker;
152 if (stop_locker.TryLock (&queue_sp->GetProcess()->GetRunLock()))
153 {
154 const std::vector<ThreadSP> thread_list(queue_sp->GetThreads());
155 m_thread_list_fetched = true;
156 const uint32_t num_threads = thread_list.size();
157 for (uint32_t idx = 0; idx < num_threads; ++idx)
158 {
159 ThreadSP thread_sp = thread_list[idx];
160 if (thread_sp && thread_sp->IsValid())
161 {
162 m_threads.push_back (thread_sp);
163 }
164 }
165 }
166 }
167 }
168 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000169
Jason Molendac8064ac2013-12-14 01:14:45 +0000170 void
171 FetchItems ()
172 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000173 if (m_pending_items_fetched == false)
Jason Molendac8064ac2013-12-14 01:14:45 +0000174 {
175 QueueSP queue_sp = m_queue_wp.lock();
176 if (queue_sp)
177 {
178 Process::StopLocker stop_locker;
179 if (stop_locker.TryLock (&queue_sp->GetProcess()->GetRunLock()))
180 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000181 const std::vector<QueueItemSP> queue_items(queue_sp->GetPendingItems());
182 m_pending_items_fetched = true;
183 const uint32_t num_pending_items = queue_items.size();
184 for (uint32_t idx = 0; idx < num_pending_items; ++idx)
Jason Molendac8064ac2013-12-14 01:14:45 +0000185 {
186 QueueItemSP item = queue_items[idx];
187 if (item && item->IsValid())
188 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000189 m_pending_items.push_back (item);
Jason Molendac8064ac2013-12-14 01:14:45 +0000190 }
191 }
192 }
193 }
194 }
195 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000196
Jason Molendac8064ac2013-12-14 01:14:45 +0000197 uint32_t
198 GetNumThreads ()
199 {
200 uint32_t result = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000201
Jason Molendac8064ac2013-12-14 01:14:45 +0000202 FetchThreads();
203 if (m_thread_list_fetched)
204 {
205 result = m_threads.size();
206 }
207 return result;
208 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000209
Jason Molendac8064ac2013-12-14 01:14:45 +0000210 lldb::SBThread
211 GetThreadAtIndex (uint32_t idx)
212 {
213 FetchThreads();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000214
Jason Molendac8064ac2013-12-14 01:14:45 +0000215 SBThread sb_thread;
216 QueueSP queue_sp = m_queue_wp.lock();
217 if (queue_sp && idx < m_threads.size())
218 {
219 ProcessSP process_sp = queue_sp->GetProcess();
220 if (process_sp)
221 {
222 ThreadSP thread_sp = m_threads[idx].lock();
223 if (thread_sp)
224 {
225 sb_thread.SetThread (thread_sp);
226 }
227 }
228 }
229 return sb_thread;
230 }
Jason Molendafe95dc92014-03-09 19:41:30 +0000231
Jason Molendac8064ac2013-12-14 01:14:45 +0000232 uint32_t
Jason Molenda2fd83352014-02-05 05:44:54 +0000233 GetNumPendingItems ()
Jason Molendac8064ac2013-12-14 01:14:45 +0000234 {
235 uint32_t result = 0;
Jason Molendafe95dc92014-03-09 19:41:30 +0000236
237 QueueSP queue_sp = m_queue_wp.lock();
238 if (m_pending_items_fetched == false && queue_sp)
239 {
240 result = queue_sp->GetNumPendingWorkItems();
241 }
242 else
Jason Molendac8064ac2013-12-14 01:14:45 +0000243 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000244 result = m_pending_items.size();
Jason Molendac8064ac2013-12-14 01:14:45 +0000245 }
246 return result;
247 }
Jason Molendafe95dc92014-03-09 19:41:30 +0000248
Jason Molendac8064ac2013-12-14 01:14:45 +0000249 lldb::SBQueueItem
Jason Molenda2fd83352014-02-05 05:44:54 +0000250 GetPendingItemAtIndex (uint32_t idx)
Jason Molendac8064ac2013-12-14 01:14:45 +0000251 {
252 SBQueueItem result;
253 FetchItems();
Jason Molenda2fd83352014-02-05 05:44:54 +0000254 if (m_pending_items_fetched && idx < m_pending_items.size())
Jason Molendac8064ac2013-12-14 01:14:45 +0000255 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000256 result.SetQueueItem (m_pending_items[idx]);
Jason Molendac8064ac2013-12-14 01:14:45 +0000257 }
258 return result;
259 }
Jason Molendafe95dc92014-03-09 19:41:30 +0000260
261 uint32_t
262 GetNumRunningItems ()
263 {
264 uint32_t result = 0;
265 QueueSP queue_sp = m_queue_wp.lock();
266 if (queue_sp)
267 result = queue_sp->GetNumRunningWorkItems();
268 return result;
269 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000270
Jason Molendac8064ac2013-12-14 01:14:45 +0000271 lldb::SBProcess
272 GetProcess ()
273 {
274 SBProcess result;
275 QueueSP queue_sp = m_queue_wp.lock();
276 if (queue_sp)
277 {
278 result.SetSP (queue_sp->GetProcess());
279 }
280 return result;
281 }
282
Jason Molendaaac16e02014-03-13 02:54:54 +0000283 lldb::QueueKind
284 GetKind ()
285 {
286 lldb::QueueKind kind = eQueueKindUnknown;
287 QueueSP queue_sp = m_queue_wp.lock();
288 if (queue_sp)
289 kind = queue_sp->GetKind();
290
291 return kind;
292 }
293
Jason Molendac8064ac2013-12-14 01:14:45 +0000294 private:
295 lldb::QueueWP m_queue_wp;
296 std::vector<lldb::ThreadWP> m_threads; // threads currently executing this queue's items
297 bool m_thread_list_fetched; // have we tried to fetch the threads list already?
Jason Molenda2fd83352014-02-05 05:44:54 +0000298 std::vector<lldb::QueueItemSP> m_pending_items; // items currently enqueued
299 bool m_pending_items_fetched; // have we tried to fetch the item list already?
Jason Molendac8064ac2013-12-14 01:14:45 +0000300 };
301
302}
303
Jason Molenda5e8dce42013-12-13 00:29:16 +0000304SBQueue::SBQueue () :
Jason Molendac8064ac2013-12-14 01:14:45 +0000305 m_opaque_sp (new QueueImpl())
Jason Molenda5e8dce42013-12-13 00:29:16 +0000306{
307}
308
309SBQueue::SBQueue (const QueueSP& queue_sp) :
Jason Molendac8064ac2013-12-14 01:14:45 +0000310 m_opaque_sp (new QueueImpl (queue_sp))
Jason Molenda5e8dce42013-12-13 00:29:16 +0000311{
312}
313
Jason Molendac8064ac2013-12-14 01:14:45 +0000314SBQueue::SBQueue (const SBQueue &rhs)
315{
316 if (&rhs == this)
317 return;
318
319 m_opaque_sp = rhs.m_opaque_sp;
320}
321
322const lldb::SBQueue &
323SBQueue::operator = (const lldb::SBQueue &rhs)
324{
325 m_opaque_sp = rhs.m_opaque_sp;
326 return *this;
327}
328
Jason Molenda5e8dce42013-12-13 00:29:16 +0000329SBQueue::~SBQueue()
330{
Jason Molenda5e8dce42013-12-13 00:29:16 +0000331}
332
333bool
334SBQueue::IsValid() const
335{
Jason Molendaac605f42014-03-08 01:34:55 +0000336 bool is_valid = m_opaque_sp->IsValid ();
337 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
338 if (log)
339 log->Printf("SBQueue(0x%" PRIx64 ")::IsValid() == %s", m_opaque_sp->GetQueueID(),
340 is_valid ? "true" : "false");
341 return is_valid;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000342}
343
344
345void
346SBQueue::Clear ()
347{
Jason Molendaac605f42014-03-08 01:34:55 +0000348 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
349 if (log)
350 log->Printf("SBQueue(0x%" PRIx64 ")::Clear()", m_opaque_sp->GetQueueID());
Jason Molendac8064ac2013-12-14 01:14:45 +0000351 m_opaque_sp->Clear();
Jason Molenda5e8dce42013-12-13 00:29:16 +0000352}
353
354
355void
356SBQueue::SetQueue (const QueueSP& queue_sp)
357{
Jason Molendac8064ac2013-12-14 01:14:45 +0000358 m_opaque_sp->SetQueue (queue_sp);
Jason Molenda5e8dce42013-12-13 00:29:16 +0000359}
360
361lldb::queue_id_t
362SBQueue::GetQueueID () const
363{
Jason Molendaac605f42014-03-08 01:34:55 +0000364 lldb::queue_id_t qid = m_opaque_sp->GetQueueID ();
365 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
366 if (log)
367 log->Printf("SBQueue(0x%" PRIx64 ")::GetQueueID() == 0x%" PRIx64, m_opaque_sp->GetQueueID(), (uint64_t) qid);
368 return qid;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000369}
370
371uint32_t
372SBQueue::GetIndexID () const
373{
Jason Molendaac605f42014-03-08 01:34:55 +0000374 uint32_t index_id = m_opaque_sp->GetIndexID ();
375 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
376 if (log)
377 log->Printf("SBQueue(0x%" PRIx64 ")::GetIndexID() == 0x%" PRIx32, m_opaque_sp->GetQueueID(), index_id);
378 return index_id;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000379}
380
381const char *
382SBQueue::GetName () const
383{
Jason Molendaac605f42014-03-08 01:34:55 +0000384 const char *name = m_opaque_sp->GetName ();
385 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
386 if (log)
387 log->Printf("SBQueue(0x%" PRIx64 ")::GetName() == %s", m_opaque_sp->GetQueueID(),
388 name ? name : "");
389 return name;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000390}
391
392uint32_t
393SBQueue::GetNumThreads ()
394{
Jason Molendaac605f42014-03-08 01:34:55 +0000395 uint32_t numthreads = m_opaque_sp->GetNumThreads ();
396 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
397 if (log)
398 log->Printf("SBQueue(0x%" PRIx64 ")::GetNumThreads() == %d", m_opaque_sp->GetQueueID(), numthreads);
399 return numthreads;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000400}
401
402SBThread
403SBQueue::GetThreadAtIndex (uint32_t idx)
404{
Jason Molendaac605f42014-03-08 01:34:55 +0000405 SBThread th = m_opaque_sp->GetThreadAtIndex (idx);
406 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
407 if (log)
408 log->Printf("SBQueue(0x%" PRIx64 ")::GetThreadAtIndex(%d)", m_opaque_sp->GetQueueID(), idx);
409 return th;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000410}
411
412
413uint32_t
Jason Molenda2fd83352014-02-05 05:44:54 +0000414SBQueue::GetNumPendingItems ()
Jason Molenda5e8dce42013-12-13 00:29:16 +0000415{
Jason Molendaac605f42014-03-08 01:34:55 +0000416 uint32_t pending_items = m_opaque_sp->GetNumPendingItems ();
417 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
418 if (log)
419 log->Printf("SBQueue(0x%" PRIx64 ")::GetNumPendingItems() == %d", m_opaque_sp->GetQueueID(), pending_items);
420 return pending_items;
Jason Molenda5e8dce42013-12-13 00:29:16 +0000421}
422
423SBQueueItem
Jason Molenda2fd83352014-02-05 05:44:54 +0000424SBQueue::GetPendingItemAtIndex (uint32_t idx)
Jason Molenda5e8dce42013-12-13 00:29:16 +0000425{
Jason Molendaac605f42014-03-08 01:34:55 +0000426 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
427 if (log)
428 log->Printf("SBQueue(0x%" PRIx64 ")::GetPendingItemAtIndex(%d)", m_opaque_sp->GetQueueID(), idx);
Jason Molenda2fd83352014-02-05 05:44:54 +0000429 return m_opaque_sp->GetPendingItemAtIndex (idx);
Jason Molenda5e8dce42013-12-13 00:29:16 +0000430}
431
Jason Molendafe95dc92014-03-09 19:41:30 +0000432uint32_t
433SBQueue::GetNumRunningItems ()
434{
435 uint32_t running_items = m_opaque_sp->GetNumRunningItems ();
436 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
437 if (log)
438 log->Printf("SBQueue(0x%" PRIx64 ")::GetNumRunningItems() == %d", m_opaque_sp->GetQueueID(), running_items);
439 return running_items;
440}
441
Jason Molenda5e8dce42013-12-13 00:29:16 +0000442SBProcess
443SBQueue::GetProcess ()
444{
Jason Molendac8064ac2013-12-14 01:14:45 +0000445 return m_opaque_sp->GetProcess();
Jason Molenda5e8dce42013-12-13 00:29:16 +0000446}
Jason Molendaaac16e02014-03-13 02:54:54 +0000447
448lldb::QueueKind
449SBQueue::GetKind ()
450{
451 return m_opaque_sp->GetKind();
452}