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