15e8dce4dSJason Molenda //===-- SBQueueItem.cpp -----------------------------------------*- C++ -*-===//
25e8dce4dSJason Molenda //
35e8dce4dSJason Molenda //                     The LLVM Compiler Infrastructure
45e8dce4dSJason Molenda //
55e8dce4dSJason Molenda // This file is distributed under the University of Illinois Open Source
65e8dce4dSJason Molenda // License. See LICENSE.TXT for details.
75e8dce4dSJason Molenda //
85e8dce4dSJason Molenda //===----------------------------------------------------------------------===//
95e8dce4dSJason Molenda 
105e8dce4dSJason Molenda #include "lldb/lldb-forward.h"
115e8dce4dSJason Molenda 
125e8dce4dSJason Molenda #include "lldb/API/SBAddress.h"
135e8dce4dSJason Molenda #include "lldb/API/SBQueueItem.h"
145e8dce4dSJason Molenda #include "lldb/API/SBThread.h"
155e8dce4dSJason Molenda #include "lldb/Core/Address.h"
16a8ff543cSJason Molenda #include "lldb/Target/Process.h"
175e8dce4dSJason Molenda #include "lldb/Target/QueueItem.h"
182fd83355SJason Molenda #include "lldb/Target/Thread.h"
19*6f9e6901SZachary Turner #include "lldb/Utility/Log.h"
205e8dce4dSJason Molenda 
215e8dce4dSJason Molenda using namespace lldb;
225e8dce4dSJason Molenda using namespace lldb_private;
235e8dce4dSJason Molenda 
245e8dce4dSJason Molenda //----------------------------------------------------------------------
255e8dce4dSJason Molenda // Constructors
265e8dce4dSJason Molenda //----------------------------------------------------------------------
27b9c1b51eSKate Stone SBQueueItem::SBQueueItem() : m_queue_item_sp() {}
285e8dce4dSJason Molenda 
29b9c1b51eSKate Stone SBQueueItem::SBQueueItem(const QueueItemSP &queue_item_sp)
30b9c1b51eSKate Stone     : m_queue_item_sp(queue_item_sp) {}
315e8dce4dSJason Molenda 
325e8dce4dSJason Molenda //----------------------------------------------------------------------
335e8dce4dSJason Molenda // Destructor
345e8dce4dSJason Molenda //----------------------------------------------------------------------
35b9c1b51eSKate Stone SBQueueItem::~SBQueueItem() { m_queue_item_sp.reset(); }
365e8dce4dSJason Molenda 
37b9c1b51eSKate Stone bool SBQueueItem::IsValid() const {
38ac605f4aSJason Molenda   bool is_valid = m_queue_item_sp.get() != NULL;
39ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
40ac605f4aSJason Molenda   if (log)
41324a1036SSaleem Abdulrasool     log->Printf("SBQueueItem(%p)::IsValid() == %s",
42324a1036SSaleem Abdulrasool                 static_cast<void *>(m_queue_item_sp.get()),
43324a1036SSaleem Abdulrasool                 is_valid ? "true" : "false");
44ac605f4aSJason Molenda   return is_valid;
455e8dce4dSJason Molenda }
465e8dce4dSJason Molenda 
47b9c1b51eSKate Stone void SBQueueItem::Clear() {
48ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
49ac605f4aSJason Molenda   if (log)
50324a1036SSaleem Abdulrasool     log->Printf("SBQueueItem(%p)::Clear()",
51324a1036SSaleem Abdulrasool                 static_cast<void *>(m_queue_item_sp.get()));
525e8dce4dSJason Molenda   m_queue_item_sp.reset();
535e8dce4dSJason Molenda }
545e8dce4dSJason Molenda 
55b9c1b51eSKate Stone void SBQueueItem::SetQueueItem(const QueueItemSP &queue_item_sp) {
565e8dce4dSJason Molenda   m_queue_item_sp = queue_item_sp;
575e8dce4dSJason Molenda }
585e8dce4dSJason Molenda 
59b9c1b51eSKate Stone lldb::QueueItemKind SBQueueItem::GetKind() const {
605e8dce4dSJason Molenda   QueueItemKind result = eQueueItemKindUnknown;
61ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
62b9c1b51eSKate Stone   if (m_queue_item_sp) {
635e8dce4dSJason Molenda     result = m_queue_item_sp->GetKind();
645e8dce4dSJason Molenda   }
65ac605f4aSJason Molenda   if (log)
66324a1036SSaleem Abdulrasool     log->Printf("SBQueueItem(%p)::GetKind() == %d",
67324a1036SSaleem Abdulrasool                 static_cast<void *>(m_queue_item_sp.get()),
68324a1036SSaleem Abdulrasool                 static_cast<int>(result));
695e8dce4dSJason Molenda   return result;
705e8dce4dSJason Molenda }
715e8dce4dSJason Molenda 
72b9c1b51eSKate Stone void SBQueueItem::SetKind(lldb::QueueItemKind kind) {
73b9c1b51eSKate Stone   if (m_queue_item_sp) {
745e8dce4dSJason Molenda     m_queue_item_sp->SetKind(kind);
755e8dce4dSJason Molenda   }
765e8dce4dSJason Molenda }
775e8dce4dSJason Molenda 
78b9c1b51eSKate Stone SBAddress SBQueueItem::GetAddress() const {
795e8dce4dSJason Molenda   SBAddress result;
80ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
81b9c1b51eSKate Stone   if (m_queue_item_sp) {
825e8dce4dSJason Molenda     result.SetAddress(&m_queue_item_sp->GetAddress());
835e8dce4dSJason Molenda   }
84b9c1b51eSKate Stone   if (log) {
85ac605f4aSJason Molenda     StreamString sstr;
86ac605f4aSJason Molenda     const Address *addr = result.get();
87ac605f4aSJason Molenda     if (addr)
88b9c1b51eSKate Stone       addr->Dump(&sstr, NULL, Address::DumpStyleModuleWithFileAddress,
89b9c1b51eSKate Stone                  Address::DumpStyleInvalid, 4);
90ac605f4aSJason Molenda     log->Printf("SBQueueItem(%p)::GetAddress() == SBAddress(%p): %s",
91324a1036SSaleem Abdulrasool                 static_cast<void *>(m_queue_item_sp.get()),
92324a1036SSaleem Abdulrasool                 static_cast<void *>(result.get()), sstr.GetData());
93ac605f4aSJason Molenda   }
945e8dce4dSJason Molenda   return result;
955e8dce4dSJason Molenda }
965e8dce4dSJason Molenda 
97b9c1b51eSKate Stone void SBQueueItem::SetAddress(SBAddress addr) {
98b9c1b51eSKate Stone   if (m_queue_item_sp) {
995e8dce4dSJason Molenda     m_queue_item_sp->SetAddress(addr.ref());
1005e8dce4dSJason Molenda   }
1015e8dce4dSJason Molenda }
1025e8dce4dSJason Molenda 
103b9c1b51eSKate Stone SBThread SBQueueItem::GetExtendedBacktraceThread(const char *type) {
1045e8dce4dSJason Molenda   SBThread result;
105ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
106b9c1b51eSKate Stone   if (m_queue_item_sp) {
107a8ff543cSJason Molenda     ProcessSP process_sp = m_queue_item_sp->GetProcessSP();
108a8ff543cSJason Molenda     Process::StopLocker stop_locker;
109b9c1b51eSKate Stone     if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) {
1105e8dce4dSJason Molenda       ThreadSP thread_sp;
1115e8dce4dSJason Molenda       ConstString type_const(type);
1125e8dce4dSJason Molenda       thread_sp = m_queue_item_sp->GetExtendedBacktraceThread(type_const);
113b9c1b51eSKate Stone       if (thread_sp) {
114b9c1b51eSKate Stone         // Save this in the Process' ExtendedThreadList so a strong pointer
115b9c1b51eSKate Stone         // retains the
116a8ff543cSJason Molenda         // object
117a8ff543cSJason Molenda         process_sp->GetExtendedThreadList().AddThread(thread_sp);
1185e8dce4dSJason Molenda         result.SetThread(thread_sp);
119b9c1b51eSKate Stone         if (log) {
120ac605f4aSJason Molenda           const char *queue_name = thread_sp->GetQueueName();
121ac605f4aSJason Molenda           if (queue_name == NULL)
122ac605f4aSJason Molenda             queue_name = "";
123b9c1b51eSKate Stone           log->Printf(
124b9c1b51eSKate Stone               "SBQueueItem(%p)::GetExtendedBacktraceThread() = new extended "
125b9c1b51eSKate Stone               "Thread created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
126324a1036SSaleem Abdulrasool               static_cast<void *>(m_queue_item_sp.get()),
127324a1036SSaleem Abdulrasool               static_cast<void *>(thread_sp.get()),
128b9c1b51eSKate Stone               static_cast<uint64_t>(thread_sp->GetQueueID()), queue_name);
129ac605f4aSJason Molenda         }
1305e8dce4dSJason Molenda       }
1315e8dce4dSJason Molenda     }
132a8ff543cSJason Molenda   }
1335e8dce4dSJason Molenda   return result;
1345e8dce4dSJason Molenda }
135