LLDB  mainline
SystemRuntimeMacOSX.h
Go to the documentation of this file.
1 //===-- SystemRuntimeMacOSX.h -----------------------------------*- 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 #ifndef liblldb_SystemRuntimeMacOSX_h_
10 #define liblldb_SystemRuntimeMacOSX_h_
11 
12 #include <mutex>
13 #include <string>
14 #include <vector>
15 
16 // Other libraries and framework include
17 #include "lldb/Core/ModuleList.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Target/QueueItem.h"
22 #include "lldb/Utility/FileSpec.h"
24 #include "lldb/Utility/UUID.h"
25 
28 #include "AppleGetQueuesHandler.h"
30 
32 public:
34 
35  ~SystemRuntimeMacOSX() override;
36 
37  // Static Functions
38  static void Initialize();
39 
40  static void Terminate();
41 
43 
44  static const char *GetPluginDescriptionStatic();
45 
48 
49  // instance methods
50 
51  void Clear(bool clear_process);
52 
53  void Detach() override;
54 
55  const std::vector<lldb_private::ConstString> &
56  GetExtendedBacktraceTypes() override;
57 
58  lldb::ThreadSP
59  GetExtendedBacktraceThread(lldb::ThreadSP thread,
60  lldb_private::ConstString type) override;
61 
62  lldb::ThreadSP
63  GetExtendedBacktraceForQueueItem(lldb::QueueItemSP queue_item_sp,
64  lldb_private::ConstString type) override;
65 
66  lldb::ThreadSP GetExtendedBacktraceFromItemRef(lldb::addr_t item_ref);
67 
68  void PopulateQueueList(lldb_private::QueueList &queue_list) override;
69 
70  void PopulateQueuesUsingLibBTR(lldb::addr_t queues_buffer,
71  uint64_t queues_buffer_size, uint64_t count,
72  lldb_private::QueueList &queue_list);
73 
75  uint64_t items_buffer_size,
76  uint64_t count,
77  lldb_private::Queue *queue);
78 
79  std::string
80  GetQueueNameFromThreadQAddress(lldb::addr_t dispatch_qaddr) override;
81 
83  GetQueueIDFromThreadQAddress(lldb::addr_t dispatch_qaddr) override;
84 
86  lldb::addr_t dispatch_qaddr) override;
87 
89 
91  lldb::addr_t item_ref) override;
92 
93  lldb::QueueKind GetQueueKind(lldb::addr_t dispatch_queue_addr) override;
94 
97 
98  bool SafeToCallFunctionsOnThisThread(lldb::ThreadSP thread_sp) override;
99 
100  // PluginInterface protocol
102 
103  uint32_t GetPluginVersion() override;
104 
105 protected:
107  mutable std::recursive_mutex m_mutex;
108 
109 private:
110  struct libBacktraceRecording_info {
111  uint16_t queue_info_version;
112  uint16_t queue_info_data_offset;
113  uint16_t item_info_version;
114  uint16_t item_info_data_offset;
115 
116  libBacktraceRecording_info()
117  : queue_info_version(0), queue_info_data_offset(0),
118  item_info_version(0), item_info_data_offset(0) {}
119  };
120 
121  // A structure which reflects the data recorded in the
122  // libBacktraceRecording introspection_dispatch_item_info_s.
123  struct ItemInfo {
124  lldb::addr_t item_that_enqueued_this;
125  lldb::addr_t function_or_block;
126  uint64_t enqueuing_thread_id;
127  uint64_t enqueuing_queue_serialnum;
128  uint64_t target_queue_serialnum;
129  uint32_t enqueuing_callstack_frame_count;
130  uint32_t stop_id;
131  std::vector<lldb::addr_t> enqueuing_callstack;
132  std::string enqueuing_thread_label;
133  std::string enqueuing_queue_label;
134  std::string target_queue_label;
135  };
136 
137  // The offsets of different fields of the dispatch_queue_t structure in
138  // a thread/queue process.
139  // Based on libdispatch src/queue_private.h, struct dispatch_queue_offsets_s
140  // With dqo_version 1-3, the dqo_label field is a per-queue value and cannot
141  // be cached.
142  // With dqo_version 4 (Mac OS X 10.9 / iOS 7), dqo_label is a constant value
143  // that can be cached.
144  struct LibdispatchOffsets {
145  uint16_t dqo_version;
146  uint16_t dqo_label;
147  uint16_t dqo_label_size;
148  uint16_t dqo_flags;
149  uint16_t dqo_flags_size;
150  uint16_t dqo_serialnum;
151  uint16_t dqo_serialnum_size;
152  uint16_t dqo_width;
153  uint16_t dqo_width_size;
154  uint16_t dqo_running;
155  uint16_t dqo_running_size;
156 
157  uint16_t dqo_suspend_cnt; // version 5 and later, starting with Mac OS X
158  // 10.10/iOS 8
159  uint16_t dqo_suspend_cnt_size; // version 5 and later, starting with Mac OS
160  // X 10.10/iOS 8
161  uint16_t dqo_target_queue; // version 5 and later, starting with Mac OS X
162  // 10.10/iOS 8
163  uint16_t dqo_target_queue_size; // version 5 and later, starting with Mac OS
164  // X 10.10/iOS 8
165  uint16_t
166  dqo_priority; // version 5 and later, starting with Mac OS X 10.10/iOS 8
167  uint16_t dqo_priority_size; // version 5 and later, starting with Mac OS X
168  // 10.10/iOS 8
169 
170  LibdispatchOffsets() {
171  dqo_version = UINT16_MAX;
172  dqo_flags = UINT16_MAX;
173  dqo_serialnum = UINT16_MAX;
174  dqo_label = UINT16_MAX;
175  dqo_width = UINT16_MAX;
176  dqo_running = UINT16_MAX;
177  dqo_suspend_cnt = UINT16_MAX;
178  dqo_target_queue = UINT16_MAX;
179  dqo_target_queue = UINT16_MAX;
180  dqo_priority = UINT16_MAX;
181  }
182 
183  bool IsValid() { return dqo_version != UINT16_MAX; }
184 
185  bool LabelIsValid() { return dqo_label != UINT16_MAX; }
186  };
187 
188  struct LibdispatchVoucherOffsets {
189  uint16_t vo_version;
190  uint16_t vo_activity_ids_count;
191  uint16_t vo_activity_ids_count_size;
192  uint16_t vo_activity_ids_array;
193  uint16_t vo_activity_ids_array_entry_size;
194 
195  LibdispatchVoucherOffsets()
196  : vo_version(UINT16_MAX), vo_activity_ids_count(UINT16_MAX),
197  vo_activity_ids_count_size(UINT16_MAX),
198  vo_activity_ids_array(UINT16_MAX),
199  vo_activity_ids_array_entry_size(UINT16_MAX) {}
200 
201  bool IsValid() { return vo_version != UINT16_MAX; }
202  };
203 
204  struct LibdispatchTSDIndexes {
205  uint16_t dti_version;
206  uint64_t dti_queue_index;
207  uint64_t dti_voucher_index;
208  uint64_t dti_qos_class_index;
209 
210  LibdispatchTSDIndexes()
211  : dti_version(UINT16_MAX), dti_queue_index(UINT64_MAX),
212  dti_voucher_index(UINT64_MAX), dti_qos_class_index(UINT64_MAX) {}
213 
214  bool IsValid() { return dti_version != UINT16_MAX; }
215  };
216 
217  struct LibpthreadOffsets {
218  uint16_t plo_version;
219  uint16_t plo_pthread_tsd_base_offset;
220  uint16_t plo_pthread_tsd_base_address_offset;
221  uint16_t plo_pthread_tsd_entry_size;
222 
223  LibpthreadOffsets()
224  : plo_version(UINT16_MAX), plo_pthread_tsd_base_offset(UINT16_MAX),
225  plo_pthread_tsd_base_address_offset(UINT16_MAX),
226  plo_pthread_tsd_entry_size(UINT16_MAX) {}
227 
228  bool IsValid() { return plo_version != UINT16_MAX; }
229  };
230 
231  // The libBacktraceRecording function
232  // __introspection_dispatch_queue_get_pending_items has
233  // two forms. It can either return a simple array of item_refs (void *) size
234  // or it can return
235  // a header with uint32_t version, a uint32_t size of item, and then an array
236  // of item_refs (void*)
237  // and code addresses (void*) for all the pending blocks.
238 
239  struct ItemRefAndCodeAddress {
240  lldb::addr_t item_ref;
241  lldb::addr_t code_address;
242  };
243 
244  struct PendingItemsForQueue {
245  bool new_style; // new-style means both item_refs and code_addresses avail
246  // old-style means only item_refs is filled in
247  std::vector<ItemRefAndCodeAddress> item_refs_and_code_addresses;
248  };
249 
250  bool BacktraceRecordingHeadersInitialized();
251 
252  void ReadLibdispatchOffsetsAddress();
253 
254  void ReadLibdispatchOffsets();
255 
256  void ReadLibpthreadOffsetsAddress();
257 
258  void ReadLibpthreadOffsets();
259 
260  void ReadLibdispatchTSDIndexesAddress();
261 
262  void ReadLibdispatchTSDIndexes();
263 
264  PendingItemsForQueue GetPendingItemRefsForQueue(lldb::addr_t queue);
265 
266  ItemInfo ExtractItemInfoFromBuffer(lldb_private::DataExtractor &extractor);
267 
268  lldb_private::AppleGetQueuesHandler m_get_queues_handler;
269  lldb_private::AppleGetPendingItemsHandler m_get_pending_items_handler;
270  lldb_private::AppleGetItemInfoHandler m_get_item_info_handler;
271  lldb_private::AppleGetThreadItemInfoHandler m_get_thread_item_info_handler;
272 
273  lldb::addr_t m_page_to_free;
274  uint64_t m_page_to_free_size;
275  libBacktraceRecording_info m_lib_backtrace_recording_info;
276 
277  lldb::addr_t m_dispatch_queue_offsets_addr;
278  struct LibdispatchOffsets m_libdispatch_offsets;
279 
280  lldb::addr_t m_libpthread_layout_offsets_addr;
281  struct LibpthreadOffsets m_libpthread_offsets;
282 
283  lldb::addr_t m_dispatch_tsd_indexes_addr;
284  struct LibdispatchTSDIndexes m_libdispatch_tsd_indexes;
285 
286  lldb::addr_t m_dispatch_voucher_offsets_addr;
287  struct LibdispatchVoucherOffsets m_libdispatch_voucher_offsets;
288 
289  DISALLOW_COPY_AND_ASSIGN(SystemRuntimeMacOSX);
290 };
291 
292 #endif // liblldb_SystemRuntimeMacOSX_h_
An data extractor class.
Definition: DataExtractor.h:47
std::string GetQueueNameFromThreadQAddress(lldb::addr_t dispatch_qaddr) override
Get the queue name for a thread given a thread&#39;s dispatch_qaddr.
void AddThreadExtendedInfoPacketHints(lldb_private::StructuredData::ObjectSP dict) override
Add key-value pairs to the StructuredData dictionary object with information debugserver may need whe...
std::recursive_mutex m_mutex
lldb::addr_t GetLibdispatchQueueAddressFromThreadQAddress(lldb::addr_t dispatch_qaddr) override
Get the libdispatch_queue_t address for the queue given the thread&#39;s dispatch_qaddr.
void Detach() override
Called before detaching from a process.
static const char * GetPluginDescriptionStatic()
uint32_t GetPluginVersion() override
void CompleteQueueItem(lldb_private::QueueItem *queue_item, lldb::addr_t item_ref) override
Complete the fields in a QueueItem.
lldb::ThreadSP GetExtendedBacktraceFromItemRef(lldb::addr_t item_ref)
lldb_private::ConstString GetPluginName() override
uint64_t user_id_t
Definition: lldb-types.h:84
bool SafeToCallFunctionsOnThisThread(lldb::ThreadSP thread_sp) override
Determine whether it is safe to run an expression on a given thread.
const std::vector< lldb_private::ConstString > & GetExtendedBacktraceTypes() override
Return a list of thread origin extended backtraces that may be available.
lldb::QueueKind GetQueueKind(lldb::addr_t dispatch_queue_addr) override
Retrieve the Queue kind for the queue at a thread&#39;s dispatch_qaddr.
A plug-in interface definition class for debugging a process.
Definition: Process.h:353
lldb::user_id_t m_break_id
lldb::ThreadSP GetExtendedBacktraceThread(lldb::ThreadSP thread, lldb_private::ConstString type) override
Return a Thread which shows the origin of this thread&#39;s creation.
uint64_t queue_id_t
Definition: lldb-types.h:91
void PopulatePendingQueuesUsingLibBTR(lldb::addr_t items_buffer, uint64_t items_buffer_size, uint64_t count, lldb_private::Queue *queue)
void PopulateQueueList(lldb_private::QueueList &queue_list) override
Populate the Process&#39; QueueList with libdispatch / GCD queues that exist.
uint64_t addr_t
Definition: lldb-types.h:83
A uniqued constant string class.
Definition: ConstString.h:38
void PopulatePendingItemsForQueue(lldb_private::Queue *queue) override
Get the pending work items for a libdispatch Queue.
std::shared_ptr< Object > ObjectSP
lldb::ThreadSP GetExtendedBacktraceForQueueItem(lldb::QueueItemSP queue_item_sp, lldb_private::ConstString type) override
Get the extended backtrace thread for a QueueItem.
static lldb_private::ConstString GetPluginNameStatic()
lldb::queue_id_t GetQueueIDFromThreadQAddress(lldb::addr_t dispatch_qaddr) override
Get the QueueID for the libdispatch queue given the thread&#39;s dispatch_qaddr.
#define UINT64_MAX
Definition: lldb-defines.h:35
void PopulateQueuesUsingLibBTR(lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count, lldb_private::QueueList &queue_list)
void Clear(bool clear_process)
A plug-in interface definition class for system runtimes.
Definition: SystemRuntime.h:42
static lldb_private::SystemRuntime * CreateInstance(lldb_private::Process *process)
SystemRuntimeMacOSX(lldb_private::Process *process)