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