blob: d4d60b3a05fc8dc73bdcc8b951ca28e3b92373de [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 Molenda2fd83352014-02-05 05:44:54 +0000529void
530SystemRuntimeMacOSX::PopulatePendingItemsForQueue (Queue *queue)
531{
532 if (BacktraceRecordingHeadersInitialized())
533 {
534 std::vector<addr_t> pending_item_refs = GetPendingItemRefsForQueue (queue->GetLibdispatchQueueAddress());
535 for (addr_t pending_item : pending_item_refs)
536 {
537 AppleGetItemInfoHandler::GetItemInfoReturnInfo ret;
538 ThreadSP cur_thread_sp (m_process->GetThreadList().GetSelectedThread());
539 Error error;
540 ret = m_get_item_info_handler.GetItemInfo (*cur_thread_sp.get(), pending_item, m_page_to_free, m_page_to_free_size, error);
Jason Molenda2a6c2522014-02-15 00:20:40 +0000541 m_page_to_free = LLDB_INVALID_ADDRESS;
542 m_page_to_free_size = 0;
Jason Molenda2fd83352014-02-05 05:44:54 +0000543 if (ret.item_buffer_ptr != 0 && ret.item_buffer_ptr != LLDB_INVALID_ADDRESS && ret.item_buffer_size > 0)
544 {
545 DataBufferHeap data (ret.item_buffer_size, 0);
546 if (m_process->ReadMemory (ret.item_buffer_ptr, data.GetBytes(), ret.item_buffer_size, error) && error.Success())
547 {
548 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), m_process->GetByteOrder(), m_process->GetAddressByteSize());
549 ItemInfo item = ExtractItemInfoFromBuffer (extractor);
550 QueueItemSP queue_item_sp (new QueueItem (queue->shared_from_this()));
551 queue_item_sp->SetItemThatEnqueuedThis (item.item_that_enqueued_this);
552
553 Address addr;
554 if (!m_process->GetTarget().ResolveLoadAddress (item.function_or_block, addr, item.stop_id))
555 {
556 m_process->GetTarget().ResolveLoadAddress (item.function_or_block, addr);
557 }
558 queue_item_sp->SetAddress (addr);
559 queue_item_sp->SetEnqueueingThreadID (item.enqueuing_thread_id);
560 queue_item_sp->SetTargetQueueID (item.enqueuing_thread_id);
561 queue_item_sp->SetStopID (item.stop_id);
562 queue_item_sp->SetEnqueueingBacktrace (item.enqueuing_callstack);
563 queue_item_sp->SetThreadLabel (item.enqueuing_thread_label);
564 queue_item_sp->SetQueueLabel (item.enqueuing_queue_label);
565 queue_item_sp->SetTargetQueueLabel (item.target_queue_label);
566
567 queue->PushPendingQueueItem (queue_item_sp);
568 }
Jason Molenda0d6a1ff2014-03-07 23:28:54 +0000569 m_page_to_free = ret.item_buffer_ptr;
570 m_page_to_free_size = ret.item_buffer_size;
Jason Molenda2fd83352014-02-05 05:44:54 +0000571 }
572 }
573 }
574}
575
576// Returns an array of introspection_dispatch_item_info_ref's for the pending items on
577// a queue. The information about each of these pending items then needs to be fetched
578// individually by passing the ref to libBacktraceRecording.
579
580std::vector<lldb::addr_t>
581SystemRuntimeMacOSX::GetPendingItemRefsForQueue (lldb::addr_t queue)
582{
583 std::vector<addr_t> pending_item_refs;
584 AppleGetPendingItemsHandler::GetPendingItemsReturnInfo pending_items_pointer;
585 ThreadSP cur_thread_sp (m_process->GetThreadList().GetSelectedThread());
586 if (cur_thread_sp)
587 {
588 Error error;
589 pending_items_pointer = m_get_pending_items_handler.GetPendingItems (*cur_thread_sp.get(), queue, m_page_to_free, m_page_to_free_size, error);
Jason Molenda2a6c2522014-02-15 00:20:40 +0000590 m_page_to_free = LLDB_INVALID_ADDRESS;
591 m_page_to_free_size = 0;
Jason Molenda2fd83352014-02-05 05:44:54 +0000592 if (error.Success())
593 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000594 if (pending_items_pointer.count > 0
595 && pending_items_pointer.items_buffer_size > 0
596 && pending_items_pointer.items_buffer_ptr != 0
597 && pending_items_pointer.items_buffer_ptr != LLDB_INVALID_ADDRESS)
598 {
599 DataBufferHeap data (pending_items_pointer.items_buffer_size, 0);
600 if (m_process->ReadMemory (pending_items_pointer.items_buffer_ptr, data.GetBytes(), pending_items_pointer.items_buffer_size, error))
601 {
602 offset_t offset = 0;
603 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), m_process->GetByteOrder(), m_process->GetAddressByteSize());
604 int i = 0;
605 while (offset < pending_items_pointer.items_buffer_size && i < pending_items_pointer.count)
606 {
607 pending_item_refs.push_back (extractor.GetPointer (&offset));
608 i++;
609 }
610 }
611 m_page_to_free = pending_items_pointer.items_buffer_ptr;
612 m_page_to_free_size = pending_items_pointer.items_buffer_size;
613 }
614 }
615 }
616 return pending_item_refs;
617}
618
619
620void
621SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR (lldb::addr_t queues_buffer, uint64_t queues_buffer_size,
622 uint64_t count, lldb_private::QueueList &queue_list)
623{
624 Error error;
625 DataBufferHeap data (queues_buffer_size, 0);
Jason Molendad84f6062014-02-26 22:27:09 +0000626 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYSTEM_RUNTIME));
Jason Molenda2fd83352014-02-05 05:44:54 +0000627 if (m_process->ReadMemory (queues_buffer, data.GetBytes(), queues_buffer_size, error) == queues_buffer_size && error.Success())
628 {
629 // We've read the information out of inferior memory; free it on the next call we make
630 m_page_to_free = queues_buffer;
631 m_page_to_free_size = queues_buffer_size;
632
633 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), m_process->GetByteOrder(), m_process->GetAddressByteSize());
634 offset_t offset = 0;
635 uint64_t queues_read = 0;
636
637 // The information about the queues is stored in this format (v1):
638 // typedef struct introspection_dispatch_queue_info_s {
639 // uint32_t offset_to_next;
640 // dispatch_queue_t queue;
641 // uint64_t serialnum; // queue's serialnum in the process, as provided by libdispatch
642 // uint32_t running_work_items_count;
643 // uint32_t pending_work_items_count;
644 //
645 // char data[]; // Starting here, we have variable-length data:
646 // // char queue_label[];
647 // } introspection_dispatch_queue_info_s;
648
649 while (queues_read < count && offset < queues_buffer_size)
650 {
651 offset_t start_of_this_item = offset;
652
653 uint32_t offset_to_next = extractor.GetU32 (&offset);
Jason Molendad84f6062014-02-26 22:27:09 +0000654
655 offset += 4; // Skip over the 4 bytes of reserved space
Jason Molenda2fd83352014-02-05 05:44:54 +0000656 addr_t queue = extractor.GetPointer (&offset);
657 uint64_t serialnum = extractor.GetU64 (&offset);
658 uint32_t running_work_items_count = extractor.GetU32 (&offset);
659 uint32_t pending_work_items_count = extractor.GetU32 (&offset);
660
661 // Read the first field of the variable length data
662 offset = start_of_this_item + m_lib_backtrace_recording_info.queue_info_data_offset;
663 const char *queue_label = extractor.GetCStr (&offset);
664 if (queue_label == NULL)
665 queue_label = "";
666
667 offset_t start_of_next_item = start_of_this_item + offset_to_next;
668 offset = start_of_next_item;
669
Jason Molendad84f6062014-02-26 22:27:09 +0000670 if (log)
671 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);
672
Jason Molenda2fd83352014-02-05 05:44:54 +0000673 QueueSP queue_sp (new Queue (m_process->shared_from_this(), serialnum, queue_label));
674 queue_sp->SetNumRunningWorkItems (running_work_items_count);
675 queue_sp->SetNumPendingWorkItems (pending_work_items_count);
676 queue_sp->SetLibdispatchQueueAddress (queue);
677 queue_list.AddQueue (queue_sp);
678 queues_read++;
679 }
680 }
681}
682
683SystemRuntimeMacOSX::ItemInfo
684SystemRuntimeMacOSX::ExtractItemInfoFromBuffer (lldb_private::DataExtractor &extractor)
685{
686 ItemInfo item;
687
688 offset_t offset = 0;
689
690 item.item_that_enqueued_this = extractor.GetPointer (&offset);
691 item.function_or_block = extractor.GetPointer (&offset);
692 item.enqueuing_thread_id = extractor.GetU64 (&offset);
693 item.enqueuing_queue_serialnum = extractor.GetU64 (&offset);
694 item.target_queue_serialnum = extractor.GetU64 (&offset);
695 item.enqueuing_callstack_frame_count = extractor.GetU32 (&offset);
696 item.stop_id = extractor.GetU32 (&offset);
697
698 offset = m_lib_backtrace_recording_info.item_info_data_offset;
699
700 for (uint32_t i = 0; i < item.enqueuing_callstack_frame_count; i++)
701 {
702 item.enqueuing_callstack.push_back (extractor.GetPointer (&offset));
703 }
704 item.enqueuing_thread_label = extractor.GetCStr (&offset);
705 item.enqueuing_queue_label = extractor.GetCStr (&offset);
706 item.target_queue_label = extractor.GetCStr (&offset);
707
708 return item;
709}
Jason Molendaa7b5afa2013-11-15 00:17:32 +0000710
711void
712SystemRuntimeMacOSX::Initialize()
713{
714 PluginManager::RegisterPlugin (GetPluginNameStatic(),
715 GetPluginDescriptionStatic(),
716 CreateInstance);
717}
718
719void
720SystemRuntimeMacOSX::Terminate()
721{
722 PluginManager::UnregisterPlugin (CreateInstance);
723}
724
725
726lldb_private::ConstString
727SystemRuntimeMacOSX::GetPluginNameStatic()
728{
729 static ConstString g_name("systemruntime-macosx");
730 return g_name;
731}
732
733const char *
734SystemRuntimeMacOSX::GetPluginDescriptionStatic()
735{
736 return "System runtime plugin for Mac OS X native libraries.";
737}
738
739
740//------------------------------------------------------------------
741// PluginInterface protocol
742//------------------------------------------------------------------
743lldb_private::ConstString
744SystemRuntimeMacOSX::GetPluginName()
745{
746 return GetPluginNameStatic();
747}
748
749uint32_t
750SystemRuntimeMacOSX::GetPluginVersion()
751{
752 return 1;
753}