blob: 30956fa3ece71af0980e539ed67cc41ebf93ce94 [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"
22#include "lldb/Host/FileSpec.h"
23#include "lldb/Core/ConstString.h"
24#include "lldb/Core/ModuleList.h"
25#include "lldb/Core/UUID.h"
26#include "lldb/Host/Mutex.h"
27#include "lldb/Target/Process.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000028#include "lldb/Target/QueueItem.h"
29
30#include "AppleGetItemInfoHandler.h"
31#include "AppleGetQueuesHandler.h"
32#include "AppleGetPendingItemsHandler.h"
33#include "AppleGetThreadItemInfoHandler.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000034
35class SystemRuntimeMacOSX : public lldb_private::SystemRuntime
36{
37public:
38 //------------------------------------------------------------------
39 // Static Functions
40 //------------------------------------------------------------------
41 static void
42 Initialize();
43
44 static void
45 Terminate();
46
47 static lldb_private::ConstString
48 GetPluginNameStatic();
49
50 static const char *
51 GetPluginDescriptionStatic();
52
53 static lldb_private::SystemRuntime *
54 CreateInstance (lldb_private::Process *process);
55
Jason Molenda2fd83352014-02-05 05:44:54 +000056
57 //------------------------------------------------------------------
58 // instance methods
59 //------------------------------------------------------------------
60
Jason Molendaa7b5afa2013-11-15 00:17:32 +000061 SystemRuntimeMacOSX (lldb_private::Process *process);
62
63 virtual
64 ~SystemRuntimeMacOSX ();
65
66 void
67 Clear (bool clear_process);
68
69 void
Jason Molenda2fd83352014-02-05 05:44:54 +000070 Detach ();
Jason Molendaa7b5afa2013-11-15 00:17:32 +000071
72 const std::vector<lldb_private::ConstString> &
73 GetExtendedBacktraceTypes ();
74
75 lldb::ThreadSP
76 GetExtendedBacktraceThread (lldb::ThreadSP thread, lldb_private::ConstString type);
77
Jason Molenda2fd83352014-02-05 05:44:54 +000078 lldb::ThreadSP
79 GetExtendedBacktraceForQueueItem (lldb::QueueItemSP queue_item_sp, lldb_private::ConstString type);
Jason Molendaa7b5afa2013-11-15 00:17:32 +000080
Jason Molenda2fd83352014-02-05 05:44:54 +000081 lldb::ThreadSP
82 GetExtendedBacktraceFromItemRef (lldb::addr_t item_ref);
Jason Molendaa7b5afa2013-11-15 00:17:32 +000083
Jason Molenda2fd83352014-02-05 05:44:54 +000084 void
85 PopulateQueueList (lldb_private::QueueList &queue_list);
Jason Molendaa7b5afa2013-11-15 00:17:32 +000086
Jason Molenda2fd83352014-02-05 05:44:54 +000087 void
88 PopulateQueuesUsingLibBTR (lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count, lldb_private::QueueList &queue_list);
89
90 void
91 PopulatePendingQueuesUsingLibBTR (lldb::addr_t items_buffer, uint64_t items_buffer_size, uint64_t count, lldb_private::Queue *queue);
92
93 std::string
94 GetQueueNameFromThreadQAddress (lldb::addr_t dispatch_qaddr);
95
96 lldb::queue_id_t
97 GetQueueIDFromThreadQAddress (lldb::addr_t dispatch_qaddr);
98
Jason Molendaaac16e02014-03-13 02:54:54 +000099 lldb::addr_t
100 GetLibdispatchQueueAddressFromThreadQAddress (lldb::addr_t dispatch_qaddr);
101
Jason Molenda2fd83352014-02-05 05:44:54 +0000102 void
103 PopulatePendingItemsForQueue (lldb_private::Queue *queue);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000104
Jason Molendae32cd192014-03-10 08:42:03 +0000105 void
106 CompleteQueueItem (lldb_private::QueueItem *queue_item, lldb::addr_t item_ref);
107
Jason Molendaaac16e02014-03-13 02:54:54 +0000108 virtual lldb::QueueKind
109 GetQueueKind (lldb::addr_t dispatch_queue_addr);
110
Jason Molendab4892cd2014-05-13 22:02:48 +0000111 virtual bool
112 SafeToCallFunctionsOnThisThread (lldb::ThreadSP thread_sp);
113
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000114 //------------------------------------------------------------------
115 // PluginInterface protocol
116 //------------------------------------------------------------------
117 virtual lldb_private::ConstString
118 GetPluginName();
119
120 virtual uint32_t
121 GetPluginVersion();
122
Jason Molenda5e8dce42013-12-13 00:29:16 +0000123
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000124protected:
125 lldb::user_id_t m_break_id;
126 mutable lldb_private::Mutex m_mutex;
127
128private:
129
Jason Molenda2fd83352014-02-05 05:44:54 +0000130 struct libBacktraceRecording_info {
131 uint16_t queue_info_version;
132 uint16_t queue_info_data_offset;
133 uint16_t item_info_version;
134 uint16_t item_info_data_offset;
135
136 libBacktraceRecording_info () :
137 queue_info_version(0),
138 queue_info_data_offset(0),
139 item_info_version(0),
140 item_info_data_offset(0) {}
141 };
142
143
144 // A structure which reflects the data recorded in the
145 // libBacktraceRecording introspection_dispatch_item_info_s.
146 struct ItemInfo {
147 lldb::addr_t item_that_enqueued_this;
148 lldb::addr_t function_or_block;
149 uint64_t enqueuing_thread_id;
150 uint64_t enqueuing_queue_serialnum;
151 uint64_t target_queue_serialnum;
152 uint32_t enqueuing_callstack_frame_count;
153 uint32_t stop_id;
154 std::vector<lldb::addr_t> enqueuing_callstack;
155 std::string enqueuing_thread_label;
156 std::string enqueuing_queue_label;
157 std::string target_queue_label;
158 };
159
160 // The offsets of different fields of the dispatch_queue_t structure in
161 // a thread/queue process.
162 // Based on libdispatch src/queue_private.h, struct dispatch_queue_offsets_s
163 // With dqo_version 1-3, the dqo_label field is a per-queue value and cannot be cached.
164 // With dqo_version 4 (Mac OS X 10.9 / iOS 7), dqo_label is a constant value that can be cached.
165 struct LibdispatchOffsets
166 {
167 uint16_t dqo_version;
168 uint16_t dqo_label;
169 uint16_t dqo_label_size;
170 uint16_t dqo_flags;
171 uint16_t dqo_flags_size;
172 uint16_t dqo_serialnum;
173 uint16_t dqo_serialnum_size;
174 uint16_t dqo_width;
175 uint16_t dqo_width_size;
176 uint16_t dqo_running;
177 uint16_t dqo_running_size;
178
179 LibdispatchOffsets ()
180 {
181 dqo_version = UINT16_MAX;
182 dqo_flags = UINT16_MAX;
183 dqo_serialnum = UINT16_MAX;
184 dqo_label = UINT16_MAX;
185 dqo_width = UINT16_MAX;
186 dqo_running = UINT16_MAX;
187 };
188
189 bool
190 IsValid ()
191 {
192 return dqo_version != UINT16_MAX;
193 }
194
195 bool
196 LabelIsValid ()
197 {
198 return dqo_label != UINT16_MAX;
199 }
200 };
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000201
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000202
203 // The libBacktraceRecording function __introspection_dispatch_queue_get_pending_items has
204 // two forms. It can either return a simple array of item_refs (void *) size or it can return
205 // a header with uint32_t version, a uint32_t size of item, and then an array of item_refs (void*)
206 // and code addresses (void*) for all the pending blocks.
207
208 struct ItemRefAndCodeAddress {
209 lldb::addr_t item_ref;
210 lldb::addr_t code_address;
211 };
212
213 struct PendingItemsForQueue {
214 bool new_style; // new-style means both item_refs and code_addresses avail
215 // old-style means only item_refs is filled in
216 std::vector<ItemRefAndCodeAddress> item_refs_and_code_addresses;
217 };
218
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000219 bool
Jason Molenda2fd83352014-02-05 05:44:54 +0000220 BacktraceRecordingHeadersInitialized ();
Jason Molenda8ee9cb52013-11-16 01:24:22 +0000221
222 void
Jason Molenda2fd83352014-02-05 05:44:54 +0000223 ReadLibdispatchOffsetsAddress();
Jason Molenda8ee9cb52013-11-16 01:24:22 +0000224
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000225 void
Jason Molenda2fd83352014-02-05 05:44:54 +0000226 ReadLibdispatchOffsets ();
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000227
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000228 PendingItemsForQueue
Jason Molenda2fd83352014-02-05 05:44:54 +0000229 GetPendingItemRefsForQueue (lldb::addr_t queue);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000230
Jason Molenda2fd83352014-02-05 05:44:54 +0000231 ItemInfo
232 ExtractItemInfoFromBuffer (lldb_private::DataExtractor &extractor);
Jason Molendaa6e91302013-11-19 05:44:41 +0000233
Jason Molenda2fd83352014-02-05 05:44:54 +0000234 lldb_private::AppleGetQueuesHandler m_get_queues_handler;
235 lldb_private::AppleGetPendingItemsHandler m_get_pending_items_handler;
236 lldb_private::AppleGetItemInfoHandler m_get_item_info_handler;
237 lldb_private::AppleGetThreadItemInfoHandler m_get_thread_item_info_handler;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000238
Jason Molenda2fd83352014-02-05 05:44:54 +0000239 lldb::addr_t m_page_to_free;
240 uint64_t m_page_to_free_size;
241 libBacktraceRecording_info m_lib_backtrace_recording_info;
242 lldb::addr_t m_dispatch_queue_offsets_addr;
243 struct LibdispatchOffsets m_libdispatch_offsets;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000244
245 DISALLOW_COPY_AND_ASSIGN (SystemRuntimeMacOSX);
246};
247
248#endif // liblldb_SystemRuntimeMacOSX_h_