|
| 1 | +//===-- OperatingSystemSwiftTasks.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 | +#if LLDB_ENABLE_SWIFT |
| 10 | + |
| 11 | +#include "OperatingSystemSwiftTasks.h" |
| 12 | +#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h" |
| 13 | +#include "Plugins/Process/Utility/ThreadMemory.h" |
| 14 | +#include "lldb/Core/Debugger.h" |
| 15 | +#include "lldb/Core/Module.h" |
| 16 | +#include "lldb/Core/PluginManager.h" |
| 17 | +#include "lldb/Target/Process.h" |
| 18 | +#include "lldb/Target/Thread.h" |
| 19 | +#include "lldb/Target/ThreadList.h" |
| 20 | +#include "lldb/Utility/LLDBLog.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | + |
| 24 | +using namespace lldb; |
| 25 | +using namespace lldb_private; |
| 26 | + |
| 27 | +LLDB_PLUGIN_DEFINE(OperatingSystemSwiftTasks) |
| 28 | + |
| 29 | +void OperatingSystemSwiftTasks::Initialize() { |
| 30 | + PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 31 | + GetPluginDescriptionStatic(), CreateInstance, |
| 32 | + nullptr); |
| 33 | +} |
| 34 | + |
| 35 | +void OperatingSystemSwiftTasks::Terminate() { |
| 36 | + PluginManager::UnregisterPlugin(CreateInstance); |
| 37 | +} |
| 38 | + |
| 39 | +OperatingSystem *OperatingSystemSwiftTasks::CreateInstance(Process *process, |
| 40 | + bool force) { |
| 41 | + if (!process || !process->GetTarget().GetSwiftUseTasksPlugin()) |
| 42 | + return nullptr; |
| 43 | + |
| 44 | + Log *log = GetLog(LLDBLog::OS); |
| 45 | + std::optional<uint32_t> concurrency_version = |
| 46 | + SwiftLanguageRuntime::FindConcurrencyDebugVersion(*process); |
| 47 | + if (!concurrency_version) { |
| 48 | + LLDB_LOG(log, |
| 49 | + "OperatingSystemSwiftTasks: did not find concurrency module."); |
| 50 | + return nullptr; |
| 51 | + } |
| 52 | + |
| 53 | + LLDB_LOGF(log, |
| 54 | + "OperatingSystemSwiftTasks: got a concurrency version symbol of %u", |
| 55 | + *concurrency_version); |
| 56 | + if (*concurrency_version > 1) { |
| 57 | + auto warning = |
| 58 | + llvm::formatv("Unexpected Swift concurrency version {0}. Stepping on " |
| 59 | + "concurrent code may behave incorrectly.", |
| 60 | + *concurrency_version); |
| 61 | + lldb::user_id_t debugger_id = process->GetTarget().GetDebugger().GetID(); |
| 62 | + static std::once_flag concurrency_warning_flag; |
| 63 | + Debugger::ReportWarning(warning, debugger_id, &concurrency_warning_flag); |
| 64 | + return nullptr; |
| 65 | + } |
| 66 | + return new OperatingSystemSwiftTasks(*process); |
| 67 | +} |
| 68 | + |
| 69 | +llvm::StringRef OperatingSystemSwiftTasks::GetPluginDescriptionStatic() { |
| 70 | + return "Operating system plug-in converting Swift Tasks into Threads."; |
| 71 | +} |
| 72 | + |
| 73 | +OperatingSystemSwiftTasks::~OperatingSystemSwiftTasks() = default; |
| 74 | + |
| 75 | +OperatingSystemSwiftTasks::OperatingSystemSwiftTasks( |
| 76 | + lldb_private::Process &process) |
| 77 | + : OperatingSystem(&process) { |
| 78 | + size_t ptr_size = process.GetAddressByteSize(); |
| 79 | + // Offset of a Task ID inside a Task data structure, guaranteed by the ABI. |
| 80 | + // See Job in swift/RemoteInspection/RuntimeInternals.h. |
| 81 | + m_job_id_offset = 4 * ptr_size + 4; |
| 82 | +} |
| 83 | + |
| 84 | +ThreadSP |
| 85 | +OperatingSystemSwiftTasks::FindOrCreateSwiftThread(ThreadList &old_thread_list, |
| 86 | + uint64_t task_id) { |
| 87 | + // Mask higher bits to avoid conflicts with core thread IDs. |
| 88 | + uint64_t masked_task_id = 0x0000000f00000000 | task_id; |
| 89 | + |
| 90 | + // If we already had a thread for this Task in the last stop, re-use it. |
| 91 | + if (ThreadSP old_thread = old_thread_list.FindThreadByID(masked_task_id); |
| 92 | + IsOperatingSystemPluginThread(old_thread)) |
| 93 | + return old_thread; |
| 94 | + |
| 95 | + std::string name = llvm::formatv("Swift Task {0:x}", task_id); |
| 96 | + llvm::StringRef queue_name = ""; |
| 97 | + return std::make_shared<ThreadMemory>(*m_process, masked_task_id, name, |
| 98 | + queue_name, |
| 99 | + /*register_data_addr*/ 0); |
| 100 | +} |
| 101 | + |
| 102 | +bool OperatingSystemSwiftTasks::UpdateThreadList(ThreadList &old_thread_list, |
| 103 | + ThreadList &core_thread_list, |
| 104 | + ThreadList &new_thread_list) { |
| 105 | + Log *log = GetLog(LLDBLog::OS); |
| 106 | + LLDB_LOG(log, "OperatingSystemSwiftTasks: Updating thread list"); |
| 107 | + |
| 108 | + for (const ThreadSP &real_thread : core_thread_list.Threads()) { |
| 109 | + std::optional<uint64_t> task_id = FindTaskId(*real_thread); |
| 110 | + |
| 111 | + // If this is not a thread running a Task, add it to the list as is. |
| 112 | + if (!task_id) { |
| 113 | + new_thread_list.AddThread(real_thread); |
| 114 | + LLDB_LOGF(log, |
| 115 | + "OperatingSystemSwiftTasks: thread %" PRIx64 |
| 116 | + " is not executing a Task", |
| 117 | + real_thread->GetID()); |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + ThreadSP swift_thread = FindOrCreateSwiftThread(old_thread_list, *task_id); |
| 122 | + swift_thread->SetBackingThread(real_thread); |
| 123 | + new_thread_list.AddThread(swift_thread); |
| 124 | + LLDB_LOGF(log, |
| 125 | + "OperatingSystemSwiftTasks: mapping thread IDs: %" PRIx64 |
| 126 | + " -> %" PRIx64, |
| 127 | + real_thread->GetID(), swift_thread->GetID()); |
| 128 | + } |
| 129 | + return true; |
| 130 | +} |
| 131 | + |
| 132 | +void OperatingSystemSwiftTasks::ThreadWasSelected(Thread *thread) {} |
| 133 | + |
| 134 | +RegisterContextSP OperatingSystemSwiftTasks::CreateRegisterContextForThread( |
| 135 | + Thread *thread, addr_t reg_data_addr) { |
| 136 | + if (!thread || !IsOperatingSystemPluginThread(thread->shared_from_this())) |
| 137 | + return nullptr; |
| 138 | + return thread->GetRegisterContext(); |
| 139 | +} |
| 140 | + |
| 141 | +StopInfoSP OperatingSystemSwiftTasks::CreateThreadStopReason( |
| 142 | + lldb_private::Thread *thread) { |
| 143 | + return thread->GetStopInfo(); |
| 144 | +} |
| 145 | + |
| 146 | +std::optional<uint64_t> OperatingSystemSwiftTasks::FindTaskId(Thread &thread) { |
| 147 | + llvm::Expected<addr_t> task_addr = GetTaskAddrFromThreadLocalStorage(thread); |
| 148 | + if (!task_addr) { |
| 149 | + LLDB_LOG_ERROR(GetLog(LLDBLog::OS), task_addr.takeError(), |
| 150 | + "OperatingSystemSwiftTasks: failed to find task address in " |
| 151 | + "thread local storage: {0}"); |
| 152 | + return {}; |
| 153 | + } |
| 154 | + |
| 155 | + Status error; |
| 156 | + // The Task ID is at offset m_job_id_offset from the Task pointer. |
| 157 | + constexpr uint32_t num_bytes_task_id = 4; |
| 158 | + auto task_id = m_process->ReadUnsignedIntegerFromMemory( |
| 159 | + *task_addr + m_job_id_offset, num_bytes_task_id, LLDB_INVALID_ADDRESS, |
| 160 | + error); |
| 161 | + if (error.Fail()) |
| 162 | + return {}; |
| 163 | + return task_id; |
| 164 | +} |
| 165 | + |
| 166 | +#endif // #if LLDB_ENABLE_SWIFT |
0 commit comments