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