112b93ac6SEd Maste //===-- SBQueueItem.cpp -----------------------------------------*- C++ -*-===//
212b93ac6SEd Maste //
312b93ac6SEd Maste //                     The LLVM Compiler Infrastructure
412b93ac6SEd Maste //
512b93ac6SEd Maste // This file is distributed under the University of Illinois Open Source
612b93ac6SEd Maste // License. See LICENSE.TXT for details.
712b93ac6SEd Maste //
812b93ac6SEd Maste //===----------------------------------------------------------------------===//
912b93ac6SEd Maste 
1012b93ac6SEd Maste #include "lldb/lldb-forward.h"
1112b93ac6SEd Maste 
1212b93ac6SEd Maste #include "lldb/API/SBAddress.h"
1312b93ac6SEd Maste #include "lldb/API/SBQueueItem.h"
1412b93ac6SEd Maste #include "lldb/API/SBThread.h"
1512b93ac6SEd Maste #include "lldb/Core/Address.h"
160127ef0fSEd Maste #include "lldb/Target/Process.h"
1712b93ac6SEd Maste #include "lldb/Target/QueueItem.h"
1812b93ac6SEd Maste #include "lldb/Target/Thread.h"
19f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
2012b93ac6SEd Maste 
2112b93ac6SEd Maste using namespace lldb;
2212b93ac6SEd Maste using namespace lldb_private;
2312b93ac6SEd Maste 
2412b93ac6SEd Maste //----------------------------------------------------------------------
2512b93ac6SEd Maste // Constructors
2612b93ac6SEd Maste //----------------------------------------------------------------------
SBQueueItem()27435933ddSDimitry Andric SBQueueItem::SBQueueItem() : m_queue_item_sp() {}
2812b93ac6SEd Maste 
SBQueueItem(const QueueItemSP & queue_item_sp)29435933ddSDimitry Andric SBQueueItem::SBQueueItem(const QueueItemSP &queue_item_sp)
30435933ddSDimitry Andric     : m_queue_item_sp(queue_item_sp) {}
3112b93ac6SEd Maste 
3212b93ac6SEd Maste //----------------------------------------------------------------------
3312b93ac6SEd Maste // Destructor
3412b93ac6SEd Maste //----------------------------------------------------------------------
~SBQueueItem()35435933ddSDimitry Andric SBQueueItem::~SBQueueItem() { m_queue_item_sp.reset(); }
3612b93ac6SEd Maste 
IsValid() const37435933ddSDimitry Andric bool SBQueueItem::IsValid() const {
380127ef0fSEd Maste   bool is_valid = m_queue_item_sp.get() != NULL;
390127ef0fSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
400127ef0fSEd Maste   if (log)
410127ef0fSEd Maste     log->Printf("SBQueueItem(%p)::IsValid() == %s",
420127ef0fSEd Maste                 static_cast<void *>(m_queue_item_sp.get()),
430127ef0fSEd Maste                 is_valid ? "true" : "false");
440127ef0fSEd Maste   return is_valid;
4512b93ac6SEd Maste }
4612b93ac6SEd Maste 
Clear()47435933ddSDimitry Andric void SBQueueItem::Clear() {
480127ef0fSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
490127ef0fSEd Maste   if (log)
500127ef0fSEd Maste     log->Printf("SBQueueItem(%p)::Clear()",
510127ef0fSEd Maste                 static_cast<void *>(m_queue_item_sp.get()));
5212b93ac6SEd Maste   m_queue_item_sp.reset();
5312b93ac6SEd Maste }
5412b93ac6SEd Maste 
SetQueueItem(const QueueItemSP & queue_item_sp)55435933ddSDimitry Andric void SBQueueItem::SetQueueItem(const QueueItemSP &queue_item_sp) {
5612b93ac6SEd Maste   m_queue_item_sp = queue_item_sp;
5712b93ac6SEd Maste }
5812b93ac6SEd Maste 
GetKind() const59435933ddSDimitry Andric lldb::QueueItemKind SBQueueItem::GetKind() const {
6012b93ac6SEd Maste   QueueItemKind result = eQueueItemKindUnknown;
610127ef0fSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
62435933ddSDimitry Andric   if (m_queue_item_sp) {
6312b93ac6SEd Maste     result = m_queue_item_sp->GetKind();
6412b93ac6SEd Maste   }
650127ef0fSEd Maste   if (log)
660127ef0fSEd Maste     log->Printf("SBQueueItem(%p)::GetKind() == %d",
670127ef0fSEd Maste                 static_cast<void *>(m_queue_item_sp.get()),
680127ef0fSEd Maste                 static_cast<int>(result));
6912b93ac6SEd Maste   return result;
7012b93ac6SEd Maste }
7112b93ac6SEd Maste 
SetKind(lldb::QueueItemKind kind)72435933ddSDimitry Andric void SBQueueItem::SetKind(lldb::QueueItemKind kind) {
73435933ddSDimitry Andric   if (m_queue_item_sp) {
7412b93ac6SEd Maste     m_queue_item_sp->SetKind(kind);
7512b93ac6SEd Maste   }
7612b93ac6SEd Maste }
7712b93ac6SEd Maste 
GetAddress() const78435933ddSDimitry Andric SBAddress SBQueueItem::GetAddress() const {
7912b93ac6SEd Maste   SBAddress result;
800127ef0fSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
81435933ddSDimitry Andric   if (m_queue_item_sp) {
8212b93ac6SEd Maste     result.SetAddress(&m_queue_item_sp->GetAddress());
8312b93ac6SEd Maste   }
84435933ddSDimitry Andric   if (log) {
850127ef0fSEd Maste     StreamString sstr;
860127ef0fSEd Maste     const Address *addr = result.get();
870127ef0fSEd Maste     if (addr)
88435933ddSDimitry Andric       addr->Dump(&sstr, NULL, Address::DumpStyleModuleWithFileAddress,
89435933ddSDimitry Andric                  Address::DumpStyleInvalid, 4);
900127ef0fSEd Maste     log->Printf("SBQueueItem(%p)::GetAddress() == SBAddress(%p): %s",
910127ef0fSEd Maste                 static_cast<void *>(m_queue_item_sp.get()),
920127ef0fSEd Maste                 static_cast<void *>(result.get()), sstr.GetData());
930127ef0fSEd Maste   }
9412b93ac6SEd Maste   return result;
9512b93ac6SEd Maste }
9612b93ac6SEd Maste 
SetAddress(SBAddress addr)97435933ddSDimitry Andric void SBQueueItem::SetAddress(SBAddress addr) {
98435933ddSDimitry Andric   if (m_queue_item_sp) {
9912b93ac6SEd Maste     m_queue_item_sp->SetAddress(addr.ref());
10012b93ac6SEd Maste   }
10112b93ac6SEd Maste }
10212b93ac6SEd Maste 
GetExtendedBacktraceThread(const char * type)103435933ddSDimitry Andric SBThread SBQueueItem::GetExtendedBacktraceThread(const char *type) {
10412b93ac6SEd Maste   SBThread result;
1050127ef0fSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
106435933ddSDimitry Andric   if (m_queue_item_sp) {
1070127ef0fSEd Maste     ProcessSP process_sp = m_queue_item_sp->GetProcessSP();
1080127ef0fSEd Maste     Process::StopLocker stop_locker;
109435933ddSDimitry Andric     if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) {
11012b93ac6SEd Maste       ThreadSP thread_sp;
11112b93ac6SEd Maste       ConstString type_const(type);
11212b93ac6SEd Maste       thread_sp = m_queue_item_sp->GetExtendedBacktraceThread(type_const);
113435933ddSDimitry Andric       if (thread_sp) {
114435933ddSDimitry Andric         // Save this in the Process' ExtendedThreadList so a strong pointer
115*4ba319b5SDimitry Andric         // retains the object
1160127ef0fSEd Maste         process_sp->GetExtendedThreadList().AddThread(thread_sp);
11712b93ac6SEd Maste         result.SetThread(thread_sp);
118435933ddSDimitry Andric         if (log) {
1190127ef0fSEd Maste           const char *queue_name = thread_sp->GetQueueName();
1200127ef0fSEd Maste           if (queue_name == NULL)
1210127ef0fSEd Maste             queue_name = "";
122435933ddSDimitry Andric           log->Printf(
123435933ddSDimitry Andric               "SBQueueItem(%p)::GetExtendedBacktraceThread() = new extended "
124435933ddSDimitry Andric               "Thread created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
1250127ef0fSEd Maste               static_cast<void *>(m_queue_item_sp.get()),
1260127ef0fSEd Maste               static_cast<void *>(thread_sp.get()),
127435933ddSDimitry Andric               static_cast<uint64_t>(thread_sp->GetQueueID()), queue_name);
1280127ef0fSEd Maste         }
1290127ef0fSEd Maste       }
13012b93ac6SEd Maste     }
13112b93ac6SEd Maste   }
13212b93ac6SEd Maste   return result;
13312b93ac6SEd Maste }
134