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: 50*0b57cec5SDimitry Andric CommandOptions() : Options() { 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 | 122*0b57cec5SDimitry Andric eCommandProcessMustBePaused), 123*0b57cec5SDimitry Andric m_options() {} 124*0b57cec5SDimitry Andric 125*0b57cec5SDimitry Andric ~CommandObjectThreadBacktrace() override = default; 126*0b57cec5SDimitry Andric 127*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 128*0b57cec5SDimitry Andric 129*0b57cec5SDimitry Andric protected: 130*0b57cec5SDimitry Andric void DoExtendedBacktrace(Thread *thread, CommandReturnObject &result) { 131*0b57cec5SDimitry Andric SystemRuntime *runtime = thread->GetProcess()->GetSystemRuntime(); 132*0b57cec5SDimitry Andric if (runtime) { 133*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 134*0b57cec5SDimitry Andric const std::vector<ConstString> &types = 135*0b57cec5SDimitry Andric runtime->GetExtendedBacktraceTypes(); 136*0b57cec5SDimitry Andric for (auto type : types) { 137*0b57cec5SDimitry Andric ThreadSP ext_thread_sp = runtime->GetExtendedBacktraceThread( 138*0b57cec5SDimitry Andric thread->shared_from_this(), type); 139*0b57cec5SDimitry Andric if (ext_thread_sp && ext_thread_sp->IsValid()) { 140*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 0; 141*0b57cec5SDimitry Andric const bool stop_format = false; 142*0b57cec5SDimitry Andric if (ext_thread_sp->GetStatus(strm, m_options.m_start, 143*0b57cec5SDimitry Andric m_options.m_count, 144480093f4SDimitry Andric num_frames_with_source, stop_format)) { 145*0b57cec5SDimitry Andric DoExtendedBacktrace(ext_thread_sp.get(), result); 146*0b57cec5SDimitry Andric } 147*0b57cec5SDimitry Andric } 148*0b57cec5SDimitry Andric } 149*0b57cec5SDimitry Andric } 150*0b57cec5SDimitry Andric } 151*0b57cec5SDimitry Andric 152*0b57cec5SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 153*0b57cec5SDimitry Andric ThreadSP thread_sp = 154*0b57cec5SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 155*0b57cec5SDimitry Andric if (!thread_sp) { 156*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 157*0b57cec5SDimitry Andric "thread disappeared while computing backtraces: 0x%" PRIx64 "\n", 158*0b57cec5SDimitry Andric tid); 159*0b57cec5SDimitry Andric return false; 160*0b57cec5SDimitry Andric } 161*0b57cec5SDimitry Andric 162*0b57cec5SDimitry Andric Thread *thread = thread_sp.get(); 163*0b57cec5SDimitry Andric 164*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 165*0b57cec5SDimitry Andric 166*0b57cec5SDimitry Andric // Only dump stack info if we processing unique stacks. 167*0b57cec5SDimitry Andric const bool only_stacks = m_unique_stacks; 168*0b57cec5SDimitry Andric 169*0b57cec5SDimitry Andric // Don't show source context when doing backtraces. 170*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 0; 171*0b57cec5SDimitry Andric const bool stop_format = true; 172*0b57cec5SDimitry Andric if (!thread->GetStatus(strm, m_options.m_start, m_options.m_count, 173*0b57cec5SDimitry Andric num_frames_with_source, stop_format, only_stacks)) { 174*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 175*0b57cec5SDimitry Andric "error displaying backtrace for thread: \"0x%4.4x\"\n", 176*0b57cec5SDimitry Andric thread->GetIndexID()); 177*0b57cec5SDimitry Andric return false; 178*0b57cec5SDimitry Andric } 179*0b57cec5SDimitry Andric if (m_options.m_extended_backtrace) { 180*0b57cec5SDimitry Andric DoExtendedBacktrace(thread, result); 181*0b57cec5SDimitry Andric } 182*0b57cec5SDimitry Andric 183*0b57cec5SDimitry Andric return true; 184*0b57cec5SDimitry Andric } 185*0b57cec5SDimitry Andric 186*0b57cec5SDimitry Andric CommandOptions m_options; 187*0b57cec5SDimitry Andric }; 188*0b57cec5SDimitry Andric 189*0b57cec5SDimitry Andric enum StepScope { eStepScopeSource, eStepScopeInstruction }; 190*0b57cec5SDimitry Andric 191*0b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_tri_running_mode[] = { 192*0b57cec5SDimitry Andric {eOnlyThisThread, "this-thread", "Run only this thread"}, 193*0b57cec5SDimitry Andric {eAllThreads, "all-threads", "Run all threads"}, 194*0b57cec5SDimitry Andric {eOnlyDuringStepping, "while-stepping", 195*0b57cec5SDimitry Andric "Run only this thread while stepping"}}; 196*0b57cec5SDimitry Andric 197*0b57cec5SDimitry Andric static constexpr OptionEnumValues TriRunningModes() { 198*0b57cec5SDimitry Andric return OptionEnumValues(g_tri_running_mode); 199*0b57cec5SDimitry Andric } 200*0b57cec5SDimitry Andric 201*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_step_scope 202*0b57cec5SDimitry Andric #include "CommandOptions.inc" 203*0b57cec5SDimitry Andric 2049dba64beSDimitry Andric class ThreadStepScopeOptionGroup : public OptionGroup { 205*0b57cec5SDimitry Andric public: 2069dba64beSDimitry Andric ThreadStepScopeOptionGroup() : OptionGroup() { 207*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 208*0b57cec5SDimitry Andric // () 209*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 210*0b57cec5SDimitry Andric } 211*0b57cec5SDimitry Andric 2129dba64beSDimitry Andric ~ThreadStepScopeOptionGroup() override = default; 2139dba64beSDimitry Andric 2149dba64beSDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 2159dba64beSDimitry Andric return llvm::makeArrayRef(g_thread_step_scope_options); 2169dba64beSDimitry Andric } 217*0b57cec5SDimitry Andric 218*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 219*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 220*0b57cec5SDimitry Andric Status error; 221480093f4SDimitry Andric const int short_option = 222480093f4SDimitry Andric g_thread_step_scope_options[option_idx].short_option; 223*0b57cec5SDimitry Andric 224*0b57cec5SDimitry Andric switch (short_option) { 225*0b57cec5SDimitry Andric case 'a': { 226*0b57cec5SDimitry Andric bool success; 227*0b57cec5SDimitry Andric bool avoid_no_debug = 228*0b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, true, &success); 229*0b57cec5SDimitry Andric if (!success) 230480093f4SDimitry Andric error.SetErrorStringWithFormat("invalid boolean value for option '%c'", 231480093f4SDimitry Andric short_option); 232*0b57cec5SDimitry Andric else { 233480093f4SDimitry Andric m_step_in_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo; 234*0b57cec5SDimitry Andric } 235*0b57cec5SDimitry Andric } break; 236*0b57cec5SDimitry Andric 237*0b57cec5SDimitry Andric case 'A': { 238*0b57cec5SDimitry Andric bool success; 239*0b57cec5SDimitry Andric bool avoid_no_debug = 240*0b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, true, &success); 241*0b57cec5SDimitry Andric if (!success) 242480093f4SDimitry Andric error.SetErrorStringWithFormat("invalid boolean value for option '%c'", 243480093f4SDimitry Andric short_option); 244*0b57cec5SDimitry Andric else { 245480093f4SDimitry Andric m_step_out_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo; 246*0b57cec5SDimitry Andric } 247*0b57cec5SDimitry Andric } break; 248*0b57cec5SDimitry Andric 249*0b57cec5SDimitry Andric case 'c': 250*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_step_count)) 251*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid step count '%s'", 252*0b57cec5SDimitry Andric option_arg.str().c_str()); 253*0b57cec5SDimitry Andric break; 254*0b57cec5SDimitry Andric 255*0b57cec5SDimitry Andric case 'm': { 256*0b57cec5SDimitry Andric auto enum_values = GetDefinitions()[option_idx].enum_values; 257*0b57cec5SDimitry Andric m_run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum( 258*0b57cec5SDimitry Andric option_arg, enum_values, eOnlyDuringStepping, error); 259*0b57cec5SDimitry Andric } break; 260*0b57cec5SDimitry Andric 261*0b57cec5SDimitry Andric case 'e': 262*0b57cec5SDimitry Andric if (option_arg == "block") { 263*0b57cec5SDimitry Andric m_end_line_is_block_end = true; 264*0b57cec5SDimitry Andric break; 265*0b57cec5SDimitry Andric } 266*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_end_line)) 267*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid end line number '%s'", 268*0b57cec5SDimitry Andric option_arg.str().c_str()); 269*0b57cec5SDimitry Andric break; 270*0b57cec5SDimitry Andric 271*0b57cec5SDimitry Andric case 'r': 272*0b57cec5SDimitry Andric m_avoid_regexp.clear(); 2735ffd83dbSDimitry Andric m_avoid_regexp.assign(std::string(option_arg)); 274*0b57cec5SDimitry Andric break; 275*0b57cec5SDimitry Andric 276*0b57cec5SDimitry Andric case 't': 277*0b57cec5SDimitry Andric m_step_in_target.clear(); 2785ffd83dbSDimitry Andric m_step_in_target.assign(std::string(option_arg)); 279*0b57cec5SDimitry Andric break; 280*0b57cec5SDimitry Andric 281*0b57cec5SDimitry Andric default: 2829dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 283*0b57cec5SDimitry Andric } 284*0b57cec5SDimitry Andric return error; 285*0b57cec5SDimitry Andric } 286*0b57cec5SDimitry Andric 287*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 288*0b57cec5SDimitry Andric m_step_in_avoid_no_debug = eLazyBoolCalculate; 289*0b57cec5SDimitry Andric m_step_out_avoid_no_debug = eLazyBoolCalculate; 290*0b57cec5SDimitry Andric m_run_mode = eOnlyDuringStepping; 291*0b57cec5SDimitry Andric 292*0b57cec5SDimitry Andric // Check if we are in Non-Stop mode 293*0b57cec5SDimitry Andric TargetSP target_sp = 294*0b57cec5SDimitry Andric execution_context ? execution_context->GetTargetSP() : TargetSP(); 295e8d8bef9SDimitry Andric ProcessSP process_sp = 296e8d8bef9SDimitry Andric execution_context ? execution_context->GetProcessSP() : ProcessSP(); 297e8d8bef9SDimitry Andric if (process_sp && process_sp->GetSteppingRunsAllThreads()) 298e8d8bef9SDimitry Andric m_run_mode = eAllThreads; 299*0b57cec5SDimitry Andric 300*0b57cec5SDimitry Andric m_avoid_regexp.clear(); 301*0b57cec5SDimitry Andric m_step_in_target.clear(); 302*0b57cec5SDimitry Andric m_step_count = 1; 303*0b57cec5SDimitry Andric m_end_line = LLDB_INVALID_LINE_NUMBER; 304*0b57cec5SDimitry Andric m_end_line_is_block_end = false; 305*0b57cec5SDimitry Andric } 306*0b57cec5SDimitry Andric 307*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 308*0b57cec5SDimitry Andric LazyBool m_step_in_avoid_no_debug; 309*0b57cec5SDimitry Andric LazyBool m_step_out_avoid_no_debug; 310*0b57cec5SDimitry Andric RunMode m_run_mode; 311*0b57cec5SDimitry Andric std::string m_avoid_regexp; 312*0b57cec5SDimitry Andric std::string m_step_in_target; 313*0b57cec5SDimitry Andric uint32_t m_step_count; 314*0b57cec5SDimitry Andric uint32_t m_end_line; 315*0b57cec5SDimitry Andric bool m_end_line_is_block_end; 316*0b57cec5SDimitry Andric }; 317*0b57cec5SDimitry Andric 3189dba64beSDimitry Andric class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed { 3199dba64beSDimitry Andric public: 320*0b57cec5SDimitry Andric CommandObjectThreadStepWithTypeAndScope(CommandInterpreter &interpreter, 321*0b57cec5SDimitry Andric const char *name, const char *help, 322*0b57cec5SDimitry Andric const char *syntax, 323*0b57cec5SDimitry Andric StepType step_type, 324*0b57cec5SDimitry Andric StepScope step_scope) 325*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, name, help, syntax, 326*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 327*0b57cec5SDimitry Andric eCommandTryTargetAPILock | 328*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 329*0b57cec5SDimitry Andric eCommandProcessMustBePaused), 3309dba64beSDimitry Andric m_step_type(step_type), m_step_scope(step_scope), m_options(), 331480093f4SDimitry Andric m_class_options("scripted step") { 332*0b57cec5SDimitry Andric CommandArgumentEntry arg; 333*0b57cec5SDimitry Andric CommandArgumentData thread_id_arg; 334*0b57cec5SDimitry Andric 335*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 336*0b57cec5SDimitry Andric thread_id_arg.arg_type = eArgTypeThreadID; 337*0b57cec5SDimitry Andric thread_id_arg.arg_repetition = eArgRepeatOptional; 338*0b57cec5SDimitry Andric 339*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 340*0b57cec5SDimitry Andric // argument entry. 341*0b57cec5SDimitry Andric arg.push_back(thread_id_arg); 342*0b57cec5SDimitry Andric 343*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 344*0b57cec5SDimitry Andric m_arguments.push_back(arg); 3459dba64beSDimitry Andric 3469dba64beSDimitry Andric if (step_type == eStepTypeScripted) { 347480093f4SDimitry Andric m_all_options.Append(&m_class_options, LLDB_OPT_SET_1 | LLDB_OPT_SET_2, 348480093f4SDimitry Andric LLDB_OPT_SET_1); 3499dba64beSDimitry Andric } 3509dba64beSDimitry Andric m_all_options.Append(&m_options); 3519dba64beSDimitry Andric m_all_options.Finalize(); 352*0b57cec5SDimitry Andric } 353*0b57cec5SDimitry Andric 354*0b57cec5SDimitry Andric ~CommandObjectThreadStepWithTypeAndScope() override = default; 355*0b57cec5SDimitry Andric 356e8d8bef9SDimitry Andric void 357e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 358e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 359e8d8bef9SDimitry Andric if (request.GetCursorIndex()) 360e8d8bef9SDimitry Andric return; 361e8d8bef9SDimitry Andric 362e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 363e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 364e8d8bef9SDimitry Andric request, nullptr); 365e8d8bef9SDimitry Andric } 366e8d8bef9SDimitry Andric 367480093f4SDimitry Andric Options *GetOptions() override { return &m_all_options; } 368*0b57cec5SDimitry Andric 369*0b57cec5SDimitry Andric protected: 370*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 371*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 372*0b57cec5SDimitry Andric bool synchronous_execution = m_interpreter.GetSynchronous(); 373*0b57cec5SDimitry Andric 374*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 375*0b57cec5SDimitry Andric Thread *thread = nullptr; 376*0b57cec5SDimitry Andric 377*0b57cec5SDimitry Andric if (command.GetArgumentCount() == 0) { 378*0b57cec5SDimitry Andric thread = GetDefaultThread(); 379*0b57cec5SDimitry Andric 380*0b57cec5SDimitry Andric if (thread == nullptr) { 381*0b57cec5SDimitry Andric result.AppendError("no selected thread in process"); 382*0b57cec5SDimitry Andric return false; 383*0b57cec5SDimitry Andric } 384*0b57cec5SDimitry Andric } else { 385*0b57cec5SDimitry Andric const char *thread_idx_cstr = command.GetArgumentAtIndex(0); 3865ffd83dbSDimitry Andric uint32_t step_thread_idx; 3875ffd83dbSDimitry Andric 3885ffd83dbSDimitry Andric if (!llvm::to_integer(thread_idx_cstr, step_thread_idx)) { 389*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid thread index '%s'.\n", 390*0b57cec5SDimitry Andric thread_idx_cstr); 391*0b57cec5SDimitry Andric return false; 392*0b57cec5SDimitry Andric } 393*0b57cec5SDimitry Andric thread = 394*0b57cec5SDimitry Andric process->GetThreadList().FindThreadByIndexID(step_thread_idx).get(); 395*0b57cec5SDimitry Andric if (thread == nullptr) { 396*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 397*0b57cec5SDimitry Andric "Thread index %u is out of range (valid values are 0 - %u).\n", 398*0b57cec5SDimitry Andric step_thread_idx, num_threads); 399*0b57cec5SDimitry Andric return false; 400*0b57cec5SDimitry Andric } 401*0b57cec5SDimitry Andric } 402*0b57cec5SDimitry Andric 403*0b57cec5SDimitry Andric if (m_step_type == eStepTypeScripted) { 404480093f4SDimitry Andric if (m_class_options.GetName().empty()) { 405*0b57cec5SDimitry Andric result.AppendErrorWithFormat("empty class name for scripted step."); 406*0b57cec5SDimitry Andric return false; 407*0b57cec5SDimitry Andric } else if (!GetDebugger().GetScriptInterpreter()->CheckObjectExists( 408480093f4SDimitry Andric m_class_options.GetName().c_str())) { 409*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 410*0b57cec5SDimitry Andric "class for scripted step: \"%s\" does not exist.", 411480093f4SDimitry Andric m_class_options.GetName().c_str()); 412*0b57cec5SDimitry Andric return false; 413*0b57cec5SDimitry Andric } 414*0b57cec5SDimitry Andric } 415*0b57cec5SDimitry Andric 416*0b57cec5SDimitry Andric if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER && 417*0b57cec5SDimitry Andric m_step_type != eStepTypeInto) { 418*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 419*0b57cec5SDimitry Andric "end line option is only valid for step into"); 420*0b57cec5SDimitry Andric return false; 421*0b57cec5SDimitry Andric } 422*0b57cec5SDimitry Andric 423*0b57cec5SDimitry Andric const bool abort_other_plans = false; 424*0b57cec5SDimitry Andric const lldb::RunMode stop_other_threads = m_options.m_run_mode; 425*0b57cec5SDimitry Andric 426*0b57cec5SDimitry Andric // This is a bit unfortunate, but not all the commands in this command 427*0b57cec5SDimitry Andric // object support only while stepping, so I use the bool for them. 428*0b57cec5SDimitry Andric bool bool_stop_other_threads; 429*0b57cec5SDimitry Andric if (m_options.m_run_mode == eAllThreads) 430*0b57cec5SDimitry Andric bool_stop_other_threads = false; 431*0b57cec5SDimitry Andric else if (m_options.m_run_mode == eOnlyDuringStepping) 432e8d8bef9SDimitry Andric bool_stop_other_threads = (m_step_type != eStepTypeOut); 433*0b57cec5SDimitry Andric else 434*0b57cec5SDimitry Andric bool_stop_other_threads = true; 435*0b57cec5SDimitry Andric 436*0b57cec5SDimitry Andric ThreadPlanSP new_plan_sp; 437*0b57cec5SDimitry Andric Status new_plan_status; 438*0b57cec5SDimitry Andric 439*0b57cec5SDimitry Andric if (m_step_type == eStepTypeInto) { 440*0b57cec5SDimitry Andric StackFrame *frame = thread->GetStackFrameAtIndex(0).get(); 441*0b57cec5SDimitry Andric assert(frame != nullptr); 442*0b57cec5SDimitry Andric 443*0b57cec5SDimitry Andric if (frame->HasDebugInformation()) { 444*0b57cec5SDimitry Andric AddressRange range; 445*0b57cec5SDimitry Andric SymbolContext sc = frame->GetSymbolContext(eSymbolContextEverything); 446*0b57cec5SDimitry Andric if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER) { 447*0b57cec5SDimitry Andric Status error; 448*0b57cec5SDimitry Andric if (!sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range, 449*0b57cec5SDimitry Andric error)) { 450*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid end-line option: %s.", 451*0b57cec5SDimitry Andric error.AsCString()); 452*0b57cec5SDimitry Andric return false; 453*0b57cec5SDimitry Andric } 454*0b57cec5SDimitry Andric } else if (m_options.m_end_line_is_block_end) { 455*0b57cec5SDimitry Andric Status error; 456*0b57cec5SDimitry Andric Block *block = frame->GetSymbolContext(eSymbolContextBlock).block; 457*0b57cec5SDimitry Andric if (!block) { 458*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Could not find the current block."); 459*0b57cec5SDimitry Andric return false; 460*0b57cec5SDimitry Andric } 461*0b57cec5SDimitry Andric 462*0b57cec5SDimitry Andric AddressRange block_range; 463*0b57cec5SDimitry Andric Address pc_address = frame->GetFrameCodeAddress(); 464*0b57cec5SDimitry Andric block->GetRangeContainingAddress(pc_address, block_range); 465*0b57cec5SDimitry Andric if (!block_range.GetBaseAddress().IsValid()) { 466*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 467*0b57cec5SDimitry Andric "Could not find the current block address."); 468*0b57cec5SDimitry Andric return false; 469*0b57cec5SDimitry Andric } 470*0b57cec5SDimitry Andric lldb::addr_t pc_offset_in_block = 471*0b57cec5SDimitry Andric pc_address.GetFileAddress() - 472*0b57cec5SDimitry Andric block_range.GetBaseAddress().GetFileAddress(); 473*0b57cec5SDimitry Andric lldb::addr_t range_length = 474*0b57cec5SDimitry Andric block_range.GetByteSize() - pc_offset_in_block; 475*0b57cec5SDimitry Andric range = AddressRange(pc_address, range_length); 476*0b57cec5SDimitry Andric } else { 477*0b57cec5SDimitry Andric range = sc.line_entry.range; 478*0b57cec5SDimitry Andric } 479*0b57cec5SDimitry Andric 480*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepInRange( 481*0b57cec5SDimitry Andric abort_other_plans, range, 482*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextEverything), 483*0b57cec5SDimitry Andric m_options.m_step_in_target.c_str(), stop_other_threads, 484*0b57cec5SDimitry Andric new_plan_status, m_options.m_step_in_avoid_no_debug, 485*0b57cec5SDimitry Andric m_options.m_step_out_avoid_no_debug); 486*0b57cec5SDimitry Andric 487*0b57cec5SDimitry Andric if (new_plan_sp && !m_options.m_avoid_regexp.empty()) { 488*0b57cec5SDimitry Andric ThreadPlanStepInRange *step_in_range_plan = 489*0b57cec5SDimitry Andric static_cast<ThreadPlanStepInRange *>(new_plan_sp.get()); 490*0b57cec5SDimitry Andric step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str()); 491*0b57cec5SDimitry Andric } 492*0b57cec5SDimitry Andric } else 493*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 494*0b57cec5SDimitry Andric false, abort_other_plans, bool_stop_other_threads, new_plan_status); 495*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeOver) { 496*0b57cec5SDimitry Andric StackFrame *frame = thread->GetStackFrameAtIndex(0).get(); 497*0b57cec5SDimitry Andric 498*0b57cec5SDimitry Andric if (frame->HasDebugInformation()) 499*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepOverRange( 500*0b57cec5SDimitry Andric abort_other_plans, 501*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextEverything).line_entry, 502*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextEverything), 503*0b57cec5SDimitry Andric stop_other_threads, new_plan_status, 504*0b57cec5SDimitry Andric m_options.m_step_out_avoid_no_debug); 505*0b57cec5SDimitry Andric else 506*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 507*0b57cec5SDimitry Andric true, abort_other_plans, bool_stop_other_threads, new_plan_status); 508*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeTrace) { 509*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 510*0b57cec5SDimitry Andric false, abort_other_plans, bool_stop_other_threads, new_plan_status); 511*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeTraceOver) { 512*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 513*0b57cec5SDimitry Andric true, abort_other_plans, bool_stop_other_threads, new_plan_status); 514*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeOut) { 515*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepOut( 516*0b57cec5SDimitry Andric abort_other_plans, nullptr, false, bool_stop_other_threads, eVoteYes, 517*0b57cec5SDimitry Andric eVoteNoOpinion, thread->GetSelectedFrameIndex(), new_plan_status, 518*0b57cec5SDimitry Andric m_options.m_step_out_avoid_no_debug); 519*0b57cec5SDimitry Andric } else if (m_step_type == eStepTypeScripted) { 520*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepScripted( 521480093f4SDimitry Andric abort_other_plans, m_class_options.GetName().c_str(), 522480093f4SDimitry Andric m_class_options.GetStructuredData(), bool_stop_other_threads, 523480093f4SDimitry Andric new_plan_status); 524*0b57cec5SDimitry Andric } else { 525*0b57cec5SDimitry Andric result.AppendError("step type is not supported"); 526*0b57cec5SDimitry Andric return false; 527*0b57cec5SDimitry Andric } 528*0b57cec5SDimitry Andric 529349cc55cSDimitry Andric // If we got a new plan, then set it to be a controlling plan (User level 530349cc55cSDimitry Andric // Plans should be controlling plans so that they can be interruptible). 531349cc55cSDimitry Andric // Then resume the process. 532*0b57cec5SDimitry Andric 533*0b57cec5SDimitry Andric if (new_plan_sp) { 534349cc55cSDimitry Andric new_plan_sp->SetIsControllingPlan(true); 535*0b57cec5SDimitry Andric new_plan_sp->SetOkayToDiscard(false); 536*0b57cec5SDimitry Andric 537*0b57cec5SDimitry Andric if (m_options.m_step_count > 1) { 538*0b57cec5SDimitry Andric if (!new_plan_sp->SetIterationCount(m_options.m_step_count)) { 539*0b57cec5SDimitry Andric result.AppendWarning( 540*0b57cec5SDimitry Andric "step operation does not support iteration count."); 541*0b57cec5SDimitry Andric } 542*0b57cec5SDimitry Andric } 543*0b57cec5SDimitry Andric 544*0b57cec5SDimitry Andric process->GetThreadList().SetSelectedThreadByID(thread->GetID()); 545*0b57cec5SDimitry Andric 546*0b57cec5SDimitry Andric const uint32_t iohandler_id = process->GetIOHandlerID(); 547*0b57cec5SDimitry Andric 548*0b57cec5SDimitry Andric StreamString stream; 549*0b57cec5SDimitry Andric Status error; 550*0b57cec5SDimitry Andric if (synchronous_execution) 551*0b57cec5SDimitry Andric error = process->ResumeSynchronous(&stream); 552*0b57cec5SDimitry Andric else 553*0b57cec5SDimitry Andric error = process->Resume(); 554*0b57cec5SDimitry Andric 555*0b57cec5SDimitry Andric if (!error.Success()) { 556*0b57cec5SDimitry Andric result.AppendMessage(error.AsCString()); 557*0b57cec5SDimitry Andric return false; 558*0b57cec5SDimitry Andric } 559*0b57cec5SDimitry Andric 560*0b57cec5SDimitry Andric // There is a race condition where this thread will return up the call 561*0b57cec5SDimitry Andric // stack to the main command handler and show an (lldb) prompt before 562*0b57cec5SDimitry Andric // HandlePrivateEvent (from PrivateStateThread) has a chance to call 563*0b57cec5SDimitry Andric // PushProcessIOHandler(). 564*0b57cec5SDimitry Andric process->SyncIOHandler(iohandler_id, std::chrono::seconds(2)); 565*0b57cec5SDimitry Andric 566*0b57cec5SDimitry Andric if (synchronous_execution) { 567*0b57cec5SDimitry Andric // If any state changed events had anything to say, add that to the 568*0b57cec5SDimitry Andric // result 569*0b57cec5SDimitry Andric if (stream.GetSize() > 0) 570*0b57cec5SDimitry Andric result.AppendMessage(stream.GetString()); 571*0b57cec5SDimitry Andric 572*0b57cec5SDimitry Andric process->GetThreadList().SetSelectedThreadByID(thread->GetID()); 573*0b57cec5SDimitry Andric result.SetDidChangeProcessState(true); 574*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 575*0b57cec5SDimitry Andric } else { 576*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessContinuingNoResult); 577*0b57cec5SDimitry Andric } 578*0b57cec5SDimitry Andric } else { 579*0b57cec5SDimitry Andric result.SetError(new_plan_status); 580*0b57cec5SDimitry Andric } 581*0b57cec5SDimitry Andric return result.Succeeded(); 582*0b57cec5SDimitry Andric } 583*0b57cec5SDimitry Andric 584*0b57cec5SDimitry Andric StepType m_step_type; 585*0b57cec5SDimitry Andric StepScope m_step_scope; 5869dba64beSDimitry Andric ThreadStepScopeOptionGroup m_options; 5879dba64beSDimitry Andric OptionGroupPythonClassWithDict m_class_options; 5889dba64beSDimitry Andric OptionGroupOptions m_all_options; 589*0b57cec5SDimitry Andric }; 590*0b57cec5SDimitry Andric 591*0b57cec5SDimitry Andric // CommandObjectThreadContinue 592*0b57cec5SDimitry Andric 593*0b57cec5SDimitry Andric class CommandObjectThreadContinue : public CommandObjectParsed { 594*0b57cec5SDimitry Andric public: 595*0b57cec5SDimitry Andric CommandObjectThreadContinue(CommandInterpreter &interpreter) 596*0b57cec5SDimitry Andric : CommandObjectParsed( 597*0b57cec5SDimitry Andric interpreter, "thread continue", 598*0b57cec5SDimitry Andric "Continue execution of the current target process. One " 599*0b57cec5SDimitry Andric "or more threads may be specified, by default all " 600*0b57cec5SDimitry Andric "threads continue.", 601*0b57cec5SDimitry Andric nullptr, 602*0b57cec5SDimitry Andric eCommandRequiresThread | eCommandTryTargetAPILock | 603*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) { 604*0b57cec5SDimitry Andric CommandArgumentEntry arg; 605*0b57cec5SDimitry Andric CommandArgumentData thread_idx_arg; 606*0b57cec5SDimitry Andric 607*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 608*0b57cec5SDimitry Andric thread_idx_arg.arg_type = eArgTypeThreadIndex; 609*0b57cec5SDimitry Andric thread_idx_arg.arg_repetition = eArgRepeatPlus; 610*0b57cec5SDimitry Andric 611*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 612*0b57cec5SDimitry Andric // argument entry. 613*0b57cec5SDimitry Andric arg.push_back(thread_idx_arg); 614*0b57cec5SDimitry Andric 615*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 616*0b57cec5SDimitry Andric m_arguments.push_back(arg); 617*0b57cec5SDimitry Andric } 618*0b57cec5SDimitry Andric 619*0b57cec5SDimitry Andric ~CommandObjectThreadContinue() override = default; 620*0b57cec5SDimitry Andric 621e8d8bef9SDimitry Andric void 622e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 623e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 624e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 625e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 626e8d8bef9SDimitry Andric request, nullptr); 627e8d8bef9SDimitry Andric } 628e8d8bef9SDimitry Andric 629*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 630*0b57cec5SDimitry Andric bool synchronous_execution = m_interpreter.GetSynchronous(); 631*0b57cec5SDimitry Andric 632*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 633*0b57cec5SDimitry Andric if (process == nullptr) { 634*0b57cec5SDimitry Andric result.AppendError("no process exists. Cannot continue"); 635*0b57cec5SDimitry Andric return false; 636*0b57cec5SDimitry Andric } 637*0b57cec5SDimitry Andric 638*0b57cec5SDimitry Andric StateType state = process->GetState(); 639*0b57cec5SDimitry Andric if ((state == eStateCrashed) || (state == eStateStopped) || 640*0b57cec5SDimitry Andric (state == eStateSuspended)) { 641*0b57cec5SDimitry Andric const size_t argc = command.GetArgumentCount(); 642*0b57cec5SDimitry Andric if (argc > 0) { 643*0b57cec5SDimitry Andric // These two lines appear at the beginning of both blocks in this 644*0b57cec5SDimitry Andric // if..else, but that is because we need to release the lock before 645*0b57cec5SDimitry Andric // calling process->Resume below. 646*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 647*0b57cec5SDimitry Andric process->GetThreadList().GetMutex()); 648*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 649*0b57cec5SDimitry Andric std::vector<Thread *> resume_threads; 650*0b57cec5SDimitry Andric for (auto &entry : command.entries()) { 651*0b57cec5SDimitry Andric uint32_t thread_idx; 6529dba64beSDimitry Andric if (entry.ref().getAsInteger(0, thread_idx)) { 653*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 654*0b57cec5SDimitry Andric "invalid thread index argument: \"%s\".\n", entry.c_str()); 655*0b57cec5SDimitry Andric return false; 656*0b57cec5SDimitry Andric } 657*0b57cec5SDimitry Andric Thread *thread = 658*0b57cec5SDimitry Andric process->GetThreadList().FindThreadByIndexID(thread_idx).get(); 659*0b57cec5SDimitry Andric 660*0b57cec5SDimitry Andric if (thread) { 661*0b57cec5SDimitry Andric resume_threads.push_back(thread); 662*0b57cec5SDimitry Andric } else { 663*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid thread index %u.\n", 664*0b57cec5SDimitry Andric thread_idx); 665*0b57cec5SDimitry Andric return false; 666*0b57cec5SDimitry Andric } 667*0b57cec5SDimitry Andric } 668*0b57cec5SDimitry Andric 669*0b57cec5SDimitry Andric if (resume_threads.empty()) { 670*0b57cec5SDimitry Andric result.AppendError("no valid thread indexes were specified"); 671*0b57cec5SDimitry Andric return false; 672*0b57cec5SDimitry Andric } else { 673*0b57cec5SDimitry Andric if (resume_threads.size() == 1) 674*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Resuming thread: "); 675*0b57cec5SDimitry Andric else 676*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Resuming threads: "); 677*0b57cec5SDimitry Andric 678*0b57cec5SDimitry Andric for (uint32_t idx = 0; idx < num_threads; ++idx) { 679*0b57cec5SDimitry Andric Thread *thread = 680*0b57cec5SDimitry Andric process->GetThreadList().GetThreadAtIndex(idx).get(); 681*0b57cec5SDimitry Andric std::vector<Thread *>::iterator this_thread_pos = 682*0b57cec5SDimitry Andric find(resume_threads.begin(), resume_threads.end(), thread); 683*0b57cec5SDimitry Andric 684*0b57cec5SDimitry Andric if (this_thread_pos != resume_threads.end()) { 685*0b57cec5SDimitry Andric resume_threads.erase(this_thread_pos); 686*0b57cec5SDimitry Andric if (!resume_threads.empty()) 687*0b57cec5SDimitry Andric result.AppendMessageWithFormat("%u, ", thread->GetIndexID()); 688*0b57cec5SDimitry Andric else 689*0b57cec5SDimitry Andric result.AppendMessageWithFormat("%u ", thread->GetIndexID()); 690*0b57cec5SDimitry Andric 691*0b57cec5SDimitry Andric const bool override_suspend = true; 692*0b57cec5SDimitry Andric thread->SetResumeState(eStateRunning, override_suspend); 693*0b57cec5SDimitry Andric } else { 694*0b57cec5SDimitry Andric thread->SetResumeState(eStateSuspended); 695*0b57cec5SDimitry Andric } 696*0b57cec5SDimitry Andric } 697*0b57cec5SDimitry Andric result.AppendMessageWithFormat("in process %" PRIu64 "\n", 698*0b57cec5SDimitry Andric process->GetID()); 699*0b57cec5SDimitry Andric } 700*0b57cec5SDimitry Andric } else { 701*0b57cec5SDimitry Andric // These two lines appear at the beginning of both blocks in this 702*0b57cec5SDimitry Andric // if..else, but that is because we need to release the lock before 703*0b57cec5SDimitry Andric // calling process->Resume below. 704*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 705*0b57cec5SDimitry Andric process->GetThreadList().GetMutex()); 706*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 707*0b57cec5SDimitry Andric Thread *current_thread = GetDefaultThread(); 708*0b57cec5SDimitry Andric if (current_thread == nullptr) { 709*0b57cec5SDimitry Andric result.AppendError("the process doesn't have a current thread"); 710*0b57cec5SDimitry Andric return false; 711*0b57cec5SDimitry Andric } 712*0b57cec5SDimitry Andric // Set the actions that the threads should each take when resuming 713*0b57cec5SDimitry Andric for (uint32_t idx = 0; idx < num_threads; ++idx) { 714*0b57cec5SDimitry Andric Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get(); 715*0b57cec5SDimitry Andric if (thread == current_thread) { 716*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Resuming thread 0x%4.4" PRIx64 717*0b57cec5SDimitry Andric " in process %" PRIu64 "\n", 718*0b57cec5SDimitry Andric thread->GetID(), process->GetID()); 719*0b57cec5SDimitry Andric const bool override_suspend = true; 720*0b57cec5SDimitry Andric thread->SetResumeState(eStateRunning, override_suspend); 721*0b57cec5SDimitry Andric } else { 722*0b57cec5SDimitry Andric thread->SetResumeState(eStateSuspended); 723*0b57cec5SDimitry Andric } 724*0b57cec5SDimitry Andric } 725*0b57cec5SDimitry Andric } 726*0b57cec5SDimitry Andric 727*0b57cec5SDimitry Andric StreamString stream; 728*0b57cec5SDimitry Andric Status error; 729*0b57cec5SDimitry Andric if (synchronous_execution) 730*0b57cec5SDimitry Andric error = process->ResumeSynchronous(&stream); 731*0b57cec5SDimitry Andric else 732*0b57cec5SDimitry Andric error = process->Resume(); 733*0b57cec5SDimitry Andric 734*0b57cec5SDimitry Andric // We should not be holding the thread list lock when we do this. 735*0b57cec5SDimitry Andric if (error.Success()) { 736*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n", 737*0b57cec5SDimitry Andric process->GetID()); 738*0b57cec5SDimitry Andric if (synchronous_execution) { 739*0b57cec5SDimitry Andric // If any state changed events had anything to say, add that to the 740*0b57cec5SDimitry Andric // result 741*0b57cec5SDimitry Andric if (stream.GetSize() > 0) 742*0b57cec5SDimitry Andric result.AppendMessage(stream.GetString()); 743*0b57cec5SDimitry Andric 744*0b57cec5SDimitry Andric result.SetDidChangeProcessState(true); 745*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 746*0b57cec5SDimitry Andric } else { 747*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessContinuingNoResult); 748*0b57cec5SDimitry Andric } 749*0b57cec5SDimitry Andric } else { 750*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to resume process: %s\n", 751*0b57cec5SDimitry Andric error.AsCString()); 752*0b57cec5SDimitry Andric } 753*0b57cec5SDimitry Andric } else { 754*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 755*0b57cec5SDimitry Andric "Process cannot be continued from its current state (%s).\n", 756*0b57cec5SDimitry Andric StateAsCString(state)); 757*0b57cec5SDimitry Andric } 758*0b57cec5SDimitry Andric 759*0b57cec5SDimitry Andric return result.Succeeded(); 760*0b57cec5SDimitry Andric } 761*0b57cec5SDimitry Andric }; 762*0b57cec5SDimitry Andric 763*0b57cec5SDimitry Andric // CommandObjectThreadUntil 764*0b57cec5SDimitry Andric 765*0b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_duo_running_mode[] = { 766*0b57cec5SDimitry Andric {eOnlyThisThread, "this-thread", "Run only this thread"}, 767*0b57cec5SDimitry Andric {eAllThreads, "all-threads", "Run all threads"}}; 768*0b57cec5SDimitry Andric 769*0b57cec5SDimitry Andric static constexpr OptionEnumValues DuoRunningModes() { 770*0b57cec5SDimitry Andric return OptionEnumValues(g_duo_running_mode); 771*0b57cec5SDimitry Andric } 772*0b57cec5SDimitry Andric 773*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_until 774*0b57cec5SDimitry Andric #include "CommandOptions.inc" 775*0b57cec5SDimitry Andric 776*0b57cec5SDimitry Andric class CommandObjectThreadUntil : public CommandObjectParsed { 777*0b57cec5SDimitry Andric public: 778*0b57cec5SDimitry Andric class CommandOptions : public Options { 779*0b57cec5SDimitry Andric public: 780fe6060f1SDimitry Andric uint32_t m_thread_idx = LLDB_INVALID_THREAD_ID; 781fe6060f1SDimitry Andric uint32_t m_frame_idx = LLDB_INVALID_FRAME_ID; 782*0b57cec5SDimitry Andric 783fe6060f1SDimitry Andric CommandOptions() : Options() { 784*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 785*0b57cec5SDimitry Andric // () 786*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 787*0b57cec5SDimitry Andric } 788*0b57cec5SDimitry Andric 789*0b57cec5SDimitry Andric ~CommandOptions() override = default; 790*0b57cec5SDimitry Andric 791*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 792*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 793*0b57cec5SDimitry Andric Status error; 794*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 795*0b57cec5SDimitry Andric 796*0b57cec5SDimitry Andric switch (short_option) { 797*0b57cec5SDimitry Andric case 'a': { 798*0b57cec5SDimitry Andric lldb::addr_t tmp_addr = OptionArgParser::ToAddress( 799*0b57cec5SDimitry Andric execution_context, option_arg, LLDB_INVALID_ADDRESS, &error); 800*0b57cec5SDimitry Andric if (error.Success()) 801*0b57cec5SDimitry Andric m_until_addrs.push_back(tmp_addr); 802*0b57cec5SDimitry Andric } break; 803*0b57cec5SDimitry Andric case 't': 804*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_thread_idx)) { 805*0b57cec5SDimitry Andric m_thread_idx = LLDB_INVALID_INDEX32; 806*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid thread index '%s'", 807*0b57cec5SDimitry Andric option_arg.str().c_str()); 808*0b57cec5SDimitry Andric } 809*0b57cec5SDimitry Andric break; 810*0b57cec5SDimitry Andric case 'f': 811*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_frame_idx)) { 812*0b57cec5SDimitry Andric m_frame_idx = LLDB_INVALID_FRAME_ID; 813*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid frame index '%s'", 814*0b57cec5SDimitry Andric option_arg.str().c_str()); 815*0b57cec5SDimitry Andric } 816*0b57cec5SDimitry Andric break; 817*0b57cec5SDimitry Andric case 'm': { 818*0b57cec5SDimitry Andric auto enum_values = GetDefinitions()[option_idx].enum_values; 819*0b57cec5SDimitry Andric lldb::RunMode run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum( 820*0b57cec5SDimitry Andric option_arg, enum_values, eOnlyDuringStepping, error); 821*0b57cec5SDimitry Andric 822*0b57cec5SDimitry Andric if (error.Success()) { 823*0b57cec5SDimitry Andric if (run_mode == eAllThreads) 824*0b57cec5SDimitry Andric m_stop_others = false; 825*0b57cec5SDimitry Andric else 826*0b57cec5SDimitry Andric m_stop_others = true; 827*0b57cec5SDimitry Andric } 828*0b57cec5SDimitry Andric } break; 829*0b57cec5SDimitry Andric default: 8309dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 831*0b57cec5SDimitry Andric } 832*0b57cec5SDimitry Andric return error; 833*0b57cec5SDimitry Andric } 834*0b57cec5SDimitry Andric 835*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 836*0b57cec5SDimitry Andric m_thread_idx = LLDB_INVALID_THREAD_ID; 837*0b57cec5SDimitry Andric m_frame_idx = 0; 838*0b57cec5SDimitry Andric m_stop_others = false; 839*0b57cec5SDimitry Andric m_until_addrs.clear(); 840*0b57cec5SDimitry Andric } 841*0b57cec5SDimitry Andric 842*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 843*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_until_options); 844*0b57cec5SDimitry Andric } 845*0b57cec5SDimitry Andric 846*0b57cec5SDimitry Andric uint32_t m_step_thread_idx; 847*0b57cec5SDimitry Andric bool m_stop_others; 848*0b57cec5SDimitry Andric std::vector<lldb::addr_t> m_until_addrs; 849*0b57cec5SDimitry Andric 850*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 851*0b57cec5SDimitry Andric }; 852*0b57cec5SDimitry Andric 853*0b57cec5SDimitry Andric CommandObjectThreadUntil(CommandInterpreter &interpreter) 854*0b57cec5SDimitry Andric : CommandObjectParsed( 855*0b57cec5SDimitry Andric interpreter, "thread until", 856*0b57cec5SDimitry Andric "Continue until a line number or address is reached by the " 857*0b57cec5SDimitry Andric "current or specified thread. Stops when returning from " 858*0b57cec5SDimitry Andric "the current function as a safety measure. " 859480093f4SDimitry Andric "The target line number(s) are given as arguments, and if more " 860480093f4SDimitry Andric "than one" 861*0b57cec5SDimitry Andric " is provided, stepping will stop when the first one is hit.", 862*0b57cec5SDimitry Andric nullptr, 863*0b57cec5SDimitry Andric eCommandRequiresThread | eCommandTryTargetAPILock | 864*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 865*0b57cec5SDimitry Andric m_options() { 866*0b57cec5SDimitry Andric CommandArgumentEntry arg; 867*0b57cec5SDimitry Andric CommandArgumentData line_num_arg; 868*0b57cec5SDimitry Andric 869*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 870*0b57cec5SDimitry Andric line_num_arg.arg_type = eArgTypeLineNum; 871*0b57cec5SDimitry Andric line_num_arg.arg_repetition = eArgRepeatPlain; 872*0b57cec5SDimitry Andric 873*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 874*0b57cec5SDimitry Andric // argument entry. 875*0b57cec5SDimitry Andric arg.push_back(line_num_arg); 876*0b57cec5SDimitry Andric 877*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 878*0b57cec5SDimitry Andric m_arguments.push_back(arg); 879*0b57cec5SDimitry Andric } 880*0b57cec5SDimitry Andric 881*0b57cec5SDimitry Andric ~CommandObjectThreadUntil() override = default; 882*0b57cec5SDimitry Andric 883*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 884*0b57cec5SDimitry Andric 885*0b57cec5SDimitry Andric protected: 886*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 887*0b57cec5SDimitry Andric bool synchronous_execution = m_interpreter.GetSynchronous(); 888*0b57cec5SDimitry Andric 8899dba64beSDimitry Andric Target *target = &GetSelectedTarget(); 890*0b57cec5SDimitry Andric 891*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 892*0b57cec5SDimitry Andric if (process == nullptr) { 893*0b57cec5SDimitry Andric result.AppendError("need a valid process to step"); 894*0b57cec5SDimitry Andric } else { 895*0b57cec5SDimitry Andric Thread *thread = nullptr; 896*0b57cec5SDimitry Andric std::vector<uint32_t> line_numbers; 897*0b57cec5SDimitry Andric 898*0b57cec5SDimitry Andric if (command.GetArgumentCount() >= 1) { 899*0b57cec5SDimitry Andric size_t num_args = command.GetArgumentCount(); 900*0b57cec5SDimitry Andric for (size_t i = 0; i < num_args; i++) { 901*0b57cec5SDimitry Andric uint32_t line_number; 9025ffd83dbSDimitry Andric if (!llvm::to_integer(command.GetArgumentAtIndex(i), line_number)) { 903*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid line number: '%s'.\n", 904*0b57cec5SDimitry Andric command.GetArgumentAtIndex(i)); 905*0b57cec5SDimitry Andric return false; 906*0b57cec5SDimitry Andric } else 907*0b57cec5SDimitry Andric line_numbers.push_back(line_number); 908*0b57cec5SDimitry Andric } 909*0b57cec5SDimitry Andric } else if (m_options.m_until_addrs.empty()) { 910*0b57cec5SDimitry Andric result.AppendErrorWithFormat("No line number or address provided:\n%s", 911*0b57cec5SDimitry Andric GetSyntax().str().c_str()); 912*0b57cec5SDimitry Andric return false; 913*0b57cec5SDimitry Andric } 914*0b57cec5SDimitry Andric 915*0b57cec5SDimitry Andric if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID) { 916*0b57cec5SDimitry Andric thread = GetDefaultThread(); 917*0b57cec5SDimitry Andric } else { 918*0b57cec5SDimitry Andric thread = process->GetThreadList() 919*0b57cec5SDimitry Andric .FindThreadByIndexID(m_options.m_thread_idx) 920*0b57cec5SDimitry Andric .get(); 921*0b57cec5SDimitry Andric } 922*0b57cec5SDimitry Andric 923*0b57cec5SDimitry Andric if (thread == nullptr) { 924*0b57cec5SDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize(); 925*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 926*0b57cec5SDimitry Andric "Thread index %u is out of range (valid values are 0 - %u).\n", 927*0b57cec5SDimitry Andric m_options.m_thread_idx, num_threads); 928*0b57cec5SDimitry Andric return false; 929*0b57cec5SDimitry Andric } 930*0b57cec5SDimitry Andric 931*0b57cec5SDimitry Andric const bool abort_other_plans = false; 932*0b57cec5SDimitry Andric 933*0b57cec5SDimitry Andric StackFrame *frame = 934*0b57cec5SDimitry Andric thread->GetStackFrameAtIndex(m_options.m_frame_idx).get(); 935*0b57cec5SDimitry Andric if (frame == nullptr) { 936*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 937*0b57cec5SDimitry Andric "Frame index %u is out of range for thread %u.\n", 938*0b57cec5SDimitry Andric m_options.m_frame_idx, m_options.m_thread_idx); 939*0b57cec5SDimitry Andric return false; 940*0b57cec5SDimitry Andric } 941*0b57cec5SDimitry Andric 942*0b57cec5SDimitry Andric ThreadPlanSP new_plan_sp; 943*0b57cec5SDimitry Andric Status new_plan_status; 944*0b57cec5SDimitry Andric 945*0b57cec5SDimitry Andric if (frame->HasDebugInformation()) { 946*0b57cec5SDimitry Andric // Finally we got here... Translate the given line number to a bunch 947*0b57cec5SDimitry Andric // of addresses: 948*0b57cec5SDimitry Andric SymbolContext sc(frame->GetSymbolContext(eSymbolContextCompUnit)); 949*0b57cec5SDimitry Andric LineTable *line_table = nullptr; 950*0b57cec5SDimitry Andric if (sc.comp_unit) 951*0b57cec5SDimitry Andric line_table = sc.comp_unit->GetLineTable(); 952*0b57cec5SDimitry Andric 953*0b57cec5SDimitry Andric if (line_table == nullptr) { 954*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to resolve the line table for " 955*0b57cec5SDimitry Andric "frame %u of thread index %u.\n", 956*0b57cec5SDimitry Andric m_options.m_frame_idx, 957*0b57cec5SDimitry Andric m_options.m_thread_idx); 958*0b57cec5SDimitry Andric return false; 959*0b57cec5SDimitry Andric } 960*0b57cec5SDimitry Andric 961*0b57cec5SDimitry Andric LineEntry function_start; 962*0b57cec5SDimitry Andric uint32_t index_ptr = 0, end_ptr; 963*0b57cec5SDimitry Andric std::vector<addr_t> address_list; 964*0b57cec5SDimitry Andric 965*0b57cec5SDimitry Andric // Find the beginning & end index of the 966*0b57cec5SDimitry Andric AddressRange fun_addr_range = sc.function->GetAddressRange(); 967*0b57cec5SDimitry Andric Address fun_start_addr = fun_addr_range.GetBaseAddress(); 968*0b57cec5SDimitry Andric line_table->FindLineEntryByAddress(fun_start_addr, function_start, 969*0b57cec5SDimitry Andric &index_ptr); 970*0b57cec5SDimitry Andric 971*0b57cec5SDimitry Andric Address fun_end_addr(fun_start_addr.GetSection(), 972*0b57cec5SDimitry Andric fun_start_addr.GetOffset() + 973*0b57cec5SDimitry Andric fun_addr_range.GetByteSize()); 974*0b57cec5SDimitry Andric 975*0b57cec5SDimitry Andric bool all_in_function = true; 976*0b57cec5SDimitry Andric 977*0b57cec5SDimitry Andric line_table->FindLineEntryByAddress(fun_end_addr, function_start, 978*0b57cec5SDimitry Andric &end_ptr); 979*0b57cec5SDimitry Andric 980*0b57cec5SDimitry Andric for (uint32_t line_number : line_numbers) { 981*0b57cec5SDimitry Andric uint32_t start_idx_ptr = index_ptr; 982*0b57cec5SDimitry Andric while (start_idx_ptr <= end_ptr) { 983*0b57cec5SDimitry Andric LineEntry line_entry; 984*0b57cec5SDimitry Andric const bool exact = false; 985*0b57cec5SDimitry Andric start_idx_ptr = sc.comp_unit->FindLineEntry( 986480093f4SDimitry Andric start_idx_ptr, line_number, nullptr, exact, &line_entry); 987*0b57cec5SDimitry Andric if (start_idx_ptr == UINT32_MAX) 988*0b57cec5SDimitry Andric break; 989*0b57cec5SDimitry Andric 990*0b57cec5SDimitry Andric addr_t address = 991*0b57cec5SDimitry Andric line_entry.range.GetBaseAddress().GetLoadAddress(target); 992*0b57cec5SDimitry Andric if (address != LLDB_INVALID_ADDRESS) { 993*0b57cec5SDimitry Andric if (fun_addr_range.ContainsLoadAddress(address, target)) 994*0b57cec5SDimitry Andric address_list.push_back(address); 995*0b57cec5SDimitry Andric else 996*0b57cec5SDimitry Andric all_in_function = false; 997*0b57cec5SDimitry Andric } 998*0b57cec5SDimitry Andric start_idx_ptr++; 999*0b57cec5SDimitry Andric } 1000*0b57cec5SDimitry Andric } 1001*0b57cec5SDimitry Andric 1002*0b57cec5SDimitry Andric for (lldb::addr_t address : m_options.m_until_addrs) { 1003*0b57cec5SDimitry Andric if (fun_addr_range.ContainsLoadAddress(address, target)) 1004*0b57cec5SDimitry Andric address_list.push_back(address); 1005*0b57cec5SDimitry Andric else 1006*0b57cec5SDimitry Andric all_in_function = false; 1007*0b57cec5SDimitry Andric } 1008*0b57cec5SDimitry Andric 1009*0b57cec5SDimitry Andric if (address_list.empty()) { 1010*0b57cec5SDimitry Andric if (all_in_function) 1011*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1012*0b57cec5SDimitry Andric "No line entries matching until target.\n"); 1013*0b57cec5SDimitry Andric else 1014*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1015*0b57cec5SDimitry Andric "Until target outside of the current function.\n"); 1016*0b57cec5SDimitry Andric 1017*0b57cec5SDimitry Andric return false; 1018*0b57cec5SDimitry Andric } 1019*0b57cec5SDimitry Andric 1020*0b57cec5SDimitry Andric new_plan_sp = thread->QueueThreadPlanForStepUntil( 1021*0b57cec5SDimitry Andric abort_other_plans, &address_list.front(), address_list.size(), 1022*0b57cec5SDimitry Andric m_options.m_stop_others, m_options.m_frame_idx, new_plan_status); 1023*0b57cec5SDimitry Andric if (new_plan_sp) { 1024349cc55cSDimitry Andric // User level plans should be controlling plans so they can be 1025349cc55cSDimitry Andric // interrupted 1026*0b57cec5SDimitry Andric // (e.g. by hitting a breakpoint) and other plans executed by the 1027*0b57cec5SDimitry Andric // user (stepping around the breakpoint) and then a "continue" will 1028*0b57cec5SDimitry Andric // resume the original plan. 1029349cc55cSDimitry Andric new_plan_sp->SetIsControllingPlan(true); 1030*0b57cec5SDimitry Andric new_plan_sp->SetOkayToDiscard(false); 1031*0b57cec5SDimitry Andric } else { 1032*0b57cec5SDimitry Andric result.SetError(new_plan_status); 1033*0b57cec5SDimitry Andric return false; 1034*0b57cec5SDimitry Andric } 1035*0b57cec5SDimitry Andric } else { 1036*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1037*0b57cec5SDimitry Andric "Frame index %u of thread %u has no debug information.\n", 1038*0b57cec5SDimitry Andric m_options.m_frame_idx, m_options.m_thread_idx); 1039*0b57cec5SDimitry Andric return false; 1040*0b57cec5SDimitry Andric } 1041*0b57cec5SDimitry Andric 1042*0b57cec5SDimitry Andric process->GetThreadList().SetSelectedThreadByID(m_options.m_thread_idx); 1043*0b57cec5SDimitry Andric 1044*0b57cec5SDimitry Andric StreamString stream; 1045*0b57cec5SDimitry Andric Status error; 1046*0b57cec5SDimitry Andric if (synchronous_execution) 1047*0b57cec5SDimitry Andric error = process->ResumeSynchronous(&stream); 1048*0b57cec5SDimitry Andric else 1049*0b57cec5SDimitry Andric error = process->Resume(); 1050*0b57cec5SDimitry Andric 1051*0b57cec5SDimitry Andric if (error.Success()) { 1052*0b57cec5SDimitry Andric result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n", 1053*0b57cec5SDimitry Andric process->GetID()); 1054*0b57cec5SDimitry Andric if (synchronous_execution) { 1055*0b57cec5SDimitry Andric // If any state changed events had anything to say, add that to the 1056*0b57cec5SDimitry Andric // result 1057*0b57cec5SDimitry Andric if (stream.GetSize() > 0) 1058*0b57cec5SDimitry Andric result.AppendMessage(stream.GetString()); 1059*0b57cec5SDimitry Andric 1060*0b57cec5SDimitry Andric result.SetDidChangeProcessState(true); 1061*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1062*0b57cec5SDimitry Andric } else { 1063*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessContinuingNoResult); 1064*0b57cec5SDimitry Andric } 1065*0b57cec5SDimitry Andric } else { 1066*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Failed to resume process: %s.\n", 1067*0b57cec5SDimitry Andric error.AsCString()); 1068*0b57cec5SDimitry Andric } 1069*0b57cec5SDimitry Andric } 1070*0b57cec5SDimitry Andric return result.Succeeded(); 1071*0b57cec5SDimitry Andric } 1072*0b57cec5SDimitry Andric 1073*0b57cec5SDimitry Andric CommandOptions m_options; 1074*0b57cec5SDimitry Andric }; 1075*0b57cec5SDimitry Andric 1076*0b57cec5SDimitry Andric // CommandObjectThreadSelect 1077*0b57cec5SDimitry Andric 1078*0b57cec5SDimitry Andric class CommandObjectThreadSelect : public CommandObjectParsed { 1079*0b57cec5SDimitry Andric public: 1080*0b57cec5SDimitry Andric CommandObjectThreadSelect(CommandInterpreter &interpreter) 1081*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "thread select", 1082*0b57cec5SDimitry Andric "Change the currently selected thread.", nullptr, 1083*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1084*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 1085*0b57cec5SDimitry Andric eCommandProcessMustBePaused) { 1086*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1087*0b57cec5SDimitry Andric CommandArgumentData thread_idx_arg; 1088*0b57cec5SDimitry Andric 1089*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1090*0b57cec5SDimitry Andric thread_idx_arg.arg_type = eArgTypeThreadIndex; 1091*0b57cec5SDimitry Andric thread_idx_arg.arg_repetition = eArgRepeatPlain; 1092*0b57cec5SDimitry Andric 1093*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1094*0b57cec5SDimitry Andric // argument entry. 1095*0b57cec5SDimitry Andric arg.push_back(thread_idx_arg); 1096*0b57cec5SDimitry Andric 1097*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1098*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1099*0b57cec5SDimitry Andric } 1100*0b57cec5SDimitry Andric 1101*0b57cec5SDimitry Andric ~CommandObjectThreadSelect() override = default; 1102*0b57cec5SDimitry Andric 1103e8d8bef9SDimitry Andric void 1104e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1105e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1106e8d8bef9SDimitry Andric if (request.GetCursorIndex()) 1107e8d8bef9SDimitry Andric return; 1108e8d8bef9SDimitry Andric 1109e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 1110e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 1111e8d8bef9SDimitry Andric request, nullptr); 1112e8d8bef9SDimitry Andric } 1113e8d8bef9SDimitry Andric 1114*0b57cec5SDimitry Andric protected: 1115*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 1116*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1117*0b57cec5SDimitry Andric if (process == nullptr) { 1118*0b57cec5SDimitry Andric result.AppendError("no process"); 1119*0b57cec5SDimitry Andric return false; 1120*0b57cec5SDimitry Andric } else if (command.GetArgumentCount() != 1) { 1121*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1122*0b57cec5SDimitry Andric "'%s' takes exactly one thread index argument:\nUsage: %s\n", 1123*0b57cec5SDimitry Andric m_cmd_name.c_str(), m_cmd_syntax.c_str()); 1124*0b57cec5SDimitry Andric return false; 1125*0b57cec5SDimitry Andric } 1126*0b57cec5SDimitry Andric 11275ffd83dbSDimitry Andric uint32_t index_id; 11285ffd83dbSDimitry Andric if (!llvm::to_integer(command.GetArgumentAtIndex(0), index_id)) { 11295ffd83dbSDimitry Andric result.AppendErrorWithFormat("Invalid thread index '%s'", 11305ffd83dbSDimitry Andric command.GetArgumentAtIndex(0)); 11315ffd83dbSDimitry Andric return false; 11325ffd83dbSDimitry Andric } 1133*0b57cec5SDimitry Andric 1134*0b57cec5SDimitry Andric Thread *new_thread = 1135*0b57cec5SDimitry Andric process->GetThreadList().FindThreadByIndexID(index_id).get(); 1136*0b57cec5SDimitry Andric if (new_thread == nullptr) { 1137*0b57cec5SDimitry Andric result.AppendErrorWithFormat("invalid thread #%s.\n", 1138*0b57cec5SDimitry Andric command.GetArgumentAtIndex(0)); 1139*0b57cec5SDimitry Andric return false; 1140*0b57cec5SDimitry Andric } 1141*0b57cec5SDimitry Andric 1142*0b57cec5SDimitry Andric process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true); 1143*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1144*0b57cec5SDimitry Andric 1145*0b57cec5SDimitry Andric return result.Succeeded(); 1146*0b57cec5SDimitry Andric } 1147*0b57cec5SDimitry Andric }; 1148*0b57cec5SDimitry Andric 1149*0b57cec5SDimitry Andric // CommandObjectThreadList 1150*0b57cec5SDimitry Andric 1151*0b57cec5SDimitry Andric class CommandObjectThreadList : public CommandObjectParsed { 1152*0b57cec5SDimitry Andric public: 1153*0b57cec5SDimitry Andric CommandObjectThreadList(CommandInterpreter &interpreter) 1154*0b57cec5SDimitry Andric : CommandObjectParsed( 1155*0b57cec5SDimitry Andric interpreter, "thread list", 1156*0b57cec5SDimitry Andric "Show a summary of each thread in the current target process. " 1157*0b57cec5SDimitry Andric "Use 'settings set thread-format' to customize the individual " 1158*0b57cec5SDimitry Andric "thread listings.", 1159*0b57cec5SDimitry Andric "thread list", 1160*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1161*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} 1162*0b57cec5SDimitry Andric 1163*0b57cec5SDimitry Andric ~CommandObjectThreadList() override = default; 1164*0b57cec5SDimitry Andric 1165*0b57cec5SDimitry Andric protected: 1166*0b57cec5SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 1167*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1168*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1169*0b57cec5SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1170*0b57cec5SDimitry Andric const bool only_threads_with_stop_reason = false; 1171*0b57cec5SDimitry Andric const uint32_t start_frame = 0; 1172*0b57cec5SDimitry Andric const uint32_t num_frames = 0; 1173*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 0; 1174*0b57cec5SDimitry Andric process->GetStatus(strm); 1175*0b57cec5SDimitry Andric process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame, 1176*0b57cec5SDimitry Andric num_frames, num_frames_with_source, false); 1177*0b57cec5SDimitry Andric return result.Succeeded(); 1178*0b57cec5SDimitry Andric } 1179*0b57cec5SDimitry Andric }; 1180*0b57cec5SDimitry Andric 1181*0b57cec5SDimitry Andric // CommandObjectThreadInfo 1182*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_info 1183*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1184*0b57cec5SDimitry Andric 1185*0b57cec5SDimitry Andric class CommandObjectThreadInfo : public CommandObjectIterateOverThreads { 1186*0b57cec5SDimitry Andric public: 1187*0b57cec5SDimitry Andric class CommandOptions : public Options { 1188*0b57cec5SDimitry Andric public: 1189*0b57cec5SDimitry Andric CommandOptions() : Options() { OptionParsingStarting(nullptr); } 1190*0b57cec5SDimitry Andric 1191*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1192*0b57cec5SDimitry Andric 1193*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1194*0b57cec5SDimitry Andric m_json_thread = false; 1195*0b57cec5SDimitry Andric m_json_stopinfo = false; 1196*0b57cec5SDimitry Andric } 1197*0b57cec5SDimitry Andric 1198*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1199*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1200*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1201*0b57cec5SDimitry Andric Status error; 1202*0b57cec5SDimitry Andric 1203*0b57cec5SDimitry Andric switch (short_option) { 1204*0b57cec5SDimitry Andric case 'j': 1205*0b57cec5SDimitry Andric m_json_thread = true; 1206*0b57cec5SDimitry Andric break; 1207*0b57cec5SDimitry Andric 1208*0b57cec5SDimitry Andric case 's': 1209*0b57cec5SDimitry Andric m_json_stopinfo = true; 1210*0b57cec5SDimitry Andric break; 1211*0b57cec5SDimitry Andric 1212*0b57cec5SDimitry Andric default: 12139dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1214*0b57cec5SDimitry Andric } 1215*0b57cec5SDimitry Andric return error; 1216*0b57cec5SDimitry Andric } 1217*0b57cec5SDimitry Andric 1218*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1219*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_info_options); 1220*0b57cec5SDimitry Andric } 1221*0b57cec5SDimitry Andric 1222*0b57cec5SDimitry Andric bool m_json_thread; 1223*0b57cec5SDimitry Andric bool m_json_stopinfo; 1224*0b57cec5SDimitry Andric }; 1225*0b57cec5SDimitry Andric 1226*0b57cec5SDimitry Andric CommandObjectThreadInfo(CommandInterpreter &interpreter) 1227*0b57cec5SDimitry Andric : CommandObjectIterateOverThreads( 1228480093f4SDimitry Andric interpreter, "thread info", 1229480093f4SDimitry Andric "Show an extended summary of one or " 1230*0b57cec5SDimitry Andric "more threads. Defaults to the " 1231*0b57cec5SDimitry Andric "current thread.", 1232*0b57cec5SDimitry Andric "thread info", 1233*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1234*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 1235*0b57cec5SDimitry Andric m_options() { 1236*0b57cec5SDimitry Andric m_add_return = false; 1237*0b57cec5SDimitry Andric } 1238*0b57cec5SDimitry Andric 1239*0b57cec5SDimitry Andric ~CommandObjectThreadInfo() override = default; 1240*0b57cec5SDimitry Andric 1241e8d8bef9SDimitry Andric void 1242e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1243e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1244e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 1245e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 1246e8d8bef9SDimitry Andric request, nullptr); 1247e8d8bef9SDimitry Andric } 1248e8d8bef9SDimitry Andric 1249*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1250*0b57cec5SDimitry Andric 1251*0b57cec5SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 1252*0b57cec5SDimitry Andric ThreadSP thread_sp = 1253*0b57cec5SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 1254*0b57cec5SDimitry Andric if (!thread_sp) { 1255*0b57cec5SDimitry Andric result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n", 1256*0b57cec5SDimitry Andric tid); 1257*0b57cec5SDimitry Andric return false; 1258*0b57cec5SDimitry Andric } 1259*0b57cec5SDimitry Andric 1260*0b57cec5SDimitry Andric Thread *thread = thread_sp.get(); 1261*0b57cec5SDimitry Andric 1262*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1263*0b57cec5SDimitry Andric if (!thread->GetDescription(strm, eDescriptionLevelFull, 1264*0b57cec5SDimitry Andric m_options.m_json_thread, 1265*0b57cec5SDimitry Andric m_options.m_json_stopinfo)) { 1266*0b57cec5SDimitry Andric result.AppendErrorWithFormat("error displaying info for thread: \"%d\"\n", 1267*0b57cec5SDimitry Andric thread->GetIndexID()); 1268*0b57cec5SDimitry Andric return false; 1269*0b57cec5SDimitry Andric } 1270*0b57cec5SDimitry Andric return true; 1271*0b57cec5SDimitry Andric } 1272*0b57cec5SDimitry Andric 1273*0b57cec5SDimitry Andric CommandOptions m_options; 1274*0b57cec5SDimitry Andric }; 1275*0b57cec5SDimitry Andric 1276*0b57cec5SDimitry Andric // CommandObjectThreadException 1277*0b57cec5SDimitry Andric 1278*0b57cec5SDimitry Andric class CommandObjectThreadException : public CommandObjectIterateOverThreads { 1279*0b57cec5SDimitry Andric public: 1280*0b57cec5SDimitry Andric CommandObjectThreadException(CommandInterpreter &interpreter) 1281*0b57cec5SDimitry Andric : CommandObjectIterateOverThreads( 1282*0b57cec5SDimitry Andric interpreter, "thread exception", 1283*0b57cec5SDimitry Andric "Display the current exception object for a thread. Defaults to " 1284*0b57cec5SDimitry Andric "the current thread.", 1285*0b57cec5SDimitry Andric "thread exception", 1286*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1287*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} 1288*0b57cec5SDimitry Andric 1289*0b57cec5SDimitry Andric ~CommandObjectThreadException() override = default; 1290*0b57cec5SDimitry Andric 1291e8d8bef9SDimitry Andric void 1292e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1293e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1294e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 1295e8d8bef9SDimitry Andric GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion, 1296e8d8bef9SDimitry Andric request, nullptr); 1297e8d8bef9SDimitry Andric } 1298e8d8bef9SDimitry Andric 1299*0b57cec5SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 1300*0b57cec5SDimitry Andric ThreadSP thread_sp = 1301*0b57cec5SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 1302*0b57cec5SDimitry Andric if (!thread_sp) { 1303*0b57cec5SDimitry Andric result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n", 1304*0b57cec5SDimitry Andric tid); 1305*0b57cec5SDimitry Andric return false; 1306*0b57cec5SDimitry Andric } 1307*0b57cec5SDimitry Andric 1308*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1309*0b57cec5SDimitry Andric ValueObjectSP exception_object_sp = thread_sp->GetCurrentException(); 1310*0b57cec5SDimitry Andric if (exception_object_sp) { 1311*0b57cec5SDimitry Andric exception_object_sp->Dump(strm); 1312*0b57cec5SDimitry Andric } 1313*0b57cec5SDimitry Andric 1314*0b57cec5SDimitry Andric ThreadSP exception_thread_sp = thread_sp->GetCurrentExceptionBacktrace(); 1315*0b57cec5SDimitry Andric if (exception_thread_sp && exception_thread_sp->IsValid()) { 1316*0b57cec5SDimitry Andric const uint32_t num_frames_with_source = 0; 1317*0b57cec5SDimitry Andric const bool stop_format = false; 1318*0b57cec5SDimitry Andric exception_thread_sp->GetStatus(strm, 0, UINT32_MAX, 1319*0b57cec5SDimitry Andric num_frames_with_source, stop_format); 1320*0b57cec5SDimitry Andric } 1321*0b57cec5SDimitry Andric 1322*0b57cec5SDimitry Andric return true; 1323*0b57cec5SDimitry Andric } 1324*0b57cec5SDimitry Andric }; 1325*0b57cec5SDimitry Andric 1326*0b57cec5SDimitry Andric // CommandObjectThreadReturn 1327*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_return 1328*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1329*0b57cec5SDimitry Andric 1330*0b57cec5SDimitry Andric class CommandObjectThreadReturn : public CommandObjectRaw { 1331*0b57cec5SDimitry Andric public: 1332*0b57cec5SDimitry Andric class CommandOptions : public Options { 1333*0b57cec5SDimitry Andric public: 1334fe6060f1SDimitry Andric CommandOptions() : Options() { 1335*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 1336*0b57cec5SDimitry Andric // () 1337*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 1338*0b57cec5SDimitry Andric } 1339*0b57cec5SDimitry Andric 1340*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1341*0b57cec5SDimitry Andric 1342*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1343*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1344*0b57cec5SDimitry Andric Status error; 1345*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1346*0b57cec5SDimitry Andric 1347*0b57cec5SDimitry Andric switch (short_option) { 1348*0b57cec5SDimitry Andric case 'x': { 1349*0b57cec5SDimitry Andric bool success; 1350*0b57cec5SDimitry Andric bool tmp_value = 1351*0b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, false, &success); 1352*0b57cec5SDimitry Andric if (success) 1353*0b57cec5SDimitry Andric m_from_expression = tmp_value; 1354*0b57cec5SDimitry Andric else { 1355*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 1356*0b57cec5SDimitry Andric "invalid boolean value '%s' for 'x' option", 1357*0b57cec5SDimitry Andric option_arg.str().c_str()); 1358*0b57cec5SDimitry Andric } 1359*0b57cec5SDimitry Andric } break; 1360*0b57cec5SDimitry Andric default: 13619dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1362*0b57cec5SDimitry Andric } 1363*0b57cec5SDimitry Andric return error; 1364*0b57cec5SDimitry Andric } 1365*0b57cec5SDimitry Andric 1366*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1367*0b57cec5SDimitry Andric m_from_expression = false; 1368*0b57cec5SDimitry Andric } 1369*0b57cec5SDimitry Andric 1370*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1371*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_return_options); 1372*0b57cec5SDimitry Andric } 1373*0b57cec5SDimitry Andric 1374fe6060f1SDimitry Andric bool m_from_expression = false; 1375*0b57cec5SDimitry Andric 1376*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 1377*0b57cec5SDimitry Andric }; 1378*0b57cec5SDimitry Andric 1379*0b57cec5SDimitry Andric CommandObjectThreadReturn(CommandInterpreter &interpreter) 1380*0b57cec5SDimitry Andric : CommandObjectRaw(interpreter, "thread return", 1381*0b57cec5SDimitry Andric "Prematurely return from a stack frame, " 1382*0b57cec5SDimitry Andric "short-circuiting execution of newer frames " 1383*0b57cec5SDimitry Andric "and optionally yielding a specified value. Defaults " 1384*0b57cec5SDimitry Andric "to the exiting the current stack " 1385*0b57cec5SDimitry Andric "frame.", 1386*0b57cec5SDimitry Andric "thread return", 1387*0b57cec5SDimitry Andric eCommandRequiresFrame | eCommandTryTargetAPILock | 1388*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 1389*0b57cec5SDimitry Andric eCommandProcessMustBePaused), 1390*0b57cec5SDimitry Andric m_options() { 1391*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1392*0b57cec5SDimitry Andric CommandArgumentData expression_arg; 1393*0b57cec5SDimitry Andric 1394*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1395*0b57cec5SDimitry Andric expression_arg.arg_type = eArgTypeExpression; 1396*0b57cec5SDimitry Andric expression_arg.arg_repetition = eArgRepeatOptional; 1397*0b57cec5SDimitry Andric 1398*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1399*0b57cec5SDimitry Andric // argument entry. 1400*0b57cec5SDimitry Andric arg.push_back(expression_arg); 1401*0b57cec5SDimitry Andric 1402*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1403*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1404*0b57cec5SDimitry Andric } 1405*0b57cec5SDimitry Andric 1406*0b57cec5SDimitry Andric ~CommandObjectThreadReturn() override = default; 1407*0b57cec5SDimitry Andric 1408*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1409*0b57cec5SDimitry Andric 1410*0b57cec5SDimitry Andric protected: 1411*0b57cec5SDimitry Andric bool DoExecute(llvm::StringRef command, 1412*0b57cec5SDimitry Andric CommandReturnObject &result) override { 1413*0b57cec5SDimitry Andric // I am going to handle this by hand, because I don't want you to have to 1414*0b57cec5SDimitry Andric // say: 1415*0b57cec5SDimitry Andric // "thread return -- -5". 1416*0b57cec5SDimitry Andric if (command.startswith("-x")) { 1417*0b57cec5SDimitry Andric if (command.size() != 2U) 1418*0b57cec5SDimitry Andric result.AppendWarning("Return values ignored when returning from user " 1419*0b57cec5SDimitry Andric "called expressions"); 1420*0b57cec5SDimitry Andric 1421*0b57cec5SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 1422*0b57cec5SDimitry Andric Status error; 1423*0b57cec5SDimitry Andric error = thread->UnwindInnermostExpression(); 1424*0b57cec5SDimitry Andric if (!error.Success()) { 1425*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Unwinding expression failed - %s.", 1426*0b57cec5SDimitry Andric error.AsCString()); 1427*0b57cec5SDimitry Andric } else { 1428*0b57cec5SDimitry Andric bool success = 1429*0b57cec5SDimitry Andric thread->SetSelectedFrameByIndexNoisily(0, result.GetOutputStream()); 1430*0b57cec5SDimitry Andric if (success) { 1431*0b57cec5SDimitry Andric m_exe_ctx.SetFrameSP(thread->GetSelectedFrame()); 1432*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1433*0b57cec5SDimitry Andric } else { 1434*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1435*0b57cec5SDimitry Andric "Could not select 0th frame after unwinding expression."); 1436*0b57cec5SDimitry Andric } 1437*0b57cec5SDimitry Andric } 1438*0b57cec5SDimitry Andric return result.Succeeded(); 1439*0b57cec5SDimitry Andric } 1440*0b57cec5SDimitry Andric 1441*0b57cec5SDimitry Andric ValueObjectSP return_valobj_sp; 1442*0b57cec5SDimitry Andric 1443*0b57cec5SDimitry Andric StackFrameSP frame_sp = m_exe_ctx.GetFrameSP(); 1444*0b57cec5SDimitry Andric uint32_t frame_idx = frame_sp->GetFrameIndex(); 1445*0b57cec5SDimitry Andric 1446*0b57cec5SDimitry Andric if (frame_sp->IsInlined()) { 1447*0b57cec5SDimitry Andric result.AppendError("Don't know how to return from inlined frames."); 1448*0b57cec5SDimitry Andric return false; 1449*0b57cec5SDimitry Andric } 1450*0b57cec5SDimitry Andric 1451*0b57cec5SDimitry Andric if (!command.empty()) { 1452*0b57cec5SDimitry Andric Target *target = m_exe_ctx.GetTargetPtr(); 1453*0b57cec5SDimitry Andric EvaluateExpressionOptions options; 1454*0b57cec5SDimitry Andric 1455*0b57cec5SDimitry Andric options.SetUnwindOnError(true); 1456*0b57cec5SDimitry Andric options.SetUseDynamic(eNoDynamicValues); 1457*0b57cec5SDimitry Andric 1458*0b57cec5SDimitry Andric ExpressionResults exe_results = eExpressionSetupError; 1459*0b57cec5SDimitry Andric exe_results = target->EvaluateExpression(command, frame_sp.get(), 1460*0b57cec5SDimitry Andric return_valobj_sp, options); 1461*0b57cec5SDimitry Andric if (exe_results != eExpressionCompleted) { 1462*0b57cec5SDimitry Andric if (return_valobj_sp) 1463*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1464*0b57cec5SDimitry Andric "Error evaluating result expression: %s", 1465*0b57cec5SDimitry Andric return_valobj_sp->GetError().AsCString()); 1466*0b57cec5SDimitry Andric else 1467*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1468*0b57cec5SDimitry Andric "Unknown error evaluating result expression."); 1469*0b57cec5SDimitry Andric return false; 1470*0b57cec5SDimitry Andric } 1471*0b57cec5SDimitry Andric } 1472*0b57cec5SDimitry Andric 1473*0b57cec5SDimitry Andric Status error; 1474*0b57cec5SDimitry Andric ThreadSP thread_sp = m_exe_ctx.GetThreadSP(); 1475*0b57cec5SDimitry Andric const bool broadcast = true; 1476*0b57cec5SDimitry Andric error = thread_sp->ReturnFromFrame(frame_sp, return_valobj_sp, broadcast); 1477*0b57cec5SDimitry Andric if (!error.Success()) { 1478*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1479*0b57cec5SDimitry Andric "Error returning from frame %d of thread %d: %s.", frame_idx, 1480*0b57cec5SDimitry Andric thread_sp->GetIndexID(), error.AsCString()); 1481*0b57cec5SDimitry Andric return false; 1482*0b57cec5SDimitry Andric } 1483*0b57cec5SDimitry Andric 1484*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1485*0b57cec5SDimitry Andric return true; 1486*0b57cec5SDimitry Andric } 1487*0b57cec5SDimitry Andric 1488*0b57cec5SDimitry Andric CommandOptions m_options; 1489*0b57cec5SDimitry Andric }; 1490*0b57cec5SDimitry Andric 1491*0b57cec5SDimitry Andric // CommandObjectThreadJump 1492*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_jump 1493*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1494*0b57cec5SDimitry Andric 1495*0b57cec5SDimitry Andric class CommandObjectThreadJump : public CommandObjectParsed { 1496*0b57cec5SDimitry Andric public: 1497*0b57cec5SDimitry Andric class CommandOptions : public Options { 1498*0b57cec5SDimitry Andric public: 1499*0b57cec5SDimitry Andric CommandOptions() : Options() { OptionParsingStarting(nullptr); } 1500*0b57cec5SDimitry Andric 1501*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1502*0b57cec5SDimitry Andric 1503*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1504*0b57cec5SDimitry Andric m_filenames.Clear(); 1505*0b57cec5SDimitry Andric m_line_num = 0; 1506*0b57cec5SDimitry Andric m_line_offset = 0; 1507*0b57cec5SDimitry Andric m_load_addr = LLDB_INVALID_ADDRESS; 1508*0b57cec5SDimitry Andric m_force = false; 1509*0b57cec5SDimitry Andric } 1510*0b57cec5SDimitry Andric 1511*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1512*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1513*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1514*0b57cec5SDimitry Andric Status error; 1515*0b57cec5SDimitry Andric 1516*0b57cec5SDimitry Andric switch (short_option) { 1517*0b57cec5SDimitry Andric case 'f': 1518*0b57cec5SDimitry Andric m_filenames.AppendIfUnique(FileSpec(option_arg)); 1519*0b57cec5SDimitry Andric if (m_filenames.GetSize() > 1) 1520*0b57cec5SDimitry Andric return Status("only one source file expected."); 1521*0b57cec5SDimitry Andric break; 1522*0b57cec5SDimitry Andric case 'l': 1523*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_line_num)) 1524*0b57cec5SDimitry Andric return Status("invalid line number: '%s'.", option_arg.str().c_str()); 1525*0b57cec5SDimitry Andric break; 1526*0b57cec5SDimitry Andric case 'b': 1527*0b57cec5SDimitry Andric if (option_arg.getAsInteger(0, m_line_offset)) 1528*0b57cec5SDimitry Andric return Status("invalid line offset: '%s'.", option_arg.str().c_str()); 1529*0b57cec5SDimitry Andric break; 1530*0b57cec5SDimitry Andric case 'a': 1531*0b57cec5SDimitry Andric m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg, 1532*0b57cec5SDimitry Andric LLDB_INVALID_ADDRESS, &error); 1533*0b57cec5SDimitry Andric break; 1534*0b57cec5SDimitry Andric case 'r': 1535*0b57cec5SDimitry Andric m_force = true; 1536*0b57cec5SDimitry Andric break; 1537*0b57cec5SDimitry Andric default: 15389dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1539*0b57cec5SDimitry Andric } 1540*0b57cec5SDimitry Andric return error; 1541*0b57cec5SDimitry Andric } 1542*0b57cec5SDimitry Andric 1543*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1544*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_jump_options); 1545*0b57cec5SDimitry Andric } 1546*0b57cec5SDimitry Andric 1547*0b57cec5SDimitry Andric FileSpecList m_filenames; 1548*0b57cec5SDimitry Andric uint32_t m_line_num; 1549*0b57cec5SDimitry Andric int32_t m_line_offset; 1550*0b57cec5SDimitry Andric lldb::addr_t m_load_addr; 1551*0b57cec5SDimitry Andric bool m_force; 1552*0b57cec5SDimitry Andric }; 1553*0b57cec5SDimitry Andric 1554*0b57cec5SDimitry Andric CommandObjectThreadJump(CommandInterpreter &interpreter) 1555*0b57cec5SDimitry Andric : CommandObjectParsed( 1556*0b57cec5SDimitry Andric interpreter, "thread jump", 1557*0b57cec5SDimitry Andric "Sets the program counter to a new address.", "thread jump", 1558*0b57cec5SDimitry Andric eCommandRequiresFrame | eCommandTryTargetAPILock | 1559*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 1560*0b57cec5SDimitry Andric m_options() {} 1561*0b57cec5SDimitry Andric 1562*0b57cec5SDimitry Andric ~CommandObjectThreadJump() override = default; 1563*0b57cec5SDimitry Andric 1564*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1565*0b57cec5SDimitry Andric 1566*0b57cec5SDimitry Andric protected: 1567*0b57cec5SDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 1568*0b57cec5SDimitry Andric RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext(); 1569*0b57cec5SDimitry Andric StackFrame *frame = m_exe_ctx.GetFramePtr(); 1570*0b57cec5SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 1571*0b57cec5SDimitry Andric Target *target = m_exe_ctx.GetTargetPtr(); 1572*0b57cec5SDimitry Andric const SymbolContext &sym_ctx = 1573*0b57cec5SDimitry Andric frame->GetSymbolContext(eSymbolContextLineEntry); 1574*0b57cec5SDimitry Andric 1575*0b57cec5SDimitry Andric if (m_options.m_load_addr != LLDB_INVALID_ADDRESS) { 1576*0b57cec5SDimitry Andric // Use this address directly. 1577*0b57cec5SDimitry Andric Address dest = Address(m_options.m_load_addr); 1578*0b57cec5SDimitry Andric 1579*0b57cec5SDimitry Andric lldb::addr_t callAddr = dest.GetCallableLoadAddress(target); 1580*0b57cec5SDimitry Andric if (callAddr == LLDB_INVALID_ADDRESS) { 1581*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid destination address."); 1582*0b57cec5SDimitry Andric return false; 1583*0b57cec5SDimitry Andric } 1584*0b57cec5SDimitry Andric 1585*0b57cec5SDimitry Andric if (!reg_ctx->SetPC(callAddr)) { 1586*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Error changing PC value for thread %d.", 1587*0b57cec5SDimitry Andric thread->GetIndexID()); 1588*0b57cec5SDimitry Andric return false; 1589*0b57cec5SDimitry Andric } 1590*0b57cec5SDimitry Andric } else { 1591*0b57cec5SDimitry Andric // Pick either the absolute line, or work out a relative one. 1592*0b57cec5SDimitry Andric int32_t line = (int32_t)m_options.m_line_num; 1593*0b57cec5SDimitry Andric if (line == 0) 1594*0b57cec5SDimitry Andric line = sym_ctx.line_entry.line + m_options.m_line_offset; 1595*0b57cec5SDimitry Andric 1596*0b57cec5SDimitry Andric // Try the current file, but override if asked. 1597*0b57cec5SDimitry Andric FileSpec file = sym_ctx.line_entry.file; 1598*0b57cec5SDimitry Andric if (m_options.m_filenames.GetSize() == 1) 1599*0b57cec5SDimitry Andric file = m_options.m_filenames.GetFileSpecAtIndex(0); 1600*0b57cec5SDimitry Andric 1601*0b57cec5SDimitry Andric if (!file) { 1602*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1603*0b57cec5SDimitry Andric "No source file available for the current location."); 1604*0b57cec5SDimitry Andric return false; 1605*0b57cec5SDimitry Andric } 1606*0b57cec5SDimitry Andric 1607*0b57cec5SDimitry Andric std::string warnings; 1608*0b57cec5SDimitry Andric Status err = thread->JumpToLine(file, line, m_options.m_force, &warnings); 1609*0b57cec5SDimitry Andric 1610*0b57cec5SDimitry Andric if (err.Fail()) { 1611*0b57cec5SDimitry Andric result.SetError(err); 1612*0b57cec5SDimitry Andric return false; 1613*0b57cec5SDimitry Andric } 1614*0b57cec5SDimitry Andric 1615*0b57cec5SDimitry Andric if (!warnings.empty()) 1616*0b57cec5SDimitry Andric result.AppendWarning(warnings.c_str()); 1617*0b57cec5SDimitry Andric } 1618*0b57cec5SDimitry Andric 1619*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1620*0b57cec5SDimitry Andric return true; 1621*0b57cec5SDimitry Andric } 1622*0b57cec5SDimitry Andric 1623*0b57cec5SDimitry Andric CommandOptions m_options; 1624*0b57cec5SDimitry Andric }; 1625*0b57cec5SDimitry Andric 1626*0b57cec5SDimitry Andric // Next are the subcommands of CommandObjectMultiwordThreadPlan 1627*0b57cec5SDimitry Andric 1628*0b57cec5SDimitry Andric // CommandObjectThreadPlanList 1629*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_plan_list 1630*0b57cec5SDimitry Andric #include "CommandOptions.inc" 1631*0b57cec5SDimitry Andric 1632*0b57cec5SDimitry Andric class CommandObjectThreadPlanList : public CommandObjectIterateOverThreads { 1633*0b57cec5SDimitry Andric public: 1634*0b57cec5SDimitry Andric class CommandOptions : public Options { 1635*0b57cec5SDimitry Andric public: 1636*0b57cec5SDimitry Andric CommandOptions() : Options() { 1637*0b57cec5SDimitry Andric // Keep default values of all options in one place: OptionParsingStarting 1638*0b57cec5SDimitry Andric // () 1639*0b57cec5SDimitry Andric OptionParsingStarting(nullptr); 1640*0b57cec5SDimitry Andric } 1641*0b57cec5SDimitry Andric 1642*0b57cec5SDimitry Andric ~CommandOptions() override = default; 1643*0b57cec5SDimitry Andric 1644*0b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1645*0b57cec5SDimitry Andric ExecutionContext *execution_context) override { 1646*0b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 1647*0b57cec5SDimitry Andric 1648*0b57cec5SDimitry Andric switch (short_option) { 1649*0b57cec5SDimitry Andric case 'i': 1650*0b57cec5SDimitry Andric m_internal = true; 1651*0b57cec5SDimitry Andric break; 16525ffd83dbSDimitry Andric case 't': 16535ffd83dbSDimitry Andric lldb::tid_t tid; 16545ffd83dbSDimitry Andric if (option_arg.getAsInteger(0, tid)) 16555ffd83dbSDimitry Andric return Status("invalid tid: '%s'.", option_arg.str().c_str()); 16565ffd83dbSDimitry Andric m_tids.push_back(tid); 16575ffd83dbSDimitry Andric break; 16585ffd83dbSDimitry Andric case 'u': 16595ffd83dbSDimitry Andric m_unreported = false; 16605ffd83dbSDimitry Andric break; 1661*0b57cec5SDimitry Andric case 'v': 1662*0b57cec5SDimitry Andric m_verbose = true; 1663*0b57cec5SDimitry Andric break; 1664*0b57cec5SDimitry Andric default: 16659dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1666*0b57cec5SDimitry Andric } 16675ffd83dbSDimitry Andric return {}; 1668*0b57cec5SDimitry Andric } 1669*0b57cec5SDimitry Andric 1670*0b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 1671*0b57cec5SDimitry Andric m_verbose = false; 1672*0b57cec5SDimitry Andric m_internal = false; 16735ffd83dbSDimitry Andric m_unreported = true; // The variable is "skip unreported" and we want to 16745ffd83dbSDimitry Andric // skip unreported by default. 16755ffd83dbSDimitry Andric m_tids.clear(); 1676*0b57cec5SDimitry Andric } 1677*0b57cec5SDimitry Andric 1678*0b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1679*0b57cec5SDimitry Andric return llvm::makeArrayRef(g_thread_plan_list_options); 1680*0b57cec5SDimitry Andric } 1681*0b57cec5SDimitry Andric 1682*0b57cec5SDimitry Andric // Instance variables to hold the values for command options. 1683*0b57cec5SDimitry Andric bool m_verbose; 1684*0b57cec5SDimitry Andric bool m_internal; 16855ffd83dbSDimitry Andric bool m_unreported; 16865ffd83dbSDimitry Andric std::vector<lldb::tid_t> m_tids; 1687*0b57cec5SDimitry Andric }; 1688*0b57cec5SDimitry Andric 1689*0b57cec5SDimitry Andric CommandObjectThreadPlanList(CommandInterpreter &interpreter) 1690*0b57cec5SDimitry Andric : CommandObjectIterateOverThreads( 1691*0b57cec5SDimitry Andric interpreter, "thread plan list", 1692*0b57cec5SDimitry Andric "Show thread plans for one or more threads. If no threads are " 1693*0b57cec5SDimitry Andric "specified, show the " 1694*0b57cec5SDimitry Andric "current thread. Use the thread-index \"all\" to see all threads.", 1695*0b57cec5SDimitry Andric nullptr, 1696*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 1697*0b57cec5SDimitry Andric eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | 1698*0b57cec5SDimitry Andric eCommandProcessMustBePaused), 1699*0b57cec5SDimitry Andric m_options() {} 1700*0b57cec5SDimitry Andric 1701*0b57cec5SDimitry Andric ~CommandObjectThreadPlanList() override = default; 1702*0b57cec5SDimitry Andric 1703*0b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; } 1704*0b57cec5SDimitry Andric 17055ffd83dbSDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 17065ffd83dbSDimitry Andric // If we are reporting all threads, dispatch to the Process to do that: 17075ffd83dbSDimitry Andric if (command.GetArgumentCount() == 0 && m_options.m_tids.empty()) { 17085ffd83dbSDimitry Andric Stream &strm = result.GetOutputStream(); 17095ffd83dbSDimitry Andric DescriptionLevel desc_level = m_options.m_verbose 17105ffd83dbSDimitry Andric ? eDescriptionLevelVerbose 17115ffd83dbSDimitry Andric : eDescriptionLevelFull; 17125ffd83dbSDimitry Andric m_exe_ctx.GetProcessPtr()->DumpThreadPlans( 17135ffd83dbSDimitry Andric strm, desc_level, m_options.m_internal, true, m_options.m_unreported); 17145ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 17155ffd83dbSDimitry Andric return true; 17165ffd83dbSDimitry Andric } else { 17175ffd83dbSDimitry Andric // Do any TID's that the user may have specified as TID, then do any 17185ffd83dbSDimitry Andric // Thread Indexes... 17195ffd83dbSDimitry Andric if (!m_options.m_tids.empty()) { 17205ffd83dbSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 17215ffd83dbSDimitry Andric StreamString tmp_strm; 17225ffd83dbSDimitry Andric for (lldb::tid_t tid : m_options.m_tids) { 17235ffd83dbSDimitry Andric bool success = process->DumpThreadPlansForTID( 17245ffd83dbSDimitry Andric tmp_strm, tid, eDescriptionLevelFull, m_options.m_internal, 17255ffd83dbSDimitry Andric true /* condense_trivial */, m_options.m_unreported); 17265ffd83dbSDimitry Andric // If we didn't find a TID, stop here and return an error. 17275ffd83dbSDimitry Andric if (!success) { 1728fe6060f1SDimitry Andric result.AppendError("Error dumping plans:"); 17295ffd83dbSDimitry Andric result.AppendError(tmp_strm.GetString()); 1730*0b57cec5SDimitry Andric return false; 1731*0b57cec5SDimitry Andric } 17325ffd83dbSDimitry Andric // Otherwise, add our data to the output: 17335ffd83dbSDimitry Andric result.GetOutputStream() << tmp_strm.GetString(); 17345ffd83dbSDimitry Andric } 17355ffd83dbSDimitry Andric } 17365ffd83dbSDimitry Andric return CommandObjectIterateOverThreads::DoExecute(command, result); 17375ffd83dbSDimitry Andric } 17385ffd83dbSDimitry Andric } 1739*0b57cec5SDimitry Andric 17405ffd83dbSDimitry Andric protected: 17415ffd83dbSDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 17425ffd83dbSDimitry Andric // If we have already handled this from a -t option, skip it here. 1743e8d8bef9SDimitry Andric if (llvm::is_contained(m_options.m_tids, tid)) 17445ffd83dbSDimitry Andric return true; 17455ffd83dbSDimitry Andric 17465ffd83dbSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 1747*0b57cec5SDimitry Andric 1748*0b57cec5SDimitry Andric Stream &strm = result.GetOutputStream(); 1749*0b57cec5SDimitry Andric DescriptionLevel desc_level = eDescriptionLevelFull; 1750*0b57cec5SDimitry Andric if (m_options.m_verbose) 1751*0b57cec5SDimitry Andric desc_level = eDescriptionLevelVerbose; 1752*0b57cec5SDimitry Andric 17535ffd83dbSDimitry Andric process->DumpThreadPlansForTID(strm, tid, desc_level, m_options.m_internal, 17545ffd83dbSDimitry Andric true /* condense_trivial */, 17555ffd83dbSDimitry Andric m_options.m_unreported); 1756*0b57cec5SDimitry Andric return true; 1757*0b57cec5SDimitry Andric } 1758*0b57cec5SDimitry Andric 1759*0b57cec5SDimitry Andric CommandOptions m_options; 1760*0b57cec5SDimitry Andric }; 1761*0b57cec5SDimitry Andric 1762*0b57cec5SDimitry Andric class CommandObjectThreadPlanDiscard : public CommandObjectParsed { 1763*0b57cec5SDimitry Andric public: 1764*0b57cec5SDimitry Andric CommandObjectThreadPlanDiscard(CommandInterpreter &interpreter) 1765*0b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "thread plan discard", 1766*0b57cec5SDimitry Andric "Discards thread plans up to and including the " 1767*0b57cec5SDimitry Andric "specified index (see 'thread plan list'.) " 1768*0b57cec5SDimitry Andric "Only user visible plans can be discarded.", 1769*0b57cec5SDimitry Andric nullptr, 1770*0b57cec5SDimitry Andric eCommandRequiresProcess | eCommandRequiresThread | 1771*0b57cec5SDimitry Andric eCommandTryTargetAPILock | 1772*0b57cec5SDimitry Andric eCommandProcessMustBeLaunched | 1773*0b57cec5SDimitry Andric eCommandProcessMustBePaused) { 1774*0b57cec5SDimitry Andric CommandArgumentEntry arg; 1775*0b57cec5SDimitry Andric CommandArgumentData plan_index_arg; 1776*0b57cec5SDimitry Andric 1777*0b57cec5SDimitry Andric // Define the first (and only) variant of this arg. 1778*0b57cec5SDimitry Andric plan_index_arg.arg_type = eArgTypeUnsignedInteger; 1779*0b57cec5SDimitry Andric plan_index_arg.arg_repetition = eArgRepeatPlain; 1780*0b57cec5SDimitry Andric 1781*0b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the 1782*0b57cec5SDimitry Andric // argument entry. 1783*0b57cec5SDimitry Andric arg.push_back(plan_index_arg); 1784*0b57cec5SDimitry Andric 1785*0b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector. 1786*0b57cec5SDimitry Andric m_arguments.push_back(arg); 1787*0b57cec5SDimitry Andric } 1788*0b57cec5SDimitry Andric 1789*0b57cec5SDimitry Andric ~CommandObjectThreadPlanDiscard() override = default; 1790*0b57cec5SDimitry Andric 1791e8d8bef9SDimitry Andric void 1792e8d8bef9SDimitry Andric HandleArgumentCompletion(CompletionRequest &request, 1793e8d8bef9SDimitry Andric OptionElementVector &opt_element_vector) override { 1794e8d8bef9SDimitry Andric if (!m_exe_ctx.HasThreadScope() || request.GetCursorIndex()) 1795e8d8bef9SDimitry Andric return; 1796e8d8bef9SDimitry Andric 1797e8d8bef9SDimitry Andric m_exe_ctx.GetThreadPtr()->AutoCompleteThreadPlans(request); 1798e8d8bef9SDimitry Andric } 1799e8d8bef9SDimitry Andric 1800*0b57cec5SDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 1801*0b57cec5SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 1802*0b57cec5SDimitry Andric if (args.GetArgumentCount() != 1) { 1803*0b57cec5SDimitry Andric result.AppendErrorWithFormat("Too many arguments, expected one - the " 1804*0b57cec5SDimitry Andric "thread plan index - but got %zu.", 1805*0b57cec5SDimitry Andric args.GetArgumentCount()); 1806*0b57cec5SDimitry Andric return false; 1807*0b57cec5SDimitry Andric } 1808*0b57cec5SDimitry Andric 18095ffd83dbSDimitry Andric uint32_t thread_plan_idx; 18105ffd83dbSDimitry Andric if (!llvm::to_integer(args.GetArgumentAtIndex(0), thread_plan_idx)) { 1811*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1812*0b57cec5SDimitry Andric "Invalid thread index: \"%s\" - should be unsigned int.", 1813*0b57cec5SDimitry Andric args.GetArgumentAtIndex(0)); 1814*0b57cec5SDimitry Andric return false; 1815*0b57cec5SDimitry Andric } 1816*0b57cec5SDimitry Andric 1817*0b57cec5SDimitry Andric if (thread_plan_idx == 0) { 1818*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1819*0b57cec5SDimitry Andric "You wouldn't really want me to discard the base thread plan."); 1820*0b57cec5SDimitry Andric return false; 1821*0b57cec5SDimitry Andric } 1822*0b57cec5SDimitry Andric 1823*0b57cec5SDimitry Andric if (thread->DiscardUserThreadPlansUpToIndex(thread_plan_idx)) { 1824*0b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 1825*0b57cec5SDimitry Andric return true; 1826*0b57cec5SDimitry Andric } else { 1827*0b57cec5SDimitry Andric result.AppendErrorWithFormat( 1828*0b57cec5SDimitry Andric "Could not find User thread plan with index %s.", 1829*0b57cec5SDimitry Andric args.GetArgumentAtIndex(0)); 1830*0b57cec5SDimitry Andric return false; 1831*0b57cec5SDimitry Andric } 1832*0b57cec5SDimitry Andric } 1833*0b57cec5SDimitry Andric }; 1834*0b57cec5SDimitry Andric 18355ffd83dbSDimitry Andric class CommandObjectThreadPlanPrune : public CommandObjectParsed { 18365ffd83dbSDimitry Andric public: 18375ffd83dbSDimitry Andric CommandObjectThreadPlanPrune(CommandInterpreter &interpreter) 18385ffd83dbSDimitry Andric : CommandObjectParsed(interpreter, "thread plan prune", 18395ffd83dbSDimitry Andric "Removes any thread plans associated with " 18405ffd83dbSDimitry Andric "currently unreported threads. " 18415ffd83dbSDimitry Andric "Specify one or more TID's to remove, or if no " 18425ffd83dbSDimitry Andric "TID's are provides, remove threads for all " 18435ffd83dbSDimitry Andric "unreported threads", 18445ffd83dbSDimitry Andric nullptr, 18455ffd83dbSDimitry Andric eCommandRequiresProcess | 18465ffd83dbSDimitry Andric eCommandTryTargetAPILock | 18475ffd83dbSDimitry Andric eCommandProcessMustBeLaunched | 18485ffd83dbSDimitry Andric eCommandProcessMustBePaused) { 18495ffd83dbSDimitry Andric CommandArgumentEntry arg; 18505ffd83dbSDimitry Andric CommandArgumentData tid_arg; 18515ffd83dbSDimitry Andric 18525ffd83dbSDimitry Andric // Define the first (and only) variant of this arg. 18535ffd83dbSDimitry Andric tid_arg.arg_type = eArgTypeThreadID; 18545ffd83dbSDimitry Andric tid_arg.arg_repetition = eArgRepeatStar; 18555ffd83dbSDimitry Andric 18565ffd83dbSDimitry Andric // There is only one variant this argument could be; put it into the 18575ffd83dbSDimitry Andric // argument entry. 18585ffd83dbSDimitry Andric arg.push_back(tid_arg); 18595ffd83dbSDimitry Andric 18605ffd83dbSDimitry Andric // Push the data for the first argument into the m_arguments vector. 18615ffd83dbSDimitry Andric m_arguments.push_back(arg); 18625ffd83dbSDimitry Andric } 18635ffd83dbSDimitry Andric 18645ffd83dbSDimitry Andric ~CommandObjectThreadPlanPrune() override = default; 18655ffd83dbSDimitry Andric 18665ffd83dbSDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 18675ffd83dbSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 18685ffd83dbSDimitry Andric 18695ffd83dbSDimitry Andric if (args.GetArgumentCount() == 0) { 18705ffd83dbSDimitry Andric process->PruneThreadPlans(); 18715ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 18725ffd83dbSDimitry Andric return true; 18735ffd83dbSDimitry Andric } 18745ffd83dbSDimitry Andric 18755ffd83dbSDimitry Andric const size_t num_args = args.GetArgumentCount(); 18765ffd83dbSDimitry Andric 18775ffd83dbSDimitry Andric std::lock_guard<std::recursive_mutex> guard( 18785ffd83dbSDimitry Andric process->GetThreadList().GetMutex()); 18795ffd83dbSDimitry Andric 18805ffd83dbSDimitry Andric for (size_t i = 0; i < num_args; i++) { 18815ffd83dbSDimitry Andric lldb::tid_t tid; 18825ffd83dbSDimitry Andric if (!llvm::to_integer(args.GetArgumentAtIndex(i), tid)) { 18835ffd83dbSDimitry Andric result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n", 18845ffd83dbSDimitry Andric args.GetArgumentAtIndex(i)); 18855ffd83dbSDimitry Andric return false; 18865ffd83dbSDimitry Andric } 18875ffd83dbSDimitry Andric if (!process->PruneThreadPlansForTID(tid)) { 18885ffd83dbSDimitry Andric result.AppendErrorWithFormat("Could not find unreported tid: \"%s\"\n", 18895ffd83dbSDimitry Andric args.GetArgumentAtIndex(i)); 18905ffd83dbSDimitry Andric return false; 18915ffd83dbSDimitry Andric } 18925ffd83dbSDimitry Andric } 18935ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 18945ffd83dbSDimitry Andric return true; 18955ffd83dbSDimitry Andric } 18965ffd83dbSDimitry Andric }; 18975ffd83dbSDimitry Andric 1898*0b57cec5SDimitry Andric // CommandObjectMultiwordThreadPlan 1899*0b57cec5SDimitry Andric 1900*0b57cec5SDimitry Andric class CommandObjectMultiwordThreadPlan : public CommandObjectMultiword { 1901*0b57cec5SDimitry Andric public: 1902*0b57cec5SDimitry Andric CommandObjectMultiwordThreadPlan(CommandInterpreter &interpreter) 1903*0b57cec5SDimitry Andric : CommandObjectMultiword( 1904*0b57cec5SDimitry Andric interpreter, "plan", 1905*0b57cec5SDimitry Andric "Commands for managing thread plans that control execution.", 1906*0b57cec5SDimitry Andric "thread plan <subcommand> [<subcommand objects]") { 1907*0b57cec5SDimitry Andric LoadSubCommand( 1908*0b57cec5SDimitry Andric "list", CommandObjectSP(new CommandObjectThreadPlanList(interpreter))); 1909*0b57cec5SDimitry Andric LoadSubCommand( 1910*0b57cec5SDimitry Andric "discard", 1911*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadPlanDiscard(interpreter))); 19125ffd83dbSDimitry Andric LoadSubCommand( 19135ffd83dbSDimitry Andric "prune", 19145ffd83dbSDimitry Andric CommandObjectSP(new CommandObjectThreadPlanPrune(interpreter))); 1915*0b57cec5SDimitry Andric } 1916*0b57cec5SDimitry Andric 1917*0b57cec5SDimitry Andric ~CommandObjectMultiwordThreadPlan() override = default; 1918*0b57cec5SDimitry Andric }; 1919*0b57cec5SDimitry Andric 1920e8d8bef9SDimitry Andric // Next are the subcommands of CommandObjectMultiwordTrace 1921e8d8bef9SDimitry Andric 1922fe6060f1SDimitry Andric // CommandObjectTraceExport 1923fe6060f1SDimitry Andric 1924fe6060f1SDimitry Andric class CommandObjectTraceExport : public CommandObjectMultiword { 1925fe6060f1SDimitry Andric public: 1926fe6060f1SDimitry Andric CommandObjectTraceExport(CommandInterpreter &interpreter) 1927fe6060f1SDimitry Andric : CommandObjectMultiword( 1928fe6060f1SDimitry Andric interpreter, "trace thread export", 1929fe6060f1SDimitry Andric "Commands for exporting traces of the threads in the current " 1930fe6060f1SDimitry Andric "process to different formats.", 1931fe6060f1SDimitry Andric "thread trace export <export-plugin> [<subcommand objects>]") { 1932fe6060f1SDimitry Andric 1933349cc55cSDimitry Andric unsigned i = 0; 1934349cc55cSDimitry Andric for (llvm::StringRef plugin_name = 1935349cc55cSDimitry Andric PluginManager::GetTraceExporterPluginNameAtIndex(i++); 1936349cc55cSDimitry Andric !plugin_name.empty(); 1937349cc55cSDimitry Andric plugin_name = PluginManager::GetTraceExporterPluginNameAtIndex(i++)) { 1938fe6060f1SDimitry Andric if (ThreadTraceExportCommandCreator command_creator = 1939fe6060f1SDimitry Andric PluginManager::GetThreadTraceExportCommandCreatorAtIndex(i)) { 1940fe6060f1SDimitry Andric LoadSubCommand(plugin_name, command_creator(interpreter)); 1941fe6060f1SDimitry Andric } 1942fe6060f1SDimitry Andric } 1943fe6060f1SDimitry Andric } 1944fe6060f1SDimitry Andric }; 1945fe6060f1SDimitry Andric 1946e8d8bef9SDimitry Andric // CommandObjectTraceStart 1947e8d8bef9SDimitry Andric 1948fe6060f1SDimitry Andric class CommandObjectTraceStart : public CommandObjectTraceProxy { 1949e8d8bef9SDimitry Andric public: 1950e8d8bef9SDimitry Andric CommandObjectTraceStart(CommandInterpreter &interpreter) 1951fe6060f1SDimitry Andric : CommandObjectTraceProxy( 1952fe6060f1SDimitry Andric /*live_debug_session_only=*/true, interpreter, "thread trace start", 1953e8d8bef9SDimitry Andric "Start tracing threads with the corresponding trace " 1954e8d8bef9SDimitry Andric "plug-in for the current process.", 1955e8d8bef9SDimitry Andric "thread trace start [<trace-options>]") {} 1956e8d8bef9SDimitry Andric 1957e8d8bef9SDimitry Andric protected: 1958fe6060f1SDimitry Andric lldb::CommandObjectSP GetDelegateCommand(Trace &trace) override { 1959fe6060f1SDimitry Andric return trace.GetThreadTraceStartCommand(m_interpreter); 1960e8d8bef9SDimitry Andric } 1961e8d8bef9SDimitry Andric }; 1962e8d8bef9SDimitry Andric 1963e8d8bef9SDimitry Andric // CommandObjectTraceStop 1964e8d8bef9SDimitry Andric 1965fe6060f1SDimitry Andric class CommandObjectTraceStop : public CommandObjectMultipleThreads { 1966e8d8bef9SDimitry Andric public: 1967e8d8bef9SDimitry Andric CommandObjectTraceStop(CommandInterpreter &interpreter) 1968fe6060f1SDimitry Andric : CommandObjectMultipleThreads( 1969e8d8bef9SDimitry Andric interpreter, "thread trace stop", 1970fe6060f1SDimitry Andric "Stop tracing threads, including the ones traced with the " 1971fe6060f1SDimitry Andric "\"process trace start\" command." 1972e8d8bef9SDimitry Andric "Defaults to the current thread. Thread indices can be " 1973fe6060f1SDimitry Andric "specified as arguments.\n Use the thread-index \"all\" to stop " 1974fe6060f1SDimitry Andric "tracing " 1975fe6060f1SDimitry Andric "for all existing threads.", 1976e8d8bef9SDimitry Andric "thread trace stop [<thread-index> <thread-index> ...]", 1977e8d8bef9SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 1978e8d8bef9SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | 1979e8d8bef9SDimitry Andric eCommandProcessMustBeTraced) {} 1980e8d8bef9SDimitry Andric 1981e8d8bef9SDimitry Andric ~CommandObjectTraceStop() override = default; 1982e8d8bef9SDimitry Andric 1983fe6060f1SDimitry Andric bool DoExecuteOnThreads(Args &command, CommandReturnObject &result, 1984fe6060f1SDimitry Andric llvm::ArrayRef<lldb::tid_t> tids) override { 1985fe6060f1SDimitry Andric ProcessSP process_sp = m_exe_ctx.GetProcessSP(); 1986e8d8bef9SDimitry Andric 1987fe6060f1SDimitry Andric TraceSP trace_sp = process_sp->GetTarget().GetTrace(); 1988e8d8bef9SDimitry Andric 1989fe6060f1SDimitry Andric if (llvm::Error err = trace_sp->Stop(tids)) 1990fe6060f1SDimitry Andric result.AppendError(toString(std::move(err))); 1991fe6060f1SDimitry Andric else 1992fe6060f1SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 1993fe6060f1SDimitry Andric 1994fe6060f1SDimitry Andric return result.Succeeded(); 1995e8d8bef9SDimitry Andric } 1996e8d8bef9SDimitry Andric }; 1997e8d8bef9SDimitry Andric 1998e8d8bef9SDimitry Andric // CommandObjectTraceDumpInstructions 1999e8d8bef9SDimitry Andric #define LLDB_OPTIONS_thread_trace_dump_instructions 2000e8d8bef9SDimitry Andric #include "CommandOptions.inc" 2001e8d8bef9SDimitry Andric 2002e8d8bef9SDimitry Andric class CommandObjectTraceDumpInstructions 2003e8d8bef9SDimitry Andric : public CommandObjectIterateOverThreads { 2004e8d8bef9SDimitry Andric public: 2005e8d8bef9SDimitry Andric class CommandOptions : public Options { 2006e8d8bef9SDimitry Andric public: 2007e8d8bef9SDimitry Andric CommandOptions() : Options() { OptionParsingStarting(nullptr); } 2008e8d8bef9SDimitry Andric 2009e8d8bef9SDimitry Andric ~CommandOptions() override = default; 2010e8d8bef9SDimitry Andric 2011e8d8bef9SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 2012e8d8bef9SDimitry Andric ExecutionContext *execution_context) override { 2013e8d8bef9SDimitry Andric Status error; 2014e8d8bef9SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 2015e8d8bef9SDimitry Andric 2016e8d8bef9SDimitry Andric switch (short_option) { 2017e8d8bef9SDimitry Andric case 'c': { 2018e8d8bef9SDimitry Andric int32_t count; 2019e8d8bef9SDimitry Andric if (option_arg.empty() || option_arg.getAsInteger(0, count) || 2020e8d8bef9SDimitry Andric count < 0) 2021e8d8bef9SDimitry Andric error.SetErrorStringWithFormat( 2022e8d8bef9SDimitry Andric "invalid integer value for option '%s'", 2023e8d8bef9SDimitry Andric option_arg.str().c_str()); 2024e8d8bef9SDimitry Andric else 2025e8d8bef9SDimitry Andric m_count = count; 2026e8d8bef9SDimitry Andric break; 2027e8d8bef9SDimitry Andric } 2028fe6060f1SDimitry Andric case 's': { 2029fe6060f1SDimitry Andric int32_t skip; 2030fe6060f1SDimitry Andric if (option_arg.empty() || option_arg.getAsInteger(0, skip) || skip < 0) 2031e8d8bef9SDimitry Andric error.SetErrorStringWithFormat( 2032e8d8bef9SDimitry Andric "invalid integer value for option '%s'", 2033e8d8bef9SDimitry Andric option_arg.str().c_str()); 2034e8d8bef9SDimitry Andric else 2035fe6060f1SDimitry Andric m_skip = skip; 2036e8d8bef9SDimitry Andric break; 2037e8d8bef9SDimitry Andric } 2038e8d8bef9SDimitry Andric case 'r': { 2039e8d8bef9SDimitry Andric m_raw = true; 2040e8d8bef9SDimitry Andric break; 2041e8d8bef9SDimitry Andric } 2042fe6060f1SDimitry Andric case 'f': { 2043fe6060f1SDimitry Andric m_forwards = true; 2044fe6060f1SDimitry Andric break; 2045fe6060f1SDimitry Andric } 2046fe6060f1SDimitry Andric case 't': { 2047fe6060f1SDimitry Andric m_show_tsc = true; 2048fe6060f1SDimitry Andric break; 2049fe6060f1SDimitry Andric } 2050e8d8bef9SDimitry Andric default: 2051e8d8bef9SDimitry Andric llvm_unreachable("Unimplemented option"); 2052e8d8bef9SDimitry Andric } 2053e8d8bef9SDimitry Andric return error; 2054e8d8bef9SDimitry Andric } 2055e8d8bef9SDimitry Andric 2056e8d8bef9SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 2057e8d8bef9SDimitry Andric m_count = kDefaultCount; 2058fe6060f1SDimitry Andric m_skip = 0; 2059e8d8bef9SDimitry Andric m_raw = false; 2060fe6060f1SDimitry Andric m_forwards = false; 2061fe6060f1SDimitry Andric m_show_tsc = false; 2062e8d8bef9SDimitry Andric } 2063e8d8bef9SDimitry Andric 2064e8d8bef9SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 2065e8d8bef9SDimitry Andric return llvm::makeArrayRef(g_thread_trace_dump_instructions_options); 2066e8d8bef9SDimitry Andric } 2067e8d8bef9SDimitry Andric 2068e8d8bef9SDimitry Andric static const size_t kDefaultCount = 20; 2069e8d8bef9SDimitry Andric 2070e8d8bef9SDimitry Andric // Instance variables to hold the values for command options. 2071e8d8bef9SDimitry Andric size_t m_count; 2072fe6060f1SDimitry Andric size_t m_skip; 2073e8d8bef9SDimitry Andric bool m_raw; 2074fe6060f1SDimitry Andric bool m_forwards; 2075fe6060f1SDimitry Andric bool m_show_tsc; 2076e8d8bef9SDimitry Andric }; 2077e8d8bef9SDimitry Andric 2078e8d8bef9SDimitry Andric CommandObjectTraceDumpInstructions(CommandInterpreter &interpreter) 2079e8d8bef9SDimitry Andric : CommandObjectIterateOverThreads( 2080e8d8bef9SDimitry Andric interpreter, "thread trace dump instructions", 2081e8d8bef9SDimitry Andric "Dump the traced instructions for one or more threads. If no " 2082e8d8bef9SDimitry Andric "threads are specified, show the current thread. Use the " 2083e8d8bef9SDimitry Andric "thread-index \"all\" to see all threads.", 2084e8d8bef9SDimitry Andric nullptr, 2085e8d8bef9SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 2086e8d8bef9SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | 2087e8d8bef9SDimitry Andric eCommandProcessMustBeTraced), 2088e8d8bef9SDimitry Andric m_options(), m_create_repeat_command_just_invoked(false) {} 2089e8d8bef9SDimitry Andric 2090e8d8bef9SDimitry Andric ~CommandObjectTraceDumpInstructions() override = default; 2091e8d8bef9SDimitry Andric 2092e8d8bef9SDimitry Andric Options *GetOptions() override { return &m_options; } 2093e8d8bef9SDimitry Andric 2094e8d8bef9SDimitry Andric const char *GetRepeatCommand(Args ¤t_command_args, 2095e8d8bef9SDimitry Andric uint32_t index) override { 2096e8d8bef9SDimitry Andric current_command_args.GetCommandString(m_repeat_command); 2097e8d8bef9SDimitry Andric m_create_repeat_command_just_invoked = true; 2098e8d8bef9SDimitry Andric return m_repeat_command.c_str(); 2099e8d8bef9SDimitry Andric } 2100e8d8bef9SDimitry Andric 2101e8d8bef9SDimitry Andric protected: 2102e8d8bef9SDimitry Andric bool DoExecute(Args &args, CommandReturnObject &result) override { 2103fe6060f1SDimitry Andric if (!IsRepeatCommand()) 2104fe6060f1SDimitry Andric m_dumpers.clear(); 2105fe6060f1SDimitry Andric 2106e8d8bef9SDimitry Andric bool status = CommandObjectIterateOverThreads::DoExecute(args, result); 2107e8d8bef9SDimitry Andric 2108e8d8bef9SDimitry Andric m_create_repeat_command_just_invoked = false; 2109e8d8bef9SDimitry Andric return status; 2110e8d8bef9SDimitry Andric } 2111e8d8bef9SDimitry Andric 2112e8d8bef9SDimitry Andric bool IsRepeatCommand() { 2113e8d8bef9SDimitry Andric return !m_repeat_command.empty() && !m_create_repeat_command_just_invoked; 2114e8d8bef9SDimitry Andric } 2115e8d8bef9SDimitry Andric 2116e8d8bef9SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 2117fe6060f1SDimitry Andric Stream &s = result.GetOutputStream(); 2118fe6060f1SDimitry Andric 2119e8d8bef9SDimitry Andric const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace(); 2120e8d8bef9SDimitry Andric ThreadSP thread_sp = 2121e8d8bef9SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 2122e8d8bef9SDimitry Andric 2123fe6060f1SDimitry Andric if (!m_dumpers.count(thread_sp->GetID())) { 2124fe6060f1SDimitry Andric lldb::TraceCursorUP cursor_up = trace_sp->GetCursor(*thread_sp); 2125fe6060f1SDimitry Andric // Set up the cursor and return the presentation index of the first 2126fe6060f1SDimitry Andric // instruction to dump after skipping instructions. 2127fe6060f1SDimitry Andric auto setUpCursor = [&]() { 2128fe6060f1SDimitry Andric cursor_up->SetForwards(m_options.m_forwards); 2129fe6060f1SDimitry Andric if (m_options.m_forwards) 2130fe6060f1SDimitry Andric return cursor_up->Seek(m_options.m_skip, TraceCursor::SeekType::Set); 2131fe6060f1SDimitry Andric return -cursor_up->Seek(-m_options.m_skip, TraceCursor::SeekType::End); 2132fe6060f1SDimitry Andric }; 2133fe6060f1SDimitry Andric 2134fe6060f1SDimitry Andric int initial_index = setUpCursor(); 2135fe6060f1SDimitry Andric 2136fe6060f1SDimitry Andric auto dumper = std::make_unique<TraceInstructionDumper>( 2137fe6060f1SDimitry Andric std::move(cursor_up), initial_index, m_options.m_raw, 2138fe6060f1SDimitry Andric m_options.m_show_tsc); 2139fe6060f1SDimitry Andric 2140fe6060f1SDimitry Andric // This happens when the seek value was more than the number of available 2141fe6060f1SDimitry Andric // instructions. 2142fe6060f1SDimitry Andric if (std::abs(initial_index) < (int)m_options.m_skip) 2143fe6060f1SDimitry Andric dumper->SetNoMoreData(); 2144fe6060f1SDimitry Andric 2145fe6060f1SDimitry Andric m_dumpers[thread_sp->GetID()] = std::move(dumper); 2146fe6060f1SDimitry Andric } 2147fe6060f1SDimitry Andric 2148fe6060f1SDimitry Andric m_dumpers[thread_sp->GetID()]->DumpInstructions(s, m_options.m_count); 2149e8d8bef9SDimitry Andric return true; 2150e8d8bef9SDimitry Andric } 2151e8d8bef9SDimitry Andric 2152e8d8bef9SDimitry Andric CommandOptions m_options; 2153e8d8bef9SDimitry Andric 2154e8d8bef9SDimitry Andric // Repeat command helpers 2155e8d8bef9SDimitry Andric std::string m_repeat_command; 2156e8d8bef9SDimitry Andric bool m_create_repeat_command_just_invoked; 2157fe6060f1SDimitry Andric std::map<lldb::tid_t, std::unique_ptr<TraceInstructionDumper>> m_dumpers; 2158fe6060f1SDimitry Andric }; 2159fe6060f1SDimitry Andric 2160fe6060f1SDimitry Andric // CommandObjectTraceDumpInfo 2161fe6060f1SDimitry Andric #define LLDB_OPTIONS_thread_trace_dump_info 2162fe6060f1SDimitry Andric #include "CommandOptions.inc" 2163fe6060f1SDimitry Andric 2164fe6060f1SDimitry Andric class CommandObjectTraceDumpInfo : public CommandObjectIterateOverThreads { 2165fe6060f1SDimitry Andric public: 2166fe6060f1SDimitry Andric class CommandOptions : public Options { 2167fe6060f1SDimitry Andric public: 2168fe6060f1SDimitry Andric CommandOptions() : Options() { OptionParsingStarting(nullptr); } 2169fe6060f1SDimitry Andric 2170fe6060f1SDimitry Andric ~CommandOptions() override = default; 2171fe6060f1SDimitry Andric 2172fe6060f1SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 2173fe6060f1SDimitry Andric ExecutionContext *execution_context) override { 2174fe6060f1SDimitry Andric Status error; 2175fe6060f1SDimitry Andric const int short_option = m_getopt_table[option_idx].val; 2176fe6060f1SDimitry Andric 2177fe6060f1SDimitry Andric switch (short_option) { 2178fe6060f1SDimitry Andric case 'v': { 2179fe6060f1SDimitry Andric m_verbose = true; 2180fe6060f1SDimitry Andric break; 2181fe6060f1SDimitry Andric } 2182fe6060f1SDimitry Andric default: 2183fe6060f1SDimitry Andric llvm_unreachable("Unimplemented option"); 2184fe6060f1SDimitry Andric } 2185fe6060f1SDimitry Andric return error; 2186fe6060f1SDimitry Andric } 2187fe6060f1SDimitry Andric 2188fe6060f1SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override { 2189fe6060f1SDimitry Andric m_verbose = false; 2190fe6060f1SDimitry Andric } 2191fe6060f1SDimitry Andric 2192fe6060f1SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 2193fe6060f1SDimitry Andric return llvm::makeArrayRef(g_thread_trace_dump_info_options); 2194fe6060f1SDimitry Andric } 2195fe6060f1SDimitry Andric 2196fe6060f1SDimitry Andric // Instance variables to hold the values for command options. 2197fe6060f1SDimitry Andric bool m_verbose; 2198fe6060f1SDimitry Andric }; 2199fe6060f1SDimitry Andric 2200fe6060f1SDimitry Andric bool DoExecute(Args &command, CommandReturnObject &result) override { 2201fe6060f1SDimitry Andric Target &target = m_exe_ctx.GetTargetRef(); 2202349cc55cSDimitry Andric result.GetOutputStream().Format("Trace technology: {0}\n", 2203349cc55cSDimitry Andric target.GetTrace()->GetPluginName()); 2204fe6060f1SDimitry Andric return CommandObjectIterateOverThreads::DoExecute(command, result); 2205fe6060f1SDimitry Andric } 2206fe6060f1SDimitry Andric 2207fe6060f1SDimitry Andric CommandObjectTraceDumpInfo(CommandInterpreter &interpreter) 2208fe6060f1SDimitry Andric : CommandObjectIterateOverThreads( 2209fe6060f1SDimitry Andric interpreter, "thread trace dump info", 2210fe6060f1SDimitry Andric "Dump the traced information for one or more threads. If no " 2211fe6060f1SDimitry Andric "threads are specified, show the current thread. Use the " 2212fe6060f1SDimitry Andric "thread-index \"all\" to see all threads.", 2213fe6060f1SDimitry Andric nullptr, 2214fe6060f1SDimitry Andric eCommandRequiresProcess | eCommandTryTargetAPILock | 2215fe6060f1SDimitry Andric eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | 2216fe6060f1SDimitry Andric eCommandProcessMustBeTraced), 2217fe6060f1SDimitry Andric m_options() {} 2218fe6060f1SDimitry Andric 2219fe6060f1SDimitry Andric ~CommandObjectTraceDumpInfo() override = default; 2220fe6060f1SDimitry Andric 2221fe6060f1SDimitry Andric Options *GetOptions() override { return &m_options; } 2222fe6060f1SDimitry Andric 2223fe6060f1SDimitry Andric protected: 2224fe6060f1SDimitry Andric bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 2225fe6060f1SDimitry Andric const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace(); 2226fe6060f1SDimitry Andric ThreadSP thread_sp = 2227fe6060f1SDimitry Andric m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 2228fe6060f1SDimitry Andric trace_sp->DumpTraceInfo(*thread_sp, result.GetOutputStream(), 2229fe6060f1SDimitry Andric m_options.m_verbose); 2230fe6060f1SDimitry Andric return true; 2231fe6060f1SDimitry Andric } 2232fe6060f1SDimitry Andric 2233fe6060f1SDimitry Andric CommandOptions m_options; 2234e8d8bef9SDimitry Andric }; 2235e8d8bef9SDimitry Andric 2236e8d8bef9SDimitry Andric // CommandObjectMultiwordTraceDump 2237e8d8bef9SDimitry Andric class CommandObjectMultiwordTraceDump : public CommandObjectMultiword { 2238e8d8bef9SDimitry Andric public: 2239e8d8bef9SDimitry Andric CommandObjectMultiwordTraceDump(CommandInterpreter &interpreter) 2240e8d8bef9SDimitry Andric : CommandObjectMultiword( 2241e8d8bef9SDimitry Andric interpreter, "dump", 2242e8d8bef9SDimitry Andric "Commands for displaying trace information of the threads " 2243e8d8bef9SDimitry Andric "in the current process.", 2244e8d8bef9SDimitry Andric "thread trace dump <subcommand> [<subcommand objects>]") { 2245e8d8bef9SDimitry Andric LoadSubCommand( 2246e8d8bef9SDimitry Andric "instructions", 2247e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectTraceDumpInstructions(interpreter))); 2248fe6060f1SDimitry Andric LoadSubCommand( 2249fe6060f1SDimitry Andric "info", CommandObjectSP(new CommandObjectTraceDumpInfo(interpreter))); 2250e8d8bef9SDimitry Andric } 2251e8d8bef9SDimitry Andric ~CommandObjectMultiwordTraceDump() override = default; 2252e8d8bef9SDimitry Andric }; 2253e8d8bef9SDimitry Andric 2254e8d8bef9SDimitry Andric // CommandObjectMultiwordTrace 2255e8d8bef9SDimitry Andric class CommandObjectMultiwordTrace : public CommandObjectMultiword { 2256e8d8bef9SDimitry Andric public: 2257e8d8bef9SDimitry Andric CommandObjectMultiwordTrace(CommandInterpreter &interpreter) 2258e8d8bef9SDimitry Andric : CommandObjectMultiword( 2259e8d8bef9SDimitry Andric interpreter, "trace", 2260e8d8bef9SDimitry Andric "Commands for operating on traces of the threads in the current " 2261e8d8bef9SDimitry Andric "process.", 2262e8d8bef9SDimitry Andric "thread trace <subcommand> [<subcommand objects>]") { 2263e8d8bef9SDimitry Andric LoadSubCommand("dump", CommandObjectSP(new CommandObjectMultiwordTraceDump( 2264e8d8bef9SDimitry Andric interpreter))); 2265e8d8bef9SDimitry Andric LoadSubCommand("start", 2266e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectTraceStart(interpreter))); 2267e8d8bef9SDimitry Andric LoadSubCommand("stop", 2268e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectTraceStop(interpreter))); 2269fe6060f1SDimitry Andric LoadSubCommand("export", 2270fe6060f1SDimitry Andric CommandObjectSP(new CommandObjectTraceExport(interpreter))); 2271e8d8bef9SDimitry Andric } 2272e8d8bef9SDimitry Andric 2273e8d8bef9SDimitry Andric ~CommandObjectMultiwordTrace() override = default; 2274e8d8bef9SDimitry Andric }; 2275e8d8bef9SDimitry Andric 2276*0b57cec5SDimitry Andric // CommandObjectMultiwordThread 2277*0b57cec5SDimitry Andric 2278*0b57cec5SDimitry Andric CommandObjectMultiwordThread::CommandObjectMultiwordThread( 2279*0b57cec5SDimitry Andric CommandInterpreter &interpreter) 2280480093f4SDimitry Andric : CommandObjectMultiword(interpreter, "thread", 2281480093f4SDimitry Andric "Commands for operating on " 2282*0b57cec5SDimitry Andric "one or more threads in " 2283*0b57cec5SDimitry Andric "the current process.", 2284*0b57cec5SDimitry Andric "thread <subcommand> [<subcommand-options>]") { 2285*0b57cec5SDimitry Andric LoadSubCommand("backtrace", CommandObjectSP(new CommandObjectThreadBacktrace( 2286*0b57cec5SDimitry Andric interpreter))); 2287*0b57cec5SDimitry Andric LoadSubCommand("continue", 2288*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadContinue(interpreter))); 2289*0b57cec5SDimitry Andric LoadSubCommand("list", 2290*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadList(interpreter))); 2291*0b57cec5SDimitry Andric LoadSubCommand("return", 2292*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadReturn(interpreter))); 2293*0b57cec5SDimitry Andric LoadSubCommand("jump", 2294*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadJump(interpreter))); 2295*0b57cec5SDimitry Andric LoadSubCommand("select", 2296*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadSelect(interpreter))); 2297*0b57cec5SDimitry Andric LoadSubCommand("until", 2298*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadUntil(interpreter))); 2299*0b57cec5SDimitry Andric LoadSubCommand("info", 2300*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadInfo(interpreter))); 2301480093f4SDimitry Andric LoadSubCommand("exception", CommandObjectSP(new CommandObjectThreadException( 2302480093f4SDimitry Andric interpreter))); 2303*0b57cec5SDimitry Andric LoadSubCommand("step-in", 2304*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2305*0b57cec5SDimitry Andric interpreter, "thread step-in", 2306*0b57cec5SDimitry Andric "Source level single step, stepping into calls. Defaults " 2307*0b57cec5SDimitry Andric "to current thread unless specified.", 2308*0b57cec5SDimitry Andric nullptr, eStepTypeInto, eStepScopeSource))); 2309*0b57cec5SDimitry Andric 2310*0b57cec5SDimitry Andric LoadSubCommand("step-out", 2311*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2312*0b57cec5SDimitry Andric interpreter, "thread step-out", 2313*0b57cec5SDimitry Andric "Finish executing the current stack frame and stop after " 2314*0b57cec5SDimitry Andric "returning. Defaults to current thread unless specified.", 2315*0b57cec5SDimitry Andric nullptr, eStepTypeOut, eStepScopeSource))); 2316*0b57cec5SDimitry Andric 2317*0b57cec5SDimitry Andric LoadSubCommand("step-over", 2318*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2319*0b57cec5SDimitry Andric interpreter, "thread step-over", 2320*0b57cec5SDimitry Andric "Source level single step, stepping over calls. Defaults " 2321*0b57cec5SDimitry Andric "to current thread unless specified.", 2322*0b57cec5SDimitry Andric nullptr, eStepTypeOver, eStepScopeSource))); 2323*0b57cec5SDimitry Andric 2324*0b57cec5SDimitry Andric LoadSubCommand("step-inst", 2325*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2326*0b57cec5SDimitry Andric interpreter, "thread step-inst", 2327*0b57cec5SDimitry Andric "Instruction level single step, stepping into calls. " 2328*0b57cec5SDimitry Andric "Defaults to current thread unless specified.", 2329*0b57cec5SDimitry Andric nullptr, eStepTypeTrace, eStepScopeInstruction))); 2330*0b57cec5SDimitry Andric 2331*0b57cec5SDimitry Andric LoadSubCommand("step-inst-over", 2332*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2333*0b57cec5SDimitry Andric interpreter, "thread step-inst-over", 2334*0b57cec5SDimitry Andric "Instruction level single step, stepping over calls. " 2335*0b57cec5SDimitry Andric "Defaults to current thread unless specified.", 2336*0b57cec5SDimitry Andric nullptr, eStepTypeTraceOver, eStepScopeInstruction))); 2337*0b57cec5SDimitry Andric 2338*0b57cec5SDimitry Andric LoadSubCommand( 2339*0b57cec5SDimitry Andric "step-scripted", 2340*0b57cec5SDimitry Andric CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2341*0b57cec5SDimitry Andric interpreter, "thread step-scripted", 23429dba64beSDimitry Andric "Step as instructed by the script class passed in the -C option. " 23439dba64beSDimitry Andric "You can also specify a dictionary of key (-k) and value (-v) pairs " 23449dba64beSDimitry Andric "that will be used to populate an SBStructuredData Dictionary, which " 23459dba64beSDimitry Andric "will be passed to the constructor of the class implementing the " 23469dba64beSDimitry Andric "scripted step. See the Python Reference for more details.", 2347*0b57cec5SDimitry Andric nullptr, eStepTypeScripted, eStepScopeSource))); 2348*0b57cec5SDimitry Andric 2349*0b57cec5SDimitry Andric LoadSubCommand("plan", CommandObjectSP(new CommandObjectMultiwordThreadPlan( 2350*0b57cec5SDimitry Andric interpreter))); 2351e8d8bef9SDimitry Andric LoadSubCommand("trace", 2352e8d8bef9SDimitry Andric CommandObjectSP(new CommandObjectMultiwordTrace(interpreter))); 2353*0b57cec5SDimitry Andric } 2354*0b57cec5SDimitry Andric 2355*0b57cec5SDimitry Andric CommandObjectMultiwordThread::~CommandObjectMultiwordThread() = default; 2356