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
203 StructuredData::Dictionary *dict = dict_sp->GetAsDictionary();
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(
428 nullptr, OptionalClangModuleID(), "__lldb_dispatch_tsd_indexes_s",
429 llvm::to_underlying(clang::TagTypeKind::Struct),
431
433 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
434 "dti_version", uint16, 0);
435 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
436 "dti_queue_index", uint16, 0);
437 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
438 "dti_voucher_index", uint16, 0);
439 TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
440 "dti_qos_class_index", uint16, 0);
442
444 dispatch_tsd_indexes_s);
445
446 m_libdispatch_tsd_indexes.dti_version =
447 struct_reader.GetField<uint16_t>("dti_version");
448 m_libdispatch_tsd_indexes.dti_queue_index =
449 struct_reader.GetField<uint16_t>("dti_queue_index");
450 m_libdispatch_tsd_indexes.dti_voucher_index =
451 struct_reader.GetField<uint16_t>("dti_voucher_index");
452 m_libdispatch_tsd_indexes.dti_qos_class_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(
474 m_process->GetThreadList().GetExpressionExecutionThread());
476 m_get_thread_item_info_handler.GetThreadItemInfo(
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);
485 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
486 ret.item_buffer_size, error) &&
487 error.Success()) {
488 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
489 m_process->GetByteOrder(),
490 m_process->GetAddressByteSize());
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 = std::make_shared<HistoryThread>(
543 *m_process, real_thread->GetIndexID(), app_specific_backtrace_pcs,
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(
556 m_process->GetThreadList().GetExpressionExecutionThread());
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);
566 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
567 ret.item_buffer_size, error) &&
568 error.Success()) {
569 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
570 m_process->GetByteOrder(),
571 m_process->GetAddressByteSize());
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
614 if (m_lib_backtrace_recording_info.queue_info_version != 0)
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;
626 m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
627 introspection_dispatch_queue_info_version, eSymbolTypeData, sc_list);
628 if (!sc_list.IsEmpty()) {
629 SymbolContext sc;
630 sc_list.GetContextAtIndex(0, sc);
632 queue_info_version_address = addr.GetLoadAddress(&target);
633 }
634 sc_list.Clear();
635
636 static ConstString introspection_dispatch_queue_info_data_offset(
637 "__introspection_dispatch_queue_info_data_offset");
638 m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
639 introspection_dispatch_queue_info_data_offset, eSymbolTypeData, sc_list);
640 if (!sc_list.IsEmpty()) {
641 SymbolContext sc;
642 sc_list.GetContextAtIndex(0, sc);
644 queue_info_data_offset_address = addr.GetLoadAddress(&target);
645 }
646 sc_list.Clear();
647
648 static ConstString introspection_dispatch_item_info_version(
649 "__introspection_dispatch_item_info_version");
650 m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
651 introspection_dispatch_item_info_version, eSymbolTypeData, sc_list);
652 if (!sc_list.IsEmpty()) {
653 SymbolContext sc;
654 sc_list.GetContextAtIndex(0, sc);
656 item_info_version_address = addr.GetLoadAddress(&target);
657 }
658 sc_list.Clear();
659
660 static ConstString introspection_dispatch_item_info_data_offset(
661 "__introspection_dispatch_item_info_data_offset");
662 m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
663 introspection_dispatch_item_info_data_offset, eSymbolTypeData, sc_list);
664 if (!sc_list.IsEmpty()) {
665 SymbolContext sc;
666 sc_list.GetContextAtIndex(0, sc);
668 item_info_data_offset_address = addr.GetLoadAddress(&target);
669 }
670
671 if (queue_info_version_address != LLDB_INVALID_ADDRESS &&
672 queue_info_data_offset_address != LLDB_INVALID_ADDRESS &&
673 item_info_version_address != LLDB_INVALID_ADDRESS &&
674 item_info_data_offset_address != LLDB_INVALID_ADDRESS) {
676 m_lib_backtrace_recording_info.queue_info_version =
677 m_process->ReadUnsignedIntegerFromMemory(queue_info_version_address, 2,
678 0, error);
679 if (error.Success()) {
680 m_lib_backtrace_recording_info.queue_info_data_offset =
681 m_process->ReadUnsignedIntegerFromMemory(
682 queue_info_data_offset_address, 2, 0, error);
683 if (error.Success()) {
684 m_lib_backtrace_recording_info.item_info_version =
685 m_process->ReadUnsignedIntegerFromMemory(item_info_version_address,
686 2, 0, error);
687 if (error.Success()) {
688 m_lib_backtrace_recording_info.item_info_data_offset =
689 m_process->ReadUnsignedIntegerFromMemory(
690 item_info_data_offset_address, 2, 0, error);
691 if (!error.Success()) {
692 m_lib_backtrace_recording_info.queue_info_version = 0;
693 }
694 } else {
695 m_lib_backtrace_recording_info.queue_info_version = 0;
696 }
697 } else {
698 m_lib_backtrace_recording_info.queue_info_version = 0;
699 }
700 }
701 }
702
703 return m_lib_backtrace_recording_info.queue_info_version != 0;
704}
705
706const std::vector<ConstString> &
708 if (m_types.size() == 0) {
709 m_types.push_back(ConstString("libdispatch"));
710 m_types.push_back(ConstString("Application Specific Backtrace"));
711 // We could have pthread as another type in the future if we have a way of
712 // gathering that information & it's useful to distinguish between them.
713 }
714 return m_types;
715}
716
718 lldb_private::QueueList &queue_list) {
721 ThreadSP cur_thread_sp(
722 m_process->GetThreadList().GetExpressionExecutionThread());
723 if (cur_thread_sp) {
725 queue_info_pointer = m_get_queues_handler.GetCurrentQueues(
726 *cur_thread_sp.get(), m_page_to_free, m_page_to_free_size, error);
729 if (error.Success()) {
730
731 if (queue_info_pointer.count > 0 &&
732 queue_info_pointer.queues_buffer_size > 0 &&
733 queue_info_pointer.queues_buffer_ptr != 0 &&
734 queue_info_pointer.queues_buffer_ptr != LLDB_INVALID_ADDRESS) {
736 queue_info_pointer.queues_buffer_size,
737 queue_info_pointer.count, queue_list);
738 }
739 }
740 }
741 }
742
743 // We either didn't have libBacktraceRecording (and need to create the queues
744 // list based on threads) or we did get the queues list from
745 // libBacktraceRecording but some special queues may not be included in its
746 // information. This is needed because libBacktraceRecording will only list
747 // queues with pending or running items by default - but the magic com.apple
748 // .main-thread queue on thread 1 is always around.
749
750 for (ThreadSP thread_sp : m_process->Threads()) {
751 if (thread_sp->GetAssociatedWithLibdispatchQueue() != eLazyBoolNo) {
752 if (thread_sp->GetQueueID() != LLDB_INVALID_QUEUE_ID) {
753 if (queue_list.FindQueueByID(thread_sp->GetQueueID()).get() ==
754 nullptr) {
755 QueueSP queue_sp(new Queue(m_process->shared_from_this(),
756 thread_sp->GetQueueID(),
757 thread_sp->GetQueueName()));
758 if (thread_sp->ThreadHasQueueInformation()) {
759 queue_sp->SetKind(thread_sp->GetQueueKind());
760 queue_sp->SetLibdispatchQueueAddress(
761 thread_sp->GetQueueLibdispatchQueueAddress());
762 queue_list.AddQueue(queue_sp);
763 } else {
764 queue_sp->SetKind(
765 GetQueueKind(thread_sp->GetQueueLibdispatchQueueAddress()));
766 queue_sp->SetLibdispatchQueueAddress(
767 thread_sp->GetQueueLibdispatchQueueAddress());
768 queue_list.AddQueue(queue_sp);
769 }
770 }
771 }
772 }
773 }
774}
775
776// Returns either an array of introspection_dispatch_item_info_ref's for the
777// pending items on a queue or an array introspection_dispatch_item_info_ref's
778// and code addresses for the pending items on a queue. The information about
779// each of these pending items then needs to be fetched individually by passing
780// the ref to libBacktraceRecording.
781
784 PendingItemsForQueue pending_item_refs = {};
786 ThreadSP cur_thread_sp(
787 m_process->GetThreadList().GetExpressionExecutionThread());
788 if (cur_thread_sp) {
790 pending_items_pointer = m_get_pending_items_handler.GetPendingItems(
791 *cur_thread_sp.get(), queue, m_page_to_free, m_page_to_free_size,
792 error);
795 if (error.Success()) {
796 if (pending_items_pointer.count > 0 &&
797 pending_items_pointer.items_buffer_size > 0 &&
798 pending_items_pointer.items_buffer_ptr != 0 &&
799 pending_items_pointer.items_buffer_ptr != LLDB_INVALID_ADDRESS) {
800 DataBufferHeap data(pending_items_pointer.items_buffer_size, 0);
801 if (m_process->ReadMemory(
802 pending_items_pointer.items_buffer_ptr, data.GetBytes(),
803 pending_items_pointer.items_buffer_size, error)) {
804 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
805 m_process->GetByteOrder(),
806 m_process->GetAddressByteSize());
807
808 // We either have an array of
809 // void* item_ref
810 // (old style) or we have a structure returned which looks like
811 //
812 // struct introspection_dispatch_pending_item_info_s {
813 // void *item_ref;
814 // void *function_or_block;
815 // };
816 //
817 // struct introspection_dispatch_pending_items_array_s {
818 // uint32_t version;
819 // uint32_t size_of_item_info;
820 // introspection_dispatch_pending_item_info_s items[];
821 // }
822
823 offset_t offset = 0;
824 uint64_t i = 0;
825 uint32_t version = extractor.GetU32(&offset);
826 if (version == 1) {
827 pending_item_refs.new_style = true;
828 uint32_t item_size = extractor.GetU32(&offset);
829 uint32_t start_of_array_offset = offset;
830 while (offset < pending_items_pointer.items_buffer_size &&
831 i < pending_items_pointer.count) {
832 offset = start_of_array_offset + (i * item_size);
834 item.item_ref = extractor.GetAddress(&offset);
835 item.code_address = extractor.GetAddress(&offset);
836 pending_item_refs.item_refs_and_code_addresses.push_back(item);
837 i++;
838 }
839 } else {
840 offset = 0;
841 pending_item_refs.new_style = false;
842 while (offset < pending_items_pointer.items_buffer_size &&
843 i < pending_items_pointer.count) {
845 item.item_ref = extractor.GetAddress(&offset);
847 pending_item_refs.item_refs_and_code_addresses.push_back(item);
848 i++;
849 }
850 }
851 }
852 m_page_to_free = pending_items_pointer.items_buffer_ptr;
853 m_page_to_free_size = pending_items_pointer.items_buffer_size;
854 }
855 }
856 }
857 return pending_item_refs;
858}
859
862 PendingItemsForQueue pending_item_refs =
864 for (ItemRefAndCodeAddress pending_item :
865 pending_item_refs.item_refs_and_code_addresses) {
866 Address addr;
867 m_process->GetTarget().ResolveLoadAddress(pending_item.code_address,
868 addr);
869 QueueItemSP queue_item_sp(new QueueItem(queue->shared_from_this(),
870 m_process->shared_from_this(),
871 pending_item.item_ref, addr));
872 queue->PushPendingQueueItem(queue_item_sp);
873 }
874 }
875}
876
878 addr_t item_ref) {
880
881 ThreadSP cur_thread_sp(
882 m_process->GetThreadList().GetExpressionExecutionThread());
884 ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
886 error);
890 ret.item_buffer_size > 0) {
891 DataBufferHeap data(ret.item_buffer_size, 0);
892 if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
893 ret.item_buffer_size, error) &&
894 error.Success()) {
895 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
896 m_process->GetByteOrder(),
897 m_process->GetAddressByteSize());
898 ItemInfo item = ExtractItemInfoFromBuffer(extractor);
902 queue_item->SetStopID(item.stop_id);
904 queue_item->SetThreadLabel(item.enqueuing_thread_label);
905 queue_item->SetQueueLabel(item.enqueuing_queue_label);
906 queue_item->SetTargetQueueLabel(item.target_queue_label);
907 }
910 }
911}
912
914 lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count,
915 lldb_private::QueueList &queue_list) {
917 DataBufferHeap data(queues_buffer_size, 0);
919 if (m_process->ReadMemory(queues_buffer, data.GetBytes(), queues_buffer_size,
920 error) == queues_buffer_size &&
921 error.Success()) {
922 // We've read the information out of inferior memory; free it on the next
923 // call we make
924 m_page_to_free = queues_buffer;
925 m_page_to_free_size = queues_buffer_size;
926
927 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
928 m_process->GetByteOrder(),
929 m_process->GetAddressByteSize());
930 offset_t offset = 0;
931 uint64_t queues_read = 0;
932
933 // The information about the queues is stored in this format (v1): typedef
934 // struct introspection_dispatch_queue_info_s {
935 // uint32_t offset_to_next;
936 // dispatch_queue_t queue;
937 // uint64_t serialnum; // queue's serialnum in the process, as
938 // provided by libdispatch
939 // uint32_t running_work_items_count;
940 // uint32_t pending_work_items_count;
941 //
942 // char data[]; // Starting here, we have variable-length data:
943 // // char queue_label[];
944 // } introspection_dispatch_queue_info_s;
945
946 while (queues_read < count && offset < queues_buffer_size) {
947 offset_t start_of_this_item = offset;
948
949 uint32_t offset_to_next = extractor.GetU32(&offset);
950
951 offset += 4; // Skip over the 4 bytes of reserved space
952 addr_t queue = extractor.GetAddress(&offset);
953 uint64_t serialnum = extractor.GetU64(&offset);
954 uint32_t running_work_items_count = extractor.GetU32(&offset);
955 uint32_t pending_work_items_count = extractor.GetU32(&offset);
956
957 // Read the first field of the variable length data
958 offset = start_of_this_item +
959 m_lib_backtrace_recording_info.queue_info_data_offset;
960 const char *queue_label = extractor.GetCStr(&offset);
961 if (queue_label == nullptr)
962 queue_label = "";
963
964 offset_t start_of_next_item = start_of_this_item + offset_to_next;
965 offset = start_of_next_item;
966
967 LLDB_LOGF(log,
968 "SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR added "
969 "queue with dispatch_queue_t 0x%" PRIx64
970 ", serial number 0x%" PRIx64
971 ", running items %d, pending items %d, name '%s'",
972 queue, serialnum, running_work_items_count,
973 pending_work_items_count, queue_label);
974
975 QueueSP queue_sp(
976 new Queue(m_process->shared_from_this(), serialnum, queue_label));
977 queue_sp->SetNumRunningWorkItems(running_work_items_count);
978 queue_sp->SetNumPendingWorkItems(pending_work_items_count);
979 queue_sp->SetLibdispatchQueueAddress(queue);
980 queue_sp->SetKind(GetQueueKind(queue));
981 queue_list.AddQueue(queue_sp);
982 queues_read++;
983 }
984 }
985}
986
988 lldb_private::DataExtractor &extractor) {
989 ItemInfo item;
990
991 offset_t offset = 0;
992
993 item.item_that_enqueued_this = extractor.GetAddress(&offset);
994 item.function_or_block = extractor.GetAddress(&offset);
995 item.enqueuing_thread_id = extractor.GetU64(&offset);
996 item.enqueuing_queue_serialnum = extractor.GetU64(&offset);
997 item.target_queue_serialnum = extractor.GetU64(&offset);
998 item.enqueuing_callstack_frame_count = extractor.GetU32(&offset);
999 item.stop_id = extractor.GetU32(&offset);
1000
1001 offset = m_lib_backtrace_recording_info.item_info_data_offset;
1002
1003 for (uint32_t i = 0; i < item.enqueuing_callstack_frame_count; i++) {
1004 item.enqueuing_callstack.push_back(extractor.GetAddress(&offset));
1005 }
1006 item.enqueuing_thread_label = extractor.GetCStr(&offset);
1007 item.enqueuing_queue_label = extractor.GetCStr(&offset);
1008 item.target_queue_label = extractor.GetCStr(&offset);
1009
1010 return item;
1011}
1012
1016 "System runtime plugin for Mac OS X native libraries.", CreateInstance);
1017}
1018
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:1188
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
RetType GetField(llvm::StringRef name, RetType fail_value=RetType())
A plug-in interface definition class for debugging a process.
Definition Process.h:354
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 bool CompleteTagDeclarationDefinition(const CompilerType &type)
static bool StartTagDeclarationDefinition(const CompilerType &type)
static clang::FieldDecl * AddFieldToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &field_type, uint32_t bitfield_bit_size)
uint8_t * GetBytes()
Get a pointer to the data.
Definition DataBuffer.h:108
#define LLDB_INVALID_QUEUE_ID
#define LLDB_INVALID_BREAK_ID
#define LLDB_BREAK_ID_IS_VALID(bid)
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h: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:91
std::shared_ptr< lldb_private::QueueItem > QueueItemSP
std::vector< lldb::addr_t > enqueuing_callstack
std::vector< ItemRefAndCodeAddress > item_refs_and_code_addresses