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"
4748#include " lldb/ValueObject/ValueObjectCast.h"
4849#include " lldb/ValueObject/ValueObjectConstResult.h"
4950#include " lldb/ValueObject/ValueObjectVariable.h"
51+ #include " llvm/Support/FormatAdapters.h"
5052
5153#include " lldb/lldb-enumerations.h"
5254#include " swift/AST/ASTMangler.h"
@@ -2083,6 +2085,84 @@ class CommandObjectSwift_RefCount : public CommandObjectRaw {
20832085 }
20842086};
20852087
2088+ class CommandObjectLanguageSwiftTaskBacktrace final
2089+ : public CommandObjectParsed {
2090+ public:
2091+ CommandObjectLanguageSwiftTaskBacktrace (CommandInterpreter &interpreter)
2092+ : CommandObjectParsed(interpreter, " backtrace" ,
2093+ " Show the backtrace of Swift tasks. See `thread "
2094+ " backtrace` for customizing backtrace output." ,
2095+ " language swift task backtrace <variable-name>" ) {
2096+ AddSimpleArgumentList (eArgTypeVarName);
2097+ }
2098+
2099+ private:
2100+ void DoExecute (Args &command, CommandReturnObject &result) override {
2101+ if (!m_exe_ctx.GetFramePtr ()) {
2102+ result.AppendError (" no active frame selected" );
2103+ return ;
2104+ }
2105+
2106+ if (command[0 ].ref ().empty ()) {
2107+ result.AppendError (" no task variable" );
2108+ return ;
2109+ }
2110+
2111+ StackFrame &frame = m_exe_ctx.GetFrameRef ();
2112+ uint32_t path_options =
2113+ StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
2114+ VariableSP var_sp;
2115+ Status status;
2116+ ValueObjectSP valobj_sp = frame.GetValueForVariableExpressionPath (
2117+ command[0 ].c_str (), eDynamicDontRunTarget, path_options, var_sp,
2118+ status);
2119+ if (!valobj_sp)
2120+ return ;
2121+
2122+ ValueObjectSP task_obj_sp = valobj_sp->GetChildMemberWithName (" _task" );
2123+ if (!task_obj_sp)
2124+ return ;
2125+ uint64_t task_ptr = task_obj_sp->GetValueAsUnsigned (LLDB_INVALID_ADDRESS);
2126+ if (task_ptr == LLDB_INVALID_ADDRESS)
2127+ return ;
2128+ auto *runtime = SwiftLanguageRuntime::Get (m_exe_ctx.GetProcessSP ());
2129+ if (!runtime)
2130+ return ;
2131+ ThreadSafeReflectionContext reflection_ctx =
2132+ runtime->GetReflectionContext ();
2133+ llvm::Expected<ReflectionContextInterface::AsyncTaskInfo> task_info =
2134+ reflection_ctx->asyncTaskInfo (task_ptr);
2135+ if (auto err = task_info.takeError ()) {
2136+ LLDB_LOG (GetLog (LLDBLog::DataFormatters | LLDBLog::Types),
2137+ " could not get info for async task {0:x}: {1}" , task_ptr,
2138+ fmt_consume (std::move (err)));
2139+ return ;
2140+ }
2141+
2142+ auto thread_task = ThreadTask::Create (
2143+ task_info->id , task_info->resumeAsyncContext , m_exe_ctx);
2144+ if (auto error = thread_task.takeError ()) {
2145+ result.AppendError (toString (std::move (error)));
2146+ return ;
2147+ }
2148+ thread_task.get ()->GetStatus (result.GetOutputStream (), 0 , UINT32_MAX, 0 ,
2149+ false , false );
2150+ result.SetStatus (lldb::eReturnStatusSuccessFinishResult);
2151+ }
2152+ };
2153+
2154+ class CommandObjectLanguageSwiftTask final : public CommandObjectMultiword {
2155+ public:
2156+ CommandObjectLanguageSwiftTask (CommandInterpreter &interpreter)
2157+ : CommandObjectMultiword(
2158+ interpreter, " task" , " Commands for inspecting Swift Tasks." ,
2159+ " language swift task <subcommand> [<subcommand-options>]" ) {
2160+ LoadSubCommand (" backtrace" ,
2161+ CommandObjectSP (new CommandObjectLanguageSwiftTaskBacktrace (
2162+ interpreter)));
2163+ }
2164+ };
2165+
20862166class CommandObjectMultiwordSwift : public CommandObjectMultiword {
20872167public:
20882168 CommandObjectMultiwordSwift (CommandInterpreter &interpreter)
@@ -2094,6 +2174,8 @@ class CommandObjectMultiwordSwift : public CommandObjectMultiword {
20942174 interpreter)));
20952175 LoadSubCommand (" refcount" , CommandObjectSP (new CommandObjectSwift_RefCount (
20962176 interpreter)));
2177+ LoadSubCommand (" task" , CommandObjectSP (new CommandObjectLanguageSwiftTask (
2178+ interpreter)));
20972179 }
20982180
20992181 virtual ~CommandObjectMultiwordSwift () {}
0 commit comments