blob: e61f04e489e8466ce5271e94f0176f7392ffa5d3 [file] [log] [blame]
Jason Molendaa7b5afa2013-11-15 00:17:32 +00001//===-- SystemRuntimeMacOSX.cpp ---------------------------------*- 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
Kate Stoneb9c1b512016-09-06 20:57:50 +000010#include "Plugins/Process/Utility/HistoryThread.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000011#include "lldb/Breakpoint/StoppointCallbackContext.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000012#include "lldb/Core/Module.h"
13#include "lldb/Core/ModuleSpec.h"
14#include "lldb/Core/PluginManager.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000015#include "lldb/Core/Section.h"
Zachary Turner88c6b622015-03-03 18:34:26 +000016#include "lldb/Symbol/ClangASTContext.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000017#include "lldb/Symbol/ObjectFile.h"
18#include "lldb/Symbol/SymbolContext.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000019#include "lldb/Target/Process.h"
Zachary Turner01c32432017-02-14 19:06:07 +000020#include "lldb/Target/ProcessStructReader.h"
Jason Molenda5e8dce42013-12-13 00:29:16 +000021#include "lldb/Target/Queue.h"
22#include "lldb/Target/QueueList.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000023#include "lldb/Target/Target.h"
24#include "lldb/Target/Thread.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000025#include "lldb/Utility/DataBufferHeap.h"
26#include "lldb/Utility/DataExtractor.h"
Zachary Turner5713a052017-03-22 18:40:07 +000027#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000028#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000029#include "lldb/Utility/StreamString.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000030
Jason Molendaa7b5afa2013-11-15 00:17:32 +000031#include "SystemRuntimeMacOSX.h"
32
33using namespace lldb;
34using namespace lldb_private;
35
36//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +000037// Create an instance of this class. This function is filled into the plugin
38// info class that gets handed out by the plugin factory and allows the lldb to
39// instantiate an instance of this class.
Jason Molendaa7b5afa2013-11-15 00:17:32 +000040//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000041SystemRuntime *SystemRuntimeMacOSX::CreateInstance(Process *process) {
42 bool create = false;
43 if (!create) {
44 create = true;
45 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
46 if (exe_module) {
47 ObjectFile *object_file = exe_module->GetObjectFile();
48 if (object_file) {
49 create = (object_file->GetStrata() == ObjectFile::eStrataUser);
50 }
Jason Molendaa7b5afa2013-11-15 00:17:32 +000051 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000052
53 if (create) {
54 const llvm::Triple &triple_ref =
55 process->GetTarget().GetArchitecture().GetTriple();
56 switch (triple_ref.getOS()) {
57 case llvm::Triple::Darwin:
58 case llvm::Triple::MacOSX:
59 case llvm::Triple::IOS:
60 case llvm::Triple::TvOS:
61 case llvm::Triple::WatchOS:
Jason Molenda32762fd2018-10-11 00:28:35 +000062 // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS:
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 create = triple_ref.getVendor() == llvm::Triple::Apple;
64 break;
65 default:
66 create = false;
67 break;
68 }
69 }
70 }
71
72 if (create)
73 return new SystemRuntimeMacOSX(process);
74 return NULL;
Jason Molendaa7b5afa2013-11-15 00:17:32 +000075}
76
77//----------------------------------------------------------------------
78// Constructor
79//----------------------------------------------------------------------
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000080SystemRuntimeMacOSX::SystemRuntimeMacOSX(Process *process)
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 : SystemRuntime(process), m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
82 m_get_queues_handler(process), m_get_pending_items_handler(process),
83 m_get_item_info_handler(process), m_get_thread_item_info_handler(process),
84 m_page_to_free(LLDB_INVALID_ADDRESS), m_page_to_free_size(0),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000085 m_lib_backtrace_recording_info(),
86 m_dispatch_queue_offsets_addr(LLDB_INVALID_ADDRESS),
87 m_libdispatch_offsets(),
88 m_libpthread_layout_offsets_addr(LLDB_INVALID_ADDRESS),
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 m_libpthread_offsets(), m_dispatch_tsd_indexes_addr(LLDB_INVALID_ADDRESS),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000090 m_libdispatch_tsd_indexes(),
91 m_dispatch_voucher_offsets_addr(LLDB_INVALID_ADDRESS),
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 m_libdispatch_voucher_offsets() {}
Jason Molendaa7b5afa2013-11-15 00:17:32 +000093
94//----------------------------------------------------------------------
95// Destructor
96//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000097SystemRuntimeMacOSX::~SystemRuntimeMacOSX() { Clear(true); }
Jason Molendaa7b5afa2013-11-15 00:17:32 +000098
Kate Stoneb9c1b512016-09-06 20:57:50 +000099void SystemRuntimeMacOSX::Detach() {
100 m_get_queues_handler.Detach();
101 m_get_pending_items_handler.Detach();
102 m_get_item_info_handler.Detach();
103 m_get_thread_item_info_handler.Detach();
Jason Molenda2fd83352014-02-05 05:44:54 +0000104}
105
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000106//----------------------------------------------------------------------
107// Clear out the state of this class.
108//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109void SystemRuntimeMacOSX::Clear(bool clear_process) {
110 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
113 m_process->ClearBreakpointSiteByID(m_break_id);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 if (clear_process)
116 m_process = NULL;
117 m_break_id = LLDB_INVALID_BREAK_ID;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000118}
119
Jason Molenda2fd83352014-02-05 05:44:54 +0000120std::string
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121SystemRuntimeMacOSX::GetQueueNameFromThreadQAddress(addr_t dispatch_qaddr) {
122 std::string dispatch_queue_name;
123 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
124 return "";
Jason Molenda2fd83352014-02-05 05:44:54 +0000125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 ReadLibdispatchOffsets();
127 if (m_libdispatch_offsets.IsValid()) {
128 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a
Adrian Prantl05097242018-04-30 16:49:04 +0000129 // thread - deref it to get the address of the dispatch_queue_t structure
130 // for this thread's queue.
Zachary Turner97206d52017-05-12 04:51:55 +0000131 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 addr_t dispatch_queue_addr =
133 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
134 if (error.Success()) {
135 if (m_libdispatch_offsets.dqo_version >= 4) {
Adrian Prantl05097242018-04-30 16:49:04 +0000136 // libdispatch versions 4+, pointer to dispatch name is in the queue
137 // structure.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 addr_t pointer_to_label_address =
139 dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
140 addr_t label_addr =
141 m_process->ReadPointerFromMemory(pointer_to_label_address, error);
142 if (error.Success()) {
143 m_process->ReadCStringFromMemory(label_addr, dispatch_queue_name,
144 error);
145 }
146 } else {
147 // libdispatch versions 1-3, dispatch name is a fixed width char array
148 // in the queue structure.
149 addr_t label_addr =
150 dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
151 dispatch_queue_name.resize(m_libdispatch_offsets.dqo_label_size, '\0');
152 size_t bytes_read =
153 m_process->ReadMemory(label_addr, &dispatch_queue_name[0],
154 m_libdispatch_offsets.dqo_label_size, error);
155 if (bytes_read < m_libdispatch_offsets.dqo_label_size)
156 dispatch_queue_name.erase(bytes_read);
157 }
Jason Molendaaac16e02014-03-13 02:54:54 +0000158 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 }
160 return dispatch_queue_name;
Jason Molendaaac16e02014-03-13 02:54:54 +0000161}
162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163lldb::addr_t SystemRuntimeMacOSX::GetLibdispatchQueueAddressFromThreadQAddress(
164 addr_t dispatch_qaddr) {
165 addr_t libdispatch_queue_t_address = LLDB_INVALID_ADDRESS;
Zachary Turner97206d52017-05-12 04:51:55 +0000166 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 libdispatch_queue_t_address =
168 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
169 if (!error.Success()) {
170 libdispatch_queue_t_address = LLDB_INVALID_ADDRESS;
171 }
172 return libdispatch_queue_t_address;
Jason Molendaaac16e02014-03-13 02:54:54 +0000173}
174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175lldb::QueueKind SystemRuntimeMacOSX::GetQueueKind(addr_t dispatch_queue_addr) {
176 if (dispatch_queue_addr == LLDB_INVALID_ADDRESS || dispatch_queue_addr == 0)
177 return eQueueKindUnknown;
178
179 QueueKind kind = eQueueKindUnknown;
180 ReadLibdispatchOffsets();
181 if (m_libdispatch_offsets.IsValid() &&
182 m_libdispatch_offsets.dqo_version >= 4) {
Zachary Turner97206d52017-05-12 04:51:55 +0000183 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 uint64_t width = m_process->ReadUnsignedIntegerFromMemory(
185 dispatch_queue_addr + m_libdispatch_offsets.dqo_width,
186 m_libdispatch_offsets.dqo_width_size, 0, error);
187 if (error.Success()) {
188 if (width == 1) {
189 kind = eQueueKindSerial;
190 }
191 if (width > 1) {
192 kind = eQueueKindConcurrent;
193 }
Jason Molenda705b1802014-06-13 02:37:02 +0000194 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 }
196 return kind;
Jason Molenda705b1802014-06-13 02:37:02 +0000197}
198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199void SystemRuntimeMacOSX::AddThreadExtendedInfoPacketHints(
200 lldb_private::StructuredData::ObjectSP dict_sp) {
201 StructuredData::Dictionary *dict = dict_sp->GetAsDictionary();
202 if (dict) {
203 ReadLibpthreadOffsets();
204 if (m_libpthread_offsets.IsValid()) {
205 dict->AddIntegerItem("plo_pthread_tsd_base_offset",
206 m_libpthread_offsets.plo_pthread_tsd_base_offset);
207 dict->AddIntegerItem(
208 "plo_pthread_tsd_base_address_offset",
209 m_libpthread_offsets.plo_pthread_tsd_base_address_offset);
210 dict->AddIntegerItem("plo_pthread_tsd_entry_size",
211 m_libpthread_offsets.plo_pthread_tsd_entry_size);
Jason Molendab4892cd2014-05-13 22:02:48 +0000212 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213
214 ReadLibdispatchTSDIndexes();
215 if (m_libdispatch_tsd_indexes.IsValid()) {
216 dict->AddIntegerItem("dti_queue_index",
217 m_libdispatch_tsd_indexes.dti_queue_index);
218 dict->AddIntegerItem("dti_voucher_index",
219 m_libdispatch_tsd_indexes.dti_voucher_index);
220 dict->AddIntegerItem("dti_qos_class_index",
221 m_libdispatch_tsd_indexes.dti_qos_class_index);
222 }
223 }
224}
225
226bool SystemRuntimeMacOSX::SafeToCallFunctionsOnThisThread(ThreadSP thread_sp) {
227 if (thread_sp && thread_sp->GetStackFrameCount() > 0 &&
228 thread_sp->GetFrameWithConcreteFrameIndex(0)) {
229 const SymbolContext sym_ctx(
230 thread_sp->GetFrameWithConcreteFrameIndex(0)->GetSymbolContext(
231 eSymbolContextSymbol));
232 static ConstString g_select_symbol("__select");
233 if (sym_ctx.GetFunctionName() == g_select_symbol) {
234 return false;
235 }
236 }
237 return true;
Jason Molendab4892cd2014-05-13 22:02:48 +0000238}
239
Jason Molenda2fd83352014-02-05 05:44:54 +0000240lldb::queue_id_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241SystemRuntimeMacOSX::GetQueueIDFromThreadQAddress(lldb::addr_t dispatch_qaddr) {
242 queue_id_t queue_id = LLDB_INVALID_QUEUE_ID;
Jason Molenda2fd83352014-02-05 05:44:54 +0000243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
Jason Molenda2fd83352014-02-05 05:44:54 +0000245 return queue_id;
Jason Molenda2fd83352014-02-05 05:44:54 +0000246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 ReadLibdispatchOffsets();
248 if (m_libdispatch_offsets.IsValid()) {
249 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a
Adrian Prantl05097242018-04-30 16:49:04 +0000250 // thread - deref it to get the address of the dispatch_queue_t structure
251 // for this thread's queue.
Zachary Turner97206d52017-05-12 04:51:55 +0000252 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 uint64_t dispatch_queue_addr =
254 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
255 if (error.Success()) {
256 addr_t serialnum_address =
257 dispatch_queue_addr + m_libdispatch_offsets.dqo_serialnum;
258 queue_id_t serialnum = m_process->ReadUnsignedIntegerFromMemory(
259 serialnum_address, m_libdispatch_offsets.dqo_serialnum_size,
260 LLDB_INVALID_QUEUE_ID, error);
261 if (error.Success()) {
262 queue_id = serialnum;
263 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000264 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 }
266
267 return queue_id;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000268}
269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270void SystemRuntimeMacOSX::ReadLibdispatchOffsetsAddress() {
271 if (m_dispatch_queue_offsets_addr != LLDB_INVALID_ADDRESS)
272 return;
Jason Molenda705b1802014-06-13 02:37:02 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 static ConstString g_dispatch_queue_offsets_symbol_name(
275 "dispatch_queue_offsets");
276 const Symbol *dispatch_queue_offsets_symbol = NULL;
Jason Molenda705b1802014-06-13 02:37:02 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 // libdispatch symbols were in libSystem.B.dylib up through Mac OS X 10.6
279 // ("Snow Leopard")
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000280 ModuleSpec libSystem_module_spec(FileSpec("libSystem.B.dylib"));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
282 libSystem_module_spec));
283 if (module_sp)
284 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
285 g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
286
Adrian Prantl05097242018-04-30 16:49:04 +0000287 // libdispatch symbols are in their own dylib as of Mac OS X 10.7 ("Lion")
288 // and later
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 if (dispatch_queue_offsets_symbol == NULL) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000290 ModuleSpec libdispatch_module_spec(FileSpec("libdispatch.dylib"));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 module_sp = m_process->GetTarget().GetImages().FindFirstModule(
292 libdispatch_module_spec);
Jason Molenda705b1802014-06-13 02:37:02 +0000293 if (module_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
295 g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
296 }
297 if (dispatch_queue_offsets_symbol)
298 m_dispatch_queue_offsets_addr =
299 dispatch_queue_offsets_symbol->GetLoadAddress(&m_process->GetTarget());
Jason Molenda705b1802014-06-13 02:37:02 +0000300}
301
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302void SystemRuntimeMacOSX::ReadLibdispatchOffsets() {
303 if (m_libdispatch_offsets.IsValid())
304 return;
Jason Molenda705b1802014-06-13 02:37:02 +0000305
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306 ReadLibdispatchOffsetsAddress();
Jason Molenda705b1802014-06-13 02:37:02 +0000307
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 uint8_t memory_buffer[sizeof(struct LibdispatchOffsets)];
309 DataExtractor data(memory_buffer, sizeof(memory_buffer),
310 m_process->GetByteOrder(),
311 m_process->GetAddressByteSize());
312
Zachary Turner97206d52017-05-12 04:51:55 +0000313 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 if (m_process->ReadMemory(m_dispatch_queue_offsets_addr, memory_buffer,
315 sizeof(memory_buffer),
316 error) == sizeof(memory_buffer)) {
317 lldb::offset_t data_offset = 0;
318
319 // The struct LibdispatchOffsets is a series of uint16_t's - extract them
Adrian Prantl05097242018-04-30 16:49:04 +0000320 // all in one big go.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 data.GetU16(&data_offset, &m_libdispatch_offsets.dqo_version,
322 sizeof(struct LibdispatchOffsets) / sizeof(uint16_t));
323 }
Jason Molenda705b1802014-06-13 02:37:02 +0000324}
325
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326void SystemRuntimeMacOSX::ReadLibpthreadOffsetsAddress() {
327 if (m_libpthread_layout_offsets_addr != LLDB_INVALID_ADDRESS)
328 return;
Jason Molenda705b1802014-06-13 02:37:02 +0000329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330 static ConstString g_libpthread_layout_offsets_symbol_name(
331 "pthread_layout_offsets");
332 const Symbol *libpthread_layout_offsets_symbol = NULL;
Jason Molenda705b1802014-06-13 02:37:02 +0000333
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000334 ModuleSpec libpthread_module_spec(FileSpec("libsystem_pthread.dylib"));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
336 libpthread_module_spec));
337 if (module_sp) {
338 libpthread_layout_offsets_symbol =
339 module_sp->FindFirstSymbolWithNameAndType(
340 g_libpthread_layout_offsets_symbol_name, eSymbolTypeData);
341 if (libpthread_layout_offsets_symbol) {
342 m_libpthread_layout_offsets_addr =
343 libpthread_layout_offsets_symbol->GetLoadAddress(
344 &m_process->GetTarget());
Jason Molenda705b1802014-06-13 02:37:02 +0000345 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 }
Jason Molenda705b1802014-06-13 02:37:02 +0000347}
348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349void SystemRuntimeMacOSX::ReadLibpthreadOffsets() {
350 if (m_libpthread_offsets.IsValid())
351 return;
Jason Molenda705b1802014-06-13 02:37:02 +0000352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 ReadLibpthreadOffsetsAddress();
Jason Molenda705b1802014-06-13 02:37:02 +0000354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 if (m_libpthread_layout_offsets_addr != LLDB_INVALID_ADDRESS) {
356 uint8_t memory_buffer[sizeof(struct LibpthreadOffsets)];
357 DataExtractor data(memory_buffer, sizeof(memory_buffer),
358 m_process->GetByteOrder(),
359 m_process->GetAddressByteSize());
Zachary Turner97206d52017-05-12 04:51:55 +0000360 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 if (m_process->ReadMemory(m_libpthread_layout_offsets_addr, memory_buffer,
362 sizeof(memory_buffer),
363 error) == sizeof(memory_buffer)) {
364 lldb::offset_t data_offset = 0;
Jason Molenda59ac67e2014-09-12 00:09:04 +0000365
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 // The struct LibpthreadOffsets is a series of uint16_t's - extract them
Adrian Prantl05097242018-04-30 16:49:04 +0000367 // all in one big go.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 data.GetU16(&data_offset, &m_libpthread_offsets.plo_version,
369 sizeof(struct LibpthreadOffsets) / sizeof(uint16_t));
370 }
371 }
372}
373
374void SystemRuntimeMacOSX::ReadLibdispatchTSDIndexesAddress() {
375 if (m_dispatch_tsd_indexes_addr != LLDB_INVALID_ADDRESS)
376 return;
377
378 static ConstString g_libdispatch_tsd_indexes_symbol_name(
379 "dispatch_tsd_indexes");
380 const Symbol *libdispatch_tsd_indexes_symbol = NULL;
381
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000382 ModuleSpec libpthread_module_spec(FileSpec("libdispatch.dylib"));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
384 libpthread_module_spec));
385 if (module_sp) {
386 libdispatch_tsd_indexes_symbol = module_sp->FindFirstSymbolWithNameAndType(
387 g_libdispatch_tsd_indexes_symbol_name, eSymbolTypeData);
388 if (libdispatch_tsd_indexes_symbol) {
389 m_dispatch_tsd_indexes_addr =
390 libdispatch_tsd_indexes_symbol->GetLoadAddress(
391 &m_process->GetTarget());
392 }
393 }
394}
395
396void SystemRuntimeMacOSX::ReadLibdispatchTSDIndexes() {
397 if (m_libdispatch_tsd_indexes.IsValid())
398 return;
399
400 ReadLibdispatchTSDIndexesAddress();
401
402 if (m_dispatch_tsd_indexes_addr != LLDB_INVALID_ADDRESS) {
403
404// We don't need to check the version number right now, it will be at least 2,
Adrian Prantl05097242018-04-30 16:49:04 +0000405// but keep this code around to fetch just the version # for the future where
406// we need to fetch alternate versions of the struct.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407#if 0
Jason Molenda705b1802014-06-13 02:37:02 +0000408 uint16_t dti_version = 2;
Jason Molenda59ac67e2014-09-12 00:09:04 +0000409 Address dti_struct_addr;
410 if (m_process->GetTarget().ResolveLoadAddress (m_dispatch_tsd_indexes_addr, dti_struct_addr))
Jason Molenda705b1802014-06-13 02:37:02 +0000411 {
Zachary Turner97206d52017-05-12 04:51:55 +0000412 Status error;
Jason Molenda705b1802014-06-13 02:37:02 +0000413 uint16_t version = m_process->GetTarget().ReadUnsignedIntegerFromMemory (dti_struct_addr, false, 2, UINT16_MAX, error);
414 if (error.Success() && dti_version != UINT16_MAX)
415 {
416 dti_version = version;
417 }
418 }
Jason Molenda59ac67e2014-09-12 00:09:04 +0000419#endif
Jason Molenda705b1802014-06-13 02:37:02 +0000420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 ClangASTContext *ast_ctx =
422 m_process->GetTarget().GetScratchClangASTContext();
423 if (ast_ctx->getASTContext() &&
424 m_dispatch_tsd_indexes_addr != LLDB_INVALID_ADDRESS) {
425 CompilerType uint16 =
426 ast_ctx->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16);
427 CompilerType dispatch_tsd_indexes_s = ast_ctx->CreateRecordType(
428 nullptr, lldb::eAccessPublic, "__lldb_dispatch_tsd_indexes_s",
429 clang::TTK_Struct, lldb::eLanguageTypeC);
Greg Claytond8d4a572015-08-11 21:38:15 +0000430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 ClangASTContext::StartTagDeclarationDefinition(dispatch_tsd_indexes_s);
432 ClangASTContext::AddFieldToRecordType(dispatch_tsd_indexes_s,
433 "dti_version", uint16,
434 lldb::eAccessPublic, 0);
435 ClangASTContext::AddFieldToRecordType(dispatch_tsd_indexes_s,
436 "dti_queue_index", uint16,
437 lldb::eAccessPublic, 0);
438 ClangASTContext::AddFieldToRecordType(dispatch_tsd_indexes_s,
439 "dti_voucher_index", uint16,
440 lldb::eAccessPublic, 0);
441 ClangASTContext::AddFieldToRecordType(dispatch_tsd_indexes_s,
442 "dti_qos_class_index", uint16,
443 lldb::eAccessPublic, 0);
444 ClangASTContext::CompleteTagDeclarationDefinition(dispatch_tsd_indexes_s);
Jason Molenda59ac67e2014-09-12 00:09:04 +0000445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 ProcessStructReader struct_reader(m_process, m_dispatch_tsd_indexes_addr,
447 dispatch_tsd_indexes_s);
Jason Molenda59ac67e2014-09-12 00:09:04 +0000448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 m_libdispatch_tsd_indexes.dti_version =
450 struct_reader.GetField<uint16_t>(ConstString("dti_version"));
451 m_libdispatch_tsd_indexes.dti_queue_index =
452 struct_reader.GetField<uint16_t>(ConstString("dti_queue_index"));
453 m_libdispatch_tsd_indexes.dti_voucher_index =
454 struct_reader.GetField<uint16_t>(ConstString("dti_voucher_index"));
455 m_libdispatch_tsd_indexes.dti_qos_class_index =
456 struct_reader.GetField<uint16_t>(ConstString("dti_qos_class_index"));
Jason Molenda705b1802014-06-13 02:37:02 +0000457 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 }
Jason Molenda705b1802014-06-13 02:37:02 +0000459}
460
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461ThreadSP SystemRuntimeMacOSX::GetExtendedBacktraceThread(ThreadSP real_thread,
462 ConstString type) {
463 ThreadSP originating_thread_sp;
464 if (BacktraceRecordingHeadersInitialized() &&
465 type == ConstString("libdispatch")) {
Zachary Turner97206d52017-05-12 04:51:55 +0000466 Status error;
Jason Molenda2fd83352014-02-05 05:44:54 +0000467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 // real_thread is either an actual, live thread (in which case we need to
Adrian Prantl05097242018-04-30 16:49:04 +0000469 // call into libBacktraceRecording to find its originator) or it is an
470 // extended backtrace itself, in which case we get the token from it and
471 // call into libBacktraceRecording to find the originator of that token.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472
473 if (real_thread->GetExtendedBacktraceToken() != LLDB_INVALID_ADDRESS) {
474 originating_thread_sp = GetExtendedBacktraceFromItemRef(
475 real_thread->GetExtendedBacktraceToken());
476 } else {
477 ThreadSP cur_thread_sp(
478 m_process->GetThreadList().GetExpressionExecutionThread());
479 AppleGetThreadItemInfoHandler::GetThreadItemInfoReturnInfo ret =
480 m_get_thread_item_info_handler.GetThreadItemInfo(
481 *cur_thread_sp.get(), real_thread->GetID(), m_page_to_free,
482 m_page_to_free_size, error);
483 m_page_to_free = LLDB_INVALID_ADDRESS;
484 m_page_to_free_size = 0;
485 if (ret.item_buffer_ptr != 0 &&
486 ret.item_buffer_ptr != LLDB_INVALID_ADDRESS &&
487 ret.item_buffer_size > 0) {
488 DataBufferHeap data(ret.item_buffer_size, 0);
489 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
490 ret.item_buffer_size, error) &&
491 error.Success()) {
492 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
493 m_process->GetByteOrder(),
494 m_process->GetAddressByteSize());
495 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
496 bool stop_id_is_valid = true;
497 if (item.stop_id == 0)
498 stop_id_is_valid = false;
499 originating_thread_sp.reset(new HistoryThread(
500 *m_process, item.enqueuing_thread_id, item.enqueuing_callstack,
501 item.stop_id, stop_id_is_valid));
502 originating_thread_sp->SetExtendedBacktraceToken(
503 item.item_that_enqueued_this);
504 originating_thread_sp->SetQueueName(
505 item.enqueuing_queue_label.c_str());
506 originating_thread_sp->SetQueueID(item.enqueuing_queue_serialnum);
507 // originating_thread_sp->SetThreadName
508 // (item.enqueuing_thread_label.c_str());
Jason Molenda2fd83352014-02-05 05:44:54 +0000509 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 m_page_to_free = ret.item_buffer_ptr;
511 m_page_to_free_size = ret.item_buffer_size;
512 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000513 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 }
515 return originating_thread_sp;
Jason Molenda2fd83352014-02-05 05:44:54 +0000516}
517
518ThreadSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519SystemRuntimeMacOSX::GetExtendedBacktraceFromItemRef(lldb::addr_t item_ref) {
520 ThreadSP return_thread_sp;
Jason Molenda2fd83352014-02-05 05:44:54 +0000521
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522 AppleGetItemInfoHandler::GetItemInfoReturnInfo ret;
523 ThreadSP cur_thread_sp(
524 m_process->GetThreadList().GetExpressionExecutionThread());
Zachary Turner97206d52017-05-12 04:51:55 +0000525 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000526 ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
527 m_page_to_free, m_page_to_free_size,
528 error);
529 m_page_to_free = LLDB_INVALID_ADDRESS;
530 m_page_to_free_size = 0;
531 if (ret.item_buffer_ptr != 0 && ret.item_buffer_ptr != LLDB_INVALID_ADDRESS &&
532 ret.item_buffer_size > 0) {
533 DataBufferHeap data(ret.item_buffer_size, 0);
534 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
535 ret.item_buffer_size, error) &&
536 error.Success()) {
537 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
538 m_process->GetByteOrder(),
539 m_process->GetAddressByteSize());
540 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
541 bool stop_id_is_valid = true;
542 if (item.stop_id == 0)
Jason Molenda2fd83352014-02-05 05:44:54 +0000543 stop_id_is_valid = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000544 return_thread_sp.reset(new HistoryThread(
545 *m_process, item.enqueuing_thread_id, item.enqueuing_callstack,
546 item.stop_id, stop_id_is_valid));
547 return_thread_sp->SetExtendedBacktraceToken(item.item_that_enqueued_this);
548 return_thread_sp->SetQueueName(item.enqueuing_queue_label.c_str());
549 return_thread_sp->SetQueueID(item.enqueuing_queue_serialnum);
550 // return_thread_sp->SetThreadName
551 // (item.enqueuing_thread_label.c_str());
Jason Molenda2fd83352014-02-05 05:44:54 +0000552
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553 m_page_to_free = ret.item_buffer_ptr;
554 m_page_to_free_size = ret.item_buffer_size;
555 }
556 }
557 return return_thread_sp;
558}
Jason Molenda2fd83352014-02-05 05:44:54 +0000559
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560ThreadSP
561SystemRuntimeMacOSX::GetExtendedBacktraceForQueueItem(QueueItemSP queue_item_sp,
562 ConstString type) {
563 ThreadSP extended_thread_sp;
564 if (type != ConstString("libdispatch"))
Jason Molenda2fd83352014-02-05 05:44:54 +0000565 return extended_thread_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566
567 bool stop_id_is_valid = true;
568 if (queue_item_sp->GetStopID() == 0)
569 stop_id_is_valid = false;
570
571 extended_thread_sp.reset(
572 new HistoryThread(*m_process, queue_item_sp->GetEnqueueingThreadID(),
573 queue_item_sp->GetEnqueueingBacktrace(),
574 queue_item_sp->GetStopID(), stop_id_is_valid));
575 extended_thread_sp->SetExtendedBacktraceToken(
576 queue_item_sp->GetItemThatEnqueuedThis());
577 extended_thread_sp->SetQueueName(queue_item_sp->GetQueueLabel().c_str());
578 extended_thread_sp->SetQueueID(queue_item_sp->GetEnqueueingQueueID());
579 // extended_thread_sp->SetThreadName
580 // (queue_item_sp->GetThreadLabel().c_str());
581
582 return extended_thread_sp;
Jason Molenda2fd83352014-02-05 05:44:54 +0000583}
584
585/* Returns true if we were able to get the version / offset information
586 * out of libBacktraceRecording. false means we were unable to retrieve
587 * this; the queue_info_version field will be 0.
588 */
589
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
591 if (m_lib_backtrace_recording_info.queue_info_version != 0)
592 return true;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000593
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 addr_t queue_info_version_address = LLDB_INVALID_ADDRESS;
595 addr_t queue_info_data_offset_address = LLDB_INVALID_ADDRESS;
596 addr_t item_info_version_address = LLDB_INVALID_ADDRESS;
597 addr_t item_info_data_offset_address = LLDB_INVALID_ADDRESS;
598 Target &target = m_process->GetTarget();
Jason Molenda2fd83352014-02-05 05:44:54 +0000599
Kate Stoneb9c1b512016-09-06 20:57:50 +0000600 static ConstString introspection_dispatch_queue_info_version(
601 "__introspection_dispatch_queue_info_version");
602 SymbolContextList sc_list;
603 if (m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
604 introspection_dispatch_queue_info_version, eSymbolTypeData, sc_list) >
605 0) {
606 SymbolContext sc;
607 sc_list.GetContextAtIndex(0, sc);
608 AddressRange addr_range;
609 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
610 queue_info_version_address =
611 addr_range.GetBaseAddress().GetLoadAddress(&target);
612 }
613 sc_list.Clear();
Jason Molenda2fd83352014-02-05 05:44:54 +0000614
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 static ConstString introspection_dispatch_queue_info_data_offset(
616 "__introspection_dispatch_queue_info_data_offset");
617 if (m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
618 introspection_dispatch_queue_info_data_offset, eSymbolTypeData,
619 sc_list) > 0) {
620 SymbolContext sc;
621 sc_list.GetContextAtIndex(0, sc);
622 AddressRange addr_range;
623 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
624 queue_info_data_offset_address =
625 addr_range.GetBaseAddress().GetLoadAddress(&target);
626 }
627 sc_list.Clear();
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000628
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629 static ConstString introspection_dispatch_item_info_version(
630 "__introspection_dispatch_item_info_version");
631 if (m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
632 introspection_dispatch_item_info_version, eSymbolTypeData, sc_list) >
633 0) {
634 SymbolContext sc;
635 sc_list.GetContextAtIndex(0, sc);
636 AddressRange addr_range;
637 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
638 item_info_version_address =
639 addr_range.GetBaseAddress().GetLoadAddress(&target);
640 }
641 sc_list.Clear();
Jason Molenda2fd83352014-02-05 05:44:54 +0000642
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 static ConstString introspection_dispatch_item_info_data_offset(
644 "__introspection_dispatch_item_info_data_offset");
645 if (m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
646 introspection_dispatch_item_info_data_offset, eSymbolTypeData,
647 sc_list) > 0) {
648 SymbolContext sc;
649 sc_list.GetContextAtIndex(0, sc);
650 AddressRange addr_range;
651 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
652 item_info_data_offset_address =
653 addr_range.GetBaseAddress().GetLoadAddress(&target);
654 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000655
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656 if (queue_info_version_address != LLDB_INVALID_ADDRESS &&
657 queue_info_data_offset_address != LLDB_INVALID_ADDRESS &&
658 item_info_version_address != LLDB_INVALID_ADDRESS &&
659 item_info_data_offset_address != LLDB_INVALID_ADDRESS) {
Zachary Turner97206d52017-05-12 04:51:55 +0000660 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 m_lib_backtrace_recording_info.queue_info_version =
662 m_process->ReadUnsignedIntegerFromMemory(queue_info_version_address, 2,
663 0, error);
664 if (error.Success()) {
665 m_lib_backtrace_recording_info.queue_info_data_offset =
666 m_process->ReadUnsignedIntegerFromMemory(
667 queue_info_data_offset_address, 2, 0, error);
668 if (error.Success()) {
669 m_lib_backtrace_recording_info.item_info_version =
670 m_process->ReadUnsignedIntegerFromMemory(item_info_version_address,
671 2, 0, error);
672 if (error.Success()) {
673 m_lib_backtrace_recording_info.item_info_data_offset =
674 m_process->ReadUnsignedIntegerFromMemory(
675 item_info_data_offset_address, 2, 0, error);
676 if (!error.Success()) {
677 m_lib_backtrace_recording_info.queue_info_version = 0;
678 }
679 } else {
680 m_lib_backtrace_recording_info.queue_info_version = 0;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000681 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682 } else {
683 m_lib_backtrace_recording_info.queue_info_version = 0;
684 }
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000685 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686 }
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000687
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688 return m_lib_backtrace_recording_info.queue_info_version != 0;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000689}
690
691const std::vector<ConstString> &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692SystemRuntimeMacOSX::GetExtendedBacktraceTypes() {
693 if (m_types.size() == 0) {
694 m_types.push_back(ConstString("libdispatch"));
695 // We could have pthread as another type in the future if we have a way of
696 // gathering that information & it's useful to distinguish between them.
697 }
698 return m_types;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000699}
700
Kate Stoneb9c1b512016-09-06 20:57:50 +0000701void SystemRuntimeMacOSX::PopulateQueueList(
702 lldb_private::QueueList &queue_list) {
703 if (BacktraceRecordingHeadersInitialized()) {
704 AppleGetQueuesHandler::GetQueuesReturnInfo queue_info_pointer;
705 ThreadSP cur_thread_sp(
706 m_process->GetThreadList().GetExpressionExecutionThread());
707 if (cur_thread_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000708 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000709 queue_info_pointer = m_get_queues_handler.GetCurrentQueues(
710 *cur_thread_sp.get(), m_page_to_free, m_page_to_free_size, error);
711 m_page_to_free = LLDB_INVALID_ADDRESS;
712 m_page_to_free_size = 0;
713 if (error.Success()) {
Jason Molenda2fd83352014-02-05 05:44:54 +0000714
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715 if (queue_info_pointer.count > 0 &&
716 queue_info_pointer.queues_buffer_size > 0 &&
717 queue_info_pointer.queues_buffer_ptr != 0 &&
718 queue_info_pointer.queues_buffer_ptr != LLDB_INVALID_ADDRESS) {
719 PopulateQueuesUsingLibBTR(queue_info_pointer.queues_buffer_ptr,
720 queue_info_pointer.queues_buffer_size,
721 queue_info_pointer.count, queue_list);
Jason Molenda5e8dce42013-12-13 00:29:16 +0000722 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723 }
Jason Molenda5e8dce42013-12-13 00:29:16 +0000724 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000725 }
Jason Molendab9ffa982014-04-25 00:01:15 +0000726
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727 // We either didn't have libBacktraceRecording (and need to create the queues
Adrian Prantl05097242018-04-30 16:49:04 +0000728 // list based on threads) or we did get the queues list from
729 // libBacktraceRecording but some special queues may not be included in its
730 // information. This is needed because libBacktraceRecording will only list
731 // queues with pending or running items by default - but the magic com.apple
732 // .main-thread queue on thread 1 is always around.
Jason Molendab9ffa982014-04-25 00:01:15 +0000733
Kate Stoneb9c1b512016-09-06 20:57:50 +0000734 for (ThreadSP thread_sp : m_process->Threads()) {
735 if (thread_sp->GetAssociatedWithLibdispatchQueue() != eLazyBoolNo) {
736 if (thread_sp->GetQueueID() != LLDB_INVALID_QUEUE_ID) {
737 if (queue_list.FindQueueByID(thread_sp->GetQueueID()).get() == NULL) {
738 QueueSP queue_sp(new Queue(m_process->shared_from_this(),
739 thread_sp->GetQueueID(),
740 thread_sp->GetQueueName()));
741 if (thread_sp->ThreadHasQueueInformation()) {
742 queue_sp->SetKind(thread_sp->GetQueueKind());
743 queue_sp->SetLibdispatchQueueAddress(
744 thread_sp->GetQueueLibdispatchQueueAddress());
745 queue_list.AddQueue(queue_sp);
746 } else {
747 queue_sp->SetKind(
748 GetQueueKind(thread_sp->GetQueueLibdispatchQueueAddress()));
749 queue_sp->SetLibdispatchQueueAddress(
750 thread_sp->GetQueueLibdispatchQueueAddress());
751 queue_list.AddQueue(queue_sp);
752 }
Jason Molendab9ffa982014-04-25 00:01:15 +0000753 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 }
Jason Molendab9ffa982014-04-25 00:01:15 +0000755 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756 }
Jason Molenda5e8dce42013-12-13 00:29:16 +0000757}
758
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759// Returns either an array of introspection_dispatch_item_info_ref's for the
Adrian Prantl05097242018-04-30 16:49:04 +0000760// pending items on a queue or an array introspection_dispatch_item_info_ref's
761// and code addresses for the pending items on a queue. The information about
762// each of these pending items then needs to be fetched individually by passing
763// the ref to libBacktraceRecording.
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000764
765SystemRuntimeMacOSX::PendingItemsForQueue
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766SystemRuntimeMacOSX::GetPendingItemRefsForQueue(lldb::addr_t queue) {
767 PendingItemsForQueue pending_item_refs;
768 AppleGetPendingItemsHandler::GetPendingItemsReturnInfo pending_items_pointer;
769 ThreadSP cur_thread_sp(
770 m_process->GetThreadList().GetExpressionExecutionThread());
771 if (cur_thread_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000772 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 pending_items_pointer = m_get_pending_items_handler.GetPendingItems(
774 *cur_thread_sp.get(), queue, m_page_to_free, m_page_to_free_size,
775 error);
Jason Molendae32cd192014-03-10 08:42:03 +0000776 m_page_to_free = LLDB_INVALID_ADDRESS;
777 m_page_to_free_size = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 if (error.Success()) {
779 if (pending_items_pointer.count > 0 &&
780 pending_items_pointer.items_buffer_size > 0 &&
781 pending_items_pointer.items_buffer_ptr != 0 &&
782 pending_items_pointer.items_buffer_ptr != LLDB_INVALID_ADDRESS) {
783 DataBufferHeap data(pending_items_pointer.items_buffer_size, 0);
784 if (m_process->ReadMemory(
785 pending_items_pointer.items_buffer_ptr, data.GetBytes(),
786 pending_items_pointer.items_buffer_size, error)) {
787 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
788 m_process->GetByteOrder(),
789 m_process->GetAddressByteSize());
790
791 // We either have an array of
792 // void* item_ref
793 // (old style) or we have a structure returned which looks like
794 //
795 // struct introspection_dispatch_pending_item_info_s {
796 // void *item_ref;
797 // void *function_or_block;
798 // };
799 //
800 // struct introspection_dispatch_pending_items_array_s {
801 // uint32_t version;
802 // uint32_t size_of_item_info;
803 // introspection_dispatch_pending_item_info_s items[];
804 // }
805
806 offset_t offset = 0;
807 int i = 0;
808 uint32_t version = extractor.GetU32(&offset);
809 if (version == 1) {
810 pending_item_refs.new_style = true;
811 uint32_t item_size = extractor.GetU32(&offset);
812 uint32_t start_of_array_offset = offset;
813 while (offset < pending_items_pointer.items_buffer_size &&
814 static_cast<size_t>(i) < pending_items_pointer.count) {
815 offset = start_of_array_offset + (i * item_size);
816 ItemRefAndCodeAddress item;
817 item.item_ref = extractor.GetPointer(&offset);
818 item.code_address = extractor.GetPointer(&offset);
819 pending_item_refs.item_refs_and_code_addresses.push_back(item);
820 i++;
821 }
822 } else {
823 offset = 0;
824 pending_item_refs.new_style = false;
825 while (offset < pending_items_pointer.items_buffer_size &&
826 static_cast<size_t>(i) < pending_items_pointer.count) {
827 ItemRefAndCodeAddress item;
828 item.item_ref = extractor.GetPointer(&offset);
829 item.code_address = LLDB_INVALID_ADDRESS;
830 pending_item_refs.item_refs_and_code_addresses.push_back(item);
831 i++;
832 }
833 }
Jason Molendae32cd192014-03-10 08:42:03 +0000834 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000835 m_page_to_free = pending_items_pointer.items_buffer_ptr;
836 m_page_to_free_size = pending_items_pointer.items_buffer_size;
837 }
Jason Molendae32cd192014-03-10 08:42:03 +0000838 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000839 }
840 return pending_item_refs;
Jason Molendae32cd192014-03-10 08:42:03 +0000841}
842
Kate Stoneb9c1b512016-09-06 20:57:50 +0000843void SystemRuntimeMacOSX::PopulatePendingItemsForQueue(Queue *queue) {
844 if (BacktraceRecordingHeadersInitialized()) {
845 PendingItemsForQueue pending_item_refs =
846 GetPendingItemRefsForQueue(queue->GetLibdispatchQueueAddress());
847 for (ItemRefAndCodeAddress pending_item :
848 pending_item_refs.item_refs_and_code_addresses) {
849 Address addr;
850 m_process->GetTarget().ResolveLoadAddress(pending_item.code_address,
851 addr);
852 QueueItemSP queue_item_sp(new QueueItem(queue->shared_from_this(),
853 m_process->shared_from_this(),
854 pending_item.item_ref, addr));
855 queue->PushPendingQueueItem(queue_item_sp);
Jason Molenda2fd83352014-02-05 05:44:54 +0000856 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000857 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000858}
859
Kate Stoneb9c1b512016-09-06 20:57:50 +0000860void SystemRuntimeMacOSX::CompleteQueueItem(QueueItem *queue_item,
861 addr_t item_ref) {
862 AppleGetItemInfoHandler::GetItemInfoReturnInfo ret;
Jason Molenda2fd83352014-02-05 05:44:54 +0000863
Kate Stoneb9c1b512016-09-06 20:57:50 +0000864 ThreadSP cur_thread_sp(
865 m_process->GetThreadList().GetExpressionExecutionThread());
Zachary Turner97206d52017-05-12 04:51:55 +0000866 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867 ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
868 m_page_to_free, m_page_to_free_size,
869 error);
870 m_page_to_free = LLDB_INVALID_ADDRESS;
871 m_page_to_free_size = 0;
872 if (ret.item_buffer_ptr != 0 && ret.item_buffer_ptr != LLDB_INVALID_ADDRESS &&
873 ret.item_buffer_size > 0) {
874 DataBufferHeap data(ret.item_buffer_size, 0);
875 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
876 ret.item_buffer_size, error) &&
877 error.Success()) {
878 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
879 m_process->GetByteOrder(),
880 m_process->GetAddressByteSize());
881 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
882 queue_item->SetItemThatEnqueuedThis(item.item_that_enqueued_this);
883 queue_item->SetEnqueueingThreadID(item.enqueuing_thread_id);
884 queue_item->SetEnqueueingQueueID(item.enqueuing_queue_serialnum);
885 queue_item->SetStopID(item.stop_id);
886 queue_item->SetEnqueueingBacktrace(item.enqueuing_callstack);
887 queue_item->SetThreadLabel(item.enqueuing_thread_label);
888 queue_item->SetQueueLabel(item.enqueuing_queue_label);
889 queue_item->SetTargetQueueLabel(item.target_queue_label);
890 }
891 m_page_to_free = ret.item_buffer_ptr;
892 m_page_to_free_size = ret.item_buffer_size;
893 }
894}
895
896void SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR(
897 lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count,
898 lldb_private::QueueList &queue_list) {
Zachary Turner97206d52017-05-12 04:51:55 +0000899 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900 DataBufferHeap data(queues_buffer_size, 0);
901 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
902 if (m_process->ReadMemory(queues_buffer, data.GetBytes(), queues_buffer_size,
903 error) == queues_buffer_size &&
904 error.Success()) {
905 // We've read the information out of inferior memory; free it on the next
906 // call we make
907 m_page_to_free = queues_buffer;
908 m_page_to_free_size = queues_buffer_size;
909
910 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
911 m_process->GetByteOrder(),
912 m_process->GetAddressByteSize());
Jason Molenda2fd83352014-02-05 05:44:54 +0000913 offset_t offset = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000914 uint64_t queues_read = 0;
Jason Molenda2fd83352014-02-05 05:44:54 +0000915
Adrian Prantl05097242018-04-30 16:49:04 +0000916 // The information about the queues is stored in this format (v1): typedef
917 // struct introspection_dispatch_queue_info_s {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918 // uint32_t offset_to_next;
919 // dispatch_queue_t queue;
920 // uint64_t serialnum; // queue's serialnum in the process, as
921 // provided by libdispatch
922 // uint32_t running_work_items_count;
923 // uint32_t pending_work_items_count;
924 //
925 // char data[]; // Starting here, we have variable-length data:
926 // // char queue_label[];
927 // } introspection_dispatch_queue_info_s;
Jason Molenda2fd83352014-02-05 05:44:54 +0000928
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929 while (queues_read < count && offset < queues_buffer_size) {
930 offset_t start_of_this_item = offset;
931
932 uint32_t offset_to_next = extractor.GetU32(&offset);
933
934 offset += 4; // Skip over the 4 bytes of reserved space
935 addr_t queue = extractor.GetPointer(&offset);
936 uint64_t serialnum = extractor.GetU64(&offset);
937 uint32_t running_work_items_count = extractor.GetU32(&offset);
938 uint32_t pending_work_items_count = extractor.GetU32(&offset);
939
940 // Read the first field of the variable length data
941 offset = start_of_this_item +
942 m_lib_backtrace_recording_info.queue_info_data_offset;
943 const char *queue_label = extractor.GetCStr(&offset);
944 if (queue_label == NULL)
945 queue_label = "";
946
947 offset_t start_of_next_item = start_of_this_item + offset_to_next;
948 offset = start_of_next_item;
949
950 if (log)
951 log->Printf("SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR added "
952 "queue with dispatch_queue_t 0x%" PRIx64
953 ", serial number 0x%" PRIx64
954 ", running items %d, pending items %d, name '%s'",
955 queue, serialnum, running_work_items_count,
956 pending_work_items_count, queue_label);
957
958 QueueSP queue_sp(
959 new Queue(m_process->shared_from_this(), serialnum, queue_label));
960 queue_sp->SetNumRunningWorkItems(running_work_items_count);
961 queue_sp->SetNumPendingWorkItems(pending_work_items_count);
962 queue_sp->SetLibdispatchQueueAddress(queue);
963 queue_sp->SetKind(GetQueueKind(queue));
964 queue_list.AddQueue(queue_sp);
965 queues_read++;
Jason Molenda2fd83352014-02-05 05:44:54 +0000966 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000967 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000968}
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000969
Kate Stoneb9c1b512016-09-06 20:57:50 +0000970SystemRuntimeMacOSX::ItemInfo SystemRuntimeMacOSX::ExtractItemInfoFromBuffer(
971 lldb_private::DataExtractor &extractor) {
972 ItemInfo item;
973
974 offset_t offset = 0;
975
976 item.item_that_enqueued_this = extractor.GetPointer(&offset);
977 item.function_or_block = extractor.GetPointer(&offset);
978 item.enqueuing_thread_id = extractor.GetU64(&offset);
979 item.enqueuing_queue_serialnum = extractor.GetU64(&offset);
980 item.target_queue_serialnum = extractor.GetU64(&offset);
981 item.enqueuing_callstack_frame_count = extractor.GetU32(&offset);
982 item.stop_id = extractor.GetU32(&offset);
983
984 offset = m_lib_backtrace_recording_info.item_info_data_offset;
985
986 for (uint32_t i = 0; i < item.enqueuing_callstack_frame_count; i++) {
987 item.enqueuing_callstack.push_back(extractor.GetPointer(&offset));
988 }
989 item.enqueuing_thread_label = extractor.GetCStr(&offset);
990 item.enqueuing_queue_label = extractor.GetCStr(&offset);
991 item.target_queue_label = extractor.GetCStr(&offset);
992
993 return item;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000994}
995
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996void SystemRuntimeMacOSX::Initialize() {
997 PluginManager::RegisterPlugin(GetPluginNameStatic(),
998 GetPluginDescriptionStatic(), CreateInstance);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000999}
1000
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001void SystemRuntimeMacOSX::Terminate() {
1002 PluginManager::UnregisterPlugin(CreateInstance);
Jason Molendaa7b5afa2013-11-15 00:17:32 +00001003}
1004
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005lldb_private::ConstString SystemRuntimeMacOSX::GetPluginNameStatic() {
1006 static ConstString g_name("systemruntime-macosx");
1007 return g_name;
Jason Molendaa7b5afa2013-11-15 00:17:32 +00001008}
1009
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010const char *SystemRuntimeMacOSX::GetPluginDescriptionStatic() {
1011 return "System runtime plugin for Mac OS X native libraries.";
1012}
Jason Molendaa7b5afa2013-11-15 00:17:32 +00001013
1014//------------------------------------------------------------------
1015// PluginInterface protocol
1016//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001017lldb_private::ConstString SystemRuntimeMacOSX::GetPluginName() {
1018 return GetPluginNameStatic();
Jason Molendaa7b5afa2013-11-15 00:17:32 +00001019}
1020
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021uint32_t SystemRuntimeMacOSX::GetPluginVersion() { return 1; }