1 //===-- SBQueueItem.cpp -----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/lldb-forward.h"
10 
11 #include "lldb/API/SBAddress.h"
12 #include "lldb/API/SBQueueItem.h"
13 #include "lldb/API/SBThread.h"
14 #include "lldb/Core/Address.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/QueueItem.h"
17 #include "lldb/Target/Thread.h"
18 #include "lldb/Utility/Log.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 //----------------------------------------------------------------------
24 // Constructors
25 //----------------------------------------------------------------------
26 SBQueueItem::SBQueueItem() : m_queue_item_sp() {}
27 
28 SBQueueItem::SBQueueItem(const QueueItemSP &queue_item_sp)
29     : m_queue_item_sp(queue_item_sp) {}
30 
31 //----------------------------------------------------------------------
32 // Destructor
33 //----------------------------------------------------------------------
34 SBQueueItem::~SBQueueItem() { m_queue_item_sp.reset(); }
35 
36 bool SBQueueItem::IsValid() const {
37   bool is_valid = m_queue_item_sp.get() != NULL;
38   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
39   if (log)
40     log->Printf("SBQueueItem(%p)::IsValid() == %s",
41                 static_cast<void *>(m_queue_item_sp.get()),
42                 is_valid ? "true" : "false");
43   return is_valid;
44 }
45 
46 void SBQueueItem::Clear() {
47   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
48   if (log)
49     log->Printf("SBQueueItem(%p)::Clear()",
50                 static_cast<void *>(m_queue_item_sp.get()));
51   m_queue_item_sp.reset();
52 }
53 
54 void SBQueueItem::SetQueueItem(const QueueItemSP &queue_item_sp) {
55   m_queue_item_sp = queue_item_sp;
56 }
57 
58 lldb::QueueItemKind SBQueueItem::GetKind() const {
59   QueueItemKind result = eQueueItemKindUnknown;
60   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
61   if (m_queue_item_sp) {
62     result = m_queue_item_sp->GetKind();
63   }
64   if (log)
65     log->Printf("SBQueueItem(%p)::GetKind() == %d",
66                 static_cast<void *>(m_queue_item_sp.get()),
67                 static_cast<int>(result));
68   return result;
69 }
70 
71 void SBQueueItem::SetKind(lldb::QueueItemKind kind) {
72   if (m_queue_item_sp) {
73     m_queue_item_sp->SetKind(kind);
74   }
75 }
76 
77 SBAddress SBQueueItem::GetAddress() const {
78   SBAddress result;
79   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
80   if (m_queue_item_sp) {
81     result.SetAddress(&m_queue_item_sp->GetAddress());
82   }
83   if (log) {
84     StreamString sstr;
85     const Address *addr = result.get();
86     if (addr)
87       addr->Dump(&sstr, NULL, Address::DumpStyleModuleWithFileAddress,
88                  Address::DumpStyleInvalid, 4);
89     log->Printf("SBQueueItem(%p)::GetAddress() == SBAddress(%p): %s",
90                 static_cast<void *>(m_queue_item_sp.get()),
91                 static_cast<void *>(result.get()), sstr.GetData());
92   }
93   return result;
94 }
95 
96 void SBQueueItem::SetAddress(SBAddress addr) {
97   if (m_queue_item_sp) {
98     m_queue_item_sp->SetAddress(addr.ref());
99   }
100 }
101 
102 SBThread SBQueueItem::GetExtendedBacktraceThread(const char *type) {
103   SBThread result;
104   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
105   if (m_queue_item_sp) {
106     ProcessSP process_sp = m_queue_item_sp->GetProcessSP();
107     Process::StopLocker stop_locker;
108     if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) {
109       ThreadSP thread_sp;
110       ConstString type_const(type);
111       thread_sp = m_queue_item_sp->GetExtendedBacktraceThread(type_const);
112       if (thread_sp) {
113         // Save this in the Process' ExtendedThreadList so a strong pointer
114         // retains the object
115         process_sp->GetExtendedThreadList().AddThread(thread_sp);
116         result.SetThread(thread_sp);
117         if (log) {
118           const char *queue_name = thread_sp->GetQueueName();
119           if (queue_name == NULL)
120             queue_name = "";
121           log->Printf(
122               "SBQueueItem(%p)::GetExtendedBacktraceThread() = new extended "
123               "Thread created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
124               static_cast<void *>(m_queue_item_sp.get()),
125               static_cast<void *>(thread_sp.get()),
126               static_cast<uint64_t>(thread_sp->GetQueueID()), queue_name);
127         }
128       }
129     }
130   }
131   return result;
132 }
133