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 11fe6060f1SDimitry Andric #include <memory> 12e8d8bef9SDimitry Andric #include <sstream> 13e8d8bef9SDimitry Andric 14e8d8bef9SDimitry Andric #include "CommandObjectThreadUtil.h" 15fe6060f1SDimitry Andric #include "CommandObjectTrace.h" 16e8d8bef9SDimitry Andric #include "lldb/Core/PluginManager.h" 17*0b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h" 18*0b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h" 19*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h" 20*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h" 21*0b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h" 229dba64beSDimitry Andric #include "lldb/Interpreter/OptionGroupPythonClassWithDict.h" 23*0b57cec5SDimitry Andric #include "lldb/Interpreter/Options.h" 24*0b57cec5SDimitry Andric #include "lldb/Symbol/CompileUnit.h" 25*0b57cec5SDimitry Andric #include "lldb/Symbol/Function.h" 26*0b57cec5SDimitry Andric #include "lldb/Symbol/LineEntry.h" 27*0b57cec5SDimitry Andric #include "lldb/Symbol/LineTable.h" 28*0b57cec5SDimitry Andric #include "lldb/Target/Process.h" 29*0b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h" 30*0b57cec5SDimitry Andric #include "lldb/Target/SystemRuntime.h" 31*0b57cec5SDimitry Andric #include "lldb/Target/Target.h" 32*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h" 33*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlan.h" 34*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanStepInRange.h" 35e8d8bef9SDimitry Andric #include "lldb/Target/Trace.h" 36fe6060f1SDimitry Andric #include "lldb/Target/TraceInstructionDumper.h" 37*0b57cec5SDimitry Andric #include "lldb/Utility/State.h" 38*0b57cec5SDimitry Andric 39*0b57cec5SDimitry Andric using namespace lldb; 40*0b57cec5SDimitry Andric using namespace lldb_private; 41*0b57cec5SDimitry Andric 42*0b57cec5SDimitry Andric // CommandObjectThreadBacktrace 43*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_backtrace 44*0b57cec5SDimitry Andric #include "CommandOptions.inc" 45*0b57cec5SDimitry Andric 46*0b57cec5SDimitry Andric class CommandObjectThreadBacktrace : public CommandObjectIterateOverThreads { 47*0b57cec5SDimitry Andric public: 48*0b57cec5SDimitry Andric class CommandOptions : public Options { 49*0b57cec5SDimitry Andric public: 5004eeddc0SDimitry Andric CommandOptions() { 51*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 52*0b57cec5SDimitry Andric // () 53*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 54*0b57cec5SDimitry Andric } 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric ~CommandOptions() override = default; 57*0b57cec5SDimitry Andric 58*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 59*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 60*0b57cec5SDimitry Andric Status error; 61*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 62*0b57cec5SDimitry Andric 63*0b57cec5SDimitry Andric switch (short_option) { 64*0b57cec5SDimitry Andric case 'c': { 65*0b57cec5SDimitry Andric int32_t input_count = 0; 66*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_count)) { 67*0b57cec5SDimitry Andric m_count = UINT32_MAX; 68*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 69*0b57cec5SDimitry Andric "invalid integer value for option '%c'", short_option); 70*0b57cec5SDimitry Andric } else if (input_count < 0) 71*0b57cec5SDimitry Andric m_count = UINT32_MAX; 72*0b57cec5SDimitry Andric } break; 73*0b57cec5SDimitry Andric case 's': 74*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_start)) 75*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 76*0b57cec5SDimitry Andric "invalid integer value for option '%c'", short_option); 77*0b57cec5SDimitry Andric break; 78*0b57cec5SDimitry Andric case 'e': { 79*0b57cec5SDimitry Andric bool success; 80*0b57cec5SDimitry Andric m_extended_backtrace = 81*0b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, false, &success); 82*0b57cec5SDimitry Andric if (!success) 83*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 84*0b57cec5SDimitry Andric "invalid boolean value for option '%c'", short_option); 85*0b57cec5SDimitry Andric } break; 86*0b57cec5SDimitry Andric default: 879dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 88*0b57cec5SDimitry Andric } 89*0b57cec5SDimitry Andric return error; 90*0b57cec5SDimitry Andric } 91*0b57cec5SDimitry Andric 92*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 93*0b57cec5SDimitry Andric m_count = UINT32_MAX; 94*0b57cec5SDimitry Andric m_start = 0; 95*0b57cec5SDimitry Andric m_extended_backtrace = false; 96*0b57cec5SDimitry Andric } 97*0b57cec5SDimitry Andric 98*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 99*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_backtrace_options); 100*0b57cec5SDimitry Andric } 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 103*0b57cec5SDimitry Andric uint32_t m_count; 104*0b57cec5SDimitry Andric uint32_t m_start; 105*0b57cec5SDimitry Andric bool m_extended_backtrace; 106*0b57cec5SDimitry Andric }; 107*0b57cec5SDimitry Andric 108*0b57cec5SDimitry Andric CommandObjectThreadBacktrace(CommandInterpreter &interpreter) 109*0b57cec5SDimitry Andric : CommandObjectIterateOverThreads( 110*0b57cec5SDimitry Andric interpreter, "thread backtrace", 111*0b57cec5SDimitry Andric "Show thread call stacks. Defaults to the current thread, thread " 112*0b57cec5SDimitry Andric "indexes can be specified as arguments.\n" 113*0b57cec5SDimitry Andric "Use the thread-index \"all\" to see all threads.\n" 114*0b57cec5SDimitry Andric "Use the thread-index \"unique\" to see threads grouped by unique " 115*0b57cec5SDimitry Andric "call stacks.\n" 116*0b57cec5SDimitry Andric "Use 'settings set frame-format' to customize the printing of " 117*0b57cec5SDimitry Andric "frames in the backtrace and 'settings set thread-format' to " 118*0b57cec5SDimitry Andric "customize the thread header.", 119*0b57cec5SDimitry Andric nullptr, 120*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 121*0b57cec5SDimitry Andric eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | 12204eeddc0SDimitry Andric eCommandProcessMustBePaused) {} 123*0b57cec5SDimitry Andric 124*0b57cec5SDimitry Andric ~CommandObjectThreadBacktrace() override = default; 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 127*0b57cec5SDimitry Andric 128*0b57cec5SDimitry Andric protected: 129*0b57cec5SDimitry Andric void DoExtendedBacktrace(Thread *thread, CommandReturnObject &result) { 130*0b57cec5SDimitry Andric SystemRuntime *runtime = thread->GetProcess()->GetSystemRuntime(); 131*0b57cec5SDimitry Andric if (runtime) { 132*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 133*0b57cec5SDimitry Andric const std::vector<ConstString> &types = 134*0b57cec5SDimitry Andric runtime->GetExtendedBacktraceTypes(); 135*0b57cec5SDimitry Andric for (auto type : types) { 136*0b57cec5SDimitry Andric ThreadSP ext_thread_sp = runtime->GetExtendedBacktraceThread( 137*0b57cec5SDimitry Andric thread->shared_from_this(), type); 138*0b57cec5SDimitry Andric if (ext_thread_sp && ext_thread_sp->IsValid()) { 139*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 0; 140*0b57cec5SDimitry Andric const bool stop_format = false; 141*0b57cec5SDimitry Andric if (ext_thread_sp->GetStatus(strm, m_options.m_start, 142*0b57cec5SDimitry Andric m_options.m_count, 143480093f4SDimitry Andric num_frames_with_source, stop_format)) { 144*0b57cec5SDimitry Andric DoExtendedBacktrace(ext_thread_sp.get(), result); 145*0b57cec5SDimitry Andric } 146*0b57cec5SDimitry Andric } 147*0b57cec5SDimitry Andric } 148*0b57cec5SDimitry Andric } 149*0b57cec5SDimitry Andric } 150*0b57cec5SDimitry Andric 151*0b57cec5SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 152*0b57cec5SDimitry Andric ThreadSP thread_sp = 153*0b57cec5SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 154*0b57cec5SDimitry Andric if (!thread_sp) { 155*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 156*0b57cec5SDimitry Andric "thread disappeared while computing backtraces: 0x%" PRIx64 "\n", 157*0b57cec5SDimitry Andric tid); 158*0b57cec5SDimitry Andric return false; 159*0b57cec5SDimitry Andric } 160*0b57cec5SDimitry Andric 161*0b57cec5SDimitry Andric Thread *thread = thread_sp.get(); 162*0b57cec5SDimitry Andric 163*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 164*0b57cec5SDimitry Andric 165*0b57cec5SDimitry Andric // Only dump stack info if we processing unique stacks. 166*0b57cec5SDimitry Andric const bool only_stacks = m_unique_stacks; 167*0b57cec5SDimitry Andric 168*0b57cec5SDimitry Andric // Don't show source context when doing backtraces. 169*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 0; 170*0b57cec5SDimitry Andric const bool stop_format = true; 171*0b57cec5SDimitry Andric if (!thread->GetStatus(strm, m_options.m_start, m_options.m_count, 172*0b57cec5SDimitry Andric num_frames_with_source, stop_format, only_stacks)) { 173*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 174*0b57cec5SDimitry Andric "error displaying backtrace for thread: \"0x%4.4x\"\n", 175*0b57cec5SDimitry Andric thread->GetIndexID()); 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: 20504eeddc0SDimitry Andric ThreadStepScopeOptionGroup() { 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 ProcessSP process_sp = 295e8d8bef9SDimitry Andric execution_context ? execution_context->GetProcessSP() : ProcessSP(); 296e8d8bef9SDimitry Andric if (process_sp && process_sp->GetSteppingRunsAllThreads()) 297e8d8bef9SDimitry Andric m_run_mode = eAllThreads; 298*0b57cec5SDimitry Andric 299*0b57cec5SDimitry Andric m_avoid_regexp.clear(); 300*0b57cec5SDimitry Andric m_step_in_target.clear(); 301*0b57cec5SDimitry Andric m_step_count = 1; 302*0b57cec5SDimitry Andric m_end_line = LLDB_INVALID_LINE_NUMBER; 303*0b57cec5SDimitry Andric m_end_line_is_block_end = false; 304*0b57cec5SDimitry Andric } 305*0b57cec5SDimitry Andric 306*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 307*0b57cec5SDimitry Andric LazyBool m_step_in_avoid_no_debug; 308*0b57cec5SDimitry Andric LazyBool m_step_out_avoid_no_debug; 309*0b57cec5SDimitry Andric RunMode m_run_mode; 310*0b57cec5SDimitry Andric std::string m_avoid_regexp; 311*0b57cec5SDimitry Andric std::string m_step_in_target; 312*0b57cec5SDimitry Andric uint32_t m_step_count; 313*0b57cec5SDimitry Andric uint32_t m_end_line; 314*0b57cec5SDimitry Andric bool m_end_line_is_block_end; 315*0b57cec5SDimitry Andric }; 316*0b57cec5SDimitry Andric 3179dba64beSDimitry Andric class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed { 3189dba64beSDimitry Andric public: 319*0b57cec5SDimitry Andric CommandObjectThreadStepWithTypeAndScope(CommandInterpreter &interpreter, 320*0b57cec5SDimitry Andric const char *name, const char *help, 321*0b57cec5SDimitry Andric const char *syntax, 322*0b57cec5SDimitry Andric StepType step_type, 323*0b57cec5SDimitry Andric StepScope step_scope) 324*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, name, help, syntax, 325*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 326*0b57cec5SDimitry Andric eCommandTryTargetAPILock | 327*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 328*0b57cec5SDimitry Andric eCommandProcessMustBePaused), 32904eeddc0SDimitry Andric m_step_type(step_type), m_step_scope(step_scope), 330480093f4SDimitry Andric m_class_options("scripted step") { 331*0b57cec5SDimitry Andric CommandArgumentEntry arg; 332*0b57cec5SDimitry Andric CommandArgumentData thread_id_arg; 333*0b57cec5SDimitry Andric 334*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 335*0b57cec5SDimitry Andric thread_id_arg.arg_type = eArgTypeThreadID; 336*0b57cec5SDimitry Andric thread_id_arg.arg_repetition = eArgRepeatOptional; 337*0b57cec5SDimitry Andric 338*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 339*0b57cec5SDimitry Andric // argument entry. 340*0b57cec5SDimitry Andric arg.push_back(thread_id_arg); 341*0b57cec5SDimitry Andric 342*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 343*0b57cec5SDimitry Andric m_arguments.push_back(arg); 3449dba64beSDimitry Andric 3459dba64beSDimitry Andric if (step_type == eStepTypeScripted) { 346480093f4SDimitry Andric m_all_options.Append(&m_class_options, LLDB_OPT_SET_1 | LLDB_OPT_SET_2, 347480093f4SDimitry Andric LLDB_OPT_SET_1); 3489dba64beSDimitry Andric } 3499dba64beSDimitry Andric m_all_options.Append(&m_options); 3509dba64beSDimitry Andric m_all_options.Finalize(); 351*0b57cec5SDimitry Andric } 352*0b57cec5SDimitry Andric 353*0b57cec5SDimitry Andric ~CommandObjectThreadStepWithTypeAndScope() override = default; 354*0b57cec5SDimitry Andric 355e8d8bef9SDimitry Andric void 356e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 357e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 358e8d8bef9SDimitry Andric if (request.GetCursorIndex()) 359e8d8bef9SDimitry Andric return; 360e8d8bef9SDimitry Andric 361e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 362e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 363e8d8bef9SDimitry Andric request, nullptr); 364e8d8bef9SDimitry Andric } 365e8d8bef9SDimitry Andric 366480093f4SDimitry Andric Options *GetOptions() override { return &m_all_options; } 367*0b57cec5SDimitry Andric 368*0b57cec5SDimitry Andric protected: 369*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 370*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 371*0b57cec5SDimitry Andric bool synchronous_execution = m_interpreter.GetSynchronous(); 372*0b57cec5SDimitry Andric 373*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 374*0b57cec5SDimitry Andric Thread *thread = nullptr; 375*0b57cec5SDimitry Andric 376*0b57cec5SDimitry Andric if (command.GetArgumentCount() == 0) { 377*0b57cec5SDimitry Andric thread = GetDefaultThread(); 378*0b57cec5SDimitry Andric 379*0b57cec5SDimitry Andric if (thread == nullptr) { 380*0b57cec5SDimitry Andric result.AppendError("no selected thread in process"); 381*0b57cec5SDimitry Andric return false; 382*0b57cec5SDimitry Andric } 383*0b57cec5SDimitry Andric } else { 384*0b57cec5SDimitry Andric const char *thread_idx_cstr = command.GetArgumentAtIndex(0); 3855ffd83dbSDimitry Andric uint32_t step_thread_idx; 3865ffd83dbSDimitry Andric 3875ffd83dbSDimitry Andric if (!llvm::to_integer(thread_idx_cstr, step_thread_idx)) { 388*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid thread index '%s'.\n", 389*0b57cec5SDimitry Andric thread_idx_cstr); 390*0b57cec5SDimitry Andric return false; 391*0b57cec5SDimitry Andric } 392*0b57cec5SDimitry Andric thread = 393*0b57cec5SDimitry Andric process->GetThreadList().FindThreadByIndexID(step_thread_idx).get(); 394*0b57cec5SDimitry Andric if (thread == nullptr) { 395*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 396*0b57cec5SDimitry Andric "Thread index %u is out of range (valid values are 0 - %u).\n", 397*0b57cec5SDimitry Andric step_thread_idx, num_threads); 398*0b57cec5SDimitry Andric return false; 399*0b57cec5SDimitry Andric } 400*0b57cec5SDimitry Andric } 401*0b57cec5SDimitry Andric 402*0b57cec5SDimitry Andric if (m_step_type == eStepTypeScripted) { 403480093f4SDimitry Andric if (m_class_options.GetName().empty()) { 404*0b57cec5SDimitry Andric result.AppendErrorWithFormat("empty class name for scripted step."); 405*0b57cec5SDimitry Andric return false; 406*0b57cec5SDimitry Andric } else if (!GetDebugger().GetScriptInterpreter()->CheckObjectExists( 407480093f4SDimitry Andric m_class_options.GetName().c_str())) { 408*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 409*0b57cec5SDimitry Andric "class for scripted step: \"%s\" does not exist.", 410480093f4SDimitry Andric m_class_options.GetName().c_str()); 411*0b57cec5SDimitry Andric return false; 412*0b57cec5SDimitry Andric } 413*0b57cec5SDimitry Andric } 414*0b57cec5SDimitry Andric 415*0b57cec5SDimitry Andric if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER && 416*0b57cec5SDimitry Andric m_step_type != eStepTypeInto) { 417*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 418*0b57cec5SDimitry Andric "end line option is only valid for step into"); 419*0b57cec5SDimitry Andric return false; 420*0b57cec5SDimitry Andric } 421*0b57cec5SDimitry Andric 422*0b57cec5SDimitry Andric const bool abort_other_plans = false; 423*0b57cec5SDimitry Andric const lldb::RunMode stop_other_threads = m_options.m_run_mode; 424*0b57cec5SDimitry Andric 425*0b57cec5SDimitry Andric // This is a bit unfortunate, but not all the commands in this command 426*0b57cec5SDimitry Andric // object support only while stepping, so I use the bool for them. 427*0b57cec5SDimitry Andric bool bool_stop_other_threads; 428*0b57cec5SDimitry Andric if (m_options.m_run_mode == eAllThreads) 429*0b57cec5SDimitry Andric bool_stop_other_threads = false; 430*0b57cec5SDimitry Andric else if (m_options.m_run_mode == eOnlyDuringStepping) 431e8d8bef9SDimitry Andric bool_stop_other_threads = (m_step_type != eStepTypeOut); 432*0b57cec5SDimitry Andric else 433*0b57cec5SDimitry Andric bool_stop_other_threads = true; 434*0b57cec5SDimitry Andric 435*0b57cec5SDimitry Andric ThreadPlanSP new_plan_sp; 436*0b57cec5SDimitry Andric Status new_plan_status; 437*0b57cec5SDimitry Andric 438*0b57cec5SDimitry Andric if (m_step_type == eStepTypeInto) { 439*0b57cec5SDimitry Andric StackFrame *frame = thread->GetStackFrameAtIndex(0).get(); 440*0b57cec5SDimitry Andric assert(frame != nullptr); 441*0b57cec5SDimitry Andric 442*0b57cec5SDimitry Andric if (frame->HasDebugInformation()) { 443*0b57cec5SDimitry Andric AddressRange range; 444*0b57cec5SDimitry Andric SymbolContext sc = frame->GetSymbolContext(eSymbolContextEverything); 445*0b57cec5SDimitry Andric if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER) { 446*0b57cec5SDimitry Andric Status error; 447*0b57cec5SDimitry Andric if (!sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range, 448*0b57cec5SDimitry Andric error)) { 449*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid end-line option: %s.", 450*0b57cec5SDimitry Andric error.AsCString()); 451*0b57cec5SDimitry Andric return false; 452*0b57cec5SDimitry Andric } 453*0b57cec5SDimitry Andric } else if (m_options.m_end_line_is_block_end) { 454*0b57cec5SDimitry Andric Status error; 455*0b57cec5SDimitry Andric Block *block = frame->GetSymbolContext(eSymbolContextBlock).block; 456*0b57cec5SDimitry Andric if (!block) { 457*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Could not find the current block."); 458*0b57cec5SDimitry Andric return false; 459*0b57cec5SDimitry Andric } 460*0b57cec5SDimitry Andric 461*0b57cec5SDimitry Andric AddressRange block_range; 462*0b57cec5SDimitry Andric Address pc_address = frame->GetFrameCodeAddress(); 463*0b57cec5SDimitry Andric block->GetRangeContainingAddress(pc_address, block_range); 464*0b57cec5SDimitry Andric if (!block_range.GetBaseAddress().IsValid()) { 465*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 466*0b57cec5SDimitry Andric "Could not find the current block address."); 467*0b57cec5SDimitry Andric return false; 468*0b57cec5SDimitry Andric } 469*0b57cec5SDimitry Andric lldb::addr_t pc_offset_in_block = 470*0b57cec5SDimitry Andric pc_address.GetFileAddress() - 471*0b57cec5SDimitry Andric block_range.GetBaseAddress().GetFileAddress(); 472*0b57cec5SDimitry Andric lldb::addr_t range_length = 473*0b57cec5SDimitry Andric block_range.GetByteSize() - pc_offset_in_block; 474*0b57cec5SDimitry Andric range = AddressRange(pc_address, range_length); 475*0b57cec5SDimitry Andric } else { 476*0b57cec5SDimitry Andric range = sc.line_entry.range; 477*0b57cec5SDimitry Andric } 478*0b57cec5SDimitry Andric 479*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepInRange( 480*0b57cec5SDimitry Andric abort_other_plans, range, 481*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextEverything), 482*0b57cec5SDimitry Andric m_options.m_step_in_target.c_str(), stop_other_threads, 483*0b57cec5SDimitry Andric new_plan_status, m_options.m_step_in_avoid_no_debug, 484*0b57cec5SDimitry Andric m_options.m_step_out_avoid_no_debug); 485*0b57cec5SDimitry Andric 486*0b57cec5SDimitry Andric if (new_plan_sp && !m_options.m_avoid_regexp.empty()) { 487*0b57cec5SDimitry Andric ThreadPlanStepInRange *step_in_range_plan = 488*0b57cec5SDimitry Andric static_cast<ThreadPlanStepInRange *>(new_plan_sp.get()); 489*0b57cec5SDimitry Andric step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str()); 490*0b57cec5SDimitry Andric } 491*0b57cec5SDimitry Andric } else 492*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 493*0b57cec5SDimitry Andric false, abort_other_plans, bool_stop_other_threads, new_plan_status); 494*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeOver) { 495*0b57cec5SDimitry Andric StackFrame *frame = thread->GetStackFrameAtIndex(0).get(); 496*0b57cec5SDimitry Andric 497*0b57cec5SDimitry Andric if (frame->HasDebugInformation()) 498*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepOverRange( 499*0b57cec5SDimitry Andric abort_other_plans, 500*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextEverything).line_entry, 501*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextEverything), 502*0b57cec5SDimitry Andric stop_other_threads, new_plan_status, 503*0b57cec5SDimitry Andric m_options.m_step_out_avoid_no_debug); 504*0b57cec5SDimitry Andric else 505*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 506*0b57cec5SDimitry Andric true, abort_other_plans, bool_stop_other_threads, new_plan_status); 507*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeTrace) { 508*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 509*0b57cec5SDimitry Andric false, abort_other_plans, bool_stop_other_threads, new_plan_status); 510*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeTraceOver) { 511*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 512*0b57cec5SDimitry Andric true, abort_other_plans, bool_stop_other_threads, new_plan_status); 513*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeOut) { 514*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepOut( 515*0b57cec5SDimitry Andric abort_other_plans, nullptr, false, bool_stop_other_threads, eVoteYes, 516*0b57cec5SDimitry Andric eVoteNoOpinion, thread->GetSelectedFrameIndex(), new_plan_status, 517*0b57cec5SDimitry Andric m_options.m_step_out_avoid_no_debug); 518*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeScripted) { 519*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepScripted( 520480093f4SDimitry Andric abort_other_plans, m_class_options.GetName().c_str(), 521480093f4SDimitry Andric m_class_options.GetStructuredData(), bool_stop_other_threads, 522480093f4SDimitry Andric new_plan_status); 523*0b57cec5SDimitry Andric } else { 524*0b57cec5SDimitry Andric result.AppendError("step type is not supported"); 525*0b57cec5SDimitry Andric return false; 526*0b57cec5SDimitry Andric } 527*0b57cec5SDimitry Andric 528349cc55cSDimitry Andric // If we got a new plan, then set it to be a controlling plan (User level 529349cc55cSDimitry Andric // Plans should be controlling plans so that they can be interruptible). 530349cc55cSDimitry Andric // Then resume the process. 531*0b57cec5SDimitry Andric 532*0b57cec5SDimitry Andric if (new_plan_sp) { 533349cc55cSDimitry Andric new_plan_sp->SetIsControllingPlan(true); 534*0b57cec5SDimitry Andric new_plan_sp->SetOkayToDiscard(false); 535*0b57cec5SDimitry Andric 536*0b57cec5SDimitry Andric if (m_options.m_step_count > 1) { 537*0b57cec5SDimitry Andric if (!new_plan_sp->SetIterationCount(m_options.m_step_count)) { 538*0b57cec5SDimitry Andric result.AppendWarning( 539*0b57cec5SDimitry Andric "step operation does not support iteration count."); 540*0b57cec5SDimitry Andric } 541*0b57cec5SDimitry Andric } 542*0b57cec5SDimitry Andric 543*0b57cec5SDimitry Andric process->GetThreadList().SetSelectedThreadByID(thread->GetID()); 544*0b57cec5SDimitry Andric 545*0b57cec5SDimitry Andric const uint32_t iohandler_id = process->GetIOHandlerID(); 546*0b57cec5SDimitry Andric 547*0b57cec5SDimitry Andric StreamString stream; 548*0b57cec5SDimitry Andric Status error; 549*0b57cec5SDimitry Andric if (synchronous_execution) 550*0b57cec5SDimitry Andric error = process->ResumeSynchronous(&stream); 551*0b57cec5SDimitry Andric else 552*0b57cec5SDimitry Andric error = process->Resume(); 553*0b57cec5SDimitry Andric 554*0b57cec5SDimitry Andric if (!error.Success()) { 555*0b57cec5SDimitry Andric result.AppendMessage(error.AsCString()); 556*0b57cec5SDimitry Andric return false; 557*0b57cec5SDimitry Andric } 558*0b57cec5SDimitry Andric 559*0b57cec5SDimitry Andric // There is a race condition where this thread will return up the call 560*0b57cec5SDimitry Andric // stack to the main command handler and show an (lldb) prompt before 561*0b57cec5SDimitry Andric // HandlePrivateEvent (from PrivateStateThread) has a chance to call 562*0b57cec5SDimitry Andric // PushProcessIOHandler(). 563*0b57cec5SDimitry Andric process->SyncIOHandler(iohandler_id, std::chrono::seconds(2)); 564*0b57cec5SDimitry Andric 565*0b57cec5SDimitry Andric if (synchronous_execution) { 566*0b57cec5SDimitry Andric // If any state changed events had anything to say, add that to the 567*0b57cec5SDimitry Andric // result 568*0b57cec5SDimitry Andric if (stream.GetSize() > 0) 569*0b57cec5SDimitry Andric result.AppendMessage(stream.GetString()); 570*0b57cec5SDimitry Andric 571*0b57cec5SDimitry Andric process->GetThreadList().SetSelectedThreadByID(thread->GetID()); 572*0b57cec5SDimitry Andric result.SetDidChangeProcessState(true); 573*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 574*0b57cec5SDimitry Andric } else { 575*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessContinuingNoResult); 576*0b57cec5SDimitry Andric } 577*0b57cec5SDimitry Andric } else { 578*0b57cec5SDimitry Andric result.SetError(new_plan_status); 579*0b57cec5SDimitry Andric } 580*0b57cec5SDimitry Andric return result.Succeeded(); 581*0b57cec5SDimitry Andric } 582*0b57cec5SDimitry Andric 583*0b57cec5SDimitry Andric StepType m_step_type; 584*0b57cec5SDimitry Andric StepScope m_step_scope; 5859dba64beSDimitry Andric ThreadStepScopeOptionGroup m_options; 5869dba64beSDimitry Andric OptionGroupPythonClassWithDict m_class_options; 5879dba64beSDimitry Andric OptionGroupOptions m_all_options; 588*0b57cec5SDimitry Andric }; 589*0b57cec5SDimitry Andric 590*0b57cec5SDimitry Andric // CommandObjectThreadContinue 591*0b57cec5SDimitry Andric 592*0b57cec5SDimitry Andric class CommandObjectThreadContinue : public CommandObjectParsed { 593*0b57cec5SDimitry Andric public: 594*0b57cec5SDimitry Andric CommandObjectThreadContinue(CommandInterpreter &interpreter) 595*0b57cec5SDimitry Andric : CommandObjectParsed( 596*0b57cec5SDimitry Andric interpreter, "thread continue", 597*0b57cec5SDimitry Andric "Continue execution of the current target process. One " 598*0b57cec5SDimitry Andric "or more threads may be specified, by default all " 599*0b57cec5SDimitry Andric "threads continue.", 600*0b57cec5SDimitry Andric nullptr, 601*0b57cec5SDimitry Andric eCommandRequiresThread | eCommandTryTargetAPILock | 602*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) { 603*0b57cec5SDimitry Andric CommandArgumentEntry arg; 604*0b57cec5SDimitry Andric CommandArgumentData thread_idx_arg; 605*0b57cec5SDimitry Andric 606*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 607*0b57cec5SDimitry Andric thread_idx_arg.arg_type = eArgTypeThreadIndex; 608*0b57cec5SDimitry Andric thread_idx_arg.arg_repetition = eArgRepeatPlus; 609*0b57cec5SDimitry Andric 610*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 611*0b57cec5SDimitry Andric // argument entry. 612*0b57cec5SDimitry Andric arg.push_back(thread_idx_arg); 613*0b57cec5SDimitry Andric 614*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 615*0b57cec5SDimitry Andric m_arguments.push_back(arg); 616*0b57cec5SDimitry Andric } 617*0b57cec5SDimitry Andric 618*0b57cec5SDimitry Andric ~CommandObjectThreadContinue() override = default; 619*0b57cec5SDimitry Andric 620e8d8bef9SDimitry Andric void 621e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 622e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 623e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 624e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 625e8d8bef9SDimitry Andric request, nullptr); 626e8d8bef9SDimitry Andric } 627e8d8bef9SDimitry Andric 628*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 629*0b57cec5SDimitry Andric bool synchronous_execution = m_interpreter.GetSynchronous(); 630*0b57cec5SDimitry Andric 631*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 632*0b57cec5SDimitry Andric if (process == nullptr) { 633*0b57cec5SDimitry Andric result.AppendError("no process exists. Cannot continue"); 634*0b57cec5SDimitry Andric return false; 635*0b57cec5SDimitry Andric } 636*0b57cec5SDimitry Andric 637*0b57cec5SDimitry Andric StateType state = process->GetState(); 638*0b57cec5SDimitry Andric if ((state == eStateCrashed) || (state == eStateStopped) || 639*0b57cec5SDimitry Andric (state == eStateSuspended)) { 640*0b57cec5SDimitry Andric const size_t argc = command.GetArgumentCount(); 641*0b57cec5SDimitry Andric if (argc > 0) { 642*0b57cec5SDimitry Andric // These two lines appear at the beginning of both blocks in this 643*0b57cec5SDimitry Andric // if..else, but that is because we need to release the lock before 644*0b57cec5SDimitry Andric // calling process->Resume below. 645*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 646*0b57cec5SDimitry Andric process->GetThreadList().GetMutex()); 647*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 648*0b57cec5SDimitry Andric std::vector<Thread *> resume_threads; 649*0b57cec5SDimitry Andric for (auto &entry : command.entries()) { 650*0b57cec5SDimitry Andric uint32_t thread_idx; 6519dba64beSDimitry Andric if (entry.ref().getAsInteger(0, thread_idx)) { 652*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 653*0b57cec5SDimitry Andric "invalid thread index argument: \"%s\".\n", entry.c_str()); 654*0b57cec5SDimitry Andric return false; 655*0b57cec5SDimitry Andric } 656*0b57cec5SDimitry Andric Thread *thread = 657*0b57cec5SDimitry Andric process->GetThreadList().FindThreadByIndexID(thread_idx).get(); 658*0b57cec5SDimitry Andric 659*0b57cec5SDimitry Andric if (thread) { 660*0b57cec5SDimitry Andric resume_threads.push_back(thread); 661*0b57cec5SDimitry Andric } else { 662*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid thread index %u.\n", 663*0b57cec5SDimitry Andric thread_idx); 664*0b57cec5SDimitry Andric return false; 665*0b57cec5SDimitry Andric } 666*0b57cec5SDimitry Andric } 667*0b57cec5SDimitry Andric 668*0b57cec5SDimitry Andric if (resume_threads.empty()) { 669*0b57cec5SDimitry Andric result.AppendError("no valid thread indexes were specified"); 670*0b57cec5SDimitry Andric return false; 671*0b57cec5SDimitry Andric } else { 672*0b57cec5SDimitry Andric if (resume_threads.size() == 1) 673*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Resuming thread: "); 674*0b57cec5SDimitry Andric else 675*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Resuming threads: "); 676*0b57cec5SDimitry Andric 677*0b57cec5SDimitry Andric for (uint32_t idx = 0; idx < num_threads; ++idx) { 678*0b57cec5SDimitry Andric Thread *thread = 679*0b57cec5SDimitry Andric process->GetThreadList().GetThreadAtIndex(idx).get(); 680*0b57cec5SDimitry Andric std::vector<Thread *>::iterator this_thread_pos = 681*0b57cec5SDimitry Andric find(resume_threads.begin(), resume_threads.end(), thread); 682*0b57cec5SDimitry Andric 683*0b57cec5SDimitry Andric if (this_thread_pos != resume_threads.end()) { 684*0b57cec5SDimitry Andric resume_threads.erase(this_thread_pos); 685*0b57cec5SDimitry Andric if (!resume_threads.empty()) 686*0b57cec5SDimitry Andric result.AppendMessageWithFormat("%u, ", thread->GetIndexID()); 687*0b57cec5SDimitry Andric else 688*0b57cec5SDimitry Andric result.AppendMessageWithFormat("%u ", thread->GetIndexID()); 689*0b57cec5SDimitry Andric 690*0b57cec5SDimitry Andric const bool override_suspend = true; 691*0b57cec5SDimitry Andric thread->SetResumeState(eStateRunning, override_suspend); 692*0b57cec5SDimitry Andric } else { 693*0b57cec5SDimitry Andric thread->SetResumeState(eStateSuspended); 694*0b57cec5SDimitry Andric } 695*0b57cec5SDimitry Andric } 696*0b57cec5SDimitry Andric result.AppendMessageWithFormat("in process %" PRIu64 "\n", 697*0b57cec5SDimitry Andric process->GetID()); 698*0b57cec5SDimitry Andric } 699*0b57cec5SDimitry Andric } else { 700*0b57cec5SDimitry Andric // These two lines appear at the beginning of both blocks in this 701*0b57cec5SDimitry Andric // if..else, but that is because we need to release the lock before 702*0b57cec5SDimitry Andric // calling process->Resume below. 703*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 704*0b57cec5SDimitry Andric process->GetThreadList().GetMutex()); 705*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 706*0b57cec5SDimitry Andric Thread *current_thread = GetDefaultThread(); 707*0b57cec5SDimitry Andric if (current_thread == nullptr) { 708*0b57cec5SDimitry Andric result.AppendError("the process doesn't have a current thread"); 709*0b57cec5SDimitry Andric return false; 710*0b57cec5SDimitry Andric } 711*0b57cec5SDimitry Andric // Set the actions that the threads should each take when resuming 712*0b57cec5SDimitry Andric for (uint32_t idx = 0; idx < num_threads; ++idx) { 713*0b57cec5SDimitry Andric Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get(); 714*0b57cec5SDimitry Andric if (thread == current_thread) { 715*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Resuming thread 0x%4.4" PRIx64 716*0b57cec5SDimitry Andric " in process %" PRIu64 "\n", 717*0b57cec5SDimitry Andric thread->GetID(), process->GetID()); 718*0b57cec5SDimitry Andric const bool override_suspend = true; 719*0b57cec5SDimitry Andric thread->SetResumeState(eStateRunning, override_suspend); 720*0b57cec5SDimitry Andric } else { 721*0b57cec5SDimitry Andric thread->SetResumeState(eStateSuspended); 722*0b57cec5SDimitry Andric } 723*0b57cec5SDimitry Andric } 724*0b57cec5SDimitry Andric } 725*0b57cec5SDimitry Andric 726*0b57cec5SDimitry Andric StreamString stream; 727*0b57cec5SDimitry Andric Status error; 728*0b57cec5SDimitry Andric if (synchronous_execution) 729*0b57cec5SDimitry Andric error = process->ResumeSynchronous(&stream); 730*0b57cec5SDimitry Andric else 731*0b57cec5SDimitry Andric error = process->Resume(); 732*0b57cec5SDimitry Andric 733*0b57cec5SDimitry Andric // We should not be holding the thread list lock when we do this. 734*0b57cec5SDimitry Andric if (error.Success()) { 735*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n", 736*0b57cec5SDimitry Andric process->GetID()); 737*0b57cec5SDimitry Andric if (synchronous_execution) { 738*0b57cec5SDimitry Andric // If any state changed events had anything to say, add that to the 739*0b57cec5SDimitry Andric // result 740*0b57cec5SDimitry Andric if (stream.GetSize() > 0) 741*0b57cec5SDimitry Andric result.AppendMessage(stream.GetString()); 742*0b57cec5SDimitry Andric 743*0b57cec5SDimitry Andric result.SetDidChangeProcessState(true); 744*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 745*0b57cec5SDimitry Andric } else { 746*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessContinuingNoResult); 747*0b57cec5SDimitry Andric } 748*0b57cec5SDimitry Andric } else { 749*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to resume process: %s\n", 750*0b57cec5SDimitry Andric error.AsCString()); 751*0b57cec5SDimitry Andric } 752*0b57cec5SDimitry Andric } else { 753*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 754*0b57cec5SDimitry Andric "Process cannot be continued from its current state (%s).\n", 755*0b57cec5SDimitry Andric StateAsCString(state)); 756*0b57cec5SDimitry Andric } 757*0b57cec5SDimitry Andric 758*0b57cec5SDimitry Andric return result.Succeeded(); 759*0b57cec5SDimitry Andric } 760*0b57cec5SDimitry Andric }; 761*0b57cec5SDimitry Andric 762*0b57cec5SDimitry Andric // CommandObjectThreadUntil 763*0b57cec5SDimitry Andric 764*0b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_duo_running_mode[] = { 765*0b57cec5SDimitry Andric {eOnlyThisThread, "this-thread", "Run only this thread"}, 766*0b57cec5SDimitry Andric {eAllThreads, "all-threads", "Run all threads"}}; 767*0b57cec5SDimitry Andric 768*0b57cec5SDimitry Andric static constexpr OptionEnumValues DuoRunningModes() { 769*0b57cec5SDimitry Andric return OptionEnumValues(g_duo_running_mode); 770*0b57cec5SDimitry Andric } 771*0b57cec5SDimitry Andric 772*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_until 773*0b57cec5SDimitry Andric #include "CommandOptions.inc" 774*0b57cec5SDimitry Andric 775*0b57cec5SDimitry Andric class CommandObjectThreadUntil : public CommandObjectParsed { 776*0b57cec5SDimitry Andric public: 777*0b57cec5SDimitry Andric class CommandOptions : public Options { 778*0b57cec5SDimitry Andric public: 779fe6060f1SDimitry Andric uint32_t m_thread_idx = LLDB_INVALID_THREAD_ID; 780fe6060f1SDimitry Andric uint32_t m_frame_idx = LLDB_INVALID_FRAME_ID; 781*0b57cec5SDimitry Andric 78204eeddc0SDimitry Andric CommandOptions() { 783*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 784*0b57cec5SDimitry Andric // () 785*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 786*0b57cec5SDimitry Andric } 787*0b57cec5SDimitry Andric 788*0b57cec5SDimitry Andric ~CommandOptions() override = default; 789*0b57cec5SDimitry Andric 790*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 791*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 792*0b57cec5SDimitry Andric Status error; 793*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 794*0b57cec5SDimitry Andric 795*0b57cec5SDimitry Andric switch (short_option) { 796*0b57cec5SDimitry Andric case 'a': { 797*0b57cec5SDimitry Andric lldb::addr_t tmp_addr = OptionArgParser::ToAddress( 798*0b57cec5SDimitry Andric execution_context, option_arg, LLDB_INVALID_ADDRESS, &error); 799*0b57cec5SDimitry Andric if (error.Success()) 800*0b57cec5SDimitry Andric m_until_addrs.push_back(tmp_addr); 801*0b57cec5SDimitry Andric } break; 802*0b57cec5SDimitry Andric case 't': 803*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_thread_idx)) { 804*0b57cec5SDimitry Andric m_thread_idx = LLDB_INVALID_INDEX32; 805*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid thread index '%s'", 806*0b57cec5SDimitry Andric option_arg.str().c_str()); 807*0b57cec5SDimitry Andric } 808*0b57cec5SDimitry Andric break; 809*0b57cec5SDimitry Andric case 'f': 810*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_frame_idx)) { 811*0b57cec5SDimitry Andric m_frame_idx = LLDB_INVALID_FRAME_ID; 812*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid frame index '%s'", 813*0b57cec5SDimitry Andric option_arg.str().c_str()); 814*0b57cec5SDimitry Andric } 815*0b57cec5SDimitry Andric break; 816*0b57cec5SDimitry Andric case 'm': { 817*0b57cec5SDimitry Andric auto enum_values = GetDefinitions()[option_idx].enum_values; 818*0b57cec5SDimitry Andric lldb::RunMode run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum( 819*0b57cec5SDimitry Andric option_arg, enum_values, eOnlyDuringStepping, error); 820*0b57cec5SDimitry Andric 821*0b57cec5SDimitry Andric if (error.Success()) { 822*0b57cec5SDimitry Andric if (run_mode == eAllThreads) 823*0b57cec5SDimitry Andric m_stop_others = false; 824*0b57cec5SDimitry Andric else 825*0b57cec5SDimitry Andric m_stop_others = true; 826*0b57cec5SDimitry Andric } 827*0b57cec5SDimitry Andric } break; 828*0b57cec5SDimitry Andric default: 8299dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 830*0b57cec5SDimitry Andric } 831*0b57cec5SDimitry Andric return error; 832*0b57cec5SDimitry Andric } 833*0b57cec5SDimitry Andric 834*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 835*0b57cec5SDimitry Andric m_thread_idx = LLDB_INVALID_THREAD_ID; 836*0b57cec5SDimitry Andric m_frame_idx = 0; 837*0b57cec5SDimitry Andric m_stop_others = false; 838*0b57cec5SDimitry Andric m_until_addrs.clear(); 839*0b57cec5SDimitry Andric } 840*0b57cec5SDimitry Andric 841*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 842*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_until_options); 843*0b57cec5SDimitry Andric } 844*0b57cec5SDimitry Andric 845*0b57cec5SDimitry Andric uint32_t m_step_thread_idx; 846*0b57cec5SDimitry Andric bool m_stop_others; 847*0b57cec5SDimitry Andric std::vector<lldb::addr_t> m_until_addrs; 848*0b57cec5SDimitry Andric 849*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 850*0b57cec5SDimitry Andric }; 851*0b57cec5SDimitry Andric 852*0b57cec5SDimitry Andric CommandObjectThreadUntil(CommandInterpreter &interpreter) 853*0b57cec5SDimitry Andric : CommandObjectParsed( 854*0b57cec5SDimitry Andric interpreter, "thread until", 855*0b57cec5SDimitry Andric "Continue until a line number or address is reached by the " 856*0b57cec5SDimitry Andric "current or specified thread. Stops when returning from " 857*0b57cec5SDimitry Andric "the current function as a safety measure. " 858480093f4SDimitry Andric "The target line number(s) are given as arguments, and if more " 859480093f4SDimitry Andric "than one" 860*0b57cec5SDimitry Andric " is provided, stepping will stop when the first one is hit.", 861*0b57cec5SDimitry Andric nullptr, 862*0b57cec5SDimitry Andric eCommandRequiresThread | eCommandTryTargetAPILock | 86304eeddc0SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) { 864*0b57cec5SDimitry Andric CommandArgumentEntry arg; 865*0b57cec5SDimitry Andric CommandArgumentData line_num_arg; 866*0b57cec5SDimitry Andric 867*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 868*0b57cec5SDimitry Andric line_num_arg.arg_type = eArgTypeLineNum; 869*0b57cec5SDimitry Andric line_num_arg.arg_repetition = eArgRepeatPlain; 870*0b57cec5SDimitry Andric 871*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 872*0b57cec5SDimitry Andric // argument entry. 873*0b57cec5SDimitry Andric arg.push_back(line_num_arg); 874*0b57cec5SDimitry Andric 875*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 876*0b57cec5SDimitry Andric m_arguments.push_back(arg); 877*0b57cec5SDimitry Andric } 878*0b57cec5SDimitry Andric 879*0b57cec5SDimitry Andric ~CommandObjectThreadUntil() override = default; 880*0b57cec5SDimitry Andric 881*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 882*0b57cec5SDimitry Andric 883*0b57cec5SDimitry Andric protected: 884*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 885*0b57cec5SDimitry Andric bool synchronous_execution = m_interpreter.GetSynchronous(); 886*0b57cec5SDimitry Andric 8879dba64beSDimitry Andric Target *target = &GetSelectedTarget(); 888*0b57cec5SDimitry Andric 889*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 890*0b57cec5SDimitry Andric if (process == nullptr) { 891*0b57cec5SDimitry Andric result.AppendError("need a valid process to step"); 892*0b57cec5SDimitry Andric } else { 893*0b57cec5SDimitry Andric Thread *thread = nullptr; 894*0b57cec5SDimitry Andric std::vector<uint32_t> line_numbers; 895*0b57cec5SDimitry Andric 896*0b57cec5SDimitry Andric if (command.GetArgumentCount() >= 1) { 897*0b57cec5SDimitry Andric size_t num_args = command.GetArgumentCount(); 898*0b57cec5SDimitry Andric for (size_t i = 0; i < num_args; i++) { 899*0b57cec5SDimitry Andric uint32_t line_number; 9005ffd83dbSDimitry Andric if (!llvm::to_integer(command.GetArgumentAtIndex(i), line_number)) { 901*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid line number: '%s'.\n", 902*0b57cec5SDimitry Andric command.GetArgumentAtIndex(i)); 903*0b57cec5SDimitry Andric return false; 904*0b57cec5SDimitry Andric } else 905*0b57cec5SDimitry Andric line_numbers.push_back(line_number); 906*0b57cec5SDimitry Andric } 907*0b57cec5SDimitry Andric } else if (m_options.m_until_addrs.empty()) { 908*0b57cec5SDimitry Andric result.AppendErrorWithFormat("No line number or address provided:\n%s", 909*0b57cec5SDimitry Andric GetSyntax().str().c_str()); 910*0b57cec5SDimitry Andric return false; 911*0b57cec5SDimitry Andric } 912*0b57cec5SDimitry Andric 913*0b57cec5SDimitry Andric if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID) { 914*0b57cec5SDimitry Andric thread = GetDefaultThread(); 915*0b57cec5SDimitry Andric } else { 916*0b57cec5SDimitry Andric thread = process->GetThreadList() 917*0b57cec5SDimitry Andric .FindThreadByIndexID(m_options.m_thread_idx) 918*0b57cec5SDimitry Andric .get(); 919*0b57cec5SDimitry Andric } 920*0b57cec5SDimitry Andric 921*0b57cec5SDimitry Andric if (thread == nullptr) { 922*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 923*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 924*0b57cec5SDimitry Andric "Thread index %u is out of range (valid values are 0 - %u).\n", 925*0b57cec5SDimitry Andric m_options.m_thread_idx, num_threads); 926*0b57cec5SDimitry Andric return false; 927*0b57cec5SDimitry Andric } 928*0b57cec5SDimitry Andric 929*0b57cec5SDimitry Andric const bool abort_other_plans = false; 930*0b57cec5SDimitry Andric 931*0b57cec5SDimitry Andric StackFrame *frame = 932*0b57cec5SDimitry Andric thread->GetStackFrameAtIndex(m_options.m_frame_idx).get(); 933*0b57cec5SDimitry Andric if (frame == nullptr) { 934*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 935*0b57cec5SDimitry Andric "Frame index %u is out of range for thread %u.\n", 936*0b57cec5SDimitry Andric m_options.m_frame_idx, m_options.m_thread_idx); 937*0b57cec5SDimitry Andric return false; 938*0b57cec5SDimitry Andric } 939*0b57cec5SDimitry Andric 940*0b57cec5SDimitry Andric ThreadPlanSP new_plan_sp; 941*0b57cec5SDimitry Andric Status new_plan_status; 942*0b57cec5SDimitry Andric 943*0b57cec5SDimitry Andric if (frame->HasDebugInformation()) { 944*0b57cec5SDimitry Andric // Finally we got here... Translate the given line number to a bunch 945*0b57cec5SDimitry Andric // of addresses: 946*0b57cec5SDimitry Andric SymbolContext sc(frame->GetSymbolContext(eSymbolContextCompUnit)); 947*0b57cec5SDimitry Andric LineTable *line_table = nullptr; 948*0b57cec5SDimitry Andric if (sc.comp_unit) 949*0b57cec5SDimitry Andric line_table = sc.comp_unit->GetLineTable(); 950*0b57cec5SDimitry Andric 951*0b57cec5SDimitry Andric if (line_table == nullptr) { 952*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to resolve the line table for " 953*0b57cec5SDimitry Andric "frame %u of thread index %u.\n", 954*0b57cec5SDimitry Andric m_options.m_frame_idx, 955*0b57cec5SDimitry Andric m_options.m_thread_idx); 956*0b57cec5SDimitry Andric return false; 957*0b57cec5SDimitry Andric } 958*0b57cec5SDimitry Andric 959*0b57cec5SDimitry Andric LineEntry function_start; 960*0b57cec5SDimitry Andric uint32_t index_ptr = 0, end_ptr; 961*0b57cec5SDimitry Andric std::vector<addr_t> address_list; 962*0b57cec5SDimitry Andric 963*0b57cec5SDimitry Andric // Find the beginning & end index of the 964*0b57cec5SDimitry Andric AddressRange fun_addr_range = sc.function->GetAddressRange(); 965*0b57cec5SDimitry Andric Address fun_start_addr = fun_addr_range.GetBaseAddress(); 966*0b57cec5SDimitry Andric line_table->FindLineEntryByAddress(fun_start_addr, function_start, 967*0b57cec5SDimitry Andric &index_ptr); 968*0b57cec5SDimitry Andric 969*0b57cec5SDimitry Andric Address fun_end_addr(fun_start_addr.GetSection(), 970*0b57cec5SDimitry Andric fun_start_addr.GetOffset() + 971*0b57cec5SDimitry Andric fun_addr_range.GetByteSize()); 972*0b57cec5SDimitry Andric 973*0b57cec5SDimitry Andric bool all_in_function = true; 974*0b57cec5SDimitry Andric 975*0b57cec5SDimitry Andric line_table->FindLineEntryByAddress(fun_end_addr, function_start, 976*0b57cec5SDimitry Andric &end_ptr); 977*0b57cec5SDimitry Andric 978*0b57cec5SDimitry Andric for (uint32_t line_number : line_numbers) { 979*0b57cec5SDimitry Andric uint32_t start_idx_ptr = index_ptr; 980*0b57cec5SDimitry Andric while (start_idx_ptr <= end_ptr) { 981*0b57cec5SDimitry Andric LineEntry line_entry; 982*0b57cec5SDimitry Andric const bool exact = false; 983*0b57cec5SDimitry Andric start_idx_ptr = sc.comp_unit->FindLineEntry( 984480093f4SDimitry Andric start_idx_ptr, line_number, nullptr, exact, &line_entry); 985*0b57cec5SDimitry Andric if (start_idx_ptr == UINT32_MAX) 986*0b57cec5SDimitry Andric break; 987*0b57cec5SDimitry Andric 988*0b57cec5SDimitry Andric addr_t address = 989*0b57cec5SDimitry Andric line_entry.range.GetBaseAddress().GetLoadAddress(target); 990*0b57cec5SDimitry Andric if (address != LLDB_INVALID_ADDRESS) { 991*0b57cec5SDimitry Andric if (fun_addr_range.ContainsLoadAddress(address, target)) 992*0b57cec5SDimitry Andric address_list.push_back(address); 993*0b57cec5SDimitry Andric else 994*0b57cec5SDimitry Andric all_in_function = false; 995*0b57cec5SDimitry Andric } 996*0b57cec5SDimitry Andric start_idx_ptr++; 997*0b57cec5SDimitry Andric } 998*0b57cec5SDimitry Andric } 999*0b57cec5SDimitry Andric 1000*0b57cec5SDimitry Andric for (lldb::addr_t address : m_options.m_until_addrs) { 1001*0b57cec5SDimitry Andric if (fun_addr_range.ContainsLoadAddress(address, target)) 1002*0b57cec5SDimitry Andric address_list.push_back(address); 1003*0b57cec5SDimitry Andric else 1004*0b57cec5SDimitry Andric all_in_function = false; 1005*0b57cec5SDimitry Andric } 1006*0b57cec5SDimitry Andric 1007*0b57cec5SDimitry Andric if (address_list.empty()) { 1008*0b57cec5SDimitry Andric if (all_in_function) 1009*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1010*0b57cec5SDimitry Andric "No line entries matching until target.\n"); 1011*0b57cec5SDimitry Andric else 1012*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1013*0b57cec5SDimitry Andric "Until target outside of the current function.\n"); 1014*0b57cec5SDimitry Andric 1015*0b57cec5SDimitry Andric return false; 1016*0b57cec5SDimitry Andric } 1017*0b57cec5SDimitry Andric 1018*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepUntil( 1019*0b57cec5SDimitry Andric abort_other_plans, &address_list.front(), address_list.size(), 1020*0b57cec5SDimitry Andric m_options.m_stop_others, m_options.m_frame_idx, new_plan_status); 1021*0b57cec5SDimitry Andric if (new_plan_sp) { 1022349cc55cSDimitry Andric // User level plans should be controlling plans so they can be 1023349cc55cSDimitry Andric // interrupted 1024*0b57cec5SDimitry Andric // (e.g. by hitting a breakpoint) and other plans executed by the 1025*0b57cec5SDimitry Andric // user (stepping around the breakpoint) and then a "continue" will 1026*0b57cec5SDimitry Andric // resume the original plan. 1027349cc55cSDimitry Andric new_plan_sp->SetIsControllingPlan(true); 1028*0b57cec5SDimitry Andric new_plan_sp->SetOkayToDiscard(false); 1029*0b57cec5SDimitry Andric } else { 1030*0b57cec5SDimitry Andric result.SetError(new_plan_status); 1031*0b57cec5SDimitry Andric return false; 1032*0b57cec5SDimitry Andric } 1033*0b57cec5SDimitry Andric } else { 1034*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1035*0b57cec5SDimitry Andric "Frame index %u of thread %u has no debug information.\n", 1036*0b57cec5SDimitry Andric m_options.m_frame_idx, m_options.m_thread_idx); 1037*0b57cec5SDimitry Andric return false; 1038*0b57cec5SDimitry Andric } 1039*0b57cec5SDimitry Andric 1040*0b57cec5SDimitry Andric process->GetThreadList().SetSelectedThreadByID(m_options.m_thread_idx); 1041*0b57cec5SDimitry Andric 1042*0b57cec5SDimitry Andric StreamString stream; 1043*0b57cec5SDimitry Andric Status error; 1044*0b57cec5SDimitry Andric if (synchronous_execution) 1045*0b57cec5SDimitry Andric error = process->ResumeSynchronous(&stream); 1046*0b57cec5SDimitry Andric else 1047*0b57cec5SDimitry Andric error = process->Resume(); 1048*0b57cec5SDimitry Andric 1049*0b57cec5SDimitry Andric if (error.Success()) { 1050*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n", 1051*0b57cec5SDimitry Andric process->GetID()); 1052*0b57cec5SDimitry Andric if (synchronous_execution) { 1053*0b57cec5SDimitry Andric // If any state changed events had anything to say, add that to the 1054*0b57cec5SDimitry Andric // result 1055*0b57cec5SDimitry Andric if (stream.GetSize() > 0) 1056*0b57cec5SDimitry Andric result.AppendMessage(stream.GetString()); 1057*0b57cec5SDimitry Andric 1058*0b57cec5SDimitry Andric result.SetDidChangeProcessState(true); 1059*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1060*0b57cec5SDimitry Andric } else { 1061*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessContinuingNoResult); 1062*0b57cec5SDimitry Andric } 1063*0b57cec5SDimitry Andric } else { 1064*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to resume process: %s.\n", 1065*0b57cec5SDimitry Andric error.AsCString()); 1066*0b57cec5SDimitry Andric } 1067*0b57cec5SDimitry Andric } 1068*0b57cec5SDimitry Andric return result.Succeeded(); 1069*0b57cec5SDimitry Andric } 1070*0b57cec5SDimitry Andric 1071*0b57cec5SDimitry Andric CommandOptions m_options; 1072*0b57cec5SDimitry Andric }; 1073*0b57cec5SDimitry Andric 1074*0b57cec5SDimitry Andric // CommandObjectThreadSelect 1075*0b57cec5SDimitry Andric 1076*0b57cec5SDimitry Andric class CommandObjectThreadSelect : public CommandObjectParsed { 1077*0b57cec5SDimitry Andric public: 1078*0b57cec5SDimitry Andric CommandObjectThreadSelect(CommandInterpreter &interpreter) 1079*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "thread select", 1080*0b57cec5SDimitry Andric "Change the currently selected thread.", nullptr, 1081*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1082*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 1083*0b57cec5SDimitry Andric eCommandProcessMustBePaused) { 1084*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1085*0b57cec5SDimitry Andric CommandArgumentData thread_idx_arg; 1086*0b57cec5SDimitry Andric 1087*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1088*0b57cec5SDimitry Andric thread_idx_arg.arg_type = eArgTypeThreadIndex; 1089*0b57cec5SDimitry Andric thread_idx_arg.arg_repetition = eArgRepeatPlain; 1090*0b57cec5SDimitry Andric 1091*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1092*0b57cec5SDimitry Andric // argument entry. 1093*0b57cec5SDimitry Andric arg.push_back(thread_idx_arg); 1094*0b57cec5SDimitry Andric 1095*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1096*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1097*0b57cec5SDimitry Andric } 1098*0b57cec5SDimitry Andric 1099*0b57cec5SDimitry Andric ~CommandObjectThreadSelect() override = default; 1100*0b57cec5SDimitry Andric 1101e8d8bef9SDimitry Andric void 1102e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1103e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1104e8d8bef9SDimitry Andric if (request.GetCursorIndex()) 1105e8d8bef9SDimitry Andric return; 1106e8d8bef9SDimitry Andric 1107e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 1108e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 1109e8d8bef9SDimitry Andric request, nullptr); 1110e8d8bef9SDimitry Andric } 1111e8d8bef9SDimitry Andric 1112*0b57cec5SDimitry Andric protected: 1113*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 1114*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1115*0b57cec5SDimitry Andric if (process == nullptr) { 1116*0b57cec5SDimitry Andric result.AppendError("no process"); 1117*0b57cec5SDimitry Andric return false; 1118*0b57cec5SDimitry Andric } else if (command.GetArgumentCount() != 1) { 1119*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1120*0b57cec5SDimitry Andric "'%s' takes exactly one thread index argument:\nUsage: %s\n", 1121*0b57cec5SDimitry Andric m_cmd_name.c_str(), m_cmd_syntax.c_str()); 1122*0b57cec5SDimitry Andric return false; 1123*0b57cec5SDimitry Andric } 1124*0b57cec5SDimitry Andric 11255ffd83dbSDimitry Andric uint32_t index_id; 11265ffd83dbSDimitry Andric if (!llvm::to_integer(command.GetArgumentAtIndex(0), index_id)) { 11275ffd83dbSDimitry Andric result.AppendErrorWithFormat("Invalid thread index '%s'", 11285ffd83dbSDimitry Andric command.GetArgumentAtIndex(0)); 11295ffd83dbSDimitry Andric return false; 11305ffd83dbSDimitry Andric } 1131*0b57cec5SDimitry Andric 1132*0b57cec5SDimitry Andric Thread *new_thread = 1133*0b57cec5SDimitry Andric process->GetThreadList().FindThreadByIndexID(index_id).get(); 1134*0b57cec5SDimitry Andric if (new_thread == nullptr) { 1135*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid thread #%s.\n", 1136*0b57cec5SDimitry Andric command.GetArgumentAtIndex(0)); 1137*0b57cec5SDimitry Andric return false; 1138*0b57cec5SDimitry Andric } 1139*0b57cec5SDimitry Andric 1140*0b57cec5SDimitry Andric process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true); 1141*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1142*0b57cec5SDimitry Andric 1143*0b57cec5SDimitry Andric return result.Succeeded(); 1144*0b57cec5SDimitry Andric } 1145*0b57cec5SDimitry Andric }; 1146*0b57cec5SDimitry Andric 1147*0b57cec5SDimitry Andric // CommandObjectThreadList 1148*0b57cec5SDimitry Andric 1149*0b57cec5SDimitry Andric class CommandObjectThreadList : public CommandObjectParsed { 1150*0b57cec5SDimitry Andric public: 1151*0b57cec5SDimitry Andric CommandObjectThreadList(CommandInterpreter &interpreter) 1152*0b57cec5SDimitry Andric : CommandObjectParsed( 1153*0b57cec5SDimitry Andric interpreter, "thread list", 1154*0b57cec5SDimitry Andric "Show a summary of each thread in the current target process. " 1155*0b57cec5SDimitry Andric "Use 'settings set thread-format' to customize the individual " 1156*0b57cec5SDimitry Andric "thread listings.", 1157*0b57cec5SDimitry Andric "thread list", 1158*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1159*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} 1160*0b57cec5SDimitry Andric 1161*0b57cec5SDimitry Andric ~CommandObjectThreadList() override = default; 1162*0b57cec5SDimitry Andric 1163*0b57cec5SDimitry Andric protected: 1164*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 1165*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1166*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1167*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1168*0b57cec5SDimitry Andric const bool only_threads_with_stop_reason = false; 1169*0b57cec5SDimitry Andric const uint32_t start_frame = 0; 1170*0b57cec5SDimitry Andric const uint32_t num_frames = 0; 1171*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 0; 1172*0b57cec5SDimitry Andric process->GetStatus(strm); 1173*0b57cec5SDimitry Andric process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame, 1174*0b57cec5SDimitry Andric num_frames, num_frames_with_source, false); 1175*0b57cec5SDimitry Andric return result.Succeeded(); 1176*0b57cec5SDimitry Andric } 1177*0b57cec5SDimitry Andric }; 1178*0b57cec5SDimitry Andric 1179*0b57cec5SDimitry Andric // CommandObjectThreadInfo 1180*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_info 1181*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1182*0b57cec5SDimitry Andric 1183*0b57cec5SDimitry Andric class CommandObjectThreadInfo : public CommandObjectIterateOverThreads { 1184*0b57cec5SDimitry Andric public: 1185*0b57cec5SDimitry Andric class CommandOptions : public Options { 1186*0b57cec5SDimitry Andric public: 118704eeddc0SDimitry Andric CommandOptions() { OptionParsingStarting(nullptr); } 1188*0b57cec5SDimitry Andric 1189*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1190*0b57cec5SDimitry Andric 1191*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1192*0b57cec5SDimitry Andric m_json_thread = false; 1193*0b57cec5SDimitry Andric m_json_stopinfo = false; 1194*0b57cec5SDimitry Andric } 1195*0b57cec5SDimitry Andric 1196*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1197*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1198*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1199*0b57cec5SDimitry Andric Status error; 1200*0b57cec5SDimitry Andric 1201*0b57cec5SDimitry Andric switch (short_option) { 1202*0b57cec5SDimitry Andric case 'j': 1203*0b57cec5SDimitry Andric m_json_thread = true; 1204*0b57cec5SDimitry Andric break; 1205*0b57cec5SDimitry Andric 1206*0b57cec5SDimitry Andric case 's': 1207*0b57cec5SDimitry Andric m_json_stopinfo = true; 1208*0b57cec5SDimitry Andric break; 1209*0b57cec5SDimitry Andric 1210*0b57cec5SDimitry Andric default: 12119dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1212*0b57cec5SDimitry Andric } 1213*0b57cec5SDimitry Andric return error; 1214*0b57cec5SDimitry Andric } 1215*0b57cec5SDimitry Andric 1216*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1217*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_info_options); 1218*0b57cec5SDimitry Andric } 1219*0b57cec5SDimitry Andric 1220*0b57cec5SDimitry Andric bool m_json_thread; 1221*0b57cec5SDimitry Andric bool m_json_stopinfo; 1222*0b57cec5SDimitry Andric }; 1223*0b57cec5SDimitry Andric 1224*0b57cec5SDimitry Andric CommandObjectThreadInfo(CommandInterpreter &interpreter) 1225*0b57cec5SDimitry Andric : CommandObjectIterateOverThreads( 1226480093f4SDimitry Andric interpreter, "thread info", 1227480093f4SDimitry Andric "Show an extended summary of one or " 1228*0b57cec5SDimitry Andric "more threads. Defaults to the " 1229*0b57cec5SDimitry Andric "current thread.", 1230*0b57cec5SDimitry Andric "thread info", 1231*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 123204eeddc0SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) { 1233*0b57cec5SDimitry Andric m_add_return = false; 1234*0b57cec5SDimitry Andric } 1235*0b57cec5SDimitry Andric 1236*0b57cec5SDimitry Andric ~CommandObjectThreadInfo() override = default; 1237*0b57cec5SDimitry Andric 1238e8d8bef9SDimitry Andric void 1239e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1240e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1241e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 1242e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 1243e8d8bef9SDimitry Andric request, nullptr); 1244e8d8bef9SDimitry Andric } 1245e8d8bef9SDimitry Andric 1246*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1247*0b57cec5SDimitry Andric 1248*0b57cec5SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 1249*0b57cec5SDimitry Andric ThreadSP thread_sp = 1250*0b57cec5SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 1251*0b57cec5SDimitry Andric if (!thread_sp) { 1252*0b57cec5SDimitry Andric result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n", 1253*0b57cec5SDimitry Andric tid); 1254*0b57cec5SDimitry Andric return false; 1255*0b57cec5SDimitry Andric } 1256*0b57cec5SDimitry Andric 1257*0b57cec5SDimitry Andric Thread *thread = thread_sp.get(); 1258*0b57cec5SDimitry Andric 1259*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1260*0b57cec5SDimitry Andric if (!thread->GetDescription(strm, eDescriptionLevelFull, 1261*0b57cec5SDimitry Andric m_options.m_json_thread, 1262*0b57cec5SDimitry Andric m_options.m_json_stopinfo)) { 1263*0b57cec5SDimitry Andric result.AppendErrorWithFormat("error displaying info for thread: \"%d\"\n", 1264*0b57cec5SDimitry Andric thread->GetIndexID()); 1265*0b57cec5SDimitry Andric return false; 1266*0b57cec5SDimitry Andric } 1267*0b57cec5SDimitry Andric return true; 1268*0b57cec5SDimitry Andric } 1269*0b57cec5SDimitry Andric 1270*0b57cec5SDimitry Andric CommandOptions m_options; 1271*0b57cec5SDimitry Andric }; 1272*0b57cec5SDimitry Andric 1273*0b57cec5SDimitry Andric // CommandObjectThreadException 1274*0b57cec5SDimitry Andric 1275*0b57cec5SDimitry Andric class CommandObjectThreadException : public CommandObjectIterateOverThreads { 1276*0b57cec5SDimitry Andric public: 1277*0b57cec5SDimitry Andric CommandObjectThreadException(CommandInterpreter &interpreter) 1278*0b57cec5SDimitry Andric : CommandObjectIterateOverThreads( 1279*0b57cec5SDimitry Andric interpreter, "thread exception", 1280*0b57cec5SDimitry Andric "Display the current exception object for a thread. Defaults to " 1281*0b57cec5SDimitry Andric "the current thread.", 1282*0b57cec5SDimitry Andric "thread exception", 1283*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1284*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} 1285*0b57cec5SDimitry Andric 1286*0b57cec5SDimitry Andric ~CommandObjectThreadException() override = default; 1287*0b57cec5SDimitry Andric 1288e8d8bef9SDimitry Andric void 1289e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1290e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1291e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 1292e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 1293e8d8bef9SDimitry Andric request, nullptr); 1294e8d8bef9SDimitry Andric } 1295e8d8bef9SDimitry Andric 1296*0b57cec5SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 1297*0b57cec5SDimitry Andric ThreadSP thread_sp = 1298*0b57cec5SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 1299*0b57cec5SDimitry Andric if (!thread_sp) { 1300*0b57cec5SDimitry Andric result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n", 1301*0b57cec5SDimitry Andric tid); 1302*0b57cec5SDimitry Andric return false; 1303*0b57cec5SDimitry Andric } 1304*0b57cec5SDimitry Andric 1305*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1306*0b57cec5SDimitry Andric ValueObjectSP exception_object_sp = thread_sp->GetCurrentException(); 1307*0b57cec5SDimitry Andric if (exception_object_sp) { 1308*0b57cec5SDimitry Andric exception_object_sp->Dump(strm); 1309*0b57cec5SDimitry Andric } 1310*0b57cec5SDimitry Andric 1311*0b57cec5SDimitry Andric ThreadSP exception_thread_sp = thread_sp->GetCurrentExceptionBacktrace(); 1312*0b57cec5SDimitry Andric if (exception_thread_sp && exception_thread_sp->IsValid()) { 1313*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 0; 1314*0b57cec5SDimitry Andric const bool stop_format = false; 1315*0b57cec5SDimitry Andric exception_thread_sp->GetStatus(strm, 0, UINT32_MAX, 1316*0b57cec5SDimitry Andric num_frames_with_source, stop_format); 1317*0b57cec5SDimitry Andric } 1318*0b57cec5SDimitry Andric 1319*0b57cec5SDimitry Andric return true; 1320*0b57cec5SDimitry Andric } 1321*0b57cec5SDimitry Andric }; 1322*0b57cec5SDimitry Andric 1323d56accc7SDimitry Andric class CommandObjectThreadSiginfo : public CommandObjectIterateOverThreads { 1324d56accc7SDimitry Andric public: 1325d56accc7SDimitry Andric CommandObjectThreadSiginfo(CommandInterpreter &interpreter) 1326d56accc7SDimitry Andric : CommandObjectIterateOverThreads( 1327d56accc7SDimitry Andric interpreter, "thread siginfo", 1328d56accc7SDimitry Andric "Display the current siginfo object for a thread. Defaults to " 1329d56accc7SDimitry Andric "the current thread.", 1330d56accc7SDimitry Andric "thread siginfo", 1331d56accc7SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1332d56accc7SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} 1333d56accc7SDimitry Andric 1334d56accc7SDimitry Andric ~CommandObjectThreadSiginfo() override = default; 1335d56accc7SDimitry Andric 1336d56accc7SDimitry Andric void 1337d56accc7SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1338d56accc7SDimitry Andric OptionElementVector &opt_element_vector) override { 1339d56accc7SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 1340d56accc7SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 1341d56accc7SDimitry Andric request, nullptr); 1342d56accc7SDimitry Andric } 1343d56accc7SDimitry Andric 1344d56accc7SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 1345d56accc7SDimitry Andric ThreadSP thread_sp = 1346d56accc7SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 1347d56accc7SDimitry Andric if (!thread_sp) { 1348d56accc7SDimitry Andric result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n", 1349d56accc7SDimitry Andric tid); 1350d56accc7SDimitry Andric return false; 1351d56accc7SDimitry Andric } 1352d56accc7SDimitry Andric 1353d56accc7SDimitry Andric Stream &strm = result.GetOutputStream(); 1354d56accc7SDimitry Andric if (!thread_sp->GetDescription(strm, eDescriptionLevelFull, false, false)) { 1355d56accc7SDimitry Andric result.AppendErrorWithFormat("error displaying info for thread: \"%d\"\n", 1356d56accc7SDimitry Andric thread_sp->GetIndexID()); 1357d56accc7SDimitry Andric return false; 1358d56accc7SDimitry Andric } 1359d56accc7SDimitry Andric ValueObjectSP exception_object_sp = thread_sp->GetSiginfoValue(); 1360d56accc7SDimitry Andric if (exception_object_sp) 1361d56accc7SDimitry Andric exception_object_sp->Dump(strm); 1362d56accc7SDimitry Andric else 1363d56accc7SDimitry Andric strm.Printf("(no siginfo)\n"); 1364d56accc7SDimitry Andric strm.PutChar('\n'); 1365d56accc7SDimitry Andric 1366d56accc7SDimitry Andric return true; 1367d56accc7SDimitry Andric } 1368d56accc7SDimitry Andric }; 1369d56accc7SDimitry Andric 1370*0b57cec5SDimitry Andric // CommandObjectThreadReturn 1371*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_return 1372*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1373*0b57cec5SDimitry Andric 1374*0b57cec5SDimitry Andric class CommandObjectThreadReturn : public CommandObjectRaw { 1375*0b57cec5SDimitry Andric public: 1376*0b57cec5SDimitry Andric class CommandOptions : public Options { 1377*0b57cec5SDimitry Andric public: 137804eeddc0SDimitry Andric CommandOptions() { 1379*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 1380*0b57cec5SDimitry Andric // () 1381*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 1382*0b57cec5SDimitry Andric } 1383*0b57cec5SDimitry Andric 1384*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1385*0b57cec5SDimitry Andric 1386*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1387*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1388*0b57cec5SDimitry Andric Status error; 1389*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1390*0b57cec5SDimitry Andric 1391*0b57cec5SDimitry Andric switch (short_option) { 1392*0b57cec5SDimitry Andric case 'x': { 1393*0b57cec5SDimitry Andric bool success; 1394*0b57cec5SDimitry Andric bool tmp_value = 1395*0b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, false, &success); 1396*0b57cec5SDimitry Andric if (success) 1397*0b57cec5SDimitry Andric m_from_expression = tmp_value; 1398*0b57cec5SDimitry Andric else { 1399*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 1400*0b57cec5SDimitry Andric "invalid boolean value '%s' for 'x' option", 1401*0b57cec5SDimitry Andric option_arg.str().c_str()); 1402*0b57cec5SDimitry Andric } 1403*0b57cec5SDimitry Andric } break; 1404*0b57cec5SDimitry Andric default: 14059dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1406*0b57cec5SDimitry Andric } 1407*0b57cec5SDimitry Andric return error; 1408*0b57cec5SDimitry Andric } 1409*0b57cec5SDimitry Andric 1410*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1411*0b57cec5SDimitry Andric m_from_expression = false; 1412*0b57cec5SDimitry Andric } 1413*0b57cec5SDimitry Andric 1414*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1415*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_return_options); 1416*0b57cec5SDimitry Andric } 1417*0b57cec5SDimitry Andric 1418fe6060f1SDimitry Andric bool m_from_expression = false; 1419*0b57cec5SDimitry Andric 1420*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 1421*0b57cec5SDimitry Andric }; 1422*0b57cec5SDimitry Andric 1423*0b57cec5SDimitry Andric CommandObjectThreadReturn(CommandInterpreter &interpreter) 1424*0b57cec5SDimitry Andric : CommandObjectRaw(interpreter, "thread return", 1425*0b57cec5SDimitry Andric "Prematurely return from a stack frame, " 1426*0b57cec5SDimitry Andric "short-circuiting execution of newer frames " 1427*0b57cec5SDimitry Andric "and optionally yielding a specified value. Defaults " 1428*0b57cec5SDimitry Andric "to the exiting the current stack " 1429*0b57cec5SDimitry Andric "frame.", 1430*0b57cec5SDimitry Andric "thread return", 1431*0b57cec5SDimitry Andric eCommandRequiresFrame | eCommandTryTargetAPILock | 1432*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 143304eeddc0SDimitry Andric eCommandProcessMustBePaused) { 1434*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1435*0b57cec5SDimitry Andric CommandArgumentData expression_arg; 1436*0b57cec5SDimitry Andric 1437*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1438*0b57cec5SDimitry Andric expression_arg.arg_type = eArgTypeExpression; 1439*0b57cec5SDimitry Andric expression_arg.arg_repetition = eArgRepeatOptional; 1440*0b57cec5SDimitry Andric 1441*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1442*0b57cec5SDimitry Andric // argument entry. 1443*0b57cec5SDimitry Andric arg.push_back(expression_arg); 1444*0b57cec5SDimitry Andric 1445*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1446*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1447*0b57cec5SDimitry Andric } 1448*0b57cec5SDimitry Andric 1449*0b57cec5SDimitry Andric ~CommandObjectThreadReturn() override = default; 1450*0b57cec5SDimitry Andric 1451*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1452*0b57cec5SDimitry Andric 1453*0b57cec5SDimitry Andric protected: 1454*0b57cec5SDimitry Andric bool DoExecute(llvm::StringRef command, 1455*0b57cec5SDimitry Andric CommandReturnObject &result) override { 1456*0b57cec5SDimitry Andric // I am going to handle this by hand, because I don't want you to have to 1457*0b57cec5SDimitry Andric // say: 1458*0b57cec5SDimitry Andric // "thread return -- -5". 1459*0b57cec5SDimitry Andric if (command.startswith("-x")) { 1460*0b57cec5SDimitry Andric if (command.size() != 2U) 1461*0b57cec5SDimitry Andric result.AppendWarning("Return values ignored when returning from user " 1462*0b57cec5SDimitry Andric "called expressions"); 1463*0b57cec5SDimitry Andric 1464*0b57cec5SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 1465*0b57cec5SDimitry Andric Status error; 1466*0b57cec5SDimitry Andric error = thread->UnwindInnermostExpression(); 1467*0b57cec5SDimitry Andric if (!error.Success()) { 1468*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Unwinding expression failed - %s.", 1469*0b57cec5SDimitry Andric error.AsCString()); 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 } 1480*0b57cec5SDimitry Andric } 1481*0b57cec5SDimitry Andric return result.Succeeded(); 1482*0b57cec5SDimitry Andric } 1483*0b57cec5SDimitry Andric 1484*0b57cec5SDimitry Andric ValueObjectSP return_valobj_sp; 1485*0b57cec5SDimitry Andric 1486*0b57cec5SDimitry Andric StackFrameSP frame_sp = m_exe_ctx.GetFrameSP(); 1487*0b57cec5SDimitry Andric uint32_t frame_idx = frame_sp->GetFrameIndex(); 1488*0b57cec5SDimitry Andric 1489*0b57cec5SDimitry Andric if (frame_sp->IsInlined()) { 1490*0b57cec5SDimitry Andric result.AppendError("Don't know how to return from inlined frames."); 1491*0b57cec5SDimitry Andric return false; 1492*0b57cec5SDimitry Andric } 1493*0b57cec5SDimitry Andric 1494*0b57cec5SDimitry Andric if (!command.empty()) { 1495*0b57cec5SDimitry Andric Target *target = m_exe_ctx.GetTargetPtr(); 1496*0b57cec5SDimitry Andric EvaluateExpressionOptions options; 1497*0b57cec5SDimitry Andric 1498*0b57cec5SDimitry Andric options.SetUnwindOnError(true); 1499*0b57cec5SDimitry Andric options.SetUseDynamic(eNoDynamicValues); 1500*0b57cec5SDimitry Andric 1501*0b57cec5SDimitry Andric ExpressionResults exe_results = eExpressionSetupError; 1502*0b57cec5SDimitry Andric exe_results = target->EvaluateExpression(command, frame_sp.get(), 1503*0b57cec5SDimitry Andric return_valobj_sp, options); 1504*0b57cec5SDimitry Andric if (exe_results != eExpressionCompleted) { 1505*0b57cec5SDimitry Andric if (return_valobj_sp) 1506*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1507*0b57cec5SDimitry Andric "Error evaluating result expression: %s", 1508*0b57cec5SDimitry Andric return_valobj_sp->GetError().AsCString()); 1509*0b57cec5SDimitry Andric else 1510*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1511*0b57cec5SDimitry Andric "Unknown error evaluating result expression."); 1512*0b57cec5SDimitry Andric return false; 1513*0b57cec5SDimitry Andric } 1514*0b57cec5SDimitry Andric } 1515*0b57cec5SDimitry Andric 1516*0b57cec5SDimitry Andric Status error; 1517*0b57cec5SDimitry Andric ThreadSP thread_sp = m_exe_ctx.GetThreadSP(); 1518*0b57cec5SDimitry Andric const bool broadcast = true; 1519*0b57cec5SDimitry Andric error = thread_sp->ReturnFromFrame(frame_sp, return_valobj_sp, broadcast); 1520*0b57cec5SDimitry Andric if (!error.Success()) { 1521*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1522*0b57cec5SDimitry Andric "Error returning from frame %d of thread %d: %s.", frame_idx, 1523*0b57cec5SDimitry Andric thread_sp->GetIndexID(), error.AsCString()); 1524*0b57cec5SDimitry Andric return false; 1525*0b57cec5SDimitry Andric } 1526*0b57cec5SDimitry Andric 1527*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1528*0b57cec5SDimitry Andric return true; 1529*0b57cec5SDimitry Andric } 1530*0b57cec5SDimitry Andric 1531*0b57cec5SDimitry Andric CommandOptions m_options; 1532*0b57cec5SDimitry Andric }; 1533*0b57cec5SDimitry Andric 1534*0b57cec5SDimitry Andric // CommandObjectThreadJump 1535*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_jump 1536*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1537*0b57cec5SDimitry Andric 1538*0b57cec5SDimitry Andric class CommandObjectThreadJump : public CommandObjectParsed { 1539*0b57cec5SDimitry Andric public: 1540*0b57cec5SDimitry Andric class CommandOptions : public Options { 1541*0b57cec5SDimitry Andric public: 154204eeddc0SDimitry Andric CommandOptions() { OptionParsingStarting(nullptr); } 1543*0b57cec5SDimitry Andric 1544*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1545*0b57cec5SDimitry Andric 1546*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1547*0b57cec5SDimitry Andric m_filenames.Clear(); 1548*0b57cec5SDimitry Andric m_line_num = 0; 1549*0b57cec5SDimitry Andric m_line_offset = 0; 1550*0b57cec5SDimitry Andric m_load_addr = LLDB_INVALID_ADDRESS; 1551*0b57cec5SDimitry Andric m_force = false; 1552*0b57cec5SDimitry Andric } 1553*0b57cec5SDimitry Andric 1554*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1555*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1556*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1557*0b57cec5SDimitry Andric Status error; 1558*0b57cec5SDimitry Andric 1559*0b57cec5SDimitry Andric switch (short_option) { 1560*0b57cec5SDimitry Andric case 'f': 1561*0b57cec5SDimitry Andric m_filenames.AppendIfUnique(FileSpec(option_arg)); 1562*0b57cec5SDimitry Andric if (m_filenames.GetSize() > 1) 1563*0b57cec5SDimitry Andric return Status("only one source file expected."); 1564*0b57cec5SDimitry Andric break; 1565*0b57cec5SDimitry Andric case 'l': 1566*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_line_num)) 1567*0b57cec5SDimitry Andric return Status("invalid line number: '%s'.", option_arg.str().c_str()); 1568*0b57cec5SDimitry Andric break; 1569*0b57cec5SDimitry Andric case 'b': 1570*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_line_offset)) 1571*0b57cec5SDimitry Andric return Status("invalid line offset: '%s'.", option_arg.str().c_str()); 1572*0b57cec5SDimitry Andric break; 1573*0b57cec5SDimitry Andric case 'a': 1574*0b57cec5SDimitry Andric m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg, 1575*0b57cec5SDimitry Andric LLDB_INVALID_ADDRESS, &error); 1576*0b57cec5SDimitry Andric break; 1577*0b57cec5SDimitry Andric case 'r': 1578*0b57cec5SDimitry Andric m_force = true; 1579*0b57cec5SDimitry Andric break; 1580*0b57cec5SDimitry Andric default: 15819dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1582*0b57cec5SDimitry Andric } 1583*0b57cec5SDimitry Andric return error; 1584*0b57cec5SDimitry Andric } 1585*0b57cec5SDimitry Andric 1586*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1587*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_jump_options); 1588*0b57cec5SDimitry Andric } 1589*0b57cec5SDimitry Andric 1590*0b57cec5SDimitry Andric FileSpecList m_filenames; 1591*0b57cec5SDimitry Andric uint32_t m_line_num; 1592*0b57cec5SDimitry Andric int32_t m_line_offset; 1593*0b57cec5SDimitry Andric lldb::addr_t m_load_addr; 1594*0b57cec5SDimitry Andric bool m_force; 1595*0b57cec5SDimitry Andric }; 1596*0b57cec5SDimitry Andric 1597*0b57cec5SDimitry Andric CommandObjectThreadJump(CommandInterpreter &interpreter) 1598*0b57cec5SDimitry Andric : CommandObjectParsed( 1599*0b57cec5SDimitry Andric interpreter, "thread jump", 1600*0b57cec5SDimitry Andric "Sets the program counter to a new address.", "thread jump", 1601*0b57cec5SDimitry Andric eCommandRequiresFrame | eCommandTryTargetAPILock | 160204eeddc0SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} 1603*0b57cec5SDimitry Andric 1604*0b57cec5SDimitry Andric ~CommandObjectThreadJump() override = default; 1605*0b57cec5SDimitry Andric 1606*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1607*0b57cec5SDimitry Andric 1608*0b57cec5SDimitry Andric protected: 1609*0b57cec5SDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 1610*0b57cec5SDimitry Andric RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext(); 1611*0b57cec5SDimitry Andric StackFrame *frame = m_exe_ctx.GetFramePtr(); 1612*0b57cec5SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 1613*0b57cec5SDimitry Andric Target *target = m_exe_ctx.GetTargetPtr(); 1614*0b57cec5SDimitry Andric const SymbolContext &sym_ctx = 1615*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextLineEntry); 1616*0b57cec5SDimitry Andric 1617*0b57cec5SDimitry Andric if (m_options.m_load_addr != LLDB_INVALID_ADDRESS) { 1618*0b57cec5SDimitry Andric // Use this address directly. 1619*0b57cec5SDimitry Andric Address dest = Address(m_options.m_load_addr); 1620*0b57cec5SDimitry Andric 1621*0b57cec5SDimitry Andric lldb::addr_t callAddr = dest.GetCallableLoadAddress(target); 1622*0b57cec5SDimitry Andric if (callAddr == LLDB_INVALID_ADDRESS) { 1623*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid destination address."); 1624*0b57cec5SDimitry Andric return false; 1625*0b57cec5SDimitry Andric } 1626*0b57cec5SDimitry Andric 1627*0b57cec5SDimitry Andric if (!reg_ctx->SetPC(callAddr)) { 1628*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Error changing PC value for thread %d.", 1629*0b57cec5SDimitry Andric thread->GetIndexID()); 1630*0b57cec5SDimitry Andric return false; 1631*0b57cec5SDimitry Andric } 1632*0b57cec5SDimitry Andric } else { 1633*0b57cec5SDimitry Andric // Pick either the absolute line, or work out a relative one. 1634*0b57cec5SDimitry Andric int32_t line = (int32_t)m_options.m_line_num; 1635*0b57cec5SDimitry Andric if (line == 0) 1636*0b57cec5SDimitry Andric line = sym_ctx.line_entry.line + m_options.m_line_offset; 1637*0b57cec5SDimitry Andric 1638*0b57cec5SDimitry Andric // Try the current file, but override if asked. 1639*0b57cec5SDimitry Andric FileSpec file = sym_ctx.line_entry.file; 1640*0b57cec5SDimitry Andric if (m_options.m_filenames.GetSize() == 1) 1641*0b57cec5SDimitry Andric file = m_options.m_filenames.GetFileSpecAtIndex(0); 1642*0b57cec5SDimitry Andric 1643*0b57cec5SDimitry Andric if (!file) { 1644*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1645*0b57cec5SDimitry Andric "No source file available for the current location."); 1646*0b57cec5SDimitry Andric return false; 1647*0b57cec5SDimitry Andric } 1648*0b57cec5SDimitry Andric 1649*0b57cec5SDimitry Andric std::string warnings; 1650*0b57cec5SDimitry Andric Status err = thread->JumpToLine(file, line, m_options.m_force, &warnings); 1651*0b57cec5SDimitry Andric 1652*0b57cec5SDimitry Andric if (err.Fail()) { 1653*0b57cec5SDimitry Andric result.SetError(err); 1654*0b57cec5SDimitry Andric return false; 1655*0b57cec5SDimitry Andric } 1656*0b57cec5SDimitry Andric 1657*0b57cec5SDimitry Andric if (!warnings.empty()) 1658*0b57cec5SDimitry Andric result.AppendWarning(warnings.c_str()); 1659*0b57cec5SDimitry Andric } 1660*0b57cec5SDimitry Andric 1661*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1662*0b57cec5SDimitry Andric return true; 1663*0b57cec5SDimitry Andric } 1664*0b57cec5SDimitry Andric 1665*0b57cec5SDimitry Andric CommandOptions m_options; 1666*0b57cec5SDimitry Andric }; 1667*0b57cec5SDimitry Andric 1668*0b57cec5SDimitry Andric // Next are the subcommands of CommandObjectMultiwordThreadPlan 1669*0b57cec5SDimitry Andric 1670*0b57cec5SDimitry Andric // CommandObjectThreadPlanList 1671*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_plan_list 1672*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1673*0b57cec5SDimitry Andric 1674*0b57cec5SDimitry Andric class CommandObjectThreadPlanList : public CommandObjectIterateOverThreads { 1675*0b57cec5SDimitry Andric public: 1676*0b57cec5SDimitry Andric class CommandOptions : public Options { 1677*0b57cec5SDimitry Andric public: 167804eeddc0SDimitry Andric CommandOptions() { 1679*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 1680*0b57cec5SDimitry Andric // () 1681*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 1682*0b57cec5SDimitry Andric } 1683*0b57cec5SDimitry Andric 1684*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1685*0b57cec5SDimitry Andric 1686*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1687*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1688*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1689*0b57cec5SDimitry Andric 1690*0b57cec5SDimitry Andric switch (short_option) { 1691*0b57cec5SDimitry Andric case 'i': 1692*0b57cec5SDimitry Andric m_internal = true; 1693*0b57cec5SDimitry Andric break; 16945ffd83dbSDimitry Andric case 't': 16955ffd83dbSDimitry Andric lldb::tid_t tid; 16965ffd83dbSDimitry Andric if (option_arg.getAsInteger(0, tid)) 16975ffd83dbSDimitry Andric return Status("invalid tid: '%s'.", option_arg.str().c_str()); 16985ffd83dbSDimitry Andric m_tids.push_back(tid); 16995ffd83dbSDimitry Andric break; 17005ffd83dbSDimitry Andric case 'u': 17015ffd83dbSDimitry Andric m_unreported = false; 17025ffd83dbSDimitry Andric break; 1703*0b57cec5SDimitry Andric case 'v': 1704*0b57cec5SDimitry Andric m_verbose = true; 1705*0b57cec5SDimitry Andric break; 1706*0b57cec5SDimitry Andric default: 17079dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1708*0b57cec5SDimitry Andric } 17095ffd83dbSDimitry Andric return {}; 1710*0b57cec5SDimitry Andric } 1711*0b57cec5SDimitry Andric 1712*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1713*0b57cec5SDimitry Andric m_verbose = false; 1714*0b57cec5SDimitry Andric m_internal = false; 17155ffd83dbSDimitry Andric m_unreported = true; // The variable is "skip unreported" and we want to 17165ffd83dbSDimitry Andric // skip unreported by default. 17175ffd83dbSDimitry Andric m_tids.clear(); 1718*0b57cec5SDimitry Andric } 1719*0b57cec5SDimitry Andric 1720*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1721*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_plan_list_options); 1722*0b57cec5SDimitry Andric } 1723*0b57cec5SDimitry Andric 1724*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 1725*0b57cec5SDimitry Andric bool m_verbose; 1726*0b57cec5SDimitry Andric bool m_internal; 17275ffd83dbSDimitry Andric bool m_unreported; 17285ffd83dbSDimitry Andric std::vector<lldb::tid_t> m_tids; 1729*0b57cec5SDimitry Andric }; 1730*0b57cec5SDimitry Andric 1731*0b57cec5SDimitry Andric CommandObjectThreadPlanList(CommandInterpreter &interpreter) 1732*0b57cec5SDimitry Andric : CommandObjectIterateOverThreads( 1733*0b57cec5SDimitry Andric interpreter, "thread plan list", 1734*0b57cec5SDimitry Andric "Show thread plans for one or more threads. If no threads are " 1735*0b57cec5SDimitry Andric "specified, show the " 1736*0b57cec5SDimitry Andric "current thread. Use the thread-index \"all\" to see all threads.", 1737*0b57cec5SDimitry Andric nullptr, 1738*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 1739*0b57cec5SDimitry Andric eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | 174004eeddc0SDimitry Andric eCommandProcessMustBePaused) {} 1741*0b57cec5SDimitry Andric 1742*0b57cec5SDimitry Andric ~CommandObjectThreadPlanList() override = default; 1743*0b57cec5SDimitry Andric 1744*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1745*0b57cec5SDimitry Andric 17465ffd83dbSDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 17475ffd83dbSDimitry Andric // If we are reporting all threads, dispatch to the Process to do that: 17485ffd83dbSDimitry Andric if (command.GetArgumentCount() == 0 && m_options.m_tids.empty()) { 17495ffd83dbSDimitry Andric Stream &strm = result.GetOutputStream(); 17505ffd83dbSDimitry Andric DescriptionLevel desc_level = m_options.m_verbose 17515ffd83dbSDimitry Andric ? eDescriptionLevelVerbose 17525ffd83dbSDimitry Andric : eDescriptionLevelFull; 17535ffd83dbSDimitry Andric m_exe_ctx.GetProcessPtr()->DumpThreadPlans( 17545ffd83dbSDimitry Andric strm, desc_level, m_options.m_internal, true, m_options.m_unreported); 17555ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 17565ffd83dbSDimitry Andric return true; 17575ffd83dbSDimitry Andric } else { 17585ffd83dbSDimitry Andric // Do any TID's that the user may have specified as TID, then do any 17595ffd83dbSDimitry Andric // Thread Indexes... 17605ffd83dbSDimitry Andric if (!m_options.m_tids.empty()) { 17615ffd83dbSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 17625ffd83dbSDimitry Andric StreamString tmp_strm; 17635ffd83dbSDimitry Andric for (lldb::tid_t tid : m_options.m_tids) { 17645ffd83dbSDimitry Andric bool success = process->DumpThreadPlansForTID( 17655ffd83dbSDimitry Andric tmp_strm, tid, eDescriptionLevelFull, m_options.m_internal, 17665ffd83dbSDimitry Andric true /* condense_trivial */, m_options.m_unreported); 17675ffd83dbSDimitry Andric // If we didn't find a TID, stop here and return an error. 17685ffd83dbSDimitry Andric if (!success) { 1769fe6060f1SDimitry Andric result.AppendError("Error dumping plans:"); 17705ffd83dbSDimitry Andric result.AppendError(tmp_strm.GetString()); 1771*0b57cec5SDimitry Andric return false; 1772*0b57cec5SDimitry Andric } 17735ffd83dbSDimitry Andric // Otherwise, add our data to the output: 17745ffd83dbSDimitry Andric result.GetOutputStream() << tmp_strm.GetString(); 17755ffd83dbSDimitry Andric } 17765ffd83dbSDimitry Andric } 17775ffd83dbSDimitry Andric return CommandObjectIterateOverThreads::DoExecute(command, result); 17785ffd83dbSDimitry Andric } 17795ffd83dbSDimitry Andric } 1780*0b57cec5SDimitry Andric 17815ffd83dbSDimitry Andric protected: 17825ffd83dbSDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 17835ffd83dbSDimitry Andric // If we have already handled this from a -t option, skip it here. 1784e8d8bef9SDimitry Andric if (llvm::is_contained(m_options.m_tids, tid)) 17855ffd83dbSDimitry Andric return true; 17865ffd83dbSDimitry Andric 17875ffd83dbSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1788*0b57cec5SDimitry Andric 1789*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1790*0b57cec5SDimitry Andric DescriptionLevel desc_level = eDescriptionLevelFull; 1791*0b57cec5SDimitry Andric if (m_options.m_verbose) 1792*0b57cec5SDimitry Andric desc_level = eDescriptionLevelVerbose; 1793*0b57cec5SDimitry Andric 17945ffd83dbSDimitry Andric process->DumpThreadPlansForTID(strm, tid, desc_level, m_options.m_internal, 17955ffd83dbSDimitry Andric true /* condense_trivial */, 17965ffd83dbSDimitry Andric m_options.m_unreported); 1797*0b57cec5SDimitry Andric return true; 1798*0b57cec5SDimitry Andric } 1799*0b57cec5SDimitry Andric 1800*0b57cec5SDimitry Andric CommandOptions m_options; 1801*0b57cec5SDimitry Andric }; 1802*0b57cec5SDimitry Andric 1803*0b57cec5SDimitry Andric class CommandObjectThreadPlanDiscard : public CommandObjectParsed { 1804*0b57cec5SDimitry Andric public: 1805*0b57cec5SDimitry Andric CommandObjectThreadPlanDiscard(CommandInterpreter &interpreter) 1806*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "thread plan discard", 1807*0b57cec5SDimitry Andric "Discards thread plans up to and including the " 1808*0b57cec5SDimitry Andric "specified index (see 'thread plan list'.) " 1809*0b57cec5SDimitry Andric "Only user visible plans can be discarded.", 1810*0b57cec5SDimitry Andric nullptr, 1811*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 1812*0b57cec5SDimitry Andric eCommandTryTargetAPILock | 1813*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 1814*0b57cec5SDimitry Andric eCommandProcessMustBePaused) { 1815*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1816*0b57cec5SDimitry Andric CommandArgumentData plan_index_arg; 1817*0b57cec5SDimitry Andric 1818*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1819*0b57cec5SDimitry Andric plan_index_arg.arg_type = eArgTypeUnsignedInteger; 1820*0b57cec5SDimitry Andric plan_index_arg.arg_repetition = eArgRepeatPlain; 1821*0b57cec5SDimitry Andric 1822*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1823*0b57cec5SDimitry Andric // argument entry. 1824*0b57cec5SDimitry Andric arg.push_back(plan_index_arg); 1825*0b57cec5SDimitry Andric 1826*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1827*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1828*0b57cec5SDimitry Andric } 1829*0b57cec5SDimitry Andric 1830*0b57cec5SDimitry Andric ~CommandObjectThreadPlanDiscard() override = default; 1831*0b57cec5SDimitry Andric 1832e8d8bef9SDimitry Andric void 1833e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1834e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1835e8d8bef9SDimitry Andric if (!m_exe_ctx.HasThreadScope() || request.GetCursorIndex()) 1836e8d8bef9SDimitry Andric return; 1837e8d8bef9SDimitry Andric 1838e8d8bef9SDimitry Andric m_exe_ctx.GetThreadPtr()->AutoCompleteThreadPlans(request); 1839e8d8bef9SDimitry Andric } 1840e8d8bef9SDimitry Andric 1841*0b57cec5SDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 1842*0b57cec5SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 1843*0b57cec5SDimitry Andric if (args.GetArgumentCount() != 1) { 1844*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Too many arguments, expected one - the " 1845*0b57cec5SDimitry Andric "thread plan index - but got %zu.", 1846*0b57cec5SDimitry Andric args.GetArgumentCount()); 1847*0b57cec5SDimitry Andric return false; 1848*0b57cec5SDimitry Andric } 1849*0b57cec5SDimitry Andric 18505ffd83dbSDimitry Andric uint32_t thread_plan_idx; 18515ffd83dbSDimitry Andric if (!llvm::to_integer(args.GetArgumentAtIndex(0), thread_plan_idx)) { 1852*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1853*0b57cec5SDimitry Andric "Invalid thread index: \"%s\" - should be unsigned int.", 1854*0b57cec5SDimitry Andric args.GetArgumentAtIndex(0)); 1855*0b57cec5SDimitry Andric return false; 1856*0b57cec5SDimitry Andric } 1857*0b57cec5SDimitry Andric 1858*0b57cec5SDimitry Andric if (thread_plan_idx == 0) { 1859*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1860*0b57cec5SDimitry Andric "You wouldn't really want me to discard the base thread plan."); 1861*0b57cec5SDimitry Andric return false; 1862*0b57cec5SDimitry Andric } 1863*0b57cec5SDimitry Andric 1864*0b57cec5SDimitry Andric if (thread->DiscardUserThreadPlansUpToIndex(thread_plan_idx)) { 1865*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1866*0b57cec5SDimitry Andric return true; 1867*0b57cec5SDimitry Andric } else { 1868*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1869*0b57cec5SDimitry Andric "Could not find User thread plan with index %s.", 1870*0b57cec5SDimitry Andric args.GetArgumentAtIndex(0)); 1871*0b57cec5SDimitry Andric return false; 1872*0b57cec5SDimitry Andric } 1873*0b57cec5SDimitry Andric } 1874*0b57cec5SDimitry Andric }; 1875*0b57cec5SDimitry Andric 18765ffd83dbSDimitry Andric class CommandObjectThreadPlanPrune : public CommandObjectParsed { 18775ffd83dbSDimitry Andric public: 18785ffd83dbSDimitry Andric CommandObjectThreadPlanPrune(CommandInterpreter &interpreter) 18795ffd83dbSDimitry Andric : CommandObjectParsed(interpreter, "thread plan prune", 18805ffd83dbSDimitry Andric "Removes any thread plans associated with " 18815ffd83dbSDimitry Andric "currently unreported threads. " 18825ffd83dbSDimitry Andric "Specify one or more TID's to remove, or if no " 18835ffd83dbSDimitry Andric "TID's are provides, remove threads for all " 18845ffd83dbSDimitry Andric "unreported threads", 18855ffd83dbSDimitry Andric nullptr, 18865ffd83dbSDimitry Andric eCommandRequiresProcess | 18875ffd83dbSDimitry Andric eCommandTryTargetAPILock | 18885ffd83dbSDimitry Andric eCommandProcessMustBeLaunched | 18895ffd83dbSDimitry Andric eCommandProcessMustBePaused) { 18905ffd83dbSDimitry Andric CommandArgumentEntry arg; 18915ffd83dbSDimitry Andric CommandArgumentData tid_arg; 18925ffd83dbSDimitry Andric 18935ffd83dbSDimitry Andric // Define the first (and only) variant of this arg. 18945ffd83dbSDimitry Andric tid_arg.arg_type = eArgTypeThreadID; 18955ffd83dbSDimitry Andric tid_arg.arg_repetition = eArgRepeatStar; 18965ffd83dbSDimitry Andric 18975ffd83dbSDimitry Andric // There is only one variant this argument could be; put it into the 18985ffd83dbSDimitry Andric // argument entry. 18995ffd83dbSDimitry Andric arg.push_back(tid_arg); 19005ffd83dbSDimitry Andric 19015ffd83dbSDimitry Andric // Push the data for the first argument into the m_arguments vector. 19025ffd83dbSDimitry Andric m_arguments.push_back(arg); 19035ffd83dbSDimitry Andric } 19045ffd83dbSDimitry Andric 19055ffd83dbSDimitry Andric ~CommandObjectThreadPlanPrune() override = default; 19065ffd83dbSDimitry Andric 19075ffd83dbSDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 19085ffd83dbSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 19095ffd83dbSDimitry Andric 19105ffd83dbSDimitry Andric if (args.GetArgumentCount() == 0) { 19115ffd83dbSDimitry Andric process->PruneThreadPlans(); 19125ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 19135ffd83dbSDimitry Andric return true; 19145ffd83dbSDimitry Andric } 19155ffd83dbSDimitry Andric 19165ffd83dbSDimitry Andric const size_t num_args = args.GetArgumentCount(); 19175ffd83dbSDimitry Andric 19185ffd83dbSDimitry Andric std::lock_guard<std::recursive_mutex> guard( 19195ffd83dbSDimitry Andric process->GetThreadList().GetMutex()); 19205ffd83dbSDimitry Andric 19215ffd83dbSDimitry Andric for (size_t i = 0; i < num_args; i++) { 19225ffd83dbSDimitry Andric lldb::tid_t tid; 19235ffd83dbSDimitry Andric if (!llvm::to_integer(args.GetArgumentAtIndex(i), tid)) { 19245ffd83dbSDimitry Andric result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n", 19255ffd83dbSDimitry Andric args.GetArgumentAtIndex(i)); 19265ffd83dbSDimitry Andric return false; 19275ffd83dbSDimitry Andric } 19285ffd83dbSDimitry Andric if (!process->PruneThreadPlansForTID(tid)) { 19295ffd83dbSDimitry Andric result.AppendErrorWithFormat("Could not find unreported tid: \"%s\"\n", 19305ffd83dbSDimitry Andric args.GetArgumentAtIndex(i)); 19315ffd83dbSDimitry Andric return false; 19325ffd83dbSDimitry Andric } 19335ffd83dbSDimitry Andric } 19345ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 19355ffd83dbSDimitry Andric return true; 19365ffd83dbSDimitry Andric } 19375ffd83dbSDimitry Andric }; 19385ffd83dbSDimitry Andric 1939*0b57cec5SDimitry Andric // CommandObjectMultiwordThreadPlan 1940*0b57cec5SDimitry Andric 1941*0b57cec5SDimitry Andric class CommandObjectMultiwordThreadPlan : public CommandObjectMultiword { 1942*0b57cec5SDimitry Andric public: 1943*0b57cec5SDimitry Andric CommandObjectMultiwordThreadPlan(CommandInterpreter &interpreter) 1944*0b57cec5SDimitry Andric : CommandObjectMultiword( 1945*0b57cec5SDimitry Andric interpreter, "plan", 1946*0b57cec5SDimitry Andric "Commands for managing thread plans that control execution.", 1947*0b57cec5SDimitry Andric "thread plan <subcommand> [<subcommand objects]") { 1948*0b57cec5SDimitry Andric LoadSubCommand( 1949*0b57cec5SDimitry Andric "list", CommandObjectSP(new CommandObjectThreadPlanList(interpreter))); 1950*0b57cec5SDimitry Andric LoadSubCommand( 1951*0b57cec5SDimitry Andric "discard", 1952*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadPlanDiscard(interpreter))); 19535ffd83dbSDimitry Andric LoadSubCommand( 19545ffd83dbSDimitry Andric "prune", 19555ffd83dbSDimitry Andric CommandObjectSP(new CommandObjectThreadPlanPrune(interpreter))); 1956*0b57cec5SDimitry Andric } 1957*0b57cec5SDimitry Andric 1958*0b57cec5SDimitry Andric ~CommandObjectMultiwordThreadPlan() override = default; 1959*0b57cec5SDimitry Andric }; 1960*0b57cec5SDimitry Andric 1961e8d8bef9SDimitry Andric // Next are the subcommands of CommandObjectMultiwordTrace 1962e8d8bef9SDimitry Andric 1963fe6060f1SDimitry Andric // CommandObjectTraceExport 1964fe6060f1SDimitry Andric 1965fe6060f1SDimitry Andric class CommandObjectTraceExport : public CommandObjectMultiword { 1966fe6060f1SDimitry Andric public: 1967fe6060f1SDimitry Andric CommandObjectTraceExport(CommandInterpreter &interpreter) 1968fe6060f1SDimitry Andric : CommandObjectMultiword( 1969fe6060f1SDimitry Andric interpreter, "trace thread export", 1970fe6060f1SDimitry Andric "Commands for exporting traces of the threads in the current " 1971fe6060f1SDimitry Andric "process to different formats.", 1972fe6060f1SDimitry Andric "thread trace export <export-plugin> [<subcommand objects>]") { 1973fe6060f1SDimitry Andric 1974349cc55cSDimitry Andric unsigned i = 0; 1975349cc55cSDimitry Andric for (llvm::StringRef plugin_name = 1976349cc55cSDimitry Andric PluginManager::GetTraceExporterPluginNameAtIndex(i++); 1977349cc55cSDimitry Andric !plugin_name.empty(); 1978349cc55cSDimitry Andric plugin_name = PluginManager::GetTraceExporterPluginNameAtIndex(i++)) { 1979fe6060f1SDimitry Andric if (ThreadTraceExportCommandCreator command_creator = 1980fe6060f1SDimitry Andric PluginManager::GetThreadTraceExportCommandCreatorAtIndex(i)) { 1981fe6060f1SDimitry Andric LoadSubCommand(plugin_name, command_creator(interpreter)); 1982fe6060f1SDimitry Andric } 1983fe6060f1SDimitry Andric } 1984fe6060f1SDimitry Andric } 1985fe6060f1SDimitry Andric }; 1986fe6060f1SDimitry Andric 1987e8d8bef9SDimitry Andric // CommandObjectTraceStart 1988e8d8bef9SDimitry Andric 1989fe6060f1SDimitry Andric class CommandObjectTraceStart : public CommandObjectTraceProxy { 1990e8d8bef9SDimitry Andric public: 1991e8d8bef9SDimitry Andric CommandObjectTraceStart(CommandInterpreter &interpreter) 1992fe6060f1SDimitry Andric : CommandObjectTraceProxy( 1993fe6060f1SDimitry Andric /*live_debug_session_only=*/true, interpreter, "thread trace start", 1994e8d8bef9SDimitry Andric "Start tracing threads with the corresponding trace " 1995e8d8bef9SDimitry Andric "plug-in for the current process.", 1996e8d8bef9SDimitry Andric "thread trace start [<trace-options>]") {} 1997e8d8bef9SDimitry Andric 1998e8d8bef9SDimitry Andric protected: 1999fe6060f1SDimitry Andric lldb::CommandObjectSP GetDelegateCommand(Trace &trace) override { 2000fe6060f1SDimitry Andric return trace.GetThreadTraceStartCommand(m_interpreter); 2001e8d8bef9SDimitry Andric } 2002e8d8bef9SDimitry Andric }; 2003e8d8bef9SDimitry Andric 2004e8d8bef9SDimitry Andric // CommandObjectTraceStop 2005e8d8bef9SDimitry Andric 2006fe6060f1SDimitry Andric class CommandObjectTraceStop : public CommandObjectMultipleThreads { 2007e8d8bef9SDimitry Andric public: 2008e8d8bef9SDimitry Andric CommandObjectTraceStop(CommandInterpreter &interpreter) 2009fe6060f1SDimitry Andric : CommandObjectMultipleThreads( 2010e8d8bef9SDimitry Andric interpreter, "thread trace stop", 2011fe6060f1SDimitry Andric "Stop tracing threads, including the ones traced with the " 2012fe6060f1SDimitry Andric "\"process trace start\" command." 2013e8d8bef9SDimitry Andric "Defaults to the current thread. Thread indices can be " 2014fe6060f1SDimitry Andric "specified as arguments.\n Use the thread-index \"all\" to stop " 2015fe6060f1SDimitry Andric "tracing " 2016fe6060f1SDimitry Andric "for all existing threads.", 2017e8d8bef9SDimitry Andric "thread trace stop [<thread-index> <thread-index> ...]", 2018e8d8bef9SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 2019e8d8bef9SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | 2020e8d8bef9SDimitry Andric eCommandProcessMustBeTraced) {} 2021e8d8bef9SDimitry Andric 2022e8d8bef9SDimitry Andric ~CommandObjectTraceStop() override = default; 2023e8d8bef9SDimitry Andric 2024fe6060f1SDimitry Andric bool DoExecuteOnThreads(Args &command, CommandReturnObject &result, 2025fe6060f1SDimitry Andric llvm::ArrayRef<lldb::tid_t> tids) override { 2026fe6060f1SDimitry Andric ProcessSP process_sp = m_exe_ctx.GetProcessSP(); 2027e8d8bef9SDimitry Andric 2028fe6060f1SDimitry Andric TraceSP trace_sp = process_sp->GetTarget().GetTrace(); 2029e8d8bef9SDimitry Andric 2030fe6060f1SDimitry Andric if (llvm::Error err = trace_sp->Stop(tids)) 2031fe6060f1SDimitry Andric result.AppendError(toString(std::move(err))); 2032fe6060f1SDimitry Andric else 2033fe6060f1SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 2034fe6060f1SDimitry Andric 2035fe6060f1SDimitry Andric return result.Succeeded(); 2036e8d8bef9SDimitry Andric } 2037e8d8bef9SDimitry Andric }; 2038e8d8bef9SDimitry Andric 2039e8d8bef9SDimitry Andric // CommandObjectTraceDumpInstructions 2040e8d8bef9SDimitry Andric #define LLDB_OPTIONS_thread_trace_dump_instructions 2041e8d8bef9SDimitry Andric #include "CommandOptions.inc" 2042e8d8bef9SDimitry Andric 2043e8d8bef9SDimitry Andric class CommandObjectTraceDumpInstructions 2044e8d8bef9SDimitry Andric : public CommandObjectIterateOverThreads { 2045e8d8bef9SDimitry Andric public: 2046e8d8bef9SDimitry Andric class CommandOptions : public Options { 2047e8d8bef9SDimitry Andric public: 204804eeddc0SDimitry Andric CommandOptions() { OptionParsingStarting(nullptr); } 2049e8d8bef9SDimitry Andric 2050e8d8bef9SDimitry Andric ~CommandOptions() override = default; 2051e8d8bef9SDimitry Andric 2052e8d8bef9SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 2053e8d8bef9SDimitry Andric ExecutionContext *execution_context) override { 2054e8d8bef9SDimitry Andric Status error; 2055e8d8bef9SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 2056e8d8bef9SDimitry Andric 2057e8d8bef9SDimitry Andric switch (short_option) { 2058e8d8bef9SDimitry Andric case 'c': { 2059e8d8bef9SDimitry Andric int32_t count; 2060e8d8bef9SDimitry Andric if (option_arg.empty() || option_arg.getAsInteger(0, count) || 2061e8d8bef9SDimitry Andric count < 0) 2062e8d8bef9SDimitry Andric error.SetErrorStringWithFormat( 2063e8d8bef9SDimitry Andric "invalid integer value for option '%s'", 2064e8d8bef9SDimitry Andric option_arg.str().c_str()); 2065e8d8bef9SDimitry Andric else 2066e8d8bef9SDimitry Andric m_count = count; 2067e8d8bef9SDimitry Andric break; 2068e8d8bef9SDimitry Andric } 2069fe6060f1SDimitry Andric case 's': { 2070fe6060f1SDimitry Andric int32_t skip; 2071fe6060f1SDimitry Andric if (option_arg.empty() || option_arg.getAsInteger(0, skip) || skip < 0) 2072e8d8bef9SDimitry Andric error.SetErrorStringWithFormat( 2073e8d8bef9SDimitry Andric "invalid integer value for option '%s'", 2074e8d8bef9SDimitry Andric option_arg.str().c_str()); 2075e8d8bef9SDimitry Andric else 2076fe6060f1SDimitry Andric m_skip = skip; 2077e8d8bef9SDimitry Andric break; 2078e8d8bef9SDimitry Andric } 2079e8d8bef9SDimitry Andric case 'r': { 2080e8d8bef9SDimitry Andric m_raw = true; 2081e8d8bef9SDimitry Andric break; 2082e8d8bef9SDimitry Andric } 2083fe6060f1SDimitry Andric case 'f': { 2084fe6060f1SDimitry Andric m_forwards = true; 2085fe6060f1SDimitry Andric break; 2086fe6060f1SDimitry Andric } 2087fe6060f1SDimitry Andric case 't': { 2088fe6060f1SDimitry Andric m_show_tsc = true; 2089fe6060f1SDimitry Andric break; 2090fe6060f1SDimitry Andric } 2091e8d8bef9SDimitry Andric default: 2092e8d8bef9SDimitry Andric llvm_unreachable("Unimplemented option"); 2093e8d8bef9SDimitry Andric } 2094e8d8bef9SDimitry Andric return error; 2095e8d8bef9SDimitry Andric } 2096e8d8bef9SDimitry Andric 2097e8d8bef9SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 2098e8d8bef9SDimitry Andric m_count = kDefaultCount; 2099fe6060f1SDimitry Andric m_skip = 0; 2100e8d8bef9SDimitry Andric m_raw = false; 2101fe6060f1SDimitry Andric m_forwards = false; 2102fe6060f1SDimitry Andric m_show_tsc = false; 2103e8d8bef9SDimitry Andric } 2104e8d8bef9SDimitry Andric 2105e8d8bef9SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 2106e8d8bef9SDimitry Andric return llvm::makeArrayRef(g_thread_trace_dump_instructions_options); 2107e8d8bef9SDimitry Andric } 2108e8d8bef9SDimitry Andric 2109e8d8bef9SDimitry Andric static const size_t kDefaultCount = 20; 2110e8d8bef9SDimitry Andric 2111e8d8bef9SDimitry Andric // Instance variables to hold the values for command options. 2112e8d8bef9SDimitry Andric size_t m_count; 2113fe6060f1SDimitry Andric size_t m_skip; 2114e8d8bef9SDimitry Andric bool m_raw; 2115fe6060f1SDimitry Andric bool m_forwards; 2116fe6060f1SDimitry Andric bool m_show_tsc; 2117e8d8bef9SDimitry Andric }; 2118e8d8bef9SDimitry Andric 2119e8d8bef9SDimitry Andric CommandObjectTraceDumpInstructions(CommandInterpreter &interpreter) 2120e8d8bef9SDimitry Andric : CommandObjectIterateOverThreads( 2121e8d8bef9SDimitry Andric interpreter, "thread trace dump instructions", 2122e8d8bef9SDimitry Andric "Dump the traced instructions for one or more threads. If no " 2123e8d8bef9SDimitry Andric "threads are specified, show the current thread. Use the " 2124e8d8bef9SDimitry Andric "thread-index \"all\" to see all threads.", 2125e8d8bef9SDimitry Andric nullptr, 2126e8d8bef9SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 2127e8d8bef9SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | 2128e8d8bef9SDimitry Andric eCommandProcessMustBeTraced), 212904eeddc0SDimitry Andric m_create_repeat_command_just_invoked(false) {} 2130e8d8bef9SDimitry Andric 2131e8d8bef9SDimitry Andric ~CommandObjectTraceDumpInstructions() override = default; 2132e8d8bef9SDimitry Andric 2133e8d8bef9SDimitry Andric Options *GetOptions() override { return &m_options; } 2134e8d8bef9SDimitry Andric 2135e8d8bef9SDimitry Andric const char *GetRepeatCommand(Args ¤t_command_args, 2136e8d8bef9SDimitry Andric uint32_t index) override { 2137e8d8bef9SDimitry Andric current_command_args.GetCommandString(m_repeat_command); 2138e8d8bef9SDimitry Andric m_create_repeat_command_just_invoked = true; 2139e8d8bef9SDimitry Andric return m_repeat_command.c_str(); 2140e8d8bef9SDimitry Andric } 2141e8d8bef9SDimitry Andric 2142e8d8bef9SDimitry Andric protected: 2143e8d8bef9SDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 2144fe6060f1SDimitry Andric if (!IsRepeatCommand()) 2145fe6060f1SDimitry Andric m_dumpers.clear(); 2146fe6060f1SDimitry Andric 2147e8d8bef9SDimitry Andric bool status = CommandObjectIterateOverThreads::DoExecute(args, result); 2148e8d8bef9SDimitry Andric 2149e8d8bef9SDimitry Andric m_create_repeat_command_just_invoked = false; 2150e8d8bef9SDimitry Andric return status; 2151e8d8bef9SDimitry Andric } 2152e8d8bef9SDimitry Andric 2153e8d8bef9SDimitry Andric bool IsRepeatCommand() { 2154e8d8bef9SDimitry Andric return !m_repeat_command.empty() && !m_create_repeat_command_just_invoked; 2155e8d8bef9SDimitry Andric } 2156e8d8bef9SDimitry Andric 2157e8d8bef9SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 2158fe6060f1SDimitry Andric Stream &s = result.GetOutputStream(); 2159fe6060f1SDimitry Andric 2160e8d8bef9SDimitry Andric const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace(); 2161e8d8bef9SDimitry Andric ThreadSP thread_sp = 2162e8d8bef9SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 2163e8d8bef9SDimitry Andric 2164fe6060f1SDimitry Andric if (!m_dumpers.count(thread_sp->GetID())) { 2165fe6060f1SDimitry Andric lldb::TraceCursorUP cursor_up = trace_sp->GetCursor(*thread_sp); 2166fe6060f1SDimitry Andric // Set up the cursor and return the presentation index of the first 2167fe6060f1SDimitry Andric // instruction to dump after skipping instructions. 2168fe6060f1SDimitry Andric auto setUpCursor = [&]() { 2169fe6060f1SDimitry Andric cursor_up->SetForwards(m_options.m_forwards); 2170fe6060f1SDimitry Andric if (m_options.m_forwards) 2171fe6060f1SDimitry Andric return cursor_up->Seek(m_options.m_skip, TraceCursor::SeekType::Set); 2172fe6060f1SDimitry Andric return -cursor_up->Seek(-m_options.m_skip, TraceCursor::SeekType::End); 2173fe6060f1SDimitry Andric }; 2174fe6060f1SDimitry Andric 2175fe6060f1SDimitry Andric int initial_index = setUpCursor(); 2176fe6060f1SDimitry Andric 2177fe6060f1SDimitry Andric auto dumper = std::make_unique<TraceInstructionDumper>( 2178fe6060f1SDimitry Andric std::move(cursor_up), initial_index, m_options.m_raw, 2179fe6060f1SDimitry Andric m_options.m_show_tsc); 2180fe6060f1SDimitry Andric 2181fe6060f1SDimitry Andric // This happens when the seek value was more than the number of available 2182fe6060f1SDimitry Andric // instructions. 2183fe6060f1SDimitry Andric if (std::abs(initial_index) < (int)m_options.m_skip) 2184fe6060f1SDimitry Andric dumper->SetNoMoreData(); 2185fe6060f1SDimitry Andric 2186fe6060f1SDimitry Andric m_dumpers[thread_sp->GetID()] = std::move(dumper); 2187fe6060f1SDimitry Andric } 2188fe6060f1SDimitry Andric 2189fe6060f1SDimitry Andric m_dumpers[thread_sp->GetID()]->DumpInstructions(s, m_options.m_count); 2190e8d8bef9SDimitry Andric return true; 2191e8d8bef9SDimitry Andric } 2192e8d8bef9SDimitry Andric 2193e8d8bef9SDimitry Andric CommandOptions m_options; 2194e8d8bef9SDimitry Andric 2195e8d8bef9SDimitry Andric // Repeat command helpers 2196e8d8bef9SDimitry Andric std::string m_repeat_command; 2197e8d8bef9SDimitry Andric bool m_create_repeat_command_just_invoked; 2198fe6060f1SDimitry Andric std::map<lldb::tid_t, std::unique_ptr<TraceInstructionDumper>> m_dumpers; 2199fe6060f1SDimitry Andric }; 2200fe6060f1SDimitry Andric 2201fe6060f1SDimitry Andric // CommandObjectTraceDumpInfo 2202fe6060f1SDimitry Andric #define LLDB_OPTIONS_thread_trace_dump_info 2203fe6060f1SDimitry Andric #include "CommandOptions.inc" 2204fe6060f1SDimitry Andric 2205fe6060f1SDimitry Andric class CommandObjectTraceDumpInfo : public CommandObjectIterateOverThreads { 2206fe6060f1SDimitry Andric public: 2207fe6060f1SDimitry Andric class CommandOptions : public Options { 2208fe6060f1SDimitry Andric public: 220904eeddc0SDimitry Andric CommandOptions() { OptionParsingStarting(nullptr); } 2210fe6060f1SDimitry Andric 2211fe6060f1SDimitry Andric ~CommandOptions() override = default; 2212fe6060f1SDimitry Andric 2213fe6060f1SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 2214fe6060f1SDimitry Andric ExecutionContext *execution_context) override { 2215fe6060f1SDimitry Andric Status error; 2216fe6060f1SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 2217fe6060f1SDimitry Andric 2218fe6060f1SDimitry Andric switch (short_option) { 2219fe6060f1SDimitry Andric case 'v': { 2220fe6060f1SDimitry Andric m_verbose = true; 2221fe6060f1SDimitry Andric break; 2222fe6060f1SDimitry Andric } 2223fe6060f1SDimitry Andric default: 2224fe6060f1SDimitry Andric llvm_unreachable("Unimplemented option"); 2225fe6060f1SDimitry Andric } 2226fe6060f1SDimitry Andric return error; 2227fe6060f1SDimitry Andric } 2228fe6060f1SDimitry Andric 2229fe6060f1SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 2230fe6060f1SDimitry Andric m_verbose = false; 2231fe6060f1SDimitry Andric } 2232fe6060f1SDimitry Andric 2233fe6060f1SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 2234fe6060f1SDimitry Andric return llvm::makeArrayRef(g_thread_trace_dump_info_options); 2235fe6060f1SDimitry Andric } 2236fe6060f1SDimitry Andric 2237fe6060f1SDimitry Andric // Instance variables to hold the values for command options. 2238fe6060f1SDimitry Andric bool m_verbose; 2239fe6060f1SDimitry Andric }; 2240fe6060f1SDimitry Andric 2241fe6060f1SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 2242fe6060f1SDimitry Andric Target &target = m_exe_ctx.GetTargetRef(); 2243349cc55cSDimitry Andric result.GetOutputStream().Format("Trace technology: {0}\n", 2244349cc55cSDimitry Andric target.GetTrace()->GetPluginName()); 2245fe6060f1SDimitry Andric return CommandObjectIterateOverThreads::DoExecute(command, result); 2246fe6060f1SDimitry Andric } 2247fe6060f1SDimitry Andric 2248fe6060f1SDimitry Andric CommandObjectTraceDumpInfo(CommandInterpreter &interpreter) 2249fe6060f1SDimitry Andric : CommandObjectIterateOverThreads( 2250fe6060f1SDimitry Andric interpreter, "thread trace dump info", 2251fe6060f1SDimitry Andric "Dump the traced information for one or more threads. If no " 2252fe6060f1SDimitry Andric "threads are specified, show the current thread. Use the " 2253fe6060f1SDimitry Andric "thread-index \"all\" to see all threads.", 2254fe6060f1SDimitry Andric nullptr, 2255fe6060f1SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 2256fe6060f1SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | 225704eeddc0SDimitry Andric eCommandProcessMustBeTraced) {} 2258fe6060f1SDimitry Andric 2259fe6060f1SDimitry Andric ~CommandObjectTraceDumpInfo() override = default; 2260fe6060f1SDimitry Andric 2261fe6060f1SDimitry Andric Options *GetOptions() override { return &m_options; } 2262fe6060f1SDimitry Andric 2263fe6060f1SDimitry Andric protected: 2264fe6060f1SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 2265fe6060f1SDimitry Andric const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace(); 2266fe6060f1SDimitry Andric ThreadSP thread_sp = 2267fe6060f1SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 2268fe6060f1SDimitry Andric trace_sp->DumpTraceInfo(*thread_sp, result.GetOutputStream(), 2269fe6060f1SDimitry Andric m_options.m_verbose); 2270fe6060f1SDimitry Andric return true; 2271fe6060f1SDimitry Andric } 2272fe6060f1SDimitry Andric 2273fe6060f1SDimitry Andric CommandOptions m_options; 2274e8d8bef9SDimitry Andric }; 2275e8d8bef9SDimitry Andric 2276e8d8bef9SDimitry Andric // CommandObjectMultiwordTraceDump 2277e8d8bef9SDimitry Andric class CommandObjectMultiwordTraceDump : public CommandObjectMultiword { 2278e8d8bef9SDimitry Andric public: 2279e8d8bef9SDimitry Andric CommandObjectMultiwordTraceDump(CommandInterpreter &interpreter) 2280e8d8bef9SDimitry Andric : CommandObjectMultiword( 2281e8d8bef9SDimitry Andric interpreter, "dump", 2282e8d8bef9SDimitry Andric "Commands for displaying trace information of the threads " 2283e8d8bef9SDimitry Andric "in the current process.", 2284e8d8bef9SDimitry Andric "thread trace dump <subcommand> [<subcommand objects>]") { 2285e8d8bef9SDimitry Andric LoadSubCommand( 2286e8d8bef9SDimitry Andric "instructions", 2287e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectTraceDumpInstructions(interpreter))); 2288fe6060f1SDimitry Andric LoadSubCommand( 2289fe6060f1SDimitry Andric "info", CommandObjectSP(new CommandObjectTraceDumpInfo(interpreter))); 2290e8d8bef9SDimitry Andric } 2291e8d8bef9SDimitry Andric ~CommandObjectMultiwordTraceDump() override = default; 2292e8d8bef9SDimitry Andric }; 2293e8d8bef9SDimitry Andric 2294e8d8bef9SDimitry Andric // CommandObjectMultiwordTrace 2295e8d8bef9SDimitry Andric class CommandObjectMultiwordTrace : public CommandObjectMultiword { 2296e8d8bef9SDimitry Andric public: 2297e8d8bef9SDimitry Andric CommandObjectMultiwordTrace(CommandInterpreter &interpreter) 2298e8d8bef9SDimitry Andric : CommandObjectMultiword( 2299e8d8bef9SDimitry Andric interpreter, "trace", 2300e8d8bef9SDimitry Andric "Commands for operating on traces of the threads in the current " 2301e8d8bef9SDimitry Andric "process.", 2302e8d8bef9SDimitry Andric "thread trace <subcommand> [<subcommand objects>]") { 2303e8d8bef9SDimitry Andric LoadSubCommand("dump", CommandObjectSP(new CommandObjectMultiwordTraceDump( 2304e8d8bef9SDimitry Andric interpreter))); 2305e8d8bef9SDimitry Andric LoadSubCommand("start", 2306e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectTraceStart(interpreter))); 2307e8d8bef9SDimitry Andric LoadSubCommand("stop", 2308e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectTraceStop(interpreter))); 2309fe6060f1SDimitry Andric LoadSubCommand("export", 2310fe6060f1SDimitry Andric CommandObjectSP(new CommandObjectTraceExport(interpreter))); 2311e8d8bef9SDimitry Andric } 2312e8d8bef9SDimitry Andric 2313e8d8bef9SDimitry Andric ~CommandObjectMultiwordTrace() override = default; 2314e8d8bef9SDimitry Andric }; 2315e8d8bef9SDimitry Andric 2316*0b57cec5SDimitry Andric // CommandObjectMultiwordThread 2317*0b57cec5SDimitry Andric 2318*0b57cec5SDimitry Andric CommandObjectMultiwordThread::CommandObjectMultiwordThread( 2319*0b57cec5SDimitry Andric CommandInterpreter &interpreter) 2320480093f4SDimitry Andric : CommandObjectMultiword(interpreter, "thread", 2321480093f4SDimitry Andric "Commands for operating on " 2322*0b57cec5SDimitry Andric "one or more threads in " 2323*0b57cec5SDimitry Andric "the current process.", 2324*0b57cec5SDimitry Andric "thread <subcommand> [<subcommand-options>]") { 2325*0b57cec5SDimitry Andric LoadSubCommand("backtrace", CommandObjectSP(new CommandObjectThreadBacktrace( 2326*0b57cec5SDimitry Andric interpreter))); 2327*0b57cec5SDimitry Andric LoadSubCommand("continue", 2328*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadContinue(interpreter))); 2329*0b57cec5SDimitry Andric LoadSubCommand("list", 2330*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadList(interpreter))); 2331*0b57cec5SDimitry Andric LoadSubCommand("return", 2332*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadReturn(interpreter))); 2333*0b57cec5SDimitry Andric LoadSubCommand("jump", 2334*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadJump(interpreter))); 2335*0b57cec5SDimitry Andric LoadSubCommand("select", 2336*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadSelect(interpreter))); 2337*0b57cec5SDimitry Andric LoadSubCommand("until", 2338*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadUntil(interpreter))); 2339*0b57cec5SDimitry Andric LoadSubCommand("info", 2340*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadInfo(interpreter))); 2341480093f4SDimitry Andric LoadSubCommand("exception", CommandObjectSP(new CommandObjectThreadException( 2342480093f4SDimitry Andric interpreter))); 2343d56accc7SDimitry Andric LoadSubCommand("siginfo", 2344d56accc7SDimitry Andric CommandObjectSP(new CommandObjectThreadSiginfo(interpreter))); 2345*0b57cec5SDimitry Andric LoadSubCommand("step-in", 2346*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2347*0b57cec5SDimitry Andric interpreter, "thread step-in", 2348*0b57cec5SDimitry Andric "Source level single step, stepping into calls. Defaults " 2349*0b57cec5SDimitry Andric "to current thread unless specified.", 2350*0b57cec5SDimitry Andric nullptr, eStepTypeInto, eStepScopeSource))); 2351*0b57cec5SDimitry Andric 2352*0b57cec5SDimitry Andric LoadSubCommand("step-out", 2353*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2354*0b57cec5SDimitry Andric interpreter, "thread step-out", 2355*0b57cec5SDimitry Andric "Finish executing the current stack frame and stop after " 2356*0b57cec5SDimitry Andric "returning. Defaults to current thread unless specified.", 2357*0b57cec5SDimitry Andric nullptr, eStepTypeOut, eStepScopeSource))); 2358*0b57cec5SDimitry Andric 2359*0b57cec5SDimitry Andric LoadSubCommand("step-over", 2360*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2361*0b57cec5SDimitry Andric interpreter, "thread step-over", 2362*0b57cec5SDimitry Andric "Source level single step, stepping over calls. Defaults " 2363*0b57cec5SDimitry Andric "to current thread unless specified.", 2364*0b57cec5SDimitry Andric nullptr, eStepTypeOver, eStepScopeSource))); 2365*0b57cec5SDimitry Andric 2366*0b57cec5SDimitry Andric LoadSubCommand("step-inst", 2367*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2368*0b57cec5SDimitry Andric interpreter, "thread step-inst", 2369*0b57cec5SDimitry Andric "Instruction level single step, stepping into calls. " 2370*0b57cec5SDimitry Andric "Defaults to current thread unless specified.", 2371*0b57cec5SDimitry Andric nullptr, eStepTypeTrace, eStepScopeInstruction))); 2372*0b57cec5SDimitry Andric 2373*0b57cec5SDimitry Andric LoadSubCommand("step-inst-over", 2374*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2375*0b57cec5SDimitry Andric interpreter, "thread step-inst-over", 2376*0b57cec5SDimitry Andric "Instruction level single step, stepping over calls. " 2377*0b57cec5SDimitry Andric "Defaults to current thread unless specified.", 2378*0b57cec5SDimitry Andric nullptr, eStepTypeTraceOver, eStepScopeInstruction))); 2379*0b57cec5SDimitry Andric 2380*0b57cec5SDimitry Andric LoadSubCommand( 2381*0b57cec5SDimitry Andric "step-scripted", 2382*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2383*0b57cec5SDimitry Andric interpreter, "thread step-scripted", 23849dba64beSDimitry Andric "Step as instructed by the script class passed in the -C option. " 23859dba64beSDimitry Andric "You can also specify a dictionary of key (-k) and value (-v) pairs " 23869dba64beSDimitry Andric "that will be used to populate an SBStructuredData Dictionary, which " 23879dba64beSDimitry Andric "will be passed to the constructor of the class implementing the " 23889dba64beSDimitry Andric "scripted step. See the Python Reference for more details.", 2389*0b57cec5SDimitry Andric nullptr, eStepTypeScripted, eStepScopeSource))); 2390*0b57cec5SDimitry Andric 2391*0b57cec5SDimitry Andric LoadSubCommand("plan", CommandObjectSP(new CommandObjectMultiwordThreadPlan( 2392*0b57cec5SDimitry Andric interpreter))); 2393e8d8bef9SDimitry Andric LoadSubCommand("trace", 2394e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectMultiwordTrace(interpreter))); 2395*0b57cec5SDimitry Andric } 2396*0b57cec5SDimitry Andric 2397*0b57cec5SDimitry Andric CommandObjectMultiwordThread::~CommandObjectMultiwordThread() = default; 2398