Coverage Report

Created: 2023-11-11 10:31

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp
Line
Count
Source (jump to first uncovered line)
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
9
#include "Plugins/Process/Utility/HistoryThread.h"
10
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
11
#include "lldb/Breakpoint/StoppointCallbackContext.h"
12
#include "lldb/Core/Module.h"
13
#include "lldb/Core/ModuleSpec.h"
14
#include "lldb/Core/PluginManager.h"
15
#include "lldb/Core/Section.h"
16
#include "lldb/Symbol/ObjectFile.h"
17
#include "lldb/Symbol/SymbolContext.h"
18
#include "lldb/Target/Process.h"
19
#include "lldb/Target/ProcessStructReader.h"
20
#include "lldb/Target/Queue.h"
21
#include "lldb/Target/QueueList.h"
22
#include "lldb/Target/Target.h"
23
#include "lldb/Target/Thread.h"
24
#include "lldb/Utility/DataBufferHeap.h"
25
#include "lldb/Utility/DataExtractor.h"
26
#include "lldb/Utility/FileSpec.h"
27
#include "lldb/Utility/LLDBLog.h"
28
#include "lldb/Utility/Log.h"
29
#include "lldb/Utility/StreamString.h"
30
31
#include "SystemRuntimeMacOSX.h"
32
33
#include <memory>
34
35
using namespace lldb;
36
using namespace lldb_private;
37
38
LLDB_PLUGIN_DEFINE(SystemRuntimeMacOSX)
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.
43
2.44k
SystemRuntime *SystemRuntimeMacOSX::CreateInstance(Process *process) {
44
2.44k
  bool create = false;
45
2.44k
  if (!create) {
46
2.44k
    create = true;
47
2.44k
    Module *exe_module = process->GetTarget().GetExecutableModulePointer();
48
2.44k
    if (exe_module) {
49
2.32k
      ObjectFile *object_file = exe_module->GetObjectFile();
50
2.32k
      if (object_file) {
51
2.32k
        create = (object_file->GetStrata() == ObjectFile::eStrataUser);
52
2.32k
      }
53
2.32k
    }
54
55
2.44k
    if (create) {
56
2.44k
      const llvm::Triple &triple_ref =
57
2.44k
          process->GetTarget().GetArchitecture().GetTriple();
58
2.44k
      switch (triple_ref.getOS()) {
59
0
      case llvm::Triple::Darwin:
60
2.15k
      case llvm::Triple::MacOSX:
61
2.15k
      case llvm::Triple::IOS:
62
2.15k
      case llvm::Triple::TvOS:
63
2.16k
      case llvm::Triple::WatchOS:
64
      // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS:
65
2.16k
        create = triple_ref.getVendor() == llvm::Triple::Apple;
66
2.16k
        break;
67
281
      default:
68
281
        create = false;
69
281
        break;
70
2.44k
      }
71
2.44k
    }
72
2.44k
  }
73
74
2.44k
  if (create)
75
2.16k
    return new SystemRuntimeMacOSX(process);
76
284
  return nullptr;
77
2.44k
}
78
79
// Constructor
80
SystemRuntimeMacOSX::SystemRuntimeMacOSX(Process *process)
81
2.16k
    : SystemRuntime(process), m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
82
2.16k
      m_get_queues_handler(process), m_get_pending_items_handler(process),
83
2.16k
      m_get_item_info_handler(process), m_get_thread_item_info_handler(process),
84
2.16k
      m_page_to_free(LLDB_INVALID_ADDRESS), m_page_to_free_size(0),
85
2.16k
      m_lib_backtrace_recording_info(),
86
      m_dispatch_queue_offsets_addr(LLDB_INVALID_ADDRESS),
87
2.16k
      m_libdispatch_offsets(),
88
      m_libpthread_layout_offsets_addr(LLDB_INVALID_ADDRESS),
89
2.16k
      m_libpthread_offsets(), m_dispatch_tsd_indexes_addr(LLDB_INVALID_ADDRESS),
90
2.16k
      m_libdispatch_tsd_indexes(),
91
      m_dispatch_voucher_offsets_addr(LLDB_INVALID_ADDRESS),
92
2.16k
      m_libdispatch_voucher_offsets() {}
93
94
// Destructor
95
2.16k
SystemRuntimeMacOSX::~SystemRuntimeMacOSX() { Clear(true); }
96
97
0
void SystemRuntimeMacOSX::Detach() {
98
0
  m_get_queues_handler.Detach();
99
0
  m_get_pending_items_handler.Detach();
100
0
  m_get_item_info_handler.Detach();
101
0
  m_get_thread_item_info_handler.Detach();
102
0
}
103
104
// Clear out the state of this class.
105
2.16k
void SystemRuntimeMacOSX::Clear(bool clear_process) {
106
2.16k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
107
108
2.16k
  if (m_process->IsAlive() && 
LLDB_BREAK_ID_IS_VALID6
(m_break_id))
109
0
    m_process->ClearBreakpointSiteByID(m_break_id);
110
111
2.16k
  if (clear_process)
112
2.16k
    m_process = nullptr;
113
2.16k
  m_break_id = LLDB_INVALID_BREAK_ID;
114
2.16k
}
115
116
std::string
117
2
SystemRuntimeMacOSX::GetQueueNameFromThreadQAddress(addr_t dispatch_qaddr) {
118
2
  std::string dispatch_queue_name;
119
2
  if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
120
0
    return "";
121
122
2
  ReadLibdispatchOffsets();
123
2
  if (m_libdispatch_offsets.IsValid()) {
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.
127
2
    Status error;
128
2
    addr_t dispatch_queue_addr =
129
2
        m_process->ReadPointerFromMemory(dispatch_qaddr, error);
130
2
    if (error.Success()) {
131
2
      if (m_libdispatch_offsets.dqo_version >= 4) {
132
        // libdispatch versions 4+, pointer to dispatch name is in the queue
133
        // structure.
134
2
        addr_t pointer_to_label_address =
135
2
            dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
136
2
        addr_t label_addr =
137
2
            m_process->ReadPointerFromMemory(pointer_to_label_address, error);
138
2
        if (error.Success()) {
139
2
          m_process->ReadCStringFromMemory(label_addr, dispatch_queue_name,
140
2
                                           error);
141
2
        }
142
2
      } else {
143
        // libdispatch versions 1-3, dispatch name is a fixed width char array
144
        // in the queue structure.
145
0
        addr_t label_addr =
146
0
            dispatch_queue_addr + m_libdispatch_offsets.dqo_label;
147
0
        dispatch_queue_name.resize(m_libdispatch_offsets.dqo_label_size, '\0');
148
0
        size_t bytes_read =
149
0
            m_process->ReadMemory(label_addr, &dispatch_queue_name[0],
150
0
                                  m_libdispatch_offsets.dqo_label_size, error);
151
0
        if (bytes_read < m_libdispatch_offsets.dqo_label_size)
152
0
          dispatch_queue_name.erase(bytes_read);
153
0
      }
154
2
    }
155
2
  }
156
2
  return dispatch_queue_name;
157
2
}
158
159
lldb::addr_t SystemRuntimeMacOSX::GetLibdispatchQueueAddressFromThreadQAddress(
160
0
    addr_t dispatch_qaddr) {
161
0
  addr_t libdispatch_queue_t_address = LLDB_INVALID_ADDRESS;
162
0
  Status error;
163
0
  libdispatch_queue_t_address =
164
0
      m_process->ReadPointerFromMemory(dispatch_qaddr, error);
165
0
  if (!error.Success()) {
166
0
    libdispatch_queue_t_address = LLDB_INVALID_ADDRESS;
167
0
  }
168
0
  return libdispatch_queue_t_address;
169
0
}
170
171
0
lldb::QueueKind SystemRuntimeMacOSX::GetQueueKind(addr_t dispatch_queue_addr) {
172
0
  if (dispatch_queue_addr == LLDB_INVALID_ADDRESS || dispatch_queue_addr == 0)
173
0
    return eQueueKindUnknown;
174
175
0
  QueueKind kind = eQueueKindUnknown;
176
0
  ReadLibdispatchOffsets();
177
0
  if (m_libdispatch_offsets.IsValid() &&
178
0
      m_libdispatch_offsets.dqo_version >= 4) {
179
0
    Status error;
180
0
    uint64_t width = m_process->ReadUnsignedIntegerFromMemory(
181
0
        dispatch_queue_addr + m_libdispatch_offsets.dqo_width,
182
0
        m_libdispatch_offsets.dqo_width_size, 0, error);
183
0
    if (error.Success()) {
184
0
      if (width == 1) {
185
0
        kind = eQueueKindSerial;
186
0
      }
187
0
      if (width > 1) {
188
0
        kind = eQueueKindConcurrent;
189
0
      }
190
0
    }
191
0
  }
192
0
  return kind;
193
0
}
194
195
void SystemRuntimeMacOSX::AddThreadExtendedInfoPacketHints(
196
1.79k
    lldb_private::StructuredData::ObjectSP dict_sp) {
197
1.79k
  StructuredData::Dictionary *dict = dict_sp->GetAsDictionary();
198
1.79k
  if (dict) {
199
1.79k
    ReadLibpthreadOffsets();
200
1.79k
    if (m_libpthread_offsets.IsValid()) {
201
1.78k
      dict->AddIntegerItem("plo_pthread_tsd_base_offset",
202
1.78k
                           m_libpthread_offsets.plo_pthread_tsd_base_offset);
203
1.78k
      dict->AddIntegerItem(
204
1.78k
          "plo_pthread_tsd_base_address_offset",
205
1.78k
          m_libpthread_offsets.plo_pthread_tsd_base_address_offset);
206
1.78k
      dict->AddIntegerItem("plo_pthread_tsd_entry_size",
207
1.78k
                           m_libpthread_offsets.plo_pthread_tsd_entry_size);
208
1.78k
    }
209
210
1.79k
    ReadLibdispatchTSDIndexes();
211
1.79k
    if (m_libdispatch_tsd_indexes.IsValid()) {
212
1.78k
      dict->AddIntegerItem("dti_queue_index",
213
1.78k
                           m_libdispatch_tsd_indexes.dti_queue_index);
214
1.78k
      dict->AddIntegerItem("dti_voucher_index",
215
1.78k
                           m_libdispatch_tsd_indexes.dti_voucher_index);
216
1.78k
      dict->AddIntegerItem("dti_qos_class_index",
217
1.78k
                           m_libdispatch_tsd_indexes.dti_qos_class_index);
218
1.78k
    }
219
1.79k
  }
220
1.79k
}
221
222
1.99k
bool SystemRuntimeMacOSX::SafeToCallFunctionsOnThisThread(ThreadSP thread_sp) {
223
1.99k
  if (thread_sp && thread_sp->GetFrameWithConcreteFrameIndex(0)) {
224
1.99k
    const SymbolContext sym_ctx(
225
1.99k
        thread_sp->GetFrameWithConcreteFrameIndex(0)->GetSymbolContext(
226
1.99k
            eSymbolContextSymbol));
227
1.99k
    static ConstString g_select_symbol("__select");
228
1.99k
    if (sym_ctx.GetFunctionName() == g_select_symbol) {
229
2
      return false;
230
2
    }
231
1.99k
  }
232
1.99k
  return true;
233
1.99k
}
234
235
lldb::queue_id_t
236
0
SystemRuntimeMacOSX::GetQueueIDFromThreadQAddress(lldb::addr_t dispatch_qaddr) {
237
0
  queue_id_t queue_id = LLDB_INVALID_QUEUE_ID;
238
239
0
  if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0)
240
0
    return queue_id;
241
242
0
  ReadLibdispatchOffsets();
243
0
  if (m_libdispatch_offsets.IsValid()) {
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.
247
0
    Status error;
248
0
    uint64_t dispatch_queue_addr =
249
0
        m_process->ReadPointerFromMemory(dispatch_qaddr, error);
250
0
    if (error.Success()) {
251
0
      addr_t serialnum_address =
252
0
          dispatch_queue_addr + m_libdispatch_offsets.dqo_serialnum;
253
0
      queue_id_t serialnum = m_process->ReadUnsignedIntegerFromMemory(
254
0
          serialnum_address, m_libdispatch_offsets.dqo_serialnum_size,
255
0
          LLDB_INVALID_QUEUE_ID, error);
256
0
      if (error.Success()) {
257
0
        queue_id = serialnum;
258
0
      }
259
0
    }
260
0
  }
261
262
0
  return queue_id;
263
0
}
264
265
2
void SystemRuntimeMacOSX::ReadLibdispatchOffsetsAddress() {
266
2
  if (m_dispatch_queue_offsets_addr != LLDB_INVALID_ADDRESS)
267
0
    return;
268
269
2
  static ConstString g_dispatch_queue_offsets_symbol_name(
270
2
      "dispatch_queue_offsets");
271
2
  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
2
  ModuleSpec libSystem_module_spec(FileSpec("libSystem.B.dylib"));
276
2
  ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
277
2
      libSystem_module_spec));
278
2
  if (module_sp)
279
2
    dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
280
2
        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
2
  if (dispatch_queue_offsets_symbol == nullptr) {
285
2
    ModuleSpec libdispatch_module_spec(FileSpec("libdispatch.dylib"));
286
2
    module_sp = m_process->GetTarget().GetImages().FindFirstModule(
287
2
        libdispatch_module_spec);
288
2
    if (module_sp)
289
2
      dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType(
290
2
          g_dispatch_queue_offsets_symbol_name, eSymbolTypeData);
291
2
  }
292
2
  if (dispatch_queue_offsets_symbol)
293
2
    m_dispatch_queue_offsets_addr =
294
2
        dispatch_queue_offsets_symbol->GetLoadAddress(&m_process->GetTarget());
295
2
}
296
297
2
void SystemRuntimeMacOSX::ReadLibdispatchOffsets() {
298
2
  if (m_libdispatch_offsets.IsValid())
299
0
    return;
300
301
2
  ReadLibdispatchOffsetsAddress();
302
303
2
  uint8_t memory_buffer[sizeof(struct LibdispatchOffsets)];
304
2
  DataExtractor data(memory_buffer, sizeof(memory_buffer),
305
2
                     m_process->GetByteOrder(),
306
2
                     m_process->GetAddressByteSize());
307
308
2
  Status error;
309
2
  if (m_process->ReadMemory(m_dispatch_queue_offsets_addr, memory_buffer,
310
2
                            sizeof(memory_buffer),
311
2
                            error) == sizeof(memory_buffer)) {
312
2
    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
2
    data.GetU16(&data_offset, &m_libdispatch_offsets.dqo_version,
317
2
                sizeof(struct LibdispatchOffsets) / sizeof(uint16_t));
318
2
  }
319
2
}
320
321
906
void SystemRuntimeMacOSX::ReadLibpthreadOffsetsAddress() {
322
906
  if (m_libpthread_layout_offsets_addr != LLDB_INVALID_ADDRESS)
323
0
    return;
324
325
906
  static ConstString g_libpthread_layout_offsets_symbol_name(
326
906
      "pthread_layout_offsets");
327
906
  const Symbol *libpthread_layout_offsets_symbol = nullptr;
328
329
906
  ModuleSpec libpthread_module_spec(FileSpec("libsystem_pthread.dylib"));
330
906
  ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
331
906
      libpthread_module_spec));
332
906
  if (module_sp) {
333
895
    libpthread_layout_offsets_symbol =
334
895
        module_sp->FindFirstSymbolWithNameAndType(
335
895
            g_libpthread_layout_offsets_symbol_name, eSymbolTypeData);
336
895
    if (libpthread_layout_offsets_symbol) {
337
895
      m_libpthread_layout_offsets_addr =
338
895
          libpthread_layout_offsets_symbol->GetLoadAddress(
339
895
              &m_process->GetTarget());
340
895
    }
341
895
  }
342
906
}
343
344
1.79k
void SystemRuntimeMacOSX::ReadLibpthreadOffsets() {
345
1.79k
  if (m_libpthread_offsets.IsValid())
346
893
    return;
347
348
906
  ReadLibpthreadOffsetsAddress();
349
350
906
  if (m_libpthread_layout_offsets_addr != LLDB_INVALID_ADDRESS) {
351
895
    uint8_t memory_buffer[sizeof(struct LibpthreadOffsets)];
352
895
    DataExtractor data(memory_buffer, sizeof(memory_buffer),
353
895
                       m_process->GetByteOrder(),
354
895
                       m_process->GetAddressByteSize());
355
895
    Status error;
356
895
    if (m_process->ReadMemory(m_libpthread_layout_offsets_addr, memory_buffer,
357
895
                              sizeof(memory_buffer),
358
895
                              error) == sizeof(memory_buffer)) {
359
895
      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
895
      data.GetU16(&data_offset, &m_libpthread_offsets.plo_version,
364
895
                  sizeof(struct LibpthreadOffsets) / sizeof(uint16_t));
365
895
    }
366
895
  }
367
906
}
368
369
906
void SystemRuntimeMacOSX::ReadLibdispatchTSDIndexesAddress() {
370
906
  if (m_dispatch_tsd_indexes_addr != LLDB_INVALID_ADDRESS)
371
0
    return;
372
373
906
  static ConstString g_libdispatch_tsd_indexes_symbol_name(
374
906
      "dispatch_tsd_indexes");
375
906
  const Symbol *libdispatch_tsd_indexes_symbol = nullptr;
376
377
906
  ModuleSpec libpthread_module_spec(FileSpec("libdispatch.dylib"));
378
906
  ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule(
379
906
      libpthread_module_spec));
380
906
  if (module_sp) {
381
895
    libdispatch_tsd_indexes_symbol = module_sp->FindFirstSymbolWithNameAndType(
382
895
        g_libdispatch_tsd_indexes_symbol_name, eSymbolTypeData);
383
895
    if (libdispatch_tsd_indexes_symbol) {
384
895
      m_dispatch_tsd_indexes_addr =
385
895
          libdispatch_tsd_indexes_symbol->GetLoadAddress(
386
895
              &m_process->GetTarget());
387
895
    }
388
895
  }
389
906
}
390
391
1.79k
void SystemRuntimeMacOSX::ReadLibdispatchTSDIndexes() {
392
1.79k
  if (m_libdispatch_tsd_indexes.IsValid())
393
893
    return;
394
395
906
  ReadLibdispatchTSDIndexesAddress();
396
397
906
  if (m_dispatch_tsd_indexes_addr != LLDB_INVALID_ADDRESS) {
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;
405
        if (m_process->GetTarget().ResolveLoadAddress (m_dispatch_tsd_indexes_addr, dti_struct_addr))
406
        {
407
            Status error;
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
895
    TypeSystemClangSP scratch_ts_sp =
417
895
        ScratchTypeSystemClang::GetForTarget(m_process->GetTarget());
418
895
    if (m_dispatch_tsd_indexes_addr != LLDB_INVALID_ADDRESS) {
419
895
      CompilerType uint16 =
420
895
          scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16);
421
895
      CompilerType dispatch_tsd_indexes_s = scratch_ts_sp->CreateRecordType(
422
895
          nullptr, OptionalClangModuleID(), lldb::eAccessPublic,
423
895
          "__lldb_dispatch_tsd_indexes_s",
424
895
          llvm::to_underlying(clang::TagTypeKind::Struct),
425
895
          lldb::eLanguageTypeC);
426
427
895
      TypeSystemClang::StartTagDeclarationDefinition(dispatch_tsd_indexes_s);
428
895
      TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
429
895
                                            "dti_version", uint16,
430
895
                                            lldb::eAccessPublic, 0);
431
895
      TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
432
895
                                            "dti_queue_index", uint16,
433
895
                                            lldb::eAccessPublic, 0);
434
895
      TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
435
895
                                            "dti_voucher_index", uint16,
436
895
                                            lldb::eAccessPublic, 0);
437
895
      TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s,
438
895
                                            "dti_qos_class_index", uint16,
439
895
                                            lldb::eAccessPublic, 0);
440
895
      TypeSystemClang::CompleteTagDeclarationDefinition(dispatch_tsd_indexes_s);
441
442
895
      ProcessStructReader struct_reader(m_process, m_dispatch_tsd_indexes_addr,
443
895
                                        dispatch_tsd_indexes_s);
444
445
895
      m_libdispatch_tsd_indexes.dti_version =
446
895
          struct_reader.GetField<uint16_t>("dti_version");
447
895
      m_libdispatch_tsd_indexes.dti_queue_index =
448
895
          struct_reader.GetField<uint16_t>("dti_queue_index");
449
895
      m_libdispatch_tsd_indexes.dti_voucher_index =
450
895
          struct_reader.GetField<uint16_t>("dti_voucher_index");
451
895
      m_libdispatch_tsd_indexes.dti_qos_class_index =
452
895
          struct_reader.GetField<uint16_t>("dti_qos_class_index");
453
895
    }
454
895
  }
455
906
}
456
457
ThreadSP SystemRuntimeMacOSX::GetExtendedBacktraceThread(ThreadSP real_thread,
458
0
                                                         ConstString type) {
459
0
  ThreadSP originating_thread_sp;
460
0
  if (BacktraceRecordingHeadersInitialized() && type == "libdispatch") {
461
0
    Status error;
462
463
    // real_thread is either an actual, live thread (in which case we need to
464
    // call into libBacktraceRecording to find its originator) or it is an
465
    // extended backtrace itself, in which case we get the token from it and
466
    // call into libBacktraceRecording to find the originator of that token.
467
468
0
    if (real_thread->GetExtendedBacktraceToken() != LLDB_INVALID_ADDRESS) {
469
0
      originating_thread_sp = GetExtendedBacktraceFromItemRef(
470
0
          real_thread->GetExtendedBacktraceToken());
471
0
    } else {
472
0
      ThreadSP cur_thread_sp(
473
0
          m_process->GetThreadList().GetExpressionExecutionThread());
474
0
      AppleGetThreadItemInfoHandler::GetThreadItemInfoReturnInfo ret =
475
0
          m_get_thread_item_info_handler.GetThreadItemInfo(
476
0
              *cur_thread_sp.get(), real_thread->GetID(), m_page_to_free,
477
0
              m_page_to_free_size, error);
478
0
      m_page_to_free = LLDB_INVALID_ADDRESS;
479
0
      m_page_to_free_size = 0;
480
0
      if (ret.item_buffer_ptr != 0 &&
481
0
          ret.item_buffer_ptr != LLDB_INVALID_ADDRESS &&
482
0
          ret.item_buffer_size > 0) {
483
0
        DataBufferHeap data(ret.item_buffer_size, 0);
484
0
        if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
485
0
                                  ret.item_buffer_size, error) &&
486
0
            error.Success()) {
487
0
          DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
488
0
                                  m_process->GetByteOrder(),
489
0
                                  m_process->GetAddressByteSize());
490
0
          ItemInfo item = ExtractItemInfoFromBuffer(extractor);
491
0
          originating_thread_sp = std::make_shared<HistoryThread>(
492
0
              *m_process, item.enqueuing_thread_id, item.enqueuing_callstack);
493
0
          originating_thread_sp->SetExtendedBacktraceToken(
494
0
              item.item_that_enqueued_this);
495
0
          originating_thread_sp->SetQueueName(
496
0
              item.enqueuing_queue_label.c_str());
497
0
          originating_thread_sp->SetQueueID(item.enqueuing_queue_serialnum);
498
          //                    originating_thread_sp->SetThreadName
499
          //                    (item.enqueuing_thread_label.c_str());
500
0
        }
501
0
        m_page_to_free = ret.item_buffer_ptr;
502
0
        m_page_to_free_size = ret.item_buffer_size;
503
0
      }
504
0
    }
505
0
  } else if (type == "Application Specific Backtrace") {
506
0
    StructuredData::ObjectSP thread_extended_sp =
507
0
        real_thread->GetExtendedInfo();
508
509
0
    if (!thread_extended_sp)
510
0
      return {};
511
512
0
    StructuredData::Array *thread_extended_info =
513
0
        thread_extended_sp->GetAsArray();
514
515
0
    if (!thread_extended_info || !thread_extended_info->GetSize())
516
0
      return {};
517
518
0
    std::vector<addr_t> app_specific_backtrace_pcs;
519
520
0
    auto extract_frame_pc =
521
0
        [&app_specific_backtrace_pcs](StructuredData::Object *obj) -> bool {
522
0
      if (!obj)
523
0
        return false;
524
525
0
      StructuredData::Dictionary *dict = obj->GetAsDictionary();
526
0
      if (!dict)
527
0
        return false;
528
529
0
      lldb::addr_t pc = LLDB_INVALID_ADDRESS;
530
0
      if (!dict->GetValueForKeyAsInteger("pc", pc))
531
0
        return false;
532
533
0
      app_specific_backtrace_pcs.push_back(pc);
534
535
0
      return pc != LLDB_INVALID_ADDRESS;
536
0
    };
537
538
0
    if (!thread_extended_info->ForEach(extract_frame_pc))
539
0
      return {};
540
541
0
    originating_thread_sp =
542
0
        std::make_shared<HistoryThread>(*m_process, real_thread->GetIndexID(),
543
0
                                        app_specific_backtrace_pcs, true);
544
0
    originating_thread_sp->SetQueueName(type.AsCString());
545
0
  }
546
0
  return originating_thread_sp;
547
0
}
548
549
ThreadSP
550
0
SystemRuntimeMacOSX::GetExtendedBacktraceFromItemRef(lldb::addr_t item_ref) {
551
0
  ThreadSP return_thread_sp;
552
553
0
  AppleGetItemInfoHandler::GetItemInfoReturnInfo ret;
554
0
  ThreadSP cur_thread_sp(
555
0
      m_process->GetThreadList().GetExpressionExecutionThread());
556
0
  Status error;
557
0
  ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
558
0
                                            m_page_to_free, m_page_to_free_size,
559
0
                                            error);
560
0
  m_page_to_free = LLDB_INVALID_ADDRESS;
561
0
  m_page_to_free_size = 0;
562
0
  if (ret.item_buffer_ptr != 0 && ret.item_buffer_ptr != LLDB_INVALID_ADDRESS &&
563
0
      ret.item_buffer_size > 0) {
564
0
    DataBufferHeap data(ret.item_buffer_size, 0);
565
0
    if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
566
0
                              ret.item_buffer_size, error) &&
567
0
        error.Success()) {
568
0
      DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
569
0
                              m_process->GetByteOrder(),
570
0
                              m_process->GetAddressByteSize());
571
0
      ItemInfo item = ExtractItemInfoFromBuffer(extractor);
572
0
      return_thread_sp = std::make_shared<HistoryThread>(
573
0
          *m_process, item.enqueuing_thread_id, item.enqueuing_callstack);
574
0
      return_thread_sp->SetExtendedBacktraceToken(item.item_that_enqueued_this);
575
0
      return_thread_sp->SetQueueName(item.enqueuing_queue_label.c_str());
576
0
      return_thread_sp->SetQueueID(item.enqueuing_queue_serialnum);
577
      //            return_thread_sp->SetThreadName
578
      //            (item.enqueuing_thread_label.c_str());
579
580
0
      m_page_to_free = ret.item_buffer_ptr;
581
0
      m_page_to_free_size = ret.item_buffer_size;
582
0
    }
583
0
  }
584
0
  return return_thread_sp;
585
0
}
586
587
ThreadSP
588
SystemRuntimeMacOSX::GetExtendedBacktraceForQueueItem(QueueItemSP queue_item_sp,
589
0
                                                      ConstString type) {
590
0
  ThreadSP extended_thread_sp;
591
0
  if (type != "libdispatch")
592
0
    return extended_thread_sp;
593
594
0
  extended_thread_sp = std::make_shared<HistoryThread>(
595
0
      *m_process, queue_item_sp->GetEnqueueingThreadID(),
596
0
      queue_item_sp->GetEnqueueingBacktrace());
597
0
  extended_thread_sp->SetExtendedBacktraceToken(
598
0
      queue_item_sp->GetItemThatEnqueuedThis());
599
0
  extended_thread_sp->SetQueueName(queue_item_sp->GetQueueLabel().c_str());
600
0
  extended_thread_sp->SetQueueID(queue_item_sp->GetEnqueueingQueueID());
601
  //    extended_thread_sp->SetThreadName
602
  //    (queue_item_sp->GetThreadLabel().c_str());
603
604
0
  return extended_thread_sp;
605
0
}
606
607
/* Returns true if we were able to get the version / offset information
608
 * out of libBacktraceRecording.  false means we were unable to retrieve
609
 * this; the queue_info_version field will be 0.
610
 */
611
612
4
bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
613
4
  if (m_lib_backtrace_recording_info.queue_info_version != 0)
614
0
    return true;
615
616
4
  addr_t queue_info_version_address = LLDB_INVALID_ADDRESS;
617
4
  addr_t queue_info_data_offset_address = LLDB_INVALID_ADDRESS;
618
4
  addr_t item_info_version_address = LLDB_INVALID_ADDRESS;
619
4
  addr_t item_info_data_offset_address = LLDB_INVALID_ADDRESS;
620
4
  Target &target = m_process->GetTarget();
621
622
4
  static ConstString introspection_dispatch_queue_info_version(
623
4
      "__introspection_dispatch_queue_info_version");
624
4
  SymbolContextList sc_list;
625
4
  m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
626
4
      introspection_dispatch_queue_info_version, eSymbolTypeData, sc_list);
627
4
  if (!sc_list.IsEmpty()) {
628
0
    SymbolContext sc;
629
0
    sc_list.GetContextAtIndex(0, sc);
630
0
    AddressRange addr_range;
631
0
    sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
632
0
    queue_info_version_address =
633
0
        addr_range.GetBaseAddress().GetLoadAddress(&target);
634
0
  }
635
4
  sc_list.Clear();
636
637
4
  static ConstString introspection_dispatch_queue_info_data_offset(
638
4
      "__introspection_dispatch_queue_info_data_offset");
639
4
  m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
640
4
      introspection_dispatch_queue_info_data_offset, eSymbolTypeData, sc_list);
641
4
  if (!sc_list.IsEmpty()) {
642
0
    SymbolContext sc;
643
0
    sc_list.GetContextAtIndex(0, sc);
644
0
    AddressRange addr_range;
645
0
    sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
646
0
    queue_info_data_offset_address =
647
0
        addr_range.GetBaseAddress().GetLoadAddress(&target);
648
0
  }
649
4
  sc_list.Clear();
650
651
4
  static ConstString introspection_dispatch_item_info_version(
652
4
      "__introspection_dispatch_item_info_version");
653
4
  m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
654
4
      introspection_dispatch_item_info_version, eSymbolTypeData, sc_list);
655
4
  if (!sc_list.IsEmpty()) {
656
0
    SymbolContext sc;
657
0
    sc_list.GetContextAtIndex(0, sc);
658
0
    AddressRange addr_range;
659
0
    sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
660
0
    item_info_version_address =
661
0
        addr_range.GetBaseAddress().GetLoadAddress(&target);
662
0
  }
663
4
  sc_list.Clear();
664
665
4
  static ConstString introspection_dispatch_item_info_data_offset(
666
4
      "__introspection_dispatch_item_info_data_offset");
667
4
  m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
668
4
      introspection_dispatch_item_info_data_offset, eSymbolTypeData, sc_list);
669
4
  if (!sc_list.IsEmpty()) {
670
0
    SymbolContext sc;
671
0
    sc_list.GetContextAtIndex(0, sc);
672
0
    AddressRange addr_range;
673
0
    sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
674
0
    item_info_data_offset_address =
675
0
        addr_range.GetBaseAddress().GetLoadAddress(&target);
676
0
  }
677
678
4
  if (queue_info_version_address != LLDB_INVALID_ADDRESS &&
679
4
      
queue_info_data_offset_address != 0
LLDB_INVALID_ADDRESS0
&&
680
4
      
item_info_version_address != 0
LLDB_INVALID_ADDRESS0
&&
681
4
      
item_info_data_offset_address != 0
LLDB_INVALID_ADDRESS0
) {
682
0
    Status error;
683
0
    m_lib_backtrace_recording_info.queue_info_version =
684
0
        m_process->ReadUnsignedIntegerFromMemory(queue_info_version_address, 2,
685
0
                                                 0, error);
686
0
    if (error.Success()) {
687
0
      m_lib_backtrace_recording_info.queue_info_data_offset =
688
0
          m_process->ReadUnsignedIntegerFromMemory(
689
0
              queue_info_data_offset_address, 2, 0, error);
690
0
      if (error.Success()) {
691
0
        m_lib_backtrace_recording_info.item_info_version =
692
0
            m_process->ReadUnsignedIntegerFromMemory(item_info_version_address,
693
0
                                                     2, 0, error);
694
0
        if (error.Success()) {
695
0
          m_lib_backtrace_recording_info.item_info_data_offset =
696
0
              m_process->ReadUnsignedIntegerFromMemory(
697
0
                  item_info_data_offset_address, 2, 0, error);
698
0
          if (!error.Success()) {
699
0
            m_lib_backtrace_recording_info.queue_info_version = 0;
700
0
          }
701
0
        } else {
702
0
          m_lib_backtrace_recording_info.queue_info_version = 0;
703
0
        }
704
0
      } else {
705
0
        m_lib_backtrace_recording_info.queue_info_version = 0;
706
0
      }
707
0
    }
708
0
  }
709
710
4
  return m_lib_backtrace_recording_info.queue_info_version != 0;
711
4
}
712
713
const std::vector<ConstString> &
714
0
SystemRuntimeMacOSX::GetExtendedBacktraceTypes() {
715
0
  if (m_types.size() == 0) {
716
0
    m_types.push_back(ConstString("libdispatch"));
717
0
    m_types.push_back(ConstString("Application Specific Backtrace"));
718
    // We could have pthread as another type in the future if we have a way of
719
    // gathering that information & it's useful to distinguish between them.
720
0
  }
721
0
  return m_types;
722
0
}
723
724
void SystemRuntimeMacOSX::PopulateQueueList(
725
4
    lldb_private::QueueList &queue_list) {
726
4
  if (BacktraceRecordingHeadersInitialized()) {
727
0
    AppleGetQueuesHandler::GetQueuesReturnInfo queue_info_pointer;
728
0
    ThreadSP cur_thread_sp(
729
0
        m_process->GetThreadList().GetExpressionExecutionThread());
730
0
    if (cur_thread_sp) {
731
0
      Status error;
732
0
      queue_info_pointer = m_get_queues_handler.GetCurrentQueues(
733
0
          *cur_thread_sp.get(), m_page_to_free, m_page_to_free_size, error);
734
0
      m_page_to_free = LLDB_INVALID_ADDRESS;
735
0
      m_page_to_free_size = 0;
736
0
      if (error.Success()) {
737
738
0
        if (queue_info_pointer.count > 0 &&
739
0
            queue_info_pointer.queues_buffer_size > 0 &&
740
0
            queue_info_pointer.queues_buffer_ptr != 0 &&
741
0
            queue_info_pointer.queues_buffer_ptr != LLDB_INVALID_ADDRESS) {
742
0
          PopulateQueuesUsingLibBTR(queue_info_pointer.queues_buffer_ptr,
743
0
                                    queue_info_pointer.queues_buffer_size,
744
0
                                    queue_info_pointer.count, queue_list);
745
0
        }
746
0
      }
747
0
    }
748
0
  }
749
750
  // We either didn't have libBacktraceRecording (and need to create the queues
751
  // list based on threads) or we did get the queues list from
752
  // libBacktraceRecording but some special queues may not be included in its
753
  // information.  This is needed because libBacktraceRecording will only list
754
  // queues with pending or running items by default - but the magic com.apple
755
  // .main-thread queue on thread 1 is always around.
756
757
56
  for (ThreadSP thread_sp : m_process->Threads()) {
758
56
    if (thread_sp->GetAssociatedWithLibdispatchQueue() != eLazyBoolNo) {
759
52
      if (thread_sp->GetQueueID() != LLDB_INVALID_QUEUE_ID) {
760
52
        if (queue_list.FindQueueByID(thread_sp->GetQueueID()).get() ==
761
52
            nullptr) {
762
40
          QueueSP queue_sp(new Queue(m_process->shared_from_this(),
763
40
                                     thread_sp->GetQueueID(),
764
40
                                     thread_sp->GetQueueName()));
765
40
          if (thread_sp->ThreadHasQueueInformation()) {
766
40
            queue_sp->SetKind(thread_sp->GetQueueKind());
767
40
            queue_sp->SetLibdispatchQueueAddress(
768
40
                thread_sp->GetQueueLibdispatchQueueAddress());
769
40
            queue_list.AddQueue(queue_sp);
770
40
          } else {
771
0
            queue_sp->SetKind(
772
0
                GetQueueKind(thread_sp->GetQueueLibdispatchQueueAddress()));
773
0
            queue_sp->SetLibdispatchQueueAddress(
774
0
                thread_sp->GetQueueLibdispatchQueueAddress());
775
0
            queue_list.AddQueue(queue_sp);
776
0
          }
777
40
        }
778
52
      }
779
52
    }
780
56
  }
781
4
}
782
783
// Returns either an array of introspection_dispatch_item_info_ref's for the
784
// pending items on a queue or an array introspection_dispatch_item_info_ref's
785
// and code addresses for the pending items on a queue.  The information about
786
// each of these pending items then needs to be fetched individually by passing
787
// the ref to libBacktraceRecording.
788
789
SystemRuntimeMacOSX::PendingItemsForQueue
790
0
SystemRuntimeMacOSX::GetPendingItemRefsForQueue(lldb::addr_t queue) {
791
0
  PendingItemsForQueue pending_item_refs = {};
792
0
  AppleGetPendingItemsHandler::GetPendingItemsReturnInfo pending_items_pointer;
793
0
  ThreadSP cur_thread_sp(
794
0
      m_process->GetThreadList().GetExpressionExecutionThread());
795
0
  if (cur_thread_sp) {
796
0
    Status error;
797
0
    pending_items_pointer = m_get_pending_items_handler.GetPendingItems(
798
0
        *cur_thread_sp.get(), queue, m_page_to_free, m_page_to_free_size,
799
0
        error);
800
0
    m_page_to_free = LLDB_INVALID_ADDRESS;
801
0
    m_page_to_free_size = 0;
802
0
    if (error.Success()) {
803
0
      if (pending_items_pointer.count > 0 &&
804
0
          pending_items_pointer.items_buffer_size > 0 &&
805
0
          pending_items_pointer.items_buffer_ptr != 0 &&
806
0
          pending_items_pointer.items_buffer_ptr != LLDB_INVALID_ADDRESS) {
807
0
        DataBufferHeap data(pending_items_pointer.items_buffer_size, 0);
808
0
        if (m_process->ReadMemory(
809
0
                pending_items_pointer.items_buffer_ptr, data.GetBytes(),
810
0
                pending_items_pointer.items_buffer_size, error)) {
811
0
          DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
812
0
                                  m_process->GetByteOrder(),
813
0
                                  m_process->GetAddressByteSize());
814
815
          // We either have an array of
816
          //    void* item_ref
817
          // (old style) or we have a structure returned which looks like
818
          //
819
          // struct introspection_dispatch_pending_item_info_s {
820
          //   void *item_ref;
821
          //   void *function_or_block;
822
          // };
823
          //
824
          // struct introspection_dispatch_pending_items_array_s {
825
          //   uint32_t version;
826
          //   uint32_t size_of_item_info;
827
          //   introspection_dispatch_pending_item_info_s items[];
828
          //   }
829
830
0
          offset_t offset = 0;
831
0
          uint64_t i = 0;
832
0
          uint32_t version = extractor.GetU32(&offset);
833
0
          if (version == 1) {
834
0
            pending_item_refs.new_style = true;
835
0
            uint32_t item_size = extractor.GetU32(&offset);
836
0
            uint32_t start_of_array_offset = offset;
837
0
            while (offset < pending_items_pointer.items_buffer_size &&
838
0
                   i < pending_items_pointer.count) {
839
0
              offset = start_of_array_offset + (i * item_size);
840
0
              ItemRefAndCodeAddress item;
841
0
              item.item_ref = extractor.GetAddress(&offset);
842
0
              item.code_address = extractor.GetAddress(&offset);
843
0
              pending_item_refs.item_refs_and_code_addresses.push_back(item);
844
0
              i++;
845
0
            }
846
0
          } else {
847
0
            offset = 0;
848
0
            pending_item_refs.new_style = false;
849
0
            while (offset < pending_items_pointer.items_buffer_size &&
850
0
                   i < pending_items_pointer.count) {
851
0
              ItemRefAndCodeAddress item;
852
0
              item.item_ref = extractor.GetAddress(&offset);
853
0
              item.code_address = LLDB_INVALID_ADDRESS;
854
0
              pending_item_refs.item_refs_and_code_addresses.push_back(item);
855
0
              i++;
856
0
            }
857
0
          }
858
0
        }
859
0
        m_page_to_free = pending_items_pointer.items_buffer_ptr;
860
0
        m_page_to_free_size = pending_items_pointer.items_buffer_size;
861
0
      }
862
0
    }
863
0
  }
864
0
  return pending_item_refs;
865
0
}
866
867
0
void SystemRuntimeMacOSX::PopulatePendingItemsForQueue(Queue *queue) {
868
0
  if (BacktraceRecordingHeadersInitialized()) {
869
0
    PendingItemsForQueue pending_item_refs =
870
0
        GetPendingItemRefsForQueue(queue->GetLibdispatchQueueAddress());
871
0
    for (ItemRefAndCodeAddress pending_item :
872
0
         pending_item_refs.item_refs_and_code_addresses) {
873
0
      Address addr;
874
0
      m_process->GetTarget().ResolveLoadAddress(pending_item.code_address,
875
0
                                                addr);
876
0
      QueueItemSP queue_item_sp(new QueueItem(queue->shared_from_this(),
877
0
                                              m_process->shared_from_this(),
878
0
                                              pending_item.item_ref, addr));
879
0
      queue->PushPendingQueueItem(queue_item_sp);
880
0
    }
881
0
  }
882
0
}
883
884
void SystemRuntimeMacOSX::CompleteQueueItem(QueueItem *queue_item,
885
0
                                            addr_t item_ref) {
886
0
  AppleGetItemInfoHandler::GetItemInfoReturnInfo ret;
887
888
0
  ThreadSP cur_thread_sp(
889
0
      m_process->GetThreadList().GetExpressionExecutionThread());
890
0
  Status error;
891
0
  ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref,
892
0
                                            m_page_to_free, m_page_to_free_size,
893
0
                                            error);
894
0
  m_page_to_free = LLDB_INVALID_ADDRESS;
895
0
  m_page_to_free_size = 0;
896
0
  if (ret.item_buffer_ptr != 0 && ret.item_buffer_ptr != LLDB_INVALID_ADDRESS &&
897
0
      ret.item_buffer_size > 0) {
898
0
    DataBufferHeap data(ret.item_buffer_size, 0);
899
0
    if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(),
900
0
                              ret.item_buffer_size, error) &&
901
0
        error.Success()) {
902
0
      DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
903
0
                              m_process->GetByteOrder(),
904
0
                              m_process->GetAddressByteSize());
905
0
      ItemInfo item = ExtractItemInfoFromBuffer(extractor);
906
0
      queue_item->SetItemThatEnqueuedThis(item.item_that_enqueued_this);
907
0
      queue_item->SetEnqueueingThreadID(item.enqueuing_thread_id);
908
0
      queue_item->SetEnqueueingQueueID(item.enqueuing_queue_serialnum);
909
0
      queue_item->SetStopID(item.stop_id);
910
0
      queue_item->SetEnqueueingBacktrace(item.enqueuing_callstack);
911
0
      queue_item->SetThreadLabel(item.enqueuing_thread_label);
912
0
      queue_item->SetQueueLabel(item.enqueuing_queue_label);
913
0
      queue_item->SetTargetQueueLabel(item.target_queue_label);
914
0
    }
915
0
    m_page_to_free = ret.item_buffer_ptr;
916
0
    m_page_to_free_size = ret.item_buffer_size;
917
0
  }
918
0
}
919
920
void SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR(
921
    lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count,
922
0
    lldb_private::QueueList &queue_list) {
923
0
  Status error;
924
0
  DataBufferHeap data(queues_buffer_size, 0);
925
0
  Log *log = GetLog(LLDBLog::SystemRuntime);
926
0
  if (m_process->ReadMemory(queues_buffer, data.GetBytes(), queues_buffer_size,
927
0
                            error) == queues_buffer_size &&
928
0
      error.Success()) {
929
    // We've read the information out of inferior memory; free it on the next
930
    // call we make
931
0
    m_page_to_free = queues_buffer;
932
0
    m_page_to_free_size = queues_buffer_size;
933
934
0
    DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
935
0
                            m_process->GetByteOrder(),
936
0
                            m_process->GetAddressByteSize());
937
0
    offset_t offset = 0;
938
0
    uint64_t queues_read = 0;
939
940
    // The information about the queues is stored in this format (v1): typedef
941
    // struct introspection_dispatch_queue_info_s {
942
    //     uint32_t offset_to_next;
943
    //     dispatch_queue_t queue;
944
    //     uint64_t serialnum;     // queue's serialnum in the process, as
945
    //     provided by libdispatch
946
    //     uint32_t running_work_items_count;
947
    //     uint32_t pending_work_items_count;
948
    //
949
    //     char data[];     // Starting here, we have variable-length data:
950
    //     // char queue_label[];
951
    // } introspection_dispatch_queue_info_s;
952
953
0
    while (queues_read < count && offset < queues_buffer_size) {
954
0
      offset_t start_of_this_item = offset;
955
956
0
      uint32_t offset_to_next = extractor.GetU32(&offset);
957
958
0
      offset += 4; // Skip over the 4 bytes of reserved space
959
0
      addr_t queue = extractor.GetAddress(&offset);
960
0
      uint64_t serialnum = extractor.GetU64(&offset);
961
0
      uint32_t running_work_items_count = extractor.GetU32(&offset);
962
0
      uint32_t pending_work_items_count = extractor.GetU32(&offset);
963
964
      // Read the first field of the variable length data
965
0
      offset = start_of_this_item +
966
0
               m_lib_backtrace_recording_info.queue_info_data_offset;
967
0
      const char *queue_label = extractor.GetCStr(&offset);
968
0
      if (queue_label == nullptr)
969
0
        queue_label = "";
970
971
0
      offset_t start_of_next_item = start_of_this_item + offset_to_next;
972
0
      offset = start_of_next_item;
973
974
0
      LLDB_LOGF(log,
975
0
                "SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR added "
976
0
                "queue with dispatch_queue_t 0x%" PRIx64
977
0
                ", serial number 0x%" PRIx64
978
0
                ", running items %d, pending items %d, name '%s'",
979
0
                queue, serialnum, running_work_items_count,
980
0
                pending_work_items_count, queue_label);
981
982
0
      QueueSP queue_sp(
983
0
          new Queue(m_process->shared_from_this(), serialnum, queue_label));
984
0
      queue_sp->SetNumRunningWorkItems(running_work_items_count);
985
0
      queue_sp->SetNumPendingWorkItems(pending_work_items_count);
986
0
      queue_sp->SetLibdispatchQueueAddress(queue);
987
0
      queue_sp->SetKind(GetQueueKind(queue));
988
0
      queue_list.AddQueue(queue_sp);
989
0
      queues_read++;
990
0
    }
991
0
  }
992
0
}
993
994
SystemRuntimeMacOSX::ItemInfo SystemRuntimeMacOSX::ExtractItemInfoFromBuffer(
995
0
    lldb_private::DataExtractor &extractor) {
996
0
  ItemInfo item;
997
998
0
  offset_t offset = 0;
999
1000
0
  item.item_that_enqueued_this = extractor.GetAddress(&offset);
1001
0
  item.function_or_block = extractor.GetAddress(&offset);
1002
0
  item.enqueuing_thread_id = extractor.GetU64(&offset);
1003
0
  item.enqueuing_queue_serialnum = extractor.GetU64(&offset);
1004
0
  item.target_queue_serialnum = extractor.GetU64(&offset);
1005
0
  item.enqueuing_callstack_frame_count = extractor.GetU32(&offset);
1006
0
  item.stop_id = extractor.GetU32(&offset);
1007
1008
0
  offset = m_lib_backtrace_recording_info.item_info_data_offset;
1009
1010
0
  for (uint32_t i = 0; i < item.enqueuing_callstack_frame_count; i++) {
1011
0
    item.enqueuing_callstack.push_back(extractor.GetAddress(&offset));
1012
0
  }
1013
0
  item.enqueuing_thread_label = extractor.GetCStr(&offset);
1014
0
  item.enqueuing_queue_label = extractor.GetCStr(&offset);
1015
0
  item.target_queue_label = extractor.GetCStr(&offset);
1016
1017
0
  return item;
1018
0
}
1019
1020
3.95k
void SystemRuntimeMacOSX::Initialize() {
1021
3.95k
  PluginManager::RegisterPlugin(
1022
3.95k
      GetPluginNameStatic(),
1023
3.95k
      "System runtime plugin for Mac OS X native libraries.", CreateInstance);
1024
3.95k
}
1025
1026
3.94k
void SystemRuntimeMacOSX::Terminate() {
1027
3.94k
  PluginManager::UnregisterPlugin(CreateInstance);
1028
3.94k
}