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
32#include "SystemRuntimeMacOSX.h"
33
34#include <memory>
35
36using namespace lldb;
37using namespace lldb_private;
38
40
41// Create an instance of this class. This function is filled into the plugin
42// info class that gets handed out by the plugin factory and allows the lldb to
43// instantiate an instance of this class.
44SystemRuntime *SystemRuntimeMacOSX::CreateInstance(Process *process) {
45 bool create = false;
46 if (!create) {
47 create = true;
48 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
49 if (exe_module) {
50 ObjectFile *object_file = exe_module->GetObjectFile();
51 if (object_file) {
52 create = (object_file->GetStrata() == ObjectFile::eStrataUser);
53 }
54 }
55
56 if (create) {
57 const llvm::Triple &triple_ref =
58 process->GetTarget().GetArchitecture().GetTriple();
59 switch (triple_ref.getOS()) {
60 case llvm::Triple::Darwin:
61 case llvm::Triple::MacOSX:
62 case llvm::Triple::IOS:
63 case llvm::Triple::TvOS:
64 case llvm::Triple::WatchOS:
65 case llvm::Triple::XROS:
66 case llvm::Triple::BridgeOS:
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 nullptr;
79}
80
81// Constructor
83 : SystemRuntime(process), m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
84 m_get_queues_handler(process), m_get_pending_items_handler(process),
85 m_get_item_info_handler(process), m_get_thread_item_info_handler(process),
86 m_page_to_free(LLDB_INVALID_ADDRESS), m_page_to_free_size(0),
87 m_lib_backtrace_recording_info(),
88 m_dispatch_queue_offsets_addr(LLDB_INVALID_ADDRESS),
89 m_libdispatch_offsets(),
90 m_libpthread_layout_offsets_addr(LLDB_INVALID_ADDRESS),
91 m_libpthread_offsets(), m_dispatch_tsd_indexes_addr(LLDB_INVALID_ADDRESS),
92 m_libdispatch_tsd_indexes(),
93 m_dispatch_voucher_offsets_addr(LLDB_INVALID_ADDRESS),
94 m_libdispatch_voucher_offsets() {
95
97}
98
99// Destructor
101
107}
108
109// Clear out the state of this class.
110void SystemRuntimeMacOSX::Clear(bool clear_process) {
111 std::lock_guard<std::recursive_mutex> guard(m_mutex);
112
115
116 if (clear_process)
117 m_process = nullptr;
119}
120
121std::string
123 std::string dispatch_queue_name;
124 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
125 return "";
126
129 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a
130 // thread - deref it to get the address of the dispatch_queue_t structure
131 // for this thread's queue.
133 addr_t dispatch_queue_addr =
134 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
135 if (error.Success()) {
137 // libdispatch versions 4+, pointer to dispatch name is in the queue
138 // structure.
139 addr_t pointer_to_label_address =
140 dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
141 addr_t label_addr =
142 m_process->ReadPointerFromMemory(pointer_to_label_address, error);
143 if (error.Success()) {
144 m_process->ReadCStringFromMemory(label_addr, dispatch_queue_name,
145 error);
146 }
147 } else {
148 // libdispatch versions 1-3, dispatch name is a fixed width char array
149 // in the queue structure.
150 addr_t label_addr =
151 dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
152 dispatch_queue_name.resize(m_libdispatch_offsets.dqo_label_size, '\0');
153 size_t bytes_read =
154 m_process->ReadMemory(label_addr, &dispatch_queue_name[0],
156 if (bytes_read < m_libdispatch_offsets.dqo_label_size)
157 dispatch_queue_name.erase(bytes_read);
158 }
159 }
160 }
161 return dispatch_queue_name;
162}
163
165 addr_t dispatch_qaddr) {
166 addr_t libdispatch_queue_t_address = LLDB_INVALID_ADDRESS;
168 libdispatch_queue_t_address =
169 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
170 if (!error.Success()) {
171 libdispatch_queue_t_address = LLDB_INVALID_ADDRESS;
172 }
173 return libdispatch_queue_t_address;
174}
175
177 if (dispatch_queue_addr == LLDB_INVALID_ADDRESS || dispatch_queue_addr == 0)
178 return eQueueKindUnknown;
179
186 dispatch_queue_addr + m_libdispatch_offsets.dqo_width,
188 if (error.Success()) {
189 if (width == 1) {
190 kind = eQueueKindSerial;
191 }
192 if (width > 1) {
194 }
195 }
196 }
197 return kind;
198}
199
203 if (dict) {
206 dict->AddIntegerItem("plo_pthread_tsd_base_offset",
208 dict->AddIntegerItem(
209 "plo_pthread_tsd_base_address_offset",
211 dict->AddIntegerItem("plo_pthread_tsd_entry_size",
213 }
214
217 dict->AddIntegerItem("dti_queue_index",
219 dict->AddIntegerItem("dti_voucher_index",
221 dict->AddIntegerItem("dti_qos_class_index",
223 }
224 }
225}
226
228 if (thread_sp && thread_sp->GetFrameWithConcreteFrameIndex(0)) {
229 const SymbolContext sym_ctx(
230 thread_sp->GetFrameWithConcreteFrameIndex(0)->GetSymbolContext(
231 eSymbolContextSymbol));
232 static ConstString g_select_symbol("__select");
233 if (sym_ctx.GetFunctionName() == g_select_symbol) {
234 return false;
235 }
236 }
237 return true;
238}
239
243
244 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
245 return queue_id;
246
249 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a
250 // thread - deref it to get the address of the dispatch_queue_t structure
251 // for this thread's queue.
253 uint64_t dispatch_queue_addr =
254 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
255 if (error.Success()) {
256 addr_t serialnum_address =
257 dispatch_queue_addr + m_libdispatch_offsets.dqo_serialnum;
259 serialnum_address, m_libdispatch_offsets.dqo_serialnum_size,
261 if (error.Success()) {
262 queue_id = serialnum;
263 }
264 }
265 }
266
267 return queue_id;
268}
269
272 return;
273
274 static ConstString g_dispatch_queue_offsets_symbol_name(
275 "dispatch_queue_offsets");
276 const Symbol *dispatch_queue_offsets_symbol = nullptr;
277
278 // libdispatch symbols were in libSystem.B.dylib up through Mac OS X 10.6
279 // ("Snow Leopard")
280 ModuleSpec libSystem_module_spec(FileSpec("libSystem.B.dylib"));
282 libSystem_module_spec));
283 if (module_sp)
284 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
285 g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
286
287 // libdispatch symbols are in their own dylib as of Mac OS X 10.7 ("Lion")
288 // and later
289 if (dispatch_queue_offsets_symbol == nullptr) {
290 ModuleSpec libdispatch_module_spec(FileSpec("libdispatch.dylib"));
292 libdispatch_module_spec);
293 if (module_sp)
294 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
295 g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
296 }
297 if (dispatch_queue_offsets_symbol)
299 dispatch_queue_offsets_symbol->GetLoadAddress(&m_process->GetTarget());
300}
301
304 return;
305
307
308 uint8_t memory_buffer[sizeof(struct LibdispatchOffsets)];
309 DataExtractor data(memory_buffer, sizeof(memory_buffer),
312
315 sizeof(memory_buffer),
316 error) == sizeof(memory_buffer)) {
317 lldb::offset_t data_offset = 0;
318
319 // The struct LibdispatchOffsets is a series of uint16_t's - extract them
320 // all in one big go.
321 data.GetU16(&data_offset, &m_libdispatch_offsets.dqo_version,
322 sizeof(struct LibdispatchOffsets) / sizeof(uint16_t));
323 }
324}
325
328 return;
329
330 static ConstString g_libpthread_layout_offsets_symbol_name(
331 "pthread_layout_offsets");
332 const Symbol *libpthread_layout_offsets_symbol = nullptr;
333
334 ModuleSpec libpthread_module_spec(FileSpec("libsystem_pthread.dylib"));
336 libpthread_module_spec));
337 if (module_sp) {
338 libpthread_layout_offsets_symbol =
339 module_sp->FindFirstSymbolWithNameAndType(
340 g_libpthread_layout_offsets_symbol_name, eSymbolTypeData);
341 if (libpthread_layout_offsets_symbol) {
343 libpthread_layout_offsets_symbol->GetLoadAddress(
344 &m_process->GetTarget());
345 }
346 }
347}
348
351 return;
352
354
356 uint8_t memory_buffer[sizeof(struct LibpthreadOffsets)];
357 DataExtractor data(memory_buffer, sizeof(memory_buffer),
362 sizeof(memory_buffer),
363 error) == sizeof(memory_buffer)) {
364 lldb::offset_t data_offset = 0;
365
366 // The struct LibpthreadOffsets is a series of uint16_t's - extract them
367 // all in one big go.
368 data.GetU16(&data_offset, &m_libpthread_offsets.plo_version,
369 sizeof(struct LibpthreadOffsets) / sizeof(uint16_t));
370 }
371 }
372}
373
376 return;
377
378 static ConstString g_libdispatch_tsd_indexes_symbol_name(
379 "dispatch_tsd_indexes");
380 const Symbol *libdispatch_tsd_indexes_symbol = nullptr;
381
382 ModuleSpec libpthread_module_spec(FileSpec("libdispatch.dylib"));
384 libpthread_module_spec));
385 if (module_sp) {
386 libdispatch_tsd_indexes_symbol = module_sp->FindFirstSymbolWithNameAndType(
387 g_libdispatch_tsd_indexes_symbol_name, eSymbolTypeData);
388 if (libdispatch_tsd_indexes_symbol) {
390 libdispatch_tsd_indexes_symbol->GetLoadAddress(
391 &m_process->GetTarget());
392 }
393 }
394}
395
398 return;
399
401
403
404// We don't need to check the version number right now, it will be at least 2,
405// but keep this code around to fetch just the version # for the future where
406// we need to fetch alternate versions of the struct.
407#if 0
408 uint16_t dti_version = 2;
409 Address dti_struct_addr;
411 {
413 uint16_t version = m_process->GetTarget().ReadUnsignedIntegerFromMemory (dti_struct_addr, false, 2, UINT16_MAX, error);
414 if (error.Success() && dti_version != UINT16_MAX)
415 {
416 dti_version = version;
417 }
418 }
419#endif
420
421 TypeSystemClangSP scratch_ts_sp =
424 CompilerType uint16 =
425 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16);
426 CompilerType dispatch_tsd_indexes_s = scratch_ts_sp->CreateRecordType(
428 "__lldb_dispatch_tsd_indexes_s",
429 llvm::to_underlying(clang::TagTypeKind::Struct),
431
433 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
434 "dti_version", uint16,
436 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
437 "dti_queue_index", uint16,
439 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
440 "dti_voucher_index", uint16,
442 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
443 "dti_qos_class_index", uint16,
446
448 dispatch_tsd_indexes_s);
449
451 struct_reader.GetField<uint16_t>("dti_version");
453 struct_reader.GetField<uint16_t>("dti_queue_index");
455 struct_reader.GetField<uint16_t>("dti_voucher_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(
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);
490 ret.item_buffer_size, error) &&
491 error.Success()) {
492 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
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 =
547 std::make_shared<HistoryThread>(*m_process, real_thread->GetIndexID(),
548 app_specific_backtrace_pcs, true);
549 originating_thread_sp->SetQueueName(type.AsCString());
550 }
551 return originating_thread_sp;
552}
553
556 ThreadSP return_thread_sp;
557
559 ThreadSP cur_thread_sp(
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);
571 ret.item_buffer_size, error) &&
572 error.Success()) {
573 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
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
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 static ConstString introspection_dispatch_queue_info_version(
628 "__introspection_dispatch_queue_info_version");
629 SymbolContextList sc_list;
631 introspection_dispatch_queue_info_version, eSymbolTypeData, sc_list);
632 if (!sc_list.IsEmpty()) {
633 SymbolContext sc;
634 sc_list.GetContextAtIndex(0, sc);
635 AddressRange addr_range;
636 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
637 queue_info_version_address =
638 addr_range.GetBaseAddress().GetLoadAddress(&target);
639 }
640 sc_list.Clear();
641
642 static ConstString introspection_dispatch_queue_info_data_offset(
643 "__introspection_dispatch_queue_info_data_offset");
645 introspection_dispatch_queue_info_data_offset, eSymbolTypeData, sc_list);
646 if (!sc_list.IsEmpty()) {
647 SymbolContext sc;
648 sc_list.GetContextAtIndex(0, sc);
649 AddressRange addr_range;
650 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
651 queue_info_data_offset_address =
652 addr_range.GetBaseAddress().GetLoadAddress(&target);
653 }
654 sc_list.Clear();
655
656 static ConstString introspection_dispatch_item_info_version(
657 "__introspection_dispatch_item_info_version");
659 introspection_dispatch_item_info_version, eSymbolTypeData, sc_list);
660 if (!sc_list.IsEmpty()) {
661 SymbolContext sc;
662 sc_list.GetContextAtIndex(0, sc);
663 AddressRange addr_range;
664 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
665 item_info_version_address =
666 addr_range.GetBaseAddress().GetLoadAddress(&target);
667 }
668 sc_list.Clear();
669
670 static ConstString introspection_dispatch_item_info_data_offset(
671 "__introspection_dispatch_item_info_data_offset");
673 introspection_dispatch_item_info_data_offset, eSymbolTypeData, sc_list);
674 if (!sc_list.IsEmpty()) {
675 SymbolContext sc;
676 sc_list.GetContextAtIndex(0, sc);
677 AddressRange addr_range;
678 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
679 item_info_data_offset_address =
680 addr_range.GetBaseAddress().GetLoadAddress(&target);
681 }
682
683 if (queue_info_version_address != LLDB_INVALID_ADDRESS &&
684 queue_info_data_offset_address != LLDB_INVALID_ADDRESS &&
685 item_info_version_address != LLDB_INVALID_ADDRESS &&
686 item_info_data_offset_address != LLDB_INVALID_ADDRESS) {
689 m_process->ReadUnsignedIntegerFromMemory(queue_info_version_address, 2,
690 0, error);
691 if (error.Success()) {
694 queue_info_data_offset_address, 2, 0, error);
695 if (error.Success()) {
697 m_process->ReadUnsignedIntegerFromMemory(item_info_version_address,
698 2, 0, error);
699 if (error.Success()) {
702 item_info_data_offset_address, 2, 0, error);
703 if (!error.Success()) {
705 }
706 } else {
708 }
709 } else {
711 }
712 }
713 }
714
716}
717
718const std::vector<ConstString> &
720 if (m_types.size() == 0) {
721 m_types.push_back(ConstString("libdispatch"));
722 m_types.push_back(ConstString("Application Specific Backtrace"));
723 // We could have pthread as another type in the future if we have a way of
724 // gathering that information & it's useful to distinguish between them.
725 }
726 return m_types;
727}
728
730 lldb_private::QueueList &queue_list) {
733 ThreadSP cur_thread_sp(
735 if (cur_thread_sp) {
737 queue_info_pointer = m_get_queues_handler.GetCurrentQueues(
738 *cur_thread_sp.get(), m_page_to_free, m_page_to_free_size, error);
741 if (error.Success()) {
742
743 if (queue_info_pointer.count > 0 &&
744 queue_info_pointer.queues_buffer_size > 0 &&
745 queue_info_pointer.queues_buffer_ptr != 0 &&
746 queue_info_pointer.queues_buffer_ptr != LLDB_INVALID_ADDRESS) {
748 queue_info_pointer.queues_buffer_size,
749 queue_info_pointer.count, queue_list);
750 }
751 }
752 }
753 }
754
755 // We either didn't have libBacktraceRecording (and need to create the queues
756 // list based on threads) or we did get the queues list from
757 // libBacktraceRecording but some special queues may not be included in its
758 // information. This is needed because libBacktraceRecording will only list
759 // queues with pending or running items by default - but the magic com.apple
760 // .main-thread queue on thread 1 is always around.
761
762 for (ThreadSP thread_sp : m_process->Threads()) {
763 if (thread_sp->GetAssociatedWithLibdispatchQueue() != eLazyBoolNo) {
764 if (thread_sp->GetQueueID() != LLDB_INVALID_QUEUE_ID) {
765 if (queue_list.FindQueueByID(thread_sp->GetQueueID()).get() ==
766 nullptr) {
767 QueueSP queue_sp(new Queue(m_process->shared_from_this(),
768 thread_sp->GetQueueID(),
769 thread_sp->GetQueueName()));
770 if (thread_sp->ThreadHasQueueInformation()) {
771 queue_sp->SetKind(thread_sp->GetQueueKind());
772 queue_sp->SetLibdispatchQueueAddress(
773 thread_sp->GetQueueLibdispatchQueueAddress());
774 queue_list.AddQueue(queue_sp);
775 } else {
776 queue_sp->SetKind(
777 GetQueueKind(thread_sp->GetQueueLibdispatchQueueAddress()));
778 queue_sp->SetLibdispatchQueueAddress(
779 thread_sp->GetQueueLibdispatchQueueAddress());
780 queue_list.AddQueue(queue_sp);
781 }
782 }
783 }
784 }
785 }
786}
787
788// Returns either an array of introspection_dispatch_item_info_ref's for the
789// pending items on a queue or an array introspection_dispatch_item_info_ref's
790// and code addresses for the pending items on a queue. The information about
791// each of these pending items then needs to be fetched individually by passing
792// the ref to libBacktraceRecording.
793
796 PendingItemsForQueue pending_item_refs = {};
798 ThreadSP cur_thread_sp(
800 if (cur_thread_sp) {
802 pending_items_pointer = m_get_pending_items_handler.GetPendingItems(
803 *cur_thread_sp.get(), queue, m_page_to_free, m_page_to_free_size,
804 error);
807 if (error.Success()) {
808 if (pending_items_pointer.count > 0 &&
809 pending_items_pointer.items_buffer_size > 0 &&
810 pending_items_pointer.items_buffer_ptr != 0 &&
811 pending_items_pointer.items_buffer_ptr != LLDB_INVALID_ADDRESS) {
812 DataBufferHeap data(pending_items_pointer.items_buffer_size, 0);
814 pending_items_pointer.items_buffer_ptr, data.GetBytes(),
815 pending_items_pointer.items_buffer_size, error)) {
816 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
819
820 // We either have an array of
821 // void* item_ref
822 // (old style) or we have a structure returned which looks like
823 //
824 // struct introspection_dispatch_pending_item_info_s {
825 // void *item_ref;
826 // void *function_or_block;
827 // };
828 //
829 // struct introspection_dispatch_pending_items_array_s {
830 // uint32_t version;
831 // uint32_t size_of_item_info;
832 // introspection_dispatch_pending_item_info_s items[];
833 // }
834
835 offset_t offset = 0;
836 uint64_t i = 0;
837 uint32_t version = extractor.GetU32(&offset);
838 if (version == 1) {
839 pending_item_refs.new_style = true;
840 uint32_t item_size = extractor.GetU32(&offset);
841 uint32_t start_of_array_offset = offset;
842 while (offset < pending_items_pointer.items_buffer_size &&
843 i < pending_items_pointer.count) {
844 offset = start_of_array_offset + (i * item_size);
846 item.item_ref = extractor.GetAddress(&offset);
847 item.code_address = extractor.GetAddress(&offset);
848 pending_item_refs.item_refs_and_code_addresses.push_back(item);
849 i++;
850 }
851 } else {
852 offset = 0;
853 pending_item_refs.new_style = false;
854 while (offset < pending_items_pointer.items_buffer_size &&
855 i < pending_items_pointer.count) {
857 item.item_ref = extractor.GetAddress(&offset);
859 pending_item_refs.item_refs_and_code_addresses.push_back(item);
860 i++;
861 }
862 }
863 }
864 m_page_to_free = pending_items_pointer.items_buffer_ptr;
865 m_page_to_free_size = pending_items_pointer.items_buffer_size;
866 }
867 }
868 }
869 return pending_item_refs;
870}
871
874 PendingItemsForQueue pending_item_refs =
876 for (ItemRefAndCodeAddress pending_item :
877 pending_item_refs.item_refs_and_code_addresses) {
878 Address addr;
880 addr);
881 QueueItemSP queue_item_sp(new QueueItem(queue->shared_from_this(),
882 m_process->shared_from_this(),
883 pending_item.item_ref, addr));
884 queue->PushPendingQueueItem(queue_item_sp);
885 }
886 }
887}
888
890 addr_t item_ref) {
892
893 ThreadSP cur_thread_sp(
896 ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
898 error);
902 ret.item_buffer_size > 0) {
903 DataBufferHeap data(ret.item_buffer_size, 0);
905 ret.item_buffer_size, error) &&
906 error.Success()) {
907 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
910 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
914 queue_item->SetStopID(item.stop_id);
916 queue_item->SetThreadLabel(item.enqueuing_thread_label);
917 queue_item->SetQueueLabel(item.enqueuing_queue_label);
918 queue_item->SetTargetQueueLabel(item.target_queue_label);
919 }
922 }
923}
924
926 lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count,
927 lldb_private::QueueList &queue_list) {
929 DataBufferHeap data(queues_buffer_size, 0);
930 Log *log = GetLog(LLDBLog::SystemRuntime);
931 if (m_process->ReadMemory(queues_buffer, data.GetBytes(), queues_buffer_size,
932 error) == queues_buffer_size &&
933 error.Success()) {
934 // We've read the information out of inferior memory; free it on the next
935 // call we make
936 m_page_to_free = queues_buffer;
937 m_page_to_free_size = queues_buffer_size;
938
939 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
942 offset_t offset = 0;
943 uint64_t queues_read = 0;
944
945 // The information about the queues is stored in this format (v1): typedef
946 // struct introspection_dispatch_queue_info_s {
947 // uint32_t offset_to_next;
948 // dispatch_queue_t queue;
949 // uint64_t serialnum; // queue's serialnum in the process, as
950 // provided by libdispatch
951 // uint32_t running_work_items_count;
952 // uint32_t pending_work_items_count;
953 //
954 // char data[]; // Starting here, we have variable-length data:
955 // // char queue_label[];
956 // } introspection_dispatch_queue_info_s;
957
958 while (queues_read < count && offset < queues_buffer_size) {
959 offset_t start_of_this_item = offset;
960
961 uint32_t offset_to_next = extractor.GetU32(&offset);
962
963 offset += 4; // Skip over the 4 bytes of reserved space
964 addr_t queue = extractor.GetAddress(&offset);
965 uint64_t serialnum = extractor.GetU64(&offset);
966 uint32_t running_work_items_count = extractor.GetU32(&offset);
967 uint32_t pending_work_items_count = extractor.GetU32(&offset);
968
969 // Read the first field of the variable length data
970 offset = start_of_this_item +
972 const char *queue_label = extractor.GetCStr(&offset);
973 if (queue_label == nullptr)
974 queue_label = "";
975
976 offset_t start_of_next_item = start_of_this_item + offset_to_next;
977 offset = start_of_next_item;
978
979 LLDB_LOGF(log,
980 "SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR added "
981 "queue with dispatch_queue_t 0x%" PRIx64
982 ", serial number 0x%" PRIx64
983 ", running items %d, pending items %d, name '%s'",
984 queue, serialnum, running_work_items_count,
985 pending_work_items_count, queue_label);
986
987 QueueSP queue_sp(
988 new Queue(m_process->shared_from_this(), serialnum, queue_label));
989 queue_sp->SetNumRunningWorkItems(running_work_items_count);
990 queue_sp->SetNumPendingWorkItems(pending_work_items_count);
991 queue_sp->SetLibdispatchQueueAddress(queue);
992 queue_sp->SetKind(GetQueueKind(queue));
993 queue_list.AddQueue(queue_sp);
994 queues_read++;
995 }
996 }
997}
998
1000 lldb_private::DataExtractor &extractor) {
1001 ItemInfo item;
1002
1003 offset_t offset = 0;
1004
1005 item.item_that_enqueued_this = extractor.GetAddress(&offset);
1006 item.function_or_block = extractor.GetAddress(&offset);
1007 item.enqueuing_thread_id = extractor.GetU64(&offset);
1008 item.enqueuing_queue_serialnum = extractor.GetU64(&offset);
1009 item.target_queue_serialnum = extractor.GetU64(&offset);
1010 item.enqueuing_callstack_frame_count = extractor.GetU32(&offset);
1011 item.stop_id = extractor.GetU32(&offset);
1012
1014
1015 for (uint32_t i = 0; i < item.enqueuing_callstack_frame_count; i++) {
1016 item.enqueuing_callstack.push_back(extractor.GetAddress(&offset));
1017 }
1018 item.enqueuing_thread_label = extractor.GetCStr(&offset);
1019 item.enqueuing_queue_label = extractor.GetCStr(&offset);
1020 item.target_queue_label = extractor.GetCStr(&offset);
1021
1022 return item;
1023}
1024
1028 "System runtime plugin for Mac OS X native libraries.", CreateInstance);
1029}
1030
1033}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:366
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:32
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.
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
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)
lldb::user_id_t m_break_id
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 range class.
Definition: AddressRange.h:25
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:211
A section + offset based address class.
Definition: Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:313
GetItemInfoReturnInfo GetItemInfo(Thread &thread, lldb::addr_t item, lldb::addr_t page_to_free, uint64_t page_to_free_size, lldb_private::Status &error)
Get the information about a work item by calling __introspection_dispatch_queue_item_get_info.
GetPendingItemsReturnInfo GetPendingItems(Thread &thread, lldb::addr_t queue, lldb::addr_t page_to_free, uint64_t page_to_free_size, lldb_private::Status &error)
Get the list of pending items for a given queue via a call to __introspection_dispatch_queue_get_pend...
GetQueuesReturnInfo GetCurrentQueues(Thread &thread, lldb::addr_t page_to_free, uint64_t page_to_free_size, lldb_private::Status &error)
Get the list of queues that exist (with any active or pending items) via a call to introspection_get_...
GetThreadItemInfoReturnInfo GetThreadItemInfo(Thread &thread, lldb::tid_t thread_id, lldb::addr_t page_to_free, uint64_t page_to_free_size, lldb_private::Status &error)
Get the information about a work item by calling __introspection_dispatch_thread_get_item_info.
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:450
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
A uniqued constant string class.
Definition: ConstString.h:40
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
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.
Definition: DataExtractor.h:48
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
Definition: ModuleList.cpp:626
void FindSymbolsWithNameAndType(ConstString name, lldb::SymbolType symbol_type, SymbolContextList &sc_list) const
Definition: ModuleList.cpp:527
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:88
virtual ObjectFile * GetObjectFile()
Get the object file representation for the current architecture.
Definition: Module.cpp:1189
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:44
virtual ArchSpec GetArchitecture()=0
Get the ArchSpec for this object file.
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:341
ThreadList & GetThreadList()
Definition: Process.h:2227
size_t ReadCStringFromMemory(lldb::addr_t vm_addr, char *cstr, size_t cstr_max_len, Status &error)
Read a NULL terminated C string from memory.
Definition: Process.cpp:2162
virtual size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition: Process.cpp:1973
lldb::ByteOrder GetByteOrder() const
Definition: Process.cpp:3596
ThreadList::ThreadIterable Threads()
Definition: Process.h:2240
uint64_t ReadUnsignedIntegerFromMemory(lldb::addr_t load_addr, size_t byte_size, uint64_t fail_value, Status &error)
Reads an unsigned integer of the specified byte size from process memory.
Definition: Process.cpp:2237
Status ClearBreakpointSiteByID(lldb::user_id_t break_id)
Definition: Process.cpp:1626
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition: Process.cpp:2259
virtual bool IsAlive()
Check if a process is still alive.
Definition: Process.cpp:1127
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3600
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1285
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:44
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.
Definition: SymbolContext.h:34
ConstString GetFunctionName(Mangled::NamePreference preference=Mangled::ePreferDemangled) const
Find a name of the innermost function for the symbol context.
bool GetAddressRange(uint32_t scope, uint32_t range_idx, bool use_inline_block_range, AddressRange &range) const
Get the address range contained within a symbol context.
lldb::addr_t GetLoadAddress(Target *target) const
Definition: Symbol.cpp:541
A plug-in interface definition class for system runtimes.
Definition: SystemRuntime.h:43
std::vector< ConstString > m_types
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow)
Definition: Target.cpp:3114
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:986
uint64_t ReadUnsignedIntegerFromMemory(const Address &addr, size_t integer_byte_size, uint64_t fail_value, Status &error, bool force_live_memory=false)
Definition: Target.cpp:2118
lldb::ThreadSP GetExpressionExecutionThread()
Definition: ThreadList.cpp:59
static clang::FieldDecl * AddFieldToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &field_type, lldb::AccessType access, uint32_t bitfield_bit_size)
static bool CompleteTagDeclarationDefinition(const CompilerType &type)
static bool StartTagDeclarationDefinition(const CompilerType &type)
uint8_t * GetBytes()
Get a pointer to the data.
Definition: DataBuffer.h:108
#define LLDB_INVALID_QUEUE_ID
Definition: lldb-defines.h:96
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:37
#define LLDB_BREAK_ID_IS_VALID(bid)
Definition: lldb-defines.h:39
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
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:331
void RegisterAbortWithPayloadFrameRecognizer(Process *process)
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Queue > QueueSP
Definition: lldb-forward.h:396
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:446
uint64_t offset_t
Definition: lldb-types.h:85
@ eLanguageTypeC
Non-standardized C, such as K&R.
@ eEncodingUint
unsigned integer
QueueKind
Queue type.
@ eQueueKindConcurrent
std::shared_ptr< lldb_private::TypeSystemClang > TypeSystemClangSP
Definition: lldb-forward.h:466
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:371
uint64_t queue_id_t
Definition: lldb-types.h:90
std::shared_ptr< lldb_private::QueueItem > QueueItemSP
Definition: lldb-forward.h:398
std::vector< lldb::addr_t > enqueuing_callstack
std::vector< ItemRefAndCodeAddress > item_refs_and_code_addresses