Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1 | //===-- 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 Bello | da0fc76 | 2014-03-08 16:22:55 +0000 | [diff] [blame^] | 12 | #include <inttypes.h> |
| 13 | |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 14 | #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 | |
| 24 | using namespace lldb; |
| 25 | using namespace lldb_private; |
| 26 | |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 27 | namespace 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 Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 37 | m_pending_items(), |
| 38 | m_pending_items_fetched(false) |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
| 42 | QueueImpl (const lldb::QueueSP &queue_sp) : |
Jason Molenda | b97f44d | 2013-12-18 00:58:23 +0000 | [diff] [blame] | 43 | m_queue_wp(), |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 44 | m_threads(), |
| 45 | m_thread_list_fetched(false), |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 46 | m_pending_items(), |
| 47 | m_pending_items_fetched(false) |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 48 | { |
Jason Molenda | b97f44d | 2013-12-18 00:58:23 +0000 | [diff] [blame] | 49 | m_queue_wp = queue_sp; |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 50 | } |
| 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 Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 59 | m_pending_items = rhs.m_pending_items; |
| 60 | m_pending_items_fetched = rhs.m_pending_items_fetched; |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 61 | } |
| 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 Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 79 | m_pending_items_fetched = false; |
| 80 | m_pending_items.clear(); |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 81 | } |
| 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) |
| 101 | log->Printf ("SBQueue(%p)::GetQueueID () => 0x%" PRIx64, this, result); |
| 102 | return result; |
| 103 | } |
| 104 | |
| 105 | uint32_t |
| 106 | GetIndexID () const |
| 107 | { |
| 108 | uint32_t result = LLDB_INVALID_INDEX32; |
| 109 | lldb::QueueSP queue_sp = m_queue_wp.lock(); |
| 110 | if (queue_sp) |
| 111 | { |
| 112 | result = queue_sp->GetIndexID(); |
| 113 | } |
| 114 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 115 | if (log) |
| 116 | log->Printf ("SBQueueImpl(%p)::GetIndexID () => %d", this, result); |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | const char * |
| 121 | GetName () const |
| 122 | { |
| 123 | const char *name = NULL; |
| 124 | lldb::QueueSP queue_sp = m_queue_wp.lock (); |
| 125 | if (queue_sp.get()) |
| 126 | { |
| 127 | name = queue_sp->GetName(); |
| 128 | } |
| 129 | |
| 130 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 131 | if (log) |
| 132 | log->Printf ("SBQueueImpl(%p)::GetName () => %s", this, name ? name : "NULL"); |
| 133 | |
| 134 | return name; |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | FetchThreads () |
| 139 | { |
| 140 | if (m_thread_list_fetched == false) |
| 141 | { |
| 142 | lldb::QueueSP queue_sp = m_queue_wp.lock(); |
| 143 | if (queue_sp) |
| 144 | { |
| 145 | Process::StopLocker stop_locker; |
| 146 | if (stop_locker.TryLock (&queue_sp->GetProcess()->GetRunLock())) |
| 147 | { |
| 148 | const std::vector<ThreadSP> thread_list(queue_sp->GetThreads()); |
| 149 | m_thread_list_fetched = true; |
| 150 | const uint32_t num_threads = thread_list.size(); |
| 151 | for (uint32_t idx = 0; idx < num_threads; ++idx) |
| 152 | { |
| 153 | ThreadSP thread_sp = thread_list[idx]; |
| 154 | if (thread_sp && thread_sp->IsValid()) |
| 155 | { |
| 156 | m_threads.push_back (thread_sp); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | FetchItems () |
| 166 | { |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 167 | if (m_pending_items_fetched == false) |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 168 | { |
| 169 | QueueSP queue_sp = m_queue_wp.lock(); |
| 170 | if (queue_sp) |
| 171 | { |
| 172 | Process::StopLocker stop_locker; |
| 173 | if (stop_locker.TryLock (&queue_sp->GetProcess()->GetRunLock())) |
| 174 | { |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 175 | const std::vector<QueueItemSP> queue_items(queue_sp->GetPendingItems()); |
| 176 | m_pending_items_fetched = true; |
| 177 | const uint32_t num_pending_items = queue_items.size(); |
| 178 | for (uint32_t idx = 0; idx < num_pending_items; ++idx) |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 179 | { |
| 180 | QueueItemSP item = queue_items[idx]; |
| 181 | if (item && item->IsValid()) |
| 182 | { |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 183 | m_pending_items.push_back (item); |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | uint32_t |
| 192 | GetNumThreads () |
| 193 | { |
| 194 | uint32_t result = 0; |
| 195 | |
| 196 | FetchThreads(); |
| 197 | if (m_thread_list_fetched) |
| 198 | { |
| 199 | result = m_threads.size(); |
| 200 | } |
| 201 | return result; |
| 202 | } |
| 203 | |
| 204 | lldb::SBThread |
| 205 | GetThreadAtIndex (uint32_t idx) |
| 206 | { |
| 207 | FetchThreads(); |
| 208 | |
| 209 | SBThread sb_thread; |
| 210 | QueueSP queue_sp = m_queue_wp.lock(); |
| 211 | if (queue_sp && idx < m_threads.size()) |
| 212 | { |
| 213 | ProcessSP process_sp = queue_sp->GetProcess(); |
| 214 | if (process_sp) |
| 215 | { |
| 216 | ThreadSP thread_sp = m_threads[idx].lock(); |
| 217 | if (thread_sp) |
| 218 | { |
| 219 | sb_thread.SetThread (thread_sp); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | return sb_thread; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | uint32_t |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 228 | GetNumPendingItems () |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 229 | { |
| 230 | uint32_t result = 0; |
| 231 | FetchItems(); |
| 232 | |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 233 | if (m_pending_items_fetched) |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 234 | { |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 235 | result = m_pending_items.size(); |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 236 | } |
| 237 | return result; |
| 238 | } |
| 239 | |
| 240 | lldb::SBQueueItem |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 241 | GetPendingItemAtIndex (uint32_t idx) |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 242 | { |
| 243 | SBQueueItem result; |
| 244 | FetchItems(); |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 245 | if (m_pending_items_fetched && idx < m_pending_items.size()) |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 246 | { |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 247 | result.SetQueueItem (m_pending_items[idx]); |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 248 | } |
| 249 | return result; |
| 250 | } |
| 251 | |
| 252 | lldb::SBProcess |
| 253 | GetProcess () |
| 254 | { |
| 255 | SBProcess result; |
| 256 | QueueSP queue_sp = m_queue_wp.lock(); |
| 257 | if (queue_sp) |
| 258 | { |
| 259 | result.SetSP (queue_sp->GetProcess()); |
| 260 | } |
| 261 | return result; |
| 262 | } |
| 263 | |
| 264 | private: |
| 265 | lldb::QueueWP m_queue_wp; |
| 266 | std::vector<lldb::ThreadWP> m_threads; // threads currently executing this queue's items |
| 267 | bool m_thread_list_fetched; // have we tried to fetch the threads list already? |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 268 | std::vector<lldb::QueueItemSP> m_pending_items; // items currently enqueued |
| 269 | bool m_pending_items_fetched; // have we tried to fetch the item list already? |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | } |
| 273 | |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 274 | SBQueue::SBQueue () : |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 275 | m_opaque_sp (new QueueImpl()) |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 276 | { |
| 277 | } |
| 278 | |
| 279 | SBQueue::SBQueue (const QueueSP& queue_sp) : |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 280 | m_opaque_sp (new QueueImpl (queue_sp)) |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 281 | { |
| 282 | } |
| 283 | |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 284 | SBQueue::SBQueue (const SBQueue &rhs) |
| 285 | { |
| 286 | if (&rhs == this) |
| 287 | return; |
| 288 | |
| 289 | m_opaque_sp = rhs.m_opaque_sp; |
| 290 | } |
| 291 | |
| 292 | const lldb::SBQueue & |
| 293 | SBQueue::operator = (const lldb::SBQueue &rhs) |
| 294 | { |
| 295 | m_opaque_sp = rhs.m_opaque_sp; |
| 296 | return *this; |
| 297 | } |
| 298 | |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 299 | SBQueue::~SBQueue() |
| 300 | { |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | bool |
| 304 | SBQueue::IsValid() const |
| 305 | { |
Jason Molenda | ac605f4 | 2014-03-08 01:34:55 +0000 | [diff] [blame] | 306 | bool is_valid = m_opaque_sp->IsValid (); |
| 307 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 308 | if (log) |
| 309 | log->Printf("SBQueue(0x%" PRIx64 ")::IsValid() == %s", m_opaque_sp->GetQueueID(), |
| 310 | is_valid ? "true" : "false"); |
| 311 | return is_valid; |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | |
| 315 | void |
| 316 | SBQueue::Clear () |
| 317 | { |
Jason Molenda | ac605f4 | 2014-03-08 01:34:55 +0000 | [diff] [blame] | 318 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 319 | if (log) |
| 320 | log->Printf("SBQueue(0x%" PRIx64 ")::Clear()", m_opaque_sp->GetQueueID()); |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 321 | m_opaque_sp->Clear(); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | |
| 325 | void |
| 326 | SBQueue::SetQueue (const QueueSP& queue_sp) |
| 327 | { |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 328 | m_opaque_sp->SetQueue (queue_sp); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | lldb::queue_id_t |
| 332 | SBQueue::GetQueueID () const |
| 333 | { |
Jason Molenda | ac605f4 | 2014-03-08 01:34:55 +0000 | [diff] [blame] | 334 | lldb::queue_id_t qid = m_opaque_sp->GetQueueID (); |
| 335 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 336 | if (log) |
| 337 | log->Printf("SBQueue(0x%" PRIx64 ")::GetQueueID() == 0x%" PRIx64, m_opaque_sp->GetQueueID(), (uint64_t) qid); |
| 338 | return qid; |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | uint32_t |
| 342 | SBQueue::GetIndexID () const |
| 343 | { |
Jason Molenda | ac605f4 | 2014-03-08 01:34:55 +0000 | [diff] [blame] | 344 | uint32_t index_id = m_opaque_sp->GetIndexID (); |
| 345 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 346 | if (log) |
| 347 | log->Printf("SBQueue(0x%" PRIx64 ")::GetIndexID() == 0x%" PRIx32, m_opaque_sp->GetQueueID(), index_id); |
| 348 | return index_id; |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | const char * |
| 352 | SBQueue::GetName () const |
| 353 | { |
Jason Molenda | ac605f4 | 2014-03-08 01:34:55 +0000 | [diff] [blame] | 354 | const char *name = m_opaque_sp->GetName (); |
| 355 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 356 | if (log) |
| 357 | log->Printf("SBQueue(0x%" PRIx64 ")::GetName() == %s", m_opaque_sp->GetQueueID(), |
| 358 | name ? name : ""); |
| 359 | return name; |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | uint32_t |
| 363 | SBQueue::GetNumThreads () |
| 364 | { |
Jason Molenda | ac605f4 | 2014-03-08 01:34:55 +0000 | [diff] [blame] | 365 | uint32_t numthreads = m_opaque_sp->GetNumThreads (); |
| 366 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 367 | if (log) |
| 368 | log->Printf("SBQueue(0x%" PRIx64 ")::GetNumThreads() == %d", m_opaque_sp->GetQueueID(), numthreads); |
| 369 | return numthreads; |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | SBThread |
| 373 | SBQueue::GetThreadAtIndex (uint32_t idx) |
| 374 | { |
Jason Molenda | ac605f4 | 2014-03-08 01:34:55 +0000 | [diff] [blame] | 375 | SBThread th = m_opaque_sp->GetThreadAtIndex (idx); |
| 376 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 377 | if (log) |
| 378 | log->Printf("SBQueue(0x%" PRIx64 ")::GetThreadAtIndex(%d)", m_opaque_sp->GetQueueID(), idx); |
| 379 | return th; |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | |
| 383 | uint32_t |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 384 | SBQueue::GetNumPendingItems () |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 385 | { |
Jason Molenda | ac605f4 | 2014-03-08 01:34:55 +0000 | [diff] [blame] | 386 | uint32_t pending_items = m_opaque_sp->GetNumPendingItems (); |
| 387 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 388 | if (log) |
| 389 | log->Printf("SBQueue(0x%" PRIx64 ")::GetNumPendingItems() == %d", m_opaque_sp->GetQueueID(), pending_items); |
| 390 | return pending_items; |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | SBQueueItem |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 394 | SBQueue::GetPendingItemAtIndex (uint32_t idx) |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 395 | { |
Jason Molenda | ac605f4 | 2014-03-08 01:34:55 +0000 | [diff] [blame] | 396 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
| 397 | if (log) |
| 398 | log->Printf("SBQueue(0x%" PRIx64 ")::GetPendingItemAtIndex(%d)", m_opaque_sp->GetQueueID(), idx); |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 399 | return m_opaque_sp->GetPendingItemAtIndex (idx); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | SBProcess |
| 403 | SBQueue::GetProcess () |
| 404 | { |
Jason Molenda | c8064ac | 2013-12-14 01:14:45 +0000 | [diff] [blame] | 405 | return m_opaque_sp->GetProcess(); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 406 | } |