blob: 53b9c83cce1083af710f9ca5aa582aa0d37783f5 [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
10
11#include "lldb/Breakpoint/StoppointCallbackContext.h"
12#include "lldb/Core/Log.h"
13#include "lldb/Core/Module.h"
14#include "lldb/Core/ModuleSpec.h"
15#include "lldb/Core/PluginManager.h"
16#include "lldb/Core/DataExtractor.h"
17#include "lldb/Core/DataBufferHeap.h"
18#include "lldb/Core/Section.h"
19#include "lldb/Expression/ClangFunction.h"
20#include "lldb/Expression/ClangUtilityFunction.h"
21#include "lldb/Host/FileSpec.h"
22#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Symbol/SymbolContext.h"
24#include "Plugins/Process/Utility/HistoryThread.h"
Jason Molenda5e8dce42013-12-13 00:29:16 +000025#include "lldb/Target/Queue.h"
26#include "lldb/Target/QueueList.h"
Jason Molendaa7b5afa2013-11-15 00:17:32 +000027#include "lldb/Target/Target.h"
28#include "lldb/Target/Thread.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000029#include "lldb/Target/Process.h"
30
Jason Molendaa7b5afa2013-11-15 00:17:32 +000031
Jason Molendaa7b5afa2013-11-15 00:17:32 +000032#include "SystemRuntimeMacOSX.h"
33
34using namespace lldb;
35using namespace lldb_private;
36
37//----------------------------------------------------------------------
38// Create an instance of this class. This function is filled into
39// the plugin info class that gets handed out by the plugin factory and
40// allows the lldb to instantiate an instance of this class.
41//----------------------------------------------------------------------
42SystemRuntime *
43SystemRuntimeMacOSX::CreateInstance (Process* process)
44{
45 bool create = false;
46 if (!create)
47 {
48 create = true;
49 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
50 if (exe_module)
51 {
52 ObjectFile *object_file = exe_module->GetObjectFile();
53 if (object_file)
54 {
55 create = (object_file->GetStrata() == ObjectFile::eStrataUser);
56 }
57 }
58
59 if (create)
60 {
61 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
62 switch (triple_ref.getOS())
63 {
64 case llvm::Triple::Darwin:
65 case llvm::Triple::MacOSX:
66 case llvm::Triple::IOS:
67 create = triple_ref.getVendor() == llvm::Triple::Apple;
68 break;
69 default:
70 create = false;
71 break;
72 }
73 }
74 }
75
76 if (create)
77 return new SystemRuntimeMacOSX (process);
78 return NULL;
79}
80
81//----------------------------------------------------------------------
82// Constructor
83//----------------------------------------------------------------------
84SystemRuntimeMacOSX::SystemRuntimeMacOSX (Process* process) :
85 SystemRuntime(process),
86 m_break_id(LLDB_INVALID_BREAK_ID),
Jason Molenda2fd83352014-02-05 05:44:54 +000087 m_mutex(Mutex::eMutexTypeRecursive),
88 m_get_queues_handler(process),
89 m_get_pending_items_handler(process),
90 m_get_item_info_handler(process),
91 m_get_thread_item_info_handler(process),
92 m_page_to_free(LLDB_INVALID_ADDRESS),
93 m_page_to_free_size(0),
94 m_lib_backtrace_recording_info(),
95 m_dispatch_queue_offsets_addr (LLDB_INVALID_ADDRESS),
96 m_libdispatch_offsets()
Jason Molendaa7b5afa2013-11-15 00:17:32 +000097{
Jason Molendaa7b5afa2013-11-15 00:17:32 +000098}
99
100//----------------------------------------------------------------------
101// Destructor
102//----------------------------------------------------------------------
103SystemRuntimeMacOSX::~SystemRuntimeMacOSX()
104{
105 Clear (true);
106}
107
Jason Molenda2fd83352014-02-05 05:44:54 +0000108void
109SystemRuntimeMacOSX::Detach ()
110{
111 m_get_queues_handler.Detach();
112 m_get_pending_items_handler.Detach();
113 m_get_item_info_handler.Detach();
114 m_get_thread_item_info_handler.Detach();
115}
116
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000117//----------------------------------------------------------------------
118// Clear out the state of this class.
119//----------------------------------------------------------------------
120void
121SystemRuntimeMacOSX::Clear (bool clear_process)
122{
123 Mutex::Locker locker(m_mutex);
124
125 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
126 m_process->ClearBreakpointSiteByID(m_break_id);
127
128 if (clear_process)
129 m_process = NULL;
130 m_break_id = LLDB_INVALID_BREAK_ID;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000131}
132
133
Jason Molenda2fd83352014-02-05 05:44:54 +0000134std::string
135SystemRuntimeMacOSX::GetQueueNameFromThreadQAddress (addr_t dispatch_qaddr)
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000136{
Jason Molenda2fd83352014-02-05 05:44:54 +0000137 std::string dispatch_queue_name;
138 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
139 return "";
140
141 ReadLibdispatchOffsets ();
142 if (m_libdispatch_offsets.IsValid ())
143 {
144 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a thread -
145 // deref it to get the address of the dispatch_queue_t structure for this thread's
146 // queue.
147 Error error;
148 addr_t dispatch_queue_addr = m_process->ReadPointerFromMemory (dispatch_qaddr, error);
149 if (error.Success())
150 {
151 if (m_libdispatch_offsets.dqo_version >= 4)
152 {
153 // libdispatch versions 4+, pointer to dispatch name is in the
154 // queue structure.
155 addr_t pointer_to_label_address = dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
156 addr_t label_addr = m_process->ReadPointerFromMemory (pointer_to_label_address, error);
157 if (error.Success())
158 {
159 m_process->ReadCStringFromMemory (label_addr, dispatch_queue_name, error);
160 }
161 }
162 else
163 {
164 // libdispatch versions 1-3, dispatch name is a fixed width char array
165 // in the queue structure.
166 addr_t label_addr = dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
167 dispatch_queue_name.resize (m_libdispatch_offsets.dqo_label_size, '\0');
168 size_t bytes_read = m_process->ReadMemory (label_addr, &dispatch_queue_name[0], m_libdispatch_offsets.dqo_label_size, error);
169 if (bytes_read < m_libdispatch_offsets.dqo_label_size)
170 dispatch_queue_name.erase (bytes_read);
171 }
172 }
173 }
174 return dispatch_queue_name;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000175}
176
Jason Molenda2fd83352014-02-05 05:44:54 +0000177lldb::queue_id_t
178SystemRuntimeMacOSX::GetQueueIDFromThreadQAddress (lldb::addr_t dispatch_qaddr)
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000179{
Jason Molenda2fd83352014-02-05 05:44:54 +0000180 queue_id_t queue_id = LLDB_INVALID_QUEUE_ID;
181
182 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
183 return queue_id;
184
185 ReadLibdispatchOffsets ();
186 if (m_libdispatch_offsets.IsValid ())
187 {
188 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a thread -
189 // deref it to get the address of the dispatch_queue_t structure for this thread's
190 // queue.
191 Error error;
192 uint64_t dispatch_queue_addr = m_process->ReadPointerFromMemory (dispatch_qaddr, error);
193 if (error.Success())
194 {
195 addr_t serialnum_address = dispatch_queue_addr + m_libdispatch_offsets.dqo_serialnum;
196 queue_id_t serialnum = m_process->ReadUnsignedIntegerFromMemory (serialnum_address, m_libdispatch_offsets.dqo_serialnum_size, LLDB_INVALID_QUEUE_ID, error);
197 if (error.Success())
198 {
199 queue_id = serialnum;
200 }
201 }
202 }
203
204 return queue_id;
205}
206
207
208void
209SystemRuntimeMacOSX::ReadLibdispatchOffsetsAddress ()
210{
211 if (m_dispatch_queue_offsets_addr != LLDB_INVALID_ADDRESS)
212 return;
213
214 static ConstString g_dispatch_queue_offsets_symbol_name ("dispatch_queue_offsets");
215 const Symbol *dispatch_queue_offsets_symbol = NULL;
216
217 // libdispatch symbols were in libSystem.B.dylib up through Mac OS X 10.6 ("Snow Leopard")
218 ModuleSpec libSystem_module_spec (FileSpec("libSystem.B.dylib", false));
219 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule (libSystem_module_spec));
220 if (module_sp)
221 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
222
223 // libdispatch symbols are in their own dylib as of Mac OS X 10.7 ("Lion") and later
224 if (dispatch_queue_offsets_symbol == NULL)
225 {
226 ModuleSpec libdispatch_module_spec (FileSpec("libdispatch.dylib", false));
227 module_sp = m_process->GetTarget().GetImages().FindFirstModule (libdispatch_module_spec);
228 if (module_sp)
229 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
230 }
231 if (dispatch_queue_offsets_symbol)
232 m_dispatch_queue_offsets_addr = dispatch_queue_offsets_symbol->GetAddress().GetLoadAddress(&m_process->GetTarget());
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000233}
234
235void
Jason Molenda2fd83352014-02-05 05:44:54 +0000236SystemRuntimeMacOSX::ReadLibdispatchOffsets ()
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000237{
Jason Molenda2fd83352014-02-05 05:44:54 +0000238 if (m_libdispatch_offsets.IsValid())
239 return;
240
241 ReadLibdispatchOffsetsAddress ();
242
243 uint8_t memory_buffer[sizeof (struct LibdispatchOffsets)];
244 DataExtractor data (memory_buffer,
245 sizeof(memory_buffer),
246 m_process->GetByteOrder(),
247 m_process->GetAddressByteSize());
248
249 Error error;
250 if (m_process->ReadMemory (m_dispatch_queue_offsets_addr, memory_buffer, sizeof(memory_buffer), error) == sizeof(memory_buffer))
251 {
252 lldb::offset_t data_offset = 0;
253
254 // The struct LibdispatchOffsets is a series of uint16_t's - extract them all
255 // in one big go.
256 data.GetU16 (&data_offset, &m_libdispatch_offsets.dqo_version, sizeof (struct LibdispatchOffsets) / sizeof (uint16_t));
257 }
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000258}
259
Jason Molenda2fd83352014-02-05 05:44:54 +0000260
261ThreadSP
262SystemRuntimeMacOSX::GetExtendedBacktraceThread (ThreadSP real_thread, ConstString type)
263{
264 ThreadSP originating_thread_sp;
265 if (BacktraceRecordingHeadersInitialized() && type == ConstString ("libdispatch"))
266 {
267 Error error;
268
269 // real_thread is either an actual, live thread (in which case we need to call into
270 // libBacktraceRecording to find its originator) or it is an extended backtrace itself,
271 // in which case we get the token from it and call into libBacktraceRecording to find
272 // the originator of that token.
273
274 if (real_thread->GetExtendedBacktraceToken() != LLDB_INVALID_ADDRESS)
275 {
276 originating_thread_sp = GetExtendedBacktraceFromItemRef (real_thread->GetExtendedBacktraceToken());
277 }
278 else
279 {
Jason Molendada276f92014-02-11 00:36:18 +0000280 ThreadSP cur_thread_sp (m_process->GetThreadList().GetSelectedThread());
281 AppleGetThreadItemInfoHandler::GetThreadItemInfoReturnInfo ret = m_get_thread_item_info_handler.GetThreadItemInfo (*cur_thread_sp.get(), real_thread->GetID(), m_page_to_free, m_page_to_free_size, error);
Jason Molenda2a6c2522014-02-15 00:20:40 +0000282 m_page_to_free = LLDB_INVALID_ADDRESS;
283 m_page_to_free_size = 0;
Jason Molenda2fd83352014-02-05 05:44:54 +0000284 if (ret.item_buffer_ptr != 0 && ret.item_buffer_ptr != LLDB_INVALID_ADDRESS && ret.item_buffer_size > 0)
285 {
286 DataBufferHeap data (ret.item_buffer_size, 0);
287 if (m_process->ReadMemory (ret.item_buffer_ptr, data.GetBytes(), ret.item_buffer_size, error) && error.Success())
288 {
289 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), m_process->GetByteOrder(), m_process->GetAddressByteSize());
290 ItemInfo item = ExtractItemInfoFromBuffer (extractor);
291 bool stop_id_is_valid = true;
292 if (item.stop_id == 0)
293 stop_id_is_valid = false;
294 originating_thread_sp.reset (new HistoryThread (*m_process,
295 item.enqueuing_thread_id,
296 item.enqueuing_callstack,
297 item.stop_id,
298 stop_id_is_valid));
299 originating_thread_sp->SetExtendedBacktraceToken (item.item_that_enqueued_this);
300 originating_thread_sp->SetQueueName (item.enqueuing_queue_label.c_str());
301 originating_thread_sp->SetQueueID (item.enqueuing_queue_serialnum);
302// originating_thread_sp->SetThreadName (item.enqueuing_thread_label.c_str());
303 }
Jason Molendada276f92014-02-11 00:36:18 +0000304 m_page_to_free = ret.item_buffer_ptr;
305 m_page_to_free_size = ret.item_buffer_size;
Jason Molenda2fd83352014-02-05 05:44:54 +0000306 }
307 }
308 }
309 return originating_thread_sp;
310}
311
312ThreadSP
313SystemRuntimeMacOSX::GetExtendedBacktraceFromItemRef (lldb::addr_t item_ref)
314{
315 ThreadSP return_thread_sp;
316
317 AppleGetItemInfoHandler::GetItemInfoReturnInfo ret;
318 ThreadSP cur_thread_sp (m_process->GetThreadList().GetSelectedThread());
319 Error error;
320 ret = m_get_item_info_handler.GetItemInfo (*cur_thread_sp.get(), item_ref, m_page_to_free, m_page_to_free_size, error);
Jason Molenda2a6c2522014-02-15 00:20:40 +0000321 m_page_to_free = LLDB_INVALID_ADDRESS;
322 m_page_to_free_size = 0;
Jason Molenda2fd83352014-02-05 05:44:54 +0000323 if (ret.item_buffer_ptr != 0 && ret.item_buffer_ptr != LLDB_INVALID_ADDRESS && ret.item_buffer_size > 0)
324 {
325 DataBufferHeap data (ret.item_buffer_size, 0);
326 if (m_process->ReadMemory (ret.item_buffer_ptr, data.GetBytes(), ret.item_buffer_size, error) && error.Success())
327 {
328 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), m_process->GetByteOrder(), m_process->GetAddressByteSize());
329 ItemInfo item = ExtractItemInfoFromBuffer (extractor);
330 bool stop_id_is_valid = true;
331 if (item.stop_id == 0)
332 stop_id_is_valid = false;
333 return_thread_sp.reset (new HistoryThread (*m_process,
334 item.enqueuing_thread_id,
335 item.enqueuing_callstack,
336 item.stop_id,
337 stop_id_is_valid));
338 return_thread_sp->SetExtendedBacktraceToken (item.item_that_enqueued_this);
339 return_thread_sp->SetQueueName (item.enqueuing_queue_label.c_str());
340 return_thread_sp->SetQueueID (item.enqueuing_queue_serialnum);
341// return_thread_sp->SetThreadName (item.enqueuing_thread_label.c_str());
342
Jason Molendada276f92014-02-11 00:36:18 +0000343 m_page_to_free = ret.item_buffer_ptr;
344 m_page_to_free_size = ret.item_buffer_size;
Jason Molenda2fd83352014-02-05 05:44:54 +0000345 }
346 }
347 return return_thread_sp;
348}
349
350ThreadSP
351SystemRuntimeMacOSX::GetExtendedBacktraceForQueueItem (QueueItemSP queue_item_sp, ConstString type)
352{
353 ThreadSP extended_thread_sp;
354 if (type != ConstString("libdispatch"))
355 return extended_thread_sp;
356
357 bool stop_id_is_valid = true;
358 if (queue_item_sp->GetStopID() == 0)
359 stop_id_is_valid = false;
360
361 extended_thread_sp.reset (new HistoryThread (*m_process,
362 queue_item_sp->GetEnqueueingThreadID(),
363 queue_item_sp->GetEnqueueingBacktrace(),
364 queue_item_sp->GetStopID(),
365 stop_id_is_valid));
366 extended_thread_sp->SetExtendedBacktraceToken (queue_item_sp->GetItemThatEnqueuedThis());
367 extended_thread_sp->SetQueueName (queue_item_sp->GetQueueLabel().c_str());
368 extended_thread_sp->SetQueueID (queue_item_sp->GetEnqueueingQueueID());
369// extended_thread_sp->SetThreadName (queue_item_sp->GetThreadLabel().c_str());
370
371 return extended_thread_sp;
372}
373
374/* Returns true if we were able to get the version / offset information
375 * out of libBacktraceRecording. false means we were unable to retrieve
376 * this; the queue_info_version field will be 0.
377 */
378
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000379bool
Jason Molenda2fd83352014-02-05 05:44:54 +0000380SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized ()
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000381{
Jason Molenda2fd83352014-02-05 05:44:54 +0000382 if (m_lib_backtrace_recording_info.queue_info_version != 0)
383 return true;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000384
Jason Molenda2fd83352014-02-05 05:44:54 +0000385 addr_t queue_info_version_address = LLDB_INVALID_ADDRESS;
386 addr_t queue_info_data_offset_address = LLDB_INVALID_ADDRESS;
387 addr_t item_info_version_address = LLDB_INVALID_ADDRESS;
388 addr_t item_info_data_offset_address = LLDB_INVALID_ADDRESS;
389 Target &target = m_process->GetTarget();
390
391
392 static ConstString introspection_dispatch_queue_info_version ("__introspection_dispatch_queue_info_version");
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000393 SymbolContextList sc_list;
Jason Molenda2fd83352014-02-05 05:44:54 +0000394 if (m_process->GetTarget().GetImages().FindSymbolsWithNameAndType (introspection_dispatch_queue_info_version, eSymbolTypeData, sc_list) > 0)
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000395 {
396 SymbolContext sc;
397 sc_list.GetContextAtIndex (0, sc);
398 AddressRange addr_range;
399 sc.GetAddressRange (eSymbolContextSymbol, 0, false, addr_range);
Jason Molenda2fd83352014-02-05 05:44:54 +0000400 queue_info_version_address = addr_range.GetBaseAddress().GetLoadAddress(&target);
401 }
402 sc_list.Clear();
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000403
Jason Molenda2fd83352014-02-05 05:44:54 +0000404 static ConstString introspection_dispatch_queue_info_data_offset ("__introspection_dispatch_queue_info_data_offset");
405 if (m_process->GetTarget().GetImages().FindSymbolsWithNameAndType (introspection_dispatch_queue_info_data_offset, eSymbolTypeData, sc_list) > 0)
406 {
407 SymbolContext sc;
408 sc_list.GetContextAtIndex (0, sc);
409 AddressRange addr_range;
410 sc.GetAddressRange (eSymbolContextSymbol, 0, false, addr_range);
411 queue_info_data_offset_address = addr_range.GetBaseAddress().GetLoadAddress(&target);
412 }
413 sc_list.Clear();
414
415 static ConstString introspection_dispatch_item_info_version ("__introspection_dispatch_item_info_version");
416 if (m_process->GetTarget().GetImages().FindSymbolsWithNameAndType (introspection_dispatch_item_info_version, eSymbolTypeData, sc_list) > 0)
417 {
418 SymbolContext sc;
419 sc_list.GetContextAtIndex (0, sc);
420 AddressRange addr_range;
421 sc.GetAddressRange (eSymbolContextSymbol, 0, false, addr_range);
422 item_info_version_address = addr_range.GetBaseAddress().GetLoadAddress(&target);
423 }
424 sc_list.Clear();
425
426 static ConstString introspection_dispatch_item_info_data_offset ("__introspection_dispatch_item_info_data_offset");
427 if (m_process->GetTarget().GetImages().FindSymbolsWithNameAndType (introspection_dispatch_item_info_data_offset, eSymbolTypeData, sc_list) > 0)
428 {
429 SymbolContext sc;
430 sc_list.GetContextAtIndex (0, sc);
431 AddressRange addr_range;
432 sc.GetAddressRange (eSymbolContextSymbol, 0, false, addr_range);
433 item_info_data_offset_address = addr_range.GetBaseAddress().GetLoadAddress(&target);
434 }
435
436 if (queue_info_version_address != LLDB_INVALID_ADDRESS
437 && queue_info_data_offset_address != LLDB_INVALID_ADDRESS
438 && item_info_version_address != LLDB_INVALID_ADDRESS
439 && item_info_data_offset_address != LLDB_INVALID_ADDRESS)
440 {
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000441 Error error;
Jason Molenda2fd83352014-02-05 05:44:54 +0000442 m_lib_backtrace_recording_info.queue_info_version = m_process->ReadUnsignedIntegerFromMemory (queue_info_version_address, 2, 0, error);
443 if (error.Success())
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000444 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000445 m_lib_backtrace_recording_info.queue_info_data_offset = m_process->ReadUnsignedIntegerFromMemory (queue_info_data_offset_address, 2, 0, error);
446 if (error.Success())
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000447 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000448 m_lib_backtrace_recording_info.item_info_version = m_process->ReadUnsignedIntegerFromMemory (item_info_version_address, 2, 0, error);
449 if (error.Success())
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000450 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000451 m_lib_backtrace_recording_info.item_info_data_offset = m_process->ReadUnsignedIntegerFromMemory (item_info_data_offset_address, 2, 0, error);
452 if (!error.Success())
Jason Molendaa6e91302013-11-19 05:44:41 +0000453 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000454 m_lib_backtrace_recording_info.queue_info_version = 0;
Jason Molendaa6e91302013-11-19 05:44:41 +0000455 }
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000456 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000457 else
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000458 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000459 m_lib_backtrace_recording_info.queue_info_version = 0;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000460 }
461 }
Jason Molenda2fd83352014-02-05 05:44:54 +0000462 else
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000463 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000464 m_lib_backtrace_recording_info.queue_info_version = 0;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000465 }
466 }
467 }
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000468
Jason Molenda2fd83352014-02-05 05:44:54 +0000469 return m_lib_backtrace_recording_info.queue_info_version != 0;
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000470}
471
472const std::vector<ConstString> &
473SystemRuntimeMacOSX::GetExtendedBacktraceTypes ()
474{
475 if (m_types.size () == 0)
476 {
477 m_types.push_back(ConstString("libdispatch"));
Jason Molenda2fd83352014-02-05 05:44:54 +0000478 // We could have pthread as another type in the future if we have a way of
479 // gathering that information & it's useful to distinguish between them.
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000480 }
481 return m_types;
482}
483
484void
Jason Molenda5e8dce42013-12-13 00:29:16 +0000485SystemRuntimeMacOSX::PopulateQueueList (lldb_private::QueueList &queue_list)
486{
Jason Molenda2fd83352014-02-05 05:44:54 +0000487 if (!BacktraceRecordingHeadersInitialized())
Jason Molenda5e8dce42013-12-13 00:29:16 +0000488 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000489 // We don't have libBacktraceRecording -- build the list of queues by looking at
490 // all extant threads, and the queues that they currently belong to.
491
492 for (ThreadSP thread_sp : m_process->Threads())
Jason Molenda5e8dce42013-12-13 00:29:16 +0000493 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000494 if (thread_sp->GetQueueID() != LLDB_INVALID_QUEUE_ID)
Jason Molenda5e8dce42013-12-13 00:29:16 +0000495 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000496 if (queue_list.FindQueueByID (thread_sp->GetQueueID()).get() == NULL)
497 {
498 QueueSP queue_sp (new Queue(m_process->shared_from_this(), thread_sp->GetQueueID(), thread_sp->GetQueueName()));
499 queue_list.AddQueue (queue_sp);
500 }
501 }
502 }
503 }
504 else
505 {
506 AppleGetQueuesHandler::GetQueuesReturnInfo queue_info_pointer;
507 ThreadSP cur_thread_sp (m_process->GetThreadList().GetSelectedThread());
508 if (cur_thread_sp)
509 {
510 Error error;
511 queue_info_pointer = m_get_queues_handler.GetCurrentQueues (*cur_thread_sp.get(), m_page_to_free, m_page_to_free_size, error);
Jason Molenda2a6c2522014-02-15 00:20:40 +0000512 m_page_to_free = LLDB_INVALID_ADDRESS;
513 m_page_to_free_size = 0;
Jason Molenda2fd83352014-02-05 05:44:54 +0000514 if (error.Success())
515 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000516
517 if (queue_info_pointer.count > 0
518 && queue_info_pointer.queues_buffer_size > 0
519 && queue_info_pointer.queues_buffer_ptr != 0
520 && queue_info_pointer.queues_buffer_ptr != LLDB_INVALID_ADDRESS)
521 {
522 PopulateQueuesUsingLibBTR (queue_info_pointer.queues_buffer_ptr, queue_info_pointer.queues_buffer_size, queue_info_pointer.count, queue_list);
523 }
Jason Molenda5e8dce42013-12-13 00:29:16 +0000524 }
525 }
526 }
527}
528
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000529// Returns either an array of introspection_dispatch_item_info_ref's for the pending items on
530// a queue or an array introspection_dispatch_item_info_ref's and code addresses for the
531// pending items on a queue. The information about each of these pending items then needs to
532// be fetched individually by passing the ref to libBacktraceRecording.
533
534SystemRuntimeMacOSX::PendingItemsForQueue
535SystemRuntimeMacOSX::GetPendingItemRefsForQueue (lldb::addr_t queue)
536{
537 PendingItemsForQueue pending_item_refs;
538 AppleGetPendingItemsHandler::GetPendingItemsReturnInfo pending_items_pointer;
539 ThreadSP cur_thread_sp (m_process->GetThreadList().GetSelectedThread());
540 if (cur_thread_sp)
541 {
542 Error error;
543 pending_items_pointer = m_get_pending_items_handler.GetPendingItems (*cur_thread_sp.get(), queue, m_page_to_free, m_page_to_free_size, error);
544 m_page_to_free = LLDB_INVALID_ADDRESS;
545 m_page_to_free_size = 0;
546 if (error.Success())
547 {
548 if (pending_items_pointer.count > 0
549 && pending_items_pointer.items_buffer_size > 0
550 && pending_items_pointer.items_buffer_ptr != 0
551 && pending_items_pointer.items_buffer_ptr != LLDB_INVALID_ADDRESS)
552 {
553 DataBufferHeap data (pending_items_pointer.items_buffer_size, 0);
554 if (m_process->ReadMemory (pending_items_pointer.items_buffer_ptr, data.GetBytes(), pending_items_pointer.items_buffer_size, error))
555 {
556 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), m_process->GetByteOrder(), m_process->GetAddressByteSize());
557
558 // We either have an array of
559 // void* item_ref
560 // (old style) or we have a structure returned which looks like
561 //
562 // struct introspection_dispatch_pending_item_info_s {
563 // void *item_ref;
564 // void *function_or_block;
565 // };
566 //
567 // struct introspection_dispatch_pending_items_array_s {
568 // uint32_t version;
569 // uint32_t size_of_item_info;
570 // introspection_dispatch_pending_item_info_s items[];
571 // }
572
573 offset_t offset = 0;
574 int i = 0;
575 uint32_t version = extractor.GetU32(&offset);
576 if (version == 1)
577 {
578 pending_item_refs.new_style = true;
579 uint32_t item_size = extractor.GetU32(&offset);
580 uint32_t start_of_array_offset = offset;
581 while (offset < pending_items_pointer.items_buffer_size && i < pending_items_pointer.count)
582 {
583 offset = start_of_array_offset + (i * item_size);
584 ItemRefAndCodeAddress item;
585 item.item_ref = extractor.GetPointer (&offset);
586 item.code_address = extractor.GetPointer (&offset);
587 pending_item_refs.item_refs_and_code_addresses.push_back (item);
588 i++;
589 }
590 }
591 else
592 {
593 offset = 0;
594 pending_item_refs.new_style = false;
595 while (offset < pending_items_pointer.items_buffer_size && i < pending_items_pointer.count)
596 {
597 ItemRefAndCodeAddress item;
598 item.item_ref = extractor.GetPointer (&offset);
599 item.code_address = LLDB_INVALID_ADDRESS;
600 pending_item_refs.item_refs_and_code_addresses.push_back (item);
601 i++;
602 }
603 }
604 }
605 m_page_to_free = pending_items_pointer.items_buffer_ptr;
606 m_page_to_free_size = pending_items_pointer.items_buffer_size;
607 }
608 }
609 }
610 return pending_item_refs;
611}
612
613
614
Jason Molenda2fd83352014-02-05 05:44:54 +0000615void
616SystemRuntimeMacOSX::PopulatePendingItemsForQueue (Queue *queue)
617{
618 if (BacktraceRecordingHeadersInitialized())
619 {
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000620 PendingItemsForQueue pending_item_refs = GetPendingItemRefsForQueue (queue->GetLibdispatchQueueAddress());
621 for (ItemRefAndCodeAddress pending_item : pending_item_refs.item_refs_and_code_addresses)
Jason Molenda2fd83352014-02-05 05:44:54 +0000622 {
623 AppleGetItemInfoHandler::GetItemInfoReturnInfo ret;
624 ThreadSP cur_thread_sp (m_process->GetThreadList().GetSelectedThread());
625 Error error;
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000626 ret = m_get_item_info_handler.GetItemInfo (*cur_thread_sp.get(), pending_item.item_ref, m_page_to_free, m_page_to_free_size, error);
Jason Molenda2a6c2522014-02-15 00:20:40 +0000627 m_page_to_free = LLDB_INVALID_ADDRESS;
628 m_page_to_free_size = 0;
Jason Molenda2fd83352014-02-05 05:44:54 +0000629 if (ret.item_buffer_ptr != 0 && ret.item_buffer_ptr != LLDB_INVALID_ADDRESS && ret.item_buffer_size > 0)
630 {
631 DataBufferHeap data (ret.item_buffer_size, 0);
632 if (m_process->ReadMemory (ret.item_buffer_ptr, data.GetBytes(), ret.item_buffer_size, error) && error.Success())
633 {
634 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), m_process->GetByteOrder(), m_process->GetAddressByteSize());
635 ItemInfo item = ExtractItemInfoFromBuffer (extractor);
Jason Molenda37e9b5a2014-03-09 21:17:08 +0000636 QueueItemSP queue_item_sp (new QueueItem (queue->shared_from_this(), pending_item.item_ref));
Jason Molenda2fd83352014-02-05 05:44:54 +0000637 queue_item_sp->SetItemThatEnqueuedThis (item.item_that_enqueued_this);
638
639 Address addr;
640 if (!m_process->GetTarget().ResolveLoadAddress (item.function_or_block, addr, item.stop_id))
641 {
642 m_process->GetTarget().ResolveLoadAddress (item.function_or_block, addr);
643 }
644 queue_item_sp->SetAddress (addr);
645 queue_item_sp->SetEnqueueingThreadID (item.enqueuing_thread_id);
646 queue_item_sp->SetTargetQueueID (item.enqueuing_thread_id);
647 queue_item_sp->SetStopID (item.stop_id);
648 queue_item_sp->SetEnqueueingBacktrace (item.enqueuing_callstack);
649 queue_item_sp->SetThreadLabel (item.enqueuing_thread_label);
650 queue_item_sp->SetQueueLabel (item.enqueuing_queue_label);
651 queue_item_sp->SetTargetQueueLabel (item.target_queue_label);
652
653 queue->PushPendingQueueItem (queue_item_sp);
654 }
Jason Molenda0d6a1ff2014-03-07 23:28:54 +0000655 m_page_to_free = ret.item_buffer_ptr;
656 m_page_to_free_size = ret.item_buffer_size;
Jason Molenda2fd83352014-02-05 05:44:54 +0000657 }
658 }
659 }
660}
661
Jason Molenda2fd83352014-02-05 05:44:54 +0000662void
663SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR (lldb::addr_t queues_buffer, uint64_t queues_buffer_size,
664 uint64_t count, lldb_private::QueueList &queue_list)
665{
666 Error error;
667 DataBufferHeap data (queues_buffer_size, 0);
Jason Molendad84f6062014-02-26 22:27:09 +0000668 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYSTEM_RUNTIME));
Jason Molenda2fd83352014-02-05 05:44:54 +0000669 if (m_process->ReadMemory (queues_buffer, data.GetBytes(), queues_buffer_size, error) == queues_buffer_size && error.Success())
670 {
671 // We've read the information out of inferior memory; free it on the next call we make
672 m_page_to_free = queues_buffer;
673 m_page_to_free_size = queues_buffer_size;
674
675 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), m_process->GetByteOrder(), m_process->GetAddressByteSize());
676 offset_t offset = 0;
677 uint64_t queues_read = 0;
678
679 // The information about the queues is stored in this format (v1):
680 // typedef struct introspection_dispatch_queue_info_s {
681 // uint32_t offset_to_next;
682 // dispatch_queue_t queue;
683 // uint64_t serialnum; // queue's serialnum in the process, as provided by libdispatch
684 // uint32_t running_work_items_count;
685 // uint32_t pending_work_items_count;
686 //
687 // char data[]; // Starting here, we have variable-length data:
688 // // char queue_label[];
689 // } introspection_dispatch_queue_info_s;
690
691 while (queues_read < count && offset < queues_buffer_size)
692 {
693 offset_t start_of_this_item = offset;
694
695 uint32_t offset_to_next = extractor.GetU32 (&offset);
Jason Molendad84f6062014-02-26 22:27:09 +0000696
697 offset += 4; // Skip over the 4 bytes of reserved space
Jason Molenda2fd83352014-02-05 05:44:54 +0000698 addr_t queue = extractor.GetPointer (&offset);
699 uint64_t serialnum = extractor.GetU64 (&offset);
700 uint32_t running_work_items_count = extractor.GetU32 (&offset);
701 uint32_t pending_work_items_count = extractor.GetU32 (&offset);
702
703 // Read the first field of the variable length data
704 offset = start_of_this_item + m_lib_backtrace_recording_info.queue_info_data_offset;
705 const char *queue_label = extractor.GetCStr (&offset);
706 if (queue_label == NULL)
707 queue_label = "";
708
709 offset_t start_of_next_item = start_of_this_item + offset_to_next;
710 offset = start_of_next_item;
711
Jason Molendad84f6062014-02-26 22:27:09 +0000712 if (log)
713 log->Printf ("SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR added queue with dispatch_queue_t 0x%" PRIx64 ", serial number 0x%" PRIx64 ", running items %d, pending items %d, name '%s'", queue, serialnum, running_work_items_count, pending_work_items_count, queue_label);
714
Jason Molenda2fd83352014-02-05 05:44:54 +0000715 QueueSP queue_sp (new Queue (m_process->shared_from_this(), serialnum, queue_label));
716 queue_sp->SetNumRunningWorkItems (running_work_items_count);
717 queue_sp->SetNumPendingWorkItems (pending_work_items_count);
718 queue_sp->SetLibdispatchQueueAddress (queue);
719 queue_list.AddQueue (queue_sp);
720 queues_read++;
721 }
722 }
723}
724
725SystemRuntimeMacOSX::ItemInfo
726SystemRuntimeMacOSX::ExtractItemInfoFromBuffer (lldb_private::DataExtractor &extractor)
727{
728 ItemInfo item;
729
730 offset_t offset = 0;
731
732 item.item_that_enqueued_this = extractor.GetPointer (&offset);
733 item.function_or_block = extractor.GetPointer (&offset);
734 item.enqueuing_thread_id = extractor.GetU64 (&offset);
735 item.enqueuing_queue_serialnum = extractor.GetU64 (&offset);
736 item.target_queue_serialnum = extractor.GetU64 (&offset);
737 item.enqueuing_callstack_frame_count = extractor.GetU32 (&offset);
738 item.stop_id = extractor.GetU32 (&offset);
739
740 offset = m_lib_backtrace_recording_info.item_info_data_offset;
741
742 for (uint32_t i = 0; i < item.enqueuing_callstack_frame_count; i++)
743 {
744 item.enqueuing_callstack.push_back (extractor.GetPointer (&offset));
745 }
746 item.enqueuing_thread_label = extractor.GetCStr (&offset);
747 item.enqueuing_queue_label = extractor.GetCStr (&offset);
748 item.target_queue_label = extractor.GetCStr (&offset);
749
750 return item;
751}
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000752
753void
754SystemRuntimeMacOSX::Initialize()
755{
756 PluginManager::RegisterPlugin (GetPluginNameStatic(),
757 GetPluginDescriptionStatic(),
758 CreateInstance);
759}
760
761void
762SystemRuntimeMacOSX::Terminate()
763{
764 PluginManager::UnregisterPlugin (CreateInstance);
765}
766
767
768lldb_private::ConstString
769SystemRuntimeMacOSX::GetPluginNameStatic()
770{
771 static ConstString g_name("systemruntime-macosx");
772 return g_name;
773}
774
775const char *
776SystemRuntimeMacOSX::GetPluginDescriptionStatic()
777{
778 return "System runtime plugin for Mac OS X native libraries.";
779}
780
781
782//------------------------------------------------------------------
783// PluginInterface protocol
784//------------------------------------------------------------------
785lldb_private::ConstString
786SystemRuntimeMacOSX::GetPluginName()
787{
788 return GetPluginNameStatic();
789}
790
791uint32_t
792SystemRuntimeMacOSX::GetPluginVersion()
793{
794 return 1;
795}