blob: 48d1f7fefb32c8c467bb176347bbba8e87b5a094 [file] [log] [blame]
Jason Molendaa7b5afa2013-11-15 00:17:32 +00001//===-- 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// C Includes
14// C++ Includes
15#include <map>
16#include <vector>
17#include <string>
18
19// Other libraries and framework includes
Jim Ingham46d005d2014-04-02 22:53:21 +000020
Jason Molendaa7b5afa2013-11-15 00:17:32 +000021#include "lldb/Target/SystemRuntime.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000022#include "lldb/Core/ConstString.h"
23#include "lldb/Core/ModuleList.h"
Jason Molenda705b1802014-06-13 02:37:02 +000024#include "lldb/Core/StructuredData.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000025#include "lldb/Core/UUID.h"
Jason Molenda705b1802014-06-13 02:37:02 +000026#include "lldb/Host/FileSpec.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000027#include "lldb/Host/Mutex.h"
28#include "lldb/Target/Process.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000029#include "lldb/Target/QueueItem.h"
30
31#include "AppleGetItemInfoHandler.h"
32#include "AppleGetQueuesHandler.h"
33#include "AppleGetPendingItemsHandler.h"
34#include "AppleGetThreadItemInfoHandler.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000035
36class SystemRuntimeMacOSX : public lldb_private::SystemRuntime
37{
38public:
39 //------------------------------------------------------------------
40 // Static Functions
41 //------------------------------------------------------------------
42 static void
43 Initialize();
44
45 static void
46 Terminate();
47
48 static lldb_private::ConstString
49 GetPluginNameStatic();
50
51 static const char *
52 GetPluginDescriptionStatic();
53
54 static lldb_private::SystemRuntime *
55 CreateInstance (lldb_private::Process *process);
56
Jason Molenda2fd83352014-02-05 05:44:54 +000057
58 //------------------------------------------------------------------
59 // instance methods
60 //------------------------------------------------------------------
61
Jason Molendaa7b5afa2013-11-15 00:17:32 +000062 SystemRuntimeMacOSX (lldb_private::Process *process);
63
64 virtual
65 ~SystemRuntimeMacOSX ();
66
67 void
68 Clear (bool clear_process);
69
70 void
Jason Molenda2fd83352014-02-05 05:44:54 +000071 Detach ();
Jason Molendaa7b5afa2013-11-15 00:17:32 +000072
73 const std::vector<lldb_private::ConstString> &
74 GetExtendedBacktraceTypes ();
75
76 lldb::ThreadSP
77 GetExtendedBacktraceThread (lldb::ThreadSP thread, lldb_private::ConstString type);
78
Jason Molenda2fd83352014-02-05 05:44:54 +000079 lldb::ThreadSP
80 GetExtendedBacktraceForQueueItem (lldb::QueueItemSP queue_item_sp, lldb_private::ConstString type);
Jason Molendaa7b5afa2013-11-15 00:17:32 +000081
Jason Molenda2fd83352014-02-05 05:44:54 +000082 lldb::ThreadSP
83 GetExtendedBacktraceFromItemRef (lldb::addr_t item_ref);
Jason Molendaa7b5afa2013-11-15 00:17:32 +000084
Jason Molenda2fd83352014-02-05 05:44:54 +000085 void
86 PopulateQueueList (lldb_private::QueueList &queue_list);
Jason Molendaa7b5afa2013-11-15 00:17:32 +000087
Jason Molenda2fd83352014-02-05 05:44:54 +000088 void
89 PopulateQueuesUsingLibBTR (lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count, lldb_private::QueueList &queue_list);
90
91 void
92 PopulatePendingQueuesUsingLibBTR (lldb::addr_t items_buffer, uint64_t items_buffer_size, uint64_t count, lldb_private::Queue *queue);
93
94 std::string
95 GetQueueNameFromThreadQAddress (lldb::addr_t dispatch_qaddr);
96
97 lldb::queue_id_t
98 GetQueueIDFromThreadQAddress (lldb::addr_t dispatch_qaddr);
99
Jason Molendaaac16e02014-03-13 02:54:54 +0000100 lldb::addr_t
101 GetLibdispatchQueueAddressFromThreadQAddress (lldb::addr_t dispatch_qaddr);
102
Jason Molenda2fd83352014-02-05 05:44:54 +0000103 void
104 PopulatePendingItemsForQueue (lldb_private::Queue *queue);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000105
Jason Molendae32cd192014-03-10 08:42:03 +0000106 void
107 CompleteQueueItem (lldb_private::QueueItem *queue_item, lldb::addr_t item_ref);
108
Jason Molendaaac16e02014-03-13 02:54:54 +0000109 virtual lldb::QueueKind
110 GetQueueKind (lldb::addr_t dispatch_queue_addr);
111
Jason Molenda705b1802014-06-13 02:37:02 +0000112 virtual void
113 AddThreadExtendedInfoPacketHints (lldb_private::StructuredData::ObjectSP dict);
114
Jason Molendab4892cd2014-05-13 22:02:48 +0000115 virtual bool
116 SafeToCallFunctionsOnThisThread (lldb::ThreadSP thread_sp);
117
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000118 //------------------------------------------------------------------
119 // PluginInterface protocol
120 //------------------------------------------------------------------
121 virtual lldb_private::ConstString
122 GetPluginName();
123
124 virtual uint32_t
125 GetPluginVersion();
126
Jason Molenda5e8dce42013-12-13 00:29:16 +0000127
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000128protected:
129 lldb::user_id_t m_break_id;
130 mutable lldb_private::Mutex m_mutex;
131
132private:
133
Jason Molenda2fd83352014-02-05 05:44:54 +0000134 struct libBacktraceRecording_info {
135 uint16_t queue_info_version;
136 uint16_t queue_info_data_offset;
137 uint16_t item_info_version;
138 uint16_t item_info_data_offset;
139
140 libBacktraceRecording_info () :
141 queue_info_version(0),
142 queue_info_data_offset(0),
143 item_info_version(0),
144 item_info_data_offset(0) {}
145 };
146
147
148 // A structure which reflects the data recorded in the
149 // libBacktraceRecording introspection_dispatch_item_info_s.
150 struct ItemInfo {
151 lldb::addr_t item_that_enqueued_this;
152 lldb::addr_t function_or_block;
153 uint64_t enqueuing_thread_id;
154 uint64_t enqueuing_queue_serialnum;
155 uint64_t target_queue_serialnum;
156 uint32_t enqueuing_callstack_frame_count;
157 uint32_t stop_id;
158 std::vector<lldb::addr_t> enqueuing_callstack;
159 std::string enqueuing_thread_label;
160 std::string enqueuing_queue_label;
161 std::string target_queue_label;
162 };
163
164 // The offsets of different fields of the dispatch_queue_t structure in
165 // a thread/queue process.
166 // Based on libdispatch src/queue_private.h, struct dispatch_queue_offsets_s
167 // With dqo_version 1-3, the dqo_label field is a per-queue value and cannot be cached.
168 // With dqo_version 4 (Mac OS X 10.9 / iOS 7), dqo_label is a constant value that can be cached.
169 struct LibdispatchOffsets
170 {
171 uint16_t dqo_version;
172 uint16_t dqo_label;
173 uint16_t dqo_label_size;
174 uint16_t dqo_flags;
175 uint16_t dqo_flags_size;
176 uint16_t dqo_serialnum;
177 uint16_t dqo_serialnum_size;
178 uint16_t dqo_width;
179 uint16_t dqo_width_size;
180 uint16_t dqo_running;
181 uint16_t dqo_running_size;
182
Jason Molenda705b1802014-06-13 02:37:02 +0000183 uint16_t dqo_suspend_cnt; // version 5 and later, starting with Mac OS X 10.10/iOS 8
184 uint16_t dqo_suspend_cnt_size; // version 5 and later, starting with Mac OS X 10.10/iOS 8
185 uint16_t dqo_target_queue; // version 5 and later, starting with Mac OS X 10.10/iOS 8
186 uint16_t dqo_target_queue_size; // version 5 and later, starting with Mac OS X 10.10/iOS 8
187 uint16_t dqo_priority; // version 5 and later, starting with Mac OS X 10.10/iOS 8
188 uint16_t dqo_priority_size; // version 5 and later, starting with Mac OS X 10.10/iOS 8
189
Jason Molenda2fd83352014-02-05 05:44:54 +0000190 LibdispatchOffsets ()
191 {
192 dqo_version = UINT16_MAX;
193 dqo_flags = UINT16_MAX;
194 dqo_serialnum = UINT16_MAX;
195 dqo_label = UINT16_MAX;
196 dqo_width = UINT16_MAX;
197 dqo_running = UINT16_MAX;
Jason Molenda705b1802014-06-13 02:37:02 +0000198 dqo_suspend_cnt = UINT16_MAX;
199 dqo_target_queue = UINT16_MAX;
200 dqo_target_queue = UINT16_MAX;
201 dqo_priority = UINT16_MAX;
Jason Molenda2fd83352014-02-05 05:44:54 +0000202 };
203
204 bool
205 IsValid ()
206 {
207 return dqo_version != UINT16_MAX;
208 }
209
210 bool
211 LabelIsValid ()
212 {
213 return dqo_label != UINT16_MAX;
214 }
215 };
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000216
Jason Molenda705b1802014-06-13 02:37:02 +0000217 struct LibdispatchVoucherOffsets
218 {
219 uint16_t vo_version;
220 uint16_t vo_activity_ids_count;
221 uint16_t vo_activity_ids_count_size;
222 uint16_t vo_activity_ids_array;
223 uint16_t vo_activity_ids_array_entry_size;
224
225 LibdispatchVoucherOffsets () :
226 vo_version (UINT16_MAX),
227 vo_activity_ids_count (UINT16_MAX),
228 vo_activity_ids_count_size (UINT16_MAX),
229 vo_activity_ids_array (UINT16_MAX),
230 vo_activity_ids_array_entry_size (UINT16_MAX)
231 { }
232
233 bool IsValid () { return vo_version != UINT16_MAX; }
234 };
235
236 struct LibdispatchTSDIndexes
237 {
238 uint16_t dti_version;
239 uint64_t dti_queue_index;
240 uint64_t dti_voucher_index;
241 uint64_t dti_qos_class_index;
242
243 LibdispatchTSDIndexes () :
244 dti_version (UINT16_MAX),
245 dti_queue_index (UINT64_MAX),
246 dti_voucher_index (UINT64_MAX),
247 dti_qos_class_index (UINT64_MAX)
248 { }
249
250 bool IsValid () { return dti_version != UINT16_MAX; }
251 };
252
253 struct LibpthreadOffsets
254 {
255 uint16_t plo_version;
256 uint16_t plo_pthread_tsd_base_offset;
257 uint16_t plo_pthread_tsd_base_address_offset;
258 uint16_t plo_pthread_tsd_entry_size;
259
260 LibpthreadOffsets () :
261 plo_version (UINT16_MAX),
262 plo_pthread_tsd_base_offset (UINT16_MAX),
263 plo_pthread_tsd_base_address_offset (UINT16_MAX),
264 plo_pthread_tsd_entry_size (UINT16_MAX)
265 {
266 }
267
268 bool IsValid ()
269 {
270 return plo_version != UINT16_MAX;
271 }
272 };
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000273
274 // The libBacktraceRecording function __introspection_dispatch_queue_get_pending_items has
275 // two forms. It can either return a simple array of item_refs (void *) size or it can return
276 // a header with uint32_t version, a uint32_t size of item, and then an array of item_refs (void*)
277 // and code addresses (void*) for all the pending blocks.
278
279 struct ItemRefAndCodeAddress {
280 lldb::addr_t item_ref;
281 lldb::addr_t code_address;
282 };
283
284 struct PendingItemsForQueue {
285 bool new_style; // new-style means both item_refs and code_addresses avail
286 // old-style means only item_refs is filled in
287 std::vector<ItemRefAndCodeAddress> item_refs_and_code_addresses;
288 };
289
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000290 bool
Jason Molenda2fd83352014-02-05 05:44:54 +0000291 BacktraceRecordingHeadersInitialized ();
Jason Molenda8ee9cb52013-11-16 01:24:22 +0000292
293 void
Jason Molenda2fd83352014-02-05 05:44:54 +0000294 ReadLibdispatchOffsetsAddress();
Jason Molenda8ee9cb52013-11-16 01:24:22 +0000295
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000296 void
Jason Molenda2fd83352014-02-05 05:44:54 +0000297 ReadLibdispatchOffsets ();
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000298
Jason Molenda705b1802014-06-13 02:37:02 +0000299 void
300 ReadLibpthreadOffsetsAddress();
301
302 void
303 ReadLibpthreadOffsets ();
304
305 void
306 ReadLibdispatchTSDIndexesAddress ();
307
308 void
309 ReadLibdispatchTSDIndexes ();
310
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000311 PendingItemsForQueue
Jason Molenda2fd83352014-02-05 05:44:54 +0000312 GetPendingItemRefsForQueue (lldb::addr_t queue);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000313
Jason Molenda2fd83352014-02-05 05:44:54 +0000314 ItemInfo
315 ExtractItemInfoFromBuffer (lldb_private::DataExtractor &extractor);
Jason Molendaa6e91302013-11-19 05:44:41 +0000316
Jason Molenda2fd83352014-02-05 05:44:54 +0000317 lldb_private::AppleGetQueuesHandler m_get_queues_handler;
318 lldb_private::AppleGetPendingItemsHandler m_get_pending_items_handler;
319 lldb_private::AppleGetItemInfoHandler m_get_item_info_handler;
320 lldb_private::AppleGetThreadItemInfoHandler m_get_thread_item_info_handler;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000321
Jason Molenda2fd83352014-02-05 05:44:54 +0000322 lldb::addr_t m_page_to_free;
323 uint64_t m_page_to_free_size;
324 libBacktraceRecording_info m_lib_backtrace_recording_info;
Jason Molenda705b1802014-06-13 02:37:02 +0000325
Jason Molenda2fd83352014-02-05 05:44:54 +0000326 lldb::addr_t m_dispatch_queue_offsets_addr;
327 struct LibdispatchOffsets m_libdispatch_offsets;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000328
Jason Molenda705b1802014-06-13 02:37:02 +0000329 lldb::addr_t m_libpthread_layout_offsets_addr;
330 struct LibpthreadOffsets m_libpthread_offsets;
331
332 lldb::addr_t m_dispatch_tsd_indexes_addr;
333 struct LibdispatchTSDIndexes m_libdispatch_tsd_indexes;
334
335 lldb::addr_t m_dispatch_voucher_offsets_addr;
336 struct LibdispatchVoucherOffsets m_libdispatch_voucher_offsets;
337
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000338 DISALLOW_COPY_AND_ASSIGN (SystemRuntimeMacOSX);
339};
340
341#endif // liblldb_SystemRuntimeMacOSX_h_