blob: 5085a079d58d6d73d679736f756e4dcd2368dca8 [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
Jason Molendaa7b5afa2013-11-15 00:17:32 +000020#include "lldb/Target/SystemRuntime.h"
21#include "lldb/Host/FileSpec.h"
22#include "lldb/Core/ConstString.h"
23#include "lldb/Core/ModuleList.h"
24#include "lldb/Core/UUID.h"
25#include "lldb/Host/Mutex.h"
26#include "lldb/Target/Process.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000027#include "lldb/Target/QueueItem.h"
28
29#include "AppleGetItemInfoHandler.h"
30#include "AppleGetQueuesHandler.h"
31#include "AppleGetPendingItemsHandler.h"
32#include "AppleGetThreadItemInfoHandler.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000033
34class SystemRuntimeMacOSX : public lldb_private::SystemRuntime
35{
36public:
37 //------------------------------------------------------------------
38 // Static Functions
39 //------------------------------------------------------------------
40 static void
41 Initialize();
42
43 static void
44 Terminate();
45
46 static lldb_private::ConstString
47 GetPluginNameStatic();
48
49 static const char *
50 GetPluginDescriptionStatic();
51
52 static lldb_private::SystemRuntime *
53 CreateInstance (lldb_private::Process *process);
54
Jason Molenda2fd83352014-02-05 05:44:54 +000055
56 //------------------------------------------------------------------
57 // instance methods
58 //------------------------------------------------------------------
59
Jason Molendaa7b5afa2013-11-15 00:17:32 +000060 SystemRuntimeMacOSX (lldb_private::Process *process);
61
62 virtual
63 ~SystemRuntimeMacOSX ();
64
65 void
66 Clear (bool clear_process);
67
68 void
Jason Molenda2fd83352014-02-05 05:44:54 +000069 Detach ();
Jason Molendaa7b5afa2013-11-15 00:17:32 +000070
71 const std::vector<lldb_private::ConstString> &
72 GetExtendedBacktraceTypes ();
73
74 lldb::ThreadSP
75 GetExtendedBacktraceThread (lldb::ThreadSP thread, lldb_private::ConstString type);
76
Jason Molenda2fd83352014-02-05 05:44:54 +000077 lldb::ThreadSP
78 GetExtendedBacktraceForQueueItem (lldb::QueueItemSP queue_item_sp, lldb_private::ConstString type);
Jason Molendaa7b5afa2013-11-15 00:17:32 +000079
Jason Molenda2fd83352014-02-05 05:44:54 +000080 lldb::ThreadSP
81 GetExtendedBacktraceFromItemRef (lldb::addr_t item_ref);
Jason Molendaa7b5afa2013-11-15 00:17:32 +000082
Jason Molenda2fd83352014-02-05 05:44:54 +000083 void
84 PopulateQueueList (lldb_private::QueueList &queue_list);
Jason Molendaa7b5afa2013-11-15 00:17:32 +000085
Jason Molenda2fd83352014-02-05 05:44:54 +000086 void
87 PopulateQueuesUsingLibBTR (lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count, lldb_private::QueueList &queue_list);
88
89 void
90 PopulatePendingQueuesUsingLibBTR (lldb::addr_t items_buffer, uint64_t items_buffer_size, uint64_t count, lldb_private::Queue *queue);
91
92 std::string
93 GetQueueNameFromThreadQAddress (lldb::addr_t dispatch_qaddr);
94
95 lldb::queue_id_t
96 GetQueueIDFromThreadQAddress (lldb::addr_t dispatch_qaddr);
97
Jason Molendaaac16e02014-03-13 02:54:54 +000098 lldb::addr_t
99 GetLibdispatchQueueAddressFromThreadQAddress (lldb::addr_t dispatch_qaddr);
100
Jason Molenda2fd83352014-02-05 05:44:54 +0000101 void
102 PopulatePendingItemsForQueue (lldb_private::Queue *queue);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000103
Jason Molendae32cd192014-03-10 08:42:03 +0000104 void
105 CompleteQueueItem (lldb_private::QueueItem *queue_item, lldb::addr_t item_ref);
106
Jason Molendaaac16e02014-03-13 02:54:54 +0000107 virtual lldb::QueueKind
108 GetQueueKind (lldb::addr_t dispatch_queue_addr);
109
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000110 //------------------------------------------------------------------
111 // PluginInterface protocol
112 //------------------------------------------------------------------
113 virtual lldb_private::ConstString
114 GetPluginName();
115
116 virtual uint32_t
117 GetPluginVersion();
118
Jason Molenda5e8dce42013-12-13 00:29:16 +0000119
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000120protected:
121 lldb::user_id_t m_break_id;
122 mutable lldb_private::Mutex m_mutex;
123
124private:
125
Jason Molenda2fd83352014-02-05 05:44:54 +0000126 struct libBacktraceRecording_info {
127 uint16_t queue_info_version;
128 uint16_t queue_info_data_offset;
129 uint16_t item_info_version;
130 uint16_t item_info_data_offset;
131
132 libBacktraceRecording_info () :
133 queue_info_version(0),
134 queue_info_data_offset(0),
135 item_info_version(0),
136 item_info_data_offset(0) {}
137 };
138
139
140 // A structure which reflects the data recorded in the
141 // libBacktraceRecording introspection_dispatch_item_info_s.
142 struct ItemInfo {
143 lldb::addr_t item_that_enqueued_this;
144 lldb::addr_t function_or_block;
145 uint64_t enqueuing_thread_id;
146 uint64_t enqueuing_queue_serialnum;
147 uint64_t target_queue_serialnum;
148 uint32_t enqueuing_callstack_frame_count;
149 uint32_t stop_id;
150 std::vector<lldb::addr_t> enqueuing_callstack;
151 std::string enqueuing_thread_label;
152 std::string enqueuing_queue_label;
153 std::string target_queue_label;
154 };
155
156 // The offsets of different fields of the dispatch_queue_t structure in
157 // a thread/queue process.
158 // Based on libdispatch src/queue_private.h, struct dispatch_queue_offsets_s
159 // With dqo_version 1-3, the dqo_label field is a per-queue value and cannot be cached.
160 // With dqo_version 4 (Mac OS X 10.9 / iOS 7), dqo_label is a constant value that can be cached.
161 struct LibdispatchOffsets
162 {
163 uint16_t dqo_version;
164 uint16_t dqo_label;
165 uint16_t dqo_label_size;
166 uint16_t dqo_flags;
167 uint16_t dqo_flags_size;
168 uint16_t dqo_serialnum;
169 uint16_t dqo_serialnum_size;
170 uint16_t dqo_width;
171 uint16_t dqo_width_size;
172 uint16_t dqo_running;
173 uint16_t dqo_running_size;
174
175 LibdispatchOffsets ()
176 {
177 dqo_version = UINT16_MAX;
178 dqo_flags = UINT16_MAX;
179 dqo_serialnum = UINT16_MAX;
180 dqo_label = UINT16_MAX;
181 dqo_width = UINT16_MAX;
182 dqo_running = UINT16_MAX;
183 };
184
185 bool
186 IsValid ()
187 {
188 return dqo_version != UINT16_MAX;
189 }
190
191 bool
192 LabelIsValid ()
193 {
194 return dqo_label != UINT16_MAX;
195 }
196 };
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000197
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000198
199 // The libBacktraceRecording function __introspection_dispatch_queue_get_pending_items has
200 // two forms. It can either return a simple array of item_refs (void *) size or it can return
201 // a header with uint32_t version, a uint32_t size of item, and then an array of item_refs (void*)
202 // and code addresses (void*) for all the pending blocks.
203
204 struct ItemRefAndCodeAddress {
205 lldb::addr_t item_ref;
206 lldb::addr_t code_address;
207 };
208
209 struct PendingItemsForQueue {
210 bool new_style; // new-style means both item_refs and code_addresses avail
211 // old-style means only item_refs is filled in
212 std::vector<ItemRefAndCodeAddress> item_refs_and_code_addresses;
213 };
214
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000215 bool
Jason Molenda2fd83352014-02-05 05:44:54 +0000216 BacktraceRecordingHeadersInitialized ();
Jason Molenda8ee9cb52013-11-16 01:24:22 +0000217
218 void
Jason Molenda2fd83352014-02-05 05:44:54 +0000219 ReadLibdispatchOffsetsAddress();
Jason Molenda8ee9cb52013-11-16 01:24:22 +0000220
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000221 void
Jason Molenda2fd83352014-02-05 05:44:54 +0000222 ReadLibdispatchOffsets ();
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000223
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000224 PendingItemsForQueue
Jason Molenda2fd83352014-02-05 05:44:54 +0000225 GetPendingItemRefsForQueue (lldb::addr_t queue);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000226
Jason Molenda2fd83352014-02-05 05:44:54 +0000227 ItemInfo
228 ExtractItemInfoFromBuffer (lldb_private::DataExtractor &extractor);
Jason Molendaa6e91302013-11-19 05:44:41 +0000229
Jason Molenda2fd83352014-02-05 05:44:54 +0000230 lldb_private::AppleGetQueuesHandler m_get_queues_handler;
231 lldb_private::AppleGetPendingItemsHandler m_get_pending_items_handler;
232 lldb_private::AppleGetItemInfoHandler m_get_item_info_handler;
233 lldb_private::AppleGetThreadItemInfoHandler m_get_thread_item_info_handler;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000234
Jason Molenda2fd83352014-02-05 05:44:54 +0000235 lldb::addr_t m_page_to_free;
236 uint64_t m_page_to_free_size;
237 libBacktraceRecording_info m_lib_backtrace_recording_info;
238 lldb::addr_t m_dispatch_queue_offsets_addr;
239 struct LibdispatchOffsets m_libdispatch_offsets;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000240
241 DISALLOW_COPY_AND_ASSIGN (SystemRuntimeMacOSX);
242};
243
244#endif // liblldb_SystemRuntimeMacOSX_h_