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 "SystemRuntimeMacOSX.h"
32
33#include <memory>
34
35using namespace lldb;
36using namespace lldb_private;
37
39
40// Create an instance of this class. This function is filled into the plugin
41// info class that gets handed out by the plugin factory and allows the lldb to
42// instantiate an instance of this class.
43SystemRuntime *SystemRuntimeMacOSX::CreateInstance(Process *process) {
44 bool create = false;
45 if (!create) {
46 create = true;
47 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
48 if (exe_module) {
49 ObjectFile *object_file = exe_module->GetObjectFile();
50 if (object_file) {
51 create = (object_file->GetStrata() == ObjectFile::eStrataUser);
52 }
53 }
54
55 if (create) {
56 const llvm::Triple &triple_ref =
57 process->GetTarget().GetArchitecture().GetTriple();
58 switch (triple_ref.getOS()) {
59 case llvm::Triple::Darwin:
60 case llvm::Triple::MacOSX:
61 case llvm::Triple::IOS:
62 case llvm::Triple::TvOS:
63 case llvm::Triple::WatchOS:
64 case llvm::Triple::XROS:
65 case llvm::Triple::BridgeOS:
66 create = triple_ref.getVendor() == llvm::Triple::Apple;
67 break;
68 default:
69 create = false;
70 break;
71 }
72 }
73 }
74
75 if (create)
76 return new SystemRuntimeMacOSX(process);
77 return nullptr;
78}
79
80// Constructor
82 : SystemRuntime(process), m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
83 m_get_queues_handler(process), m_get_pending_items_handler(process),
84 m_get_item_info_handler(process), m_get_thread_item_info_handler(process),
85 m_page_to_free(LLDB_INVALID_ADDRESS), m_page_to_free_size(0),
86 m_lib_backtrace_recording_info(),
87 m_dispatch_queue_offsets_addr(LLDB_INVALID_ADDRESS),
88 m_libdispatch_offsets(),
89 m_libpthread_layout_offsets_addr(LLDB_INVALID_ADDRESS),
90 m_libpthread_offsets(), m_dispatch_tsd_indexes_addr(LLDB_INVALID_ADDRESS),
91 m_libdispatch_tsd_indexes(),
92 m_dispatch_voucher_offsets_addr(LLDB_INVALID_ADDRESS),
93 m_libdispatch_voucher_offsets() {}
94
95// Destructor
97
103}
104
105// Clear out the state of this class.
106void SystemRuntimeMacOSX::Clear(bool clear_process) {
107 std::lock_guard<std::recursive_mutex> guard(m_mutex);
108
111
112 if (clear_process)
113 m_process = nullptr;
115}
116
117std::string
119 std::string dispatch_queue_name;
120 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
121 return "";
122
125 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a
126 // thread - deref it to get the address of the dispatch_queue_t structure
127 // for this thread's queue.
129 addr_t dispatch_queue_addr =
130 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
131 if (error.Success()) {
133 // libdispatch versions 4+, pointer to dispatch name is in the queue
134 // structure.
135 addr_t pointer_to_label_address =
136 dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
137 addr_t label_addr =
138 m_process->ReadPointerFromMemory(pointer_to_label_address, error);
139 if (error.Success()) {
140 m_process->ReadCStringFromMemory(label_addr, dispatch_queue_name,
141 error);
142 }
143 } else {
144 // libdispatch versions 1-3, dispatch name is a fixed width char array
145 // in the queue structure.
146 addr_t label_addr =
147 dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
148 dispatch_queue_name.resize(m_libdispatch_offsets.dqo_label_size, '\0');
149 size_t bytes_read =
150 m_process->ReadMemory(label_addr, &dispatch_queue_name[0],
152 if (bytes_read < m_libdispatch_offsets.dqo_label_size)
153 dispatch_queue_name.erase(bytes_read);
154 }
155 }
156 }
157 return dispatch_queue_name;
158}
159
161 addr_t dispatch_qaddr) {
162 addr_t libdispatch_queue_t_address = LLDB_INVALID_ADDRESS;
164 libdispatch_queue_t_address =
165 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
166 if (!error.Success()) {
167 libdispatch_queue_t_address = LLDB_INVALID_ADDRESS;
168 }
169 return libdispatch_queue_t_address;
170}
171
173 if (dispatch_queue_addr == LLDB_INVALID_ADDRESS || dispatch_queue_addr == 0)
174 return eQueueKindUnknown;
175
182 dispatch_queue_addr + m_libdispatch_offsets.dqo_width,
184 if (error.Success()) {
185 if (width == 1) {
186 kind = eQueueKindSerial;
187 }
188 if (width > 1) {
190 }
191 }
192 }
193 return kind;
194}
195
199 if (dict) {
202 dict->AddIntegerItem("plo_pthread_tsd_base_offset",
204 dict->AddIntegerItem(
205 "plo_pthread_tsd_base_address_offset",
207 dict->AddIntegerItem("plo_pthread_tsd_entry_size",
209 }
210
213 dict->AddIntegerItem("dti_queue_index",
215 dict->AddIntegerItem("dti_voucher_index",
217 dict->AddIntegerItem("dti_qos_class_index",
219 }
220 }
221}
222
224 if (thread_sp && thread_sp->GetFrameWithConcreteFrameIndex(0)) {
225 const SymbolContext sym_ctx(
226 thread_sp->GetFrameWithConcreteFrameIndex(0)->GetSymbolContext(
227 eSymbolContextSymbol));
228 static ConstString g_select_symbol("__select");
229 if (sym_ctx.GetFunctionName() == g_select_symbol) {
230 return false;
231 }
232 }
233 return true;
234}
235
239
240 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
241 return queue_id;
242
245 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a
246 // thread - deref it to get the address of the dispatch_queue_t structure
247 // for this thread's queue.
249 uint64_t dispatch_queue_addr =
250 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
251 if (error.Success()) {
252 addr_t serialnum_address =
253 dispatch_queue_addr + m_libdispatch_offsets.dqo_serialnum;
255 serialnum_address, m_libdispatch_offsets.dqo_serialnum_size,
257 if (error.Success()) {
258 queue_id = serialnum;
259 }
260 }
261 }
262
263 return queue_id;
264}
265
268 return;
269
270 static ConstString g_dispatch_queue_offsets_symbol_name(
271 "dispatch_queue_offsets");
272 const Symbol *dispatch_queue_offsets_symbol = nullptr;
273
274 // libdispatch symbols were in libSystem.B.dylib up through Mac OS X 10.6
275 // ("Snow Leopard")
276 ModuleSpec libSystem_module_spec(FileSpec("libSystem.B.dylib"));
278 libSystem_module_spec));
279 if (module_sp)
280 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
281 g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
282
283 // libdispatch symbols are in their own dylib as of Mac OS X 10.7 ("Lion")
284 // and later
285 if (dispatch_queue_offsets_symbol == nullptr) {
286 ModuleSpec libdispatch_module_spec(FileSpec("libdispatch.dylib"));
288 libdispatch_module_spec);
289 if (module_sp)
290 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
291 g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
292 }
293 if (dispatch_queue_offsets_symbol)
295 dispatch_queue_offsets_symbol->GetLoadAddress(&m_process->GetTarget());
296}
297
300 return;
301
303
304 uint8_t memory_buffer[sizeof(struct LibdispatchOffsets)];
305 DataExtractor data(memory_buffer, sizeof(memory_buffer),
308
311 sizeof(memory_buffer),
312 error) == sizeof(memory_buffer)) {
313 lldb::offset_t data_offset = 0;
314
315 // The struct LibdispatchOffsets is a series of uint16_t's - extract them
316 // all in one big go.
317 data.GetU16(&data_offset, &m_libdispatch_offsets.dqo_version,
318 sizeof(struct LibdispatchOffsets) / sizeof(uint16_t));
319 }
320}
321
324 return;
325
326 static ConstString g_libpthread_layout_offsets_symbol_name(
327 "pthread_layout_offsets");
328 const Symbol *libpthread_layout_offsets_symbol = nullptr;
329
330 ModuleSpec libpthread_module_spec(FileSpec("libsystem_pthread.dylib"));
332 libpthread_module_spec));
333 if (module_sp) {
334 libpthread_layout_offsets_symbol =
335 module_sp->FindFirstSymbolWithNameAndType(
336 g_libpthread_layout_offsets_symbol_name, eSymbolTypeData);
337 if (libpthread_layout_offsets_symbol) {
339 libpthread_layout_offsets_symbol->GetLoadAddress(
340 &m_process->GetTarget());
341 }
342 }
343}
344
347 return;
348
350
352 uint8_t memory_buffer[sizeof(struct LibpthreadOffsets)];
353 DataExtractor data(memory_buffer, sizeof(memory_buffer),
358 sizeof(memory_buffer),
359 error) == sizeof(memory_buffer)) {
360 lldb::offset_t data_offset = 0;
361
362 // The struct LibpthreadOffsets is a series of uint16_t's - extract them
363 // all in one big go.
364 data.GetU16(&data_offset, &m_libpthread_offsets.plo_version,
365 sizeof(struct LibpthreadOffsets) / sizeof(uint16_t));
366 }
367 }
368}
369
372 return;
373
374 static ConstString g_libdispatch_tsd_indexes_symbol_name(
375 "dispatch_tsd_indexes");
376 const Symbol *libdispatch_tsd_indexes_symbol = nullptr;
377
378 ModuleSpec libpthread_module_spec(FileSpec("libdispatch.dylib"));
380 libpthread_module_spec));
381 if (module_sp) {
382 libdispatch_tsd_indexes_symbol = module_sp->FindFirstSymbolWithNameAndType(
383 g_libdispatch_tsd_indexes_symbol_name, eSymbolTypeData);
384 if (libdispatch_tsd_indexes_symbol) {
386 libdispatch_tsd_indexes_symbol->GetLoadAddress(
387 &m_process->GetTarget());
388 }
389 }
390}
391
394 return;
395
397
399
400// We don't need to check the version number right now, it will be at least 2,
401// but keep this code around to fetch just the version # for the future where
402// we need to fetch alternate versions of the struct.
403#if 0
404 uint16_t dti_version = 2;
405 Address dti_struct_addr;
407 {
409 uint16_t version = m_process->GetTarget().ReadUnsignedIntegerFromMemory (dti_struct_addr, false, 2, UINT16_MAX, error);
410 if (error.Success() && dti_version != UINT16_MAX)
411 {
412 dti_version = version;
413 }
414 }
415#endif
416
417 TypeSystemClangSP scratch_ts_sp =
420 CompilerType uint16 =
421 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16);
422 CompilerType dispatch_tsd_indexes_s = scratch_ts_sp->CreateRecordType(
424 "__lldb_dispatch_tsd_indexes_s",
425 llvm::to_underlying(clang::TagTypeKind::Struct),
427
429 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
430 "dti_version", uint16,
432 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
433 "dti_queue_index", uint16,
435 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
436 "dti_voucher_index", uint16,
438 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
439 "dti_qos_class_index", uint16,
442
444 dispatch_tsd_indexes_s);
445
447 struct_reader.GetField<uint16_t>("dti_version");
449 struct_reader.GetField<uint16_t>("dti_queue_index");
451 struct_reader.GetField<uint16_t>("dti_voucher_index");
453 struct_reader.GetField<uint16_t>("dti_qos_class_index");
454 }
455 }
456}
457
459 ConstString type) {
460 ThreadSP originating_thread_sp;
461 if (BacktraceRecordingHeadersInitialized() && type == "libdispatch") {
463
464 // real_thread is either an actual, live thread (in which case we need to
465 // call into libBacktraceRecording to find its originator) or it is an
466 // extended backtrace itself, in which case we get the token from it and
467 // call into libBacktraceRecording to find the originator of that token.
468
469 if (real_thread->GetExtendedBacktraceToken() != LLDB_INVALID_ADDRESS) {
470 originating_thread_sp = GetExtendedBacktraceFromItemRef(
471 real_thread->GetExtendedBacktraceToken());
472 } else {
473 ThreadSP cur_thread_sp(
477 *cur_thread_sp.get(), real_thread->GetID(), m_page_to_free,
481 if (ret.item_buffer_ptr != 0 &&
483 ret.item_buffer_size > 0) {
484 DataBufferHeap data(ret.item_buffer_size, 0);
486 ret.item_buffer_size, error) &&
487 error.Success()) {
488 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
491 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
492 originating_thread_sp = std::make_shared<HistoryThread>(
494 originating_thread_sp->SetExtendedBacktraceToken(
496 originating_thread_sp->SetQueueName(
497 item.enqueuing_queue_label.c_str());
498 originating_thread_sp->SetQueueID(item.enqueuing_queue_serialnum);
499 // originating_thread_sp->SetThreadName
500 // (item.enqueuing_thread_label.c_str());
501 }
504 }
505 }
506 } else if (type == "Application Specific Backtrace") {
507 StructuredData::ObjectSP thread_extended_sp =
508 real_thread->GetExtendedInfo();
509
510 if (!thread_extended_sp)
511 return {};
512
513 StructuredData::Array *thread_extended_info =
514 thread_extended_sp->GetAsArray();
515
516 if (!thread_extended_info || !thread_extended_info->GetSize())
517 return {};
518
519 std::vector<addr_t> app_specific_backtrace_pcs;
520
521 auto extract_frame_pc =
522 [&app_specific_backtrace_pcs](StructuredData::Object *obj) -> bool {
523 if (!obj)
524 return false;
525
527 if (!dict)
528 return false;
529
531 if (!dict->GetValueForKeyAsInteger("pc", pc))
532 return false;
533
534 app_specific_backtrace_pcs.push_back(pc);
535
536 return pc != LLDB_INVALID_ADDRESS;
537 };
538
539 if (!thread_extended_info->ForEach(extract_frame_pc))
540 return {};
541
542 originating_thread_sp =
543 std::make_shared<HistoryThread>(*m_process, real_thread->GetIndexID(),
544 app_specific_backtrace_pcs, true);
545 originating_thread_sp->SetQueueName(type.AsCString());
546 }
547 return originating_thread_sp;
548}
549
552 ThreadSP return_thread_sp;
553
555 ThreadSP cur_thread_sp(
558 ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
560 error);
564 ret.item_buffer_size > 0) {
565 DataBufferHeap data(ret.item_buffer_size, 0);
567 ret.item_buffer_size, error) &&
568 error.Success()) {
569 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
572 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
573 return_thread_sp = std::make_shared<HistoryThread>(
575 return_thread_sp->SetExtendedBacktraceToken(item.item_that_enqueued_this);
576 return_thread_sp->SetQueueName(item.enqueuing_queue_label.c_str());
577 return_thread_sp->SetQueueID(item.enqueuing_queue_serialnum);
578 // return_thread_sp->SetThreadName
579 // (item.enqueuing_thread_label.c_str());
580
583 }
584 }
585 return return_thread_sp;
586}
587
590 ConstString type) {
591 ThreadSP extended_thread_sp;
592 if (type != "libdispatch")
593 return extended_thread_sp;
594
595 extended_thread_sp = std::make_shared<HistoryThread>(
596 *m_process, queue_item_sp->GetEnqueueingThreadID(),
597 queue_item_sp->GetEnqueueingBacktrace());
598 extended_thread_sp->SetExtendedBacktraceToken(
599 queue_item_sp->GetItemThatEnqueuedThis());
600 extended_thread_sp->SetQueueName(queue_item_sp->GetQueueLabel().c_str());
601 extended_thread_sp->SetQueueID(queue_item_sp->GetEnqueueingQueueID());
602 // extended_thread_sp->SetThreadName
603 // (queue_item_sp->GetThreadLabel().c_str());
604
605 return extended_thread_sp;
606}
607
608/* Returns true if we were able to get the version / offset information
609 * out of libBacktraceRecording. false means we were unable to retrieve
610 * this; the queue_info_version field will be 0.
611 */
612
615 return true;
616
617 addr_t queue_info_version_address = LLDB_INVALID_ADDRESS;
618 addr_t queue_info_data_offset_address = LLDB_INVALID_ADDRESS;
619 addr_t item_info_version_address = LLDB_INVALID_ADDRESS;
620 addr_t item_info_data_offset_address = LLDB_INVALID_ADDRESS;
621 Target &target = m_process->GetTarget();
622
623 static ConstString introspection_dispatch_queue_info_version(
624 "__introspection_dispatch_queue_info_version");
625 SymbolContextList sc_list;
627 introspection_dispatch_queue_info_version, eSymbolTypeData, sc_list);
628 if (!sc_list.IsEmpty()) {
629 SymbolContext sc;
630 sc_list.GetContextAtIndex(0, sc);
631 AddressRange addr_range;
632 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
633 queue_info_version_address =
634 addr_range.GetBaseAddress().GetLoadAddress(&target);
635 }
636 sc_list.Clear();
637
638 static ConstString introspection_dispatch_queue_info_data_offset(
639 "__introspection_dispatch_queue_info_data_offset");
641 introspection_dispatch_queue_info_data_offset, eSymbolTypeData, sc_list);
642 if (!sc_list.IsEmpty()) {
643 SymbolContext sc;
644 sc_list.GetContextAtIndex(0, sc);
645 AddressRange addr_range;
646 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
647 queue_info_data_offset_address =
648 addr_range.GetBaseAddress().GetLoadAddress(&target);
649 }
650 sc_list.Clear();
651
652 static ConstString introspection_dispatch_item_info_version(
653 "__introspection_dispatch_item_info_version");
655 introspection_dispatch_item_info_version, eSymbolTypeData, sc_list);
656 if (!sc_list.IsEmpty()) {
657 SymbolContext sc;
658 sc_list.GetContextAtIndex(0, sc);
659 AddressRange addr_range;
660 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
661 item_info_version_address =
662 addr_range.GetBaseAddress().GetLoadAddress(&target);
663 }
664 sc_list.Clear();
665
666 static ConstString introspection_dispatch_item_info_data_offset(
667 "__introspection_dispatch_item_info_data_offset");
669 introspection_dispatch_item_info_data_offset, eSymbolTypeData, sc_list);
670 if (!sc_list.IsEmpty()) {
671 SymbolContext sc;
672 sc_list.GetContextAtIndex(0, sc);
673 AddressRange addr_range;
674 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
675 item_info_data_offset_address =
676 addr_range.GetBaseAddress().GetLoadAddress(&target);
677 }
678
679 if (queue_info_version_address != LLDB_INVALID_ADDRESS &&
680 queue_info_data_offset_address != LLDB_INVALID_ADDRESS &&
681 item_info_version_address != LLDB_INVALID_ADDRESS &&
682 item_info_data_offset_address != LLDB_INVALID_ADDRESS) {
685 m_process->ReadUnsignedIntegerFromMemory(queue_info_version_address, 2,
686 0, error);
687 if (error.Success()) {
690 queue_info_data_offset_address, 2, 0, error);
691 if (error.Success()) {
693 m_process->ReadUnsignedIntegerFromMemory(item_info_version_address,
694 2, 0, error);
695 if (error.Success()) {
698 item_info_data_offset_address, 2, 0, error);
699 if (!error.Success()) {
701 }
702 } else {
704 }
705 } else {
707 }
708 }
709 }
710
712}
713
714const std::vector<ConstString> &
716 if (m_types.size() == 0) {
717 m_types.push_back(ConstString("libdispatch"));
718 m_types.push_back(ConstString("Application Specific Backtrace"));
719 // We could have pthread as another type in the future if we have a way of
720 // gathering that information & it's useful to distinguish between them.
721 }
722 return m_types;
723}
724
726 lldb_private::QueueList &queue_list) {
729 ThreadSP cur_thread_sp(
731 if (cur_thread_sp) {
733 queue_info_pointer = m_get_queues_handler.GetCurrentQueues(
734 *cur_thread_sp.get(), m_page_to_free, m_page_to_free_size, error);
737 if (error.Success()) {
738
739 if (queue_info_pointer.count > 0 &&
740 queue_info_pointer.queues_buffer_size > 0 &&
741 queue_info_pointer.queues_buffer_ptr != 0 &&
742 queue_info_pointer.queues_buffer_ptr != LLDB_INVALID_ADDRESS) {
744 queue_info_pointer.queues_buffer_size,
745 queue_info_pointer.count, queue_list);
746 }
747 }
748 }
749 }
750
751 // We either didn't have libBacktraceRecording (and need to create the queues
752 // list based on threads) or we did get the queues list from
753 // libBacktraceRecording but some special queues may not be included in its
754 // information. This is needed because libBacktraceRecording will only list
755 // queues with pending or running items by default - but the magic com.apple
756 // .main-thread queue on thread 1 is always around.
757
758 for (ThreadSP thread_sp : m_process->Threads()) {
759 if (thread_sp->GetAssociatedWithLibdispatchQueue() != eLazyBoolNo) {
760 if (thread_sp->GetQueueID() != LLDB_INVALID_QUEUE_ID) {
761 if (queue_list.FindQueueByID(thread_sp->GetQueueID()).get() ==
762 nullptr) {
763 QueueSP queue_sp(new Queue(m_process->shared_from_this(),
764 thread_sp->GetQueueID(),
765 thread_sp->GetQueueName()));
766 if (thread_sp->ThreadHasQueueInformation()) {
767 queue_sp->SetKind(thread_sp->GetQueueKind());
768 queue_sp->SetLibdispatchQueueAddress(
769 thread_sp->GetQueueLibdispatchQueueAddress());
770 queue_list.AddQueue(queue_sp);
771 } else {
772 queue_sp->SetKind(
773 GetQueueKind(thread_sp->GetQueueLibdispatchQueueAddress()));
774 queue_sp->SetLibdispatchQueueAddress(
775 thread_sp->GetQueueLibdispatchQueueAddress());
776 queue_list.AddQueue(queue_sp);
777 }
778 }
779 }
780 }
781 }
782}
783
784// Returns either an array of introspection_dispatch_item_info_ref's for the
785// pending items on a queue or an array introspection_dispatch_item_info_ref's
786// and code addresses for the pending items on a queue. The information about
787// each of these pending items then needs to be fetched individually by passing
788// the ref to libBacktraceRecording.
789
792 PendingItemsForQueue pending_item_refs = {};
794 ThreadSP cur_thread_sp(
796 if (cur_thread_sp) {
798 pending_items_pointer = m_get_pending_items_handler.GetPendingItems(
799 *cur_thread_sp.get(), queue, m_page_to_free, m_page_to_free_size,
800 error);
803 if (error.Success()) {
804 if (pending_items_pointer.count > 0 &&
805 pending_items_pointer.items_buffer_size > 0 &&
806 pending_items_pointer.items_buffer_ptr != 0 &&
807 pending_items_pointer.items_buffer_ptr != LLDB_INVALID_ADDRESS) {
808 DataBufferHeap data(pending_items_pointer.items_buffer_size, 0);
810 pending_items_pointer.items_buffer_ptr, data.GetBytes(),
811 pending_items_pointer.items_buffer_size, error)) {
812 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
815
816 // We either have an array of
817 // void* item_ref
818 // (old style) or we have a structure returned which looks like
819 //
820 // struct introspection_dispatch_pending_item_info_s {
821 // void *item_ref;
822 // void *function_or_block;
823 // };
824 //
825 // struct introspection_dispatch_pending_items_array_s {
826 // uint32_t version;
827 // uint32_t size_of_item_info;
828 // introspection_dispatch_pending_item_info_s items[];
829 // }
830
831 offset_t offset = 0;
832 uint64_t i = 0;
833 uint32_t version = extractor.GetU32(&offset);
834 if (version == 1) {
835 pending_item_refs.new_style = true;
836 uint32_t item_size = extractor.GetU32(&offset);
837 uint32_t start_of_array_offset = offset;
838 while (offset < pending_items_pointer.items_buffer_size &&
839 i < pending_items_pointer.count) {
840 offset = start_of_array_offset + (i * item_size);
842 item.item_ref = extractor.GetAddress(&offset);
843 item.code_address = extractor.GetAddress(&offset);
844 pending_item_refs.item_refs_and_code_addresses.push_back(item);
845 i++;
846 }
847 } else {
848 offset = 0;
849 pending_item_refs.new_style = false;
850 while (offset < pending_items_pointer.items_buffer_size &&
851 i < pending_items_pointer.count) {
853 item.item_ref = extractor.GetAddress(&offset);
855 pending_item_refs.item_refs_and_code_addresses.push_back(item);
856 i++;
857 }
858 }
859 }
860 m_page_to_free = pending_items_pointer.items_buffer_ptr;
861 m_page_to_free_size = pending_items_pointer.items_buffer_size;
862 }
863 }
864 }
865 return pending_item_refs;
866}
867
870 PendingItemsForQueue pending_item_refs =
872 for (ItemRefAndCodeAddress pending_item :
873 pending_item_refs.item_refs_and_code_addresses) {
874 Address addr;
876 addr);
877 QueueItemSP queue_item_sp(new QueueItem(queue->shared_from_this(),
878 m_process->shared_from_this(),
879 pending_item.item_ref, addr));
880 queue->PushPendingQueueItem(queue_item_sp);
881 }
882 }
883}
884
886 addr_t item_ref) {
888
889 ThreadSP cur_thread_sp(
892 ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
894 error);
898 ret.item_buffer_size > 0) {
899 DataBufferHeap data(ret.item_buffer_size, 0);
901 ret.item_buffer_size, error) &&
902 error.Success()) {
903 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
906 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
910 queue_item->SetStopID(item.stop_id);
912 queue_item->SetThreadLabel(item.enqueuing_thread_label);
913 queue_item->SetQueueLabel(item.enqueuing_queue_label);
914 queue_item->SetTargetQueueLabel(item.target_queue_label);
915 }
918 }
919}
920
922 lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count,
923 lldb_private::QueueList &queue_list) {
925 DataBufferHeap data(queues_buffer_size, 0);
926 Log *log = GetLog(LLDBLog::SystemRuntime);
927 if (m_process->ReadMemory(queues_buffer, data.GetBytes(), queues_buffer_size,
928 error) == queues_buffer_size &&
929 error.Success()) {
930 // We've read the information out of inferior memory; free it on the next
931 // call we make
932 m_page_to_free = queues_buffer;
933 m_page_to_free_size = queues_buffer_size;
934
935 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
938 offset_t offset = 0;
939 uint64_t queues_read = 0;
940
941 // The information about the queues is stored in this format (v1): typedef
942 // struct introspection_dispatch_queue_info_s {
943 // uint32_t offset_to_next;
944 // dispatch_queue_t queue;
945 // uint64_t serialnum; // queue's serialnum in the process, as
946 // provided by libdispatch
947 // uint32_t running_work_items_count;
948 // uint32_t pending_work_items_count;
949 //
950 // char data[]; // Starting here, we have variable-length data:
951 // // char queue_label[];
952 // } introspection_dispatch_queue_info_s;
953
954 while (queues_read < count && offset < queues_buffer_size) {
955 offset_t start_of_this_item = offset;
956
957 uint32_t offset_to_next = extractor.GetU32(&offset);
958
959 offset += 4; // Skip over the 4 bytes of reserved space
960 addr_t queue = extractor.GetAddress(&offset);
961 uint64_t serialnum = extractor.GetU64(&offset);
962 uint32_t running_work_items_count = extractor.GetU32(&offset);
963 uint32_t pending_work_items_count = extractor.GetU32(&offset);
964
965 // Read the first field of the variable length data
966 offset = start_of_this_item +
968 const char *queue_label = extractor.GetCStr(&offset);
969 if (queue_label == nullptr)
970 queue_label = "";
971
972 offset_t start_of_next_item = start_of_this_item + offset_to_next;
973 offset = start_of_next_item;
974
975 LLDB_LOGF(log,
976 "SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR added "
977 "queue with dispatch_queue_t 0x%" PRIx64
978 ", serial number 0x%" PRIx64
979 ", running items %d, pending items %d, name '%s'",
980 queue, serialnum, running_work_items_count,
981 pending_work_items_count, queue_label);
982
983 QueueSP queue_sp(
984 new Queue(m_process->shared_from_this(), serialnum, queue_label));
985 queue_sp->SetNumRunningWorkItems(running_work_items_count);
986 queue_sp->SetNumPendingWorkItems(pending_work_items_count);
987 queue_sp->SetLibdispatchQueueAddress(queue);
988 queue_sp->SetKind(GetQueueKind(queue));
989 queue_list.AddQueue(queue_sp);
990 queues_read++;
991 }
992 }
993}
994
996 lldb_private::DataExtractor &extractor) {
997 ItemInfo item;
998
999 offset_t offset = 0;
1000
1001 item.item_that_enqueued_this = extractor.GetAddress(&offset);
1002 item.function_or_block = extractor.GetAddress(&offset);
1003 item.enqueuing_thread_id = extractor.GetU64(&offset);
1004 item.enqueuing_queue_serialnum = extractor.GetU64(&offset);
1005 item.target_queue_serialnum = extractor.GetU64(&offset);
1006 item.enqueuing_callstack_frame_count = extractor.GetU32(&offset);
1007 item.stop_id = extractor.GetU32(&offset);
1008
1010
1011 for (uint32_t i = 0; i < item.enqueuing_callstack_frame_count; i++) {
1012 item.enqueuing_callstack.push_back(extractor.GetAddress(&offset));
1013 }
1014 item.enqueuing_thread_label = extractor.GetCStr(&offset);
1015 item.enqueuing_queue_label = extractor.GetCStr(&offset);
1016 item.target_queue_label = extractor.GetCStr(&offset);
1017
1018 return item;
1019}
1020
1024 "System runtime plugin for Mac OS X native libraries.", CreateInstance);
1025}
1026
1029}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:349
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
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:209
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:1184
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:2213
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:2005
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:1939
lldb::ByteOrder GetByteOrder() const
Definition: Process.cpp:3400
ThreadList::ThreadIterable Threads()
Definition: Process.h:2226
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:2080
Status ClearBreakpointSiteByID(lldb::user_id_t break_id)
Definition: Process.cpp:1592
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition: Process.cpp:2102
virtual bool IsAlive()
Check if a process is still alive.
Definition: Process.cpp:1093
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3404
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1277
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:545
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:3111
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:972
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:2117
lldb::ThreadSP GetExpressionExecutionThread()
Definition: ThreadList.cpp:60
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.
Definition: SBAttachInfo.h:14
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:314
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Queue > QueueSP
Definition: lldb-forward.h:390
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:438
uint64_t offset_t
Definition: lldb-types.h:83
@ 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:458
uint64_t addr_t
Definition: lldb-types.h:79
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:365
uint64_t queue_id_t
Definition: lldb-types.h:88
std::shared_ptr< lldb_private::QueueItem > QueueItemSP
Definition: lldb-forward.h:392
std::vector< lldb::addr_t > enqueuing_callstack
std::vector< ItemRefAndCodeAddress > item_refs_and_code_addresses