LLDB mainline
SystemRuntimeMacOSX.cpp
Go to the documentation of this file.
1//===-- SystemRuntimeMacOSX.cpp -------------------------------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
12#include "lldb/Core/Module.h"
15#include "lldb/Core/Section.h"
18#include "lldb/Target/Process.h"
20#include "lldb/Target/Queue.h"
22#include "lldb/Target/Target.h"
23#include "lldb/Target/Thread.h"
28#include "lldb/Utility/Log.h"
30
31#include "llvm/Support/Error.h"
32
34#include "SystemRuntimeMacOSX.h"
35
36#include <memory>
37
38using namespace lldb;
39using namespace lldb_private;
40
42
43// Create an instance of this class. This function is filled into the plugin
44// info class that gets handed out by the plugin factory and allows the lldb to
45// instantiate an instance of this class.
47 bool create = false;
48 if (!create) {
49 create = true;
50 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
51 if (exe_module) {
52 ObjectFile *object_file = exe_module->GetObjectFile();
53 if (object_file) {
54 create = (object_file->GetStrata() == ObjectFile::eStrataUser);
55 }
56 }
57
58 if (create) {
59 const llvm::Triple &triple_ref =
60 process->GetTarget().GetArchitecture().GetTriple();
61 switch (triple_ref.getOS()) {
62 case llvm::Triple::Darwin:
63 case llvm::Triple::MacOSX:
64 case llvm::Triple::IOS:
65 case llvm::Triple::TvOS:
66 case llvm::Triple::WatchOS:
67 case llvm::Triple::BridgeOS:
68 case llvm::Triple::DriverKit:
69 case llvm::Triple::XROS:
70 create = triple_ref.getVendor() == llvm::Triple::Apple;
71 break;
72 default:
73 create = false;
74 break;
75 }
76 }
77 }
78
79 if (create)
80 return new SystemRuntimeMacOSX(process);
81 return nullptr;
82}
83
84// Constructor
101
102// Destructor
104
111
112// Clear out the state of this class.
113void SystemRuntimeMacOSX::Clear(bool clear_process) {
114 std::lock_guard<std::recursive_mutex> guard(m_mutex);
115
117 m_process->ClearBreakpointSiteByID(m_break_id);
118
119 if (clear_process)
120 m_process = nullptr;
122}
123
124std::string
126 std::string dispatch_queue_name;
127 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
128 return "";
129
131 if (m_libdispatch_offsets.IsValid()) {
132 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a
133 // thread - deref it to get the address of the dispatch_queue_t structure
134 // for this thread's queue.
135 llvm::Expected<lldb::addr_t> dispatch_queue_addr =
136 m_process->ReadPointerFromMemory(dispatch_qaddr);
137 if (!dispatch_queue_addr) {
138 llvm::consumeError(dispatch_queue_addr.takeError());
139 return dispatch_queue_name;
140 }
141 if (m_libdispatch_offsets.dqo_version >= 4) {
142 // libdispatch versions 4+, pointer to dispatch name is in the queue
143 // structure.
144 addr_t pointer_to_label_address =
145 *dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
146 llvm::Expected<lldb::addr_t> label_addr =
147 m_process->ReadPointerFromMemory(pointer_to_label_address);
148 if (label_addr) {
150 m_process->ReadCStringFromMemory(*label_addr, dispatch_queue_name,
151 error);
152 } else {
153 llvm::consumeError(label_addr.takeError());
154 }
155 } else {
156 // libdispatch versions 1-3, dispatch name is a fixed width char array
157 // in the queue structure.
158 addr_t label_addr =
159 *dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
160 dispatch_queue_name.resize(m_libdispatch_offsets.dqo_label_size, '\0');
162 size_t bytes_read =
163 m_process->ReadMemory(label_addr, &dispatch_queue_name[0],
164 m_libdispatch_offsets.dqo_label_size, error);
165 if (bytes_read < m_libdispatch_offsets.dqo_label_size)
166 dispatch_queue_name.erase(bytes_read);
167 }
168 }
169 return dispatch_queue_name;
170}
171
173 addr_t dispatch_qaddr) {
174 return llvm::expectedToOptional(
175 m_process->ReadPointerFromMemory(dispatch_qaddr))
176 .value_or(LLDB_INVALID_ADDRESS);
177}
178
180 if (dispatch_queue_addr == LLDB_INVALID_ADDRESS || dispatch_queue_addr == 0)
181 return eQueueKindUnknown;
182
185 if (m_libdispatch_offsets.IsValid() &&
186 m_libdispatch_offsets.dqo_version >= 4) {
188 uint64_t width = m_process->ReadUnsignedIntegerFromMemory(
189 dispatch_queue_addr + m_libdispatch_offsets.dqo_width,
190 m_libdispatch_offsets.dqo_width_size, 0, error);
191 if (error.Success()) {
192 if (width == 1) {
193 kind = eQueueKindSerial;
194 }
195 if (width > 1) {
197 }
198 }
199 }
200 return kind;
201}
202
205 StructuredData::Dictionary *dict = dict_sp->GetAsDictionary();
206 if (dict) {
208 if (m_libpthread_offsets.IsValid()) {
209 dict->AddIntegerItem("plo_pthread_tsd_base_offset",
210 m_libpthread_offsets.plo_pthread_tsd_base_offset);
211 dict->AddIntegerItem(
212 "plo_pthread_tsd_base_address_offset",
213 m_libpthread_offsets.plo_pthread_tsd_base_address_offset);
214 dict->AddIntegerItem("plo_pthread_tsd_entry_size",
215 m_libpthread_offsets.plo_pthread_tsd_entry_size);
216 }
217
219 if (m_libdispatch_tsd_indexes.IsValid()) {
220 dict->AddIntegerItem("dti_queue_index",
221 m_libdispatch_tsd_indexes.dti_queue_index);
222 dict->AddIntegerItem("dti_voucher_index",
223 m_libdispatch_tsd_indexes.dti_voucher_index);
224 dict->AddIntegerItem("dti_qos_class_index",
225 m_libdispatch_tsd_indexes.dti_qos_class_index);
226 }
227 }
228}
229
231 if (thread_sp && thread_sp->GetFrameWithConcreteFrameIndex(0)) {
232 const SymbolContext sym_ctx(
233 thread_sp->GetFrameWithConcreteFrameIndex(0)->GetSymbolContext(
234 eSymbolContextSymbol));
235 static ConstString g_select_symbol("__select");
236 if (sym_ctx.GetFunctionName() == g_select_symbol) {
237 return false;
238 }
239 }
240 return true;
241}
242
246
247 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
248 return queue_id;
249
251 if (m_libdispatch_offsets.IsValid()) {
252 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a
253 // thread - deref it to get the address of the dispatch_queue_t structure
254 // for this thread's queue.
256 llvm::Expected<lldb::addr_t> dispatch_queue_addr =
257 m_process->ReadPointerFromMemory(dispatch_qaddr);
258 if (!dispatch_queue_addr) {
259 llvm::consumeError(dispatch_queue_addr.takeError());
260 return queue_id;
261 }
262 addr_t serialnum_address =
263 *dispatch_queue_addr + m_libdispatch_offsets.dqo_serialnum;
264 queue_id_t serialnum = m_process->ReadUnsignedIntegerFromMemory(
265 serialnum_address, m_libdispatch_offsets.dqo_serialnum_size,
267 if (error.Success()) {
268 queue_id = serialnum;
269 }
270 }
271
272 return queue_id;
273}
274
277 return;
278
279 static ConstString g_dispatch_queue_offsets_symbol_name(
280 "dispatch_queue_offsets");
281 const Symbol *dispatch_queue_offsets_symbol = nullptr;
282
283 // libdispatch symbols were in libSystem.B.dylib up through Mac OS X 10.6
284 // ("Snow Leopard")
285 ModuleSpec libSystem_module_spec(FileSpec("libSystem.B.dylib"));
286 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
287 libSystem_module_spec));
288 if (module_sp)
289 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
290 g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
291
292 // libdispatch symbols are in their own dylib as of Mac OS X 10.7 ("Lion")
293 // and later
294 if (dispatch_queue_offsets_symbol == nullptr) {
295 ModuleSpec libdispatch_module_spec(FileSpec("libdispatch.dylib"));
296 module_sp = m_process->GetTarget().GetImages().FindFirstModule(
297 libdispatch_module_spec);
298 if (module_sp)
299 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
300 g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
301 }
302 if (dispatch_queue_offsets_symbol)
304 dispatch_queue_offsets_symbol->GetLoadAddress(&m_process->GetTarget());
305}
306
308 if (m_libdispatch_offsets.IsValid())
309 return;
310
312
313 uint8_t memory_buffer[sizeof(struct LibdispatchOffsets)];
314 DataExtractor data(memory_buffer, sizeof(memory_buffer),
315 m_process->GetByteOrder(),
316 m_process->GetAddressByteSize());
317
319 if (m_process->ReadMemory(m_dispatch_queue_offsets_addr, memory_buffer,
320 sizeof(memory_buffer),
321 error) == sizeof(memory_buffer)) {
322 lldb::offset_t data_offset = 0;
323
324 // The struct LibdispatchOffsets is a series of uint16_t's - extract them
325 // all in one big go.
326 data.GetU16(&data_offset, &m_libdispatch_offsets.dqo_version,
327 sizeof(struct LibdispatchOffsets) / sizeof(uint16_t));
328 }
329}
330
333 return;
334
335 static ConstString g_libpthread_layout_offsets_symbol_name(
336 "pthread_layout_offsets");
337 const Symbol *libpthread_layout_offsets_symbol = nullptr;
338
339 ModuleSpec libpthread_module_spec(FileSpec("libsystem_pthread.dylib"));
340 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
341 libpthread_module_spec));
342 if (module_sp) {
343 libpthread_layout_offsets_symbol =
344 module_sp->FindFirstSymbolWithNameAndType(
345 g_libpthread_layout_offsets_symbol_name, eSymbolTypeData);
346 if (libpthread_layout_offsets_symbol) {
348 libpthread_layout_offsets_symbol->GetLoadAddress(
349 &m_process->GetTarget());
350 }
351 }
352}
353
355 if (m_libpthread_offsets.IsValid())
356 return;
357
359
361 uint8_t memory_buffer[sizeof(struct LibpthreadOffsets)];
362 DataExtractor data(memory_buffer, sizeof(memory_buffer),
363 m_process->GetByteOrder(),
364 m_process->GetAddressByteSize());
366 if (m_process->ReadMemory(m_libpthread_layout_offsets_addr, memory_buffer,
367 sizeof(memory_buffer),
368 error) == sizeof(memory_buffer)) {
369 lldb::offset_t data_offset = 0;
370
371 // The struct LibpthreadOffsets is a series of uint16_t's - extract them
372 // all in one big go.
373 data.GetU16(&data_offset, &m_libpthread_offsets.plo_version,
374 sizeof(struct LibpthreadOffsets) / sizeof(uint16_t));
375 }
376 }
377}
378
381 return;
382
383 static ConstString g_libdispatch_tsd_indexes_symbol_name(
384 "dispatch_tsd_indexes");
385 const Symbol *libdispatch_tsd_indexes_symbol = nullptr;
386
387 ModuleSpec libpthread_module_spec(FileSpec("libdispatch.dylib"));
388 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
389 libpthread_module_spec));
390 if (module_sp) {
391 libdispatch_tsd_indexes_symbol = module_sp->FindFirstSymbolWithNameAndType(
392 g_libdispatch_tsd_indexes_symbol_name, eSymbolTypeData);
393 if (libdispatch_tsd_indexes_symbol) {
395 libdispatch_tsd_indexes_symbol->GetLoadAddress(
396 &m_process->GetTarget());
397 }
398 }
399}
400
402 if (m_libdispatch_tsd_indexes.IsValid())
403 return;
404
406
408
409// We don't need to check the version number right now, it will be at least 2,
410// but keep this code around to fetch just the version # for the future where
411// we need to fetch alternate versions of the struct.
412#if 0
413 uint16_t dti_version = 2;
414 Address dti_struct_addr;
415 if (m_process->GetTarget().ResolveLoadAddress (m_dispatch_tsd_indexes_addr, dti_struct_addr))
416 {
418 uint16_t version = m_process->GetTarget().ReadUnsignedIntegerFromMemory (dti_struct_addr, false, 2, UINT16_MAX, error);
419 if (error.Success() && dti_version != UINT16_MAX)
420 {
421 dti_version = version;
422 }
423 }
424#endif
425
426 TypeSystemClangSP scratch_ts_sp =
429 CompilerType uint16 =
430 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16);
431 CompilerType dispatch_tsd_indexes_s = scratch_ts_sp->CreateRecordType(
432 nullptr, OptionalClangModuleID(), "__lldb_dispatch_tsd_indexes_s",
433 llvm::to_underlying(clang::TagTypeKind::Struct),
435
437 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
438 "dti_version", uint16, 0);
439 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
440 "dti_queue_index", uint16, 0);
441 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
442 "dti_voucher_index", uint16, 0);
443 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
444 "dti_qos_class_index", uint16, 0);
446
448 dispatch_tsd_indexes_s);
449
450 m_libdispatch_tsd_indexes.dti_version =
451 struct_reader.GetField<uint16_t>("dti_version");
452 m_libdispatch_tsd_indexes.dti_queue_index =
453 struct_reader.GetField<uint16_t>("dti_queue_index");
454 m_libdispatch_tsd_indexes.dti_voucher_index =
455 struct_reader.GetField<uint16_t>("dti_voucher_index");
456 m_libdispatch_tsd_indexes.dti_qos_class_index =
457 struct_reader.GetField<uint16_t>("dti_qos_class_index");
458 }
459 }
460}
461
463 ConstString type) {
464 ThreadSP originating_thread_sp;
465 if (BacktraceRecordingHeadersInitialized() && type == "libdispatch") {
467
468 // real_thread is either an actual, live thread (in which case we need to
469 // call into libBacktraceRecording to find its originator) or it is an
470 // extended backtrace itself, in which case we get the token from it and
471 // call into libBacktraceRecording to find the originator of that token.
472
473 if (real_thread->GetExtendedBacktraceToken() != LLDB_INVALID_ADDRESS) {
474 originating_thread_sp = GetExtendedBacktraceFromItemRef(
475 real_thread->GetExtendedBacktraceToken());
476 } else {
477 ThreadSP cur_thread_sp(
478 m_process->GetThreadList().GetExpressionExecutionThread());
480 m_get_thread_item_info_handler.GetThreadItemInfo(
481 *cur_thread_sp.get(), real_thread->GetID(), m_page_to_free,
485 if (ret.item_buffer_ptr != 0 &&
487 ret.item_buffer_size > 0) {
488 DataBufferHeap data(ret.item_buffer_size, 0);
489 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
490 ret.item_buffer_size, error) &&
491 error.Success()) {
492 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
493 m_process->GetByteOrder(),
494 m_process->GetAddressByteSize());
495 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
496 originating_thread_sp = std::make_shared<HistoryThread>(
498 originating_thread_sp->SetExtendedBacktraceToken(
500 originating_thread_sp->SetQueueName(
501 item.enqueuing_queue_label.c_str());
502 originating_thread_sp->SetQueueID(item.enqueuing_queue_serialnum);
503 // originating_thread_sp->SetThreadName
504 // (item.enqueuing_thread_label.c_str());
505 }
508 }
509 }
510 } else if (type == "Application Specific Backtrace") {
511 StructuredData::ObjectSP thread_extended_sp =
512 real_thread->GetExtendedInfo();
513
514 if (!thread_extended_sp)
515 return {};
516
517 StructuredData::Array *thread_extended_info =
518 thread_extended_sp->GetAsArray();
519
520 if (!thread_extended_info || !thread_extended_info->GetSize())
521 return {};
522
523 std::vector<addr_t> app_specific_backtrace_pcs;
524
525 auto extract_frame_pc =
526 [&app_specific_backtrace_pcs](StructuredData::Object *obj) -> bool {
527 if (!obj)
528 return false;
529
531 if (!dict)
532 return false;
533
535 if (!dict->GetValueForKeyAsInteger("pc", pc))
536 return false;
537
538 app_specific_backtrace_pcs.push_back(pc);
539
540 return pc != LLDB_INVALID_ADDRESS;
541 };
542
543 if (!thread_extended_info->ForEach(extract_frame_pc))
544 return {};
545
546 originating_thread_sp = std::make_shared<HistoryThread>(
547 *m_process, real_thread->GetIndexID(), app_specific_backtrace_pcs,
549 originating_thread_sp->SetQueueName(type.AsCString(nullptr));
550 }
551 return originating_thread_sp;
552}
553
556 ThreadSP return_thread_sp;
557
559 ThreadSP cur_thread_sp(
560 m_process->GetThreadList().GetExpressionExecutionThread());
562 ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
564 error);
568 ret.item_buffer_size > 0) {
569 DataBufferHeap data(ret.item_buffer_size, 0);
570 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
571 ret.item_buffer_size, error) &&
572 error.Success()) {
573 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
574 m_process->GetByteOrder(),
575 m_process->GetAddressByteSize());
576 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
577 return_thread_sp = std::make_shared<HistoryThread>(
579 return_thread_sp->SetExtendedBacktraceToken(item.item_that_enqueued_this);
580 return_thread_sp->SetQueueName(item.enqueuing_queue_label.c_str());
581 return_thread_sp->SetQueueID(item.enqueuing_queue_serialnum);
582 // return_thread_sp->SetThreadName
583 // (item.enqueuing_thread_label.c_str());
584
587 }
588 }
589 return return_thread_sp;
590}
591
594 ConstString type) {
595 ThreadSP extended_thread_sp;
596 if (type != "libdispatch")
597 return extended_thread_sp;
598
599 extended_thread_sp = std::make_shared<HistoryThread>(
600 *m_process, queue_item_sp->GetEnqueueingThreadID(),
601 queue_item_sp->GetEnqueueingBacktrace());
602 extended_thread_sp->SetExtendedBacktraceToken(
603 queue_item_sp->GetItemThatEnqueuedThis());
604 extended_thread_sp->SetQueueName(queue_item_sp->GetQueueLabel().c_str());
605 extended_thread_sp->SetQueueID(queue_item_sp->GetEnqueueingQueueID());
606 // extended_thread_sp->SetThreadName
607 // (queue_item_sp->GetThreadLabel().c_str());
608
609 return extended_thread_sp;
610}
611
612/* Returns true if we were able to get the version / offset information
613 * out of libBacktraceRecording. false means we were unable to retrieve
614 * this; the queue_info_version field will be 0.
615 */
616
618 if (m_lib_backtrace_recording_info.queue_info_version != 0)
619 return true;
620
621 addr_t queue_info_version_address = LLDB_INVALID_ADDRESS;
622 addr_t queue_info_data_offset_address = LLDB_INVALID_ADDRESS;
623 addr_t item_info_version_address = LLDB_INVALID_ADDRESS;
624 addr_t item_info_data_offset_address = LLDB_INVALID_ADDRESS;
625 Target &target = m_process->GetTarget();
626
627 ModuleSpec lookup_spec(FileSpec("libBacktraceRecording.dylib"));
628 ModuleSP module_sp(target.GetImages().FindFirstModule(lookup_spec));
629 if (!module_sp)
630 return false;
631
632 static ConstString introspection_dispatch_queue_info_version(
633 "__introspection_dispatch_queue_info_version");
634 SymbolContextList sc_list;
635 module_sp->FindSymbolsWithNameAndType(
636 introspection_dispatch_queue_info_version, eSymbolTypeData, sc_list);
637 if (!sc_list.IsEmpty()) {
638 SymbolContext sc;
639 sc_list.GetContextAtIndex(0, sc);
641 queue_info_version_address = addr.GetLoadAddress(&target);
642 }
643 sc_list.Clear();
644
645 static ConstString introspection_dispatch_queue_info_data_offset(
646 "__introspection_dispatch_queue_info_data_offset");
647 module_sp->FindSymbolsWithNameAndType(
648 introspection_dispatch_queue_info_data_offset, eSymbolTypeData, sc_list);
649 if (!sc_list.IsEmpty()) {
650 SymbolContext sc;
651 sc_list.GetContextAtIndex(0, sc);
653 queue_info_data_offset_address = addr.GetLoadAddress(&target);
654 }
655 sc_list.Clear();
656
657 static ConstString introspection_dispatch_item_info_version(
658 "__introspection_dispatch_item_info_version");
659 module_sp->FindSymbolsWithNameAndType(
660 introspection_dispatch_item_info_version, eSymbolTypeData, sc_list);
661 if (!sc_list.IsEmpty()) {
662 SymbolContext sc;
663 sc_list.GetContextAtIndex(0, sc);
665 item_info_version_address = addr.GetLoadAddress(&target);
666 }
667 sc_list.Clear();
668
669 static ConstString introspection_dispatch_item_info_data_offset(
670 "__introspection_dispatch_item_info_data_offset");
671 module_sp->FindSymbolsWithNameAndType(
672 introspection_dispatch_item_info_data_offset, eSymbolTypeData, sc_list);
673 if (!sc_list.IsEmpty()) {
674 SymbolContext sc;
675 sc_list.GetContextAtIndex(0, sc);
677 item_info_data_offset_address = addr.GetLoadAddress(&target);
678 }
679
680 if (queue_info_version_address != LLDB_INVALID_ADDRESS &&
681 queue_info_data_offset_address != LLDB_INVALID_ADDRESS &&
682 item_info_version_address != LLDB_INVALID_ADDRESS &&
683 item_info_data_offset_address != LLDB_INVALID_ADDRESS) {
685 m_lib_backtrace_recording_info.queue_info_version =
686 m_process->ReadUnsignedIntegerFromMemory(queue_info_version_address, 2,
687 0, error);
688 if (error.Success()) {
689 m_lib_backtrace_recording_info.queue_info_data_offset =
690 m_process->ReadUnsignedIntegerFromMemory(
691 queue_info_data_offset_address, 2, 0, error);
692 if (error.Success()) {
693 m_lib_backtrace_recording_info.item_info_version =
694 m_process->ReadUnsignedIntegerFromMemory(item_info_version_address,
695 2, 0, error);
696 if (error.Success()) {
697 m_lib_backtrace_recording_info.item_info_data_offset =
698 m_process->ReadUnsignedIntegerFromMemory(
699 item_info_data_offset_address, 2, 0, error);
700 if (!error.Success()) {
701 m_lib_backtrace_recording_info.queue_info_version = 0;
702 }
703 } else {
704 m_lib_backtrace_recording_info.queue_info_version = 0;
705 }
706 } else {
707 m_lib_backtrace_recording_info.queue_info_version = 0;
708 }
709 }
710 }
711
712 return m_lib_backtrace_recording_info.queue_info_version != 0;
713}
714
715const std::vector<ConstString> &
717 if (m_types.size() == 0) {
718 m_types.push_back(ConstString("libdispatch"));
719 m_types.push_back(ConstString("Application Specific Backtrace"));
720 // We could have pthread as another type in the future if we have a way of
721 // gathering that information & it's useful to distinguish between them.
722 }
723 return m_types;
724}
725
727 lldb_private::QueueList &queue_list) {
730 ThreadSP cur_thread_sp(
731 m_process->GetThreadList().GetExpressionExecutionThread());
732 if (cur_thread_sp) {
734 queue_info_pointer = m_get_queues_handler.GetCurrentQueues(
735 *cur_thread_sp.get(), m_page_to_free, m_page_to_free_size, error);
738 if (error.Success()) {
739
740 if (queue_info_pointer.count > 0 &&
741 queue_info_pointer.queues_buffer_size > 0 &&
742 queue_info_pointer.queues_buffer_ptr != 0 &&
743 queue_info_pointer.queues_buffer_ptr != LLDB_INVALID_ADDRESS) {
745 queue_info_pointer.queues_buffer_size,
746 queue_info_pointer.count, queue_list);
747 }
748 }
749 }
750 }
751
752 // We either didn't have libBacktraceRecording (and need to create the queues
753 // list based on threads) or we did get the queues list from
754 // libBacktraceRecording but some special queues may not be included in its
755 // information. This is needed because libBacktraceRecording will only list
756 // queues with pending or running items by default - but the magic com.apple
757 // .main-thread queue on thread 1 is always around.
758
759 for (ThreadSP thread_sp : m_process->Threads()) {
760 if (thread_sp->GetAssociatedWithLibdispatchQueue() != eLazyBoolNo) {
761 if (thread_sp->GetQueueID() != LLDB_INVALID_QUEUE_ID) {
762 if (queue_list.FindQueueByID(thread_sp->GetQueueID()).get() ==
763 nullptr) {
764 QueueSP queue_sp(new Queue(m_process->shared_from_this(),
765 thread_sp->GetQueueID(),
766 thread_sp->GetQueueName()));
767 if (thread_sp->ThreadHasQueueInformation()) {
768 queue_sp->SetKind(thread_sp->GetQueueKind());
769 queue_sp->SetLibdispatchQueueAddress(
770 thread_sp->GetQueueLibdispatchQueueAddress());
771 queue_list.AddQueue(queue_sp);
772 } else {
773 queue_sp->SetKind(
774 GetQueueKind(thread_sp->GetQueueLibdispatchQueueAddress()));
775 queue_sp->SetLibdispatchQueueAddress(
776 thread_sp->GetQueueLibdispatchQueueAddress());
777 queue_list.AddQueue(queue_sp);
778 }
779 }
780 }
781 }
782 }
783}
784
785// Returns either an array of introspection_dispatch_item_info_ref's for the
786// pending items on a queue or an array introspection_dispatch_item_info_ref's
787// and code addresses for the pending items on a queue. The information about
788// each of these pending items then needs to be fetched individually by passing
789// the ref to libBacktraceRecording.
790
793 PendingItemsForQueue pending_item_refs = {};
795 ThreadSP cur_thread_sp(
796 m_process->GetThreadList().GetExpressionExecutionThread());
797 if (cur_thread_sp) {
799 pending_items_pointer = m_get_pending_items_handler.GetPendingItems(
800 *cur_thread_sp.get(), queue, m_page_to_free, m_page_to_free_size,
801 error);
804 if (error.Success()) {
805 if (pending_items_pointer.count > 0 &&
806 pending_items_pointer.items_buffer_size > 0 &&
807 pending_items_pointer.items_buffer_ptr != 0 &&
808 pending_items_pointer.items_buffer_ptr != LLDB_INVALID_ADDRESS) {
809 DataBufferHeap data(pending_items_pointer.items_buffer_size, 0);
810 if (m_process->ReadMemory(
811 pending_items_pointer.items_buffer_ptr, data.GetBytes(),
812 pending_items_pointer.items_buffer_size, error)) {
813 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
814 m_process->GetByteOrder(),
815 m_process->GetAddressByteSize());
816
817 // We either have an array of
818 // void* item_ref
819 // (old style) or we have a structure returned which looks like
820 //
821 // struct introspection_dispatch_pending_item_info_s {
822 // void *item_ref;
823 // void *function_or_block;
824 // };
825 //
826 // struct introspection_dispatch_pending_items_array_s {
827 // uint32_t version;
828 // uint32_t size_of_item_info;
829 // introspection_dispatch_pending_item_info_s items[];
830 // }
831
832 offset_t offset = 0;
833 uint64_t i = 0;
834 uint32_t version = extractor.GetU32(&offset);
835 if (version == 1) {
836 pending_item_refs.new_style = true;
837 uint32_t item_size = extractor.GetU32(&offset);
838 uint32_t start_of_array_offset = offset;
839 while (offset < pending_items_pointer.items_buffer_size &&
840 i < pending_items_pointer.count) {
841 offset = start_of_array_offset + (i * item_size);
843 item.item_ref = extractor.GetAddress(&offset);
844 item.code_address = extractor.GetAddress(&offset);
845 pending_item_refs.item_refs_and_code_addresses.push_back(item);
846 i++;
847 }
848 } else {
849 offset = 0;
850 pending_item_refs.new_style = false;
851 while (offset < pending_items_pointer.items_buffer_size &&
852 i < pending_items_pointer.count) {
854 item.item_ref = extractor.GetAddress(&offset);
856 pending_item_refs.item_refs_and_code_addresses.push_back(item);
857 i++;
858 }
859 }
860 }
861 m_page_to_free = pending_items_pointer.items_buffer_ptr;
862 m_page_to_free_size = pending_items_pointer.items_buffer_size;
863 }
864 }
865 }
866 return pending_item_refs;
867}
868
871 PendingItemsForQueue pending_item_refs =
873 for (ItemRefAndCodeAddress pending_item :
874 pending_item_refs.item_refs_and_code_addresses) {
875 Address addr;
876 m_process->GetTarget().ResolveLoadAddress(pending_item.code_address,
877 addr);
878 QueueItemSP queue_item_sp(new QueueItem(queue->shared_from_this(),
879 m_process->shared_from_this(),
880 pending_item.item_ref, addr));
881 queue->PushPendingQueueItem(queue_item_sp);
882 }
883 }
884}
885
887 addr_t item_ref) {
889
890 ThreadSP cur_thread_sp(
891 m_process->GetThreadList().GetExpressionExecutionThread());
893 ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
895 error);
899 ret.item_buffer_size > 0) {
900 DataBufferHeap data(ret.item_buffer_size, 0);
901 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
902 ret.item_buffer_size, error) &&
903 error.Success()) {
904 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
905 m_process->GetByteOrder(),
906 m_process->GetAddressByteSize());
907 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
911 queue_item->SetStopID(item.stop_id);
913 queue_item->SetThreadLabel(item.enqueuing_thread_label);
914 queue_item->SetQueueLabel(item.enqueuing_queue_label);
915 queue_item->SetTargetQueueLabel(item.target_queue_label);
916 }
919 }
920}
921
923 lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count,
924 lldb_private::QueueList &queue_list) {
926 DataBufferHeap data(queues_buffer_size, 0);
928 if (m_process->ReadMemory(queues_buffer, data.GetBytes(), queues_buffer_size,
929 error) == queues_buffer_size &&
930 error.Success()) {
931 // We've read the information out of inferior memory; free it on the next
932 // call we make
933 m_page_to_free = queues_buffer;
934 m_page_to_free_size = queues_buffer_size;
935
936 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
937 m_process->GetByteOrder(),
938 m_process->GetAddressByteSize());
939 offset_t offset = 0;
940 uint64_t queues_read = 0;
941
942 // The information about the queues is stored in this format (v1): typedef
943 // struct introspection_dispatch_queue_info_s {
944 // uint32_t offset_to_next;
945 // dispatch_queue_t queue;
946 // uint64_t serialnum; // queue's serialnum in the process, as
947 // provided by libdispatch
948 // uint32_t running_work_items_count;
949 // uint32_t pending_work_items_count;
950 //
951 // char data[]; // Starting here, we have variable-length data:
952 // // char queue_label[];
953 // } introspection_dispatch_queue_info_s;
954
955 while (queues_read < count && offset < queues_buffer_size) {
956 offset_t start_of_this_item = offset;
957
958 uint32_t offset_to_next = extractor.GetU32(&offset);
959
960 offset += 4; // Skip over the 4 bytes of reserved space
961 addr_t queue = extractor.GetAddress(&offset);
962 uint64_t serialnum = extractor.GetU64(&offset);
963 uint32_t running_work_items_count = extractor.GetU32(&offset);
964 uint32_t pending_work_items_count = extractor.GetU32(&offset);
965
966 // Read the first field of the variable length data
967 offset = start_of_this_item +
968 m_lib_backtrace_recording_info.queue_info_data_offset;
969 const char *queue_label = extractor.GetCStr(&offset);
970 if (queue_label == nullptr)
971 queue_label = "";
972
973 offset_t start_of_next_item = start_of_this_item + offset_to_next;
974 offset = start_of_next_item;
975
976 LLDB_LOGF(log,
977 "SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR added "
978 "queue with dispatch_queue_t 0x%" PRIx64
979 ", serial number 0x%" PRIx64
980 ", running items %d, pending items %d, name '%s'",
981 queue, serialnum, running_work_items_count,
982 pending_work_items_count, queue_label);
983
984 QueueSP queue_sp(
985 new Queue(m_process->shared_from_this(), serialnum, queue_label));
986 queue_sp->SetNumRunningWorkItems(running_work_items_count);
987 queue_sp->SetNumPendingWorkItems(pending_work_items_count);
988 queue_sp->SetLibdispatchQueueAddress(queue);
989 queue_sp->SetKind(GetQueueKind(queue));
990 queue_list.AddQueue(queue_sp);
991 queues_read++;
992 }
993 }
994}
995
997 lldb_private::DataExtractor &extractor) {
998 ItemInfo item;
999
1000 offset_t offset = 0;
1001
1002 item.item_that_enqueued_this = extractor.GetAddress(&offset);
1003 item.function_or_block = extractor.GetAddress(&offset);
1004 item.enqueuing_thread_id = extractor.GetU64(&offset);
1005 item.enqueuing_queue_serialnum = extractor.GetU64(&offset);
1006 item.target_queue_serialnum = extractor.GetU64(&offset);
1007 item.enqueuing_callstack_frame_count = extractor.GetU32(&offset);
1008 item.stop_id = extractor.GetU32(&offset);
1009
1010 offset = m_lib_backtrace_recording_info.item_info_data_offset;
1011
1012 for (uint32_t i = 0; i < item.enqueuing_callstack_frame_count; i++) {
1013 item.enqueuing_callstack.push_back(extractor.GetAddress(&offset));
1014 }
1015 item.enqueuing_thread_label = extractor.GetCStr(&offset);
1016 item.enqueuing_queue_label = extractor.GetCStr(&offset);
1017 item.target_queue_label = extractor.GetCStr(&offset);
1018
1019 return item;
1020}
1021
1025 "System runtime plugin for Mac OS X native libraries.", CreateInstance);
1026}
1027
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:389
#define LLDB_PLUGIN_DEFINE(PluginName)
lldb::addr_t m_dispatch_tsd_indexes_addr
lldb::ThreadSP GetExtendedBacktraceThread(lldb::ThreadSP thread, lldb_private::ConstString type) override
Return a Thread which shows the origin of this thread's creation.
PendingItemsForQueue GetPendingItemRefsForQueue(lldb::addr_t queue)
lldb::queue_id_t GetQueueIDFromThreadQAddress(lldb::addr_t dispatch_qaddr) override
Get the QueueID for the libdispatch queue given the thread's dispatch_qaddr.
lldb_private::AppleGetPendingItemsHandler m_get_pending_items_handler
lldb_private::AppleGetQueuesHandler m_get_queues_handler
struct LibdispatchTSDIndexes m_libdispatch_tsd_indexes
libBacktraceRecording_info m_lib_backtrace_recording_info
lldb_private::AppleGetItemInfoHandler m_get_item_info_handler
lldb::addr_t m_dispatch_queue_offsets_addr
std::string GetQueueNameFromThreadQAddress(lldb::addr_t dispatch_qaddr) override
Get the queue name for a thread given a thread's dispatch_qaddr.
void PopulateQueuesUsingLibBTR(lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count, lldb_private::QueueList &queue_list)
void Detach() override
Called before detaching from a process.
struct LibdispatchOffsets m_libdispatch_offsets
std::recursive_mutex m_mutex
void PopulatePendingItemsForQueue(lldb_private::Queue *queue) override
Get the pending work items for a libdispatch Queue.
const std::vector< lldb_private::ConstString > & GetExtendedBacktraceTypes() override
Return a list of thread origin extended backtraces that may be available.
lldb_private::AppleGetThreadItemInfoHandler m_get_thread_item_info_handler
lldb::addr_t GetLibdispatchQueueAddressFromThreadQAddress(lldb::addr_t dispatch_qaddr) override
Get the libdispatch_queue_t address for the queue given the thread's dispatch_qaddr.
struct LibdispatchVoucherOffsets m_libdispatch_voucher_offsets
lldb::QueueKind GetQueueKind(lldb::addr_t dispatch_queue_addr) override
Retrieve the Queue kind for the queue at a thread's dispatch_qaddr.
ItemInfo ExtractItemInfoFromBuffer(lldb_private::DataExtractor &extractor)
lldb::addr_t m_libpthread_layout_offsets_addr
lldb::addr_t m_dispatch_voucher_offsets_addr
bool SafeToCallFunctionsOnThisThread(lldb::ThreadSP thread_sp) override
Determine whether it is safe to run an expression on a given thread.
void CompleteQueueItem(lldb_private::QueueItem *queue_item, lldb::addr_t item_ref) override
Complete the fields in a QueueItem.
static lldb_private::SystemRuntime * CreateInstance(lldb_private::Process *process)
void AddThreadExtendedInfoPacketHints(lldb_private::StructuredData::ObjectSP dict) override
Add key-value pairs to the StructuredData dictionary object with information debugserver may need whe...
lldb::ThreadSP GetExtendedBacktraceFromItemRef(lldb::addr_t item_ref)
struct LibpthreadOffsets m_libpthread_offsets
lldb::ThreadSP GetExtendedBacktraceForQueueItem(lldb::QueueItemSP queue_item_sp, lldb_private::ConstString type) override
Get the extended backtrace thread for a QueueItem.
void Clear(bool clear_process)
void PopulateQueueList(lldb_private::QueueList &queue_list) override
Populate the Process' QueueList with libdispatch / GCD queues that exist.
SystemRuntimeMacOSX(lldb_private::Process *process)
static llvm::StringRef GetPluginNameStatic()
A section + offset based address class.
Definition Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition Address.cpp:303
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
An data extractor class.
const char * GetCStr(lldb::offset_t *offset_ptr) const
Extract a C string from *offset_ptr.
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
uint64_t GetAddress(lldb::offset_t *offset_ptr) const
Extract an address from *offset_ptr.
A file utility class.
Definition FileSpec.h:56
lldb::ModuleSP FindFirstModule(const ModuleSpec &module_spec) const
Finds the first module whose file specification matches module_spec.
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:91
virtual ObjectFile * GetObjectFile()
Get the object file representation for the current architecture.
Definition Module.cpp:1179
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
RetType GetField(llvm::StringRef name, RetType fail_value=RetType())
A plug-in interface definition class for debugging a process.
Definition Process.h:367
void SetThreadLabel(std::string thread_name)
Definition QueueItem.h:122
void SetTargetQueueLabel(std::string queue_name)
Definition QueueItem.h:130
void SetEnqueueingBacktrace(std::vector< lldb::addr_t > backtrace)
Definition QueueItem.h:116
void SetStopID(uint32_t stop_id)
Definition QueueItem.h:112
void SetEnqueueingThreadID(lldb::tid_t tid)
Definition QueueItem.h:100
void SetQueueLabel(std::string queue_name)
Definition QueueItem.h:126
void SetItemThatEnqueuedThis(lldb::addr_t address_of_item)
Definition QueueItem.h:94
void SetEnqueueingQueueID(lldb::queue_id_t qid)
Definition QueueItem.h:104
lldb::QueueSP FindQueueByID(lldb::queue_id_t qid)
Find a queue in the QueueList by QueueID.
Definition QueueList.cpp:47
void AddQueue(lldb::QueueSP queue)
Add a Queue to the QueueList.
Definition QueueList.cpp:40
void PushPendingQueueItem(lldb::QueueItemSP item)
Definition Queue.h:125
lldb::addr_t GetLibdispatchQueueAddress() const
Get the dispatch_queue_t structure address for this Queue.
Definition Queue.cpp:73
Process * m_process
Definition Runtime.h:29
static lldb::TypeSystemClangSP GetForTarget(Target &target, std::optional< IsolatedASTKind > ast_kind=DefaultAST, bool create_on_demand=true)
Returns the scratch TypeSystemClang for the given target.
An error handling class.
Definition Status.h:118
bool ForEach(std::function< bool(Object *object)> const &foreach_callback) const
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
void AddIntegerItem(llvm::StringRef key, T value)
std::shared_ptr< Object > ObjectSP
Defines a list of symbol context objects.
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
void Clear()
Clear the object's state.
Defines a symbol context baton that can be handed other debug core functions.
ConstString GetFunctionName(Mangled::NamePreference preference=Mangled::ePreferDemangled) const
Find a name of the innermost function for the symbol context.
Address GetFunctionOrSymbolAddress() const
Get the address of the function or symbol represented by this symbol context.
lldb::addr_t GetLoadAddress(Target *target) const
Definition Symbol.cpp:605
SystemRuntime(Process *process)
Construct with a process.
std::vector< ConstString > m_types
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1254
static bool CompleteTagDeclarationDefinition(const CompilerType &type)
static bool StartTagDeclarationDefinition(const CompilerType &type)
static clang::FieldDecl * AddFieldToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &field_type, uint32_t bitfield_bit_size)
uint8_t * GetBytes()
Get a pointer to the data.
Definition DataBuffer.h:108
#define LLDB_INVALID_QUEUE_ID
#define LLDB_INVALID_BREAK_ID
#define LLDB_BREAK_ID_IS_VALID(bid)
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
void RegisterAbortWithPayloadFrameRecognizer(Process *process)
std::shared_ptr< lldb_private::Queue > QueueSP
std::shared_ptr< lldb_private::Thread > ThreadSP
uint64_t offset_t
Definition lldb-types.h:86
@ eLanguageTypeC
Non-standardized C, such as K&R.
@ eEncodingUint
unsigned integer
QueueKind
Queue type.
std::shared_ptr< lldb_private::TypeSystemClang > TypeSystemClangSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP
uint64_t queue_id_t
Definition lldb-types.h:92
std::shared_ptr< lldb_private::QueueItem > QueueItemSP
std::vector< lldb::addr_t > enqueuing_callstack
std::vector< ItemRefAndCodeAddress > item_refs_and_code_addresses