blob: 69e59bc4f7ee3ffc2c9e1f51ec93ea62cb2aae1b [file] [log] [blame]
Jason Molendaa7b5afa2013-11-15 00:17:32 +00001//===-- SystemRuntimeMacOSX.cpp ---------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jason Molendaa7b5afa2013-11-15 00:17:32 +00006//
7//===----------------------------------------------------------------------===//
8
Kate Stoneb9c1b512016-09-06 20:57:50 +00009#include "Plugins/Process/Utility/HistoryThread.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000010#include "lldb/Breakpoint/StoppointCallbackContext.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000011#include "lldb/Core/Module.h"
12#include "lldb/Core/ModuleSpec.h"
13#include "lldb/Core/PluginManager.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000014#include "lldb/Core/Section.h"
Zachary Turner88c6b622015-03-03 18:34:26 +000015#include "lldb/Symbol/ClangASTContext.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000016#include "lldb/Symbol/ObjectFile.h"
17#include "lldb/Symbol/SymbolContext.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include "lldb/Target/Process.h"
Zachary Turner01c32432017-02-14 19:06:07 +000019#include "lldb/Target/ProcessStructReader.h"
Jason Molenda5e8dce42013-12-13 00:29:16 +000020#include "lldb/Target/Queue.h"
21#include "lldb/Target/QueueList.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000022#include "lldb/Target/Target.h"
23#include "lldb/Target/Thread.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000024#include "lldb/Utility/DataBufferHeap.h"
25#include "lldb/Utility/DataExtractor.h"
Zachary Turner5713a052017-03-22 18:40:07 +000026#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000027#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000028#include "lldb/Utility/StreamString.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000029
Jason Molendaa7b5afa2013-11-15 00:17:32 +000030#include "SystemRuntimeMacOSX.h"
31
Jonas Devlieghere796ac802019-02-11 23:13:08 +000032#include <memory>
33
Jason Molendaa7b5afa2013-11-15 00:17:32 +000034using namespace lldb;
35using namespace lldb_private;
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.
Kate Stoneb9c1b512016-09-06 20:57:50 +000040SystemRuntime *SystemRuntimeMacOSX::CreateInstance(Process *process) {
41 bool create = false;
42 if (!create) {
43 create = true;
44 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
45 if (exe_module) {
46 ObjectFile *object_file = exe_module->GetObjectFile();
47 if (object_file) {
48 create = (object_file->GetStrata() == ObjectFile::eStrataUser);
49 }
Jason Molendaa7b5afa2013-11-15 00:17:32 +000050 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000051
52 if (create) {
53 const llvm::Triple &triple_ref =
54 process->GetTarget().GetArchitecture().GetTriple();
55 switch (triple_ref.getOS()) {
56 case llvm::Triple::Darwin:
57 case llvm::Triple::MacOSX:
58 case llvm::Triple::IOS:
59 case llvm::Triple::TvOS:
60 case llvm::Triple::WatchOS:
Jason Molenda32762fd2018-10-11 00:28:35 +000061 // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS:
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 create = triple_ref.getVendor() == llvm::Triple::Apple;
63 break;
64 default:
65 create = false;
66 break;
67 }
68 }
69 }
70
71 if (create)
72 return new SystemRuntimeMacOSX(process);
Konrad Kleine248a1302019-05-23 11:14:47 +000073 return nullptr;
Jason Molendaa7b5afa2013-11-15 00:17:32 +000074}
75
Jason Molendaa7b5afa2013-11-15 00:17:32 +000076// Constructor
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000077SystemRuntimeMacOSX::SystemRuntimeMacOSX(Process *process)
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 : SystemRuntime(process), m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
79 m_get_queues_handler(process), m_get_pending_items_handler(process),
80 m_get_item_info_handler(process), m_get_thread_item_info_handler(process),
81 m_page_to_free(LLDB_INVALID_ADDRESS), m_page_to_free_size(0),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000082 m_lib_backtrace_recording_info(),
83 m_dispatch_queue_offsets_addr(LLDB_INVALID_ADDRESS),
84 m_libdispatch_offsets(),
85 m_libpthread_layout_offsets_addr(LLDB_INVALID_ADDRESS),
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 m_libpthread_offsets(), m_dispatch_tsd_indexes_addr(LLDB_INVALID_ADDRESS),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000087 m_libdispatch_tsd_indexes(),
88 m_dispatch_voucher_offsets_addr(LLDB_INVALID_ADDRESS),
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 m_libdispatch_voucher_offsets() {}
Jason Molendaa7b5afa2013-11-15 00:17:32 +000090
Jason Molendaa7b5afa2013-11-15 00:17:32 +000091// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +000092SystemRuntimeMacOSX::~SystemRuntimeMacOSX() { Clear(true); }
Jason Molendaa7b5afa2013-11-15 00:17:32 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094void SystemRuntimeMacOSX::Detach() {
95 m_get_queues_handler.Detach();
96 m_get_pending_items_handler.Detach();
97 m_get_item_info_handler.Detach();
98 m_get_thread_item_info_handler.Detach();
Jason Molenda2fd83352014-02-05 05:44:54 +000099}
100
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000101// Clear out the state of this class.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102void SystemRuntimeMacOSX::Clear(bool clear_process) {
103 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
106 m_process->ClearBreakpointSiteByID(m_break_id);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 if (clear_process)
Konrad Kleine248a1302019-05-23 11:14:47 +0000109 m_process = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 m_break_id = LLDB_INVALID_BREAK_ID;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000111}
112
Jason Molenda2fd83352014-02-05 05:44:54 +0000113std::string
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114SystemRuntimeMacOSX::GetQueueNameFromThreadQAddress(addr_t dispatch_qaddr) {
115 std::string dispatch_queue_name;
116 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
117 return "";
Jason Molenda2fd83352014-02-05 05:44:54 +0000118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 ReadLibdispatchOffsets();
120 if (m_libdispatch_offsets.IsValid()) {
121 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a
Adrian Prantl05097242018-04-30 16:49:04 +0000122 // thread - deref it to get the address of the dispatch_queue_t structure
123 // for this thread's queue.
Zachary Turner97206d52017-05-12 04:51:55 +0000124 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 addr_t dispatch_queue_addr =
126 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
127 if (error.Success()) {
128 if (m_libdispatch_offsets.dqo_version >= 4) {
Adrian Prantl05097242018-04-30 16:49:04 +0000129 // libdispatch versions 4+, pointer to dispatch name is in the queue
130 // structure.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 addr_t pointer_to_label_address =
132 dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
133 addr_t label_addr =
134 m_process->ReadPointerFromMemory(pointer_to_label_address, error);
135 if (error.Success()) {
136 m_process->ReadCStringFromMemory(label_addr, dispatch_queue_name,
137 error);
138 }
139 } else {
140 // libdispatch versions 1-3, dispatch name is a fixed width char array
141 // in the queue structure.
142 addr_t label_addr =
143 dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
144 dispatch_queue_name.resize(m_libdispatch_offsets.dqo_label_size, '\0');
145 size_t bytes_read =
146 m_process->ReadMemory(label_addr, &dispatch_queue_name[0],
147 m_libdispatch_offsets.dqo_label_size, error);
148 if (bytes_read < m_libdispatch_offsets.dqo_label_size)
149 dispatch_queue_name.erase(bytes_read);
150 }
Jason Molendaaac16e02014-03-13 02:54:54 +0000151 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 }
153 return dispatch_queue_name;
Jason Molendaaac16e02014-03-13 02:54:54 +0000154}
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156lldb::addr_t SystemRuntimeMacOSX::GetLibdispatchQueueAddressFromThreadQAddress(
157 addr_t dispatch_qaddr) {
158 addr_t libdispatch_queue_t_address = LLDB_INVALID_ADDRESS;
Zachary Turner97206d52017-05-12 04:51:55 +0000159 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 libdispatch_queue_t_address =
161 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
162 if (!error.Success()) {
163 libdispatch_queue_t_address = LLDB_INVALID_ADDRESS;
164 }
165 return libdispatch_queue_t_address;
Jason Molendaaac16e02014-03-13 02:54:54 +0000166}
167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168lldb::QueueKind SystemRuntimeMacOSX::GetQueueKind(addr_t dispatch_queue_addr) {
169 if (dispatch_queue_addr == LLDB_INVALID_ADDRESS || dispatch_queue_addr == 0)
170 return eQueueKindUnknown;
171
172 QueueKind kind = eQueueKindUnknown;
173 ReadLibdispatchOffsets();
174 if (m_libdispatch_offsets.IsValid() &&
175 m_libdispatch_offsets.dqo_version >= 4) {
Zachary Turner97206d52017-05-12 04:51:55 +0000176 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 uint64_t width = m_process->ReadUnsignedIntegerFromMemory(
178 dispatch_queue_addr + m_libdispatch_offsets.dqo_width,
179 m_libdispatch_offsets.dqo_width_size, 0, error);
180 if (error.Success()) {
181 if (width == 1) {
182 kind = eQueueKindSerial;
183 }
184 if (width > 1) {
185 kind = eQueueKindConcurrent;
186 }
Jason Molenda705b1802014-06-13 02:37:02 +0000187 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 }
189 return kind;
Jason Molenda705b1802014-06-13 02:37:02 +0000190}
191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192void SystemRuntimeMacOSX::AddThreadExtendedInfoPacketHints(
193 lldb_private::StructuredData::ObjectSP dict_sp) {
194 StructuredData::Dictionary *dict = dict_sp->GetAsDictionary();
195 if (dict) {
196 ReadLibpthreadOffsets();
197 if (m_libpthread_offsets.IsValid()) {
198 dict->AddIntegerItem("plo_pthread_tsd_base_offset",
199 m_libpthread_offsets.plo_pthread_tsd_base_offset);
200 dict->AddIntegerItem(
201 "plo_pthread_tsd_base_address_offset",
202 m_libpthread_offsets.plo_pthread_tsd_base_address_offset);
203 dict->AddIntegerItem("plo_pthread_tsd_entry_size",
204 m_libpthread_offsets.plo_pthread_tsd_entry_size);
Jason Molendab4892cd2014-05-13 22:02:48 +0000205 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206
207 ReadLibdispatchTSDIndexes();
208 if (m_libdispatch_tsd_indexes.IsValid()) {
209 dict->AddIntegerItem("dti_queue_index",
210 m_libdispatch_tsd_indexes.dti_queue_index);
211 dict->AddIntegerItem("dti_voucher_index",
212 m_libdispatch_tsd_indexes.dti_voucher_index);
213 dict->AddIntegerItem("dti_qos_class_index",
214 m_libdispatch_tsd_indexes.dti_qos_class_index);
215 }
216 }
217}
218
219bool SystemRuntimeMacOSX::SafeToCallFunctionsOnThisThread(ThreadSP thread_sp) {
220 if (thread_sp && thread_sp->GetStackFrameCount() > 0 &&
221 thread_sp->GetFrameWithConcreteFrameIndex(0)) {
222 const SymbolContext sym_ctx(
223 thread_sp->GetFrameWithConcreteFrameIndex(0)->GetSymbolContext(
224 eSymbolContextSymbol));
225 static ConstString g_select_symbol("__select");
226 if (sym_ctx.GetFunctionName() == g_select_symbol) {
227 return false;
228 }
229 }
230 return true;
Jason Molendab4892cd2014-05-13 22:02:48 +0000231}
232
Jason Molenda2fd83352014-02-05 05:44:54 +0000233lldb::queue_id_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234SystemRuntimeMacOSX::GetQueueIDFromThreadQAddress(lldb::addr_t dispatch_qaddr) {
235 queue_id_t queue_id = LLDB_INVALID_QUEUE_ID;
Jason Molenda2fd83352014-02-05 05:44:54 +0000236
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
Jason Molenda2fd83352014-02-05 05:44:54 +0000238 return queue_id;
Jason Molenda2fd83352014-02-05 05:44:54 +0000239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 ReadLibdispatchOffsets();
241 if (m_libdispatch_offsets.IsValid()) {
242 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a
Adrian Prantl05097242018-04-30 16:49:04 +0000243 // thread - deref it to get the address of the dispatch_queue_t structure
244 // for this thread's queue.
Zachary Turner97206d52017-05-12 04:51:55 +0000245 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 uint64_t dispatch_queue_addr =
247 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
248 if (error.Success()) {
249 addr_t serialnum_address =
250 dispatch_queue_addr + m_libdispatch_offsets.dqo_serialnum;
251 queue_id_t serialnum = m_process->ReadUnsignedIntegerFromMemory(
252 serialnum_address, m_libdispatch_offsets.dqo_serialnum_size,
253 LLDB_INVALID_QUEUE_ID, error);
254 if (error.Success()) {
255 queue_id = serialnum;
256 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000257 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 }
259
260 return queue_id;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000261}
262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263void SystemRuntimeMacOSX::ReadLibdispatchOffsetsAddress() {
264 if (m_dispatch_queue_offsets_addr != LLDB_INVALID_ADDRESS)
265 return;
Jason Molenda705b1802014-06-13 02:37:02 +0000266
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 static ConstString g_dispatch_queue_offsets_symbol_name(
268 "dispatch_queue_offsets");
Konrad Kleine248a1302019-05-23 11:14:47 +0000269 const Symbol *dispatch_queue_offsets_symbol = nullptr;
Jason Molenda705b1802014-06-13 02:37:02 +0000270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 // libdispatch symbols were in libSystem.B.dylib up through Mac OS X 10.6
272 // ("Snow Leopard")
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000273 ModuleSpec libSystem_module_spec(FileSpec("libSystem.B.dylib"));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
275 libSystem_module_spec));
276 if (module_sp)
277 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
278 g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
279
Adrian Prantl05097242018-04-30 16:49:04 +0000280 // libdispatch symbols are in their own dylib as of Mac OS X 10.7 ("Lion")
281 // and later
Konrad Kleine248a1302019-05-23 11:14:47 +0000282 if (dispatch_queue_offsets_symbol == nullptr) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000283 ModuleSpec libdispatch_module_spec(FileSpec("libdispatch.dylib"));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 module_sp = m_process->GetTarget().GetImages().FindFirstModule(
285 libdispatch_module_spec);
Jason Molenda705b1802014-06-13 02:37:02 +0000286 if (module_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
288 g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
289 }
290 if (dispatch_queue_offsets_symbol)
291 m_dispatch_queue_offsets_addr =
292 dispatch_queue_offsets_symbol->GetLoadAddress(&m_process->GetTarget());
Jason Molenda705b1802014-06-13 02:37:02 +0000293}
294
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295void SystemRuntimeMacOSX::ReadLibdispatchOffsets() {
296 if (m_libdispatch_offsets.IsValid())
297 return;
Jason Molenda705b1802014-06-13 02:37:02 +0000298
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 ReadLibdispatchOffsetsAddress();
Jason Molenda705b1802014-06-13 02:37:02 +0000300
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301 uint8_t memory_buffer[sizeof(struct LibdispatchOffsets)];
302 DataExtractor data(memory_buffer, sizeof(memory_buffer),
303 m_process->GetByteOrder(),
304 m_process->GetAddressByteSize());
305
Zachary Turner97206d52017-05-12 04:51:55 +0000306 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307 if (m_process->ReadMemory(m_dispatch_queue_offsets_addr, memory_buffer,
308 sizeof(memory_buffer),
309 error) == sizeof(memory_buffer)) {
310 lldb::offset_t data_offset = 0;
311
312 // The struct LibdispatchOffsets is a series of uint16_t's - extract them
Adrian Prantl05097242018-04-30 16:49:04 +0000313 // all in one big go.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 data.GetU16(&data_offset, &m_libdispatch_offsets.dqo_version,
315 sizeof(struct LibdispatchOffsets) / sizeof(uint16_t));
316 }
Jason Molenda705b1802014-06-13 02:37:02 +0000317}
318
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319void SystemRuntimeMacOSX::ReadLibpthreadOffsetsAddress() {
320 if (m_libpthread_layout_offsets_addr != LLDB_INVALID_ADDRESS)
321 return;
Jason Molenda705b1802014-06-13 02:37:02 +0000322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 static ConstString g_libpthread_layout_offsets_symbol_name(
324 "pthread_layout_offsets");
Konrad Kleine248a1302019-05-23 11:14:47 +0000325 const Symbol *libpthread_layout_offsets_symbol = nullptr;
Jason Molenda705b1802014-06-13 02:37:02 +0000326
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000327 ModuleSpec libpthread_module_spec(FileSpec("libsystem_pthread.dylib"));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
329 libpthread_module_spec));
330 if (module_sp) {
331 libpthread_layout_offsets_symbol =
332 module_sp->FindFirstSymbolWithNameAndType(
333 g_libpthread_layout_offsets_symbol_name, eSymbolTypeData);
334 if (libpthread_layout_offsets_symbol) {
335 m_libpthread_layout_offsets_addr =
336 libpthread_layout_offsets_symbol->GetLoadAddress(
337 &m_process->GetTarget());
Jason Molenda705b1802014-06-13 02:37:02 +0000338 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 }
Jason Molenda705b1802014-06-13 02:37:02 +0000340}
341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342void SystemRuntimeMacOSX::ReadLibpthreadOffsets() {
343 if (m_libpthread_offsets.IsValid())
344 return;
Jason Molenda705b1802014-06-13 02:37:02 +0000345
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 ReadLibpthreadOffsetsAddress();
Jason Molenda705b1802014-06-13 02:37:02 +0000347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 if (m_libpthread_layout_offsets_addr != LLDB_INVALID_ADDRESS) {
349 uint8_t memory_buffer[sizeof(struct LibpthreadOffsets)];
350 DataExtractor data(memory_buffer, sizeof(memory_buffer),
351 m_process->GetByteOrder(),
352 m_process->GetAddressByteSize());
Zachary Turner97206d52017-05-12 04:51:55 +0000353 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 if (m_process->ReadMemory(m_libpthread_layout_offsets_addr, memory_buffer,
355 sizeof(memory_buffer),
356 error) == sizeof(memory_buffer)) {
357 lldb::offset_t data_offset = 0;
Jason Molenda59ac67e2014-09-12 00:09:04 +0000358
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 // The struct LibpthreadOffsets is a series of uint16_t's - extract them
Adrian Prantl05097242018-04-30 16:49:04 +0000360 // all in one big go.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 data.GetU16(&data_offset, &m_libpthread_offsets.plo_version,
362 sizeof(struct LibpthreadOffsets) / sizeof(uint16_t));
363 }
364 }
365}
366
367void SystemRuntimeMacOSX::ReadLibdispatchTSDIndexesAddress() {
368 if (m_dispatch_tsd_indexes_addr != LLDB_INVALID_ADDRESS)
369 return;
370
371 static ConstString g_libdispatch_tsd_indexes_symbol_name(
372 "dispatch_tsd_indexes");
Konrad Kleine248a1302019-05-23 11:14:47 +0000373 const Symbol *libdispatch_tsd_indexes_symbol = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000375 ModuleSpec libpthread_module_spec(FileSpec("libdispatch.dylib"));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
377 libpthread_module_spec));
378 if (module_sp) {
379 libdispatch_tsd_indexes_symbol = module_sp->FindFirstSymbolWithNameAndType(
380 g_libdispatch_tsd_indexes_symbol_name, eSymbolTypeData);
381 if (libdispatch_tsd_indexes_symbol) {
382 m_dispatch_tsd_indexes_addr =
383 libdispatch_tsd_indexes_symbol->GetLoadAddress(
384 &m_process->GetTarget());
385 }
386 }
387}
388
389void SystemRuntimeMacOSX::ReadLibdispatchTSDIndexes() {
390 if (m_libdispatch_tsd_indexes.IsValid())
391 return;
392
393 ReadLibdispatchTSDIndexesAddress();
394
395 if (m_dispatch_tsd_indexes_addr != LLDB_INVALID_ADDRESS) {
396
397// We don't need to check the version number right now, it will be at least 2,
Adrian Prantl05097242018-04-30 16:49:04 +0000398// but keep this code around to fetch just the version # for the future where
399// we need to fetch alternate versions of the struct.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400#if 0
Jason Molenda705b1802014-06-13 02:37:02 +0000401 uint16_t dti_version = 2;
Jason Molenda59ac67e2014-09-12 00:09:04 +0000402 Address dti_struct_addr;
403 if (m_process->GetTarget().ResolveLoadAddress (m_dispatch_tsd_indexes_addr, dti_struct_addr))
Jason Molenda705b1802014-06-13 02:37:02 +0000404 {
Zachary Turner97206d52017-05-12 04:51:55 +0000405 Status error;
Jason Molenda705b1802014-06-13 02:37:02 +0000406 uint16_t version = m_process->GetTarget().ReadUnsignedIntegerFromMemory (dti_struct_addr, false, 2, UINT16_MAX, error);
407 if (error.Success() && dti_version != UINT16_MAX)
408 {
409 dti_version = version;
410 }
411 }
Jason Molenda59ac67e2014-09-12 00:09:04 +0000412#endif
Jason Molenda705b1802014-06-13 02:37:02 +0000413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 ClangASTContext *ast_ctx =
415 m_process->GetTarget().GetScratchClangASTContext();
416 if (ast_ctx->getASTContext() &&
417 m_dispatch_tsd_indexes_addr != LLDB_INVALID_ADDRESS) {
418 CompilerType uint16 =
419 ast_ctx->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16);
420 CompilerType dispatch_tsd_indexes_s = ast_ctx->CreateRecordType(
421 nullptr, lldb::eAccessPublic, "__lldb_dispatch_tsd_indexes_s",
422 clang::TTK_Struct, lldb::eLanguageTypeC);
Greg Claytond8d4a572015-08-11 21:38:15 +0000423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 ClangASTContext::StartTagDeclarationDefinition(dispatch_tsd_indexes_s);
425 ClangASTContext::AddFieldToRecordType(dispatch_tsd_indexes_s,
426 "dti_version", uint16,
427 lldb::eAccessPublic, 0);
428 ClangASTContext::AddFieldToRecordType(dispatch_tsd_indexes_s,
429 "dti_queue_index", uint16,
430 lldb::eAccessPublic, 0);
431 ClangASTContext::AddFieldToRecordType(dispatch_tsd_indexes_s,
432 "dti_voucher_index", uint16,
433 lldb::eAccessPublic, 0);
434 ClangASTContext::AddFieldToRecordType(dispatch_tsd_indexes_s,
435 "dti_qos_class_index", uint16,
436 lldb::eAccessPublic, 0);
437 ClangASTContext::CompleteTagDeclarationDefinition(dispatch_tsd_indexes_s);
Jason Molenda59ac67e2014-09-12 00:09:04 +0000438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 ProcessStructReader struct_reader(m_process, m_dispatch_tsd_indexes_addr,
440 dispatch_tsd_indexes_s);
Jason Molenda59ac67e2014-09-12 00:09:04 +0000441
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 m_libdispatch_tsd_indexes.dti_version =
443 struct_reader.GetField<uint16_t>(ConstString("dti_version"));
444 m_libdispatch_tsd_indexes.dti_queue_index =
445 struct_reader.GetField<uint16_t>(ConstString("dti_queue_index"));
446 m_libdispatch_tsd_indexes.dti_voucher_index =
447 struct_reader.GetField<uint16_t>(ConstString("dti_voucher_index"));
448 m_libdispatch_tsd_indexes.dti_qos_class_index =
449 struct_reader.GetField<uint16_t>(ConstString("dti_qos_class_index"));
Jason Molenda705b1802014-06-13 02:37:02 +0000450 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 }
Jason Molenda705b1802014-06-13 02:37:02 +0000452}
453
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454ThreadSP SystemRuntimeMacOSX::GetExtendedBacktraceThread(ThreadSP real_thread,
455 ConstString type) {
456 ThreadSP originating_thread_sp;
Raphael Isemann05cfdb02019-04-26 07:21:36 +0000457 if (BacktraceRecordingHeadersInitialized() && type == "libdispatch") {
Zachary Turner97206d52017-05-12 04:51:55 +0000458 Status error;
Jason Molenda2fd83352014-02-05 05:44:54 +0000459
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 // real_thread is either an actual, live thread (in which case we need to
Adrian Prantl05097242018-04-30 16:49:04 +0000461 // call into libBacktraceRecording to find its originator) or it is an
462 // extended backtrace itself, in which case we get the token from it and
463 // call into libBacktraceRecording to find the originator of that token.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464
465 if (real_thread->GetExtendedBacktraceToken() != LLDB_INVALID_ADDRESS) {
466 originating_thread_sp = GetExtendedBacktraceFromItemRef(
467 real_thread->GetExtendedBacktraceToken());
468 } else {
469 ThreadSP cur_thread_sp(
470 m_process->GetThreadList().GetExpressionExecutionThread());
471 AppleGetThreadItemInfoHandler::GetThreadItemInfoReturnInfo ret =
472 m_get_thread_item_info_handler.GetThreadItemInfo(
473 *cur_thread_sp.get(), real_thread->GetID(), m_page_to_free,
474 m_page_to_free_size, error);
475 m_page_to_free = LLDB_INVALID_ADDRESS;
476 m_page_to_free_size = 0;
477 if (ret.item_buffer_ptr != 0 &&
478 ret.item_buffer_ptr != LLDB_INVALID_ADDRESS &&
479 ret.item_buffer_size > 0) {
480 DataBufferHeap data(ret.item_buffer_size, 0);
481 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
482 ret.item_buffer_size, error) &&
483 error.Success()) {
484 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
485 m_process->GetByteOrder(),
486 m_process->GetAddressByteSize());
487 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000488 originating_thread_sp = std::make_shared<HistoryThread>(
Alex Langford86df61c2019-06-19 21:33:44 +0000489 *m_process, item.enqueuing_thread_id, item.enqueuing_callstack);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 originating_thread_sp->SetExtendedBacktraceToken(
491 item.item_that_enqueued_this);
492 originating_thread_sp->SetQueueName(
493 item.enqueuing_queue_label.c_str());
494 originating_thread_sp->SetQueueID(item.enqueuing_queue_serialnum);
495 // originating_thread_sp->SetThreadName
496 // (item.enqueuing_thread_label.c_str());
Jason Molenda2fd83352014-02-05 05:44:54 +0000497 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498 m_page_to_free = ret.item_buffer_ptr;
499 m_page_to_free_size = ret.item_buffer_size;
500 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000501 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 }
503 return originating_thread_sp;
Jason Molenda2fd83352014-02-05 05:44:54 +0000504}
505
506ThreadSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507SystemRuntimeMacOSX::GetExtendedBacktraceFromItemRef(lldb::addr_t item_ref) {
508 ThreadSP return_thread_sp;
Jason Molenda2fd83352014-02-05 05:44:54 +0000509
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 AppleGetItemInfoHandler::GetItemInfoReturnInfo ret;
511 ThreadSP cur_thread_sp(
512 m_process->GetThreadList().GetExpressionExecutionThread());
Zachary Turner97206d52017-05-12 04:51:55 +0000513 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
515 m_page_to_free, m_page_to_free_size,
516 error);
517 m_page_to_free = LLDB_INVALID_ADDRESS;
518 m_page_to_free_size = 0;
519 if (ret.item_buffer_ptr != 0 && ret.item_buffer_ptr != LLDB_INVALID_ADDRESS &&
520 ret.item_buffer_size > 0) {
521 DataBufferHeap data(ret.item_buffer_size, 0);
522 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
523 ret.item_buffer_size, error) &&
524 error.Success()) {
525 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
526 m_process->GetByteOrder(),
527 m_process->GetAddressByteSize());
528 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000529 return_thread_sp = std::make_shared<HistoryThread>(
Alex Langford86df61c2019-06-19 21:33:44 +0000530 *m_process, item.enqueuing_thread_id, item.enqueuing_callstack);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000531 return_thread_sp->SetExtendedBacktraceToken(item.item_that_enqueued_this);
532 return_thread_sp->SetQueueName(item.enqueuing_queue_label.c_str());
533 return_thread_sp->SetQueueID(item.enqueuing_queue_serialnum);
534 // return_thread_sp->SetThreadName
535 // (item.enqueuing_thread_label.c_str());
Jason Molenda2fd83352014-02-05 05:44:54 +0000536
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537 m_page_to_free = ret.item_buffer_ptr;
538 m_page_to_free_size = ret.item_buffer_size;
539 }
540 }
541 return return_thread_sp;
542}
Jason Molenda2fd83352014-02-05 05:44:54 +0000543
Kate Stoneb9c1b512016-09-06 20:57:50 +0000544ThreadSP
545SystemRuntimeMacOSX::GetExtendedBacktraceForQueueItem(QueueItemSP queue_item_sp,
546 ConstString type) {
547 ThreadSP extended_thread_sp;
Raphael Isemann05cfdb02019-04-26 07:21:36 +0000548 if (type != "libdispatch")
Jason Molenda2fd83352014-02-05 05:44:54 +0000549 return extended_thread_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000551 extended_thread_sp = std::make_shared<HistoryThread>(
552 *m_process, queue_item_sp->GetEnqueueingThreadID(),
Alex Langford86df61c2019-06-19 21:33:44 +0000553 queue_item_sp->GetEnqueueingBacktrace());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554 extended_thread_sp->SetExtendedBacktraceToken(
555 queue_item_sp->GetItemThatEnqueuedThis());
556 extended_thread_sp->SetQueueName(queue_item_sp->GetQueueLabel().c_str());
557 extended_thread_sp->SetQueueID(queue_item_sp->GetEnqueueingQueueID());
558 // extended_thread_sp->SetThreadName
559 // (queue_item_sp->GetThreadLabel().c_str());
560
561 return extended_thread_sp;
Jason Molenda2fd83352014-02-05 05:44:54 +0000562}
563
564/* Returns true if we were able to get the version / offset information
565 * out of libBacktraceRecording. false means we were unable to retrieve
566 * this; the queue_info_version field will be 0.
567 */
568
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
570 if (m_lib_backtrace_recording_info.queue_info_version != 0)
571 return true;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000572
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 addr_t queue_info_version_address = LLDB_INVALID_ADDRESS;
574 addr_t queue_info_data_offset_address = LLDB_INVALID_ADDRESS;
575 addr_t item_info_version_address = LLDB_INVALID_ADDRESS;
576 addr_t item_info_data_offset_address = LLDB_INVALID_ADDRESS;
577 Target &target = m_process->GetTarget();
Jason Molenda2fd83352014-02-05 05:44:54 +0000578
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 static ConstString introspection_dispatch_queue_info_version(
580 "__introspection_dispatch_queue_info_version");
581 SymbolContextList sc_list;
Adrian Prantl1ad655e2019-10-17 19:56:40 +0000582 m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
583 introspection_dispatch_queue_info_version, eSymbolTypeData, sc_list);
584 if (!sc_list.IsEmpty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 SymbolContext sc;
586 sc_list.GetContextAtIndex(0, sc);
587 AddressRange addr_range;
588 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
589 queue_info_version_address =
590 addr_range.GetBaseAddress().GetLoadAddress(&target);
591 }
592 sc_list.Clear();
Jason Molenda2fd83352014-02-05 05:44:54 +0000593
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 static ConstString introspection_dispatch_queue_info_data_offset(
595 "__introspection_dispatch_queue_info_data_offset");
Adrian Prantl1ad655e2019-10-17 19:56:40 +0000596 m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
597 introspection_dispatch_queue_info_data_offset, eSymbolTypeData, sc_list);
598 if (!sc_list.IsEmpty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599 SymbolContext sc;
600 sc_list.GetContextAtIndex(0, sc);
601 AddressRange addr_range;
602 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
603 queue_info_data_offset_address =
604 addr_range.GetBaseAddress().GetLoadAddress(&target);
605 }
606 sc_list.Clear();
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000607
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 static ConstString introspection_dispatch_item_info_version(
609 "__introspection_dispatch_item_info_version");
Adrian Prantl1ad655e2019-10-17 19:56:40 +0000610 m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
611 introspection_dispatch_item_info_version, eSymbolTypeData, sc_list);
612 if (!sc_list.IsEmpty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613 SymbolContext sc;
614 sc_list.GetContextAtIndex(0, sc);
615 AddressRange addr_range;
616 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
617 item_info_version_address =
618 addr_range.GetBaseAddress().GetLoadAddress(&target);
619 }
620 sc_list.Clear();
Jason Molenda2fd83352014-02-05 05:44:54 +0000621
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622 static ConstString introspection_dispatch_item_info_data_offset(
623 "__introspection_dispatch_item_info_data_offset");
Adrian Prantl1ad655e2019-10-17 19:56:40 +0000624 m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
625 introspection_dispatch_item_info_data_offset, eSymbolTypeData, sc_list);
626 if (!sc_list.IsEmpty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627 SymbolContext sc;
628 sc_list.GetContextAtIndex(0, sc);
629 AddressRange addr_range;
630 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
631 item_info_data_offset_address =
632 addr_range.GetBaseAddress().GetLoadAddress(&target);
633 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000634
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635 if (queue_info_version_address != LLDB_INVALID_ADDRESS &&
636 queue_info_data_offset_address != LLDB_INVALID_ADDRESS &&
637 item_info_version_address != LLDB_INVALID_ADDRESS &&
638 item_info_data_offset_address != LLDB_INVALID_ADDRESS) {
Zachary Turner97206d52017-05-12 04:51:55 +0000639 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 m_lib_backtrace_recording_info.queue_info_version =
641 m_process->ReadUnsignedIntegerFromMemory(queue_info_version_address, 2,
642 0, error);
643 if (error.Success()) {
644 m_lib_backtrace_recording_info.queue_info_data_offset =
645 m_process->ReadUnsignedIntegerFromMemory(
646 queue_info_data_offset_address, 2, 0, error);
647 if (error.Success()) {
648 m_lib_backtrace_recording_info.item_info_version =
649 m_process->ReadUnsignedIntegerFromMemory(item_info_version_address,
650 2, 0, error);
651 if (error.Success()) {
652 m_lib_backtrace_recording_info.item_info_data_offset =
653 m_process->ReadUnsignedIntegerFromMemory(
654 item_info_data_offset_address, 2, 0, error);
655 if (!error.Success()) {
656 m_lib_backtrace_recording_info.queue_info_version = 0;
657 }
658 } else {
659 m_lib_backtrace_recording_info.queue_info_version = 0;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000660 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 } else {
662 m_lib_backtrace_recording_info.queue_info_version = 0;
663 }
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000664 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000665 }
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000666
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667 return m_lib_backtrace_recording_info.queue_info_version != 0;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000668}
669
670const std::vector<ConstString> &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000671SystemRuntimeMacOSX::GetExtendedBacktraceTypes() {
672 if (m_types.size() == 0) {
673 m_types.push_back(ConstString("libdispatch"));
674 // We could have pthread as another type in the future if we have a way of
675 // gathering that information & it's useful to distinguish between them.
676 }
677 return m_types;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000678}
679
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680void SystemRuntimeMacOSX::PopulateQueueList(
681 lldb_private::QueueList &queue_list) {
682 if (BacktraceRecordingHeadersInitialized()) {
683 AppleGetQueuesHandler::GetQueuesReturnInfo queue_info_pointer;
684 ThreadSP cur_thread_sp(
685 m_process->GetThreadList().GetExpressionExecutionThread());
686 if (cur_thread_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000687 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688 queue_info_pointer = m_get_queues_handler.GetCurrentQueues(
689 *cur_thread_sp.get(), m_page_to_free, m_page_to_free_size, error);
690 m_page_to_free = LLDB_INVALID_ADDRESS;
691 m_page_to_free_size = 0;
692 if (error.Success()) {
Jason Molenda2fd83352014-02-05 05:44:54 +0000693
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694 if (queue_info_pointer.count > 0 &&
695 queue_info_pointer.queues_buffer_size > 0 &&
696 queue_info_pointer.queues_buffer_ptr != 0 &&
697 queue_info_pointer.queues_buffer_ptr != LLDB_INVALID_ADDRESS) {
698 PopulateQueuesUsingLibBTR(queue_info_pointer.queues_buffer_ptr,
699 queue_info_pointer.queues_buffer_size,
700 queue_info_pointer.count, queue_list);
Jason Molenda5e8dce42013-12-13 00:29:16 +0000701 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702 }
Jason Molenda5e8dce42013-12-13 00:29:16 +0000703 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704 }
Jason Molendab9ffa982014-04-25 00:01:15 +0000705
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706 // We either didn't have libBacktraceRecording (and need to create the queues
Adrian Prantl05097242018-04-30 16:49:04 +0000707 // list based on threads) or we did get the queues list from
708 // libBacktraceRecording but some special queues may not be included in its
709 // information. This is needed because libBacktraceRecording will only list
710 // queues with pending or running items by default - but the magic com.apple
711 // .main-thread queue on thread 1 is always around.
Jason Molendab9ffa982014-04-25 00:01:15 +0000712
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713 for (ThreadSP thread_sp : m_process->Threads()) {
714 if (thread_sp->GetAssociatedWithLibdispatchQueue() != eLazyBoolNo) {
715 if (thread_sp->GetQueueID() != LLDB_INVALID_QUEUE_ID) {
Konrad Kleine248a1302019-05-23 11:14:47 +0000716 if (queue_list.FindQueueByID(thread_sp->GetQueueID()).get() ==
717 nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718 QueueSP queue_sp(new Queue(m_process->shared_from_this(),
719 thread_sp->GetQueueID(),
720 thread_sp->GetQueueName()));
721 if (thread_sp->ThreadHasQueueInformation()) {
722 queue_sp->SetKind(thread_sp->GetQueueKind());
723 queue_sp->SetLibdispatchQueueAddress(
724 thread_sp->GetQueueLibdispatchQueueAddress());
725 queue_list.AddQueue(queue_sp);
726 } else {
727 queue_sp->SetKind(
728 GetQueueKind(thread_sp->GetQueueLibdispatchQueueAddress()));
729 queue_sp->SetLibdispatchQueueAddress(
730 thread_sp->GetQueueLibdispatchQueueAddress());
731 queue_list.AddQueue(queue_sp);
732 }
Jason Molendab9ffa982014-04-25 00:01:15 +0000733 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000734 }
Jason Molendab9ffa982014-04-25 00:01:15 +0000735 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 }
Jason Molenda5e8dce42013-12-13 00:29:16 +0000737}
738
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739// Returns either an array of introspection_dispatch_item_info_ref's for the
Adrian Prantl05097242018-04-30 16:49:04 +0000740// pending items on a queue or an array introspection_dispatch_item_info_ref's
741// and code addresses for the pending items on a queue. The information about
742// each of these pending items then needs to be fetched individually by passing
743// the ref to libBacktraceRecording.
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000744
745SystemRuntimeMacOSX::PendingItemsForQueue
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746SystemRuntimeMacOSX::GetPendingItemRefsForQueue(lldb::addr_t queue) {
747 PendingItemsForQueue pending_item_refs;
748 AppleGetPendingItemsHandler::GetPendingItemsReturnInfo pending_items_pointer;
749 ThreadSP cur_thread_sp(
750 m_process->GetThreadList().GetExpressionExecutionThread());
751 if (cur_thread_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000752 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753 pending_items_pointer = m_get_pending_items_handler.GetPendingItems(
754 *cur_thread_sp.get(), queue, m_page_to_free, m_page_to_free_size,
755 error);
Jason Molendae32cd192014-03-10 08:42:03 +0000756 m_page_to_free = LLDB_INVALID_ADDRESS;
757 m_page_to_free_size = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758 if (error.Success()) {
759 if (pending_items_pointer.count > 0 &&
760 pending_items_pointer.items_buffer_size > 0 &&
761 pending_items_pointer.items_buffer_ptr != 0 &&
762 pending_items_pointer.items_buffer_ptr != LLDB_INVALID_ADDRESS) {
763 DataBufferHeap data(pending_items_pointer.items_buffer_size, 0);
764 if (m_process->ReadMemory(
765 pending_items_pointer.items_buffer_ptr, data.GetBytes(),
766 pending_items_pointer.items_buffer_size, error)) {
767 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
768 m_process->GetByteOrder(),
769 m_process->GetAddressByteSize());
770
771 // We either have an array of
772 // void* item_ref
773 // (old style) or we have a structure returned which looks like
774 //
775 // struct introspection_dispatch_pending_item_info_s {
776 // void *item_ref;
777 // void *function_or_block;
778 // };
779 //
780 // struct introspection_dispatch_pending_items_array_s {
781 // uint32_t version;
782 // uint32_t size_of_item_info;
783 // introspection_dispatch_pending_item_info_s items[];
784 // }
785
786 offset_t offset = 0;
787 int i = 0;
788 uint32_t version = extractor.GetU32(&offset);
789 if (version == 1) {
790 pending_item_refs.new_style = true;
791 uint32_t item_size = extractor.GetU32(&offset);
792 uint32_t start_of_array_offset = offset;
793 while (offset < pending_items_pointer.items_buffer_size &&
794 static_cast<size_t>(i) < pending_items_pointer.count) {
795 offset = start_of_array_offset + (i * item_size);
796 ItemRefAndCodeAddress item;
797 item.item_ref = extractor.GetPointer(&offset);
798 item.code_address = extractor.GetPointer(&offset);
799 pending_item_refs.item_refs_and_code_addresses.push_back(item);
800 i++;
801 }
802 } else {
803 offset = 0;
804 pending_item_refs.new_style = false;
805 while (offset < pending_items_pointer.items_buffer_size &&
806 static_cast<size_t>(i) < pending_items_pointer.count) {
807 ItemRefAndCodeAddress item;
808 item.item_ref = extractor.GetPointer(&offset);
809 item.code_address = LLDB_INVALID_ADDRESS;
810 pending_item_refs.item_refs_and_code_addresses.push_back(item);
811 i++;
812 }
813 }
Jason Molendae32cd192014-03-10 08:42:03 +0000814 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000815 m_page_to_free = pending_items_pointer.items_buffer_ptr;
816 m_page_to_free_size = pending_items_pointer.items_buffer_size;
817 }
Jason Molendae32cd192014-03-10 08:42:03 +0000818 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000819 }
820 return pending_item_refs;
Jason Molendae32cd192014-03-10 08:42:03 +0000821}
822
Kate Stoneb9c1b512016-09-06 20:57:50 +0000823void SystemRuntimeMacOSX::PopulatePendingItemsForQueue(Queue *queue) {
824 if (BacktraceRecordingHeadersInitialized()) {
825 PendingItemsForQueue pending_item_refs =
826 GetPendingItemRefsForQueue(queue->GetLibdispatchQueueAddress());
827 for (ItemRefAndCodeAddress pending_item :
828 pending_item_refs.item_refs_and_code_addresses) {
829 Address addr;
830 m_process->GetTarget().ResolveLoadAddress(pending_item.code_address,
831 addr);
832 QueueItemSP queue_item_sp(new QueueItem(queue->shared_from_this(),
833 m_process->shared_from_this(),
834 pending_item.item_ref, addr));
835 queue->PushPendingQueueItem(queue_item_sp);
Jason Molenda2fd83352014-02-05 05:44:54 +0000836 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000838}
839
Kate Stoneb9c1b512016-09-06 20:57:50 +0000840void SystemRuntimeMacOSX::CompleteQueueItem(QueueItem *queue_item,
841 addr_t item_ref) {
842 AppleGetItemInfoHandler::GetItemInfoReturnInfo ret;
Jason Molenda2fd83352014-02-05 05:44:54 +0000843
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 ThreadSP cur_thread_sp(
845 m_process->GetThreadList().GetExpressionExecutionThread());
Zachary Turner97206d52017-05-12 04:51:55 +0000846 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
848 m_page_to_free, m_page_to_free_size,
849 error);
850 m_page_to_free = LLDB_INVALID_ADDRESS;
851 m_page_to_free_size = 0;
852 if (ret.item_buffer_ptr != 0 && ret.item_buffer_ptr != LLDB_INVALID_ADDRESS &&
853 ret.item_buffer_size > 0) {
854 DataBufferHeap data(ret.item_buffer_size, 0);
855 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
856 ret.item_buffer_size, error) &&
857 error.Success()) {
858 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
859 m_process->GetByteOrder(),
860 m_process->GetAddressByteSize());
861 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
862 queue_item->SetItemThatEnqueuedThis(item.item_that_enqueued_this);
863 queue_item->SetEnqueueingThreadID(item.enqueuing_thread_id);
864 queue_item->SetEnqueueingQueueID(item.enqueuing_queue_serialnum);
865 queue_item->SetStopID(item.stop_id);
866 queue_item->SetEnqueueingBacktrace(item.enqueuing_callstack);
867 queue_item->SetThreadLabel(item.enqueuing_thread_label);
868 queue_item->SetQueueLabel(item.enqueuing_queue_label);
869 queue_item->SetTargetQueueLabel(item.target_queue_label);
870 }
871 m_page_to_free = ret.item_buffer_ptr;
872 m_page_to_free_size = ret.item_buffer_size;
873 }
874}
875
876void SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR(
877 lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count,
878 lldb_private::QueueList &queue_list) {
Zachary Turner97206d52017-05-12 04:51:55 +0000879 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880 DataBufferHeap data(queues_buffer_size, 0);
881 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
882 if (m_process->ReadMemory(queues_buffer, data.GetBytes(), queues_buffer_size,
883 error) == queues_buffer_size &&
884 error.Success()) {
885 // We've read the information out of inferior memory; free it on the next
886 // call we make
887 m_page_to_free = queues_buffer;
888 m_page_to_free_size = queues_buffer_size;
889
890 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
891 m_process->GetByteOrder(),
892 m_process->GetAddressByteSize());
Jason Molenda2fd83352014-02-05 05:44:54 +0000893 offset_t offset = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894 uint64_t queues_read = 0;
Jason Molenda2fd83352014-02-05 05:44:54 +0000895
Adrian Prantl05097242018-04-30 16:49:04 +0000896 // The information about the queues is stored in this format (v1): typedef
897 // struct introspection_dispatch_queue_info_s {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000898 // uint32_t offset_to_next;
899 // dispatch_queue_t queue;
900 // uint64_t serialnum; // queue's serialnum in the process, as
901 // provided by libdispatch
902 // uint32_t running_work_items_count;
903 // uint32_t pending_work_items_count;
904 //
905 // char data[]; // Starting here, we have variable-length data:
906 // // char queue_label[];
907 // } introspection_dispatch_queue_info_s;
Jason Molenda2fd83352014-02-05 05:44:54 +0000908
Kate Stoneb9c1b512016-09-06 20:57:50 +0000909 while (queues_read < count && offset < queues_buffer_size) {
910 offset_t start_of_this_item = offset;
911
912 uint32_t offset_to_next = extractor.GetU32(&offset);
913
914 offset += 4; // Skip over the 4 bytes of reserved space
915 addr_t queue = extractor.GetPointer(&offset);
916 uint64_t serialnum = extractor.GetU64(&offset);
917 uint32_t running_work_items_count = extractor.GetU32(&offset);
918 uint32_t pending_work_items_count = extractor.GetU32(&offset);
919
920 // Read the first field of the variable length data
921 offset = start_of_this_item +
922 m_lib_backtrace_recording_info.queue_info_data_offset;
923 const char *queue_label = extractor.GetCStr(&offset);
Konrad Kleine248a1302019-05-23 11:14:47 +0000924 if (queue_label == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 queue_label = "";
926
927 offset_t start_of_next_item = start_of_this_item + offset_to_next;
928 offset = start_of_next_item;
929
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000930 LLDB_LOGF(log,
931 "SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR added "
932 "queue with dispatch_queue_t 0x%" PRIx64
933 ", serial number 0x%" PRIx64
934 ", running items %d, pending items %d, name '%s'",
935 queue, serialnum, running_work_items_count,
936 pending_work_items_count, queue_label);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000937
938 QueueSP queue_sp(
939 new Queue(m_process->shared_from_this(), serialnum, queue_label));
940 queue_sp->SetNumRunningWorkItems(running_work_items_count);
941 queue_sp->SetNumPendingWorkItems(pending_work_items_count);
942 queue_sp->SetLibdispatchQueueAddress(queue);
943 queue_sp->SetKind(GetQueueKind(queue));
944 queue_list.AddQueue(queue_sp);
945 queues_read++;
Jason Molenda2fd83352014-02-05 05:44:54 +0000946 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000947 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000948}
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000949
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950SystemRuntimeMacOSX::ItemInfo SystemRuntimeMacOSX::ExtractItemInfoFromBuffer(
951 lldb_private::DataExtractor &extractor) {
952 ItemInfo item;
953
954 offset_t offset = 0;
955
956 item.item_that_enqueued_this = extractor.GetPointer(&offset);
957 item.function_or_block = extractor.GetPointer(&offset);
958 item.enqueuing_thread_id = extractor.GetU64(&offset);
959 item.enqueuing_queue_serialnum = extractor.GetU64(&offset);
960 item.target_queue_serialnum = extractor.GetU64(&offset);
961 item.enqueuing_callstack_frame_count = extractor.GetU32(&offset);
962 item.stop_id = extractor.GetU32(&offset);
963
964 offset = m_lib_backtrace_recording_info.item_info_data_offset;
965
966 for (uint32_t i = 0; i < item.enqueuing_callstack_frame_count; i++) {
967 item.enqueuing_callstack.push_back(extractor.GetPointer(&offset));
968 }
969 item.enqueuing_thread_label = extractor.GetCStr(&offset);
970 item.enqueuing_queue_label = extractor.GetCStr(&offset);
971 item.target_queue_label = extractor.GetCStr(&offset);
972
973 return item;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000974}
975
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976void SystemRuntimeMacOSX::Initialize() {
977 PluginManager::RegisterPlugin(GetPluginNameStatic(),
978 GetPluginDescriptionStatic(), CreateInstance);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000979}
980
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981void SystemRuntimeMacOSX::Terminate() {
982 PluginManager::UnregisterPlugin(CreateInstance);
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000983}
984
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985lldb_private::ConstString SystemRuntimeMacOSX::GetPluginNameStatic() {
986 static ConstString g_name("systemruntime-macosx");
987 return g_name;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000988}
989
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990const char *SystemRuntimeMacOSX::GetPluginDescriptionStatic() {
991 return "System runtime plugin for Mac OS X native libraries.";
992}
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000993
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000994// PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995lldb_private::ConstString SystemRuntimeMacOSX::GetPluginName() {
996 return GetPluginNameStatic();
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000997}
998
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999uint32_t SystemRuntimeMacOSX::GetPluginVersion() { return 1; }