1616#include " SwiftMetadataCache.h"
1717
1818#include " Plugins/ExpressionParser/Swift/SwiftPersistentExpressionState.h"
19+ #include " Plugins/LanguageRuntime/Swift/SwiftTask.h"
1920#include " Plugins/Process/Utility/RegisterContext_x86.h"
2021#include " Plugins/TypeSystem/Clang/TypeSystemClang.h"
2122#include " Plugins/TypeSystem/Swift/SwiftDemangle.h"
@@ -2083,6 +2084,89 @@ class CommandObjectSwift_RefCount : public CommandObjectRaw {
20832084 }
20842085};
20852086
2087+ class CommandObjectLanguageSwiftTaskBacktrace final
2088+ : public CommandObjectParsed {
2089+ public:
2090+ CommandObjectLanguageSwiftTaskBacktrace (CommandInterpreter &interpreter)
2091+ : CommandObjectParsed(interpreter, " backtrace" ,
2092+ " Show the backtrace of Swift tasks. See `thread "
2093+ " backtrace` for customizing backtrace output." ,
2094+ " language swift task backtrace <variable-name>" ) {
2095+ AddSimpleArgumentList (eArgTypeVarName);
2096+ }
2097+
2098+ private:
2099+ void DoExecute (Args &command, CommandReturnObject &result) override {
2100+ if (!m_exe_ctx.GetFramePtr ()) {
2101+ result.AppendError (" no active frame selected" );
2102+ return ;
2103+ }
2104+
2105+ if (command.empty () || command[0 ].ref ().empty ()) {
2106+ result.AppendError (" no task variable" );
2107+ return ;
2108+ }
2109+
2110+ StackFrame &frame = m_exe_ctx.GetFrameRef ();
2111+ uint32_t path_options =
2112+ StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
2113+ VariableSP var_sp;
2114+ Status status;
2115+ ValueObjectSP valobj_sp = frame.GetValueForVariableExpressionPath (
2116+ command[0 ].c_str (), eDynamicDontRunTarget, path_options, var_sp,
2117+ status);
2118+ if (!valobj_sp) {
2119+ result.AppendError (status.AsCString ());
2120+ return ;
2121+ }
2122+
2123+ addr_t task_ptr = LLDB_INVALID_ADDRESS;
2124+ ThreadSafeReflectionContext reflection_ctx;
2125+ if (ValueObjectSP task_obj_sp =
2126+ valobj_sp->GetChildMemberWithName (" _task" )) {
2127+ task_ptr = task_obj_sp->GetValueAsUnsigned (LLDB_INVALID_ADDRESS);
2128+ if (task_ptr != LLDB_INVALID_ADDRESS)
2129+ if (auto *runtime = SwiftLanguageRuntime::Get (m_exe_ctx.GetProcessSP ()))
2130+ reflection_ctx = runtime->GetReflectionContext ();
2131+ }
2132+ if (task_ptr == LLDB_INVALID_ADDRESS || !reflection_ctx) {
2133+ result.AppendError (" failed to access Task data from runtime" );
2134+ return ;
2135+ }
2136+
2137+ llvm::Expected<ReflectionContextInterface::AsyncTaskInfo> task_info =
2138+ reflection_ctx->asyncTaskInfo (task_ptr);
2139+ if (auto error = task_info.takeError ()) {
2140+ result.AppendError (toString (std::move (error)));
2141+ return ;
2142+ }
2143+
2144+ auto thread_task = ThreadTask::Create (
2145+ task_info->id , task_info->resumeAsyncContext , m_exe_ctx);
2146+ if (auto error = thread_task.takeError ()) {
2147+ result.AppendError (toString (std::move (error)));
2148+ return ;
2149+ }
2150+
2151+ // GetStatus prints the backtrace.
2152+ thread_task.get ()->GetStatus (result.GetOutputStream (), 0 , UINT32_MAX, 0 ,
2153+ false , true );
2154+ result.SetStatus (lldb::eReturnStatusSuccessFinishResult);
2155+ }
2156+ };
2157+
2158+ class CommandObjectLanguageSwiftTask final : public CommandObjectMultiword {
2159+ public:
2160+ CommandObjectLanguageSwiftTask (CommandInterpreter &interpreter)
2161+ : CommandObjectMultiword(
2162+ interpreter, " task" , " Commands for inspecting Swift Tasks." ,
2163+ " language swift task <subcommand> [<subcommand-options>]" ) {
2164+ LoadSubCommand (" backtrace" ,
2165+ CommandObjectSP (new CommandObjectLanguageSwiftTaskBacktrace (
2166+ interpreter)));
2167+ }
2168+ };
2169+
20862170class CommandObjectMultiwordSwift : public CommandObjectMultiword {
20872171public:
20882172 CommandObjectMultiwordSwift (CommandInterpreter &interpreter)
@@ -2094,6 +2178,8 @@ class CommandObjectMultiwordSwift : public CommandObjectMultiword {
20942178 interpreter)));
20952179 LoadSubCommand (" refcount" , CommandObjectSP (new CommandObjectSwift_RefCount (
20962180 interpreter)));
2181+ LoadSubCommand (" task" , CommandObjectSP (new CommandObjectLanguageSwiftTask (
2182+ interpreter)));
20972183 }
20982184
20992185 virtual ~CommandObjectMultiwordSwift () {}
0 commit comments