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 1323*0b57cec5SDimitry Andric // CommandObjectThreadReturn 1324*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_return 1325*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1326*0b57cec5SDimitry Andric 1327*0b57cec5SDimitry Andric class CommandObjectThreadReturn : public CommandObjectRaw { 1328*0b57cec5SDimitry Andric public: 1329*0b57cec5SDimitry Andric class CommandOptions : public Options { 1330*0b57cec5SDimitry Andric public: 133104eeddc0SDimitry Andric CommandOptions() { 1332*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 1333*0b57cec5SDimitry Andric // () 1334*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 1335*0b57cec5SDimitry Andric } 1336*0b57cec5SDimitry Andric 1337*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1338*0b57cec5SDimitry Andric 1339*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1340*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1341*0b57cec5SDimitry Andric Status error; 1342*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1343*0b57cec5SDimitry Andric 1344*0b57cec5SDimitry Andric switch (short_option) { 1345*0b57cec5SDimitry Andric case 'x': { 1346*0b57cec5SDimitry Andric bool success; 1347*0b57cec5SDimitry Andric bool tmp_value = 1348*0b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, false, &success); 1349*0b57cec5SDimitry Andric if (success) 1350*0b57cec5SDimitry Andric m_from_expression = tmp_value; 1351*0b57cec5SDimitry Andric else { 1352*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 1353*0b57cec5SDimitry Andric "invalid boolean value '%s' for 'x' option", 1354*0b57cec5SDimitry Andric option_arg.str().c_str()); 1355*0b57cec5SDimitry Andric } 1356*0b57cec5SDimitry Andric } break; 1357*0b57cec5SDimitry Andric default: 13589dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1359*0b57cec5SDimitry Andric } 1360*0b57cec5SDimitry Andric return error; 1361*0b57cec5SDimitry Andric } 1362*0b57cec5SDimitry Andric 1363*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1364*0b57cec5SDimitry Andric m_from_expression = false; 1365*0b57cec5SDimitry Andric } 1366*0b57cec5SDimitry Andric 1367*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1368*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_return_options); 1369*0b57cec5SDimitry Andric } 1370*0b57cec5SDimitry Andric 1371fe6060f1SDimitry Andric bool m_from_expression = false; 1372*0b57cec5SDimitry Andric 1373*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 1374*0b57cec5SDimitry Andric }; 1375*0b57cec5SDimitry Andric 1376*0b57cec5SDimitry Andric CommandObjectThreadReturn(CommandInterpreter &interpreter) 1377*0b57cec5SDimitry Andric : CommandObjectRaw(interpreter, "thread return", 1378*0b57cec5SDimitry Andric "Prematurely return from a stack frame, " 1379*0b57cec5SDimitry Andric "short-circuiting execution of newer frames " 1380*0b57cec5SDimitry Andric "and optionally yielding a specified value. Defaults " 1381*0b57cec5SDimitry Andric "to the exiting the current stack " 1382*0b57cec5SDimitry Andric "frame.", 1383*0b57cec5SDimitry Andric "thread return", 1384*0b57cec5SDimitry Andric eCommandRequiresFrame | eCommandTryTargetAPILock | 1385*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 138604eeddc0SDimitry Andric eCommandProcessMustBePaused) { 1387*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1388*0b57cec5SDimitry Andric CommandArgumentData expression_arg; 1389*0b57cec5SDimitry Andric 1390*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1391*0b57cec5SDimitry Andric expression_arg.arg_type = eArgTypeExpression; 1392*0b57cec5SDimitry Andric expression_arg.arg_repetition = eArgRepeatOptional; 1393*0b57cec5SDimitry Andric 1394*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1395*0b57cec5SDimitry Andric // argument entry. 1396*0b57cec5SDimitry Andric arg.push_back(expression_arg); 1397*0b57cec5SDimitry Andric 1398*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1399*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1400*0b57cec5SDimitry Andric } 1401*0b57cec5SDimitry Andric 1402*0b57cec5SDimitry Andric ~CommandObjectThreadReturn() override = default; 1403*0b57cec5SDimitry Andric 1404*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1405*0b57cec5SDimitry Andric 1406*0b57cec5SDimitry Andric protected: 1407*0b57cec5SDimitry Andric bool DoExecute(llvm::StringRef command, 1408*0b57cec5SDimitry Andric CommandReturnObject &result) override { 1409*0b57cec5SDimitry Andric // I am going to handle this by hand, because I don't want you to have to 1410*0b57cec5SDimitry Andric // say: 1411*0b57cec5SDimitry Andric // "thread return -- -5". 1412*0b57cec5SDimitry Andric if (command.startswith("-x")) { 1413*0b57cec5SDimitry Andric if (command.size() != 2U) 1414*0b57cec5SDimitry Andric result.AppendWarning("Return values ignored when returning from user " 1415*0b57cec5SDimitry Andric "called expressions"); 1416*0b57cec5SDimitry Andric 1417*0b57cec5SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 1418*0b57cec5SDimitry Andric Status error; 1419*0b57cec5SDimitry Andric error = thread->UnwindInnermostExpression(); 1420*0b57cec5SDimitry Andric if (!error.Success()) { 1421*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Unwinding expression failed - %s.", 1422*0b57cec5SDimitry Andric error.AsCString()); 1423*0b57cec5SDimitry Andric } else { 1424*0b57cec5SDimitry Andric bool success = 1425*0b57cec5SDimitry Andric thread->SetSelectedFrameByIndexNoisily(0, result.GetOutputStream()); 1426*0b57cec5SDimitry Andric if (success) { 1427*0b57cec5SDimitry Andric m_exe_ctx.SetFrameSP(thread->GetSelectedFrame()); 1428*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1429*0b57cec5SDimitry Andric } else { 1430*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1431*0b57cec5SDimitry Andric "Could not select 0th frame after unwinding expression."); 1432*0b57cec5SDimitry Andric } 1433*0b57cec5SDimitry Andric } 1434*0b57cec5SDimitry Andric return result.Succeeded(); 1435*0b57cec5SDimitry Andric } 1436*0b57cec5SDimitry Andric 1437*0b57cec5SDimitry Andric ValueObjectSP return_valobj_sp; 1438*0b57cec5SDimitry Andric 1439*0b57cec5SDimitry Andric StackFrameSP frame_sp = m_exe_ctx.GetFrameSP(); 1440*0b57cec5SDimitry Andric uint32_t frame_idx = frame_sp->GetFrameIndex(); 1441*0b57cec5SDimitry Andric 1442*0b57cec5SDimitry Andric if (frame_sp->IsInlined()) { 1443*0b57cec5SDimitry Andric result.AppendError("Don't know how to return from inlined frames."); 1444*0b57cec5SDimitry Andric return false; 1445*0b57cec5SDimitry Andric } 1446*0b57cec5SDimitry Andric 1447*0b57cec5SDimitry Andric if (!command.empty()) { 1448*0b57cec5SDimitry Andric Target *target = m_exe_ctx.GetTargetPtr(); 1449*0b57cec5SDimitry Andric EvaluateExpressionOptions options; 1450*0b57cec5SDimitry Andric 1451*0b57cec5SDimitry Andric options.SetUnwindOnError(true); 1452*0b57cec5SDimitry Andric options.SetUseDynamic(eNoDynamicValues); 1453*0b57cec5SDimitry Andric 1454*0b57cec5SDimitry Andric ExpressionResults exe_results = eExpressionSetupError; 1455*0b57cec5SDimitry Andric exe_results = target->EvaluateExpression(command, frame_sp.get(), 1456*0b57cec5SDimitry Andric return_valobj_sp, options); 1457*0b57cec5SDimitry Andric if (exe_results != eExpressionCompleted) { 1458*0b57cec5SDimitry Andric if (return_valobj_sp) 1459*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1460*0b57cec5SDimitry Andric "Error evaluating result expression: %s", 1461*0b57cec5SDimitry Andric return_valobj_sp->GetError().AsCString()); 1462*0b57cec5SDimitry Andric else 1463*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1464*0b57cec5SDimitry Andric "Unknown error evaluating result expression."); 1465*0b57cec5SDimitry Andric return false; 1466*0b57cec5SDimitry Andric } 1467*0b57cec5SDimitry Andric } 1468*0b57cec5SDimitry Andric 1469*0b57cec5SDimitry Andric Status error; 1470*0b57cec5SDimitry Andric ThreadSP thread_sp = m_exe_ctx.GetThreadSP(); 1471*0b57cec5SDimitry Andric const bool broadcast = true; 1472*0b57cec5SDimitry Andric error = thread_sp->ReturnFromFrame(frame_sp, return_valobj_sp, broadcast); 1473*0b57cec5SDimitry Andric if (!error.Success()) { 1474*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1475*0b57cec5SDimitry Andric "Error returning from frame %d of thread %d: %s.", frame_idx, 1476*0b57cec5SDimitry Andric thread_sp->GetIndexID(), error.AsCString()); 1477*0b57cec5SDimitry Andric return false; 1478*0b57cec5SDimitry Andric } 1479*0b57cec5SDimitry Andric 1480*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1481*0b57cec5SDimitry Andric return true; 1482*0b57cec5SDimitry Andric } 1483*0b57cec5SDimitry Andric 1484*0b57cec5SDimitry Andric CommandOptions m_options; 1485*0b57cec5SDimitry Andric }; 1486*0b57cec5SDimitry Andric 1487*0b57cec5SDimitry Andric // CommandObjectThreadJump 1488*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_jump 1489*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1490*0b57cec5SDimitry Andric 1491*0b57cec5SDimitry Andric class CommandObjectThreadJump : public CommandObjectParsed { 1492*0b57cec5SDimitry Andric public: 1493*0b57cec5SDimitry Andric class CommandOptions : public Options { 1494*0b57cec5SDimitry Andric public: 149504eeddc0SDimitry Andric CommandOptions() { OptionParsingStarting(nullptr); } 1496*0b57cec5SDimitry Andric 1497*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1498*0b57cec5SDimitry Andric 1499*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1500*0b57cec5SDimitry Andric m_filenames.Clear(); 1501*0b57cec5SDimitry Andric m_line_num = 0; 1502*0b57cec5SDimitry Andric m_line_offset = 0; 1503*0b57cec5SDimitry Andric m_load_addr = LLDB_INVALID_ADDRESS; 1504*0b57cec5SDimitry Andric m_force = false; 1505*0b57cec5SDimitry Andric } 1506*0b57cec5SDimitry Andric 1507*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1508*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1509*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1510*0b57cec5SDimitry Andric Status error; 1511*0b57cec5SDimitry Andric 1512*0b57cec5SDimitry Andric switch (short_option) { 1513*0b57cec5SDimitry Andric case 'f': 1514*0b57cec5SDimitry Andric m_filenames.AppendIfUnique(FileSpec(option_arg)); 1515*0b57cec5SDimitry Andric if (m_filenames.GetSize() > 1) 1516*0b57cec5SDimitry Andric return Status("only one source file expected."); 1517*0b57cec5SDimitry Andric break; 1518*0b57cec5SDimitry Andric case 'l': 1519*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_line_num)) 1520*0b57cec5SDimitry Andric return Status("invalid line number: '%s'.", option_arg.str().c_str()); 1521*0b57cec5SDimitry Andric break; 1522*0b57cec5SDimitry Andric case 'b': 1523*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_line_offset)) 1524*0b57cec5SDimitry Andric return Status("invalid line offset: '%s'.", option_arg.str().c_str()); 1525*0b57cec5SDimitry Andric break; 1526*0b57cec5SDimitry Andric case 'a': 1527*0b57cec5SDimitry Andric m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg, 1528*0b57cec5SDimitry Andric LLDB_INVALID_ADDRESS, &error); 1529*0b57cec5SDimitry Andric break; 1530*0b57cec5SDimitry Andric case 'r': 1531*0b57cec5SDimitry Andric m_force = true; 1532*0b57cec5SDimitry Andric break; 1533*0b57cec5SDimitry Andric default: 15349dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1535*0b57cec5SDimitry Andric } 1536*0b57cec5SDimitry Andric return error; 1537*0b57cec5SDimitry Andric } 1538*0b57cec5SDimitry Andric 1539*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1540*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_jump_options); 1541*0b57cec5SDimitry Andric } 1542*0b57cec5SDimitry Andric 1543*0b57cec5SDimitry Andric FileSpecList m_filenames; 1544*0b57cec5SDimitry Andric uint32_t m_line_num; 1545*0b57cec5SDimitry Andric int32_t m_line_offset; 1546*0b57cec5SDimitry Andric lldb::addr_t m_load_addr; 1547*0b57cec5SDimitry Andric bool m_force; 1548*0b57cec5SDimitry Andric }; 1549*0b57cec5SDimitry Andric 1550*0b57cec5SDimitry Andric CommandObjectThreadJump(CommandInterpreter &interpreter) 1551*0b57cec5SDimitry Andric : CommandObjectParsed( 1552*0b57cec5SDimitry Andric interpreter, "thread jump", 1553*0b57cec5SDimitry Andric "Sets the program counter to a new address.", "thread jump", 1554*0b57cec5SDimitry Andric eCommandRequiresFrame | eCommandTryTargetAPILock | 155504eeddc0SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} 1556*0b57cec5SDimitry Andric 1557*0b57cec5SDimitry Andric ~CommandObjectThreadJump() override = default; 1558*0b57cec5SDimitry Andric 1559*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1560*0b57cec5SDimitry Andric 1561*0b57cec5SDimitry Andric protected: 1562*0b57cec5SDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 1563*0b57cec5SDimitry Andric RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext(); 1564*0b57cec5SDimitry Andric StackFrame *frame = m_exe_ctx.GetFramePtr(); 1565*0b57cec5SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 1566*0b57cec5SDimitry Andric Target *target = m_exe_ctx.GetTargetPtr(); 1567*0b57cec5SDimitry Andric const SymbolContext &sym_ctx = 1568*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextLineEntry); 1569*0b57cec5SDimitry Andric 1570*0b57cec5SDimitry Andric if (m_options.m_load_addr != LLDB_INVALID_ADDRESS) { 1571*0b57cec5SDimitry Andric // Use this address directly. 1572*0b57cec5SDimitry Andric Address dest = Address(m_options.m_load_addr); 1573*0b57cec5SDimitry Andric 1574*0b57cec5SDimitry Andric lldb::addr_t callAddr = dest.GetCallableLoadAddress(target); 1575*0b57cec5SDimitry Andric if (callAddr == LLDB_INVALID_ADDRESS) { 1576*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid destination address."); 1577*0b57cec5SDimitry Andric return false; 1578*0b57cec5SDimitry Andric } 1579*0b57cec5SDimitry Andric 1580*0b57cec5SDimitry Andric if (!reg_ctx->SetPC(callAddr)) { 1581*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Error changing PC value for thread %d.", 1582*0b57cec5SDimitry Andric thread->GetIndexID()); 1583*0b57cec5SDimitry Andric return false; 1584*0b57cec5SDimitry Andric } 1585*0b57cec5SDimitry Andric } else { 1586*0b57cec5SDimitry Andric // Pick either the absolute line, or work out a relative one. 1587*0b57cec5SDimitry Andric int32_t line = (int32_t)m_options.m_line_num; 1588*0b57cec5SDimitry Andric if (line == 0) 1589*0b57cec5SDimitry Andric line = sym_ctx.line_entry.line + m_options.m_line_offset; 1590*0b57cec5SDimitry Andric 1591*0b57cec5SDimitry Andric // Try the current file, but override if asked. 1592*0b57cec5SDimitry Andric FileSpec file = sym_ctx.line_entry.file; 1593*0b57cec5SDimitry Andric if (m_options.m_filenames.GetSize() == 1) 1594*0b57cec5SDimitry Andric file = m_options.m_filenames.GetFileSpecAtIndex(0); 1595*0b57cec5SDimitry Andric 1596*0b57cec5SDimitry Andric if (!file) { 1597*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1598*0b57cec5SDimitry Andric "No source file available for the current location."); 1599*0b57cec5SDimitry Andric return false; 1600*0b57cec5SDimitry Andric } 1601*0b57cec5SDimitry Andric 1602*0b57cec5SDimitry Andric std::string warnings; 1603*0b57cec5SDimitry Andric Status err = thread->JumpToLine(file, line, m_options.m_force, &warnings); 1604*0b57cec5SDimitry Andric 1605*0b57cec5SDimitry Andric if (err.Fail()) { 1606*0b57cec5SDimitry Andric result.SetError(err); 1607*0b57cec5SDimitry Andric return false; 1608*0b57cec5SDimitry Andric } 1609*0b57cec5SDimitry Andric 1610*0b57cec5SDimitry Andric if (!warnings.empty()) 1611*0b57cec5SDimitry Andric result.AppendWarning(warnings.c_str()); 1612*0b57cec5SDimitry Andric } 1613*0b57cec5SDimitry Andric 1614*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1615*0b57cec5SDimitry Andric return true; 1616*0b57cec5SDimitry Andric } 1617*0b57cec5SDimitry Andric 1618*0b57cec5SDimitry Andric CommandOptions m_options; 1619*0b57cec5SDimitry Andric }; 1620*0b57cec5SDimitry Andric 1621*0b57cec5SDimitry Andric // Next are the subcommands of CommandObjectMultiwordThreadPlan 1622*0b57cec5SDimitry Andric 1623*0b57cec5SDimitry Andric // CommandObjectThreadPlanList 1624*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_plan_list 1625*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1626*0b57cec5SDimitry Andric 1627*0b57cec5SDimitry Andric class CommandObjectThreadPlanList : public CommandObjectIterateOverThreads { 1628*0b57cec5SDimitry Andric public: 1629*0b57cec5SDimitry Andric class CommandOptions : public Options { 1630*0b57cec5SDimitry Andric public: 163104eeddc0SDimitry Andric CommandOptions() { 1632*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 1633*0b57cec5SDimitry Andric // () 1634*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 1635*0b57cec5SDimitry Andric } 1636*0b57cec5SDimitry Andric 1637*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1638*0b57cec5SDimitry Andric 1639*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1640*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1641*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1642*0b57cec5SDimitry Andric 1643*0b57cec5SDimitry Andric switch (short_option) { 1644*0b57cec5SDimitry Andric case 'i': 1645*0b57cec5SDimitry Andric m_internal = true; 1646*0b57cec5SDimitry Andric break; 16475ffd83dbSDimitry Andric case 't': 16485ffd83dbSDimitry Andric lldb::tid_t tid; 16495ffd83dbSDimitry Andric if (option_arg.getAsInteger(0, tid)) 16505ffd83dbSDimitry Andric return Status("invalid tid: '%s'.", option_arg.str().c_str()); 16515ffd83dbSDimitry Andric m_tids.push_back(tid); 16525ffd83dbSDimitry Andric break; 16535ffd83dbSDimitry Andric case 'u': 16545ffd83dbSDimitry Andric m_unreported = false; 16555ffd83dbSDimitry Andric break; 1656*0b57cec5SDimitry Andric case 'v': 1657*0b57cec5SDimitry Andric m_verbose = true; 1658*0b57cec5SDimitry Andric break; 1659*0b57cec5SDimitry Andric default: 16609dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1661*0b57cec5SDimitry Andric } 16625ffd83dbSDimitry Andric return {}; 1663*0b57cec5SDimitry Andric } 1664*0b57cec5SDimitry Andric 1665*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1666*0b57cec5SDimitry Andric m_verbose = false; 1667*0b57cec5SDimitry Andric m_internal = false; 16685ffd83dbSDimitry Andric m_unreported = true; // The variable is "skip unreported" and we want to 16695ffd83dbSDimitry Andric // skip unreported by default. 16705ffd83dbSDimitry Andric m_tids.clear(); 1671*0b57cec5SDimitry Andric } 1672*0b57cec5SDimitry Andric 1673*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1674*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_plan_list_options); 1675*0b57cec5SDimitry Andric } 1676*0b57cec5SDimitry Andric 1677*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 1678*0b57cec5SDimitry Andric bool m_verbose; 1679*0b57cec5SDimitry Andric bool m_internal; 16805ffd83dbSDimitry Andric bool m_unreported; 16815ffd83dbSDimitry Andric std::vector<lldb::tid_t> m_tids; 1682*0b57cec5SDimitry Andric }; 1683*0b57cec5SDimitry Andric 1684*0b57cec5SDimitry Andric CommandObjectThreadPlanList(CommandInterpreter &interpreter) 1685*0b57cec5SDimitry Andric : CommandObjectIterateOverThreads( 1686*0b57cec5SDimitry Andric interpreter, "thread plan list", 1687*0b57cec5SDimitry Andric "Show thread plans for one or more threads. If no threads are " 1688*0b57cec5SDimitry Andric "specified, show the " 1689*0b57cec5SDimitry Andric "current thread. Use the thread-index \"all\" to see all threads.", 1690*0b57cec5SDimitry Andric nullptr, 1691*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 1692*0b57cec5SDimitry Andric eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | 169304eeddc0SDimitry Andric eCommandProcessMustBePaused) {} 1694*0b57cec5SDimitry Andric 1695*0b57cec5SDimitry Andric ~CommandObjectThreadPlanList() override = default; 1696*0b57cec5SDimitry Andric 1697*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1698*0b57cec5SDimitry Andric 16995ffd83dbSDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 17005ffd83dbSDimitry Andric // If we are reporting all threads, dispatch to the Process to do that: 17015ffd83dbSDimitry Andric if (command.GetArgumentCount() == 0 && m_options.m_tids.empty()) { 17025ffd83dbSDimitry Andric Stream &strm = result.GetOutputStream(); 17035ffd83dbSDimitry Andric DescriptionLevel desc_level = m_options.m_verbose 17045ffd83dbSDimitry Andric ? eDescriptionLevelVerbose 17055ffd83dbSDimitry Andric : eDescriptionLevelFull; 17065ffd83dbSDimitry Andric m_exe_ctx.GetProcessPtr()->DumpThreadPlans( 17075ffd83dbSDimitry Andric strm, desc_level, m_options.m_internal, true, m_options.m_unreported); 17085ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 17095ffd83dbSDimitry Andric return true; 17105ffd83dbSDimitry Andric } else { 17115ffd83dbSDimitry Andric // Do any TID's that the user may have specified as TID, then do any 17125ffd83dbSDimitry Andric // Thread Indexes... 17135ffd83dbSDimitry Andric if (!m_options.m_tids.empty()) { 17145ffd83dbSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 17155ffd83dbSDimitry Andric StreamString tmp_strm; 17165ffd83dbSDimitry Andric for (lldb::tid_t tid : m_options.m_tids) { 17175ffd83dbSDimitry Andric bool success = process->DumpThreadPlansForTID( 17185ffd83dbSDimitry Andric tmp_strm, tid, eDescriptionLevelFull, m_options.m_internal, 17195ffd83dbSDimitry Andric true /* condense_trivial */, m_options.m_unreported); 17205ffd83dbSDimitry Andric // If we didn't find a TID, stop here and return an error. 17215ffd83dbSDimitry Andric if (!success) { 1722fe6060f1SDimitry Andric result.AppendError("Error dumping plans:"); 17235ffd83dbSDimitry Andric result.AppendError(tmp_strm.GetString()); 1724*0b57cec5SDimitry Andric return false; 1725*0b57cec5SDimitry Andric } 17265ffd83dbSDimitry Andric // Otherwise, add our data to the output: 17275ffd83dbSDimitry Andric result.GetOutputStream() << tmp_strm.GetString(); 17285ffd83dbSDimitry Andric } 17295ffd83dbSDimitry Andric } 17305ffd83dbSDimitry Andric return CommandObjectIterateOverThreads::DoExecute(command, result); 17315ffd83dbSDimitry Andric } 17325ffd83dbSDimitry Andric } 1733*0b57cec5SDimitry Andric 17345ffd83dbSDimitry Andric protected: 17355ffd83dbSDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 17365ffd83dbSDimitry Andric // If we have already handled this from a -t option, skip it here. 1737e8d8bef9SDimitry Andric if (llvm::is_contained(m_options.m_tids, tid)) 17385ffd83dbSDimitry Andric return true; 17395ffd83dbSDimitry Andric 17405ffd83dbSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1741*0b57cec5SDimitry Andric 1742*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1743*0b57cec5SDimitry Andric DescriptionLevel desc_level = eDescriptionLevelFull; 1744*0b57cec5SDimitry Andric if (m_options.m_verbose) 1745*0b57cec5SDimitry Andric desc_level = eDescriptionLevelVerbose; 1746*0b57cec5SDimitry Andric 17475ffd83dbSDimitry Andric process->DumpThreadPlansForTID(strm, tid, desc_level, m_options.m_internal, 17485ffd83dbSDimitry Andric true /* condense_trivial */, 17495ffd83dbSDimitry Andric m_options.m_unreported); 1750*0b57cec5SDimitry Andric return true; 1751*0b57cec5SDimitry Andric } 1752*0b57cec5SDimitry Andric 1753*0b57cec5SDimitry Andric CommandOptions m_options; 1754*0b57cec5SDimitry Andric }; 1755*0b57cec5SDimitry Andric 1756*0b57cec5SDimitry Andric class CommandObjectThreadPlanDiscard : public CommandObjectParsed { 1757*0b57cec5SDimitry Andric public: 1758*0b57cec5SDimitry Andric CommandObjectThreadPlanDiscard(CommandInterpreter &interpreter) 1759*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "thread plan discard", 1760*0b57cec5SDimitry Andric "Discards thread plans up to and including the " 1761*0b57cec5SDimitry Andric "specified index (see 'thread plan list'.) " 1762*0b57cec5SDimitry Andric "Only user visible plans can be discarded.", 1763*0b57cec5SDimitry Andric nullptr, 1764*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 1765*0b57cec5SDimitry Andric eCommandTryTargetAPILock | 1766*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 1767*0b57cec5SDimitry Andric eCommandProcessMustBePaused) { 1768*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1769*0b57cec5SDimitry Andric CommandArgumentData plan_index_arg; 1770*0b57cec5SDimitry Andric 1771*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1772*0b57cec5SDimitry Andric plan_index_arg.arg_type = eArgTypeUnsignedInteger; 1773*0b57cec5SDimitry Andric plan_index_arg.arg_repetition = eArgRepeatPlain; 1774*0b57cec5SDimitry Andric 1775*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1776*0b57cec5SDimitry Andric // argument entry. 1777*0b57cec5SDimitry Andric arg.push_back(plan_index_arg); 1778*0b57cec5SDimitry Andric 1779*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1780*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1781*0b57cec5SDimitry Andric } 1782*0b57cec5SDimitry Andric 1783*0b57cec5SDimitry Andric ~CommandObjectThreadPlanDiscard() override = default; 1784*0b57cec5SDimitry Andric 1785e8d8bef9SDimitry Andric void 1786e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1787e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1788e8d8bef9SDimitry Andric if (!m_exe_ctx.HasThreadScope() || request.GetCursorIndex()) 1789e8d8bef9SDimitry Andric return; 1790e8d8bef9SDimitry Andric 1791e8d8bef9SDimitry Andric m_exe_ctx.GetThreadPtr()->AutoCompleteThreadPlans(request); 1792e8d8bef9SDimitry Andric } 1793e8d8bef9SDimitry Andric 1794*0b57cec5SDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 1795*0b57cec5SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 1796*0b57cec5SDimitry Andric if (args.GetArgumentCount() != 1) { 1797*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Too many arguments, expected one - the " 1798*0b57cec5SDimitry Andric "thread plan index - but got %zu.", 1799*0b57cec5SDimitry Andric args.GetArgumentCount()); 1800*0b57cec5SDimitry Andric return false; 1801*0b57cec5SDimitry Andric } 1802*0b57cec5SDimitry Andric 18035ffd83dbSDimitry Andric uint32_t thread_plan_idx; 18045ffd83dbSDimitry Andric if (!llvm::to_integer(args.GetArgumentAtIndex(0), thread_plan_idx)) { 1805*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1806*0b57cec5SDimitry Andric "Invalid thread index: \"%s\" - should be unsigned int.", 1807*0b57cec5SDimitry Andric args.GetArgumentAtIndex(0)); 1808*0b57cec5SDimitry Andric return false; 1809*0b57cec5SDimitry Andric } 1810*0b57cec5SDimitry Andric 1811*0b57cec5SDimitry Andric if (thread_plan_idx == 0) { 1812*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1813*0b57cec5SDimitry Andric "You wouldn't really want me to discard the base thread plan."); 1814*0b57cec5SDimitry Andric return false; 1815*0b57cec5SDimitry Andric } 1816*0b57cec5SDimitry Andric 1817*0b57cec5SDimitry Andric if (thread->DiscardUserThreadPlansUpToIndex(thread_plan_idx)) { 1818*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1819*0b57cec5SDimitry Andric return true; 1820*0b57cec5SDimitry Andric } else { 1821*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1822*0b57cec5SDimitry Andric "Could not find User thread plan with index %s.", 1823*0b57cec5SDimitry Andric args.GetArgumentAtIndex(0)); 1824*0b57cec5SDimitry Andric return false; 1825*0b57cec5SDimitry Andric } 1826*0b57cec5SDimitry Andric } 1827*0b57cec5SDimitry Andric }; 1828*0b57cec5SDimitry Andric 18295ffd83dbSDimitry Andric class CommandObjectThreadPlanPrune : public CommandObjectParsed { 18305ffd83dbSDimitry Andric public: 18315ffd83dbSDimitry Andric CommandObjectThreadPlanPrune(CommandInterpreter &interpreter) 18325ffd83dbSDimitry Andric : CommandObjectParsed(interpreter, "thread plan prune", 18335ffd83dbSDimitry Andric "Removes any thread plans associated with " 18345ffd83dbSDimitry Andric "currently unreported threads. " 18355ffd83dbSDimitry Andric "Specify one or more TID's to remove, or if no " 18365ffd83dbSDimitry Andric "TID's are provides, remove threads for all " 18375ffd83dbSDimitry Andric "unreported threads", 18385ffd83dbSDimitry Andric nullptr, 18395ffd83dbSDimitry Andric eCommandRequiresProcess | 18405ffd83dbSDimitry Andric eCommandTryTargetAPILock | 18415ffd83dbSDimitry Andric eCommandProcessMustBeLaunched | 18425ffd83dbSDimitry Andric eCommandProcessMustBePaused) { 18435ffd83dbSDimitry Andric CommandArgumentEntry arg; 18445ffd83dbSDimitry Andric CommandArgumentData tid_arg; 18455ffd83dbSDimitry Andric 18465ffd83dbSDimitry Andric // Define the first (and only) variant of this arg. 18475ffd83dbSDimitry Andric tid_arg.arg_type = eArgTypeThreadID; 18485ffd83dbSDimitry Andric tid_arg.arg_repetition = eArgRepeatStar; 18495ffd83dbSDimitry Andric 18505ffd83dbSDimitry Andric // There is only one variant this argument could be; put it into the 18515ffd83dbSDimitry Andric // argument entry. 18525ffd83dbSDimitry Andric arg.push_back(tid_arg); 18535ffd83dbSDimitry Andric 18545ffd83dbSDimitry Andric // Push the data for the first argument into the m_arguments vector. 18555ffd83dbSDimitry Andric m_arguments.push_back(arg); 18565ffd83dbSDimitry Andric } 18575ffd83dbSDimitry Andric 18585ffd83dbSDimitry Andric ~CommandObjectThreadPlanPrune() override = default; 18595ffd83dbSDimitry Andric 18605ffd83dbSDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 18615ffd83dbSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 18625ffd83dbSDimitry Andric 18635ffd83dbSDimitry Andric if (args.GetArgumentCount() == 0) { 18645ffd83dbSDimitry Andric process->PruneThreadPlans(); 18655ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 18665ffd83dbSDimitry Andric return true; 18675ffd83dbSDimitry Andric } 18685ffd83dbSDimitry Andric 18695ffd83dbSDimitry Andric const size_t num_args = args.GetArgumentCount(); 18705ffd83dbSDimitry Andric 18715ffd83dbSDimitry Andric std::lock_guard<std::recursive_mutex> guard( 18725ffd83dbSDimitry Andric process->GetThreadList().GetMutex()); 18735ffd83dbSDimitry Andric 18745ffd83dbSDimitry Andric for (size_t i = 0; i < num_args; i++) { 18755ffd83dbSDimitry Andric lldb::tid_t tid; 18765ffd83dbSDimitry Andric if (!llvm::to_integer(args.GetArgumentAtIndex(i), tid)) { 18775ffd83dbSDimitry Andric result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n", 18785ffd83dbSDimitry Andric args.GetArgumentAtIndex(i)); 18795ffd83dbSDimitry Andric return false; 18805ffd83dbSDimitry Andric } 18815ffd83dbSDimitry Andric if (!process->PruneThreadPlansForTID(tid)) { 18825ffd83dbSDimitry Andric result.AppendErrorWithFormat("Could not find unreported tid: \"%s\"\n", 18835ffd83dbSDimitry Andric args.GetArgumentAtIndex(i)); 18845ffd83dbSDimitry Andric return false; 18855ffd83dbSDimitry Andric } 18865ffd83dbSDimitry Andric } 18875ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 18885ffd83dbSDimitry Andric return true; 18895ffd83dbSDimitry Andric } 18905ffd83dbSDimitry Andric }; 18915ffd83dbSDimitry Andric 1892*0b57cec5SDimitry Andric // CommandObjectMultiwordThreadPlan 1893*0b57cec5SDimitry Andric 1894*0b57cec5SDimitry Andric class CommandObjectMultiwordThreadPlan : public CommandObjectMultiword { 1895*0b57cec5SDimitry Andric public: 1896*0b57cec5SDimitry Andric CommandObjectMultiwordThreadPlan(CommandInterpreter &interpreter) 1897*0b57cec5SDimitry Andric : CommandObjectMultiword( 1898*0b57cec5SDimitry Andric interpreter, "plan", 1899*0b57cec5SDimitry Andric "Commands for managing thread plans that control execution.", 1900*0b57cec5SDimitry Andric "thread plan <subcommand> [<subcommand objects]") { 1901*0b57cec5SDimitry Andric LoadSubCommand( 1902*0b57cec5SDimitry Andric "list", CommandObjectSP(new CommandObjectThreadPlanList(interpreter))); 1903*0b57cec5SDimitry Andric LoadSubCommand( 1904*0b57cec5SDimitry Andric "discard", 1905*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadPlanDiscard(interpreter))); 19065ffd83dbSDimitry Andric LoadSubCommand( 19075ffd83dbSDimitry Andric "prune", 19085ffd83dbSDimitry Andric CommandObjectSP(new CommandObjectThreadPlanPrune(interpreter))); 1909*0b57cec5SDimitry Andric } 1910*0b57cec5SDimitry Andric 1911*0b57cec5SDimitry Andric ~CommandObjectMultiwordThreadPlan() override = default; 1912*0b57cec5SDimitry Andric }; 1913*0b57cec5SDimitry Andric 1914e8d8bef9SDimitry Andric // Next are the subcommands of CommandObjectMultiwordTrace 1915e8d8bef9SDimitry Andric 1916fe6060f1SDimitry Andric // CommandObjectTraceExport 1917fe6060f1SDimitry Andric 1918fe6060f1SDimitry Andric class CommandObjectTraceExport : public CommandObjectMultiword { 1919fe6060f1SDimitry Andric public: 1920fe6060f1SDimitry Andric CommandObjectTraceExport(CommandInterpreter &interpreter) 1921fe6060f1SDimitry Andric : CommandObjectMultiword( 1922fe6060f1SDimitry Andric interpreter, "trace thread export", 1923fe6060f1SDimitry Andric "Commands for exporting traces of the threads in the current " 1924fe6060f1SDimitry Andric "process to different formats.", 1925fe6060f1SDimitry Andric "thread trace export <export-plugin> [<subcommand objects>]") { 1926fe6060f1SDimitry Andric 1927349cc55cSDimitry Andric unsigned i = 0; 1928349cc55cSDimitry Andric for (llvm::StringRef plugin_name = 1929349cc55cSDimitry Andric PluginManager::GetTraceExporterPluginNameAtIndex(i++); 1930349cc55cSDimitry Andric !plugin_name.empty(); 1931349cc55cSDimitry Andric plugin_name = PluginManager::GetTraceExporterPluginNameAtIndex(i++)) { 1932fe6060f1SDimitry Andric if (ThreadTraceExportCommandCreator command_creator = 1933fe6060f1SDimitry Andric PluginManager::GetThreadTraceExportCommandCreatorAtIndex(i)) { 1934fe6060f1SDimitry Andric LoadSubCommand(plugin_name, command_creator(interpreter)); 1935fe6060f1SDimitry Andric } 1936fe6060f1SDimitry Andric } 1937fe6060f1SDimitry Andric } 1938fe6060f1SDimitry Andric }; 1939fe6060f1SDimitry Andric 1940e8d8bef9SDimitry Andric // CommandObjectTraceStart 1941e8d8bef9SDimitry Andric 1942fe6060f1SDimitry Andric class CommandObjectTraceStart : public CommandObjectTraceProxy { 1943e8d8bef9SDimitry Andric public: 1944e8d8bef9SDimitry Andric CommandObjectTraceStart(CommandInterpreter &interpreter) 1945fe6060f1SDimitry Andric : CommandObjectTraceProxy( 1946fe6060f1SDimitry Andric /*live_debug_session_only=*/true, interpreter, "thread trace start", 1947e8d8bef9SDimitry Andric "Start tracing threads with the corresponding trace " 1948e8d8bef9SDimitry Andric "plug-in for the current process.", 1949e8d8bef9SDimitry Andric "thread trace start [<trace-options>]") {} 1950e8d8bef9SDimitry Andric 1951e8d8bef9SDimitry Andric protected: 1952fe6060f1SDimitry Andric lldb::CommandObjectSP GetDelegateCommand(Trace &trace) override { 1953fe6060f1SDimitry Andric return trace.GetThreadTraceStartCommand(m_interpreter); 1954e8d8bef9SDimitry Andric } 1955e8d8bef9SDimitry Andric }; 1956e8d8bef9SDimitry Andric 1957e8d8bef9SDimitry Andric // CommandObjectTraceStop 1958e8d8bef9SDimitry Andric 1959fe6060f1SDimitry Andric class CommandObjectTraceStop : public CommandObjectMultipleThreads { 1960e8d8bef9SDimitry Andric public: 1961e8d8bef9SDimitry Andric CommandObjectTraceStop(CommandInterpreter &interpreter) 1962fe6060f1SDimitry Andric : CommandObjectMultipleThreads( 1963e8d8bef9SDimitry Andric interpreter, "thread trace stop", 1964fe6060f1SDimitry Andric "Stop tracing threads, including the ones traced with the " 1965fe6060f1SDimitry Andric "\"process trace start\" command." 1966e8d8bef9SDimitry Andric "Defaults to the current thread. Thread indices can be " 1967fe6060f1SDimitry Andric "specified as arguments.\n Use the thread-index \"all\" to stop " 1968fe6060f1SDimitry Andric "tracing " 1969fe6060f1SDimitry Andric "for all existing threads.", 1970e8d8bef9SDimitry Andric "thread trace stop [<thread-index> <thread-index> ...]", 1971e8d8bef9SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1972e8d8bef9SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | 1973e8d8bef9SDimitry Andric eCommandProcessMustBeTraced) {} 1974e8d8bef9SDimitry Andric 1975e8d8bef9SDimitry Andric ~CommandObjectTraceStop() override = default; 1976e8d8bef9SDimitry Andric 1977fe6060f1SDimitry Andric bool DoExecuteOnThreads(Args &command, CommandReturnObject &result, 1978fe6060f1SDimitry Andric llvm::ArrayRef<lldb::tid_t> tids) override { 1979fe6060f1SDimitry Andric ProcessSP process_sp = m_exe_ctx.GetProcessSP(); 1980e8d8bef9SDimitry Andric 1981fe6060f1SDimitry Andric TraceSP trace_sp = process_sp->GetTarget().GetTrace(); 1982e8d8bef9SDimitry Andric 1983fe6060f1SDimitry Andric if (llvm::Error err = trace_sp->Stop(tids)) 1984fe6060f1SDimitry Andric result.AppendError(toString(std::move(err))); 1985fe6060f1SDimitry Andric else 1986fe6060f1SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1987fe6060f1SDimitry Andric 1988fe6060f1SDimitry Andric return result.Succeeded(); 1989e8d8bef9SDimitry Andric } 1990e8d8bef9SDimitry Andric }; 1991e8d8bef9SDimitry Andric 1992e8d8bef9SDimitry Andric // CommandObjectTraceDumpInstructions 1993e8d8bef9SDimitry Andric #define LLDB_OPTIONS_thread_trace_dump_instructions 1994e8d8bef9SDimitry Andric #include "CommandOptions.inc" 1995e8d8bef9SDimitry Andric 1996e8d8bef9SDimitry Andric class CommandObjectTraceDumpInstructions 1997e8d8bef9SDimitry Andric : public CommandObjectIterateOverThreads { 1998e8d8bef9SDimitry Andric public: 1999e8d8bef9SDimitry Andric class CommandOptions : public Options { 2000e8d8bef9SDimitry Andric public: 200104eeddc0SDimitry Andric CommandOptions() { OptionParsingStarting(nullptr); } 2002e8d8bef9SDimitry Andric 2003e8d8bef9SDimitry Andric ~CommandOptions() override = default; 2004e8d8bef9SDimitry Andric 2005e8d8bef9SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 2006e8d8bef9SDimitry Andric ExecutionContext *execution_context) override { 2007e8d8bef9SDimitry Andric Status error; 2008e8d8bef9SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 2009e8d8bef9SDimitry Andric 2010e8d8bef9SDimitry Andric switch (short_option) { 2011e8d8bef9SDimitry Andric case 'c': { 2012e8d8bef9SDimitry Andric int32_t count; 2013e8d8bef9SDimitry Andric if (option_arg.empty() || option_arg.getAsInteger(0, count) || 2014e8d8bef9SDimitry Andric count < 0) 2015e8d8bef9SDimitry Andric error.SetErrorStringWithFormat( 2016e8d8bef9SDimitry Andric "invalid integer value for option '%s'", 2017e8d8bef9SDimitry Andric option_arg.str().c_str()); 2018e8d8bef9SDimitry Andric else 2019e8d8bef9SDimitry Andric m_count = count; 2020e8d8bef9SDimitry Andric break; 2021e8d8bef9SDimitry Andric } 2022fe6060f1SDimitry Andric case 's': { 2023fe6060f1SDimitry Andric int32_t skip; 2024fe6060f1SDimitry Andric if (option_arg.empty() || option_arg.getAsInteger(0, skip) || skip < 0) 2025e8d8bef9SDimitry Andric error.SetErrorStringWithFormat( 2026e8d8bef9SDimitry Andric "invalid integer value for option '%s'", 2027e8d8bef9SDimitry Andric option_arg.str().c_str()); 2028e8d8bef9SDimitry Andric else 2029fe6060f1SDimitry Andric m_skip = skip; 2030e8d8bef9SDimitry Andric break; 2031e8d8bef9SDimitry Andric } 2032e8d8bef9SDimitry Andric case 'r': { 2033e8d8bef9SDimitry Andric m_raw = true; 2034e8d8bef9SDimitry Andric break; 2035e8d8bef9SDimitry Andric } 2036fe6060f1SDimitry Andric case 'f': { 2037fe6060f1SDimitry Andric m_forwards = true; 2038fe6060f1SDimitry Andric break; 2039fe6060f1SDimitry Andric } 2040fe6060f1SDimitry Andric case 't': { 2041fe6060f1SDimitry Andric m_show_tsc = true; 2042fe6060f1SDimitry Andric break; 2043fe6060f1SDimitry Andric } 2044e8d8bef9SDimitry Andric default: 2045e8d8bef9SDimitry Andric llvm_unreachable("Unimplemented option"); 2046e8d8bef9SDimitry Andric } 2047e8d8bef9SDimitry Andric return error; 2048e8d8bef9SDimitry Andric } 2049e8d8bef9SDimitry Andric 2050e8d8bef9SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 2051e8d8bef9SDimitry Andric m_count = kDefaultCount; 2052fe6060f1SDimitry Andric m_skip = 0; 2053e8d8bef9SDimitry Andric m_raw = false; 2054fe6060f1SDimitry Andric m_forwards = false; 2055fe6060f1SDimitry Andric m_show_tsc = false; 2056e8d8bef9SDimitry Andric } 2057e8d8bef9SDimitry Andric 2058e8d8bef9SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 2059e8d8bef9SDimitry Andric return llvm::makeArrayRef(g_thread_trace_dump_instructions_options); 2060e8d8bef9SDimitry Andric } 2061e8d8bef9SDimitry Andric 2062e8d8bef9SDimitry Andric static const size_t kDefaultCount = 20; 2063e8d8bef9SDimitry Andric 2064e8d8bef9SDimitry Andric // Instance variables to hold the values for command options. 2065e8d8bef9SDimitry Andric size_t m_count; 2066fe6060f1SDimitry Andric size_t m_skip; 2067e8d8bef9SDimitry Andric bool m_raw; 2068fe6060f1SDimitry Andric bool m_forwards; 2069fe6060f1SDimitry Andric bool m_show_tsc; 2070e8d8bef9SDimitry Andric }; 2071e8d8bef9SDimitry Andric 2072e8d8bef9SDimitry Andric CommandObjectTraceDumpInstructions(CommandInterpreter &interpreter) 2073e8d8bef9SDimitry Andric : CommandObjectIterateOverThreads( 2074e8d8bef9SDimitry Andric interpreter, "thread trace dump instructions", 2075e8d8bef9SDimitry Andric "Dump the traced instructions for one or more threads. If no " 2076e8d8bef9SDimitry Andric "threads are specified, show the current thread. Use the " 2077e8d8bef9SDimitry Andric "thread-index \"all\" to see all threads.", 2078e8d8bef9SDimitry Andric nullptr, 2079e8d8bef9SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 2080e8d8bef9SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | 2081e8d8bef9SDimitry Andric eCommandProcessMustBeTraced), 208204eeddc0SDimitry Andric m_create_repeat_command_just_invoked(false) {} 2083e8d8bef9SDimitry Andric 2084e8d8bef9SDimitry Andric ~CommandObjectTraceDumpInstructions() override = default; 2085e8d8bef9SDimitry Andric 2086e8d8bef9SDimitry Andric Options *GetOptions() override { return &m_options; } 2087e8d8bef9SDimitry Andric 2088e8d8bef9SDimitry Andric const char *GetRepeatCommand(Args ¤t_command_args, 2089e8d8bef9SDimitry Andric uint32_t index) override { 2090e8d8bef9SDimitry Andric current_command_args.GetCommandString(m_repeat_command); 2091e8d8bef9SDimitry Andric m_create_repeat_command_just_invoked = true; 2092e8d8bef9SDimitry Andric return m_repeat_command.c_str(); 2093e8d8bef9SDimitry Andric } 2094e8d8bef9SDimitry Andric 2095e8d8bef9SDimitry Andric protected: 2096e8d8bef9SDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 2097fe6060f1SDimitry Andric if (!IsRepeatCommand()) 2098fe6060f1SDimitry Andric m_dumpers.clear(); 2099fe6060f1SDimitry Andric 2100e8d8bef9SDimitry Andric bool status = CommandObjectIterateOverThreads::DoExecute(args, result); 2101e8d8bef9SDimitry Andric 2102e8d8bef9SDimitry Andric m_create_repeat_command_just_invoked = false; 2103e8d8bef9SDimitry Andric return status; 2104e8d8bef9SDimitry Andric } 2105e8d8bef9SDimitry Andric 2106e8d8bef9SDimitry Andric bool IsRepeatCommand() { 2107e8d8bef9SDimitry Andric return !m_repeat_command.empty() && !m_create_repeat_command_just_invoked; 2108e8d8bef9SDimitry Andric } 2109e8d8bef9SDimitry Andric 2110e8d8bef9SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 2111fe6060f1SDimitry Andric Stream &s = result.GetOutputStream(); 2112fe6060f1SDimitry Andric 2113e8d8bef9SDimitry Andric const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace(); 2114e8d8bef9SDimitry Andric ThreadSP thread_sp = 2115e8d8bef9SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 2116e8d8bef9SDimitry Andric 2117fe6060f1SDimitry Andric if (!m_dumpers.count(thread_sp->GetID())) { 2118fe6060f1SDimitry Andric lldb::TraceCursorUP cursor_up = trace_sp->GetCursor(*thread_sp); 2119fe6060f1SDimitry Andric // Set up the cursor and return the presentation index of the first 2120fe6060f1SDimitry Andric // instruction to dump after skipping instructions. 2121fe6060f1SDimitry Andric auto setUpCursor = [&]() { 2122fe6060f1SDimitry Andric cursor_up->SetForwards(m_options.m_forwards); 2123fe6060f1SDimitry Andric if (m_options.m_forwards) 2124fe6060f1SDimitry Andric return cursor_up->Seek(m_options.m_skip, TraceCursor::SeekType::Set); 2125fe6060f1SDimitry Andric return -cursor_up->Seek(-m_options.m_skip, TraceCursor::SeekType::End); 2126fe6060f1SDimitry Andric }; 2127fe6060f1SDimitry Andric 2128fe6060f1SDimitry Andric int initial_index = setUpCursor(); 2129fe6060f1SDimitry Andric 2130fe6060f1SDimitry Andric auto dumper = std::make_unique<TraceInstructionDumper>( 2131fe6060f1SDimitry Andric std::move(cursor_up), initial_index, m_options.m_raw, 2132fe6060f1SDimitry Andric m_options.m_show_tsc); 2133fe6060f1SDimitry Andric 2134fe6060f1SDimitry Andric // This happens when the seek value was more than the number of available 2135fe6060f1SDimitry Andric // instructions. 2136fe6060f1SDimitry Andric if (std::abs(initial_index) < (int)m_options.m_skip) 2137fe6060f1SDimitry Andric dumper->SetNoMoreData(); 2138fe6060f1SDimitry Andric 2139fe6060f1SDimitry Andric m_dumpers[thread_sp->GetID()] = std::move(dumper); 2140fe6060f1SDimitry Andric } 2141fe6060f1SDimitry Andric 2142fe6060f1SDimitry Andric m_dumpers[thread_sp->GetID()]->DumpInstructions(s, m_options.m_count); 2143e8d8bef9SDimitry Andric return true; 2144e8d8bef9SDimitry Andric } 2145e8d8bef9SDimitry Andric 2146e8d8bef9SDimitry Andric CommandOptions m_options; 2147e8d8bef9SDimitry Andric 2148e8d8bef9SDimitry Andric // Repeat command helpers 2149e8d8bef9SDimitry Andric std::string m_repeat_command; 2150e8d8bef9SDimitry Andric bool m_create_repeat_command_just_invoked; 2151fe6060f1SDimitry Andric std::map<lldb::tid_t, std::unique_ptr<TraceInstructionDumper>> m_dumpers; 2152fe6060f1SDimitry Andric }; 2153fe6060f1SDimitry Andric 2154fe6060f1SDimitry Andric // CommandObjectTraceDumpInfo 2155fe6060f1SDimitry Andric #define LLDB_OPTIONS_thread_trace_dump_info 2156fe6060f1SDimitry Andric #include "CommandOptions.inc" 2157fe6060f1SDimitry Andric 2158fe6060f1SDimitry Andric class CommandObjectTraceDumpInfo : public CommandObjectIterateOverThreads { 2159fe6060f1SDimitry Andric public: 2160fe6060f1SDimitry Andric class CommandOptions : public Options { 2161fe6060f1SDimitry Andric public: 216204eeddc0SDimitry Andric CommandOptions() { OptionParsingStarting(nullptr); } 2163fe6060f1SDimitry Andric 2164fe6060f1SDimitry Andric ~CommandOptions() override = default; 2165fe6060f1SDimitry Andric 2166fe6060f1SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 2167fe6060f1SDimitry Andric ExecutionContext *execution_context) override { 2168fe6060f1SDimitry Andric Status error; 2169fe6060f1SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 2170fe6060f1SDimitry Andric 2171fe6060f1SDimitry Andric switch (short_option) { 2172fe6060f1SDimitry Andric case 'v': { 2173fe6060f1SDimitry Andric m_verbose = true; 2174fe6060f1SDimitry Andric break; 2175fe6060f1SDimitry Andric } 2176fe6060f1SDimitry Andric default: 2177fe6060f1SDimitry Andric llvm_unreachable("Unimplemented option"); 2178fe6060f1SDimitry Andric } 2179fe6060f1SDimitry Andric return error; 2180fe6060f1SDimitry Andric } 2181fe6060f1SDimitry Andric 2182fe6060f1SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 2183fe6060f1SDimitry Andric m_verbose = false; 2184fe6060f1SDimitry Andric } 2185fe6060f1SDimitry Andric 2186fe6060f1SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 2187fe6060f1SDimitry Andric return llvm::makeArrayRef(g_thread_trace_dump_info_options); 2188fe6060f1SDimitry Andric } 2189fe6060f1SDimitry Andric 2190fe6060f1SDimitry Andric // Instance variables to hold the values for command options. 2191fe6060f1SDimitry Andric bool m_verbose; 2192fe6060f1SDimitry Andric }; 2193fe6060f1SDimitry Andric 2194fe6060f1SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 2195fe6060f1SDimitry Andric Target &target = m_exe_ctx.GetTargetRef(); 2196349cc55cSDimitry Andric result.GetOutputStream().Format("Trace technology: {0}\n", 2197349cc55cSDimitry Andric target.GetTrace()->GetPluginName()); 2198fe6060f1SDimitry Andric return CommandObjectIterateOverThreads::DoExecute(command, result); 2199fe6060f1SDimitry Andric } 2200fe6060f1SDimitry Andric 2201fe6060f1SDimitry Andric CommandObjectTraceDumpInfo(CommandInterpreter &interpreter) 2202fe6060f1SDimitry Andric : CommandObjectIterateOverThreads( 2203fe6060f1SDimitry Andric interpreter, "thread trace dump info", 2204fe6060f1SDimitry Andric "Dump the traced information for one or more threads. If no " 2205fe6060f1SDimitry Andric "threads are specified, show the current thread. Use the " 2206fe6060f1SDimitry Andric "thread-index \"all\" to see all threads.", 2207fe6060f1SDimitry Andric nullptr, 2208fe6060f1SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 2209fe6060f1SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | 221004eeddc0SDimitry Andric eCommandProcessMustBeTraced) {} 2211fe6060f1SDimitry Andric 2212fe6060f1SDimitry Andric ~CommandObjectTraceDumpInfo() override = default; 2213fe6060f1SDimitry Andric 2214fe6060f1SDimitry Andric Options *GetOptions() override { return &m_options; } 2215fe6060f1SDimitry Andric 2216fe6060f1SDimitry Andric protected: 2217fe6060f1SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 2218fe6060f1SDimitry Andric const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace(); 2219fe6060f1SDimitry Andric ThreadSP thread_sp = 2220fe6060f1SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 2221fe6060f1SDimitry Andric trace_sp->DumpTraceInfo(*thread_sp, result.GetOutputStream(), 2222fe6060f1SDimitry Andric m_options.m_verbose); 2223fe6060f1SDimitry Andric return true; 2224fe6060f1SDimitry Andric } 2225fe6060f1SDimitry Andric 2226fe6060f1SDimitry Andric CommandOptions m_options; 2227e8d8bef9SDimitry Andric }; 2228e8d8bef9SDimitry Andric 2229e8d8bef9SDimitry Andric // CommandObjectMultiwordTraceDump 2230e8d8bef9SDimitry Andric class CommandObjectMultiwordTraceDump : public CommandObjectMultiword { 2231e8d8bef9SDimitry Andric public: 2232e8d8bef9SDimitry Andric CommandObjectMultiwordTraceDump(CommandInterpreter &interpreter) 2233e8d8bef9SDimitry Andric : CommandObjectMultiword( 2234e8d8bef9SDimitry Andric interpreter, "dump", 2235e8d8bef9SDimitry Andric "Commands for displaying trace information of the threads " 2236e8d8bef9SDimitry Andric "in the current process.", 2237e8d8bef9SDimitry Andric "thread trace dump <subcommand> [<subcommand objects>]") { 2238e8d8bef9SDimitry Andric LoadSubCommand( 2239e8d8bef9SDimitry Andric "instructions", 2240e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectTraceDumpInstructions(interpreter))); 2241fe6060f1SDimitry Andric LoadSubCommand( 2242fe6060f1SDimitry Andric "info", CommandObjectSP(new CommandObjectTraceDumpInfo(interpreter))); 2243e8d8bef9SDimitry Andric } 2244e8d8bef9SDimitry Andric ~CommandObjectMultiwordTraceDump() override = default; 2245e8d8bef9SDimitry Andric }; 2246e8d8bef9SDimitry Andric 2247e8d8bef9SDimitry Andric // CommandObjectMultiwordTrace 2248e8d8bef9SDimitry Andric class CommandObjectMultiwordTrace : public CommandObjectMultiword { 2249e8d8bef9SDimitry Andric public: 2250e8d8bef9SDimitry Andric CommandObjectMultiwordTrace(CommandInterpreter &interpreter) 2251e8d8bef9SDimitry Andric : CommandObjectMultiword( 2252e8d8bef9SDimitry Andric interpreter, "trace", 2253e8d8bef9SDimitry Andric "Commands for operating on traces of the threads in the current " 2254e8d8bef9SDimitry Andric "process.", 2255e8d8bef9SDimitry Andric "thread trace <subcommand> [<subcommand objects>]") { 2256e8d8bef9SDimitry Andric LoadSubCommand("dump", CommandObjectSP(new CommandObjectMultiwordTraceDump( 2257e8d8bef9SDimitry Andric interpreter))); 2258e8d8bef9SDimitry Andric LoadSubCommand("start", 2259e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectTraceStart(interpreter))); 2260e8d8bef9SDimitry Andric LoadSubCommand("stop", 2261e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectTraceStop(interpreter))); 2262fe6060f1SDimitry Andric LoadSubCommand("export", 2263fe6060f1SDimitry Andric CommandObjectSP(new CommandObjectTraceExport(interpreter))); 2264e8d8bef9SDimitry Andric } 2265e8d8bef9SDimitry Andric 2266e8d8bef9SDimitry Andric ~CommandObjectMultiwordTrace() override = default; 2267e8d8bef9SDimitry Andric }; 2268e8d8bef9SDimitry Andric 2269*0b57cec5SDimitry Andric // CommandObjectMultiwordThread 2270*0b57cec5SDimitry Andric 2271*0b57cec5SDimitry Andric CommandObjectMultiwordThread::CommandObjectMultiwordThread( 2272*0b57cec5SDimitry Andric CommandInterpreter &interpreter) 2273480093f4SDimitry Andric : CommandObjectMultiword(interpreter, "thread", 2274480093f4SDimitry Andric "Commands for operating on " 2275*0b57cec5SDimitry Andric "one or more threads in " 2276*0b57cec5SDimitry Andric "the current process.", 2277*0b57cec5SDimitry Andric "thread <subcommand> [<subcommand-options>]") { 2278*0b57cec5SDimitry Andric LoadSubCommand("backtrace", CommandObjectSP(new CommandObjectThreadBacktrace( 2279*0b57cec5SDimitry Andric interpreter))); 2280*0b57cec5SDimitry Andric LoadSubCommand("continue", 2281*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadContinue(interpreter))); 2282*0b57cec5SDimitry Andric LoadSubCommand("list", 2283*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadList(interpreter))); 2284*0b57cec5SDimitry Andric LoadSubCommand("return", 2285*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadReturn(interpreter))); 2286*0b57cec5SDimitry Andric LoadSubCommand("jump", 2287*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadJump(interpreter))); 2288*0b57cec5SDimitry Andric LoadSubCommand("select", 2289*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadSelect(interpreter))); 2290*0b57cec5SDimitry Andric LoadSubCommand("until", 2291*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadUntil(interpreter))); 2292*0b57cec5SDimitry Andric LoadSubCommand("info", 2293*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadInfo(interpreter))); 2294480093f4SDimitry Andric LoadSubCommand("exception", CommandObjectSP(new CommandObjectThreadException( 2295480093f4SDimitry Andric interpreter))); 2296*0b57cec5SDimitry Andric LoadSubCommand("step-in", 2297*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2298*0b57cec5SDimitry Andric interpreter, "thread step-in", 2299*0b57cec5SDimitry Andric "Source level single step, stepping into calls. Defaults " 2300*0b57cec5SDimitry Andric "to current thread unless specified.", 2301*0b57cec5SDimitry Andric nullptr, eStepTypeInto, eStepScopeSource))); 2302*0b57cec5SDimitry Andric 2303*0b57cec5SDimitry Andric LoadSubCommand("step-out", 2304*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2305*0b57cec5SDimitry Andric interpreter, "thread step-out", 2306*0b57cec5SDimitry Andric "Finish executing the current stack frame and stop after " 2307*0b57cec5SDimitry Andric "returning. Defaults to current thread unless specified.", 2308*0b57cec5SDimitry Andric nullptr, eStepTypeOut, eStepScopeSource))); 2309*0b57cec5SDimitry Andric 2310*0b57cec5SDimitry Andric LoadSubCommand("step-over", 2311*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2312*0b57cec5SDimitry Andric interpreter, "thread step-over", 2313*0b57cec5SDimitry Andric "Source level single step, stepping over calls. Defaults " 2314*0b57cec5SDimitry Andric "to current thread unless specified.", 2315*0b57cec5SDimitry Andric nullptr, eStepTypeOver, eStepScopeSource))); 2316*0b57cec5SDimitry Andric 2317*0b57cec5SDimitry Andric LoadSubCommand("step-inst", 2318*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2319*0b57cec5SDimitry Andric interpreter, "thread step-inst", 2320*0b57cec5SDimitry Andric "Instruction level single step, stepping into calls. " 2321*0b57cec5SDimitry Andric "Defaults to current thread unless specified.", 2322*0b57cec5SDimitry Andric nullptr, eStepTypeTrace, eStepScopeInstruction))); 2323*0b57cec5SDimitry Andric 2324*0b57cec5SDimitry Andric LoadSubCommand("step-inst-over", 2325*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2326*0b57cec5SDimitry Andric interpreter, "thread step-inst-over", 2327*0b57cec5SDimitry Andric "Instruction level single step, stepping over calls. " 2328*0b57cec5SDimitry Andric "Defaults to current thread unless specified.", 2329*0b57cec5SDimitry Andric nullptr, eStepTypeTraceOver, eStepScopeInstruction))); 2330*0b57cec5SDimitry Andric 2331*0b57cec5SDimitry Andric LoadSubCommand( 2332*0b57cec5SDimitry Andric "step-scripted", 2333*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2334*0b57cec5SDimitry Andric interpreter, "thread step-scripted", 23359dba64beSDimitry Andric "Step as instructed by the script class passed in the -C option. " 23369dba64beSDimitry Andric "You can also specify a dictionary of key (-k) and value (-v) pairs " 23379dba64beSDimitry Andric "that will be used to populate an SBStructuredData Dictionary, which " 23389dba64beSDimitry Andric "will be passed to the constructor of the class implementing the " 23399dba64beSDimitry Andric "scripted step. See the Python Reference for more details.", 2340*0b57cec5SDimitry Andric nullptr, eStepTypeScripted, eStepScopeSource))); 2341*0b57cec5SDimitry Andric 2342*0b57cec5SDimitry Andric LoadSubCommand("plan", CommandObjectSP(new CommandObjectMultiwordThreadPlan( 2343*0b57cec5SDimitry Andric interpreter))); 2344e8d8bef9SDimitry Andric LoadSubCommand("trace", 2345e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectMultiwordTrace(interpreter))); 2346*0b57cec5SDimitry Andric } 2347*0b57cec5SDimitry Andric 2348*0b57cec5SDimitry Andric CommandObjectMultiwordThread::~CommandObjectMultiwordThread() = default; 2349