180814287SRaphael Isemann //===-- SBQueueItem.cpp ---------------------------------------------------===//
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 
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"
18*1755f5b1SJonas Devlieghere #include "lldb/Utility/Instrumentation.h"
195e8dce4dSJason Molenda 
205e8dce4dSJason Molenda using namespace lldb;
215e8dce4dSJason Molenda using namespace lldb_private;
225e8dce4dSJason Molenda 
235e8dce4dSJason Molenda // Constructors
SBQueueItem()24*1755f5b1SJonas Devlieghere SBQueueItem::SBQueueItem() { LLDB_INSTRUMENT_VA(this); }
255e8dce4dSJason Molenda 
SBQueueItem(const QueueItemSP & queue_item_sp)26b9c1b51eSKate Stone SBQueueItem::SBQueueItem(const QueueItemSP &queue_item_sp)
27baf5664fSJonas Devlieghere     : m_queue_item_sp(queue_item_sp) {
28*1755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, queue_item_sp);
29baf5664fSJonas Devlieghere }
305e8dce4dSJason Molenda 
315e8dce4dSJason Molenda // Destructor
~SBQueueItem()32b9c1b51eSKate Stone SBQueueItem::~SBQueueItem() { m_queue_item_sp.reset(); }
335e8dce4dSJason Molenda 
IsValid() const34b9c1b51eSKate Stone bool SBQueueItem::IsValid() const {
35*1755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
367f5237bcSPavel Labath   return this->operator bool();
377f5237bcSPavel Labath }
operator bool() const387f5237bcSPavel Labath SBQueueItem::operator bool() const {
39*1755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
40baf5664fSJonas Devlieghere 
41248a1305SKonrad Kleine   return m_queue_item_sp.get() != nullptr;
425e8dce4dSJason Molenda }
435e8dce4dSJason Molenda 
Clear()44b9c1b51eSKate Stone void SBQueueItem::Clear() {
45*1755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
46baf5664fSJonas Devlieghere 
475e8dce4dSJason Molenda   m_queue_item_sp.reset();
485e8dce4dSJason Molenda }
495e8dce4dSJason Molenda 
SetQueueItem(const QueueItemSP & queue_item_sp)50b9c1b51eSKate Stone void SBQueueItem::SetQueueItem(const QueueItemSP &queue_item_sp) {
51*1755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, queue_item_sp);
52baf5664fSJonas Devlieghere 
535e8dce4dSJason Molenda   m_queue_item_sp = queue_item_sp;
545e8dce4dSJason Molenda }
555e8dce4dSJason Molenda 
GetKind() const56b9c1b51eSKate Stone lldb::QueueItemKind SBQueueItem::GetKind() const {
57*1755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
58baf5664fSJonas Devlieghere 
595e8dce4dSJason Molenda   QueueItemKind result = eQueueItemKindUnknown;
60b9c1b51eSKate Stone   if (m_queue_item_sp) {
615e8dce4dSJason Molenda     result = m_queue_item_sp->GetKind();
625e8dce4dSJason Molenda   }
635e8dce4dSJason Molenda   return result;
645e8dce4dSJason Molenda }
655e8dce4dSJason Molenda 
SetKind(lldb::QueueItemKind kind)66b9c1b51eSKate Stone void SBQueueItem::SetKind(lldb::QueueItemKind kind) {
67*1755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, kind);
68baf5664fSJonas Devlieghere 
69b9c1b51eSKate Stone   if (m_queue_item_sp) {
705e8dce4dSJason Molenda     m_queue_item_sp->SetKind(kind);
715e8dce4dSJason Molenda   }
725e8dce4dSJason Molenda }
735e8dce4dSJason Molenda 
GetAddress() const74b9c1b51eSKate Stone SBAddress SBQueueItem::GetAddress() const {
75*1755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
76baf5664fSJonas Devlieghere 
775e8dce4dSJason Molenda   SBAddress result;
78b9c1b51eSKate Stone   if (m_queue_item_sp) {
796cd4a4cdSJonas Devlieghere     result.SetAddress(m_queue_item_sp->GetAddress());
805e8dce4dSJason Molenda   }
81d232abc3SJonas Devlieghere   return result;
825e8dce4dSJason Molenda }
835e8dce4dSJason Molenda 
SetAddress(SBAddress addr)84b9c1b51eSKate Stone void SBQueueItem::SetAddress(SBAddress addr) {
85*1755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, addr);
86baf5664fSJonas Devlieghere 
87b9c1b51eSKate Stone   if (m_queue_item_sp) {
885e8dce4dSJason Molenda     m_queue_item_sp->SetAddress(addr.ref());
895e8dce4dSJason Molenda   }
905e8dce4dSJason Molenda }
915e8dce4dSJason Molenda 
GetExtendedBacktraceThread(const char * type)92b9c1b51eSKate Stone SBThread SBQueueItem::GetExtendedBacktraceThread(const char *type) {
93*1755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, type);
94baf5664fSJonas Devlieghere 
955e8dce4dSJason Molenda   SBThread result;
96b9c1b51eSKate Stone   if (m_queue_item_sp) {
97a8ff543cSJason Molenda     ProcessSP process_sp = m_queue_item_sp->GetProcessSP();
98a8ff543cSJason Molenda     Process::StopLocker stop_locker;
99b9c1b51eSKate Stone     if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) {
1005e8dce4dSJason Molenda       ThreadSP thread_sp;
1015e8dce4dSJason Molenda       ConstString type_const(type);
1025e8dce4dSJason Molenda       thread_sp = m_queue_item_sp->GetExtendedBacktraceThread(type_const);
103b9c1b51eSKate Stone       if (thread_sp) {
104b9c1b51eSKate Stone         // Save this in the Process' ExtendedThreadList so a strong pointer
10505097246SAdrian Prantl         // retains the object
106a8ff543cSJason Molenda         process_sp->GetExtendedThreadList().AddThread(thread_sp);
1075e8dce4dSJason Molenda         result.SetThread(thread_sp);
1085e8dce4dSJason Molenda       }
1095e8dce4dSJason Molenda     }
110a8ff543cSJason Molenda   }
111d232abc3SJonas Devlieghere   return result;
1125e8dce4dSJason Molenda }
113