15ffd83dbSDimitry Andric //===-- CommandObjectThread.cpp -------------------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #include "CommandObjectThread.h" 10*0b57cec5SDimitry Andric 11e8d8bef9SDimitry Andric #include <sstream> 12e8d8bef9SDimitry Andric 13e8d8bef9SDimitry Andric #include "CommandObjectThreadUtil.h" 14e8d8bef9SDimitry Andric #include "lldb/Core/PluginManager.h" 15*0b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h" 16*0b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h" 17*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h" 18*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h" 19*0b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h" 209dba64beSDimitry Andric #include "lldb/Interpreter/OptionGroupPythonClassWithDict.h" 21*0b57cec5SDimitry Andric #include "lldb/Interpreter/Options.h" 22*0b57cec5SDimitry Andric #include "lldb/Symbol/CompileUnit.h" 23*0b57cec5SDimitry Andric #include "lldb/Symbol/Function.h" 24*0b57cec5SDimitry Andric #include "lldb/Symbol/LineEntry.h" 25*0b57cec5SDimitry Andric #include "lldb/Symbol/LineTable.h" 26*0b57cec5SDimitry Andric #include "lldb/Target/Process.h" 27*0b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h" 28*0b57cec5SDimitry Andric #include "lldb/Target/SystemRuntime.h" 29*0b57cec5SDimitry Andric #include "lldb/Target/Target.h" 30*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h" 31*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlan.h" 32*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanStepInRange.h" 33e8d8bef9SDimitry Andric #include "lldb/Target/Trace.h" 34*0b57cec5SDimitry Andric #include "lldb/Utility/State.h" 35*0b57cec5SDimitry Andric 36*0b57cec5SDimitry Andric using namespace lldb; 37*0b57cec5SDimitry Andric using namespace lldb_private; 38*0b57cec5SDimitry Andric 39*0b57cec5SDimitry Andric // CommandObjectThreadBacktrace 40*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_backtrace 41*0b57cec5SDimitry Andric #include "CommandOptions.inc" 42*0b57cec5SDimitry Andric 43*0b57cec5SDimitry Andric class CommandObjectThreadBacktrace : public CommandObjectIterateOverThreads { 44*0b57cec5SDimitry Andric public: 45*0b57cec5SDimitry Andric class CommandOptions : public Options { 46*0b57cec5SDimitry Andric public: 47*0b57cec5SDimitry Andric CommandOptions() : Options() { 48*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 49*0b57cec5SDimitry Andric // () 50*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 51*0b57cec5SDimitry Andric } 52*0b57cec5SDimitry Andric 53*0b57cec5SDimitry Andric ~CommandOptions() override = default; 54*0b57cec5SDimitry Andric 55*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 56*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 57*0b57cec5SDimitry Andric Status error; 58*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 59*0b57cec5SDimitry Andric 60*0b57cec5SDimitry Andric switch (short_option) { 61*0b57cec5SDimitry Andric case 'c': { 62*0b57cec5SDimitry Andric int32_t input_count = 0; 63*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_count)) { 64*0b57cec5SDimitry Andric m_count = UINT32_MAX; 65*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 66*0b57cec5SDimitry Andric "invalid integer value for option '%c'", short_option); 67*0b57cec5SDimitry Andric } else if (input_count < 0) 68*0b57cec5SDimitry Andric m_count = UINT32_MAX; 69*0b57cec5SDimitry Andric } break; 70*0b57cec5SDimitry Andric case 's': 71*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_start)) 72*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 73*0b57cec5SDimitry Andric "invalid integer value for option '%c'", short_option); 74*0b57cec5SDimitry Andric break; 75*0b57cec5SDimitry Andric case 'e': { 76*0b57cec5SDimitry Andric bool success; 77*0b57cec5SDimitry Andric m_extended_backtrace = 78*0b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, false, &success); 79*0b57cec5SDimitry Andric if (!success) 80*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 81*0b57cec5SDimitry Andric "invalid boolean value for option '%c'", short_option); 82*0b57cec5SDimitry Andric } break; 83*0b57cec5SDimitry Andric default: 849dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 85*0b57cec5SDimitry Andric } 86*0b57cec5SDimitry Andric return error; 87*0b57cec5SDimitry Andric } 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 90*0b57cec5SDimitry Andric m_count = UINT32_MAX; 91*0b57cec5SDimitry Andric m_start = 0; 92*0b57cec5SDimitry Andric m_extended_backtrace = false; 93*0b57cec5SDimitry Andric } 94*0b57cec5SDimitry Andric 95*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 96*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_backtrace_options); 97*0b57cec5SDimitry Andric } 98*0b57cec5SDimitry Andric 99*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 100*0b57cec5SDimitry Andric uint32_t m_count; 101*0b57cec5SDimitry Andric uint32_t m_start; 102*0b57cec5SDimitry Andric bool m_extended_backtrace; 103*0b57cec5SDimitry Andric }; 104*0b57cec5SDimitry Andric 105*0b57cec5SDimitry Andric CommandObjectThreadBacktrace(CommandInterpreter &interpreter) 106*0b57cec5SDimitry Andric : CommandObjectIterateOverThreads( 107*0b57cec5SDimitry Andric interpreter, "thread backtrace", 108*0b57cec5SDimitry Andric "Show thread call stacks. Defaults to the current thread, thread " 109*0b57cec5SDimitry Andric "indexes can be specified as arguments.\n" 110*0b57cec5SDimitry Andric "Use the thread-index \"all\" to see all threads.\n" 111*0b57cec5SDimitry Andric "Use the thread-index \"unique\" to see threads grouped by unique " 112*0b57cec5SDimitry Andric "call stacks.\n" 113*0b57cec5SDimitry Andric "Use 'settings set frame-format' to customize the printing of " 114*0b57cec5SDimitry Andric "frames in the backtrace and 'settings set thread-format' to " 115*0b57cec5SDimitry Andric "customize the thread header.", 116*0b57cec5SDimitry Andric nullptr, 117*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 118*0b57cec5SDimitry Andric eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | 119*0b57cec5SDimitry Andric eCommandProcessMustBePaused), 120*0b57cec5SDimitry Andric m_options() {} 121*0b57cec5SDimitry Andric 122*0b57cec5SDimitry Andric ~CommandObjectThreadBacktrace() override = default; 123*0b57cec5SDimitry Andric 124*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric protected: 127*0b57cec5SDimitry Andric void DoExtendedBacktrace(Thread *thread, CommandReturnObject &result) { 128*0b57cec5SDimitry Andric SystemRuntime *runtime = thread->GetProcess()->GetSystemRuntime(); 129*0b57cec5SDimitry Andric if (runtime) { 130*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 131*0b57cec5SDimitry Andric const std::vector<ConstString> &types = 132*0b57cec5SDimitry Andric runtime->GetExtendedBacktraceTypes(); 133*0b57cec5SDimitry Andric for (auto type : types) { 134*0b57cec5SDimitry Andric ThreadSP ext_thread_sp = runtime->GetExtendedBacktraceThread( 135*0b57cec5SDimitry Andric thread->shared_from_this(), type); 136*0b57cec5SDimitry Andric if (ext_thread_sp && ext_thread_sp->IsValid()) { 137*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 0; 138*0b57cec5SDimitry Andric const bool stop_format = false; 139*0b57cec5SDimitry Andric if (ext_thread_sp->GetStatus(strm, m_options.m_start, 140*0b57cec5SDimitry Andric m_options.m_count, 141480093f4SDimitry Andric num_frames_with_source, stop_format)) { 142*0b57cec5SDimitry Andric DoExtendedBacktrace(ext_thread_sp.get(), result); 143*0b57cec5SDimitry Andric } 144*0b57cec5SDimitry Andric } 145*0b57cec5SDimitry Andric } 146*0b57cec5SDimitry Andric } 147*0b57cec5SDimitry Andric } 148*0b57cec5SDimitry Andric 149*0b57cec5SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 150*0b57cec5SDimitry Andric ThreadSP thread_sp = 151*0b57cec5SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 152*0b57cec5SDimitry Andric if (!thread_sp) { 153*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 154*0b57cec5SDimitry Andric "thread disappeared while computing backtraces: 0x%" PRIx64 "\n", 155*0b57cec5SDimitry Andric tid); 156*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 157*0b57cec5SDimitry Andric return false; 158*0b57cec5SDimitry Andric } 159*0b57cec5SDimitry Andric 160*0b57cec5SDimitry Andric Thread *thread = thread_sp.get(); 161*0b57cec5SDimitry Andric 162*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 163*0b57cec5SDimitry Andric 164*0b57cec5SDimitry Andric // Only dump stack info if we processing unique stacks. 165*0b57cec5SDimitry Andric const bool only_stacks = m_unique_stacks; 166*0b57cec5SDimitry Andric 167*0b57cec5SDimitry Andric // Don't show source context when doing backtraces. 168*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 0; 169*0b57cec5SDimitry Andric const bool stop_format = true; 170*0b57cec5SDimitry Andric if (!thread->GetStatus(strm, m_options.m_start, m_options.m_count, 171*0b57cec5SDimitry Andric num_frames_with_source, stop_format, only_stacks)) { 172*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 173*0b57cec5SDimitry Andric "error displaying backtrace for thread: \"0x%4.4x\"\n", 174*0b57cec5SDimitry Andric thread->GetIndexID()); 175*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 176*0b57cec5SDimitry Andric return false; 177*0b57cec5SDimitry Andric } 178*0b57cec5SDimitry Andric if (m_options.m_extended_backtrace) { 179*0b57cec5SDimitry Andric DoExtendedBacktrace(thread, result); 180*0b57cec5SDimitry Andric } 181*0b57cec5SDimitry Andric 182*0b57cec5SDimitry Andric return true; 183*0b57cec5SDimitry Andric } 184*0b57cec5SDimitry Andric 185*0b57cec5SDimitry Andric CommandOptions m_options; 186*0b57cec5SDimitry Andric }; 187*0b57cec5SDimitry Andric 188*0b57cec5SDimitry Andric enum StepScope { eStepScopeSource, eStepScopeInstruction }; 189*0b57cec5SDimitry Andric 190*0b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_tri_running_mode[] = { 191*0b57cec5SDimitry Andric {eOnlyThisThread, "this-thread", "Run only this thread"}, 192*0b57cec5SDimitry Andric {eAllThreads, "all-threads", "Run all threads"}, 193*0b57cec5SDimitry Andric {eOnlyDuringStepping, "while-stepping", 194*0b57cec5SDimitry Andric "Run only this thread while stepping"}}; 195*0b57cec5SDimitry Andric 196*0b57cec5SDimitry Andric static constexpr OptionEnumValues TriRunningModes() { 197*0b57cec5SDimitry Andric return OptionEnumValues(g_tri_running_mode); 198*0b57cec5SDimitry Andric } 199*0b57cec5SDimitry Andric 200*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_step_scope 201*0b57cec5SDimitry Andric #include "CommandOptions.inc" 202*0b57cec5SDimitry Andric 2039dba64beSDimitry Andric class ThreadStepScopeOptionGroup : public OptionGroup { 204*0b57cec5SDimitry Andric public: 2059dba64beSDimitry Andric ThreadStepScopeOptionGroup() : OptionGroup() { 206*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 207*0b57cec5SDimitry Andric // () 208*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 209*0b57cec5SDimitry Andric } 210*0b57cec5SDimitry Andric 2119dba64beSDimitry Andric ~ThreadStepScopeOptionGroup() override = default; 2129dba64beSDimitry Andric 2139dba64beSDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 2149dba64beSDimitry Andric return llvm::makeArrayRef(g_thread_step_scope_options); 2159dba64beSDimitry Andric } 216*0b57cec5SDimitry Andric 217*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 218*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 219*0b57cec5SDimitry Andric Status error; 220480093f4SDimitry Andric const int short_option = 221480093f4SDimitry Andric g_thread_step_scope_options[option_idx].short_option; 222*0b57cec5SDimitry Andric 223*0b57cec5SDimitry Andric switch (short_option) { 224*0b57cec5SDimitry Andric case 'a': { 225*0b57cec5SDimitry Andric bool success; 226*0b57cec5SDimitry Andric bool avoid_no_debug = 227*0b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, true, &success); 228*0b57cec5SDimitry Andric if (!success) 229480093f4SDimitry Andric error.SetErrorStringWithFormat("invalid boolean value for option '%c'", 230480093f4SDimitry Andric short_option); 231*0b57cec5SDimitry Andric else { 232480093f4SDimitry Andric m_step_in_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo; 233*0b57cec5SDimitry Andric } 234*0b57cec5SDimitry Andric } break; 235*0b57cec5SDimitry Andric 236*0b57cec5SDimitry Andric case 'A': { 237*0b57cec5SDimitry Andric bool success; 238*0b57cec5SDimitry Andric bool avoid_no_debug = 239*0b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, true, &success); 240*0b57cec5SDimitry Andric if (!success) 241480093f4SDimitry Andric error.SetErrorStringWithFormat("invalid boolean value for option '%c'", 242480093f4SDimitry Andric short_option); 243*0b57cec5SDimitry Andric else { 244480093f4SDimitry Andric m_step_out_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo; 245*0b57cec5SDimitry Andric } 246*0b57cec5SDimitry Andric } break; 247*0b57cec5SDimitry Andric 248*0b57cec5SDimitry Andric case 'c': 249*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_step_count)) 250*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid step count '%s'", 251*0b57cec5SDimitry Andric option_arg.str().c_str()); 252*0b57cec5SDimitry Andric break; 253*0b57cec5SDimitry Andric 254*0b57cec5SDimitry Andric case 'm': { 255*0b57cec5SDimitry Andric auto enum_values = GetDefinitions()[option_idx].enum_values; 256*0b57cec5SDimitry Andric m_run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum( 257*0b57cec5SDimitry Andric option_arg, enum_values, eOnlyDuringStepping, error); 258*0b57cec5SDimitry Andric } break; 259*0b57cec5SDimitry Andric 260*0b57cec5SDimitry Andric case 'e': 261*0b57cec5SDimitry Andric if (option_arg == "block") { 262*0b57cec5SDimitry Andric m_end_line_is_block_end = true; 263*0b57cec5SDimitry Andric break; 264*0b57cec5SDimitry Andric } 265*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_end_line)) 266*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid end line number '%s'", 267*0b57cec5SDimitry Andric option_arg.str().c_str()); 268*0b57cec5SDimitry Andric break; 269*0b57cec5SDimitry Andric 270*0b57cec5SDimitry Andric case 'r': 271*0b57cec5SDimitry Andric m_avoid_regexp.clear(); 2725ffd83dbSDimitry Andric m_avoid_regexp.assign(std::string(option_arg)); 273*0b57cec5SDimitry Andric break; 274*0b57cec5SDimitry Andric 275*0b57cec5SDimitry Andric case 't': 276*0b57cec5SDimitry Andric m_step_in_target.clear(); 2775ffd83dbSDimitry Andric m_step_in_target.assign(std::string(option_arg)); 278*0b57cec5SDimitry Andric break; 279*0b57cec5SDimitry Andric 280*0b57cec5SDimitry Andric default: 2819dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 282*0b57cec5SDimitry Andric } 283*0b57cec5SDimitry Andric return error; 284*0b57cec5SDimitry Andric } 285*0b57cec5SDimitry Andric 286*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 287*0b57cec5SDimitry Andric m_step_in_avoid_no_debug = eLazyBoolCalculate; 288*0b57cec5SDimitry Andric m_step_out_avoid_no_debug = eLazyBoolCalculate; 289*0b57cec5SDimitry Andric m_run_mode = eOnlyDuringStepping; 290*0b57cec5SDimitry Andric 291*0b57cec5SDimitry Andric // Check if we are in Non-Stop mode 292*0b57cec5SDimitry Andric TargetSP target_sp = 293*0b57cec5SDimitry Andric execution_context ? execution_context->GetTargetSP() : TargetSP(); 294e8d8bef9SDimitry Andric if (target_sp && target_sp->GetNonStopModeEnabled()) { 295e8d8bef9SDimitry Andric // NonStopMode runs all threads by definition, so when it is on we don't 296e8d8bef9SDimitry Andric // need to check the process setting for runs all threads. 297*0b57cec5SDimitry Andric m_run_mode = eOnlyThisThread; 298e8d8bef9SDimitry Andric } else { 299e8d8bef9SDimitry Andric ProcessSP process_sp = 300e8d8bef9SDimitry Andric execution_context ? execution_context->GetProcessSP() : ProcessSP(); 301e8d8bef9SDimitry Andric if (process_sp && process_sp->GetSteppingRunsAllThreads()) 302e8d8bef9SDimitry Andric m_run_mode = eAllThreads; 303e8d8bef9SDimitry Andric } 304*0b57cec5SDimitry Andric 305*0b57cec5SDimitry Andric m_avoid_regexp.clear(); 306*0b57cec5SDimitry Andric m_step_in_target.clear(); 307*0b57cec5SDimitry Andric m_step_count = 1; 308*0b57cec5SDimitry Andric m_end_line = LLDB_INVALID_LINE_NUMBER; 309*0b57cec5SDimitry Andric m_end_line_is_block_end = false; 310*0b57cec5SDimitry Andric } 311*0b57cec5SDimitry Andric 312*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 313*0b57cec5SDimitry Andric LazyBool m_step_in_avoid_no_debug; 314*0b57cec5SDimitry Andric LazyBool m_step_out_avoid_no_debug; 315*0b57cec5SDimitry Andric RunMode m_run_mode; 316*0b57cec5SDimitry Andric std::string m_avoid_regexp; 317*0b57cec5SDimitry Andric std::string m_step_in_target; 318*0b57cec5SDimitry Andric uint32_t m_step_count; 319*0b57cec5SDimitry Andric uint32_t m_end_line; 320*0b57cec5SDimitry Andric bool m_end_line_is_block_end; 321*0b57cec5SDimitry Andric }; 322*0b57cec5SDimitry Andric 3239dba64beSDimitry Andric class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed { 3249dba64beSDimitry Andric public: 325*0b57cec5SDimitry Andric CommandObjectThreadStepWithTypeAndScope(CommandInterpreter &interpreter, 326*0b57cec5SDimitry Andric const char *name, const char *help, 327*0b57cec5SDimitry Andric const char *syntax, 328*0b57cec5SDimitry Andric StepType step_type, 329*0b57cec5SDimitry Andric StepScope step_scope) 330*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, name, help, syntax, 331*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 332*0b57cec5SDimitry Andric eCommandTryTargetAPILock | 333*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 334*0b57cec5SDimitry Andric eCommandProcessMustBePaused), 3359dba64beSDimitry Andric m_step_type(step_type), m_step_scope(step_scope), m_options(), 336480093f4SDimitry Andric m_class_options("scripted step") { 337*0b57cec5SDimitry Andric CommandArgumentEntry arg; 338*0b57cec5SDimitry Andric CommandArgumentData thread_id_arg; 339*0b57cec5SDimitry Andric 340*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 341*0b57cec5SDimitry Andric thread_id_arg.arg_type = eArgTypeThreadID; 342*0b57cec5SDimitry Andric thread_id_arg.arg_repetition = eArgRepeatOptional; 343*0b57cec5SDimitry Andric 344*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 345*0b57cec5SDimitry Andric // argument entry. 346*0b57cec5SDimitry Andric arg.push_back(thread_id_arg); 347*0b57cec5SDimitry Andric 348*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 349*0b57cec5SDimitry Andric m_arguments.push_back(arg); 3509dba64beSDimitry Andric 3519dba64beSDimitry Andric if (step_type == eStepTypeScripted) { 352480093f4SDimitry Andric m_all_options.Append(&m_class_options, LLDB_OPT_SET_1 | LLDB_OPT_SET_2, 353480093f4SDimitry Andric LLDB_OPT_SET_1); 3549dba64beSDimitry Andric } 3559dba64beSDimitry Andric m_all_options.Append(&m_options); 3569dba64beSDimitry Andric m_all_options.Finalize(); 357*0b57cec5SDimitry Andric } 358*0b57cec5SDimitry Andric 359*0b57cec5SDimitry Andric ~CommandObjectThreadStepWithTypeAndScope() override = default; 360*0b57cec5SDimitry Andric 361e8d8bef9SDimitry Andric void 362e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 363e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 364e8d8bef9SDimitry Andric if (request.GetCursorIndex()) 365e8d8bef9SDimitry Andric return; 366e8d8bef9SDimitry Andric 367e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 368e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 369e8d8bef9SDimitry Andric request, nullptr); 370e8d8bef9SDimitry Andric } 371e8d8bef9SDimitry Andric 372480093f4SDimitry Andric Options *GetOptions() override { return &m_all_options; } 373*0b57cec5SDimitry Andric 374*0b57cec5SDimitry Andric protected: 375*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 376*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 377*0b57cec5SDimitry Andric bool synchronous_execution = m_interpreter.GetSynchronous(); 378*0b57cec5SDimitry Andric 379*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 380*0b57cec5SDimitry Andric Thread *thread = nullptr; 381*0b57cec5SDimitry Andric 382*0b57cec5SDimitry Andric if (command.GetArgumentCount() == 0) { 383*0b57cec5SDimitry Andric thread = GetDefaultThread(); 384*0b57cec5SDimitry Andric 385*0b57cec5SDimitry Andric if (thread == nullptr) { 386*0b57cec5SDimitry Andric result.AppendError("no selected thread in process"); 387*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 388*0b57cec5SDimitry Andric return false; 389*0b57cec5SDimitry Andric } 390*0b57cec5SDimitry Andric } else { 391*0b57cec5SDimitry Andric const char *thread_idx_cstr = command.GetArgumentAtIndex(0); 3925ffd83dbSDimitry Andric uint32_t step_thread_idx; 3935ffd83dbSDimitry Andric 3945ffd83dbSDimitry Andric if (!llvm::to_integer(thread_idx_cstr, step_thread_idx)) { 395*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid thread index '%s'.\n", 396*0b57cec5SDimitry Andric thread_idx_cstr); 397*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 398*0b57cec5SDimitry Andric return false; 399*0b57cec5SDimitry Andric } 400*0b57cec5SDimitry Andric thread = 401*0b57cec5SDimitry Andric process->GetThreadList().FindThreadByIndexID(step_thread_idx).get(); 402*0b57cec5SDimitry Andric if (thread == nullptr) { 403*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 404*0b57cec5SDimitry Andric "Thread index %u is out of range (valid values are 0 - %u).\n", 405*0b57cec5SDimitry Andric step_thread_idx, num_threads); 406*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 407*0b57cec5SDimitry Andric return false; 408*0b57cec5SDimitry Andric } 409*0b57cec5SDimitry Andric } 410*0b57cec5SDimitry Andric 411*0b57cec5SDimitry Andric if (m_step_type == eStepTypeScripted) { 412480093f4SDimitry Andric if (m_class_options.GetName().empty()) { 413*0b57cec5SDimitry Andric result.AppendErrorWithFormat("empty class name for scripted step."); 414*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 415*0b57cec5SDimitry Andric return false; 416*0b57cec5SDimitry Andric } else if (!GetDebugger().GetScriptInterpreter()->CheckObjectExists( 417480093f4SDimitry Andric m_class_options.GetName().c_str())) { 418*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 419*0b57cec5SDimitry Andric "class for scripted step: \"%s\" does not exist.", 420480093f4SDimitry Andric m_class_options.GetName().c_str()); 421*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 422*0b57cec5SDimitry Andric return false; 423*0b57cec5SDimitry Andric } 424*0b57cec5SDimitry Andric } 425*0b57cec5SDimitry Andric 426*0b57cec5SDimitry Andric if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER && 427*0b57cec5SDimitry Andric m_step_type != eStepTypeInto) { 428*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 429*0b57cec5SDimitry Andric "end line option is only valid for step into"); 430*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 431*0b57cec5SDimitry Andric return false; 432*0b57cec5SDimitry Andric } 433*0b57cec5SDimitry Andric 434*0b57cec5SDimitry Andric const bool abort_other_plans = false; 435*0b57cec5SDimitry Andric const lldb::RunMode stop_other_threads = m_options.m_run_mode; 436*0b57cec5SDimitry Andric 437*0b57cec5SDimitry Andric // This is a bit unfortunate, but not all the commands in this command 438*0b57cec5SDimitry Andric // object support only while stepping, so I use the bool for them. 439*0b57cec5SDimitry Andric bool bool_stop_other_threads; 440*0b57cec5SDimitry Andric if (m_options.m_run_mode == eAllThreads) 441*0b57cec5SDimitry Andric bool_stop_other_threads = false; 442*0b57cec5SDimitry Andric else if (m_options.m_run_mode == eOnlyDuringStepping) 443e8d8bef9SDimitry Andric bool_stop_other_threads = (m_step_type != eStepTypeOut); 444*0b57cec5SDimitry Andric else 445*0b57cec5SDimitry Andric bool_stop_other_threads = true; 446*0b57cec5SDimitry Andric 447*0b57cec5SDimitry Andric ThreadPlanSP new_plan_sp; 448*0b57cec5SDimitry Andric Status new_plan_status; 449*0b57cec5SDimitry Andric 450*0b57cec5SDimitry Andric if (m_step_type == eStepTypeInto) { 451*0b57cec5SDimitry Andric StackFrame *frame = thread->GetStackFrameAtIndex(0).get(); 452*0b57cec5SDimitry Andric assert(frame != nullptr); 453*0b57cec5SDimitry Andric 454*0b57cec5SDimitry Andric if (frame->HasDebugInformation()) { 455*0b57cec5SDimitry Andric AddressRange range; 456*0b57cec5SDimitry Andric SymbolContext sc = frame->GetSymbolContext(eSymbolContextEverything); 457*0b57cec5SDimitry Andric if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER) { 458*0b57cec5SDimitry Andric Status error; 459*0b57cec5SDimitry Andric if (!sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range, 460*0b57cec5SDimitry Andric error)) { 461*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid end-line option: %s.", 462*0b57cec5SDimitry Andric error.AsCString()); 463*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 464*0b57cec5SDimitry Andric return false; 465*0b57cec5SDimitry Andric } 466*0b57cec5SDimitry Andric } else if (m_options.m_end_line_is_block_end) { 467*0b57cec5SDimitry Andric Status error; 468*0b57cec5SDimitry Andric Block *block = frame->GetSymbolContext(eSymbolContextBlock).block; 469*0b57cec5SDimitry Andric if (!block) { 470*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Could not find the current block."); 471*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 472*0b57cec5SDimitry Andric return false; 473*0b57cec5SDimitry Andric } 474*0b57cec5SDimitry Andric 475*0b57cec5SDimitry Andric AddressRange block_range; 476*0b57cec5SDimitry Andric Address pc_address = frame->GetFrameCodeAddress(); 477*0b57cec5SDimitry Andric block->GetRangeContainingAddress(pc_address, block_range); 478*0b57cec5SDimitry Andric if (!block_range.GetBaseAddress().IsValid()) { 479*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 480*0b57cec5SDimitry Andric "Could not find the current block address."); 481*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 482*0b57cec5SDimitry Andric return false; 483*0b57cec5SDimitry Andric } 484*0b57cec5SDimitry Andric lldb::addr_t pc_offset_in_block = 485*0b57cec5SDimitry Andric pc_address.GetFileAddress() - 486*0b57cec5SDimitry Andric block_range.GetBaseAddress().GetFileAddress(); 487*0b57cec5SDimitry Andric lldb::addr_t range_length = 488*0b57cec5SDimitry Andric block_range.GetByteSize() - pc_offset_in_block; 489*0b57cec5SDimitry Andric range = AddressRange(pc_address, range_length); 490*0b57cec5SDimitry Andric } else { 491*0b57cec5SDimitry Andric range = sc.line_entry.range; 492*0b57cec5SDimitry Andric } 493*0b57cec5SDimitry Andric 494*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepInRange( 495*0b57cec5SDimitry Andric abort_other_plans, range, 496*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextEverything), 497*0b57cec5SDimitry Andric m_options.m_step_in_target.c_str(), stop_other_threads, 498*0b57cec5SDimitry Andric new_plan_status, m_options.m_step_in_avoid_no_debug, 499*0b57cec5SDimitry Andric m_options.m_step_out_avoid_no_debug); 500*0b57cec5SDimitry Andric 501*0b57cec5SDimitry Andric if (new_plan_sp && !m_options.m_avoid_regexp.empty()) { 502*0b57cec5SDimitry Andric ThreadPlanStepInRange *step_in_range_plan = 503*0b57cec5SDimitry Andric static_cast<ThreadPlanStepInRange *>(new_plan_sp.get()); 504*0b57cec5SDimitry Andric step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str()); 505*0b57cec5SDimitry Andric } 506*0b57cec5SDimitry Andric } else 507*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 508*0b57cec5SDimitry Andric false, abort_other_plans, bool_stop_other_threads, new_plan_status); 509*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeOver) { 510*0b57cec5SDimitry Andric StackFrame *frame = thread->GetStackFrameAtIndex(0).get(); 511*0b57cec5SDimitry Andric 512*0b57cec5SDimitry Andric if (frame->HasDebugInformation()) 513*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepOverRange( 514*0b57cec5SDimitry Andric abort_other_plans, 515*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextEverything).line_entry, 516*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextEverything), 517*0b57cec5SDimitry Andric stop_other_threads, new_plan_status, 518*0b57cec5SDimitry Andric m_options.m_step_out_avoid_no_debug); 519*0b57cec5SDimitry Andric else 520*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 521*0b57cec5SDimitry Andric true, abort_other_plans, bool_stop_other_threads, new_plan_status); 522*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeTrace) { 523*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 524*0b57cec5SDimitry Andric false, abort_other_plans, bool_stop_other_threads, new_plan_status); 525*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeTraceOver) { 526*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 527*0b57cec5SDimitry Andric true, abort_other_plans, bool_stop_other_threads, new_plan_status); 528*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeOut) { 529*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepOut( 530*0b57cec5SDimitry Andric abort_other_plans, nullptr, false, bool_stop_other_threads, eVoteYes, 531*0b57cec5SDimitry Andric eVoteNoOpinion, thread->GetSelectedFrameIndex(), new_plan_status, 532*0b57cec5SDimitry Andric m_options.m_step_out_avoid_no_debug); 533*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeScripted) { 534*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepScripted( 535480093f4SDimitry Andric abort_other_plans, m_class_options.GetName().c_str(), 536480093f4SDimitry Andric m_class_options.GetStructuredData(), bool_stop_other_threads, 537480093f4SDimitry Andric new_plan_status); 538*0b57cec5SDimitry Andric } else { 539*0b57cec5SDimitry Andric result.AppendError("step type is not supported"); 540*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 541*0b57cec5SDimitry Andric return false; 542*0b57cec5SDimitry Andric } 543*0b57cec5SDimitry Andric 544*0b57cec5SDimitry Andric // If we got a new plan, then set it to be a master plan (User level Plans 545*0b57cec5SDimitry Andric // should be master plans so that they can be interruptible). Then resume 546*0b57cec5SDimitry Andric // the process. 547*0b57cec5SDimitry Andric 548*0b57cec5SDimitry Andric if (new_plan_sp) { 549*0b57cec5SDimitry Andric new_plan_sp->SetIsMasterPlan(true); 550*0b57cec5SDimitry Andric new_plan_sp->SetOkayToDiscard(false); 551*0b57cec5SDimitry Andric 552*0b57cec5SDimitry Andric if (m_options.m_step_count > 1) { 553*0b57cec5SDimitry Andric if (!new_plan_sp->SetIterationCount(m_options.m_step_count)) { 554*0b57cec5SDimitry Andric result.AppendWarning( 555*0b57cec5SDimitry Andric "step operation does not support iteration count."); 556*0b57cec5SDimitry Andric } 557*0b57cec5SDimitry Andric } 558*0b57cec5SDimitry Andric 559*0b57cec5SDimitry Andric process->GetThreadList().SetSelectedThreadByID(thread->GetID()); 560*0b57cec5SDimitry Andric 561*0b57cec5SDimitry Andric const uint32_t iohandler_id = process->GetIOHandlerID(); 562*0b57cec5SDimitry Andric 563*0b57cec5SDimitry Andric StreamString stream; 564*0b57cec5SDimitry Andric Status error; 565*0b57cec5SDimitry Andric if (synchronous_execution) 566*0b57cec5SDimitry Andric error = process->ResumeSynchronous(&stream); 567*0b57cec5SDimitry Andric else 568*0b57cec5SDimitry Andric error = process->Resume(); 569*0b57cec5SDimitry Andric 570*0b57cec5SDimitry Andric if (!error.Success()) { 571*0b57cec5SDimitry Andric result.AppendMessage(error.AsCString()); 572*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 573*0b57cec5SDimitry Andric return false; 574*0b57cec5SDimitry Andric } 575*0b57cec5SDimitry Andric 576*0b57cec5SDimitry Andric // There is a race condition where this thread will return up the call 577*0b57cec5SDimitry Andric // stack to the main command handler and show an (lldb) prompt before 578*0b57cec5SDimitry Andric // HandlePrivateEvent (from PrivateStateThread) has a chance to call 579*0b57cec5SDimitry Andric // PushProcessIOHandler(). 580*0b57cec5SDimitry Andric process->SyncIOHandler(iohandler_id, std::chrono::seconds(2)); 581*0b57cec5SDimitry Andric 582*0b57cec5SDimitry Andric if (synchronous_execution) { 583*0b57cec5SDimitry Andric // If any state changed events had anything to say, add that to the 584*0b57cec5SDimitry Andric // result 585*0b57cec5SDimitry Andric if (stream.GetSize() > 0) 586*0b57cec5SDimitry Andric result.AppendMessage(stream.GetString()); 587*0b57cec5SDimitry Andric 588*0b57cec5SDimitry Andric process->GetThreadList().SetSelectedThreadByID(thread->GetID()); 589*0b57cec5SDimitry Andric result.SetDidChangeProcessState(true); 590*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 591*0b57cec5SDimitry Andric } else { 592*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessContinuingNoResult); 593*0b57cec5SDimitry Andric } 594*0b57cec5SDimitry Andric } else { 595*0b57cec5SDimitry Andric result.SetError(new_plan_status); 596*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 597*0b57cec5SDimitry Andric } 598*0b57cec5SDimitry Andric return result.Succeeded(); 599*0b57cec5SDimitry Andric } 600*0b57cec5SDimitry Andric 601*0b57cec5SDimitry Andric StepType m_step_type; 602*0b57cec5SDimitry Andric StepScope m_step_scope; 6039dba64beSDimitry Andric ThreadStepScopeOptionGroup m_options; 6049dba64beSDimitry Andric OptionGroupPythonClassWithDict m_class_options; 6059dba64beSDimitry Andric OptionGroupOptions m_all_options; 606*0b57cec5SDimitry Andric }; 607*0b57cec5SDimitry Andric 608*0b57cec5SDimitry Andric // CommandObjectThreadContinue 609*0b57cec5SDimitry Andric 610*0b57cec5SDimitry Andric class CommandObjectThreadContinue : public CommandObjectParsed { 611*0b57cec5SDimitry Andric public: 612*0b57cec5SDimitry Andric CommandObjectThreadContinue(CommandInterpreter &interpreter) 613*0b57cec5SDimitry Andric : CommandObjectParsed( 614*0b57cec5SDimitry Andric interpreter, "thread continue", 615*0b57cec5SDimitry Andric "Continue execution of the current target process. One " 616*0b57cec5SDimitry Andric "or more threads may be specified, by default all " 617*0b57cec5SDimitry Andric "threads continue.", 618*0b57cec5SDimitry Andric nullptr, 619*0b57cec5SDimitry Andric eCommandRequiresThread | eCommandTryTargetAPILock | 620*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) { 621*0b57cec5SDimitry Andric CommandArgumentEntry arg; 622*0b57cec5SDimitry Andric CommandArgumentData thread_idx_arg; 623*0b57cec5SDimitry Andric 624*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 625*0b57cec5SDimitry Andric thread_idx_arg.arg_type = eArgTypeThreadIndex; 626*0b57cec5SDimitry Andric thread_idx_arg.arg_repetition = eArgRepeatPlus; 627*0b57cec5SDimitry Andric 628*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 629*0b57cec5SDimitry Andric // argument entry. 630*0b57cec5SDimitry Andric arg.push_back(thread_idx_arg); 631*0b57cec5SDimitry Andric 632*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 633*0b57cec5SDimitry Andric m_arguments.push_back(arg); 634*0b57cec5SDimitry Andric } 635*0b57cec5SDimitry Andric 636*0b57cec5SDimitry Andric ~CommandObjectThreadContinue() override = default; 637*0b57cec5SDimitry Andric 638e8d8bef9SDimitry Andric void 639e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 640e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 641e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 642e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 643e8d8bef9SDimitry Andric request, nullptr); 644e8d8bef9SDimitry Andric } 645e8d8bef9SDimitry Andric 646*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 647*0b57cec5SDimitry Andric bool synchronous_execution = m_interpreter.GetSynchronous(); 648*0b57cec5SDimitry Andric 649*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 650*0b57cec5SDimitry Andric if (process == nullptr) { 651*0b57cec5SDimitry Andric result.AppendError("no process exists. Cannot continue"); 652*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 653*0b57cec5SDimitry Andric return false; 654*0b57cec5SDimitry Andric } 655*0b57cec5SDimitry Andric 656*0b57cec5SDimitry Andric StateType state = process->GetState(); 657*0b57cec5SDimitry Andric if ((state == eStateCrashed) || (state == eStateStopped) || 658*0b57cec5SDimitry Andric (state == eStateSuspended)) { 659*0b57cec5SDimitry Andric const size_t argc = command.GetArgumentCount(); 660*0b57cec5SDimitry Andric if (argc > 0) { 661*0b57cec5SDimitry Andric // These two lines appear at the beginning of both blocks in this 662*0b57cec5SDimitry Andric // if..else, but that is because we need to release the lock before 663*0b57cec5SDimitry Andric // calling process->Resume below. 664*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 665*0b57cec5SDimitry Andric process->GetThreadList().GetMutex()); 666*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 667*0b57cec5SDimitry Andric std::vector<Thread *> resume_threads; 668*0b57cec5SDimitry Andric for (auto &entry : command.entries()) { 669*0b57cec5SDimitry Andric uint32_t thread_idx; 6709dba64beSDimitry Andric if (entry.ref().getAsInteger(0, thread_idx)) { 671*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 672*0b57cec5SDimitry Andric "invalid thread index argument: \"%s\".\n", entry.c_str()); 673*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 674*0b57cec5SDimitry Andric return false; 675*0b57cec5SDimitry Andric } 676*0b57cec5SDimitry Andric Thread *thread = 677*0b57cec5SDimitry Andric process->GetThreadList().FindThreadByIndexID(thread_idx).get(); 678*0b57cec5SDimitry Andric 679*0b57cec5SDimitry Andric if (thread) { 680*0b57cec5SDimitry Andric resume_threads.push_back(thread); 681*0b57cec5SDimitry Andric } else { 682*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid thread index %u.\n", 683*0b57cec5SDimitry Andric thread_idx); 684*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 685*0b57cec5SDimitry Andric return false; 686*0b57cec5SDimitry Andric } 687*0b57cec5SDimitry Andric } 688*0b57cec5SDimitry Andric 689*0b57cec5SDimitry Andric if (resume_threads.empty()) { 690*0b57cec5SDimitry Andric result.AppendError("no valid thread indexes were specified"); 691*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 692*0b57cec5SDimitry Andric return false; 693*0b57cec5SDimitry Andric } else { 694*0b57cec5SDimitry Andric if (resume_threads.size() == 1) 695*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Resuming thread: "); 696*0b57cec5SDimitry Andric else 697*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Resuming threads: "); 698*0b57cec5SDimitry Andric 699*0b57cec5SDimitry Andric for (uint32_t idx = 0; idx < num_threads; ++idx) { 700*0b57cec5SDimitry Andric Thread *thread = 701*0b57cec5SDimitry Andric process->GetThreadList().GetThreadAtIndex(idx).get(); 702*0b57cec5SDimitry Andric std::vector<Thread *>::iterator this_thread_pos = 703*0b57cec5SDimitry Andric find(resume_threads.begin(), resume_threads.end(), thread); 704*0b57cec5SDimitry Andric 705*0b57cec5SDimitry Andric if (this_thread_pos != resume_threads.end()) { 706*0b57cec5SDimitry Andric resume_threads.erase(this_thread_pos); 707*0b57cec5SDimitry Andric if (!resume_threads.empty()) 708*0b57cec5SDimitry Andric result.AppendMessageWithFormat("%u, ", thread->GetIndexID()); 709*0b57cec5SDimitry Andric else 710*0b57cec5SDimitry Andric result.AppendMessageWithFormat("%u ", thread->GetIndexID()); 711*0b57cec5SDimitry Andric 712*0b57cec5SDimitry Andric const bool override_suspend = true; 713*0b57cec5SDimitry Andric thread->SetResumeState(eStateRunning, override_suspend); 714*0b57cec5SDimitry Andric } else { 715*0b57cec5SDimitry Andric thread->SetResumeState(eStateSuspended); 716*0b57cec5SDimitry Andric } 717*0b57cec5SDimitry Andric } 718*0b57cec5SDimitry Andric result.AppendMessageWithFormat("in process %" PRIu64 "\n", 719*0b57cec5SDimitry Andric process->GetID()); 720*0b57cec5SDimitry Andric } 721*0b57cec5SDimitry Andric } else { 722*0b57cec5SDimitry Andric // These two lines appear at the beginning of both blocks in this 723*0b57cec5SDimitry Andric // if..else, but that is because we need to release the lock before 724*0b57cec5SDimitry Andric // calling process->Resume below. 725*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 726*0b57cec5SDimitry Andric process->GetThreadList().GetMutex()); 727*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 728*0b57cec5SDimitry Andric Thread *current_thread = GetDefaultThread(); 729*0b57cec5SDimitry Andric if (current_thread == nullptr) { 730*0b57cec5SDimitry Andric result.AppendError("the process doesn't have a current thread"); 731*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 732*0b57cec5SDimitry Andric return false; 733*0b57cec5SDimitry Andric } 734*0b57cec5SDimitry Andric // Set the actions that the threads should each take when resuming 735*0b57cec5SDimitry Andric for (uint32_t idx = 0; idx < num_threads; ++idx) { 736*0b57cec5SDimitry Andric Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get(); 737*0b57cec5SDimitry Andric if (thread == current_thread) { 738*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Resuming thread 0x%4.4" PRIx64 739*0b57cec5SDimitry Andric " in process %" PRIu64 "\n", 740*0b57cec5SDimitry Andric thread->GetID(), process->GetID()); 741*0b57cec5SDimitry Andric const bool override_suspend = true; 742*0b57cec5SDimitry Andric thread->SetResumeState(eStateRunning, override_suspend); 743*0b57cec5SDimitry Andric } else { 744*0b57cec5SDimitry Andric thread->SetResumeState(eStateSuspended); 745*0b57cec5SDimitry Andric } 746*0b57cec5SDimitry Andric } 747*0b57cec5SDimitry Andric } 748*0b57cec5SDimitry Andric 749*0b57cec5SDimitry Andric StreamString stream; 750*0b57cec5SDimitry Andric Status error; 751*0b57cec5SDimitry Andric if (synchronous_execution) 752*0b57cec5SDimitry Andric error = process->ResumeSynchronous(&stream); 753*0b57cec5SDimitry Andric else 754*0b57cec5SDimitry Andric error = process->Resume(); 755*0b57cec5SDimitry Andric 756*0b57cec5SDimitry Andric // We should not be holding the thread list lock when we do this. 757*0b57cec5SDimitry Andric if (error.Success()) { 758*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n", 759*0b57cec5SDimitry Andric process->GetID()); 760*0b57cec5SDimitry Andric if (synchronous_execution) { 761*0b57cec5SDimitry Andric // If any state changed events had anything to say, add that to the 762*0b57cec5SDimitry Andric // result 763*0b57cec5SDimitry Andric if (stream.GetSize() > 0) 764*0b57cec5SDimitry Andric result.AppendMessage(stream.GetString()); 765*0b57cec5SDimitry Andric 766*0b57cec5SDimitry Andric result.SetDidChangeProcessState(true); 767*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 768*0b57cec5SDimitry Andric } else { 769*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessContinuingNoResult); 770*0b57cec5SDimitry Andric } 771*0b57cec5SDimitry Andric } else { 772*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to resume process: %s\n", 773*0b57cec5SDimitry Andric error.AsCString()); 774*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 775*0b57cec5SDimitry Andric } 776*0b57cec5SDimitry Andric } else { 777*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 778*0b57cec5SDimitry Andric "Process cannot be continued from its current state (%s).\n", 779*0b57cec5SDimitry Andric StateAsCString(state)); 780*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 781*0b57cec5SDimitry Andric } 782*0b57cec5SDimitry Andric 783*0b57cec5SDimitry Andric return result.Succeeded(); 784*0b57cec5SDimitry Andric } 785*0b57cec5SDimitry Andric }; 786*0b57cec5SDimitry Andric 787*0b57cec5SDimitry Andric // CommandObjectThreadUntil 788*0b57cec5SDimitry Andric 789*0b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_duo_running_mode[] = { 790*0b57cec5SDimitry Andric {eOnlyThisThread, "this-thread", "Run only this thread"}, 791*0b57cec5SDimitry Andric {eAllThreads, "all-threads", "Run all threads"}}; 792*0b57cec5SDimitry Andric 793*0b57cec5SDimitry Andric static constexpr OptionEnumValues DuoRunningModes() { 794*0b57cec5SDimitry Andric return OptionEnumValues(g_duo_running_mode); 795*0b57cec5SDimitry Andric } 796*0b57cec5SDimitry Andric 797*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_until 798*0b57cec5SDimitry Andric #include "CommandOptions.inc" 799*0b57cec5SDimitry Andric 800*0b57cec5SDimitry Andric class CommandObjectThreadUntil : public CommandObjectParsed { 801*0b57cec5SDimitry Andric public: 802*0b57cec5SDimitry Andric class CommandOptions : public Options { 803*0b57cec5SDimitry Andric public: 804*0b57cec5SDimitry Andric uint32_t m_thread_idx; 805*0b57cec5SDimitry Andric uint32_t m_frame_idx; 806*0b57cec5SDimitry Andric 807*0b57cec5SDimitry Andric CommandOptions() 808*0b57cec5SDimitry Andric : Options(), m_thread_idx(LLDB_INVALID_THREAD_ID), 809*0b57cec5SDimitry Andric m_frame_idx(LLDB_INVALID_FRAME_ID) { 810*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 811*0b57cec5SDimitry Andric // () 812*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 813*0b57cec5SDimitry Andric } 814*0b57cec5SDimitry Andric 815*0b57cec5SDimitry Andric ~CommandOptions() override = default; 816*0b57cec5SDimitry Andric 817*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 818*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 819*0b57cec5SDimitry Andric Status error; 820*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 821*0b57cec5SDimitry Andric 822*0b57cec5SDimitry Andric switch (short_option) { 823*0b57cec5SDimitry Andric case 'a': { 824*0b57cec5SDimitry Andric lldb::addr_t tmp_addr = OptionArgParser::ToAddress( 825*0b57cec5SDimitry Andric execution_context, option_arg, LLDB_INVALID_ADDRESS, &error); 826*0b57cec5SDimitry Andric if (error.Success()) 827*0b57cec5SDimitry Andric m_until_addrs.push_back(tmp_addr); 828*0b57cec5SDimitry Andric } break; 829*0b57cec5SDimitry Andric case 't': 830*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_thread_idx)) { 831*0b57cec5SDimitry Andric m_thread_idx = LLDB_INVALID_INDEX32; 832*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid thread index '%s'", 833*0b57cec5SDimitry Andric option_arg.str().c_str()); 834*0b57cec5SDimitry Andric } 835*0b57cec5SDimitry Andric break; 836*0b57cec5SDimitry Andric case 'f': 837*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_frame_idx)) { 838*0b57cec5SDimitry Andric m_frame_idx = LLDB_INVALID_FRAME_ID; 839*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid frame index '%s'", 840*0b57cec5SDimitry Andric option_arg.str().c_str()); 841*0b57cec5SDimitry Andric } 842*0b57cec5SDimitry Andric break; 843*0b57cec5SDimitry Andric case 'm': { 844*0b57cec5SDimitry Andric auto enum_values = GetDefinitions()[option_idx].enum_values; 845*0b57cec5SDimitry Andric lldb::RunMode run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum( 846*0b57cec5SDimitry Andric option_arg, enum_values, eOnlyDuringStepping, error); 847*0b57cec5SDimitry Andric 848*0b57cec5SDimitry Andric if (error.Success()) { 849*0b57cec5SDimitry Andric if (run_mode == eAllThreads) 850*0b57cec5SDimitry Andric m_stop_others = false; 851*0b57cec5SDimitry Andric else 852*0b57cec5SDimitry Andric m_stop_others = true; 853*0b57cec5SDimitry Andric } 854*0b57cec5SDimitry Andric } break; 855*0b57cec5SDimitry Andric default: 8569dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 857*0b57cec5SDimitry Andric } 858*0b57cec5SDimitry Andric return error; 859*0b57cec5SDimitry Andric } 860*0b57cec5SDimitry Andric 861*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 862*0b57cec5SDimitry Andric m_thread_idx = LLDB_INVALID_THREAD_ID; 863*0b57cec5SDimitry Andric m_frame_idx = 0; 864*0b57cec5SDimitry Andric m_stop_others = false; 865*0b57cec5SDimitry Andric m_until_addrs.clear(); 866*0b57cec5SDimitry Andric } 867*0b57cec5SDimitry Andric 868*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 869*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_until_options); 870*0b57cec5SDimitry Andric } 871*0b57cec5SDimitry Andric 872*0b57cec5SDimitry Andric uint32_t m_step_thread_idx; 873*0b57cec5SDimitry Andric bool m_stop_others; 874*0b57cec5SDimitry Andric std::vector<lldb::addr_t> m_until_addrs; 875*0b57cec5SDimitry Andric 876*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 877*0b57cec5SDimitry Andric }; 878*0b57cec5SDimitry Andric 879*0b57cec5SDimitry Andric CommandObjectThreadUntil(CommandInterpreter &interpreter) 880*0b57cec5SDimitry Andric : CommandObjectParsed( 881*0b57cec5SDimitry Andric interpreter, "thread until", 882*0b57cec5SDimitry Andric "Continue until a line number or address is reached by the " 883*0b57cec5SDimitry Andric "current or specified thread. Stops when returning from " 884*0b57cec5SDimitry Andric "the current function as a safety measure. " 885480093f4SDimitry Andric "The target line number(s) are given as arguments, and if more " 886480093f4SDimitry Andric "than one" 887*0b57cec5SDimitry Andric " is provided, stepping will stop when the first one is hit.", 888*0b57cec5SDimitry Andric nullptr, 889*0b57cec5SDimitry Andric eCommandRequiresThread | eCommandTryTargetAPILock | 890*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 891*0b57cec5SDimitry Andric m_options() { 892*0b57cec5SDimitry Andric CommandArgumentEntry arg; 893*0b57cec5SDimitry Andric CommandArgumentData line_num_arg; 894*0b57cec5SDimitry Andric 895*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 896*0b57cec5SDimitry Andric line_num_arg.arg_type = eArgTypeLineNum; 897*0b57cec5SDimitry Andric line_num_arg.arg_repetition = eArgRepeatPlain; 898*0b57cec5SDimitry Andric 899*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 900*0b57cec5SDimitry Andric // argument entry. 901*0b57cec5SDimitry Andric arg.push_back(line_num_arg); 902*0b57cec5SDimitry Andric 903*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 904*0b57cec5SDimitry Andric m_arguments.push_back(arg); 905*0b57cec5SDimitry Andric } 906*0b57cec5SDimitry Andric 907*0b57cec5SDimitry Andric ~CommandObjectThreadUntil() override = default; 908*0b57cec5SDimitry Andric 909*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 910*0b57cec5SDimitry Andric 911*0b57cec5SDimitry Andric protected: 912*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 913*0b57cec5SDimitry Andric bool synchronous_execution = m_interpreter.GetSynchronous(); 914*0b57cec5SDimitry Andric 9159dba64beSDimitry Andric Target *target = &GetSelectedTarget(); 916*0b57cec5SDimitry Andric 917*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 918*0b57cec5SDimitry Andric if (process == nullptr) { 919*0b57cec5SDimitry Andric result.AppendError("need a valid process to step"); 920*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 921*0b57cec5SDimitry Andric } else { 922*0b57cec5SDimitry Andric Thread *thread = nullptr; 923*0b57cec5SDimitry Andric std::vector<uint32_t> line_numbers; 924*0b57cec5SDimitry Andric 925*0b57cec5SDimitry Andric if (command.GetArgumentCount() >= 1) { 926*0b57cec5SDimitry Andric size_t num_args = command.GetArgumentCount(); 927*0b57cec5SDimitry Andric for (size_t i = 0; i < num_args; i++) { 928*0b57cec5SDimitry Andric uint32_t line_number; 9295ffd83dbSDimitry Andric if (!llvm::to_integer(command.GetArgumentAtIndex(i), line_number)) { 930*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid line number: '%s'.\n", 931*0b57cec5SDimitry Andric command.GetArgumentAtIndex(i)); 932*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 933*0b57cec5SDimitry Andric return false; 934*0b57cec5SDimitry Andric } else 935*0b57cec5SDimitry Andric line_numbers.push_back(line_number); 936*0b57cec5SDimitry Andric } 937*0b57cec5SDimitry Andric } else if (m_options.m_until_addrs.empty()) { 938*0b57cec5SDimitry Andric result.AppendErrorWithFormat("No line number or address provided:\n%s", 939*0b57cec5SDimitry Andric GetSyntax().str().c_str()); 940*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 941*0b57cec5SDimitry Andric return false; 942*0b57cec5SDimitry Andric } 943*0b57cec5SDimitry Andric 944*0b57cec5SDimitry Andric if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID) { 945*0b57cec5SDimitry Andric thread = GetDefaultThread(); 946*0b57cec5SDimitry Andric } else { 947*0b57cec5SDimitry Andric thread = process->GetThreadList() 948*0b57cec5SDimitry Andric .FindThreadByIndexID(m_options.m_thread_idx) 949*0b57cec5SDimitry Andric .get(); 950*0b57cec5SDimitry Andric } 951*0b57cec5SDimitry Andric 952*0b57cec5SDimitry Andric if (thread == nullptr) { 953*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 954*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 955*0b57cec5SDimitry Andric "Thread index %u is out of range (valid values are 0 - %u).\n", 956*0b57cec5SDimitry Andric m_options.m_thread_idx, num_threads); 957*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 958*0b57cec5SDimitry Andric return false; 959*0b57cec5SDimitry Andric } 960*0b57cec5SDimitry Andric 961*0b57cec5SDimitry Andric const bool abort_other_plans = false; 962*0b57cec5SDimitry Andric 963*0b57cec5SDimitry Andric StackFrame *frame = 964*0b57cec5SDimitry Andric thread->GetStackFrameAtIndex(m_options.m_frame_idx).get(); 965*0b57cec5SDimitry Andric if (frame == nullptr) { 966*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 967*0b57cec5SDimitry Andric "Frame index %u is out of range for thread %u.\n", 968*0b57cec5SDimitry Andric m_options.m_frame_idx, m_options.m_thread_idx); 969*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 970*0b57cec5SDimitry Andric return false; 971*0b57cec5SDimitry Andric } 972*0b57cec5SDimitry Andric 973*0b57cec5SDimitry Andric ThreadPlanSP new_plan_sp; 974*0b57cec5SDimitry Andric Status new_plan_status; 975*0b57cec5SDimitry Andric 976*0b57cec5SDimitry Andric if (frame->HasDebugInformation()) { 977*0b57cec5SDimitry Andric // Finally we got here... Translate the given line number to a bunch 978*0b57cec5SDimitry Andric // of addresses: 979*0b57cec5SDimitry Andric SymbolContext sc(frame->GetSymbolContext(eSymbolContextCompUnit)); 980*0b57cec5SDimitry Andric LineTable *line_table = nullptr; 981*0b57cec5SDimitry Andric if (sc.comp_unit) 982*0b57cec5SDimitry Andric line_table = sc.comp_unit->GetLineTable(); 983*0b57cec5SDimitry Andric 984*0b57cec5SDimitry Andric if (line_table == nullptr) { 985*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to resolve the line table for " 986*0b57cec5SDimitry Andric "frame %u of thread index %u.\n", 987*0b57cec5SDimitry Andric m_options.m_frame_idx, 988*0b57cec5SDimitry Andric m_options.m_thread_idx); 989*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 990*0b57cec5SDimitry Andric return false; 991*0b57cec5SDimitry Andric } 992*0b57cec5SDimitry Andric 993*0b57cec5SDimitry Andric LineEntry function_start; 994*0b57cec5SDimitry Andric uint32_t index_ptr = 0, end_ptr; 995*0b57cec5SDimitry Andric std::vector<addr_t> address_list; 996*0b57cec5SDimitry Andric 997*0b57cec5SDimitry Andric // Find the beginning & end index of the 998*0b57cec5SDimitry Andric AddressRange fun_addr_range = sc.function->GetAddressRange(); 999*0b57cec5SDimitry Andric Address fun_start_addr = fun_addr_range.GetBaseAddress(); 1000*0b57cec5SDimitry Andric line_table->FindLineEntryByAddress(fun_start_addr, function_start, 1001*0b57cec5SDimitry Andric &index_ptr); 1002*0b57cec5SDimitry Andric 1003*0b57cec5SDimitry Andric Address fun_end_addr(fun_start_addr.GetSection(), 1004*0b57cec5SDimitry Andric fun_start_addr.GetOffset() + 1005*0b57cec5SDimitry Andric fun_addr_range.GetByteSize()); 1006*0b57cec5SDimitry Andric 1007*0b57cec5SDimitry Andric bool all_in_function = true; 1008*0b57cec5SDimitry Andric 1009*0b57cec5SDimitry Andric line_table->FindLineEntryByAddress(fun_end_addr, function_start, 1010*0b57cec5SDimitry Andric &end_ptr); 1011*0b57cec5SDimitry Andric 1012*0b57cec5SDimitry Andric for (uint32_t line_number : line_numbers) { 1013*0b57cec5SDimitry Andric uint32_t start_idx_ptr = index_ptr; 1014*0b57cec5SDimitry Andric while (start_idx_ptr <= end_ptr) { 1015*0b57cec5SDimitry Andric LineEntry line_entry; 1016*0b57cec5SDimitry Andric const bool exact = false; 1017*0b57cec5SDimitry Andric start_idx_ptr = sc.comp_unit->FindLineEntry( 1018480093f4SDimitry Andric start_idx_ptr, line_number, nullptr, exact, &line_entry); 1019*0b57cec5SDimitry Andric if (start_idx_ptr == UINT32_MAX) 1020*0b57cec5SDimitry Andric break; 1021*0b57cec5SDimitry Andric 1022*0b57cec5SDimitry Andric addr_t address = 1023*0b57cec5SDimitry Andric line_entry.range.GetBaseAddress().GetLoadAddress(target); 1024*0b57cec5SDimitry Andric if (address != LLDB_INVALID_ADDRESS) { 1025*0b57cec5SDimitry Andric if (fun_addr_range.ContainsLoadAddress(address, target)) 1026*0b57cec5SDimitry Andric address_list.push_back(address); 1027*0b57cec5SDimitry Andric else 1028*0b57cec5SDimitry Andric all_in_function = false; 1029*0b57cec5SDimitry Andric } 1030*0b57cec5SDimitry Andric start_idx_ptr++; 1031*0b57cec5SDimitry Andric } 1032*0b57cec5SDimitry Andric } 1033*0b57cec5SDimitry Andric 1034*0b57cec5SDimitry Andric for (lldb::addr_t address : m_options.m_until_addrs) { 1035*0b57cec5SDimitry Andric if (fun_addr_range.ContainsLoadAddress(address, target)) 1036*0b57cec5SDimitry Andric address_list.push_back(address); 1037*0b57cec5SDimitry Andric else 1038*0b57cec5SDimitry Andric all_in_function = false; 1039*0b57cec5SDimitry Andric } 1040*0b57cec5SDimitry Andric 1041*0b57cec5SDimitry Andric if (address_list.empty()) { 1042*0b57cec5SDimitry Andric if (all_in_function) 1043*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1044*0b57cec5SDimitry Andric "No line entries matching until target.\n"); 1045*0b57cec5SDimitry Andric else 1046*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1047*0b57cec5SDimitry Andric "Until target outside of the current function.\n"); 1048*0b57cec5SDimitry Andric 1049*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1050*0b57cec5SDimitry Andric return false; 1051*0b57cec5SDimitry Andric } 1052*0b57cec5SDimitry Andric 1053*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepUntil( 1054*0b57cec5SDimitry Andric abort_other_plans, &address_list.front(), address_list.size(), 1055*0b57cec5SDimitry Andric m_options.m_stop_others, m_options.m_frame_idx, new_plan_status); 1056*0b57cec5SDimitry Andric if (new_plan_sp) { 1057*0b57cec5SDimitry Andric // User level plans should be master plans so they can be interrupted 1058*0b57cec5SDimitry Andric // (e.g. by hitting a breakpoint) and other plans executed by the 1059*0b57cec5SDimitry Andric // user (stepping around the breakpoint) and then a "continue" will 1060*0b57cec5SDimitry Andric // resume the original plan. 1061*0b57cec5SDimitry Andric new_plan_sp->SetIsMasterPlan(true); 1062*0b57cec5SDimitry Andric new_plan_sp->SetOkayToDiscard(false); 1063*0b57cec5SDimitry Andric } else { 1064*0b57cec5SDimitry Andric result.SetError(new_plan_status); 1065*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1066*0b57cec5SDimitry Andric return false; 1067*0b57cec5SDimitry Andric } 1068*0b57cec5SDimitry Andric } else { 1069*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1070*0b57cec5SDimitry Andric "Frame index %u of thread %u has no debug information.\n", 1071*0b57cec5SDimitry Andric m_options.m_frame_idx, m_options.m_thread_idx); 1072*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1073*0b57cec5SDimitry Andric return false; 1074*0b57cec5SDimitry Andric } 1075*0b57cec5SDimitry Andric 1076*0b57cec5SDimitry Andric process->GetThreadList().SetSelectedThreadByID(m_options.m_thread_idx); 1077*0b57cec5SDimitry Andric 1078*0b57cec5SDimitry Andric StreamString stream; 1079*0b57cec5SDimitry Andric Status error; 1080*0b57cec5SDimitry Andric if (synchronous_execution) 1081*0b57cec5SDimitry Andric error = process->ResumeSynchronous(&stream); 1082*0b57cec5SDimitry Andric else 1083*0b57cec5SDimitry Andric error = process->Resume(); 1084*0b57cec5SDimitry Andric 1085*0b57cec5SDimitry Andric if (error.Success()) { 1086*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n", 1087*0b57cec5SDimitry Andric process->GetID()); 1088*0b57cec5SDimitry Andric if (synchronous_execution) { 1089*0b57cec5SDimitry Andric // If any state changed events had anything to say, add that to the 1090*0b57cec5SDimitry Andric // result 1091*0b57cec5SDimitry Andric if (stream.GetSize() > 0) 1092*0b57cec5SDimitry Andric result.AppendMessage(stream.GetString()); 1093*0b57cec5SDimitry Andric 1094*0b57cec5SDimitry Andric result.SetDidChangeProcessState(true); 1095*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1096*0b57cec5SDimitry Andric } else { 1097*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessContinuingNoResult); 1098*0b57cec5SDimitry Andric } 1099*0b57cec5SDimitry Andric } else { 1100*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to resume process: %s.\n", 1101*0b57cec5SDimitry Andric error.AsCString()); 1102*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1103*0b57cec5SDimitry Andric } 1104*0b57cec5SDimitry Andric } 1105*0b57cec5SDimitry Andric return result.Succeeded(); 1106*0b57cec5SDimitry Andric } 1107*0b57cec5SDimitry Andric 1108*0b57cec5SDimitry Andric CommandOptions m_options; 1109*0b57cec5SDimitry Andric }; 1110*0b57cec5SDimitry Andric 1111*0b57cec5SDimitry Andric // CommandObjectThreadSelect 1112*0b57cec5SDimitry Andric 1113*0b57cec5SDimitry Andric class CommandObjectThreadSelect : public CommandObjectParsed { 1114*0b57cec5SDimitry Andric public: 1115*0b57cec5SDimitry Andric CommandObjectThreadSelect(CommandInterpreter &interpreter) 1116*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "thread select", 1117*0b57cec5SDimitry Andric "Change the currently selected thread.", nullptr, 1118*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1119*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 1120*0b57cec5SDimitry Andric eCommandProcessMustBePaused) { 1121*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1122*0b57cec5SDimitry Andric CommandArgumentData thread_idx_arg; 1123*0b57cec5SDimitry Andric 1124*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1125*0b57cec5SDimitry Andric thread_idx_arg.arg_type = eArgTypeThreadIndex; 1126*0b57cec5SDimitry Andric thread_idx_arg.arg_repetition = eArgRepeatPlain; 1127*0b57cec5SDimitry Andric 1128*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1129*0b57cec5SDimitry Andric // argument entry. 1130*0b57cec5SDimitry Andric arg.push_back(thread_idx_arg); 1131*0b57cec5SDimitry Andric 1132*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1133*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1134*0b57cec5SDimitry Andric } 1135*0b57cec5SDimitry Andric 1136*0b57cec5SDimitry Andric ~CommandObjectThreadSelect() override = default; 1137*0b57cec5SDimitry Andric 1138e8d8bef9SDimitry Andric void 1139e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1140e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1141e8d8bef9SDimitry Andric if (request.GetCursorIndex()) 1142e8d8bef9SDimitry Andric return; 1143e8d8bef9SDimitry Andric 1144e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 1145e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 1146e8d8bef9SDimitry Andric request, nullptr); 1147e8d8bef9SDimitry Andric } 1148e8d8bef9SDimitry Andric 1149*0b57cec5SDimitry Andric protected: 1150*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 1151*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1152*0b57cec5SDimitry Andric if (process == nullptr) { 1153*0b57cec5SDimitry Andric result.AppendError("no process"); 1154*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1155*0b57cec5SDimitry Andric return false; 1156*0b57cec5SDimitry Andric } else if (command.GetArgumentCount() != 1) { 1157*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1158*0b57cec5SDimitry Andric "'%s' takes exactly one thread index argument:\nUsage: %s\n", 1159*0b57cec5SDimitry Andric m_cmd_name.c_str(), m_cmd_syntax.c_str()); 1160*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1161*0b57cec5SDimitry Andric return false; 1162*0b57cec5SDimitry Andric } 1163*0b57cec5SDimitry Andric 11645ffd83dbSDimitry Andric uint32_t index_id; 11655ffd83dbSDimitry Andric if (!llvm::to_integer(command.GetArgumentAtIndex(0), index_id)) { 11665ffd83dbSDimitry Andric result.AppendErrorWithFormat("Invalid thread index '%s'", 11675ffd83dbSDimitry Andric command.GetArgumentAtIndex(0)); 11685ffd83dbSDimitry Andric result.SetStatus(eReturnStatusFailed); 11695ffd83dbSDimitry Andric return false; 11705ffd83dbSDimitry Andric } 1171*0b57cec5SDimitry Andric 1172*0b57cec5SDimitry Andric Thread *new_thread = 1173*0b57cec5SDimitry Andric process->GetThreadList().FindThreadByIndexID(index_id).get(); 1174*0b57cec5SDimitry Andric if (new_thread == nullptr) { 1175*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid thread #%s.\n", 1176*0b57cec5SDimitry Andric command.GetArgumentAtIndex(0)); 1177*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1178*0b57cec5SDimitry Andric return false; 1179*0b57cec5SDimitry Andric } 1180*0b57cec5SDimitry Andric 1181*0b57cec5SDimitry Andric process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true); 1182*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1183*0b57cec5SDimitry Andric 1184*0b57cec5SDimitry Andric return result.Succeeded(); 1185*0b57cec5SDimitry Andric } 1186*0b57cec5SDimitry Andric }; 1187*0b57cec5SDimitry Andric 1188*0b57cec5SDimitry Andric // CommandObjectThreadList 1189*0b57cec5SDimitry Andric 1190*0b57cec5SDimitry Andric class CommandObjectThreadList : public CommandObjectParsed { 1191*0b57cec5SDimitry Andric public: 1192*0b57cec5SDimitry Andric CommandObjectThreadList(CommandInterpreter &interpreter) 1193*0b57cec5SDimitry Andric : CommandObjectParsed( 1194*0b57cec5SDimitry Andric interpreter, "thread list", 1195*0b57cec5SDimitry Andric "Show a summary of each thread in the current target process. " 1196*0b57cec5SDimitry Andric "Use 'settings set thread-format' to customize the individual " 1197*0b57cec5SDimitry Andric "thread listings.", 1198*0b57cec5SDimitry Andric "thread list", 1199*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1200*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} 1201*0b57cec5SDimitry Andric 1202*0b57cec5SDimitry Andric ~CommandObjectThreadList() override = default; 1203*0b57cec5SDimitry Andric 1204*0b57cec5SDimitry Andric protected: 1205*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 1206*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1207*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1208*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1209*0b57cec5SDimitry Andric const bool only_threads_with_stop_reason = false; 1210*0b57cec5SDimitry Andric const uint32_t start_frame = 0; 1211*0b57cec5SDimitry Andric const uint32_t num_frames = 0; 1212*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 0; 1213*0b57cec5SDimitry Andric process->GetStatus(strm); 1214*0b57cec5SDimitry Andric process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame, 1215*0b57cec5SDimitry Andric num_frames, num_frames_with_source, false); 1216*0b57cec5SDimitry Andric return result.Succeeded(); 1217*0b57cec5SDimitry Andric } 1218*0b57cec5SDimitry Andric }; 1219*0b57cec5SDimitry Andric 1220*0b57cec5SDimitry Andric // CommandObjectThreadInfo 1221*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_info 1222*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1223*0b57cec5SDimitry Andric 1224*0b57cec5SDimitry Andric class CommandObjectThreadInfo : public CommandObjectIterateOverThreads { 1225*0b57cec5SDimitry Andric public: 1226*0b57cec5SDimitry Andric class CommandOptions : public Options { 1227*0b57cec5SDimitry Andric public: 1228*0b57cec5SDimitry Andric CommandOptions() : Options() { OptionParsingStarting(nullptr); } 1229*0b57cec5SDimitry Andric 1230*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1231*0b57cec5SDimitry Andric 1232*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1233*0b57cec5SDimitry Andric m_json_thread = false; 1234*0b57cec5SDimitry Andric m_json_stopinfo = false; 1235*0b57cec5SDimitry Andric } 1236*0b57cec5SDimitry Andric 1237*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1238*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1239*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1240*0b57cec5SDimitry Andric Status error; 1241*0b57cec5SDimitry Andric 1242*0b57cec5SDimitry Andric switch (short_option) { 1243*0b57cec5SDimitry Andric case 'j': 1244*0b57cec5SDimitry Andric m_json_thread = true; 1245*0b57cec5SDimitry Andric break; 1246*0b57cec5SDimitry Andric 1247*0b57cec5SDimitry Andric case 's': 1248*0b57cec5SDimitry Andric m_json_stopinfo = true; 1249*0b57cec5SDimitry Andric break; 1250*0b57cec5SDimitry Andric 1251*0b57cec5SDimitry Andric default: 12529dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1253*0b57cec5SDimitry Andric } 1254*0b57cec5SDimitry Andric return error; 1255*0b57cec5SDimitry Andric } 1256*0b57cec5SDimitry Andric 1257*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1258*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_info_options); 1259*0b57cec5SDimitry Andric } 1260*0b57cec5SDimitry Andric 1261*0b57cec5SDimitry Andric bool m_json_thread; 1262*0b57cec5SDimitry Andric bool m_json_stopinfo; 1263*0b57cec5SDimitry Andric }; 1264*0b57cec5SDimitry Andric 1265*0b57cec5SDimitry Andric CommandObjectThreadInfo(CommandInterpreter &interpreter) 1266*0b57cec5SDimitry Andric : CommandObjectIterateOverThreads( 1267480093f4SDimitry Andric interpreter, "thread info", 1268480093f4SDimitry Andric "Show an extended summary of one or " 1269*0b57cec5SDimitry Andric "more threads. Defaults to the " 1270*0b57cec5SDimitry Andric "current thread.", 1271*0b57cec5SDimitry Andric "thread info", 1272*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1273*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 1274*0b57cec5SDimitry Andric m_options() { 1275*0b57cec5SDimitry Andric m_add_return = false; 1276*0b57cec5SDimitry Andric } 1277*0b57cec5SDimitry Andric 1278*0b57cec5SDimitry Andric ~CommandObjectThreadInfo() override = default; 1279*0b57cec5SDimitry Andric 1280e8d8bef9SDimitry Andric void 1281e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1282e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1283e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 1284e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 1285e8d8bef9SDimitry Andric request, nullptr); 1286e8d8bef9SDimitry Andric } 1287e8d8bef9SDimitry Andric 1288*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1289*0b57cec5SDimitry Andric 1290*0b57cec5SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 1291*0b57cec5SDimitry Andric ThreadSP thread_sp = 1292*0b57cec5SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 1293*0b57cec5SDimitry Andric if (!thread_sp) { 1294*0b57cec5SDimitry Andric result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n", 1295*0b57cec5SDimitry Andric tid); 1296*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1297*0b57cec5SDimitry Andric return false; 1298*0b57cec5SDimitry Andric } 1299*0b57cec5SDimitry Andric 1300*0b57cec5SDimitry Andric Thread *thread = thread_sp.get(); 1301*0b57cec5SDimitry Andric 1302*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1303*0b57cec5SDimitry Andric if (!thread->GetDescription(strm, eDescriptionLevelFull, 1304*0b57cec5SDimitry Andric m_options.m_json_thread, 1305*0b57cec5SDimitry Andric m_options.m_json_stopinfo)) { 1306*0b57cec5SDimitry Andric result.AppendErrorWithFormat("error displaying info for thread: \"%d\"\n", 1307*0b57cec5SDimitry Andric thread->GetIndexID()); 1308*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1309*0b57cec5SDimitry Andric return false; 1310*0b57cec5SDimitry Andric } 1311*0b57cec5SDimitry Andric return true; 1312*0b57cec5SDimitry Andric } 1313*0b57cec5SDimitry Andric 1314*0b57cec5SDimitry Andric CommandOptions m_options; 1315*0b57cec5SDimitry Andric }; 1316*0b57cec5SDimitry Andric 1317*0b57cec5SDimitry Andric // CommandObjectThreadException 1318*0b57cec5SDimitry Andric 1319*0b57cec5SDimitry Andric class CommandObjectThreadException : public CommandObjectIterateOverThreads { 1320*0b57cec5SDimitry Andric public: 1321*0b57cec5SDimitry Andric CommandObjectThreadException(CommandInterpreter &interpreter) 1322*0b57cec5SDimitry Andric : CommandObjectIterateOverThreads( 1323*0b57cec5SDimitry Andric interpreter, "thread exception", 1324*0b57cec5SDimitry Andric "Display the current exception object for a thread. Defaults to " 1325*0b57cec5SDimitry Andric "the current thread.", 1326*0b57cec5SDimitry Andric "thread exception", 1327*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1328*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} 1329*0b57cec5SDimitry Andric 1330*0b57cec5SDimitry Andric ~CommandObjectThreadException() override = default; 1331*0b57cec5SDimitry Andric 1332e8d8bef9SDimitry Andric void 1333e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1334e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1335e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 1336e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 1337e8d8bef9SDimitry Andric request, nullptr); 1338e8d8bef9SDimitry Andric } 1339e8d8bef9SDimitry Andric 1340*0b57cec5SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 1341*0b57cec5SDimitry Andric ThreadSP thread_sp = 1342*0b57cec5SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 1343*0b57cec5SDimitry Andric if (!thread_sp) { 1344*0b57cec5SDimitry Andric result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n", 1345*0b57cec5SDimitry Andric tid); 1346*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1347*0b57cec5SDimitry Andric return false; 1348*0b57cec5SDimitry Andric } 1349*0b57cec5SDimitry Andric 1350*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1351*0b57cec5SDimitry Andric ValueObjectSP exception_object_sp = thread_sp->GetCurrentException(); 1352*0b57cec5SDimitry Andric if (exception_object_sp) { 1353*0b57cec5SDimitry Andric exception_object_sp->Dump(strm); 1354*0b57cec5SDimitry Andric } 1355*0b57cec5SDimitry Andric 1356*0b57cec5SDimitry Andric ThreadSP exception_thread_sp = thread_sp->GetCurrentExceptionBacktrace(); 1357*0b57cec5SDimitry Andric if (exception_thread_sp && exception_thread_sp->IsValid()) { 1358*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 0; 1359*0b57cec5SDimitry Andric const bool stop_format = false; 1360*0b57cec5SDimitry Andric exception_thread_sp->GetStatus(strm, 0, UINT32_MAX, 1361*0b57cec5SDimitry Andric num_frames_with_source, stop_format); 1362*0b57cec5SDimitry Andric } 1363*0b57cec5SDimitry Andric 1364*0b57cec5SDimitry Andric return true; 1365*0b57cec5SDimitry Andric } 1366*0b57cec5SDimitry Andric }; 1367*0b57cec5SDimitry Andric 1368*0b57cec5SDimitry Andric // CommandObjectThreadReturn 1369*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_return 1370*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1371*0b57cec5SDimitry Andric 1372*0b57cec5SDimitry Andric class CommandObjectThreadReturn : public CommandObjectRaw { 1373*0b57cec5SDimitry Andric public: 1374*0b57cec5SDimitry Andric class CommandOptions : public Options { 1375*0b57cec5SDimitry Andric public: 1376*0b57cec5SDimitry Andric CommandOptions() : Options(), m_from_expression(false) { 1377*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 1378*0b57cec5SDimitry Andric // () 1379*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 1380*0b57cec5SDimitry Andric } 1381*0b57cec5SDimitry Andric 1382*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1383*0b57cec5SDimitry Andric 1384*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1385*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1386*0b57cec5SDimitry Andric Status error; 1387*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1388*0b57cec5SDimitry Andric 1389*0b57cec5SDimitry Andric switch (short_option) { 1390*0b57cec5SDimitry Andric case 'x': { 1391*0b57cec5SDimitry Andric bool success; 1392*0b57cec5SDimitry Andric bool tmp_value = 1393*0b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, false, &success); 1394*0b57cec5SDimitry Andric if (success) 1395*0b57cec5SDimitry Andric m_from_expression = tmp_value; 1396*0b57cec5SDimitry Andric else { 1397*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 1398*0b57cec5SDimitry Andric "invalid boolean value '%s' for 'x' option", 1399*0b57cec5SDimitry Andric option_arg.str().c_str()); 1400*0b57cec5SDimitry Andric } 1401*0b57cec5SDimitry Andric } break; 1402*0b57cec5SDimitry Andric default: 14039dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1404*0b57cec5SDimitry Andric } 1405*0b57cec5SDimitry Andric return error; 1406*0b57cec5SDimitry Andric } 1407*0b57cec5SDimitry Andric 1408*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1409*0b57cec5SDimitry Andric m_from_expression = false; 1410*0b57cec5SDimitry Andric } 1411*0b57cec5SDimitry Andric 1412*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1413*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_return_options); 1414*0b57cec5SDimitry Andric } 1415*0b57cec5SDimitry Andric 1416*0b57cec5SDimitry Andric bool m_from_expression; 1417*0b57cec5SDimitry Andric 1418*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 1419*0b57cec5SDimitry Andric }; 1420*0b57cec5SDimitry Andric 1421*0b57cec5SDimitry Andric CommandObjectThreadReturn(CommandInterpreter &interpreter) 1422*0b57cec5SDimitry Andric : CommandObjectRaw(interpreter, "thread return", 1423*0b57cec5SDimitry Andric "Prematurely return from a stack frame, " 1424*0b57cec5SDimitry Andric "short-circuiting execution of newer frames " 1425*0b57cec5SDimitry Andric "and optionally yielding a specified value. Defaults " 1426*0b57cec5SDimitry Andric "to the exiting the current stack " 1427*0b57cec5SDimitry Andric "frame.", 1428*0b57cec5SDimitry Andric "thread return", 1429*0b57cec5SDimitry Andric eCommandRequiresFrame | eCommandTryTargetAPILock | 1430*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 1431*0b57cec5SDimitry Andric eCommandProcessMustBePaused), 1432*0b57cec5SDimitry Andric m_options() { 1433*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1434*0b57cec5SDimitry Andric CommandArgumentData expression_arg; 1435*0b57cec5SDimitry Andric 1436*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1437*0b57cec5SDimitry Andric expression_arg.arg_type = eArgTypeExpression; 1438*0b57cec5SDimitry Andric expression_arg.arg_repetition = eArgRepeatOptional; 1439*0b57cec5SDimitry Andric 1440*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1441*0b57cec5SDimitry Andric // argument entry. 1442*0b57cec5SDimitry Andric arg.push_back(expression_arg); 1443*0b57cec5SDimitry Andric 1444*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1445*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1446*0b57cec5SDimitry Andric } 1447*0b57cec5SDimitry Andric 1448*0b57cec5SDimitry Andric ~CommandObjectThreadReturn() override = default; 1449*0b57cec5SDimitry Andric 1450*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1451*0b57cec5SDimitry Andric 1452*0b57cec5SDimitry Andric protected: 1453*0b57cec5SDimitry Andric bool DoExecute(llvm::StringRef command, 1454*0b57cec5SDimitry Andric CommandReturnObject &result) override { 1455*0b57cec5SDimitry Andric // I am going to handle this by hand, because I don't want you to have to 1456*0b57cec5SDimitry Andric // say: 1457*0b57cec5SDimitry Andric // "thread return -- -5". 1458*0b57cec5SDimitry Andric if (command.startswith("-x")) { 1459*0b57cec5SDimitry Andric if (command.size() != 2U) 1460*0b57cec5SDimitry Andric result.AppendWarning("Return values ignored when returning from user " 1461*0b57cec5SDimitry Andric "called expressions"); 1462*0b57cec5SDimitry Andric 1463*0b57cec5SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 1464*0b57cec5SDimitry Andric Status error; 1465*0b57cec5SDimitry Andric error = thread->UnwindInnermostExpression(); 1466*0b57cec5SDimitry Andric if (!error.Success()) { 1467*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Unwinding expression failed - %s.", 1468*0b57cec5SDimitry Andric error.AsCString()); 1469*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1470*0b57cec5SDimitry Andric } else { 1471*0b57cec5SDimitry Andric bool success = 1472*0b57cec5SDimitry Andric thread->SetSelectedFrameByIndexNoisily(0, result.GetOutputStream()); 1473*0b57cec5SDimitry Andric if (success) { 1474*0b57cec5SDimitry Andric m_exe_ctx.SetFrameSP(thread->GetSelectedFrame()); 1475*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1476*0b57cec5SDimitry Andric } else { 1477*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1478*0b57cec5SDimitry Andric "Could not select 0th frame after unwinding expression."); 1479*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1480*0b57cec5SDimitry Andric } 1481*0b57cec5SDimitry Andric } 1482*0b57cec5SDimitry Andric return result.Succeeded(); 1483*0b57cec5SDimitry Andric } 1484*0b57cec5SDimitry Andric 1485*0b57cec5SDimitry Andric ValueObjectSP return_valobj_sp; 1486*0b57cec5SDimitry Andric 1487*0b57cec5SDimitry Andric StackFrameSP frame_sp = m_exe_ctx.GetFrameSP(); 1488*0b57cec5SDimitry Andric uint32_t frame_idx = frame_sp->GetFrameIndex(); 1489*0b57cec5SDimitry Andric 1490*0b57cec5SDimitry Andric if (frame_sp->IsInlined()) { 1491*0b57cec5SDimitry Andric result.AppendError("Don't know how to return from inlined frames."); 1492*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1493*0b57cec5SDimitry Andric return false; 1494*0b57cec5SDimitry Andric } 1495*0b57cec5SDimitry Andric 1496*0b57cec5SDimitry Andric if (!command.empty()) { 1497*0b57cec5SDimitry Andric Target *target = m_exe_ctx.GetTargetPtr(); 1498*0b57cec5SDimitry Andric EvaluateExpressionOptions options; 1499*0b57cec5SDimitry Andric 1500*0b57cec5SDimitry Andric options.SetUnwindOnError(true); 1501*0b57cec5SDimitry Andric options.SetUseDynamic(eNoDynamicValues); 1502*0b57cec5SDimitry Andric 1503*0b57cec5SDimitry Andric ExpressionResults exe_results = eExpressionSetupError; 1504*0b57cec5SDimitry Andric exe_results = target->EvaluateExpression(command, frame_sp.get(), 1505*0b57cec5SDimitry Andric return_valobj_sp, options); 1506*0b57cec5SDimitry Andric if (exe_results != eExpressionCompleted) { 1507*0b57cec5SDimitry Andric if (return_valobj_sp) 1508*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1509*0b57cec5SDimitry Andric "Error evaluating result expression: %s", 1510*0b57cec5SDimitry Andric return_valobj_sp->GetError().AsCString()); 1511*0b57cec5SDimitry Andric else 1512*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1513*0b57cec5SDimitry Andric "Unknown error evaluating result expression."); 1514*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1515*0b57cec5SDimitry Andric return false; 1516*0b57cec5SDimitry Andric } 1517*0b57cec5SDimitry Andric } 1518*0b57cec5SDimitry Andric 1519*0b57cec5SDimitry Andric Status error; 1520*0b57cec5SDimitry Andric ThreadSP thread_sp = m_exe_ctx.GetThreadSP(); 1521*0b57cec5SDimitry Andric const bool broadcast = true; 1522*0b57cec5SDimitry Andric error = thread_sp->ReturnFromFrame(frame_sp, return_valobj_sp, broadcast); 1523*0b57cec5SDimitry Andric if (!error.Success()) { 1524*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1525*0b57cec5SDimitry Andric "Error returning from frame %d of thread %d: %s.", frame_idx, 1526*0b57cec5SDimitry Andric thread_sp->GetIndexID(), error.AsCString()); 1527*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1528*0b57cec5SDimitry Andric return false; 1529*0b57cec5SDimitry Andric } 1530*0b57cec5SDimitry Andric 1531*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1532*0b57cec5SDimitry Andric return true; 1533*0b57cec5SDimitry Andric } 1534*0b57cec5SDimitry Andric 1535*0b57cec5SDimitry Andric CommandOptions m_options; 1536*0b57cec5SDimitry Andric }; 1537*0b57cec5SDimitry Andric 1538*0b57cec5SDimitry Andric // CommandObjectThreadJump 1539*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_jump 1540*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1541*0b57cec5SDimitry Andric 1542*0b57cec5SDimitry Andric class CommandObjectThreadJump : public CommandObjectParsed { 1543*0b57cec5SDimitry Andric public: 1544*0b57cec5SDimitry Andric class CommandOptions : public Options { 1545*0b57cec5SDimitry Andric public: 1546*0b57cec5SDimitry Andric CommandOptions() : Options() { OptionParsingStarting(nullptr); } 1547*0b57cec5SDimitry Andric 1548*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1549*0b57cec5SDimitry Andric 1550*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1551*0b57cec5SDimitry Andric m_filenames.Clear(); 1552*0b57cec5SDimitry Andric m_line_num = 0; 1553*0b57cec5SDimitry Andric m_line_offset = 0; 1554*0b57cec5SDimitry Andric m_load_addr = LLDB_INVALID_ADDRESS; 1555*0b57cec5SDimitry Andric m_force = false; 1556*0b57cec5SDimitry Andric } 1557*0b57cec5SDimitry Andric 1558*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1559*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1560*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1561*0b57cec5SDimitry Andric Status error; 1562*0b57cec5SDimitry Andric 1563*0b57cec5SDimitry Andric switch (short_option) { 1564*0b57cec5SDimitry Andric case 'f': 1565*0b57cec5SDimitry Andric m_filenames.AppendIfUnique(FileSpec(option_arg)); 1566*0b57cec5SDimitry Andric if (m_filenames.GetSize() > 1) 1567*0b57cec5SDimitry Andric return Status("only one source file expected."); 1568*0b57cec5SDimitry Andric break; 1569*0b57cec5SDimitry Andric case 'l': 1570*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_line_num)) 1571*0b57cec5SDimitry Andric return Status("invalid line number: '%s'.", option_arg.str().c_str()); 1572*0b57cec5SDimitry Andric break; 1573*0b57cec5SDimitry Andric case 'b': 1574*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_line_offset)) 1575*0b57cec5SDimitry Andric return Status("invalid line offset: '%s'.", option_arg.str().c_str()); 1576*0b57cec5SDimitry Andric break; 1577*0b57cec5SDimitry Andric case 'a': 1578*0b57cec5SDimitry Andric m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg, 1579*0b57cec5SDimitry Andric LLDB_INVALID_ADDRESS, &error); 1580*0b57cec5SDimitry Andric break; 1581*0b57cec5SDimitry Andric case 'r': 1582*0b57cec5SDimitry Andric m_force = true; 1583*0b57cec5SDimitry Andric break; 1584*0b57cec5SDimitry Andric default: 15859dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1586*0b57cec5SDimitry Andric } 1587*0b57cec5SDimitry Andric return error; 1588*0b57cec5SDimitry Andric } 1589*0b57cec5SDimitry Andric 1590*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1591*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_jump_options); 1592*0b57cec5SDimitry Andric } 1593*0b57cec5SDimitry Andric 1594*0b57cec5SDimitry Andric FileSpecList m_filenames; 1595*0b57cec5SDimitry Andric uint32_t m_line_num; 1596*0b57cec5SDimitry Andric int32_t m_line_offset; 1597*0b57cec5SDimitry Andric lldb::addr_t m_load_addr; 1598*0b57cec5SDimitry Andric bool m_force; 1599*0b57cec5SDimitry Andric }; 1600*0b57cec5SDimitry Andric 1601*0b57cec5SDimitry Andric CommandObjectThreadJump(CommandInterpreter &interpreter) 1602*0b57cec5SDimitry Andric : CommandObjectParsed( 1603*0b57cec5SDimitry Andric interpreter, "thread jump", 1604*0b57cec5SDimitry Andric "Sets the program counter to a new address.", "thread jump", 1605*0b57cec5SDimitry Andric eCommandRequiresFrame | eCommandTryTargetAPILock | 1606*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 1607*0b57cec5SDimitry Andric m_options() {} 1608*0b57cec5SDimitry Andric 1609*0b57cec5SDimitry Andric ~CommandObjectThreadJump() override = default; 1610*0b57cec5SDimitry Andric 1611*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1612*0b57cec5SDimitry Andric 1613*0b57cec5SDimitry Andric protected: 1614*0b57cec5SDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 1615*0b57cec5SDimitry Andric RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext(); 1616*0b57cec5SDimitry Andric StackFrame *frame = m_exe_ctx.GetFramePtr(); 1617*0b57cec5SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 1618*0b57cec5SDimitry Andric Target *target = m_exe_ctx.GetTargetPtr(); 1619*0b57cec5SDimitry Andric const SymbolContext &sym_ctx = 1620*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextLineEntry); 1621*0b57cec5SDimitry Andric 1622*0b57cec5SDimitry Andric if (m_options.m_load_addr != LLDB_INVALID_ADDRESS) { 1623*0b57cec5SDimitry Andric // Use this address directly. 1624*0b57cec5SDimitry Andric Address dest = Address(m_options.m_load_addr); 1625*0b57cec5SDimitry Andric 1626*0b57cec5SDimitry Andric lldb::addr_t callAddr = dest.GetCallableLoadAddress(target); 1627*0b57cec5SDimitry Andric if (callAddr == LLDB_INVALID_ADDRESS) { 1628*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid destination address."); 1629*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1630*0b57cec5SDimitry Andric return false; 1631*0b57cec5SDimitry Andric } 1632*0b57cec5SDimitry Andric 1633*0b57cec5SDimitry Andric if (!reg_ctx->SetPC(callAddr)) { 1634*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Error changing PC value for thread %d.", 1635*0b57cec5SDimitry Andric thread->GetIndexID()); 1636*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1637*0b57cec5SDimitry Andric return false; 1638*0b57cec5SDimitry Andric } 1639*0b57cec5SDimitry Andric } else { 1640*0b57cec5SDimitry Andric // Pick either the absolute line, or work out a relative one. 1641*0b57cec5SDimitry Andric int32_t line = (int32_t)m_options.m_line_num; 1642*0b57cec5SDimitry Andric if (line == 0) 1643*0b57cec5SDimitry Andric line = sym_ctx.line_entry.line + m_options.m_line_offset; 1644*0b57cec5SDimitry Andric 1645*0b57cec5SDimitry Andric // Try the current file, but override if asked. 1646*0b57cec5SDimitry Andric FileSpec file = sym_ctx.line_entry.file; 1647*0b57cec5SDimitry Andric if (m_options.m_filenames.GetSize() == 1) 1648*0b57cec5SDimitry Andric file = m_options.m_filenames.GetFileSpecAtIndex(0); 1649*0b57cec5SDimitry Andric 1650*0b57cec5SDimitry Andric if (!file) { 1651*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1652*0b57cec5SDimitry Andric "No source file available for the current location."); 1653*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1654*0b57cec5SDimitry Andric return false; 1655*0b57cec5SDimitry Andric } 1656*0b57cec5SDimitry Andric 1657*0b57cec5SDimitry Andric std::string warnings; 1658*0b57cec5SDimitry Andric Status err = thread->JumpToLine(file, line, m_options.m_force, &warnings); 1659*0b57cec5SDimitry Andric 1660*0b57cec5SDimitry Andric if (err.Fail()) { 1661*0b57cec5SDimitry Andric result.SetError(err); 1662*0b57cec5SDimitry Andric return false; 1663*0b57cec5SDimitry Andric } 1664*0b57cec5SDimitry Andric 1665*0b57cec5SDimitry Andric if (!warnings.empty()) 1666*0b57cec5SDimitry Andric result.AppendWarning(warnings.c_str()); 1667*0b57cec5SDimitry Andric } 1668*0b57cec5SDimitry Andric 1669*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1670*0b57cec5SDimitry Andric return true; 1671*0b57cec5SDimitry Andric } 1672*0b57cec5SDimitry Andric 1673*0b57cec5SDimitry Andric CommandOptions m_options; 1674*0b57cec5SDimitry Andric }; 1675*0b57cec5SDimitry Andric 1676*0b57cec5SDimitry Andric // Next are the subcommands of CommandObjectMultiwordThreadPlan 1677*0b57cec5SDimitry Andric 1678*0b57cec5SDimitry Andric // CommandObjectThreadPlanList 1679*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_plan_list 1680*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1681*0b57cec5SDimitry Andric 1682*0b57cec5SDimitry Andric class CommandObjectThreadPlanList : public CommandObjectIterateOverThreads { 1683*0b57cec5SDimitry Andric public: 1684*0b57cec5SDimitry Andric class CommandOptions : public Options { 1685*0b57cec5SDimitry Andric public: 1686*0b57cec5SDimitry Andric CommandOptions() : Options() { 1687*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 1688*0b57cec5SDimitry Andric // () 1689*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 1690*0b57cec5SDimitry Andric } 1691*0b57cec5SDimitry Andric 1692*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1693*0b57cec5SDimitry Andric 1694*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1695*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1696*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1697*0b57cec5SDimitry Andric 1698*0b57cec5SDimitry Andric switch (short_option) { 1699*0b57cec5SDimitry Andric case 'i': 1700*0b57cec5SDimitry Andric m_internal = true; 1701*0b57cec5SDimitry Andric break; 17025ffd83dbSDimitry Andric case 't': 17035ffd83dbSDimitry Andric lldb::tid_t tid; 17045ffd83dbSDimitry Andric if (option_arg.getAsInteger(0, tid)) 17055ffd83dbSDimitry Andric return Status("invalid tid: '%s'.", option_arg.str().c_str()); 17065ffd83dbSDimitry Andric m_tids.push_back(tid); 17075ffd83dbSDimitry Andric break; 17085ffd83dbSDimitry Andric case 'u': 17095ffd83dbSDimitry Andric m_unreported = false; 17105ffd83dbSDimitry Andric break; 1711*0b57cec5SDimitry Andric case 'v': 1712*0b57cec5SDimitry Andric m_verbose = true; 1713*0b57cec5SDimitry Andric break; 1714*0b57cec5SDimitry Andric default: 17159dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1716*0b57cec5SDimitry Andric } 17175ffd83dbSDimitry Andric return {}; 1718*0b57cec5SDimitry Andric } 1719*0b57cec5SDimitry Andric 1720*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1721*0b57cec5SDimitry Andric m_verbose = false; 1722*0b57cec5SDimitry Andric m_internal = false; 17235ffd83dbSDimitry Andric m_unreported = true; // The variable is "skip unreported" and we want to 17245ffd83dbSDimitry Andric // skip unreported by default. 17255ffd83dbSDimitry Andric m_tids.clear(); 1726*0b57cec5SDimitry Andric } 1727*0b57cec5SDimitry Andric 1728*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1729*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_plan_list_options); 1730*0b57cec5SDimitry Andric } 1731*0b57cec5SDimitry Andric 1732*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 1733*0b57cec5SDimitry Andric bool m_verbose; 1734*0b57cec5SDimitry Andric bool m_internal; 17355ffd83dbSDimitry Andric bool m_unreported; 17365ffd83dbSDimitry Andric std::vector<lldb::tid_t> m_tids; 1737*0b57cec5SDimitry Andric }; 1738*0b57cec5SDimitry Andric 1739*0b57cec5SDimitry Andric CommandObjectThreadPlanList(CommandInterpreter &interpreter) 1740*0b57cec5SDimitry Andric : CommandObjectIterateOverThreads( 1741*0b57cec5SDimitry Andric interpreter, "thread plan list", 1742*0b57cec5SDimitry Andric "Show thread plans for one or more threads. If no threads are " 1743*0b57cec5SDimitry Andric "specified, show the " 1744*0b57cec5SDimitry Andric "current thread. Use the thread-index \"all\" to see all threads.", 1745*0b57cec5SDimitry Andric nullptr, 1746*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 1747*0b57cec5SDimitry Andric eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | 1748*0b57cec5SDimitry Andric eCommandProcessMustBePaused), 1749*0b57cec5SDimitry Andric m_options() {} 1750*0b57cec5SDimitry Andric 1751*0b57cec5SDimitry Andric ~CommandObjectThreadPlanList() override = default; 1752*0b57cec5SDimitry Andric 1753*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1754*0b57cec5SDimitry Andric 17555ffd83dbSDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 17565ffd83dbSDimitry Andric // If we are reporting all threads, dispatch to the Process to do that: 17575ffd83dbSDimitry Andric if (command.GetArgumentCount() == 0 && m_options.m_tids.empty()) { 17585ffd83dbSDimitry Andric Stream &strm = result.GetOutputStream(); 17595ffd83dbSDimitry Andric DescriptionLevel desc_level = m_options.m_verbose 17605ffd83dbSDimitry Andric ? eDescriptionLevelVerbose 17615ffd83dbSDimitry Andric : eDescriptionLevelFull; 17625ffd83dbSDimitry Andric m_exe_ctx.GetProcessPtr()->DumpThreadPlans( 17635ffd83dbSDimitry Andric strm, desc_level, m_options.m_internal, true, m_options.m_unreported); 17645ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 17655ffd83dbSDimitry Andric return true; 17665ffd83dbSDimitry Andric } else { 17675ffd83dbSDimitry Andric // Do any TID's that the user may have specified as TID, then do any 17685ffd83dbSDimitry Andric // Thread Indexes... 17695ffd83dbSDimitry Andric if (!m_options.m_tids.empty()) { 17705ffd83dbSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 17715ffd83dbSDimitry Andric StreamString tmp_strm; 17725ffd83dbSDimitry Andric for (lldb::tid_t tid : m_options.m_tids) { 17735ffd83dbSDimitry Andric bool success = process->DumpThreadPlansForTID( 17745ffd83dbSDimitry Andric tmp_strm, tid, eDescriptionLevelFull, m_options.m_internal, 17755ffd83dbSDimitry Andric true /* condense_trivial */, m_options.m_unreported); 17765ffd83dbSDimitry Andric // If we didn't find a TID, stop here and return an error. 17775ffd83dbSDimitry Andric if (!success) { 17785ffd83dbSDimitry Andric result.SetError("Error dumping plans:"); 17795ffd83dbSDimitry Andric result.AppendError(tmp_strm.GetString()); 1780*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1781*0b57cec5SDimitry Andric return false; 1782*0b57cec5SDimitry Andric } 17835ffd83dbSDimitry Andric // Otherwise, add our data to the output: 17845ffd83dbSDimitry Andric result.GetOutputStream() << tmp_strm.GetString(); 17855ffd83dbSDimitry Andric } 17865ffd83dbSDimitry Andric } 17875ffd83dbSDimitry Andric return CommandObjectIterateOverThreads::DoExecute(command, result); 17885ffd83dbSDimitry Andric } 17895ffd83dbSDimitry Andric } 1790*0b57cec5SDimitry Andric 17915ffd83dbSDimitry Andric protected: 17925ffd83dbSDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 17935ffd83dbSDimitry Andric // If we have already handled this from a -t option, skip it here. 1794e8d8bef9SDimitry Andric if (llvm::is_contained(m_options.m_tids, tid)) 17955ffd83dbSDimitry Andric return true; 17965ffd83dbSDimitry Andric 17975ffd83dbSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1798*0b57cec5SDimitry Andric 1799*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1800*0b57cec5SDimitry Andric DescriptionLevel desc_level = eDescriptionLevelFull; 1801*0b57cec5SDimitry Andric if (m_options.m_verbose) 1802*0b57cec5SDimitry Andric desc_level = eDescriptionLevelVerbose; 1803*0b57cec5SDimitry Andric 18045ffd83dbSDimitry Andric process->DumpThreadPlansForTID(strm, tid, desc_level, m_options.m_internal, 18055ffd83dbSDimitry Andric true /* condense_trivial */, 18065ffd83dbSDimitry Andric m_options.m_unreported); 1807*0b57cec5SDimitry Andric return true; 1808*0b57cec5SDimitry Andric } 1809*0b57cec5SDimitry Andric 1810*0b57cec5SDimitry Andric CommandOptions m_options; 1811*0b57cec5SDimitry Andric }; 1812*0b57cec5SDimitry Andric 1813*0b57cec5SDimitry Andric class CommandObjectThreadPlanDiscard : public CommandObjectParsed { 1814*0b57cec5SDimitry Andric public: 1815*0b57cec5SDimitry Andric CommandObjectThreadPlanDiscard(CommandInterpreter &interpreter) 1816*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "thread plan discard", 1817*0b57cec5SDimitry Andric "Discards thread plans up to and including the " 1818*0b57cec5SDimitry Andric "specified index (see 'thread plan list'.) " 1819*0b57cec5SDimitry Andric "Only user visible plans can be discarded.", 1820*0b57cec5SDimitry Andric nullptr, 1821*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 1822*0b57cec5SDimitry Andric eCommandTryTargetAPILock | 1823*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 1824*0b57cec5SDimitry Andric eCommandProcessMustBePaused) { 1825*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1826*0b57cec5SDimitry Andric CommandArgumentData plan_index_arg; 1827*0b57cec5SDimitry Andric 1828*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1829*0b57cec5SDimitry Andric plan_index_arg.arg_type = eArgTypeUnsignedInteger; 1830*0b57cec5SDimitry Andric plan_index_arg.arg_repetition = eArgRepeatPlain; 1831*0b57cec5SDimitry Andric 1832*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1833*0b57cec5SDimitry Andric // argument entry. 1834*0b57cec5SDimitry Andric arg.push_back(plan_index_arg); 1835*0b57cec5SDimitry Andric 1836*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1837*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1838*0b57cec5SDimitry Andric } 1839*0b57cec5SDimitry Andric 1840*0b57cec5SDimitry Andric ~CommandObjectThreadPlanDiscard() override = default; 1841*0b57cec5SDimitry Andric 1842e8d8bef9SDimitry Andric void 1843e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1844e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1845e8d8bef9SDimitry Andric if (!m_exe_ctx.HasThreadScope() || request.GetCursorIndex()) 1846e8d8bef9SDimitry Andric return; 1847e8d8bef9SDimitry Andric 1848e8d8bef9SDimitry Andric m_exe_ctx.GetThreadPtr()->AutoCompleteThreadPlans(request); 1849e8d8bef9SDimitry Andric } 1850e8d8bef9SDimitry Andric 1851*0b57cec5SDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 1852*0b57cec5SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 1853*0b57cec5SDimitry Andric if (args.GetArgumentCount() != 1) { 1854*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Too many arguments, expected one - the " 1855*0b57cec5SDimitry Andric "thread plan index - but got %zu.", 1856*0b57cec5SDimitry Andric args.GetArgumentCount()); 1857*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1858*0b57cec5SDimitry Andric return false; 1859*0b57cec5SDimitry Andric } 1860*0b57cec5SDimitry Andric 18615ffd83dbSDimitry Andric uint32_t thread_plan_idx; 18625ffd83dbSDimitry Andric if (!llvm::to_integer(args.GetArgumentAtIndex(0), thread_plan_idx)) { 1863*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1864*0b57cec5SDimitry Andric "Invalid thread index: \"%s\" - should be unsigned int.", 1865*0b57cec5SDimitry Andric args.GetArgumentAtIndex(0)); 1866*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1867*0b57cec5SDimitry Andric return false; 1868*0b57cec5SDimitry Andric } 1869*0b57cec5SDimitry Andric 1870*0b57cec5SDimitry Andric if (thread_plan_idx == 0) { 1871*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1872*0b57cec5SDimitry Andric "You wouldn't really want me to discard the base thread plan."); 1873*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1874*0b57cec5SDimitry Andric return false; 1875*0b57cec5SDimitry Andric } 1876*0b57cec5SDimitry Andric 1877*0b57cec5SDimitry Andric if (thread->DiscardUserThreadPlansUpToIndex(thread_plan_idx)) { 1878*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1879*0b57cec5SDimitry Andric return true; 1880*0b57cec5SDimitry Andric } else { 1881*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1882*0b57cec5SDimitry Andric "Could not find User thread plan with index %s.", 1883*0b57cec5SDimitry Andric args.GetArgumentAtIndex(0)); 1884*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 1885*0b57cec5SDimitry Andric return false; 1886*0b57cec5SDimitry Andric } 1887*0b57cec5SDimitry Andric } 1888*0b57cec5SDimitry Andric }; 1889*0b57cec5SDimitry Andric 18905ffd83dbSDimitry Andric class CommandObjectThreadPlanPrune : public CommandObjectParsed { 18915ffd83dbSDimitry Andric public: 18925ffd83dbSDimitry Andric CommandObjectThreadPlanPrune(CommandInterpreter &interpreter) 18935ffd83dbSDimitry Andric : CommandObjectParsed(interpreter, "thread plan prune", 18945ffd83dbSDimitry Andric "Removes any thread plans associated with " 18955ffd83dbSDimitry Andric "currently unreported threads. " 18965ffd83dbSDimitry Andric "Specify one or more TID's to remove, or if no " 18975ffd83dbSDimitry Andric "TID's are provides, remove threads for all " 18985ffd83dbSDimitry Andric "unreported threads", 18995ffd83dbSDimitry Andric nullptr, 19005ffd83dbSDimitry Andric eCommandRequiresProcess | 19015ffd83dbSDimitry Andric eCommandTryTargetAPILock | 19025ffd83dbSDimitry Andric eCommandProcessMustBeLaunched | 19035ffd83dbSDimitry Andric eCommandProcessMustBePaused) { 19045ffd83dbSDimitry Andric CommandArgumentEntry arg; 19055ffd83dbSDimitry Andric CommandArgumentData tid_arg; 19065ffd83dbSDimitry Andric 19075ffd83dbSDimitry Andric // Define the first (and only) variant of this arg. 19085ffd83dbSDimitry Andric tid_arg.arg_type = eArgTypeThreadID; 19095ffd83dbSDimitry Andric tid_arg.arg_repetition = eArgRepeatStar; 19105ffd83dbSDimitry Andric 19115ffd83dbSDimitry Andric // There is only one variant this argument could be; put it into the 19125ffd83dbSDimitry Andric // argument entry. 19135ffd83dbSDimitry Andric arg.push_back(tid_arg); 19145ffd83dbSDimitry Andric 19155ffd83dbSDimitry Andric // Push the data for the first argument into the m_arguments vector. 19165ffd83dbSDimitry Andric m_arguments.push_back(arg); 19175ffd83dbSDimitry Andric } 19185ffd83dbSDimitry Andric 19195ffd83dbSDimitry Andric ~CommandObjectThreadPlanPrune() override = default; 19205ffd83dbSDimitry Andric 19215ffd83dbSDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 19225ffd83dbSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 19235ffd83dbSDimitry Andric 19245ffd83dbSDimitry Andric if (args.GetArgumentCount() == 0) { 19255ffd83dbSDimitry Andric process->PruneThreadPlans(); 19265ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 19275ffd83dbSDimitry Andric return true; 19285ffd83dbSDimitry Andric } 19295ffd83dbSDimitry Andric 19305ffd83dbSDimitry Andric const size_t num_args = args.GetArgumentCount(); 19315ffd83dbSDimitry Andric 19325ffd83dbSDimitry Andric std::lock_guard<std::recursive_mutex> guard( 19335ffd83dbSDimitry Andric process->GetThreadList().GetMutex()); 19345ffd83dbSDimitry Andric 19355ffd83dbSDimitry Andric for (size_t i = 0; i < num_args; i++) { 19365ffd83dbSDimitry Andric lldb::tid_t tid; 19375ffd83dbSDimitry Andric if (!llvm::to_integer(args.GetArgumentAtIndex(i), tid)) { 19385ffd83dbSDimitry Andric result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n", 19395ffd83dbSDimitry Andric args.GetArgumentAtIndex(i)); 19405ffd83dbSDimitry Andric result.SetStatus(eReturnStatusFailed); 19415ffd83dbSDimitry Andric return false; 19425ffd83dbSDimitry Andric } 19435ffd83dbSDimitry Andric if (!process->PruneThreadPlansForTID(tid)) { 19445ffd83dbSDimitry Andric result.AppendErrorWithFormat("Could not find unreported tid: \"%s\"\n", 19455ffd83dbSDimitry Andric args.GetArgumentAtIndex(i)); 19465ffd83dbSDimitry Andric result.SetStatus(eReturnStatusFailed); 19475ffd83dbSDimitry Andric return false; 19485ffd83dbSDimitry Andric } 19495ffd83dbSDimitry Andric } 19505ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 19515ffd83dbSDimitry Andric return true; 19525ffd83dbSDimitry Andric } 19535ffd83dbSDimitry Andric }; 19545ffd83dbSDimitry Andric 1955*0b57cec5SDimitry Andric // CommandObjectMultiwordThreadPlan 1956*0b57cec5SDimitry Andric 1957*0b57cec5SDimitry Andric class CommandObjectMultiwordThreadPlan : public CommandObjectMultiword { 1958*0b57cec5SDimitry Andric public: 1959*0b57cec5SDimitry Andric CommandObjectMultiwordThreadPlan(CommandInterpreter &interpreter) 1960*0b57cec5SDimitry Andric : CommandObjectMultiword( 1961*0b57cec5SDimitry Andric interpreter, "plan", 1962*0b57cec5SDimitry Andric "Commands for managing thread plans that control execution.", 1963*0b57cec5SDimitry Andric "thread plan <subcommand> [<subcommand objects]") { 1964*0b57cec5SDimitry Andric LoadSubCommand( 1965*0b57cec5SDimitry Andric "list", CommandObjectSP(new CommandObjectThreadPlanList(interpreter))); 1966*0b57cec5SDimitry Andric LoadSubCommand( 1967*0b57cec5SDimitry Andric "discard", 1968*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadPlanDiscard(interpreter))); 19695ffd83dbSDimitry Andric LoadSubCommand( 19705ffd83dbSDimitry Andric "prune", 19715ffd83dbSDimitry Andric CommandObjectSP(new CommandObjectThreadPlanPrune(interpreter))); 1972*0b57cec5SDimitry Andric } 1973*0b57cec5SDimitry Andric 1974*0b57cec5SDimitry Andric ~CommandObjectMultiwordThreadPlan() override = default; 1975*0b57cec5SDimitry Andric }; 1976*0b57cec5SDimitry Andric 1977e8d8bef9SDimitry Andric // Next are the subcommands of CommandObjectMultiwordTrace 1978e8d8bef9SDimitry Andric 1979e8d8bef9SDimitry Andric // CommandObjectTraceStart 1980e8d8bef9SDimitry Andric 1981e8d8bef9SDimitry Andric /// This class works by delegating the logic to the actual trace plug-in that 1982e8d8bef9SDimitry Andric /// can support the current process. 1983e8d8bef9SDimitry Andric class CommandObjectTraceStart : public CommandObjectProxy { 1984e8d8bef9SDimitry Andric public: 1985e8d8bef9SDimitry Andric CommandObjectTraceStart(CommandInterpreter &interpreter) 1986e8d8bef9SDimitry Andric : CommandObjectProxy(interpreter, "thread trace start", 1987e8d8bef9SDimitry Andric "Start tracing threads with the corresponding trace " 1988e8d8bef9SDimitry Andric "plug-in for the current process.", 1989e8d8bef9SDimitry Andric "thread trace start [<trace-options>]") {} 1990e8d8bef9SDimitry Andric 1991e8d8bef9SDimitry Andric protected: 1992e8d8bef9SDimitry Andric llvm::Expected<CommandObjectSP> DoGetProxyCommandObject() { 1993e8d8bef9SDimitry Andric ProcessSP process_sp = m_interpreter.GetExecutionContext().GetProcessSP(); 1994e8d8bef9SDimitry Andric 1995e8d8bef9SDimitry Andric if (!process_sp) 1996e8d8bef9SDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 1997e8d8bef9SDimitry Andric "Process not available."); 1998e8d8bef9SDimitry Andric if (!process_sp->IsAlive()) 1999e8d8bef9SDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 2000e8d8bef9SDimitry Andric "Process must be launched."); 2001e8d8bef9SDimitry Andric 2002e8d8bef9SDimitry Andric llvm::Expected<TraceTypeInfo> trace_type = 2003e8d8bef9SDimitry Andric process_sp->GetSupportedTraceType(); 2004e8d8bef9SDimitry Andric 2005e8d8bef9SDimitry Andric if (!trace_type) 2006e8d8bef9SDimitry Andric return llvm::createStringError( 2007e8d8bef9SDimitry Andric llvm::inconvertibleErrorCode(), "Tracing is not supported. %s", 2008e8d8bef9SDimitry Andric llvm::toString(trace_type.takeError()).c_str()); 2009e8d8bef9SDimitry Andric 2010e8d8bef9SDimitry Andric CommandObjectSP delegate_sp = 2011e8d8bef9SDimitry Andric PluginManager::GetTraceStartCommand(trace_type->name, m_interpreter); 2012e8d8bef9SDimitry Andric if (!delegate_sp) 2013e8d8bef9SDimitry Andric return llvm::createStringError( 2014e8d8bef9SDimitry Andric llvm::inconvertibleErrorCode(), 2015e8d8bef9SDimitry Andric "No trace plug-in matches the specified type: \"%s\"", 2016e8d8bef9SDimitry Andric trace_type->name.c_str()); 2017e8d8bef9SDimitry Andric return delegate_sp; 2018e8d8bef9SDimitry Andric } 2019e8d8bef9SDimitry Andric 2020e8d8bef9SDimitry Andric CommandObject *GetProxyCommandObject() override { 2021e8d8bef9SDimitry Andric if (llvm::Expected<CommandObjectSP> delegate = DoGetProxyCommandObject()) { 2022e8d8bef9SDimitry Andric m_delegate_sp = *delegate; 2023e8d8bef9SDimitry Andric m_delegate_error.clear(); 2024e8d8bef9SDimitry Andric return m_delegate_sp.get(); 2025e8d8bef9SDimitry Andric } else { 2026e8d8bef9SDimitry Andric m_delegate_sp.reset(); 2027e8d8bef9SDimitry Andric m_delegate_error = llvm::toString(delegate.takeError()); 2028e8d8bef9SDimitry Andric return nullptr; 2029e8d8bef9SDimitry Andric } 2030e8d8bef9SDimitry Andric } 2031e8d8bef9SDimitry Andric 2032e8d8bef9SDimitry Andric private: 2033e8d8bef9SDimitry Andric llvm::StringRef GetUnsupportedError() override { return m_delegate_error; } 2034e8d8bef9SDimitry Andric 2035e8d8bef9SDimitry Andric CommandObjectSP m_delegate_sp; 2036e8d8bef9SDimitry Andric std::string m_delegate_error; 2037e8d8bef9SDimitry Andric }; 2038e8d8bef9SDimitry Andric 2039e8d8bef9SDimitry Andric // CommandObjectTraceStop 2040e8d8bef9SDimitry Andric 2041e8d8bef9SDimitry Andric class CommandObjectTraceStop : public CommandObjectIterateOverThreads { 2042e8d8bef9SDimitry Andric public: 2043e8d8bef9SDimitry Andric CommandObjectTraceStop(CommandInterpreter &interpreter) 2044e8d8bef9SDimitry Andric : CommandObjectIterateOverThreads( 2045e8d8bef9SDimitry Andric interpreter, "thread trace stop", 2046e8d8bef9SDimitry Andric "Stop tracing threads. " 2047e8d8bef9SDimitry Andric "Defaults to the current thread. Thread indices can be " 2048e8d8bef9SDimitry Andric "specified as arguments.\n Use the thread-index \"all\" to trace " 2049e8d8bef9SDimitry Andric "all threads.", 2050e8d8bef9SDimitry Andric "thread trace stop [<thread-index> <thread-index> ...]", 2051e8d8bef9SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 2052e8d8bef9SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | 2053e8d8bef9SDimitry Andric eCommandProcessMustBeTraced) {} 2054e8d8bef9SDimitry Andric 2055e8d8bef9SDimitry Andric ~CommandObjectTraceStop() override = default; 2056e8d8bef9SDimitry Andric 2057e8d8bef9SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 2058e8d8bef9SDimitry Andric const Thread &thread = 2059e8d8bef9SDimitry Andric *m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 2060e8d8bef9SDimitry Andric Trace &trace = *m_exe_ctx.GetTargetSP()->GetTrace(); 2061e8d8bef9SDimitry Andric 2062e8d8bef9SDimitry Andric if (llvm::Error err = trace.StopTracingThread(thread)) { 2063e8d8bef9SDimitry Andric result.AppendErrorWithFormat("Failed stopping thread %" PRIu64 ": %s\n", 2064e8d8bef9SDimitry Andric tid, toString(std::move(err)).c_str()); 2065e8d8bef9SDimitry Andric result.SetStatus(eReturnStatusFailed); 2066e8d8bef9SDimitry Andric } 2067e8d8bef9SDimitry Andric 2068e8d8bef9SDimitry Andric // We don't return false on errors to try to stop as many threads as 2069e8d8bef9SDimitry Andric // possible. 2070e8d8bef9SDimitry Andric return true; 2071e8d8bef9SDimitry Andric } 2072e8d8bef9SDimitry Andric }; 2073e8d8bef9SDimitry Andric 2074e8d8bef9SDimitry Andric // CommandObjectTraceDumpInstructions 2075e8d8bef9SDimitry Andric #define LLDB_OPTIONS_thread_trace_dump_instructions 2076e8d8bef9SDimitry Andric #include "CommandOptions.inc" 2077e8d8bef9SDimitry Andric 2078e8d8bef9SDimitry Andric class CommandObjectTraceDumpInstructions 2079e8d8bef9SDimitry Andric : public CommandObjectIterateOverThreads { 2080e8d8bef9SDimitry Andric public: 2081e8d8bef9SDimitry Andric class CommandOptions : public Options { 2082e8d8bef9SDimitry Andric public: 2083e8d8bef9SDimitry Andric CommandOptions() : Options() { OptionParsingStarting(nullptr); } 2084e8d8bef9SDimitry Andric 2085e8d8bef9SDimitry Andric ~CommandOptions() override = default; 2086e8d8bef9SDimitry Andric 2087e8d8bef9SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 2088e8d8bef9SDimitry Andric ExecutionContext *execution_context) override { 2089e8d8bef9SDimitry Andric Status error; 2090e8d8bef9SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 2091e8d8bef9SDimitry Andric 2092e8d8bef9SDimitry Andric switch (short_option) { 2093e8d8bef9SDimitry Andric case 'c': { 2094e8d8bef9SDimitry Andric int32_t count; 2095e8d8bef9SDimitry Andric if (option_arg.empty() || option_arg.getAsInteger(0, count) || 2096e8d8bef9SDimitry Andric count < 0) 2097e8d8bef9SDimitry Andric error.SetErrorStringWithFormat( 2098e8d8bef9SDimitry Andric "invalid integer value for option '%s'", 2099e8d8bef9SDimitry Andric option_arg.str().c_str()); 2100e8d8bef9SDimitry Andric else 2101e8d8bef9SDimitry Andric m_count = count; 2102e8d8bef9SDimitry Andric break; 2103e8d8bef9SDimitry Andric } 2104e8d8bef9SDimitry Andric case 'p': { 2105e8d8bef9SDimitry Andric int32_t position; 2106e8d8bef9SDimitry Andric if (option_arg.empty() || option_arg.getAsInteger(0, position) || 2107e8d8bef9SDimitry Andric position < 0) 2108e8d8bef9SDimitry Andric error.SetErrorStringWithFormat( 2109e8d8bef9SDimitry Andric "invalid integer value for option '%s'", 2110e8d8bef9SDimitry Andric option_arg.str().c_str()); 2111e8d8bef9SDimitry Andric else 2112e8d8bef9SDimitry Andric m_position = position; 2113e8d8bef9SDimitry Andric break; 2114e8d8bef9SDimitry Andric } 2115e8d8bef9SDimitry Andric case 'r': { 2116e8d8bef9SDimitry Andric m_raw = true; 2117e8d8bef9SDimitry Andric break; 2118e8d8bef9SDimitry Andric } 2119e8d8bef9SDimitry Andric default: 2120e8d8bef9SDimitry Andric llvm_unreachable("Unimplemented option"); 2121e8d8bef9SDimitry Andric } 2122e8d8bef9SDimitry Andric return error; 2123e8d8bef9SDimitry Andric } 2124e8d8bef9SDimitry Andric 2125e8d8bef9SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 2126e8d8bef9SDimitry Andric m_count = kDefaultCount; 2127e8d8bef9SDimitry Andric m_position = llvm::None; 2128e8d8bef9SDimitry Andric m_raw = false; 2129e8d8bef9SDimitry Andric } 2130e8d8bef9SDimitry Andric 2131e8d8bef9SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 2132e8d8bef9SDimitry Andric return llvm::makeArrayRef(g_thread_trace_dump_instructions_options); 2133e8d8bef9SDimitry Andric } 2134e8d8bef9SDimitry Andric 2135e8d8bef9SDimitry Andric static const size_t kDefaultCount = 20; 2136e8d8bef9SDimitry Andric 2137e8d8bef9SDimitry Andric // Instance variables to hold the values for command options. 2138e8d8bef9SDimitry Andric size_t m_count; 2139e8d8bef9SDimitry Andric llvm::Optional<ssize_t> m_position; 2140e8d8bef9SDimitry Andric bool m_raw; 2141e8d8bef9SDimitry Andric }; 2142e8d8bef9SDimitry Andric 2143e8d8bef9SDimitry Andric CommandObjectTraceDumpInstructions(CommandInterpreter &interpreter) 2144e8d8bef9SDimitry Andric : CommandObjectIterateOverThreads( 2145e8d8bef9SDimitry Andric interpreter, "thread trace dump instructions", 2146e8d8bef9SDimitry Andric "Dump the traced instructions for one or more threads. If no " 2147e8d8bef9SDimitry Andric "threads are specified, show the current thread. Use the " 2148e8d8bef9SDimitry Andric "thread-index \"all\" to see all threads.", 2149e8d8bef9SDimitry Andric nullptr, 2150e8d8bef9SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 2151e8d8bef9SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | 2152e8d8bef9SDimitry Andric eCommandProcessMustBeTraced), 2153e8d8bef9SDimitry Andric m_options(), m_create_repeat_command_just_invoked(false) {} 2154e8d8bef9SDimitry Andric 2155e8d8bef9SDimitry Andric ~CommandObjectTraceDumpInstructions() override = default; 2156e8d8bef9SDimitry Andric 2157e8d8bef9SDimitry Andric Options *GetOptions() override { return &m_options; } 2158e8d8bef9SDimitry Andric 2159e8d8bef9SDimitry Andric const char *GetRepeatCommand(Args ¤t_command_args, 2160e8d8bef9SDimitry Andric uint32_t index) override { 2161e8d8bef9SDimitry Andric current_command_args.GetCommandString(m_repeat_command); 2162e8d8bef9SDimitry Andric m_create_repeat_command_just_invoked = true; 2163e8d8bef9SDimitry Andric m_consecutive_repetitions = 0; 2164e8d8bef9SDimitry Andric return m_repeat_command.c_str(); 2165e8d8bef9SDimitry Andric } 2166e8d8bef9SDimitry Andric 2167e8d8bef9SDimitry Andric protected: 2168e8d8bef9SDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 2169e8d8bef9SDimitry Andric if (IsRepeatCommand()) 2170e8d8bef9SDimitry Andric m_consecutive_repetitions++; 2171e8d8bef9SDimitry Andric bool status = CommandObjectIterateOverThreads::DoExecute(args, result); 2172e8d8bef9SDimitry Andric 2173e8d8bef9SDimitry Andric m_create_repeat_command_just_invoked = false; 2174e8d8bef9SDimitry Andric return status; 2175e8d8bef9SDimitry Andric } 2176e8d8bef9SDimitry Andric 2177e8d8bef9SDimitry Andric bool IsRepeatCommand() { 2178e8d8bef9SDimitry Andric return !m_repeat_command.empty() && !m_create_repeat_command_just_invoked; 2179e8d8bef9SDimitry Andric } 2180e8d8bef9SDimitry Andric 2181e8d8bef9SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 2182e8d8bef9SDimitry Andric const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace(); 2183e8d8bef9SDimitry Andric ThreadSP thread_sp = 2184e8d8bef9SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 2185e8d8bef9SDimitry Andric 2186e8d8bef9SDimitry Andric size_t count = m_options.m_count; 2187e8d8bef9SDimitry Andric ssize_t position = m_options.m_position.getValueOr( 2188e8d8bef9SDimitry Andric trace_sp->GetCursorPosition(*thread_sp)) - 2189e8d8bef9SDimitry Andric m_consecutive_repetitions * count; 2190e8d8bef9SDimitry Andric if (position < 0) 2191e8d8bef9SDimitry Andric result.SetError("error: no more data"); 2192e8d8bef9SDimitry Andric else 2193e8d8bef9SDimitry Andric trace_sp->DumpTraceInstructions(*thread_sp, result.GetOutputStream(), 2194e8d8bef9SDimitry Andric count, position, m_options.m_raw); 2195e8d8bef9SDimitry Andric return true; 2196e8d8bef9SDimitry Andric } 2197e8d8bef9SDimitry Andric 2198e8d8bef9SDimitry Andric CommandOptions m_options; 2199e8d8bef9SDimitry Andric 2200e8d8bef9SDimitry Andric // Repeat command helpers 2201e8d8bef9SDimitry Andric std::string m_repeat_command; 2202e8d8bef9SDimitry Andric bool m_create_repeat_command_just_invoked; 2203e8d8bef9SDimitry Andric size_t m_consecutive_repetitions = 0; 2204e8d8bef9SDimitry Andric }; 2205e8d8bef9SDimitry Andric 2206e8d8bef9SDimitry Andric // CommandObjectMultiwordTraceDump 2207e8d8bef9SDimitry Andric class CommandObjectMultiwordTraceDump : public CommandObjectMultiword { 2208e8d8bef9SDimitry Andric public: 2209e8d8bef9SDimitry Andric CommandObjectMultiwordTraceDump(CommandInterpreter &interpreter) 2210e8d8bef9SDimitry Andric : CommandObjectMultiword( 2211e8d8bef9SDimitry Andric interpreter, "dump", 2212e8d8bef9SDimitry Andric "Commands for displaying trace information of the threads " 2213e8d8bef9SDimitry Andric "in the current process.", 2214e8d8bef9SDimitry Andric "thread trace dump <subcommand> [<subcommand objects>]") { 2215e8d8bef9SDimitry Andric LoadSubCommand( 2216e8d8bef9SDimitry Andric "instructions", 2217e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectTraceDumpInstructions(interpreter))); 2218e8d8bef9SDimitry Andric } 2219e8d8bef9SDimitry Andric ~CommandObjectMultiwordTraceDump() override = default; 2220e8d8bef9SDimitry Andric }; 2221e8d8bef9SDimitry Andric 2222e8d8bef9SDimitry Andric // CommandObjectMultiwordTrace 2223e8d8bef9SDimitry Andric class CommandObjectMultiwordTrace : public CommandObjectMultiword { 2224e8d8bef9SDimitry Andric public: 2225e8d8bef9SDimitry Andric CommandObjectMultiwordTrace(CommandInterpreter &interpreter) 2226e8d8bef9SDimitry Andric : CommandObjectMultiword( 2227e8d8bef9SDimitry Andric interpreter, "trace", 2228e8d8bef9SDimitry Andric "Commands for operating on traces of the threads in the current " 2229e8d8bef9SDimitry Andric "process.", 2230e8d8bef9SDimitry Andric "thread trace <subcommand> [<subcommand objects>]") { 2231e8d8bef9SDimitry Andric LoadSubCommand("dump", CommandObjectSP(new CommandObjectMultiwordTraceDump( 2232e8d8bef9SDimitry Andric interpreter))); 2233e8d8bef9SDimitry Andric LoadSubCommand("start", 2234e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectTraceStart(interpreter))); 2235e8d8bef9SDimitry Andric LoadSubCommand("stop", 2236e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectTraceStop(interpreter))); 2237e8d8bef9SDimitry Andric } 2238e8d8bef9SDimitry Andric 2239e8d8bef9SDimitry Andric ~CommandObjectMultiwordTrace() override = default; 2240e8d8bef9SDimitry Andric }; 2241e8d8bef9SDimitry Andric 2242*0b57cec5SDimitry Andric // CommandObjectMultiwordThread 2243*0b57cec5SDimitry Andric 2244*0b57cec5SDimitry Andric CommandObjectMultiwordThread::CommandObjectMultiwordThread( 2245*0b57cec5SDimitry Andric CommandInterpreter &interpreter) 2246480093f4SDimitry Andric : CommandObjectMultiword(interpreter, "thread", 2247480093f4SDimitry Andric "Commands for operating on " 2248*0b57cec5SDimitry Andric "one or more threads in " 2249*0b57cec5SDimitry Andric "the current process.", 2250*0b57cec5SDimitry Andric "thread <subcommand> [<subcommand-options>]") { 2251*0b57cec5SDimitry Andric LoadSubCommand("backtrace", CommandObjectSP(new CommandObjectThreadBacktrace( 2252*0b57cec5SDimitry Andric interpreter))); 2253*0b57cec5SDimitry Andric LoadSubCommand("continue", 2254*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadContinue(interpreter))); 2255*0b57cec5SDimitry Andric LoadSubCommand("list", 2256*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadList(interpreter))); 2257*0b57cec5SDimitry Andric LoadSubCommand("return", 2258*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadReturn(interpreter))); 2259*0b57cec5SDimitry Andric LoadSubCommand("jump", 2260*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadJump(interpreter))); 2261*0b57cec5SDimitry Andric LoadSubCommand("select", 2262*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadSelect(interpreter))); 2263*0b57cec5SDimitry Andric LoadSubCommand("until", 2264*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadUntil(interpreter))); 2265*0b57cec5SDimitry Andric LoadSubCommand("info", 2266*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadInfo(interpreter))); 2267480093f4SDimitry Andric LoadSubCommand("exception", CommandObjectSP(new CommandObjectThreadException( 2268480093f4SDimitry Andric interpreter))); 2269*0b57cec5SDimitry Andric LoadSubCommand("step-in", 2270*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2271*0b57cec5SDimitry Andric interpreter, "thread step-in", 2272*0b57cec5SDimitry Andric "Source level single step, stepping into calls. Defaults " 2273*0b57cec5SDimitry Andric "to current thread unless specified.", 2274*0b57cec5SDimitry Andric nullptr, eStepTypeInto, eStepScopeSource))); 2275*0b57cec5SDimitry Andric 2276*0b57cec5SDimitry Andric LoadSubCommand("step-out", 2277*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2278*0b57cec5SDimitry Andric interpreter, "thread step-out", 2279*0b57cec5SDimitry Andric "Finish executing the current stack frame and stop after " 2280*0b57cec5SDimitry Andric "returning. Defaults to current thread unless specified.", 2281*0b57cec5SDimitry Andric nullptr, eStepTypeOut, eStepScopeSource))); 2282*0b57cec5SDimitry Andric 2283*0b57cec5SDimitry Andric LoadSubCommand("step-over", 2284*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2285*0b57cec5SDimitry Andric interpreter, "thread step-over", 2286*0b57cec5SDimitry Andric "Source level single step, stepping over calls. Defaults " 2287*0b57cec5SDimitry Andric "to current thread unless specified.", 2288*0b57cec5SDimitry Andric nullptr, eStepTypeOver, eStepScopeSource))); 2289*0b57cec5SDimitry Andric 2290*0b57cec5SDimitry Andric LoadSubCommand("step-inst", 2291*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2292*0b57cec5SDimitry Andric interpreter, "thread step-inst", 2293*0b57cec5SDimitry Andric "Instruction level single step, stepping into calls. " 2294*0b57cec5SDimitry Andric "Defaults to current thread unless specified.", 2295*0b57cec5SDimitry Andric nullptr, eStepTypeTrace, eStepScopeInstruction))); 2296*0b57cec5SDimitry Andric 2297*0b57cec5SDimitry Andric LoadSubCommand("step-inst-over", 2298*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2299*0b57cec5SDimitry Andric interpreter, "thread step-inst-over", 2300*0b57cec5SDimitry Andric "Instruction level single step, stepping over calls. " 2301*0b57cec5SDimitry Andric "Defaults to current thread unless specified.", 2302*0b57cec5SDimitry Andric nullptr, eStepTypeTraceOver, eStepScopeInstruction))); 2303*0b57cec5SDimitry Andric 2304*0b57cec5SDimitry Andric LoadSubCommand( 2305*0b57cec5SDimitry Andric "step-scripted", 2306*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2307*0b57cec5SDimitry Andric interpreter, "thread step-scripted", 23089dba64beSDimitry Andric "Step as instructed by the script class passed in the -C option. " 23099dba64beSDimitry Andric "You can also specify a dictionary of key (-k) and value (-v) pairs " 23109dba64beSDimitry Andric "that will be used to populate an SBStructuredData Dictionary, which " 23119dba64beSDimitry Andric "will be passed to the constructor of the class implementing the " 23129dba64beSDimitry Andric "scripted step. See the Python Reference for more details.", 2313*0b57cec5SDimitry Andric nullptr, eStepTypeScripted, eStepScopeSource))); 2314*0b57cec5SDimitry Andric 2315*0b57cec5SDimitry Andric LoadSubCommand("plan", CommandObjectSP(new CommandObjectMultiwordThreadPlan( 2316*0b57cec5SDimitry Andric interpreter))); 2317e8d8bef9SDimitry Andric LoadSubCommand("trace", 2318e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectMultiwordTrace(interpreter))); 2319*0b57cec5SDimitry Andric } 2320*0b57cec5SDimitry Andric 2321*0b57cec5SDimitry Andric CommandObjectMultiwordThread::~CommandObjectMultiwordThread() = default; 2322