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