15e8dce4dSJason Molenda //===-- SBQueueItem.cpp -----------------------------------------*- C++ -*-===//
25e8dce4dSJason Molenda //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler 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 
11*baf5664fSJonas Devlieghere #include "SBReproducerPrivate.h"
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"
196f9e6901SZachary 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 //----------------------------------------------------------------------
27*baf5664fSJonas Devlieghere SBQueueItem::SBQueueItem() : m_queue_item_sp() {
28*baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBQueueItem);
29*baf5664fSJonas Devlieghere }
305e8dce4dSJason Molenda 
31b9c1b51eSKate Stone SBQueueItem::SBQueueItem(const QueueItemSP &queue_item_sp)
32*baf5664fSJonas Devlieghere     : m_queue_item_sp(queue_item_sp) {
33*baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR(SBQueueItem, (const lldb::QueueItemSP &),
34*baf5664fSJonas Devlieghere                           queue_item_sp);
35*baf5664fSJonas Devlieghere }
365e8dce4dSJason Molenda 
375e8dce4dSJason Molenda //----------------------------------------------------------------------
385e8dce4dSJason Molenda // Destructor
395e8dce4dSJason Molenda //----------------------------------------------------------------------
40b9c1b51eSKate Stone SBQueueItem::~SBQueueItem() { m_queue_item_sp.reset(); }
415e8dce4dSJason Molenda 
42b9c1b51eSKate Stone bool SBQueueItem::IsValid() const {
43*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBQueueItem, IsValid);
44*baf5664fSJonas Devlieghere 
45ac605f4aSJason Molenda   bool is_valid = m_queue_item_sp.get() != NULL;
46ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
47ac605f4aSJason Molenda   if (log)
48324a1036SSaleem Abdulrasool     log->Printf("SBQueueItem(%p)::IsValid() == %s",
49324a1036SSaleem Abdulrasool                 static_cast<void *>(m_queue_item_sp.get()),
50324a1036SSaleem Abdulrasool                 is_valid ? "true" : "false");
51ac605f4aSJason Molenda   return is_valid;
525e8dce4dSJason Molenda }
535e8dce4dSJason Molenda 
54b9c1b51eSKate Stone void SBQueueItem::Clear() {
55*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_NO_ARGS(void, SBQueueItem, Clear);
56*baf5664fSJonas Devlieghere 
57ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
58ac605f4aSJason Molenda   if (log)
59324a1036SSaleem Abdulrasool     log->Printf("SBQueueItem(%p)::Clear()",
60324a1036SSaleem Abdulrasool                 static_cast<void *>(m_queue_item_sp.get()));
615e8dce4dSJason Molenda   m_queue_item_sp.reset();
625e8dce4dSJason Molenda }
635e8dce4dSJason Molenda 
64b9c1b51eSKate Stone void SBQueueItem::SetQueueItem(const QueueItemSP &queue_item_sp) {
65*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(void, SBQueueItem, SetQueueItem,
66*baf5664fSJonas Devlieghere                      (const lldb::QueueItemSP &), queue_item_sp);
67*baf5664fSJonas Devlieghere 
685e8dce4dSJason Molenda   m_queue_item_sp = queue_item_sp;
695e8dce4dSJason Molenda }
705e8dce4dSJason Molenda 
71b9c1b51eSKate Stone lldb::QueueItemKind SBQueueItem::GetKind() const {
72*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::QueueItemKind, SBQueueItem, GetKind);
73*baf5664fSJonas Devlieghere 
745e8dce4dSJason Molenda   QueueItemKind result = eQueueItemKindUnknown;
75ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
76b9c1b51eSKate Stone   if (m_queue_item_sp) {
775e8dce4dSJason Molenda     result = m_queue_item_sp->GetKind();
785e8dce4dSJason Molenda   }
79ac605f4aSJason Molenda   if (log)
80324a1036SSaleem Abdulrasool     log->Printf("SBQueueItem(%p)::GetKind() == %d",
81324a1036SSaleem Abdulrasool                 static_cast<void *>(m_queue_item_sp.get()),
82324a1036SSaleem Abdulrasool                 static_cast<int>(result));
835e8dce4dSJason Molenda   return result;
845e8dce4dSJason Molenda }
855e8dce4dSJason Molenda 
86b9c1b51eSKate Stone void SBQueueItem::SetKind(lldb::QueueItemKind kind) {
87*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(void, SBQueueItem, SetKind, (lldb::QueueItemKind), kind);
88*baf5664fSJonas Devlieghere 
89b9c1b51eSKate Stone   if (m_queue_item_sp) {
905e8dce4dSJason Molenda     m_queue_item_sp->SetKind(kind);
915e8dce4dSJason Molenda   }
925e8dce4dSJason Molenda }
935e8dce4dSJason Molenda 
94b9c1b51eSKate Stone SBAddress SBQueueItem::GetAddress() const {
95*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBAddress, SBQueueItem, GetAddress);
96*baf5664fSJonas Devlieghere 
975e8dce4dSJason Molenda   SBAddress result;
98ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
99b9c1b51eSKate Stone   if (m_queue_item_sp) {
1005e8dce4dSJason Molenda     result.SetAddress(&m_queue_item_sp->GetAddress());
1015e8dce4dSJason Molenda   }
102b9c1b51eSKate Stone   if (log) {
103ac605f4aSJason Molenda     StreamString sstr;
104ac605f4aSJason Molenda     const Address *addr = result.get();
105ac605f4aSJason Molenda     if (addr)
106b9c1b51eSKate Stone       addr->Dump(&sstr, NULL, Address::DumpStyleModuleWithFileAddress,
107b9c1b51eSKate Stone                  Address::DumpStyleInvalid, 4);
108ac605f4aSJason Molenda     log->Printf("SBQueueItem(%p)::GetAddress() == SBAddress(%p): %s",
109324a1036SSaleem Abdulrasool                 static_cast<void *>(m_queue_item_sp.get()),
110324a1036SSaleem Abdulrasool                 static_cast<void *>(result.get()), sstr.GetData());
111ac605f4aSJason Molenda   }
112*baf5664fSJonas Devlieghere   return LLDB_RECORD_RESULT(result);
1135e8dce4dSJason Molenda }
1145e8dce4dSJason Molenda 
115b9c1b51eSKate Stone void SBQueueItem::SetAddress(SBAddress addr) {
116*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(void, SBQueueItem, SetAddress, (lldb::SBAddress), addr);
117*baf5664fSJonas Devlieghere 
118b9c1b51eSKate Stone   if (m_queue_item_sp) {
1195e8dce4dSJason Molenda     m_queue_item_sp->SetAddress(addr.ref());
1205e8dce4dSJason Molenda   }
1215e8dce4dSJason Molenda }
1225e8dce4dSJason Molenda 
123b9c1b51eSKate Stone SBThread SBQueueItem::GetExtendedBacktraceThread(const char *type) {
124*baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(lldb::SBThread, SBQueueItem, GetExtendedBacktraceThread,
125*baf5664fSJonas Devlieghere                      (const char *), type);
126*baf5664fSJonas Devlieghere 
1275e8dce4dSJason Molenda   SBThread result;
128ac605f4aSJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
129b9c1b51eSKate Stone   if (m_queue_item_sp) {
130a8ff543cSJason Molenda     ProcessSP process_sp = m_queue_item_sp->GetProcessSP();
131a8ff543cSJason Molenda     Process::StopLocker stop_locker;
132b9c1b51eSKate Stone     if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) {
1335e8dce4dSJason Molenda       ThreadSP thread_sp;
1345e8dce4dSJason Molenda       ConstString type_const(type);
1355e8dce4dSJason Molenda       thread_sp = m_queue_item_sp->GetExtendedBacktraceThread(type_const);
136b9c1b51eSKate Stone       if (thread_sp) {
137b9c1b51eSKate Stone         // Save this in the Process' ExtendedThreadList so a strong pointer
13805097246SAdrian Prantl         // retains the object
139a8ff543cSJason Molenda         process_sp->GetExtendedThreadList().AddThread(thread_sp);
1405e8dce4dSJason Molenda         result.SetThread(thread_sp);
141b9c1b51eSKate Stone         if (log) {
142ac605f4aSJason Molenda           const char *queue_name = thread_sp->GetQueueName();
143ac605f4aSJason Molenda           if (queue_name == NULL)
144ac605f4aSJason Molenda             queue_name = "";
145b9c1b51eSKate Stone           log->Printf(
146b9c1b51eSKate Stone               "SBQueueItem(%p)::GetExtendedBacktraceThread() = new extended "
147b9c1b51eSKate Stone               "Thread created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
148324a1036SSaleem Abdulrasool               static_cast<void *>(m_queue_item_sp.get()),
149324a1036SSaleem Abdulrasool               static_cast<void *>(thread_sp.get()),
150b9c1b51eSKate Stone               static_cast<uint64_t>(thread_sp->GetQueueID()), queue_name);
151ac605f4aSJason Molenda         }
1525e8dce4dSJason Molenda       }
1535e8dce4dSJason Molenda     }
154a8ff543cSJason Molenda   }
155*baf5664fSJonas Devlieghere   return LLDB_RECORD_RESULT(result);
1565e8dce4dSJason Molenda }
157