@@ -2086,6 +2086,46 @@ class CommandObjectSwift_RefCount : public CommandObjectRaw {
2086
2086
}
2087
2087
};
2088
2088
2089
+ // / Construct a `ThreadTask` instance for a Task variable contained in the first
2090
+ // / argument.
2091
+ static llvm::Expected<ThreadSP>
2092
+ ThreadForTaskVariable (Args &command, ExecutionContext &exe_ctx) {
2093
+ if (!exe_ctx.GetFramePtr ())
2094
+ return llvm::createStringError (" no active frame selected" );
2095
+
2096
+ if (command.empty () || command[0 ].ref ().empty ())
2097
+ return llvm::createStringError (" missing task variable argument" );
2098
+
2099
+ StackFrame &frame = exe_ctx.GetFrameRef ();
2100
+ uint32_t path_options =
2101
+ StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
2102
+ VariableSP var_sp;
2103
+ Status status;
2104
+ ValueObjectSP valobj_sp = frame.GetValueForVariableExpressionPath (
2105
+ command[0 ].c_str (), eDynamicDontRunTarget, path_options, var_sp, status);
2106
+ if (!valobj_sp)
2107
+ return status.takeError ();
2108
+
2109
+ addr_t task_ptr = LLDB_INVALID_ADDRESS;
2110
+ ThreadSafeReflectionContext reflection_ctx;
2111
+ if (ValueObjectSP task_obj_sp = valobj_sp->GetChildMemberWithName (" _task" )) {
2112
+ task_ptr = task_obj_sp->GetValueAsUnsigned (LLDB_INVALID_ADDRESS);
2113
+ if (task_ptr != LLDB_INVALID_ADDRESS)
2114
+ if (auto *runtime = SwiftLanguageRuntime::Get (exe_ctx.GetProcessSP ()))
2115
+ reflection_ctx = runtime->GetReflectionContext ();
2116
+ }
2117
+ if (task_ptr == LLDB_INVALID_ADDRESS || !reflection_ctx)
2118
+ return llvm::createStringError (" failed to access Task data from runtime" );
2119
+
2120
+ llvm::Expected<ReflectionContextInterface::AsyncTaskInfo> task_info =
2121
+ reflection_ctx->asyncTaskInfo (task_ptr);
2122
+ if (auto error = task_info.takeError ())
2123
+ return error;
2124
+
2125
+ return ThreadTask::Create (task_info->id , task_info->resumeAsyncContext ,
2126
+ exe_ctx);
2127
+ }
2128
+
2089
2129
class CommandObjectLanguageSwiftTaskBacktrace final
2090
2130
: public CommandObjectParsed {
2091
2131
public:
@@ -2099,60 +2139,44 @@ class CommandObjectLanguageSwiftTaskBacktrace final
2099
2139
2100
2140
private:
2101
2141
void DoExecute (Args &command, CommandReturnObject &result) override {
2102
- if (!m_exe_ctx.GetFramePtr ()) {
2103
- result.AppendError (" no active frame selected" );
2104
- return ;
2105
- }
2106
-
2107
- if (command.empty () || command[0 ].ref ().empty ()) {
2108
- result.AppendError (" no task variable" );
2109
- return ;
2110
- }
2111
-
2112
- StackFrame &frame = m_exe_ctx.GetFrameRef ();
2113
- uint32_t path_options =
2114
- StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
2115
- VariableSP var_sp;
2116
- Status status;
2117
- ValueObjectSP valobj_sp = frame.GetValueForVariableExpressionPath (
2118
- command[0 ].c_str (), eDynamicDontRunTarget, path_options, var_sp,
2119
- status);
2120
- if (!valobj_sp) {
2121
- result.AppendError (status.AsCString ());
2142
+ llvm::Expected<ThreadSP> thread_task =
2143
+ ThreadForTaskVariable (command, m_exe_ctx);
2144
+ if (auto error = thread_task.takeError ()) {
2145
+ result.AppendError (toString (std::move (error)));
2122
2146
return ;
2123
2147
}
2124
2148
2125
- addr_t task_ptr = LLDB_INVALID_ADDRESS;
2126
- ThreadSafeReflectionContext reflection_ctx;
2127
- if (ValueObjectSP task_obj_sp =
2128
- valobj_sp->GetChildMemberWithName (" _task" )) {
2129
- task_ptr = task_obj_sp->GetValueAsUnsigned (LLDB_INVALID_ADDRESS);
2130
- if (task_ptr != LLDB_INVALID_ADDRESS)
2131
- if (auto *runtime = SwiftLanguageRuntime::Get (m_exe_ctx.GetProcessSP ()))
2132
- reflection_ctx = runtime->GetReflectionContext ();
2133
- }
2134
- if (task_ptr == LLDB_INVALID_ADDRESS || !reflection_ctx) {
2135
- result.AppendError (" failed to access Task data from runtime" );
2136
- return ;
2137
- }
2149
+ // GetStatus prints the backtrace.
2150
+ thread_task.get ()->GetStatus (result.GetOutputStream (), 0 , UINT32_MAX, 0 ,
2151
+ false , true );
2152
+ result.SetStatus (lldb::eReturnStatusSuccessFinishResult);
2153
+ }
2154
+ };
2138
2155
2139
- llvm::Expected<ReflectionContextInterface::AsyncTaskInfo> task_info =
2140
- reflection_ctx->asyncTaskInfo (task_ptr);
2141
- if (auto error = task_info.takeError ()) {
2142
- result.AppendError (toString (std::move (error)));
2143
- return ;
2144
- }
2156
+ class CommandObjectLanguageSwiftTaskSelect final : public CommandObjectParsed {
2157
+ public:
2158
+ CommandObjectLanguageSwiftTaskSelect (CommandInterpreter &interpreter)
2159
+ : CommandObjectParsed(
2160
+ interpreter, " select" ,
2161
+ " Change the currently selected thread to thread representation of "
2162
+ " the given Swift Task. See `thread select`." ,
2163
+ " language swift task select <variable-name>" ) {
2164
+ AddSimpleArgumentList (eArgTypeVarName);
2165
+ }
2145
2166
2146
- auto thread_task = ThreadTask::Create (
2147
- task_info->id , task_info->resumeAsyncContext , m_exe_ctx);
2167
+ private:
2168
+ void DoExecute (Args &command, CommandReturnObject &result) override {
2169
+ llvm::Expected<ThreadSP> thread_task =
2170
+ ThreadForTaskVariable (command, m_exe_ctx);
2148
2171
if (auto error = thread_task.takeError ()) {
2149
2172
result.AppendError (toString (std::move (error)));
2150
2173
return ;
2151
2174
}
2152
2175
2153
- // GetStatus prints the backtrace.
2154
- thread_task.get ()->GetStatus (result.GetOutputStream (), 0 , UINT32_MAX, 0 ,
2155
- false , true );
2176
+ auto &thread_list = m_exe_ctx.GetProcessRef ().GetThreadList ();
2177
+ thread_list.AddThread (thread_task.get ());
2178
+ thread_list.SetSelectedThreadByID (thread_task.get ()->GetID ());
2179
+
2156
2180
result.SetStatus (lldb::eReturnStatusSuccessFinishResult);
2157
2181
}
2158
2182
};
@@ -2166,6 +2190,9 @@ class CommandObjectLanguageSwiftTask final : public CommandObjectMultiword {
2166
2190
LoadSubCommand (" backtrace" ,
2167
2191
CommandObjectSP (new CommandObjectLanguageSwiftTaskBacktrace (
2168
2192
interpreter)));
2193
+ LoadSubCommand (
2194
+ " select" ,
2195
+ CommandObjectSP (new CommandObjectLanguageSwiftTaskSelect (interpreter)));
2169
2196
}
2170
2197
};
2171
2198
0 commit comments