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