forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[lldb] Swift OS plugin #9839
Draft
felipepiovezan
wants to merge
4
commits into
swiftlang:stable/20240723
Choose a base branch
from
felipepiovezan:felipe/swift_os_plugin_rebase_61
base: stable/20240723
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[lldb] Swift OS plugin #9839
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ed1b8ba
[lldb] WORKAROUND for threadmemory stop reason circular issue
felipepiovezan ba90ba9
[lldb][swift] Add helper function to find Swift concurrency's module …
felipepiovezan a267f0f
[lldb][swift] Add helper function to find Task addresses in TLS
felipepiovezan 9bd7fa3
[lldb][swift] Swift OS Plugin
felipepiovezan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
if (LLDB_ENABLE_PYTHON) | ||
add_subdirectory(Python) | ||
add_subdirectory(SwiftTasks) | ||
endif() |
12 changes: 12 additions & 0 deletions
12
lldb/source/Plugins/OperatingSystem/SwiftTasks/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
add_lldb_library(lldbPluginOperatingSystemSwiftTasks PLUGIN | ||
OperatingSystemSwiftTasks.cpp | ||
|
||
LINK_LIBS | ||
lldbCore | ||
lldbInterpreter | ||
lldbSymbol | ||
lldbTarget | ||
lldbValueObject | ||
lldbPluginProcessUtility | ||
lldbPluginSwiftLanguageRuntime | ||
) |
151 changes: 151 additions & 0 deletions
151
lldb/source/Plugins/OperatingSystem/SwiftTasks/OperatingSystemSwiftTasks.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
//===-- OperatingSystemSwiftTasks.cpp -------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if LLDB_ENABLE_SWIFT | ||
|
||
#include "OperatingSystemSwiftTasks.h" | ||
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h" | ||
#include "Plugins/Process/Utility/ThreadMemory.h" | ||
#include "lldb/Core/Debugger.h" | ||
#include "lldb/Core/Module.h" | ||
#include "lldb/Core/PluginManager.h" | ||
#include "lldb/Target/Process.h" | ||
#include "lldb/Target/Thread.h" | ||
#include "lldb/Target/ThreadList.h" | ||
#include "lldb/Utility/LLDBLog.h" | ||
|
||
#include <memory> | ||
|
||
using namespace lldb; | ||
using namespace lldb_private; | ||
|
||
LLDB_PLUGIN_DEFINE(OperatingSystemSwiftTasks) | ||
|
||
void OperatingSystemSwiftTasks::Initialize() { | ||
PluginManager::RegisterPlugin(GetPluginNameStatic(), | ||
GetPluginDescriptionStatic(), CreateInstance, | ||
nullptr); | ||
} | ||
|
||
void OperatingSystemSwiftTasks::Terminate() { | ||
PluginManager::UnregisterPlugin(CreateInstance); | ||
} | ||
|
||
OperatingSystem *OperatingSystemSwiftTasks::CreateInstance(Process *process, | ||
bool force) { | ||
if (!process) | ||
return nullptr; | ||
|
||
Log *log = GetLog(LLDBLog::OS); | ||
std::optional<uint32_t> concurrency_version = | ||
SwiftLanguageRuntime::findConcurrencyDebugVersion(*process); | ||
if (!concurrency_version) { | ||
LLDB_LOG(log, | ||
"OperatingSystemSwiftTasks: did not find concurrency module."); | ||
return nullptr; | ||
} | ||
|
||
if (*concurrency_version <= 1) | ||
return new OperatingSystemSwiftTasks(*process); | ||
LLDB_LOGF(log, | ||
"OperatingSystemSwiftTasks: got a concurrency version symbol of %u", | ||
*concurrency_version); | ||
return nullptr; | ||
} | ||
|
||
llvm::StringRef OperatingSystemSwiftTasks::GetPluginDescriptionStatic() { | ||
return "Operating system plug-in converting Swift Tasks into Threads."; | ||
} | ||
|
||
OperatingSystemSwiftTasks::~OperatingSystemSwiftTasks() = default; | ||
|
||
OperatingSystemSwiftTasks::OperatingSystemSwiftTasks( | ||
lldb_private::Process &process) | ||
: OperatingSystem(&process) { | ||
size_t ptr_size = process.GetAddressByteSize(); | ||
// Offset of a Task ID inside a Task data structure, guaranteed by the ABI. | ||
// See Job in swift/RemoteInspection/RuntimeInternals.h. | ||
m_task_id_offset = 4 * ptr_size + 4; | ||
} | ||
|
||
bool OperatingSystemSwiftTasks::UpdateThreadList(ThreadList &old_thread_list, | ||
ThreadList &core_thread_list, | ||
ThreadList &new_thread_list) { | ||
Log *log = GetLog(LLDBLog::OS); | ||
LLDB_LOG(log, "OperatingSystemSwiftTasks: Updating thread list"); | ||
|
||
for (const ThreadSP &real_thread : core_thread_list.Threads()) { | ||
std::optional<uint64_t> task_id = FindTaskId(*real_thread); | ||
|
||
// If this is not a thread running a Task, add it to the list as is. | ||
if (!task_id) { | ||
new_thread_list.AddThread(real_thread); | ||
LLDB_LOGF(log, | ||
"OperatingSystemSwiftTasks: thread %" PRIx64 | ||
" is not executing a Task", | ||
real_thread->GetID()); | ||
continue; | ||
} | ||
|
||
// Mask higher bits to avoid conflicts with core thread IDs. | ||
uint64_t masked_task_id = 0x0000000f00000000 | *task_id; | ||
|
||
ThreadSP swift_thread = [&]() -> ThreadSP { | ||
// If we already had a thread for this Task in the last stop, re-use it. | ||
if (ThreadSP old_thread = old_thread_list.FindThreadByID(masked_task_id); | ||
IsOperatingSystemPluginThread(old_thread)) | ||
return old_thread; | ||
|
||
std::string name = llvm::formatv("Swift Task {0:x}", *task_id); | ||
llvm::StringRef queue_name = ""; | ||
return std::make_shared<ThreadMemory>(*m_process, masked_task_id, name, | ||
queue_name, | ||
/*register_data_addr*/ 0); | ||
}(); | ||
|
||
swift_thread->SetBackingThread(real_thread); | ||
new_thread_list.AddThread(swift_thread); | ||
LLDB_LOGF(log, | ||
"OperatingSystemSwiftTasks: mapping thread IDs: %" PRIx64 | ||
" -> %" PRIx64, | ||
real_thread->GetID(), swift_thread->GetID()); | ||
} | ||
return true; | ||
} | ||
|
||
void OperatingSystemSwiftTasks::ThreadWasSelected(Thread *thread) {} | ||
|
||
RegisterContextSP OperatingSystemSwiftTasks::CreateRegisterContextForThread( | ||
Thread *thread, addr_t reg_data_addr) { | ||
if (!thread || !IsOperatingSystemPluginThread(thread->shared_from_this())) | ||
return nullptr; | ||
return thread->GetRegisterContext(); | ||
} | ||
|
||
StopInfoSP OperatingSystemSwiftTasks::CreateThreadStopReason( | ||
lldb_private::Thread *thread) { | ||
return thread->GetStopInfo(); | ||
} | ||
|
||
std::optional<uint64_t> OperatingSystemSwiftTasks::FindTaskId(Thread &thread) { | ||
std::optional<addr_t> task_addr = GetTaskAddrFromThreadLocalStorage(thread); | ||
if (!task_addr) | ||
return {}; | ||
|
||
Status error; | ||
// The Task ID is at offset m_task_id_offset from the Task pointer. | ||
constexpr uint32_t num_bytes_task_id = 4; | ||
auto task_id = m_process->ReadUnsignedIntegerFromMemory( | ||
*task_addr + m_task_id_offset, num_bytes_task_id, LLDB_INVALID_ADDRESS, | ||
error); | ||
if (error.Fail()) | ||
return {}; | ||
return task_id; | ||
} | ||
|
||
#endif // #if LLDB_ENABLE_SWIFT |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to refresh reviewer's memories: this is the thread plan to "run through a task switch and stop when we schedule this same async function again. This plan now sets thread specific breakpoints, since they are now task breakpoints!
If we somehow push this type of plan without having the OS plugin, we are at a risk of breaking in the wrong task. But if the plugin is not there and there are tasks, we are going to have much bigger problems anyway.