15e8dce4dSJason Molenda //===-- SBQueueItem.cpp -----------------------------------------*- C++ -*-===//
25e8dce4dSJason Molenda //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65e8dce4dSJason Molenda //
75e8dce4dSJason Molenda //===----------------------------------------------------------------------===//
85e8dce4dSJason Molenda 
95e8dce4dSJason Molenda #include "lldb/lldb-forward.h"
105e8dce4dSJason Molenda 
115e8dce4dSJason Molenda #include "lldb/API/SBAddress.h"
125e8dce4dSJason Molenda #include "lldb/API/SBQueueItem.h"
135e8dce4dSJason Molenda #include "lldb/API/SBThread.h"
145e8dce4dSJason Molenda #include "lldb/Core/Address.h"
15a8ff543cSJason Molenda #include "lldb/Target/Process.h"
165e8dce4dSJason Molenda #include "lldb/Target/QueueItem.h"
172fd83355SJason Molenda #include "lldb/Target/Thread.h"
186f9e6901SZachary Turner #include "lldb/Utility/Log.h"
195e8dce4dSJason Molenda 
205e8dce4dSJason Molenda using namespace lldb;
215e8dce4dSJason Molenda using namespace lldb_private;
225e8dce4dSJason Molenda 
235e8dce4dSJason Molenda //----------------------------------------------------------------------
245e8dce4dSJason Molenda // Constructors
255e8dce4dSJason Molenda //----------------------------------------------------------------------
26b9c1b51eSKate Stone SBQueueItem::SBQueueItem() : m_queue_item_sp() {}
275e8dce4dSJason Molenda 
28b9c1b51eSKate Stone SBQueueItem::SBQueueItem(const QueueItemSP &queue_item_sp)
29b9c1b51eSKate Stone     : m_queue_item_sp(queue_item_sp) {}
305e8dce4dSJason Molenda 
315e8dce4dSJason Molenda //----------------------------------------------------------------------
325e8dce4dSJason Molenda // Destructor
335e8dce4dSJason Molenda //----------------------------------------------------------------------
34b9c1b51eSKate Stone SBQueueItem::~SBQueueItem() { m_queue_item_sp.reset(); }
355e8dce4dSJason Molenda 
36b9c1b51eSKate Stone bool SBQueueItem::IsValid() const {
37ac605f4aSJason Molenda   bool is_valid = m_queue_item_sp.get() != NULL;
38ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
39ac605f4aSJason Molenda   if (log)
40324a1036SSaleem Abdulrasool     log->Printf("SBQueueItem(%p)::IsValid() == %s",
41324a1036SSaleem Abdulrasool                 static_cast<void *>(m_queue_item_sp.get()),
42324a1036SSaleem Abdulrasool                 is_valid ? "true" : "false");
43ac605f4aSJason Molenda   return is_valid;
445e8dce4dSJason Molenda }
455e8dce4dSJason Molenda 
46b9c1b51eSKate Stone void SBQueueItem::Clear() {
47ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
48ac605f4aSJason Molenda   if (log)
49324a1036SSaleem Abdulrasool     log->Printf("SBQueueItem(%p)::Clear()",
50324a1036SSaleem Abdulrasool                 static_cast<void *>(m_queue_item_sp.get()));
515e8dce4dSJason Molenda   m_queue_item_sp.reset();
525e8dce4dSJason Molenda }
535e8dce4dSJason Molenda 
54b9c1b51eSKate Stone void SBQueueItem::SetQueueItem(const QueueItemSP &queue_item_sp) {
555e8dce4dSJason Molenda   m_queue_item_sp = queue_item_sp;
565e8dce4dSJason Molenda }
575e8dce4dSJason Molenda 
58b9c1b51eSKate Stone lldb::QueueItemKind SBQueueItem::GetKind() const {
595e8dce4dSJason Molenda   QueueItemKind result = eQueueItemKindUnknown;
60ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
61b9c1b51eSKate Stone   if (m_queue_item_sp) {
625e8dce4dSJason Molenda     result = m_queue_item_sp->GetKind();
635e8dce4dSJason Molenda   }
64ac605f4aSJason Molenda   if (log)
65324a1036SSaleem Abdulrasool     log->Printf("SBQueueItem(%p)::GetKind() == %d",
66324a1036SSaleem Abdulrasool                 static_cast<void *>(m_queue_item_sp.get()),
67324a1036SSaleem Abdulrasool                 static_cast<int>(result));
685e8dce4dSJason Molenda   return result;
695e8dce4dSJason Molenda }
705e8dce4dSJason Molenda 
71b9c1b51eSKate Stone void SBQueueItem::SetKind(lldb::QueueItemKind kind) {
72b9c1b51eSKate Stone   if (m_queue_item_sp) {
735e8dce4dSJason Molenda     m_queue_item_sp->SetKind(kind);
745e8dce4dSJason Molenda   }
755e8dce4dSJason Molenda }
765e8dce4dSJason Molenda 
77b9c1b51eSKate Stone SBAddress SBQueueItem::GetAddress() const {
785e8dce4dSJason Molenda   SBAddress result;
79ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
80b9c1b51eSKate Stone   if (m_queue_item_sp) {
815e8dce4dSJason Molenda     result.SetAddress(&m_queue_item_sp->GetAddress());
825e8dce4dSJason Molenda   }
83b9c1b51eSKate Stone   if (log) {
84ac605f4aSJason Molenda     StreamString sstr;
85ac605f4aSJason Molenda     const Address *addr = result.get();
86ac605f4aSJason Molenda     if (addr)
87b9c1b51eSKate Stone       addr->Dump(&sstr, NULL, Address::DumpStyleModuleWithFileAddress,
88b9c1b51eSKate Stone                  Address::DumpStyleInvalid, 4);
89ac605f4aSJason Molenda     log->Printf("SBQueueItem(%p)::GetAddress() == SBAddress(%p): %s",
90324a1036SSaleem Abdulrasool                 static_cast<void *>(m_queue_item_sp.get()),
91324a1036SSaleem Abdulrasool                 static_cast<void *>(result.get()), sstr.GetData());
92ac605f4aSJason Molenda   }
935e8dce4dSJason Molenda   return result;
945e8dce4dSJason Molenda }
955e8dce4dSJason Molenda 
96b9c1b51eSKate Stone void SBQueueItem::SetAddress(SBAddress addr) {
97b9c1b51eSKate Stone   if (m_queue_item_sp) {
985e8dce4dSJason Molenda     m_queue_item_sp->SetAddress(addr.ref());
995e8dce4dSJason Molenda   }
1005e8dce4dSJason Molenda }
1015e8dce4dSJason Molenda 
102b9c1b51eSKate Stone SBThread SBQueueItem::GetExtendedBacktraceThread(const char *type) {
1035e8dce4dSJason Molenda   SBThread result;
104ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
105b9c1b51eSKate Stone   if (m_queue_item_sp) {
106a8ff543cSJason Molenda     ProcessSP process_sp = m_queue_item_sp->GetProcessSP();
107a8ff543cSJason Molenda     Process::StopLocker stop_locker;
108b9c1b51eSKate Stone     if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) {
1095e8dce4dSJason Molenda       ThreadSP thread_sp;
1105e8dce4dSJason Molenda       ConstString type_const(type);
1115e8dce4dSJason Molenda       thread_sp = m_queue_item_sp->GetExtendedBacktraceThread(type_const);
112b9c1b51eSKate Stone       if (thread_sp) {
113b9c1b51eSKate Stone         // Save this in the Process' ExtendedThreadList so a strong pointer
11405097246SAdrian Prantl         // retains the object
115a8ff543cSJason Molenda         process_sp->GetExtendedThreadList().AddThread(thread_sp);
1165e8dce4dSJason Molenda         result.SetThread(thread_sp);
117b9c1b51eSKate Stone         if (log) {
118ac605f4aSJason Molenda           const char *queue_name = thread_sp->GetQueueName();
119ac605f4aSJason Molenda           if (queue_name == NULL)
120ac605f4aSJason Molenda             queue_name = "";
121b9c1b51eSKate Stone           log->Printf(
122b9c1b51eSKate Stone               "SBQueueItem(%p)::GetExtendedBacktraceThread() = new extended "
123b9c1b51eSKate Stone               "Thread created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
124324a1036SSaleem Abdulrasool               static_cast<void *>(m_queue_item_sp.get()),
125324a1036SSaleem Abdulrasool               static_cast<void *>(thread_sp.get()),
126b9c1b51eSKate Stone               static_cast<uint64_t>(thread_sp->GetQueueID()), queue_name);
127ac605f4aSJason Molenda         }
1285e8dce4dSJason Molenda       }
1295e8dce4dSJason Molenda     }
130a8ff543cSJason Molenda   }
1315e8dce4dSJason Molenda   return result;
1325e8dce4dSJason Molenda }
133