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