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