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 // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS:
65 create = triple_ref.getVendor() == llvm::Triple::Apple;
66 break;
67 default:
68 create = false;
69 break;
70 }
71 }
72 }
73
74 if (create)
75 return new SystemRuntimeMacOSX(process);
76 return nullptr;
77}
78
79// Constructor
81 : SystemRuntime(process), m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
82 m_get_queues_handler(process), m_get_pending_items_handler(process),
83 m_get_item_info_handler(process), m_get_thread_item_info_handler(process),
84 m_page_to_free(LLDB_INVALID_ADDRESS), m_page_to_free_size(0),
85 m_lib_backtrace_recording_info(),
86 m_dispatch_queue_offsets_addr(LLDB_INVALID_ADDRESS),
87 m_libdispatch_offsets(),
88 m_libpthread_layout_offsets_addr(LLDB_INVALID_ADDRESS),
89 m_libpthread_offsets(), m_dispatch_tsd_indexes_addr(LLDB_INVALID_ADDRESS),
90 m_libdispatch_tsd_indexes(),
91 m_dispatch_voucher_offsets_addr(LLDB_INVALID_ADDRESS),
92 m_libdispatch_voucher_offsets() {}
93
94// Destructor
96
102}
103
104// Clear out the state of this class.
105void SystemRuntimeMacOSX::Clear(bool clear_process) {
106 std::lock_guard<std::recursive_mutex> guard(m_mutex);
107
110
111 if (clear_process)
112 m_process = nullptr;
114}
115
116std::string
118 std::string dispatch_queue_name;
119 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
120 return "";
121
124 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a
125 // thread - deref it to get the address of the dispatch_queue_t structure
126 // for this thread's queue.
128 addr_t dispatch_queue_addr =
129 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
130 if (error.Success()) {
132 // libdispatch versions 4+, pointer to dispatch name is in the queue
133 // structure.
134 addr_t pointer_to_label_address =
135 dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
136 addr_t label_addr =
137 m_process->ReadPointerFromMemory(pointer_to_label_address, error);
138 if (error.Success()) {
139 m_process->ReadCStringFromMemory(label_addr, dispatch_queue_name,
140 error);
141 }
142 } else {
143 // libdispatch versions 1-3, dispatch name is a fixed width char array
144 // in the queue structure.
145 addr_t label_addr =
146 dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
147 dispatch_queue_name.resize(m_libdispatch_offsets.dqo_label_size, '\0');
148 size_t bytes_read =
149 m_process->ReadMemory(label_addr, &dispatch_queue_name[0],
151 if (bytes_read < m_libdispatch_offsets.dqo_label_size)
152 dispatch_queue_name.erase(bytes_read);
153 }
154 }
155 }
156 return dispatch_queue_name;
157}
158
160 addr_t dispatch_qaddr) {
161 addr_t libdispatch_queue_t_address = LLDB_INVALID_ADDRESS;
163 libdispatch_queue_t_address =
164 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
165 if (!error.Success()) {
166 libdispatch_queue_t_address = LLDB_INVALID_ADDRESS;
167 }
168 return libdispatch_queue_t_address;
169}
170
172 if (dispatch_queue_addr == LLDB_INVALID_ADDRESS || dispatch_queue_addr == 0)
173 return eQueueKindUnknown;
174
181 dispatch_queue_addr + m_libdispatch_offsets.dqo_width,
183 if (error.Success()) {
184 if (width == 1) {
185 kind = eQueueKindSerial;
186 }
187 if (width > 1) {
189 }
190 }
191 }
192 return kind;
193}
194
198 if (dict) {
201 dict->AddIntegerItem("plo_pthread_tsd_base_offset",
203 dict->AddIntegerItem(
204 "plo_pthread_tsd_base_address_offset",
206 dict->AddIntegerItem("plo_pthread_tsd_entry_size",
208 }
209
212 dict->AddIntegerItem("dti_queue_index",
214 dict->AddIntegerItem("dti_voucher_index",
216 dict->AddIntegerItem("dti_qos_class_index",
218 }
219 }
220}
221
223 if (thread_sp && thread_sp->GetFrameWithConcreteFrameIndex(0)) {
224 const SymbolContext sym_ctx(
225 thread_sp->GetFrameWithConcreteFrameIndex(0)->GetSymbolContext(
226 eSymbolContextSymbol));
227 static ConstString g_select_symbol("__select");
228 if (sym_ctx.GetFunctionName() == g_select_symbol) {
229 return false;
230 }
231 }
232 return true;
233}
234
238
239 if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
240 return queue_id;
241
244 // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a
245 // thread - deref it to get the address of the dispatch_queue_t structure
246 // for this thread's queue.
248 uint64_t dispatch_queue_addr =
249 m_process->ReadPointerFromMemory(dispatch_qaddr, error);
250 if (error.Success()) {
251 addr_t serialnum_address =
252 dispatch_queue_addr + m_libdispatch_offsets.dqo_serialnum;
254 serialnum_address, m_libdispatch_offsets.dqo_serialnum_size,
256 if (error.Success()) {
257 queue_id = serialnum;
258 }
259 }
260 }
261
262 return queue_id;
263}
264
267 return;
268
269 static ConstString g_dispatch_queue_offsets_symbol_name(
270 "dispatch_queue_offsets");
271 const Symbol *dispatch_queue_offsets_symbol = nullptr;
272
273 // libdispatch symbols were in libSystem.B.dylib up through Mac OS X 10.6
274 // ("Snow Leopard")
275 ModuleSpec libSystem_module_spec(FileSpec("libSystem.B.dylib"));
276 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
277 libSystem_module_spec));
278 if (module_sp)
279 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
280 g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
281
282 // libdispatch symbols are in their own dylib as of Mac OS X 10.7 ("Lion")
283 // and later
284 if (dispatch_queue_offsets_symbol == nullptr) {
285 ModuleSpec libdispatch_module_spec(FileSpec("libdispatch.dylib"));
287 libdispatch_module_spec);
288 if (module_sp)
289 dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
290 g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
291 }
292 if (dispatch_queue_offsets_symbol)
294 dispatch_queue_offsets_symbol->GetLoadAddress(&m_process->GetTarget());
295}
296
299 return;
300
302
303 uint8_t memory_buffer[sizeof(struct LibdispatchOffsets)];
304 DataExtractor data(memory_buffer, sizeof(memory_buffer),
307
310 sizeof(memory_buffer),
311 error) == sizeof(memory_buffer)) {
312 lldb::offset_t data_offset = 0;
313
314 // The struct LibdispatchOffsets is a series of uint16_t's - extract them
315 // all in one big go.
316 data.GetU16(&data_offset, &m_libdispatch_offsets.dqo_version,
317 sizeof(struct LibdispatchOffsets) / sizeof(uint16_t));
318 }
319}
320
323 return;
324
325 static ConstString g_libpthread_layout_offsets_symbol_name(
326 "pthread_layout_offsets");
327 const Symbol *libpthread_layout_offsets_symbol = nullptr;
328
329 ModuleSpec libpthread_module_spec(FileSpec("libsystem_pthread.dylib"));
330 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
331 libpthread_module_spec));
332 if (module_sp) {
333 libpthread_layout_offsets_symbol =
334 module_sp->FindFirstSymbolWithNameAndType(
335 g_libpthread_layout_offsets_symbol_name, eSymbolTypeData);
336 if (libpthread_layout_offsets_symbol) {
338 libpthread_layout_offsets_symbol->GetLoadAddress(
339 &m_process->GetTarget());
340 }
341 }
342}
343
346 return;
347
349
351 uint8_t memory_buffer[sizeof(struct LibpthreadOffsets)];
352 DataExtractor data(memory_buffer, sizeof(memory_buffer),
357 sizeof(memory_buffer),
358 error) == sizeof(memory_buffer)) {
359 lldb::offset_t data_offset = 0;
360
361 // The struct LibpthreadOffsets is a series of uint16_t's - extract them
362 // all in one big go.
363 data.GetU16(&data_offset, &m_libpthread_offsets.plo_version,
364 sizeof(struct LibpthreadOffsets) / sizeof(uint16_t));
365 }
366 }
367}
368
371 return;
372
373 static ConstString g_libdispatch_tsd_indexes_symbol_name(
374 "dispatch_tsd_indexes");
375 const Symbol *libdispatch_tsd_indexes_symbol = nullptr;
376
377 ModuleSpec libpthread_module_spec(FileSpec("libdispatch.dylib"));
378 ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
379 libpthread_module_spec));
380 if (module_sp) {
381 libdispatch_tsd_indexes_symbol = module_sp->FindFirstSymbolWithNameAndType(
382 g_libdispatch_tsd_indexes_symbol_name, eSymbolTypeData);
383 if (libdispatch_tsd_indexes_symbol) {
385 libdispatch_tsd_indexes_symbol->GetLoadAddress(
386 &m_process->GetTarget());
387 }
388 }
389}
390
393 return;
394
396
398
399// We don't need to check the version number right now, it will be at least 2,
400// but keep this code around to fetch just the version # for the future where
401// we need to fetch alternate versions of the struct.
402#if 0
403 uint16_t dti_version = 2;
404 Address dti_struct_addr;
406 {
408 uint16_t version = m_process->GetTarget().ReadUnsignedIntegerFromMemory (dti_struct_addr, false, 2, UINT16_MAX, error);
409 if (error.Success() && dti_version != UINT16_MAX)
410 {
411 dti_version = version;
412 }
413 }
414#endif
415
416 TypeSystemClangSP scratch_ts_sp =
419 CompilerType uint16 =
420 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16);
421 CompilerType dispatch_tsd_indexes_s = scratch_ts_sp->CreateRecordType(
423 "__lldb_dispatch_tsd_indexes_s", clang::TTK_Struct,
425
427 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
428 "dti_version", uint16,
430 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
431 "dti_queue_index", uint16,
433 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
434 "dti_voucher_index", uint16,
436 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
437 "dti_qos_class_index", uint16,
440
442 dispatch_tsd_indexes_s);
443
445 struct_reader.GetField<uint16_t>(ConstString("dti_version"));
447 struct_reader.GetField<uint16_t>(ConstString("dti_queue_index"));
449 struct_reader.GetField<uint16_t>(ConstString("dti_voucher_index"));
451 struct_reader.GetField<uint16_t>(ConstString("dti_qos_class_index"));
452 }
453 }
454}
455
457 ConstString type) {
458 ThreadSP originating_thread_sp;
459 if (BacktraceRecordingHeadersInitialized() && type == "libdispatch") {
461
462 // real_thread is either an actual, live thread (in which case we need to
463 // call into libBacktraceRecording to find its originator) or it is an
464 // extended backtrace itself, in which case we get the token from it and
465 // call into libBacktraceRecording to find the originator of that token.
466
467 if (real_thread->GetExtendedBacktraceToken() != LLDB_INVALID_ADDRESS) {
468 originating_thread_sp = GetExtendedBacktraceFromItemRef(
469 real_thread->GetExtendedBacktraceToken());
470 } else {
471 ThreadSP cur_thread_sp(
475 *cur_thread_sp.get(), real_thread->GetID(), m_page_to_free,
479 if (ret.item_buffer_ptr != 0 &&
481 ret.item_buffer_size > 0) {
482 DataBufferHeap data(ret.item_buffer_size, 0);
483 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
484 ret.item_buffer_size, error) &&
485 error.Success()) {
486 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
489 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
490 originating_thread_sp = std::make_shared<HistoryThread>(
492 originating_thread_sp->SetExtendedBacktraceToken(
494 originating_thread_sp->SetQueueName(
495 item.enqueuing_queue_label.c_str());
496 originating_thread_sp->SetQueueID(item.enqueuing_queue_serialnum);
497 // originating_thread_sp->SetThreadName
498 // (item.enqueuing_thread_label.c_str());
499 }
502 }
503 }
504 } else if (type == "Application Specific Backtrace") {
505 StructuredData::ObjectSP thread_extended_sp =
506 real_thread->GetExtendedInfo();
507
508 if (!thread_extended_sp)
509 return {};
510
511 StructuredData::Array *thread_extended_info =
512 thread_extended_sp->GetAsArray();
513
514 if (!thread_extended_info || !thread_extended_info->GetSize())
515 return {};
516
517 std::vector<addr_t> app_specific_backtrace_pcs;
518
519 auto extract_frame_pc =
520 [&app_specific_backtrace_pcs](StructuredData::Object *obj) -> bool {
521 if (!obj)
522 return false;
523
525 if (!dict)
526 return false;
527
529 if (!dict->GetValueForKeyAsInteger("pc", pc))
530 return false;
531
532 app_specific_backtrace_pcs.push_back(pc);
533
534 return pc != LLDB_INVALID_ADDRESS;
535 };
536
537 if (!thread_extended_info->ForEach(extract_frame_pc))
538 return {};
539
540 originating_thread_sp =
541 std::make_shared<HistoryThread>(*m_process, real_thread->GetIndexID(),
542 app_specific_backtrace_pcs, true);
543 originating_thread_sp->SetQueueName(type.AsCString());
544 }
545 return originating_thread_sp;
546}
547
548ThreadSP
550 ThreadSP return_thread_sp;
551
553 ThreadSP cur_thread_sp(
556 ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
558 error);
562 ret.item_buffer_size > 0) {
563 DataBufferHeap data(ret.item_buffer_size, 0);
564 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
565 ret.item_buffer_size, error) &&
566 error.Success()) {
567 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
570 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
571 return_thread_sp = std::make_shared<HistoryThread>(
573 return_thread_sp->SetExtendedBacktraceToken(item.item_that_enqueued_this);
574 return_thread_sp->SetQueueName(item.enqueuing_queue_label.c_str());
575 return_thread_sp->SetQueueID(item.enqueuing_queue_serialnum);
576 // return_thread_sp->SetThreadName
577 // (item.enqueuing_thread_label.c_str());
578
581 }
582 }
583 return return_thread_sp;
584}
585
586ThreadSP
588 ConstString type) {
589 ThreadSP extended_thread_sp;
590 if (type != "libdispatch")
591 return extended_thread_sp;
592
593 extended_thread_sp = std::make_shared<HistoryThread>(
594 *m_process, queue_item_sp->GetEnqueueingThreadID(),
595 queue_item_sp->GetEnqueueingBacktrace());
596 extended_thread_sp->SetExtendedBacktraceToken(
597 queue_item_sp->GetItemThatEnqueuedThis());
598 extended_thread_sp->SetQueueName(queue_item_sp->GetQueueLabel().c_str());
599 extended_thread_sp->SetQueueID(queue_item_sp->GetEnqueueingQueueID());
600 // extended_thread_sp->SetThreadName
601 // (queue_item_sp->GetThreadLabel().c_str());
602
603 return extended_thread_sp;
604}
605
606/* Returns true if we were able to get the version / offset information
607 * out of libBacktraceRecording. false means we were unable to retrieve
608 * this; the queue_info_version field will be 0.
609 */
610
613 return true;
614
615 addr_t queue_info_version_address = LLDB_INVALID_ADDRESS;
616 addr_t queue_info_data_offset_address = LLDB_INVALID_ADDRESS;
617 addr_t item_info_version_address = LLDB_INVALID_ADDRESS;
618 addr_t item_info_data_offset_address = LLDB_INVALID_ADDRESS;
619 Target &target = m_process->GetTarget();
620
621 static ConstString introspection_dispatch_queue_info_version(
622 "__introspection_dispatch_queue_info_version");
623 SymbolContextList sc_list;
625 introspection_dispatch_queue_info_version, eSymbolTypeData, sc_list);
626 if (!sc_list.IsEmpty()) {
627 SymbolContext sc;
628 sc_list.GetContextAtIndex(0, sc);
629 AddressRange addr_range;
630 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
631 queue_info_version_address =
632 addr_range.GetBaseAddress().GetLoadAddress(&target);
633 }
634 sc_list.Clear();
635
636 static ConstString introspection_dispatch_queue_info_data_offset(
637 "__introspection_dispatch_queue_info_data_offset");
639 introspection_dispatch_queue_info_data_offset, eSymbolTypeData, sc_list);
640 if (!sc_list.IsEmpty()) {
641 SymbolContext sc;
642 sc_list.GetContextAtIndex(0, sc);
643 AddressRange addr_range;
644 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
645 queue_info_data_offset_address =
646 addr_range.GetBaseAddress().GetLoadAddress(&target);
647 }
648 sc_list.Clear();
649
650 static ConstString introspection_dispatch_item_info_version(
651 "__introspection_dispatch_item_info_version");
653 introspection_dispatch_item_info_version, eSymbolTypeData, sc_list);
654 if (!sc_list.IsEmpty()) {
655 SymbolContext sc;
656 sc_list.GetContextAtIndex(0, sc);
657 AddressRange addr_range;
658 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
659 item_info_version_address =
660 addr_range.GetBaseAddress().GetLoadAddress(&target);
661 }
662 sc_list.Clear();
663
664 static ConstString introspection_dispatch_item_info_data_offset(
665 "__introspection_dispatch_item_info_data_offset");
667 introspection_dispatch_item_info_data_offset, eSymbolTypeData, sc_list);
668 if (!sc_list.IsEmpty()) {
669 SymbolContext sc;
670 sc_list.GetContextAtIndex(0, sc);
671 AddressRange addr_range;
672 sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
673 item_info_data_offset_address =
674 addr_range.GetBaseAddress().GetLoadAddress(&target);
675 }
676
677 if (queue_info_version_address != LLDB_INVALID_ADDRESS &&
678 queue_info_data_offset_address != LLDB_INVALID_ADDRESS &&
679 item_info_version_address != LLDB_INVALID_ADDRESS &&
680 item_info_data_offset_address != LLDB_INVALID_ADDRESS) {
683 m_process->ReadUnsignedIntegerFromMemory(queue_info_version_address, 2,
684 0, error);
685 if (error.Success()) {
688 queue_info_data_offset_address, 2, 0, error);
689 if (error.Success()) {
691 m_process->ReadUnsignedIntegerFromMemory(item_info_version_address,
692 2, 0, error);
693 if (error.Success()) {
696 item_info_data_offset_address, 2, 0, error);
697 if (!error.Success()) {
699 }
700 } else {
702 }
703 } else {
705 }
706 }
707 }
708
710}
711
712const std::vector<ConstString> &
714 if (m_types.size() == 0) {
715 m_types.push_back(ConstString("libdispatch"));
716 m_types.push_back(ConstString("Application Specific Backtrace"));
717 // We could have pthread as another type in the future if we have a way of
718 // gathering that information & it's useful to distinguish between them.
719 }
720 return m_types;
721}
722
724 lldb_private::QueueList &queue_list) {
727 ThreadSP cur_thread_sp(
729 if (cur_thread_sp) {
731 queue_info_pointer = m_get_queues_handler.GetCurrentQueues(
732 *cur_thread_sp.get(), m_page_to_free, m_page_to_free_size, error);
735 if (error.Success()) {
736
737 if (queue_info_pointer.count > 0 &&
738 queue_info_pointer.queues_buffer_size > 0 &&
739 queue_info_pointer.queues_buffer_ptr != 0 &&
740 queue_info_pointer.queues_buffer_ptr != LLDB_INVALID_ADDRESS) {
742 queue_info_pointer.queues_buffer_size,
743 queue_info_pointer.count, queue_list);
744 }
745 }
746 }
747 }
748
749 // We either didn't have libBacktraceRecording (and need to create the queues
750 // list based on threads) or we did get the queues list from
751 // libBacktraceRecording but some special queues may not be included in its
752 // information. This is needed because libBacktraceRecording will only list
753 // queues with pending or running items by default - but the magic com.apple
754 // .main-thread queue on thread 1 is always around.
755
756 for (ThreadSP thread_sp : m_process->Threads()) {
757 if (thread_sp->GetAssociatedWithLibdispatchQueue() != eLazyBoolNo) {
758 if (thread_sp->GetQueueID() != LLDB_INVALID_QUEUE_ID) {
759 if (queue_list.FindQueueByID(thread_sp->GetQueueID()).get() ==
760 nullptr) {
761 QueueSP queue_sp(new Queue(m_process->shared_from_this(),
762 thread_sp->GetQueueID(),
763 thread_sp->GetQueueName()));
764 if (thread_sp->ThreadHasQueueInformation()) {
765 queue_sp->SetKind(thread_sp->GetQueueKind());
766 queue_sp->SetLibdispatchQueueAddress(
767 thread_sp->GetQueueLibdispatchQueueAddress());
768 queue_list.AddQueue(queue_sp);
769 } else {
770 queue_sp->SetKind(
771 GetQueueKind(thread_sp->GetQueueLibdispatchQueueAddress()));
772 queue_sp->SetLibdispatchQueueAddress(
773 thread_sp->GetQueueLibdispatchQueueAddress());
774 queue_list.AddQueue(queue_sp);
775 }
776 }
777 }
778 }
779 }
780}
781
782// Returns either an array of introspection_dispatch_item_info_ref's for the
783// pending items on a queue or an array introspection_dispatch_item_info_ref's
784// and code addresses for the pending items on a queue. The information about
785// each of these pending items then needs to be fetched individually by passing
786// the ref to libBacktraceRecording.
787
790 PendingItemsForQueue pending_item_refs = {};
792 ThreadSP cur_thread_sp(
794 if (cur_thread_sp) {
796 pending_items_pointer = m_get_pending_items_handler.GetPendingItems(
797 *cur_thread_sp.get(), queue, m_page_to_free, m_page_to_free_size,
798 error);
801 if (error.Success()) {
802 if (pending_items_pointer.count > 0 &&
803 pending_items_pointer.items_buffer_size > 0 &&
804 pending_items_pointer.items_buffer_ptr != 0 &&
805 pending_items_pointer.items_buffer_ptr != LLDB_INVALID_ADDRESS) {
806 DataBufferHeap data(pending_items_pointer.items_buffer_size, 0);
808 pending_items_pointer.items_buffer_ptr, data.GetBytes(),
809 pending_items_pointer.items_buffer_size, error)) {
810 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
813
814 // We either have an array of
815 // void* item_ref
816 // (old style) or we have a structure returned which looks like
817 //
818 // struct introspection_dispatch_pending_item_info_s {
819 // void *item_ref;
820 // void *function_or_block;
821 // };
822 //
823 // struct introspection_dispatch_pending_items_array_s {
824 // uint32_t version;
825 // uint32_t size_of_item_info;
826 // introspection_dispatch_pending_item_info_s items[];
827 // }
828
829 offset_t offset = 0;
830 uint64_t i = 0;
831 uint32_t version = extractor.GetU32(&offset);
832 if (version == 1) {
833 pending_item_refs.new_style = true;
834 uint32_t item_size = extractor.GetU32(&offset);
835 uint32_t start_of_array_offset = offset;
836 while (offset < pending_items_pointer.items_buffer_size &&
837 i < pending_items_pointer.count) {
838 offset = start_of_array_offset + (i * item_size);
840 item.item_ref = extractor.GetAddress(&offset);
841 item.code_address = extractor.GetAddress(&offset);
842 pending_item_refs.item_refs_and_code_addresses.push_back(item);
843 i++;
844 }
845 } else {
846 offset = 0;
847 pending_item_refs.new_style = false;
848 while (offset < pending_items_pointer.items_buffer_size &&
849 i < pending_items_pointer.count) {
851 item.item_ref = extractor.GetAddress(&offset);
853 pending_item_refs.item_refs_and_code_addresses.push_back(item);
854 i++;
855 }
856 }
857 }
858 m_page_to_free = pending_items_pointer.items_buffer_ptr;
859 m_page_to_free_size = pending_items_pointer.items_buffer_size;
860 }
861 }
862 }
863 return pending_item_refs;
864}
865
868 PendingItemsForQueue pending_item_refs =
870 for (ItemRefAndCodeAddress pending_item :
871 pending_item_refs.item_refs_and_code_addresses) {
872 Address addr;
874 addr);
875 QueueItemSP queue_item_sp(new QueueItem(queue->shared_from_this(),
876 m_process->shared_from_this(),
877 pending_item.item_ref, addr));
878 queue->PushPendingQueueItem(queue_item_sp);
879 }
880 }
881}
882
884 addr_t item_ref) {
886
887 ThreadSP cur_thread_sp(
890 ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
892 error);
896 ret.item_buffer_size > 0) {
897 DataBufferHeap data(ret.item_buffer_size, 0);
898 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
899 ret.item_buffer_size, error) &&
900 error.Success()) {
901 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
904 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
908 queue_item->SetStopID(item.stop_id);
910 queue_item->SetThreadLabel(item.enqueuing_thread_label);
911 queue_item->SetQueueLabel(item.enqueuing_queue_label);
912 queue_item->SetTargetQueueLabel(item.target_queue_label);
913 }
916 }
917}
918
920 lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count,
921 lldb_private::QueueList &queue_list) {
923 DataBufferHeap data(queues_buffer_size, 0);
924 Log *log = GetLog(LLDBLog::SystemRuntime);
925 if (m_process->ReadMemory(queues_buffer, data.GetBytes(), queues_buffer_size,
926 error) == queues_buffer_size &&
927 error.Success()) {
928 // We've read the information out of inferior memory; free it on the next
929 // call we make
930 m_page_to_free = queues_buffer;
931 m_page_to_free_size = queues_buffer_size;
932
933 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
936 offset_t offset = 0;
937 uint64_t queues_read = 0;
938
939 // The information about the queues is stored in this format (v1): typedef
940 // struct introspection_dispatch_queue_info_s {
941 // uint32_t offset_to_next;
942 // dispatch_queue_t queue;
943 // uint64_t serialnum; // queue's serialnum in the process, as
944 // provided by libdispatch
945 // uint32_t running_work_items_count;
946 // uint32_t pending_work_items_count;
947 //
948 // char data[]; // Starting here, we have variable-length data:
949 // // char queue_label[];
950 // } introspection_dispatch_queue_info_s;
951
952 while (queues_read < count && offset < queues_buffer_size) {
953 offset_t start_of_this_item = offset;
954
955 uint32_t offset_to_next = extractor.GetU32(&offset);
956
957 offset += 4; // Skip over the 4 bytes of reserved space
958 addr_t queue = extractor.GetAddress(&offset);
959 uint64_t serialnum = extractor.GetU64(&offset);
960 uint32_t running_work_items_count = extractor.GetU32(&offset);
961 uint32_t pending_work_items_count = extractor.GetU32(&offset);
962
963 // Read the first field of the variable length data
964 offset = start_of_this_item +
966 const char *queue_label = extractor.GetCStr(&offset);
967 if (queue_label == nullptr)
968 queue_label = "";
969
970 offset_t start_of_next_item = start_of_this_item + offset_to_next;
971 offset = start_of_next_item;
972
973 LLDB_LOGF(log,
974 "SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR added "
975 "queue with dispatch_queue_t 0x%" PRIx64
976 ", serial number 0x%" PRIx64
977 ", running items %d, pending items %d, name '%s'",
978 queue, serialnum, running_work_items_count,
979 pending_work_items_count, queue_label);
980
981 QueueSP queue_sp(
982 new Queue(m_process->shared_from_this(), serialnum, queue_label));
983 queue_sp->SetNumRunningWorkItems(running_work_items_count);
984 queue_sp->SetNumPendingWorkItems(pending_work_items_count);
985 queue_sp->SetLibdispatchQueueAddress(queue);
986 queue_sp->SetKind(GetQueueKind(queue));
987 queue_list.AddQueue(queue_sp);
988 queues_read++;
989 }
990 }
991}
992
994 lldb_private::DataExtractor &extractor) {
995 ItemInfo item;
996
997 offset_t offset = 0;
998
999 item.item_that_enqueued_this = extractor.GetAddress(&offset);
1000 item.function_or_block = extractor.GetAddress(&offset);
1001 item.enqueuing_thread_id = extractor.GetU64(&offset);
1002 item.enqueuing_queue_serialnum = extractor.GetU64(&offset);
1003 item.target_queue_serialnum = extractor.GetU64(&offset);
1004 item.enqueuing_callstack_frame_count = extractor.GetU32(&offset);
1005 item.stop_id = extractor.GetU32(&offset);
1006
1008
1009 for (uint32_t i = 0; i < item.enqueuing_callstack_frame_count; i++) {
1010 item.enqueuing_callstack.push_back(extractor.GetAddress(&offset));
1011 }
1012 item.enqueuing_thread_label = extractor.GetCStr(&offset);
1013 item.enqueuing_queue_label = extractor.GetCStr(&offset);
1014 item.target_queue_label = extractor.GetCStr(&offset);
1015
1016 return item;
1017}
1018
1022 "System runtime plugin for Mac OS X native libraries.", CreateInstance);
1023}
1024
1027}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:344
#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:59
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:311
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:198
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
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:615
void FindSymbolsWithNameAndType(ConstString name, lldb::SymbolType symbol_type, SymbolContextList &sc_list) const
Definition: ModuleList.cpp:501
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:1230
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:43
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(ConstString name, RetType fail_value=RetType())
A plug-in interface definition class for debugging a process.
Definition: Process.h:335
ThreadList & GetThreadList()
Definition: Process.h:2126
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:2007
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:1941
lldb::ByteOrder GetByteOrder() const
Definition: Process.cpp:3400
ThreadList::ThreadIterable Threads()
Definition: Process.h:2134
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:2082
Status ClearBreakpointSiteByID(lldb::user_id_t break_id)
Definition: Process.cpp:1596
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition: Process.cpp:2104
virtual bool IsAlive()
Check if a process is still alive.
Definition: Process.cpp:1102
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3404
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1224
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:33
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:537
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:3009
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:954
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:2071
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)
#define LLDB_INVALID_QUEUE_ID
Definition: lldb-defines.h:88
#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:74
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:309
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:83
@ eLanguageTypeC
Non-standardized C, such as K&R.
@ eEncodingUint
unsigned integer
QueueKind
Queue type.
@ eQueueKindUnknown
@ eQueueKindConcurrent
@ eQueueKindSerial
uint64_t addr_t
Definition: lldb-types.h:79
uint64_t queue_id_t
Definition: lldb-types.h:87
std::vector< lldb::addr_t > enqueuing_callstack
std::vector< ItemRefAndCodeAddress > item_refs_and_code_addresses