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 "SBReproducerPrivate.h"
12 #include "lldb/API/SBAddress.h"
13 #include "lldb/API/SBQueueItem.h"
14 #include "lldb/API/SBThread.h"
15 #include "lldb/Core/Address.h"
16 #include "lldb/Target/Process.h"
17 #include "lldb/Target/QueueItem.h"
18 #include "lldb/Target/Thread.h"
19 #include "lldb/Utility/Log.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 //----------------------------------------------------------------------
25 // Constructors
26 //----------------------------------------------------------------------
27 SBQueueItem::SBQueueItem() : m_queue_item_sp() {
28   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBQueueItem);
29 }
30 
31 SBQueueItem::SBQueueItem(const QueueItemSP &queue_item_sp)
32     : m_queue_item_sp(queue_item_sp) {
33   LLDB_RECORD_CONSTRUCTOR(SBQueueItem, (const lldb::QueueItemSP &),
34                           queue_item_sp);
35 }
36 
37 //----------------------------------------------------------------------
38 // Destructor
39 //----------------------------------------------------------------------
40 SBQueueItem::~SBQueueItem() { m_queue_item_sp.reset(); }
41 
42 bool SBQueueItem::IsValid() const {
43   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBQueueItem, IsValid);
44 
45   bool is_valid = m_queue_item_sp.get() != NULL;
46   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
47   if (log)
48     log->Printf("SBQueueItem(%p)::IsValid() == %s",
49                 static_cast<void *>(m_queue_item_sp.get()),
50                 is_valid ? "true" : "false");
51   return is_valid;
52 }
53 
54 void SBQueueItem::Clear() {
55   LLDB_RECORD_METHOD_NO_ARGS(void, SBQueueItem, Clear);
56 
57   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
58   if (log)
59     log->Printf("SBQueueItem(%p)::Clear()",
60                 static_cast<void *>(m_queue_item_sp.get()));
61   m_queue_item_sp.reset();
62 }
63 
64 void SBQueueItem::SetQueueItem(const QueueItemSP &queue_item_sp) {
65   LLDB_RECORD_METHOD(void, SBQueueItem, SetQueueItem,
66                      (const lldb::QueueItemSP &), queue_item_sp);
67 
68   m_queue_item_sp = queue_item_sp;
69 }
70 
71 lldb::QueueItemKind SBQueueItem::GetKind() const {
72   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::QueueItemKind, SBQueueItem, GetKind);
73 
74   QueueItemKind result = eQueueItemKindUnknown;
75   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
76   if (m_queue_item_sp) {
77     result = m_queue_item_sp->GetKind();
78   }
79   if (log)
80     log->Printf("SBQueueItem(%p)::GetKind() == %d",
81                 static_cast<void *>(m_queue_item_sp.get()),
82                 static_cast<int>(result));
83   return result;
84 }
85 
86 void SBQueueItem::SetKind(lldb::QueueItemKind kind) {
87   LLDB_RECORD_METHOD(void, SBQueueItem, SetKind, (lldb::QueueItemKind), kind);
88 
89   if (m_queue_item_sp) {
90     m_queue_item_sp->SetKind(kind);
91   }
92 }
93 
94 SBAddress SBQueueItem::GetAddress() const {
95   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBAddress, SBQueueItem, GetAddress);
96 
97   SBAddress result;
98   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
99   if (m_queue_item_sp) {
100     result.SetAddress(&m_queue_item_sp->GetAddress());
101   }
102   if (log) {
103     StreamString sstr;
104     const Address *addr = result.get();
105     if (addr)
106       addr->Dump(&sstr, NULL, Address::DumpStyleModuleWithFileAddress,
107                  Address::DumpStyleInvalid, 4);
108     log->Printf("SBQueueItem(%p)::GetAddress() == SBAddress(%p): %s",
109                 static_cast<void *>(m_queue_item_sp.get()),
110                 static_cast<void *>(result.get()), sstr.GetData());
111   }
112   return LLDB_RECORD_RESULT(result);
113 }
114 
115 void SBQueueItem::SetAddress(SBAddress addr) {
116   LLDB_RECORD_METHOD(void, SBQueueItem, SetAddress, (lldb::SBAddress), addr);
117 
118   if (m_queue_item_sp) {
119     m_queue_item_sp->SetAddress(addr.ref());
120   }
121 }
122 
123 SBThread SBQueueItem::GetExtendedBacktraceThread(const char *type) {
124   LLDB_RECORD_METHOD(lldb::SBThread, SBQueueItem, GetExtendedBacktraceThread,
125                      (const char *), type);
126 
127   SBThread result;
128   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
129   if (m_queue_item_sp) {
130     ProcessSP process_sp = m_queue_item_sp->GetProcessSP();
131     Process::StopLocker stop_locker;
132     if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) {
133       ThreadSP thread_sp;
134       ConstString type_const(type);
135       thread_sp = m_queue_item_sp->GetExtendedBacktraceThread(type_const);
136       if (thread_sp) {
137         // Save this in the Process' ExtendedThreadList so a strong pointer
138         // retains the object
139         process_sp->GetExtendedThreadList().AddThread(thread_sp);
140         result.SetThread(thread_sp);
141         if (log) {
142           const char *queue_name = thread_sp->GetQueueName();
143           if (queue_name == NULL)
144             queue_name = "";
145           log->Printf(
146               "SBQueueItem(%p)::GetExtendedBacktraceThread() = new extended "
147               "Thread created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
148               static_cast<void *>(m_queue_item_sp.get()),
149               static_cast<void *>(thread_sp.get()),
150               static_cast<uint64_t>(thread_sp->GetQueueID()), queue_name);
151         }
152       }
153     }
154   }
155   return LLDB_RECORD_RESULT(result);
156 }
157