blob: 75e9b9e8f0386eb7beca6ad313ff75e0d097139d [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
20#include "llvm/Support/MachO.h"
21
22#include "lldb/Target/SystemRuntime.h"
23#include "lldb/Host/FileSpec.h"
24#include "lldb/Core/ConstString.h"
25#include "lldb/Core/ModuleList.h"
26#include "lldb/Core/UUID.h"
27#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 Molendaa7b5afa2013-11-15 00:17:32 +0000112 //------------------------------------------------------------------
113 // PluginInterface protocol
114 //------------------------------------------------------------------
115 virtual lldb_private::ConstString
116 GetPluginName();
117
118 virtual uint32_t
119 GetPluginVersion();
120
Jason Molenda5e8dce42013-12-13 00:29:16 +0000121
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000122protected:
123 lldb::user_id_t m_break_id;
124 mutable lldb_private::Mutex m_mutex;
125
126private:
127
Jason Molenda2fd83352014-02-05 05:44:54 +0000128 struct libBacktraceRecording_info {
129 uint16_t queue_info_version;
130 uint16_t queue_info_data_offset;
131 uint16_t item_info_version;
132 uint16_t item_info_data_offset;
133
134 libBacktraceRecording_info () :
135 queue_info_version(0),
136 queue_info_data_offset(0),
137 item_info_version(0),
138 item_info_data_offset(0) {}
139 };
140
141
142 // A structure which reflects the data recorded in the
143 // libBacktraceRecording introspection_dispatch_item_info_s.
144 struct ItemInfo {
145 lldb::addr_t item_that_enqueued_this;
146 lldb::addr_t function_or_block;
147 uint64_t enqueuing_thread_id;
148 uint64_t enqueuing_queue_serialnum;
149 uint64_t target_queue_serialnum;
150 uint32_t enqueuing_callstack_frame_count;
151 uint32_t stop_id;
152 std::vector<lldb::addr_t> enqueuing_callstack;
153 std::string enqueuing_thread_label;
154 std::string enqueuing_queue_label;
155 std::string target_queue_label;
156 };
157
158 // The offsets of different fields of the dispatch_queue_t structure in
159 // a thread/queue process.
160 // Based on libdispatch src/queue_private.h, struct dispatch_queue_offsets_s
161 // With dqo_version 1-3, the dqo_label field is a per-queue value and cannot be cached.
162 // With dqo_version 4 (Mac OS X 10.9 / iOS 7), dqo_label is a constant value that can be cached.
163 struct LibdispatchOffsets
164 {
165 uint16_t dqo_version;
166 uint16_t dqo_label;
167 uint16_t dqo_label_size;
168 uint16_t dqo_flags;
169 uint16_t dqo_flags_size;
170 uint16_t dqo_serialnum;
171 uint16_t dqo_serialnum_size;
172 uint16_t dqo_width;
173 uint16_t dqo_width_size;
174 uint16_t dqo_running;
175 uint16_t dqo_running_size;
176
177 LibdispatchOffsets ()
178 {
179 dqo_version = UINT16_MAX;
180 dqo_flags = UINT16_MAX;
181 dqo_serialnum = UINT16_MAX;
182 dqo_label = UINT16_MAX;
183 dqo_width = UINT16_MAX;
184 dqo_running = UINT16_MAX;
185 };
186
187 bool
188 IsValid ()
189 {
190 return dqo_version != UINT16_MAX;
191 }
192
193 bool
194 LabelIsValid ()
195 {
196 return dqo_label != UINT16_MAX;
197 }
198 };
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000199
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000200
201 // The libBacktraceRecording function __introspection_dispatch_queue_get_pending_items has
202 // two forms. It can either return a simple array of item_refs (void *) size or it can return
203 // a header with uint32_t version, a uint32_t size of item, and then an array of item_refs (void*)
204 // and code addresses (void*) for all the pending blocks.
205
206 struct ItemRefAndCodeAddress {
207 lldb::addr_t item_ref;
208 lldb::addr_t code_address;
209 };
210
211 struct PendingItemsForQueue {
212 bool new_style; // new-style means both item_refs and code_addresses avail
213 // old-style means only item_refs is filled in
214 std::vector<ItemRefAndCodeAddress> item_refs_and_code_addresses;
215 };
216
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000217 bool
Jason Molenda2fd83352014-02-05 05:44:54 +0000218 BacktraceRecordingHeadersInitialized ();
Jason Molenda8ee9cb52013-11-16 01:24:22 +0000219
220 void
Jason Molenda2fd83352014-02-05 05:44:54 +0000221 ReadLibdispatchOffsetsAddress();
Jason Molenda8ee9cb52013-11-16 01:24:22 +0000222
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000223 void
Jason Molenda2fd83352014-02-05 05:44:54 +0000224 ReadLibdispatchOffsets ();
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000225
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000226 PendingItemsForQueue
Jason Molenda2fd83352014-02-05 05:44:54 +0000227 GetPendingItemRefsForQueue (lldb::addr_t queue);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000228
Jason Molenda2fd83352014-02-05 05:44:54 +0000229 ItemInfo
230 ExtractItemInfoFromBuffer (lldb_private::DataExtractor &extractor);
Jason Molendaa6e91302013-11-19 05:44:41 +0000231
Jason Molenda2fd83352014-02-05 05:44:54 +0000232 lldb_private::AppleGetQueuesHandler m_get_queues_handler;
233 lldb_private::AppleGetPendingItemsHandler m_get_pending_items_handler;
234 lldb_private::AppleGetItemInfoHandler m_get_item_info_handler;
235 lldb_private::AppleGetThreadItemInfoHandler m_get_thread_item_info_handler;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000236
Jason Molenda2fd83352014-02-05 05:44:54 +0000237 lldb::addr_t m_page_to_free;
238 uint64_t m_page_to_free_size;
239 libBacktraceRecording_info m_lib_backtrace_recording_info;
240 lldb::addr_t m_dispatch_queue_offsets_addr;
241 struct LibdispatchOffsets m_libdispatch_offsets;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000242
243 DISALLOW_COPY_AND_ASSIGN (SystemRuntimeMacOSX);
244};
245
246#endif // liblldb_SystemRuntimeMacOSX_h_