1 //===-- QueueItem.h ---------------------------------------------*- 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 #ifndef liblldb_QueueItem_h_
11 #define liblldb_QueueItem_h_
12 
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
17 #include "lldb/lldb-enumerations.h"
18 #include "lldb/lldb-forward.h"
19 #include "lldb/lldb-private.h"
20 
21 #include "lldb/Core/Address.h"
22 #include "lldb/Utility/ConstString.h"
23 
24 namespace lldb_private {
25 
26 //------------------------------------------------------------------
27 // QueueItem:
28 // This class represents a work item enqueued on a libdispatch aka Grand
29 // Central Dispatch (GCD) queue.  Most often, this will be a function or block.
30 // "enqueued" here means that the work item has been added to a queue but it
31 // has not yet started executing.  When it is "dequeued", execution of the item
32 // begins.
33 //------------------------------------------------------------------
34 
35 class QueueItem : public std::enable_shared_from_this<QueueItem> {
36 public:
37   QueueItem(lldb::QueueSP queue_sp, lldb::ProcessSP process_sp,
38             lldb::addr_t item_ref, lldb_private::Address address);
39 
40   ~QueueItem();
41 
42   //------------------------------------------------------------------
43   /// Get the kind of work item this is
44   ///
45   /// @return
46   ///     The type of work item that this QueueItem object
47   ///     represents.  eQueueItemKindUnknown may be returned.
48   //------------------------------------------------------------------
49   lldb::QueueItemKind GetKind();
50 
51   //------------------------------------------------------------------
52   /// Set the type of work item this is
53   ///
54   /// @param [in] item_kind
55   ///     Set the kind of this work item object.
56   //------------------------------------------------------------------
57   void SetKind(lldb::QueueItemKind item_kind);
58 
59   //------------------------------------------------------------------
60   /// Get the code address that will be executed when this work item
61   /// is executed.
62   ///
63   /// @return
64   ///     The address that will be invoked when this work item is
65   ///     executed.  Not all types of QueueItems will have an
66   ///     address associated with them; check that the returned
67   ///     Address is valid, or check that the WorkItemKind is a
68   ///     kind that involves an address, such as eQueueItemKindFunction
69   ///     or eQueueItemKindBlock.
70   //------------------------------------------------------------------
71   lldb_private::Address &GetAddress();
72 
73   //------------------------------------------------------------------
74   /// Set the work item address for this object
75   ///
76   /// @param [in] addr
77   ///     The address that will be invoked when this work item
78   ///     is executed.
79   //------------------------------------------------------------------
80   void SetAddress(lldb_private::Address addr);
81 
82   //------------------------------------------------------------------
83   /// Check if this QueueItem object is valid
84   ///
85   /// If the weak pointer to the parent Queue cannot be revivified,
86   /// it is invalid.
87   ///
88   /// @return
89   ///     True if this object is valid.
90   //------------------------------------------------------------------
IsValid()91   bool IsValid() { return m_queue_wp.lock() != nullptr; }
92 
93   //------------------------------------------------------------------
94   /// Get an extended backtrace thread for this queue item, if available
95   ///
96   /// If the backtrace/thread information was collected when this item
97   /// was enqueued, this call will provide it.
98   ///
99   /// @param [in] type
100   ///     The type of extended backtrace being requested, e.g. "libdispatch"
101   ///     or "pthread".
102   ///
103   /// @return
104   ///     A thread shared pointer which will have a reference to an extended
105   ///     thread if one was available.
106   //------------------------------------------------------------------
107   lldb::ThreadSP GetExtendedBacktraceThread(ConstString type);
108 
SetItemThatEnqueuedThis(lldb::addr_t address_of_item)109   void SetItemThatEnqueuedThis(lldb::addr_t address_of_item) {
110     m_item_that_enqueued_this_ref = address_of_item;
111   }
112 
113   lldb::addr_t GetItemThatEnqueuedThis();
114 
SetEnqueueingThreadID(lldb::tid_t tid)115   void SetEnqueueingThreadID(lldb::tid_t tid) { m_enqueueing_thread_id = tid; }
116 
117   lldb::tid_t GetEnqueueingThreadID();
118 
SetEnqueueingQueueID(lldb::queue_id_t qid)119   void SetEnqueueingQueueID(lldb::queue_id_t qid) {
120     m_enqueueing_queue_id = qid;
121   }
122 
123   lldb::queue_id_t GetEnqueueingQueueID();
124 
SetTargetQueueID(lldb::queue_id_t qid)125   void SetTargetQueueID(lldb::queue_id_t qid) { m_target_queue_id = qid; }
126 
SetStopID(uint32_t stop_id)127   void SetStopID(uint32_t stop_id) { m_stop_id = stop_id; }
128 
129   uint32_t GetStopID();
130 
SetEnqueueingBacktrace(std::vector<lldb::addr_t> backtrace)131   void SetEnqueueingBacktrace(std::vector<lldb::addr_t> backtrace) {
132     m_backtrace = backtrace;
133   }
134 
135   std::vector<lldb::addr_t> &GetEnqueueingBacktrace();
136 
SetThreadLabel(std::string thread_name)137   void SetThreadLabel(std::string thread_name) { m_thread_label = thread_name; }
138 
139   std::string GetThreadLabel();
140 
SetQueueLabel(std::string queue_name)141   void SetQueueLabel(std::string queue_name) { m_queue_label = queue_name; }
142 
143   std::string GetQueueLabel();
144 
SetTargetQueueLabel(std::string queue_name)145   void SetTargetQueueLabel(std::string queue_name) {
146     m_target_queue_label = queue_name;
147   }
148 
149   lldb::ProcessSP GetProcessSP();
150 
151 protected:
152   void FetchEntireItem();
153 
154   lldb::QueueWP m_queue_wp;
155   lldb::ProcessWP m_process_wp;
156 
157   lldb::addr_t m_item_ref; // the token we can be used to fetch more information
158                            // about this queue item
159   lldb_private::Address m_address;
160   bool m_have_fetched_entire_item;
161 
162   lldb::QueueItemKind m_kind;
163   lldb::addr_t m_item_that_enqueued_this_ref; // a handle that we can pass into
164                                               // libBacktraceRecording
165   // to get the QueueItem that enqueued this item
166   lldb::tid_t m_enqueueing_thread_id; // thread that enqueued this item
167   lldb::queue_id_t
168       m_enqueueing_queue_id; // Queue that enqueued this item, if it was a queue
169   lldb::queue_id_t m_target_queue_id;
170   uint32_t m_stop_id; // indicates when this backtrace was recorded in time
171   std::vector<lldb::addr_t> m_backtrace;
172   std::string m_thread_label;
173   std::string m_queue_label;
174   std::string m_target_queue_label;
175 
176 private:
177   DISALLOW_COPY_AND_ASSIGN(QueueItem);
178 };
179 
180 } // namespace lldb_private
181 
182 #endif // liblldb_QueueItem_h_
183