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"
20fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
21*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
22*0b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
239dba64beSDimitry Andric #include "lldb/Interpreter/OptionGroupPythonClassWithDict.h"
24*0b57cec5SDimitry Andric #include "lldb/Interpreter/Options.h"
25*0b57cec5SDimitry Andric #include "lldb/Symbol/CompileUnit.h"
26*0b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
27*0b57cec5SDimitry Andric #include "lldb/Symbol/LineEntry.h"
28*0b57cec5SDimitry Andric #include "lldb/Symbol/LineTable.h"
29*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
30*0b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
31*0b57cec5SDimitry Andric #include "lldb/Target/SystemRuntime.h"
32*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
33*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
34*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlan.h"
35*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanStepInRange.h"
36e8d8bef9SDimitry Andric #include "lldb/Target/Trace.h"
3781ad6265SDimitry Andric #include "lldb/Target/TraceDumper.h"
38*0b57cec5SDimitry Andric #include "lldb/Utility/State.h"
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric using namespace lldb;
41*0b57cec5SDimitry Andric using namespace lldb_private;
42*0b57cec5SDimitry Andric 
43*0b57cec5SDimitry Andric // CommandObjectThreadBacktrace
44*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_backtrace
45*0b57cec5SDimitry Andric #include "CommandOptions.inc"
46*0b57cec5SDimitry Andric 
47*0b57cec5SDimitry Andric class CommandObjectThreadBacktrace : public CommandObjectIterateOverThreads {
48*0b57cec5SDimitry Andric public:
49*0b57cec5SDimitry Andric   class CommandOptions : public Options {
50*0b57cec5SDimitry Andric   public:
5104eeddc0SDimitry Andric     CommandOptions() {
52*0b57cec5SDimitry Andric       // Keep default values of all options in one place: OptionParsingStarting
53*0b57cec5SDimitry Andric       // ()
54*0b57cec5SDimitry Andric       OptionParsingStarting(nullptr);
55*0b57cec5SDimitry Andric     }
56*0b57cec5SDimitry Andric 
57*0b57cec5SDimitry Andric     ~CommandOptions() override = default;
58*0b57cec5SDimitry Andric 
59*0b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
60*0b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
61*0b57cec5SDimitry Andric       Status error;
62*0b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
63*0b57cec5SDimitry Andric 
64*0b57cec5SDimitry Andric       switch (short_option) {
65*0b57cec5SDimitry Andric       case 'c': {
66*0b57cec5SDimitry Andric         int32_t input_count = 0;
67*0b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, m_count)) {
68*0b57cec5SDimitry Andric           m_count = UINT32_MAX;
69*0b57cec5SDimitry Andric           error.SetErrorStringWithFormat(
70*0b57cec5SDimitry Andric               "invalid integer value for option '%c'", short_option);
71*0b57cec5SDimitry Andric         } else if (input_count < 0)
72*0b57cec5SDimitry Andric           m_count = UINT32_MAX;
73*0b57cec5SDimitry Andric       } break;
74*0b57cec5SDimitry Andric       case 's':
75*0b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, m_start))
76*0b57cec5SDimitry Andric           error.SetErrorStringWithFormat(
77*0b57cec5SDimitry Andric               "invalid integer value for option '%c'", short_option);
78*0b57cec5SDimitry Andric         break;
79*0b57cec5SDimitry Andric       case 'e': {
80*0b57cec5SDimitry Andric         bool success;
81*0b57cec5SDimitry Andric         m_extended_backtrace =
82*0b57cec5SDimitry Andric             OptionArgParser::ToBoolean(option_arg, false, &success);
83*0b57cec5SDimitry Andric         if (!success)
84*0b57cec5SDimitry Andric           error.SetErrorStringWithFormat(
85*0b57cec5SDimitry Andric               "invalid boolean value for option '%c'", short_option);
86*0b57cec5SDimitry Andric       } break;
87*0b57cec5SDimitry Andric       default:
889dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
89*0b57cec5SDimitry Andric       }
90*0b57cec5SDimitry Andric       return error;
91*0b57cec5SDimitry Andric     }
92*0b57cec5SDimitry Andric 
93*0b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
94*0b57cec5SDimitry Andric       m_count = UINT32_MAX;
95*0b57cec5SDimitry Andric       m_start = 0;
96*0b57cec5SDimitry Andric       m_extended_backtrace = false;
97*0b57cec5SDimitry Andric     }
98*0b57cec5SDimitry Andric 
99*0b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
100*0b57cec5SDimitry Andric       return llvm::makeArrayRef(g_thread_backtrace_options);
101*0b57cec5SDimitry Andric     }
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
104*0b57cec5SDimitry Andric     uint32_t m_count;
105*0b57cec5SDimitry Andric     uint32_t m_start;
106*0b57cec5SDimitry Andric     bool m_extended_backtrace;
107*0b57cec5SDimitry Andric   };
108*0b57cec5SDimitry Andric 
109*0b57cec5SDimitry Andric   CommandObjectThreadBacktrace(CommandInterpreter &interpreter)
110*0b57cec5SDimitry Andric       : CommandObjectIterateOverThreads(
111*0b57cec5SDimitry Andric             interpreter, "thread backtrace",
112*0b57cec5SDimitry Andric             "Show thread call stacks.  Defaults to the current thread, thread "
113*0b57cec5SDimitry Andric             "indexes can be specified as arguments.\n"
114*0b57cec5SDimitry Andric             "Use the thread-index \"all\" to see all threads.\n"
115*0b57cec5SDimitry Andric             "Use the thread-index \"unique\" to see threads grouped by unique "
116*0b57cec5SDimitry Andric             "call stacks.\n"
117*0b57cec5SDimitry Andric             "Use 'settings set frame-format' to customize the printing of "
118*0b57cec5SDimitry Andric             "frames in the backtrace and 'settings set thread-format' to "
119*0b57cec5SDimitry Andric             "customize the thread header.",
120*0b57cec5SDimitry Andric             nullptr,
121*0b57cec5SDimitry Andric             eCommandRequiresProcess | eCommandRequiresThread |
122*0b57cec5SDimitry Andric                 eCommandTryTargetAPILock | eCommandProcessMustBeLaunched |
12304eeddc0SDimitry Andric                 eCommandProcessMustBePaused) {}
124*0b57cec5SDimitry Andric 
125*0b57cec5SDimitry Andric   ~CommandObjectThreadBacktrace() override = default;
126*0b57cec5SDimitry Andric 
127*0b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
128*0b57cec5SDimitry Andric 
12981ad6265SDimitry Andric   llvm::Optional<std::string> GetRepeatCommand(Args &current_args,
13081ad6265SDimitry Andric                                                uint32_t idx) override {
13181ad6265SDimitry Andric     llvm::StringRef count_opt("--count");
13281ad6265SDimitry Andric     llvm::StringRef start_opt("--start");
13381ad6265SDimitry Andric 
13481ad6265SDimitry Andric     // If no "count" was provided, we are dumping the entire backtrace, so
13581ad6265SDimitry Andric     // there isn't a repeat command.  So we search for the count option in
13681ad6265SDimitry Andric     // the args, and if we find it, we make a copy and insert or modify the
13781ad6265SDimitry Andric     // start option's value to start count indices greater.
13881ad6265SDimitry Andric 
13981ad6265SDimitry Andric     Args copy_args(current_args);
14081ad6265SDimitry Andric     size_t num_entries = copy_args.GetArgumentCount();
14181ad6265SDimitry Andric     // These two point at the index of the option value if found.
14281ad6265SDimitry Andric     size_t count_idx = 0;
14381ad6265SDimitry Andric     size_t start_idx = 0;
14481ad6265SDimitry Andric     size_t count_val = 0;
14581ad6265SDimitry Andric     size_t start_val = 0;
14681ad6265SDimitry Andric 
14781ad6265SDimitry Andric     for (size_t idx = 0; idx < num_entries; idx++) {
14881ad6265SDimitry Andric       llvm::StringRef arg_string = copy_args[idx].ref();
14981ad6265SDimitry Andric       if (arg_string.equals("-c") || count_opt.startswith(arg_string)) {
15081ad6265SDimitry Andric         idx++;
15181ad6265SDimitry Andric         if (idx == num_entries)
15281ad6265SDimitry Andric           return llvm::None;
15381ad6265SDimitry Andric         count_idx = idx;
15481ad6265SDimitry Andric         if (copy_args[idx].ref().getAsInteger(0, count_val))
15581ad6265SDimitry Andric           return llvm::None;
15681ad6265SDimitry Andric       } else if (arg_string.equals("-s") || start_opt.startswith(arg_string)) {
15781ad6265SDimitry Andric         idx++;
15881ad6265SDimitry Andric         if (idx == num_entries)
15981ad6265SDimitry Andric           return llvm::None;
16081ad6265SDimitry Andric         start_idx = idx;
16181ad6265SDimitry Andric         if (copy_args[idx].ref().getAsInteger(0, start_val))
16281ad6265SDimitry Andric           return llvm::None;
16381ad6265SDimitry Andric       }
16481ad6265SDimitry Andric     }
16581ad6265SDimitry Andric     if (count_idx == 0)
16681ad6265SDimitry Andric       return llvm::None;
16781ad6265SDimitry Andric 
16881ad6265SDimitry Andric     std::string new_start_val = llvm::formatv("{0}", start_val + count_val);
16981ad6265SDimitry Andric     if (start_idx == 0) {
17081ad6265SDimitry Andric       copy_args.AppendArgument(start_opt);
17181ad6265SDimitry Andric       copy_args.AppendArgument(new_start_val);
17281ad6265SDimitry Andric     } else {
17381ad6265SDimitry Andric       copy_args.ReplaceArgumentAtIndex(start_idx, new_start_val);
17481ad6265SDimitry Andric     }
17581ad6265SDimitry Andric     std::string repeat_command;
17681ad6265SDimitry Andric     if (!copy_args.GetQuotedCommandString(repeat_command))
17781ad6265SDimitry Andric       return llvm::None;
17881ad6265SDimitry Andric     return repeat_command;
17981ad6265SDimitry Andric   }
18081ad6265SDimitry Andric 
181*0b57cec5SDimitry Andric protected:
182*0b57cec5SDimitry Andric   void DoExtendedBacktrace(Thread *thread, CommandReturnObject &result) {
183*0b57cec5SDimitry Andric     SystemRuntime *runtime = thread->GetProcess()->GetSystemRuntime();
184*0b57cec5SDimitry Andric     if (runtime) {
185*0b57cec5SDimitry Andric       Stream &strm = result.GetOutputStream();
186*0b57cec5SDimitry Andric       const std::vector<ConstString> &types =
187*0b57cec5SDimitry Andric           runtime->GetExtendedBacktraceTypes();
188*0b57cec5SDimitry Andric       for (auto type : types) {
189*0b57cec5SDimitry Andric         ThreadSP ext_thread_sp = runtime->GetExtendedBacktraceThread(
190*0b57cec5SDimitry Andric             thread->shared_from_this(), type);
191*0b57cec5SDimitry Andric         if (ext_thread_sp && ext_thread_sp->IsValid()) {
192*0b57cec5SDimitry Andric           const uint32_t num_frames_with_source = 0;
193*0b57cec5SDimitry Andric           const bool stop_format = false;
194*0b57cec5SDimitry Andric           if (ext_thread_sp->GetStatus(strm, m_options.m_start,
195*0b57cec5SDimitry Andric                                        m_options.m_count,
196480093f4SDimitry Andric                                        num_frames_with_source, stop_format)) {
197*0b57cec5SDimitry Andric             DoExtendedBacktrace(ext_thread_sp.get(), result);
198*0b57cec5SDimitry Andric           }
199*0b57cec5SDimitry Andric         }
200*0b57cec5SDimitry Andric       }
201*0b57cec5SDimitry Andric     }
202*0b57cec5SDimitry Andric   }
203*0b57cec5SDimitry Andric 
204*0b57cec5SDimitry Andric   bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override {
205*0b57cec5SDimitry Andric     ThreadSP thread_sp =
206*0b57cec5SDimitry Andric         m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
207*0b57cec5SDimitry Andric     if (!thread_sp) {
208*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
209*0b57cec5SDimitry Andric           "thread disappeared while computing backtraces: 0x%" PRIx64 "\n",
210*0b57cec5SDimitry Andric           tid);
211*0b57cec5SDimitry Andric       return false;
212*0b57cec5SDimitry Andric     }
213*0b57cec5SDimitry Andric 
214*0b57cec5SDimitry Andric     Thread *thread = thread_sp.get();
215*0b57cec5SDimitry Andric 
216*0b57cec5SDimitry Andric     Stream &strm = result.GetOutputStream();
217*0b57cec5SDimitry Andric 
218*0b57cec5SDimitry Andric     // Only dump stack info if we processing unique stacks.
219*0b57cec5SDimitry Andric     const bool only_stacks = m_unique_stacks;
220*0b57cec5SDimitry Andric 
221*0b57cec5SDimitry Andric     // Don't show source context when doing backtraces.
222*0b57cec5SDimitry Andric     const uint32_t num_frames_with_source = 0;
223*0b57cec5SDimitry Andric     const bool stop_format = true;
224*0b57cec5SDimitry Andric     if (!thread->GetStatus(strm, m_options.m_start, m_options.m_count,
225*0b57cec5SDimitry Andric                            num_frames_with_source, stop_format, only_stacks)) {
226*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
227*0b57cec5SDimitry Andric           "error displaying backtrace for thread: \"0x%4.4x\"\n",
228*0b57cec5SDimitry Andric           thread->GetIndexID());
229*0b57cec5SDimitry Andric       return false;
230*0b57cec5SDimitry Andric     }
231*0b57cec5SDimitry Andric     if (m_options.m_extended_backtrace) {
232*0b57cec5SDimitry Andric       DoExtendedBacktrace(thread, result);
233*0b57cec5SDimitry Andric     }
234*0b57cec5SDimitry Andric 
235*0b57cec5SDimitry Andric     return true;
236*0b57cec5SDimitry Andric   }
237*0b57cec5SDimitry Andric 
238*0b57cec5SDimitry Andric   CommandOptions m_options;
239*0b57cec5SDimitry Andric };
240*0b57cec5SDimitry Andric 
241*0b57cec5SDimitry Andric enum StepScope { eStepScopeSource, eStepScopeInstruction };
242*0b57cec5SDimitry Andric 
243*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_step_scope
244*0b57cec5SDimitry Andric #include "CommandOptions.inc"
245*0b57cec5SDimitry Andric 
2469dba64beSDimitry Andric class ThreadStepScopeOptionGroup : public OptionGroup {
247*0b57cec5SDimitry Andric public:
24804eeddc0SDimitry Andric   ThreadStepScopeOptionGroup() {
249*0b57cec5SDimitry Andric     // Keep default values of all options in one place: OptionParsingStarting
250*0b57cec5SDimitry Andric     // ()
251*0b57cec5SDimitry Andric     OptionParsingStarting(nullptr);
252*0b57cec5SDimitry Andric   }
253*0b57cec5SDimitry Andric 
2549dba64beSDimitry Andric   ~ThreadStepScopeOptionGroup() override = default;
2559dba64beSDimitry Andric 
2569dba64beSDimitry Andric   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
2579dba64beSDimitry Andric     return llvm::makeArrayRef(g_thread_step_scope_options);
2589dba64beSDimitry Andric   }
259*0b57cec5SDimitry Andric 
260*0b57cec5SDimitry Andric   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
261*0b57cec5SDimitry Andric                         ExecutionContext *execution_context) override {
262*0b57cec5SDimitry Andric     Status error;
263480093f4SDimitry Andric     const int short_option =
264480093f4SDimitry Andric         g_thread_step_scope_options[option_idx].short_option;
265*0b57cec5SDimitry Andric 
266*0b57cec5SDimitry Andric     switch (short_option) {
267*0b57cec5SDimitry Andric     case 'a': {
268*0b57cec5SDimitry Andric       bool success;
269*0b57cec5SDimitry Andric       bool avoid_no_debug =
270*0b57cec5SDimitry Andric           OptionArgParser::ToBoolean(option_arg, true, &success);
271*0b57cec5SDimitry Andric       if (!success)
272480093f4SDimitry Andric         error.SetErrorStringWithFormat("invalid boolean value for option '%c'",
273480093f4SDimitry Andric                                        short_option);
274*0b57cec5SDimitry Andric       else {
275480093f4SDimitry Andric         m_step_in_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo;
276*0b57cec5SDimitry Andric       }
277*0b57cec5SDimitry Andric     } break;
278*0b57cec5SDimitry Andric 
279*0b57cec5SDimitry Andric     case 'A': {
280*0b57cec5SDimitry Andric       bool success;
281*0b57cec5SDimitry Andric       bool avoid_no_debug =
282*0b57cec5SDimitry Andric           OptionArgParser::ToBoolean(option_arg, true, &success);
283*0b57cec5SDimitry Andric       if (!success)
284480093f4SDimitry Andric         error.SetErrorStringWithFormat("invalid boolean value for option '%c'",
285480093f4SDimitry Andric                                        short_option);
286*0b57cec5SDimitry Andric       else {
287480093f4SDimitry Andric         m_step_out_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo;
288*0b57cec5SDimitry Andric       }
289*0b57cec5SDimitry Andric     } break;
290*0b57cec5SDimitry Andric 
291*0b57cec5SDimitry Andric     case 'c':
292*0b57cec5SDimitry Andric       if (option_arg.getAsInteger(0, m_step_count))
293*0b57cec5SDimitry Andric         error.SetErrorStringWithFormat("invalid step count '%s'",
294*0b57cec5SDimitry Andric                                        option_arg.str().c_str());
295*0b57cec5SDimitry Andric       break;
296*0b57cec5SDimitry Andric 
297*0b57cec5SDimitry Andric     case 'm': {
298*0b57cec5SDimitry Andric       auto enum_values = GetDefinitions()[option_idx].enum_values;
299*0b57cec5SDimitry Andric       m_run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum(
300*0b57cec5SDimitry Andric           option_arg, enum_values, eOnlyDuringStepping, error);
301*0b57cec5SDimitry Andric     } break;
302*0b57cec5SDimitry Andric 
303*0b57cec5SDimitry Andric     case 'e':
304*0b57cec5SDimitry Andric       if (option_arg == "block") {
305*0b57cec5SDimitry Andric         m_end_line_is_block_end = true;
306*0b57cec5SDimitry Andric         break;
307*0b57cec5SDimitry Andric       }
308*0b57cec5SDimitry Andric       if (option_arg.getAsInteger(0, m_end_line))
309*0b57cec5SDimitry Andric         error.SetErrorStringWithFormat("invalid end line number '%s'",
310*0b57cec5SDimitry Andric                                        option_arg.str().c_str());
311*0b57cec5SDimitry Andric       break;
312*0b57cec5SDimitry Andric 
313*0b57cec5SDimitry Andric     case 'r':
314*0b57cec5SDimitry Andric       m_avoid_regexp.clear();
3155ffd83dbSDimitry Andric       m_avoid_regexp.assign(std::string(option_arg));
316*0b57cec5SDimitry Andric       break;
317*0b57cec5SDimitry Andric 
318*0b57cec5SDimitry Andric     case 't':
319*0b57cec5SDimitry Andric       m_step_in_target.clear();
3205ffd83dbSDimitry Andric       m_step_in_target.assign(std::string(option_arg));
321*0b57cec5SDimitry Andric       break;
322*0b57cec5SDimitry Andric 
323*0b57cec5SDimitry Andric     default:
3249dba64beSDimitry Andric       llvm_unreachable("Unimplemented option");
325*0b57cec5SDimitry Andric     }
326*0b57cec5SDimitry Andric     return error;
327*0b57cec5SDimitry Andric   }
328*0b57cec5SDimitry Andric 
329*0b57cec5SDimitry Andric   void OptionParsingStarting(ExecutionContext *execution_context) override {
330*0b57cec5SDimitry Andric     m_step_in_avoid_no_debug = eLazyBoolCalculate;
331*0b57cec5SDimitry Andric     m_step_out_avoid_no_debug = eLazyBoolCalculate;
332*0b57cec5SDimitry Andric     m_run_mode = eOnlyDuringStepping;
333*0b57cec5SDimitry Andric 
334*0b57cec5SDimitry Andric     // Check if we are in Non-Stop mode
335*0b57cec5SDimitry Andric     TargetSP target_sp =
336*0b57cec5SDimitry Andric         execution_context ? execution_context->GetTargetSP() : TargetSP();
337e8d8bef9SDimitry Andric     ProcessSP process_sp =
338e8d8bef9SDimitry Andric         execution_context ? execution_context->GetProcessSP() : ProcessSP();
339e8d8bef9SDimitry Andric     if (process_sp && process_sp->GetSteppingRunsAllThreads())
340e8d8bef9SDimitry Andric       m_run_mode = eAllThreads;
341*0b57cec5SDimitry Andric 
342*0b57cec5SDimitry Andric     m_avoid_regexp.clear();
343*0b57cec5SDimitry Andric     m_step_in_target.clear();
344*0b57cec5SDimitry Andric     m_step_count = 1;
345*0b57cec5SDimitry Andric     m_end_line = LLDB_INVALID_LINE_NUMBER;
346*0b57cec5SDimitry Andric     m_end_line_is_block_end = false;
347*0b57cec5SDimitry Andric   }
348*0b57cec5SDimitry Andric 
349*0b57cec5SDimitry Andric   // Instance variables to hold the values for command options.
350*0b57cec5SDimitry Andric   LazyBool m_step_in_avoid_no_debug;
351*0b57cec5SDimitry Andric   LazyBool m_step_out_avoid_no_debug;
352*0b57cec5SDimitry Andric   RunMode m_run_mode;
353*0b57cec5SDimitry Andric   std::string m_avoid_regexp;
354*0b57cec5SDimitry Andric   std::string m_step_in_target;
355*0b57cec5SDimitry Andric   uint32_t m_step_count;
356*0b57cec5SDimitry Andric   uint32_t m_end_line;
357*0b57cec5SDimitry Andric   bool m_end_line_is_block_end;
358*0b57cec5SDimitry Andric };
359*0b57cec5SDimitry Andric 
3609dba64beSDimitry Andric class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed {
3619dba64beSDimitry Andric public:
362*0b57cec5SDimitry Andric   CommandObjectThreadStepWithTypeAndScope(CommandInterpreter &interpreter,
363*0b57cec5SDimitry Andric                                           const char *name, const char *help,
364*0b57cec5SDimitry Andric                                           const char *syntax,
365*0b57cec5SDimitry Andric                                           StepType step_type,
366*0b57cec5SDimitry Andric                                           StepScope step_scope)
367*0b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, name, help, syntax,
368*0b57cec5SDimitry Andric                             eCommandRequiresProcess | eCommandRequiresThread |
369*0b57cec5SDimitry Andric                                 eCommandTryTargetAPILock |
370*0b57cec5SDimitry Andric                                 eCommandProcessMustBeLaunched |
371*0b57cec5SDimitry Andric                                 eCommandProcessMustBePaused),
37204eeddc0SDimitry Andric         m_step_type(step_type), m_step_scope(step_scope),
373480093f4SDimitry Andric         m_class_options("scripted step") {
374*0b57cec5SDimitry Andric     CommandArgumentEntry arg;
375*0b57cec5SDimitry Andric     CommandArgumentData thread_id_arg;
376*0b57cec5SDimitry Andric 
377*0b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
378*0b57cec5SDimitry Andric     thread_id_arg.arg_type = eArgTypeThreadID;
379*0b57cec5SDimitry Andric     thread_id_arg.arg_repetition = eArgRepeatOptional;
380*0b57cec5SDimitry Andric 
381*0b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
382*0b57cec5SDimitry Andric     // argument entry.
383*0b57cec5SDimitry Andric     arg.push_back(thread_id_arg);
384*0b57cec5SDimitry Andric 
385*0b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
386*0b57cec5SDimitry Andric     m_arguments.push_back(arg);
3879dba64beSDimitry Andric 
3889dba64beSDimitry Andric     if (step_type == eStepTypeScripted) {
389480093f4SDimitry Andric       m_all_options.Append(&m_class_options, LLDB_OPT_SET_1 | LLDB_OPT_SET_2,
390480093f4SDimitry Andric                            LLDB_OPT_SET_1);
3919dba64beSDimitry Andric     }
3929dba64beSDimitry Andric     m_all_options.Append(&m_options);
3939dba64beSDimitry Andric     m_all_options.Finalize();
394*0b57cec5SDimitry Andric   }
395*0b57cec5SDimitry Andric 
396*0b57cec5SDimitry Andric   ~CommandObjectThreadStepWithTypeAndScope() override = default;
397*0b57cec5SDimitry Andric 
398e8d8bef9SDimitry Andric   void
399e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
400e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
401e8d8bef9SDimitry Andric     if (request.GetCursorIndex())
402e8d8bef9SDimitry Andric       return;
403e8d8bef9SDimitry Andric 
404e8d8bef9SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
405e8d8bef9SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion,
406e8d8bef9SDimitry Andric         request, nullptr);
407e8d8bef9SDimitry Andric   }
408e8d8bef9SDimitry Andric 
409480093f4SDimitry Andric   Options *GetOptions() override { return &m_all_options; }
410*0b57cec5SDimitry Andric 
411*0b57cec5SDimitry Andric protected:
412*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
413*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
414*0b57cec5SDimitry Andric     bool synchronous_execution = m_interpreter.GetSynchronous();
415*0b57cec5SDimitry Andric 
416*0b57cec5SDimitry Andric     const uint32_t num_threads = process->GetThreadList().GetSize();
417*0b57cec5SDimitry Andric     Thread *thread = nullptr;
418*0b57cec5SDimitry Andric 
419*0b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
420*0b57cec5SDimitry Andric       thread = GetDefaultThread();
421*0b57cec5SDimitry Andric 
422*0b57cec5SDimitry Andric       if (thread == nullptr) {
423*0b57cec5SDimitry Andric         result.AppendError("no selected thread in process");
424*0b57cec5SDimitry Andric         return false;
425*0b57cec5SDimitry Andric       }
426*0b57cec5SDimitry Andric     } else {
427*0b57cec5SDimitry Andric       const char *thread_idx_cstr = command.GetArgumentAtIndex(0);
4285ffd83dbSDimitry Andric       uint32_t step_thread_idx;
4295ffd83dbSDimitry Andric 
4305ffd83dbSDimitry Andric       if (!llvm::to_integer(thread_idx_cstr, step_thread_idx)) {
431*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("invalid thread index '%s'.\n",
432*0b57cec5SDimitry Andric                                      thread_idx_cstr);
433*0b57cec5SDimitry Andric         return false;
434*0b57cec5SDimitry Andric       }
435*0b57cec5SDimitry Andric       thread =
436*0b57cec5SDimitry Andric           process->GetThreadList().FindThreadByIndexID(step_thread_idx).get();
437*0b57cec5SDimitry Andric       if (thread == nullptr) {
438*0b57cec5SDimitry Andric         result.AppendErrorWithFormat(
439*0b57cec5SDimitry Andric             "Thread index %u is out of range (valid values are 0 - %u).\n",
440*0b57cec5SDimitry Andric             step_thread_idx, num_threads);
441*0b57cec5SDimitry Andric         return false;
442*0b57cec5SDimitry Andric       }
443*0b57cec5SDimitry Andric     }
444*0b57cec5SDimitry Andric 
445*0b57cec5SDimitry Andric     if (m_step_type == eStepTypeScripted) {
446480093f4SDimitry Andric       if (m_class_options.GetName().empty()) {
447*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("empty class name for scripted step.");
448*0b57cec5SDimitry Andric         return false;
449*0b57cec5SDimitry Andric       } else if (!GetDebugger().GetScriptInterpreter()->CheckObjectExists(
450480093f4SDimitry Andric                      m_class_options.GetName().c_str())) {
451*0b57cec5SDimitry Andric         result.AppendErrorWithFormat(
452*0b57cec5SDimitry Andric             "class for scripted step: \"%s\" does not exist.",
453480093f4SDimitry Andric             m_class_options.GetName().c_str());
454*0b57cec5SDimitry Andric         return false;
455*0b57cec5SDimitry Andric       }
456*0b57cec5SDimitry Andric     }
457*0b57cec5SDimitry Andric 
458*0b57cec5SDimitry Andric     if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER &&
459*0b57cec5SDimitry Andric         m_step_type != eStepTypeInto) {
460*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
461*0b57cec5SDimitry Andric           "end line option is only valid for step into");
462*0b57cec5SDimitry Andric       return false;
463*0b57cec5SDimitry Andric     }
464*0b57cec5SDimitry Andric 
465*0b57cec5SDimitry Andric     const bool abort_other_plans = false;
466*0b57cec5SDimitry Andric     const lldb::RunMode stop_other_threads = m_options.m_run_mode;
467*0b57cec5SDimitry Andric 
468*0b57cec5SDimitry Andric     // This is a bit unfortunate, but not all the commands in this command
469*0b57cec5SDimitry Andric     // object support only while stepping, so I use the bool for them.
470*0b57cec5SDimitry Andric     bool bool_stop_other_threads;
471*0b57cec5SDimitry Andric     if (m_options.m_run_mode == eAllThreads)
472*0b57cec5SDimitry Andric       bool_stop_other_threads = false;
473*0b57cec5SDimitry Andric     else if (m_options.m_run_mode == eOnlyDuringStepping)
474e8d8bef9SDimitry Andric       bool_stop_other_threads = (m_step_type != eStepTypeOut);
475*0b57cec5SDimitry Andric     else
476*0b57cec5SDimitry Andric       bool_stop_other_threads = true;
477*0b57cec5SDimitry Andric 
478*0b57cec5SDimitry Andric     ThreadPlanSP new_plan_sp;
479*0b57cec5SDimitry Andric     Status new_plan_status;
480*0b57cec5SDimitry Andric 
481*0b57cec5SDimitry Andric     if (m_step_type == eStepTypeInto) {
482*0b57cec5SDimitry Andric       StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
483*0b57cec5SDimitry Andric       assert(frame != nullptr);
484*0b57cec5SDimitry Andric 
485*0b57cec5SDimitry Andric       if (frame->HasDebugInformation()) {
486*0b57cec5SDimitry Andric         AddressRange range;
487*0b57cec5SDimitry Andric         SymbolContext sc = frame->GetSymbolContext(eSymbolContextEverything);
488*0b57cec5SDimitry Andric         if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER) {
489*0b57cec5SDimitry Andric           Status error;
490*0b57cec5SDimitry Andric           if (!sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range,
491*0b57cec5SDimitry Andric                                                    error)) {
492*0b57cec5SDimitry Andric             result.AppendErrorWithFormat("invalid end-line option: %s.",
493*0b57cec5SDimitry Andric                                          error.AsCString());
494*0b57cec5SDimitry Andric             return false;
495*0b57cec5SDimitry Andric           }
496*0b57cec5SDimitry Andric         } else if (m_options.m_end_line_is_block_end) {
497*0b57cec5SDimitry Andric           Status error;
498*0b57cec5SDimitry Andric           Block *block = frame->GetSymbolContext(eSymbolContextBlock).block;
499*0b57cec5SDimitry Andric           if (!block) {
500*0b57cec5SDimitry Andric             result.AppendErrorWithFormat("Could not find the current block.");
501*0b57cec5SDimitry Andric             return false;
502*0b57cec5SDimitry Andric           }
503*0b57cec5SDimitry Andric 
504*0b57cec5SDimitry Andric           AddressRange block_range;
505*0b57cec5SDimitry Andric           Address pc_address = frame->GetFrameCodeAddress();
506*0b57cec5SDimitry Andric           block->GetRangeContainingAddress(pc_address, block_range);
507*0b57cec5SDimitry Andric           if (!block_range.GetBaseAddress().IsValid()) {
508*0b57cec5SDimitry Andric             result.AppendErrorWithFormat(
509*0b57cec5SDimitry Andric                 "Could not find the current block address.");
510*0b57cec5SDimitry Andric             return false;
511*0b57cec5SDimitry Andric           }
512*0b57cec5SDimitry Andric           lldb::addr_t pc_offset_in_block =
513*0b57cec5SDimitry Andric               pc_address.GetFileAddress() -
514*0b57cec5SDimitry Andric               block_range.GetBaseAddress().GetFileAddress();
515*0b57cec5SDimitry Andric           lldb::addr_t range_length =
516*0b57cec5SDimitry Andric               block_range.GetByteSize() - pc_offset_in_block;
517*0b57cec5SDimitry Andric           range = AddressRange(pc_address, range_length);
518*0b57cec5SDimitry Andric         } else {
519*0b57cec5SDimitry Andric           range = sc.line_entry.range;
520*0b57cec5SDimitry Andric         }
521*0b57cec5SDimitry Andric 
522*0b57cec5SDimitry Andric         new_plan_sp = thread->QueueThreadPlanForStepInRange(
523*0b57cec5SDimitry Andric             abort_other_plans, range,
524*0b57cec5SDimitry Andric             frame->GetSymbolContext(eSymbolContextEverything),
525*0b57cec5SDimitry Andric             m_options.m_step_in_target.c_str(), stop_other_threads,
526*0b57cec5SDimitry Andric             new_plan_status, m_options.m_step_in_avoid_no_debug,
527*0b57cec5SDimitry Andric             m_options.m_step_out_avoid_no_debug);
528*0b57cec5SDimitry Andric 
529*0b57cec5SDimitry Andric         if (new_plan_sp && !m_options.m_avoid_regexp.empty()) {
530*0b57cec5SDimitry Andric           ThreadPlanStepInRange *step_in_range_plan =
531*0b57cec5SDimitry Andric               static_cast<ThreadPlanStepInRange *>(new_plan_sp.get());
532*0b57cec5SDimitry Andric           step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str());
533*0b57cec5SDimitry Andric         }
534*0b57cec5SDimitry Andric       } else
535*0b57cec5SDimitry Andric         new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
536*0b57cec5SDimitry Andric             false, abort_other_plans, bool_stop_other_threads, new_plan_status);
537*0b57cec5SDimitry Andric     } else if (m_step_type == eStepTypeOver) {
538*0b57cec5SDimitry Andric       StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
539*0b57cec5SDimitry Andric 
540*0b57cec5SDimitry Andric       if (frame->HasDebugInformation())
541*0b57cec5SDimitry Andric         new_plan_sp = thread->QueueThreadPlanForStepOverRange(
542*0b57cec5SDimitry Andric             abort_other_plans,
543*0b57cec5SDimitry Andric             frame->GetSymbolContext(eSymbolContextEverything).line_entry,
544*0b57cec5SDimitry Andric             frame->GetSymbolContext(eSymbolContextEverything),
545*0b57cec5SDimitry Andric             stop_other_threads, new_plan_status,
546*0b57cec5SDimitry Andric             m_options.m_step_out_avoid_no_debug);
547*0b57cec5SDimitry Andric       else
548*0b57cec5SDimitry Andric         new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
549*0b57cec5SDimitry Andric             true, abort_other_plans, bool_stop_other_threads, new_plan_status);
550*0b57cec5SDimitry Andric     } else if (m_step_type == eStepTypeTrace) {
551*0b57cec5SDimitry Andric       new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
552*0b57cec5SDimitry Andric           false, abort_other_plans, bool_stop_other_threads, new_plan_status);
553*0b57cec5SDimitry Andric     } else if (m_step_type == eStepTypeTraceOver) {
554*0b57cec5SDimitry Andric       new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
555*0b57cec5SDimitry Andric           true, abort_other_plans, bool_stop_other_threads, new_plan_status);
556*0b57cec5SDimitry Andric     } else if (m_step_type == eStepTypeOut) {
557*0b57cec5SDimitry Andric       new_plan_sp = thread->QueueThreadPlanForStepOut(
558*0b57cec5SDimitry Andric           abort_other_plans, nullptr, false, bool_stop_other_threads, eVoteYes,
559*0b57cec5SDimitry Andric           eVoteNoOpinion, thread->GetSelectedFrameIndex(), new_plan_status,
560*0b57cec5SDimitry Andric           m_options.m_step_out_avoid_no_debug);
561*0b57cec5SDimitry Andric     } else if (m_step_type == eStepTypeScripted) {
562*0b57cec5SDimitry Andric       new_plan_sp = thread->QueueThreadPlanForStepScripted(
563480093f4SDimitry Andric           abort_other_plans, m_class_options.GetName().c_str(),
564480093f4SDimitry Andric           m_class_options.GetStructuredData(), bool_stop_other_threads,
565480093f4SDimitry Andric           new_plan_status);
566*0b57cec5SDimitry Andric     } else {
567*0b57cec5SDimitry Andric       result.AppendError("step type is not supported");
568*0b57cec5SDimitry Andric       return false;
569*0b57cec5SDimitry Andric     }
570*0b57cec5SDimitry Andric 
571349cc55cSDimitry Andric     // If we got a new plan, then set it to be a controlling plan (User level
572349cc55cSDimitry Andric     // Plans should be controlling plans so that they can be interruptible).
573349cc55cSDimitry Andric     // Then resume the process.
574*0b57cec5SDimitry Andric 
575*0b57cec5SDimitry Andric     if (new_plan_sp) {
576349cc55cSDimitry Andric       new_plan_sp->SetIsControllingPlan(true);
577*0b57cec5SDimitry Andric       new_plan_sp->SetOkayToDiscard(false);
578*0b57cec5SDimitry Andric 
579*0b57cec5SDimitry Andric       if (m_options.m_step_count > 1) {
580*0b57cec5SDimitry Andric         if (!new_plan_sp->SetIterationCount(m_options.m_step_count)) {
581*0b57cec5SDimitry Andric           result.AppendWarning(
582*0b57cec5SDimitry Andric               "step operation does not support iteration count.");
583*0b57cec5SDimitry Andric         }
584*0b57cec5SDimitry Andric       }
585*0b57cec5SDimitry Andric 
586*0b57cec5SDimitry Andric       process->GetThreadList().SetSelectedThreadByID(thread->GetID());
587*0b57cec5SDimitry Andric 
588*0b57cec5SDimitry Andric       const uint32_t iohandler_id = process->GetIOHandlerID();
589*0b57cec5SDimitry Andric 
590*0b57cec5SDimitry Andric       StreamString stream;
591*0b57cec5SDimitry Andric       Status error;
592*0b57cec5SDimitry Andric       if (synchronous_execution)
593*0b57cec5SDimitry Andric         error = process->ResumeSynchronous(&stream);
594*0b57cec5SDimitry Andric       else
595*0b57cec5SDimitry Andric         error = process->Resume();
596*0b57cec5SDimitry Andric 
597*0b57cec5SDimitry Andric       if (!error.Success()) {
598*0b57cec5SDimitry Andric         result.AppendMessage(error.AsCString());
599*0b57cec5SDimitry Andric         return false;
600*0b57cec5SDimitry Andric       }
601*0b57cec5SDimitry Andric 
602*0b57cec5SDimitry Andric       // There is a race condition where this thread will return up the call
603*0b57cec5SDimitry Andric       // stack to the main command handler and show an (lldb) prompt before
604*0b57cec5SDimitry Andric       // HandlePrivateEvent (from PrivateStateThread) has a chance to call
605*0b57cec5SDimitry Andric       // PushProcessIOHandler().
606*0b57cec5SDimitry Andric       process->SyncIOHandler(iohandler_id, std::chrono::seconds(2));
607*0b57cec5SDimitry Andric 
608*0b57cec5SDimitry Andric       if (synchronous_execution) {
609*0b57cec5SDimitry Andric         // If any state changed events had anything to say, add that to the
610*0b57cec5SDimitry Andric         // result
611*0b57cec5SDimitry Andric         if (stream.GetSize() > 0)
612*0b57cec5SDimitry Andric           result.AppendMessage(stream.GetString());
613*0b57cec5SDimitry Andric 
614*0b57cec5SDimitry Andric         process->GetThreadList().SetSelectedThreadByID(thread->GetID());
615*0b57cec5SDimitry Andric         result.SetDidChangeProcessState(true);
616*0b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishNoResult);
617*0b57cec5SDimitry Andric       } else {
618*0b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessContinuingNoResult);
619*0b57cec5SDimitry Andric       }
620*0b57cec5SDimitry Andric     } else {
621*0b57cec5SDimitry Andric       result.SetError(new_plan_status);
622*0b57cec5SDimitry Andric     }
623*0b57cec5SDimitry Andric     return result.Succeeded();
624*0b57cec5SDimitry Andric   }
625*0b57cec5SDimitry Andric 
626*0b57cec5SDimitry Andric   StepType m_step_type;
627*0b57cec5SDimitry Andric   StepScope m_step_scope;
6289dba64beSDimitry Andric   ThreadStepScopeOptionGroup m_options;
6299dba64beSDimitry Andric   OptionGroupPythonClassWithDict m_class_options;
6309dba64beSDimitry Andric   OptionGroupOptions m_all_options;
631*0b57cec5SDimitry Andric };
632*0b57cec5SDimitry Andric 
633*0b57cec5SDimitry Andric // CommandObjectThreadContinue
634*0b57cec5SDimitry Andric 
635*0b57cec5SDimitry Andric class CommandObjectThreadContinue : public CommandObjectParsed {
636*0b57cec5SDimitry Andric public:
637*0b57cec5SDimitry Andric   CommandObjectThreadContinue(CommandInterpreter &interpreter)
638*0b57cec5SDimitry Andric       : CommandObjectParsed(
639*0b57cec5SDimitry Andric             interpreter, "thread continue",
640*0b57cec5SDimitry Andric             "Continue execution of the current target process.  One "
641*0b57cec5SDimitry Andric             "or more threads may be specified, by default all "
642*0b57cec5SDimitry Andric             "threads continue.",
643*0b57cec5SDimitry Andric             nullptr,
644*0b57cec5SDimitry Andric             eCommandRequiresThread | eCommandTryTargetAPILock |
645*0b57cec5SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {
646*0b57cec5SDimitry Andric     CommandArgumentEntry arg;
647*0b57cec5SDimitry Andric     CommandArgumentData thread_idx_arg;
648*0b57cec5SDimitry Andric 
649*0b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
650*0b57cec5SDimitry Andric     thread_idx_arg.arg_type = eArgTypeThreadIndex;
651*0b57cec5SDimitry Andric     thread_idx_arg.arg_repetition = eArgRepeatPlus;
652*0b57cec5SDimitry Andric 
653*0b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
654*0b57cec5SDimitry Andric     // argument entry.
655*0b57cec5SDimitry Andric     arg.push_back(thread_idx_arg);
656*0b57cec5SDimitry Andric 
657*0b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
658*0b57cec5SDimitry Andric     m_arguments.push_back(arg);
659*0b57cec5SDimitry Andric   }
660*0b57cec5SDimitry Andric 
661*0b57cec5SDimitry Andric   ~CommandObjectThreadContinue() override = default;
662*0b57cec5SDimitry Andric 
663e8d8bef9SDimitry Andric   void
664e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
665e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
666e8d8bef9SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
667e8d8bef9SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion,
668e8d8bef9SDimitry Andric         request, nullptr);
669e8d8bef9SDimitry Andric   }
670e8d8bef9SDimitry Andric 
671*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
672*0b57cec5SDimitry Andric     bool synchronous_execution = m_interpreter.GetSynchronous();
673*0b57cec5SDimitry Andric 
674*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
675*0b57cec5SDimitry Andric     if (process == nullptr) {
676*0b57cec5SDimitry Andric       result.AppendError("no process exists. Cannot continue");
677*0b57cec5SDimitry Andric       return false;
678*0b57cec5SDimitry Andric     }
679*0b57cec5SDimitry Andric 
680*0b57cec5SDimitry Andric     StateType state = process->GetState();
681*0b57cec5SDimitry Andric     if ((state == eStateCrashed) || (state == eStateStopped) ||
682*0b57cec5SDimitry Andric         (state == eStateSuspended)) {
683*0b57cec5SDimitry Andric       const size_t argc = command.GetArgumentCount();
684*0b57cec5SDimitry Andric       if (argc > 0) {
685*0b57cec5SDimitry Andric         // These two lines appear at the beginning of both blocks in this
686*0b57cec5SDimitry Andric         // if..else, but that is because we need to release the lock before
687*0b57cec5SDimitry Andric         // calling process->Resume below.
688*0b57cec5SDimitry Andric         std::lock_guard<std::recursive_mutex> guard(
689*0b57cec5SDimitry Andric             process->GetThreadList().GetMutex());
690*0b57cec5SDimitry Andric         const uint32_t num_threads = process->GetThreadList().GetSize();
691*0b57cec5SDimitry Andric         std::vector<Thread *> resume_threads;
692*0b57cec5SDimitry Andric         for (auto &entry : command.entries()) {
693*0b57cec5SDimitry Andric           uint32_t thread_idx;
6949dba64beSDimitry Andric           if (entry.ref().getAsInteger(0, thread_idx)) {
695*0b57cec5SDimitry Andric             result.AppendErrorWithFormat(
696*0b57cec5SDimitry Andric                 "invalid thread index argument: \"%s\".\n", entry.c_str());
697*0b57cec5SDimitry Andric             return false;
698*0b57cec5SDimitry Andric           }
699*0b57cec5SDimitry Andric           Thread *thread =
700*0b57cec5SDimitry Andric               process->GetThreadList().FindThreadByIndexID(thread_idx).get();
701*0b57cec5SDimitry Andric 
702*0b57cec5SDimitry Andric           if (thread) {
703*0b57cec5SDimitry Andric             resume_threads.push_back(thread);
704*0b57cec5SDimitry Andric           } else {
705*0b57cec5SDimitry Andric             result.AppendErrorWithFormat("invalid thread index %u.\n",
706*0b57cec5SDimitry Andric                                          thread_idx);
707*0b57cec5SDimitry Andric             return false;
708*0b57cec5SDimitry Andric           }
709*0b57cec5SDimitry Andric         }
710*0b57cec5SDimitry Andric 
711*0b57cec5SDimitry Andric         if (resume_threads.empty()) {
712*0b57cec5SDimitry Andric           result.AppendError("no valid thread indexes were specified");
713*0b57cec5SDimitry Andric           return false;
714*0b57cec5SDimitry Andric         } else {
715*0b57cec5SDimitry Andric           if (resume_threads.size() == 1)
716*0b57cec5SDimitry Andric             result.AppendMessageWithFormat("Resuming thread: ");
717*0b57cec5SDimitry Andric           else
718*0b57cec5SDimitry Andric             result.AppendMessageWithFormat("Resuming threads: ");
719*0b57cec5SDimitry Andric 
720*0b57cec5SDimitry Andric           for (uint32_t idx = 0; idx < num_threads; ++idx) {
721*0b57cec5SDimitry Andric             Thread *thread =
722*0b57cec5SDimitry Andric                 process->GetThreadList().GetThreadAtIndex(idx).get();
723*0b57cec5SDimitry Andric             std::vector<Thread *>::iterator this_thread_pos =
724*0b57cec5SDimitry Andric                 find(resume_threads.begin(), resume_threads.end(), thread);
725*0b57cec5SDimitry Andric 
726*0b57cec5SDimitry Andric             if (this_thread_pos != resume_threads.end()) {
727*0b57cec5SDimitry Andric               resume_threads.erase(this_thread_pos);
728*0b57cec5SDimitry Andric               if (!resume_threads.empty())
729*0b57cec5SDimitry Andric                 result.AppendMessageWithFormat("%u, ", thread->GetIndexID());
730*0b57cec5SDimitry Andric               else
731*0b57cec5SDimitry Andric                 result.AppendMessageWithFormat("%u ", thread->GetIndexID());
732*0b57cec5SDimitry Andric 
733*0b57cec5SDimitry Andric               const bool override_suspend = true;
734*0b57cec5SDimitry Andric               thread->SetResumeState(eStateRunning, override_suspend);
735*0b57cec5SDimitry Andric             } else {
736*0b57cec5SDimitry Andric               thread->SetResumeState(eStateSuspended);
737*0b57cec5SDimitry Andric             }
738*0b57cec5SDimitry Andric           }
739*0b57cec5SDimitry Andric           result.AppendMessageWithFormat("in process %" PRIu64 "\n",
740*0b57cec5SDimitry Andric                                          process->GetID());
741*0b57cec5SDimitry Andric         }
742*0b57cec5SDimitry Andric       } else {
743*0b57cec5SDimitry Andric         // These two lines appear at the beginning of both blocks in this
744*0b57cec5SDimitry Andric         // if..else, but that is because we need to release the lock before
745*0b57cec5SDimitry Andric         // calling process->Resume below.
746*0b57cec5SDimitry Andric         std::lock_guard<std::recursive_mutex> guard(
747*0b57cec5SDimitry Andric             process->GetThreadList().GetMutex());
748*0b57cec5SDimitry Andric         const uint32_t num_threads = process->GetThreadList().GetSize();
749*0b57cec5SDimitry Andric         Thread *current_thread = GetDefaultThread();
750*0b57cec5SDimitry Andric         if (current_thread == nullptr) {
751*0b57cec5SDimitry Andric           result.AppendError("the process doesn't have a current thread");
752*0b57cec5SDimitry Andric           return false;
753*0b57cec5SDimitry Andric         }
754*0b57cec5SDimitry Andric         // Set the actions that the threads should each take when resuming
755*0b57cec5SDimitry Andric         for (uint32_t idx = 0; idx < num_threads; ++idx) {
756*0b57cec5SDimitry Andric           Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get();
757*0b57cec5SDimitry Andric           if (thread == current_thread) {
758*0b57cec5SDimitry Andric             result.AppendMessageWithFormat("Resuming thread 0x%4.4" PRIx64
759*0b57cec5SDimitry Andric                                            " in process %" PRIu64 "\n",
760*0b57cec5SDimitry Andric                                            thread->GetID(), process->GetID());
761*0b57cec5SDimitry Andric             const bool override_suspend = true;
762*0b57cec5SDimitry Andric             thread->SetResumeState(eStateRunning, override_suspend);
763*0b57cec5SDimitry Andric           } else {
764*0b57cec5SDimitry Andric             thread->SetResumeState(eStateSuspended);
765*0b57cec5SDimitry Andric           }
766*0b57cec5SDimitry Andric         }
767*0b57cec5SDimitry Andric       }
768*0b57cec5SDimitry Andric 
769*0b57cec5SDimitry Andric       StreamString stream;
770*0b57cec5SDimitry Andric       Status error;
771*0b57cec5SDimitry Andric       if (synchronous_execution)
772*0b57cec5SDimitry Andric         error = process->ResumeSynchronous(&stream);
773*0b57cec5SDimitry Andric       else
774*0b57cec5SDimitry Andric         error = process->Resume();
775*0b57cec5SDimitry Andric 
776*0b57cec5SDimitry Andric       // We should not be holding the thread list lock when we do this.
777*0b57cec5SDimitry Andric       if (error.Success()) {
778*0b57cec5SDimitry Andric         result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
779*0b57cec5SDimitry Andric                                        process->GetID());
780*0b57cec5SDimitry Andric         if (synchronous_execution) {
781*0b57cec5SDimitry Andric           // If any state changed events had anything to say, add that to the
782*0b57cec5SDimitry Andric           // result
783*0b57cec5SDimitry Andric           if (stream.GetSize() > 0)
784*0b57cec5SDimitry Andric             result.AppendMessage(stream.GetString());
785*0b57cec5SDimitry Andric 
786*0b57cec5SDimitry Andric           result.SetDidChangeProcessState(true);
787*0b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessFinishNoResult);
788*0b57cec5SDimitry Andric         } else {
789*0b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessContinuingNoResult);
790*0b57cec5SDimitry Andric         }
791*0b57cec5SDimitry Andric       } else {
792*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("Failed to resume process: %s\n",
793*0b57cec5SDimitry Andric                                      error.AsCString());
794*0b57cec5SDimitry Andric       }
795*0b57cec5SDimitry Andric     } else {
796*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
797*0b57cec5SDimitry Andric           "Process cannot be continued from its current state (%s).\n",
798*0b57cec5SDimitry Andric           StateAsCString(state));
799*0b57cec5SDimitry Andric     }
800*0b57cec5SDimitry Andric 
801*0b57cec5SDimitry Andric     return result.Succeeded();
802*0b57cec5SDimitry Andric   }
803*0b57cec5SDimitry Andric };
804*0b57cec5SDimitry Andric 
805*0b57cec5SDimitry Andric // CommandObjectThreadUntil
806*0b57cec5SDimitry Andric 
807*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_until
808*0b57cec5SDimitry Andric #include "CommandOptions.inc"
809*0b57cec5SDimitry Andric 
810*0b57cec5SDimitry Andric class CommandObjectThreadUntil : public CommandObjectParsed {
811*0b57cec5SDimitry Andric public:
812*0b57cec5SDimitry Andric   class CommandOptions : public Options {
813*0b57cec5SDimitry Andric   public:
814fe6060f1SDimitry Andric     uint32_t m_thread_idx = LLDB_INVALID_THREAD_ID;
815fe6060f1SDimitry Andric     uint32_t m_frame_idx = LLDB_INVALID_FRAME_ID;
816*0b57cec5SDimitry Andric 
81704eeddc0SDimitry Andric     CommandOptions() {
818*0b57cec5SDimitry Andric       // Keep default values of all options in one place: OptionParsingStarting
819*0b57cec5SDimitry Andric       // ()
820*0b57cec5SDimitry Andric       OptionParsingStarting(nullptr);
821*0b57cec5SDimitry Andric     }
822*0b57cec5SDimitry Andric 
823*0b57cec5SDimitry Andric     ~CommandOptions() override = default;
824*0b57cec5SDimitry Andric 
825*0b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
826*0b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
827*0b57cec5SDimitry Andric       Status error;
828*0b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
829*0b57cec5SDimitry Andric 
830*0b57cec5SDimitry Andric       switch (short_option) {
831*0b57cec5SDimitry Andric       case 'a': {
832*0b57cec5SDimitry Andric         lldb::addr_t tmp_addr = OptionArgParser::ToAddress(
833*0b57cec5SDimitry Andric             execution_context, option_arg, LLDB_INVALID_ADDRESS, &error);
834*0b57cec5SDimitry Andric         if (error.Success())
835*0b57cec5SDimitry Andric           m_until_addrs.push_back(tmp_addr);
836*0b57cec5SDimitry Andric       } break;
837*0b57cec5SDimitry Andric       case 't':
838*0b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, m_thread_idx)) {
839*0b57cec5SDimitry Andric           m_thread_idx = LLDB_INVALID_INDEX32;
840*0b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid thread index '%s'",
841*0b57cec5SDimitry Andric                                          option_arg.str().c_str());
842*0b57cec5SDimitry Andric         }
843*0b57cec5SDimitry Andric         break;
844*0b57cec5SDimitry Andric       case 'f':
845*0b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, m_frame_idx)) {
846*0b57cec5SDimitry Andric           m_frame_idx = LLDB_INVALID_FRAME_ID;
847*0b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid frame index '%s'",
848*0b57cec5SDimitry Andric                                          option_arg.str().c_str());
849*0b57cec5SDimitry Andric         }
850*0b57cec5SDimitry Andric         break;
851*0b57cec5SDimitry Andric       case 'm': {
852*0b57cec5SDimitry Andric         auto enum_values = GetDefinitions()[option_idx].enum_values;
853*0b57cec5SDimitry Andric         lldb::RunMode run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum(
854*0b57cec5SDimitry Andric             option_arg, enum_values, eOnlyDuringStepping, error);
855*0b57cec5SDimitry Andric 
856*0b57cec5SDimitry Andric         if (error.Success()) {
857*0b57cec5SDimitry Andric           if (run_mode == eAllThreads)
858*0b57cec5SDimitry Andric             m_stop_others = false;
859*0b57cec5SDimitry Andric           else
860*0b57cec5SDimitry Andric             m_stop_others = true;
861*0b57cec5SDimitry Andric         }
862*0b57cec5SDimitry Andric       } break;
863*0b57cec5SDimitry Andric       default:
8649dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
865*0b57cec5SDimitry Andric       }
866*0b57cec5SDimitry Andric       return error;
867*0b57cec5SDimitry Andric     }
868*0b57cec5SDimitry Andric 
869*0b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
870*0b57cec5SDimitry Andric       m_thread_idx = LLDB_INVALID_THREAD_ID;
871*0b57cec5SDimitry Andric       m_frame_idx = 0;
872*0b57cec5SDimitry Andric       m_stop_others = false;
873*0b57cec5SDimitry Andric       m_until_addrs.clear();
874*0b57cec5SDimitry Andric     }
875*0b57cec5SDimitry Andric 
876*0b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
877*0b57cec5SDimitry Andric       return llvm::makeArrayRef(g_thread_until_options);
878*0b57cec5SDimitry Andric     }
879*0b57cec5SDimitry Andric 
880fcaf7f86SDimitry Andric     uint32_t m_step_thread_idx = LLDB_INVALID_THREAD_ID;
881fcaf7f86SDimitry Andric     bool m_stop_others = false;
882*0b57cec5SDimitry Andric     std::vector<lldb::addr_t> m_until_addrs;
883*0b57cec5SDimitry Andric 
884*0b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
885*0b57cec5SDimitry Andric   };
886*0b57cec5SDimitry Andric 
887*0b57cec5SDimitry Andric   CommandObjectThreadUntil(CommandInterpreter &interpreter)
888*0b57cec5SDimitry Andric       : CommandObjectParsed(
889*0b57cec5SDimitry Andric             interpreter, "thread until",
890*0b57cec5SDimitry Andric             "Continue until a line number or address is reached by the "
891*0b57cec5SDimitry Andric             "current or specified thread.  Stops when returning from "
892*0b57cec5SDimitry Andric             "the current function as a safety measure.  "
893480093f4SDimitry Andric             "The target line number(s) are given as arguments, and if more "
894480093f4SDimitry Andric             "than one"
895*0b57cec5SDimitry Andric             " is provided, stepping will stop when the first one is hit.",
896*0b57cec5SDimitry Andric             nullptr,
897*0b57cec5SDimitry Andric             eCommandRequiresThread | eCommandTryTargetAPILock |
89804eeddc0SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {
899*0b57cec5SDimitry Andric     CommandArgumentEntry arg;
900*0b57cec5SDimitry Andric     CommandArgumentData line_num_arg;
901*0b57cec5SDimitry Andric 
902*0b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
903*0b57cec5SDimitry Andric     line_num_arg.arg_type = eArgTypeLineNum;
904*0b57cec5SDimitry Andric     line_num_arg.arg_repetition = eArgRepeatPlain;
905*0b57cec5SDimitry Andric 
906*0b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
907*0b57cec5SDimitry Andric     // argument entry.
908*0b57cec5SDimitry Andric     arg.push_back(line_num_arg);
909*0b57cec5SDimitry Andric 
910*0b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
911*0b57cec5SDimitry Andric     m_arguments.push_back(arg);
912*0b57cec5SDimitry Andric   }
913*0b57cec5SDimitry Andric 
914*0b57cec5SDimitry Andric   ~CommandObjectThreadUntil() override = default;
915*0b57cec5SDimitry Andric 
916*0b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
917*0b57cec5SDimitry Andric 
918*0b57cec5SDimitry Andric protected:
919*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
920*0b57cec5SDimitry Andric     bool synchronous_execution = m_interpreter.GetSynchronous();
921*0b57cec5SDimitry Andric 
9229dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
923*0b57cec5SDimitry Andric 
924*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
925*0b57cec5SDimitry Andric     if (process == nullptr) {
926*0b57cec5SDimitry Andric       result.AppendError("need a valid process to step");
927*0b57cec5SDimitry Andric     } else {
928*0b57cec5SDimitry Andric       Thread *thread = nullptr;
929*0b57cec5SDimitry Andric       std::vector<uint32_t> line_numbers;
930*0b57cec5SDimitry Andric 
931*0b57cec5SDimitry Andric       if (command.GetArgumentCount() >= 1) {
932*0b57cec5SDimitry Andric         size_t num_args = command.GetArgumentCount();
933*0b57cec5SDimitry Andric         for (size_t i = 0; i < num_args; i++) {
934*0b57cec5SDimitry Andric           uint32_t line_number;
9355ffd83dbSDimitry Andric           if (!llvm::to_integer(command.GetArgumentAtIndex(i), line_number)) {
936*0b57cec5SDimitry Andric             result.AppendErrorWithFormat("invalid line number: '%s'.\n",
937*0b57cec5SDimitry Andric                                          command.GetArgumentAtIndex(i));
938*0b57cec5SDimitry Andric             return false;
939*0b57cec5SDimitry Andric           } else
940*0b57cec5SDimitry Andric             line_numbers.push_back(line_number);
941*0b57cec5SDimitry Andric         }
942*0b57cec5SDimitry Andric       } else if (m_options.m_until_addrs.empty()) {
943*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("No line number or address provided:\n%s",
944*0b57cec5SDimitry Andric                                      GetSyntax().str().c_str());
945*0b57cec5SDimitry Andric         return false;
946*0b57cec5SDimitry Andric       }
947*0b57cec5SDimitry Andric 
948*0b57cec5SDimitry Andric       if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID) {
949*0b57cec5SDimitry Andric         thread = GetDefaultThread();
950*0b57cec5SDimitry Andric       } else {
951*0b57cec5SDimitry Andric         thread = process->GetThreadList()
952*0b57cec5SDimitry Andric                      .FindThreadByIndexID(m_options.m_thread_idx)
953*0b57cec5SDimitry Andric                      .get();
954*0b57cec5SDimitry Andric       }
955*0b57cec5SDimitry Andric 
956*0b57cec5SDimitry Andric       if (thread == nullptr) {
957*0b57cec5SDimitry Andric         const uint32_t num_threads = process->GetThreadList().GetSize();
958*0b57cec5SDimitry Andric         result.AppendErrorWithFormat(
959*0b57cec5SDimitry Andric             "Thread index %u is out of range (valid values are 0 - %u).\n",
960*0b57cec5SDimitry Andric             m_options.m_thread_idx, num_threads);
961*0b57cec5SDimitry Andric         return false;
962*0b57cec5SDimitry Andric       }
963*0b57cec5SDimitry Andric 
964*0b57cec5SDimitry Andric       const bool abort_other_plans = false;
965*0b57cec5SDimitry Andric 
966*0b57cec5SDimitry Andric       StackFrame *frame =
967*0b57cec5SDimitry Andric           thread->GetStackFrameAtIndex(m_options.m_frame_idx).get();
968*0b57cec5SDimitry Andric       if (frame == nullptr) {
969*0b57cec5SDimitry Andric         result.AppendErrorWithFormat(
97081ad6265SDimitry Andric             "Frame index %u is out of range for thread id %" PRIu64 ".\n",
97181ad6265SDimitry Andric             m_options.m_frame_idx, thread->GetID());
972*0b57cec5SDimitry Andric         return false;
973*0b57cec5SDimitry Andric       }
974*0b57cec5SDimitry Andric 
975*0b57cec5SDimitry Andric       ThreadPlanSP new_plan_sp;
976*0b57cec5SDimitry Andric       Status new_plan_status;
977*0b57cec5SDimitry Andric 
978*0b57cec5SDimitry Andric       if (frame->HasDebugInformation()) {
979*0b57cec5SDimitry Andric         // Finally we got here...  Translate the given line number to a bunch
980*0b57cec5SDimitry Andric         // of addresses:
981*0b57cec5SDimitry Andric         SymbolContext sc(frame->GetSymbolContext(eSymbolContextCompUnit));
982*0b57cec5SDimitry Andric         LineTable *line_table = nullptr;
983*0b57cec5SDimitry Andric         if (sc.comp_unit)
984*0b57cec5SDimitry Andric           line_table = sc.comp_unit->GetLineTable();
985*0b57cec5SDimitry Andric 
986*0b57cec5SDimitry Andric         if (line_table == nullptr) {
987*0b57cec5SDimitry Andric           result.AppendErrorWithFormat("Failed to resolve the line table for "
98881ad6265SDimitry Andric                                        "frame %u of thread id %" PRIu64 ".\n",
98981ad6265SDimitry Andric                                        m_options.m_frame_idx, thread->GetID());
990*0b57cec5SDimitry Andric           return false;
991*0b57cec5SDimitry Andric         }
992*0b57cec5SDimitry Andric 
993*0b57cec5SDimitry Andric         LineEntry function_start;
994*0b57cec5SDimitry Andric         uint32_t index_ptr = 0, end_ptr;
995*0b57cec5SDimitry Andric         std::vector<addr_t> address_list;
996*0b57cec5SDimitry Andric 
99781ad6265SDimitry Andric         // Find the beginning & end index of the function, but first make
99881ad6265SDimitry Andric         // sure it is valid:
99981ad6265SDimitry Andric         if (!sc.function) {
100081ad6265SDimitry Andric           result.AppendErrorWithFormat("Have debug information but no "
100181ad6265SDimitry Andric                                        "function info - can't get until range.");
100281ad6265SDimitry Andric           return false;
100381ad6265SDimitry Andric         }
100481ad6265SDimitry Andric 
1005*0b57cec5SDimitry Andric         AddressRange fun_addr_range = sc.function->GetAddressRange();
1006*0b57cec5SDimitry Andric         Address fun_start_addr = fun_addr_range.GetBaseAddress();
1007*0b57cec5SDimitry Andric         line_table->FindLineEntryByAddress(fun_start_addr, function_start,
1008*0b57cec5SDimitry Andric                                            &index_ptr);
1009*0b57cec5SDimitry Andric 
1010*0b57cec5SDimitry Andric         Address fun_end_addr(fun_start_addr.GetSection(),
1011*0b57cec5SDimitry Andric                              fun_start_addr.GetOffset() +
1012*0b57cec5SDimitry Andric                                  fun_addr_range.GetByteSize());
1013*0b57cec5SDimitry Andric 
1014*0b57cec5SDimitry Andric         bool all_in_function = true;
1015*0b57cec5SDimitry Andric 
1016*0b57cec5SDimitry Andric         line_table->FindLineEntryByAddress(fun_end_addr, function_start,
1017*0b57cec5SDimitry Andric                                            &end_ptr);
1018*0b57cec5SDimitry Andric 
1019753f127fSDimitry Andric         // Since not all source lines will contribute code, check if we are
1020753f127fSDimitry Andric         // setting the breakpoint on the exact line number or the nearest
1021753f127fSDimitry Andric         // subsequent line number and set breakpoints at all the line table
1022753f127fSDimitry Andric         // entries of the chosen line number (exact or nearest subsequent).
1023*0b57cec5SDimitry Andric         for (uint32_t line_number : line_numbers) {
1024*0b57cec5SDimitry Andric           LineEntry line_entry;
1025753f127fSDimitry Andric           bool exact = false;
1026753f127fSDimitry Andric           uint32_t start_idx_ptr = index_ptr;
1027753f127fSDimitry Andric           start_idx_ptr = sc.comp_unit->FindLineEntry(
1028753f127fSDimitry Andric               index_ptr, line_number, nullptr, exact, &line_entry);
1029753f127fSDimitry Andric           if (start_idx_ptr != UINT32_MAX)
1030753f127fSDimitry Andric             line_number = line_entry.line;
1031753f127fSDimitry Andric           exact = true;
1032753f127fSDimitry Andric           start_idx_ptr = index_ptr;
1033753f127fSDimitry Andric           while (start_idx_ptr <= end_ptr) {
1034*0b57cec5SDimitry Andric             start_idx_ptr = sc.comp_unit->FindLineEntry(
1035480093f4SDimitry Andric                 start_idx_ptr, line_number, nullptr, exact, &line_entry);
1036*0b57cec5SDimitry Andric             if (start_idx_ptr == UINT32_MAX)
1037*0b57cec5SDimitry Andric               break;
1038*0b57cec5SDimitry Andric 
1039*0b57cec5SDimitry Andric             addr_t address =
1040*0b57cec5SDimitry Andric                 line_entry.range.GetBaseAddress().GetLoadAddress(target);
1041*0b57cec5SDimitry Andric             if (address != LLDB_INVALID_ADDRESS) {
1042*0b57cec5SDimitry Andric               if (fun_addr_range.ContainsLoadAddress(address, target))
1043*0b57cec5SDimitry Andric                 address_list.push_back(address);
1044*0b57cec5SDimitry Andric               else
1045*0b57cec5SDimitry Andric                 all_in_function = false;
1046*0b57cec5SDimitry Andric             }
1047*0b57cec5SDimitry Andric             start_idx_ptr++;
1048*0b57cec5SDimitry Andric           }
1049*0b57cec5SDimitry Andric         }
1050*0b57cec5SDimitry Andric 
1051*0b57cec5SDimitry Andric         for (lldb::addr_t address : m_options.m_until_addrs) {
1052*0b57cec5SDimitry Andric           if (fun_addr_range.ContainsLoadAddress(address, target))
1053*0b57cec5SDimitry Andric             address_list.push_back(address);
1054*0b57cec5SDimitry Andric           else
1055*0b57cec5SDimitry Andric             all_in_function = false;
1056*0b57cec5SDimitry Andric         }
1057*0b57cec5SDimitry Andric 
1058*0b57cec5SDimitry Andric         if (address_list.empty()) {
1059*0b57cec5SDimitry Andric           if (all_in_function)
1060*0b57cec5SDimitry Andric             result.AppendErrorWithFormat(
1061*0b57cec5SDimitry Andric                 "No line entries matching until target.\n");
1062*0b57cec5SDimitry Andric           else
1063*0b57cec5SDimitry Andric             result.AppendErrorWithFormat(
1064*0b57cec5SDimitry Andric                 "Until target outside of the current function.\n");
1065*0b57cec5SDimitry Andric 
1066*0b57cec5SDimitry Andric           return false;
1067*0b57cec5SDimitry Andric         }
1068*0b57cec5SDimitry Andric 
1069*0b57cec5SDimitry Andric         new_plan_sp = thread->QueueThreadPlanForStepUntil(
1070*0b57cec5SDimitry Andric             abort_other_plans, &address_list.front(), address_list.size(),
1071*0b57cec5SDimitry Andric             m_options.m_stop_others, m_options.m_frame_idx, new_plan_status);
1072*0b57cec5SDimitry Andric         if (new_plan_sp) {
1073349cc55cSDimitry Andric           // User level plans should be controlling plans so they can be
1074349cc55cSDimitry Andric           // interrupted
1075*0b57cec5SDimitry Andric           // (e.g. by hitting a breakpoint) and other plans executed by the
1076*0b57cec5SDimitry Andric           // user (stepping around the breakpoint) and then a "continue" will
1077*0b57cec5SDimitry Andric           // resume the original plan.
1078349cc55cSDimitry Andric           new_plan_sp->SetIsControllingPlan(true);
1079*0b57cec5SDimitry Andric           new_plan_sp->SetOkayToDiscard(false);
1080*0b57cec5SDimitry Andric         } else {
1081*0b57cec5SDimitry Andric           result.SetError(new_plan_status);
1082*0b57cec5SDimitry Andric           return false;
1083*0b57cec5SDimitry Andric         }
1084*0b57cec5SDimitry Andric       } else {
108581ad6265SDimitry Andric         result.AppendErrorWithFormat("Frame index %u of thread id %" PRIu64
108681ad6265SDimitry Andric                                      " has no debug information.\n",
108781ad6265SDimitry Andric                                      m_options.m_frame_idx, thread->GetID());
1088*0b57cec5SDimitry Andric         return false;
1089*0b57cec5SDimitry Andric       }
1090*0b57cec5SDimitry Andric 
109181ad6265SDimitry Andric       if (!process->GetThreadList().SetSelectedThreadByID(thread->GetID())) {
109281ad6265SDimitry Andric         result.AppendErrorWithFormat(
109381ad6265SDimitry Andric             "Failed to set the selected thread to thread id %" PRIu64 ".\n",
109481ad6265SDimitry Andric             thread->GetID());
109581ad6265SDimitry Andric         return false;
109681ad6265SDimitry Andric       }
1097*0b57cec5SDimitry Andric 
1098*0b57cec5SDimitry Andric       StreamString stream;
1099*0b57cec5SDimitry Andric       Status error;
1100*0b57cec5SDimitry Andric       if (synchronous_execution)
1101*0b57cec5SDimitry Andric         error = process->ResumeSynchronous(&stream);
1102*0b57cec5SDimitry Andric       else
1103*0b57cec5SDimitry Andric         error = process->Resume();
1104*0b57cec5SDimitry Andric 
1105*0b57cec5SDimitry Andric       if (error.Success()) {
1106*0b57cec5SDimitry Andric         result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
1107*0b57cec5SDimitry Andric                                        process->GetID());
1108*0b57cec5SDimitry Andric         if (synchronous_execution) {
1109*0b57cec5SDimitry Andric           // If any state changed events had anything to say, add that to the
1110*0b57cec5SDimitry Andric           // result
1111*0b57cec5SDimitry Andric           if (stream.GetSize() > 0)
1112*0b57cec5SDimitry Andric             result.AppendMessage(stream.GetString());
1113*0b57cec5SDimitry Andric 
1114*0b57cec5SDimitry Andric           result.SetDidChangeProcessState(true);
1115*0b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessFinishNoResult);
1116*0b57cec5SDimitry Andric         } else {
1117*0b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessContinuingNoResult);
1118*0b57cec5SDimitry Andric         }
1119*0b57cec5SDimitry Andric       } else {
1120*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("Failed to resume process: %s.\n",
1121*0b57cec5SDimitry Andric                                      error.AsCString());
1122*0b57cec5SDimitry Andric       }
1123*0b57cec5SDimitry Andric     }
1124*0b57cec5SDimitry Andric     return result.Succeeded();
1125*0b57cec5SDimitry Andric   }
1126*0b57cec5SDimitry Andric 
1127*0b57cec5SDimitry Andric   CommandOptions m_options;
1128*0b57cec5SDimitry Andric };
1129*0b57cec5SDimitry Andric 
1130*0b57cec5SDimitry Andric // CommandObjectThreadSelect
1131*0b57cec5SDimitry Andric 
1132*0b57cec5SDimitry Andric class CommandObjectThreadSelect : public CommandObjectParsed {
1133*0b57cec5SDimitry Andric public:
1134*0b57cec5SDimitry Andric   CommandObjectThreadSelect(CommandInterpreter &interpreter)
1135*0b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "thread select",
1136*0b57cec5SDimitry Andric                             "Change the currently selected thread.", nullptr,
1137*0b57cec5SDimitry Andric                             eCommandRequiresProcess | eCommandTryTargetAPILock |
1138*0b57cec5SDimitry Andric                                 eCommandProcessMustBeLaunched |
1139*0b57cec5SDimitry Andric                                 eCommandProcessMustBePaused) {
1140*0b57cec5SDimitry Andric     CommandArgumentEntry arg;
1141*0b57cec5SDimitry Andric     CommandArgumentData thread_idx_arg;
1142*0b57cec5SDimitry Andric 
1143*0b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
1144*0b57cec5SDimitry Andric     thread_idx_arg.arg_type = eArgTypeThreadIndex;
1145*0b57cec5SDimitry Andric     thread_idx_arg.arg_repetition = eArgRepeatPlain;
1146*0b57cec5SDimitry Andric 
1147*0b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
1148*0b57cec5SDimitry Andric     // argument entry.
1149*0b57cec5SDimitry Andric     arg.push_back(thread_idx_arg);
1150*0b57cec5SDimitry Andric 
1151*0b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
1152*0b57cec5SDimitry Andric     m_arguments.push_back(arg);
1153*0b57cec5SDimitry Andric   }
1154*0b57cec5SDimitry Andric 
1155*0b57cec5SDimitry Andric   ~CommandObjectThreadSelect() override = default;
1156*0b57cec5SDimitry Andric 
1157e8d8bef9SDimitry Andric   void
1158e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
1159e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
1160e8d8bef9SDimitry Andric     if (request.GetCursorIndex())
1161e8d8bef9SDimitry Andric       return;
1162e8d8bef9SDimitry Andric 
1163e8d8bef9SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
1164e8d8bef9SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion,
1165e8d8bef9SDimitry Andric         request, nullptr);
1166e8d8bef9SDimitry Andric   }
1167e8d8bef9SDimitry Andric 
1168*0b57cec5SDimitry Andric protected:
1169*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
1170*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
1171*0b57cec5SDimitry Andric     if (process == nullptr) {
1172*0b57cec5SDimitry Andric       result.AppendError("no process");
1173*0b57cec5SDimitry Andric       return false;
1174*0b57cec5SDimitry Andric     } else if (command.GetArgumentCount() != 1) {
1175*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
1176*0b57cec5SDimitry Andric           "'%s' takes exactly one thread index argument:\nUsage: %s\n",
1177*0b57cec5SDimitry Andric           m_cmd_name.c_str(), m_cmd_syntax.c_str());
1178*0b57cec5SDimitry Andric       return false;
1179*0b57cec5SDimitry Andric     }
1180*0b57cec5SDimitry Andric 
11815ffd83dbSDimitry Andric     uint32_t index_id;
11825ffd83dbSDimitry Andric     if (!llvm::to_integer(command.GetArgumentAtIndex(0), index_id)) {
11835ffd83dbSDimitry Andric       result.AppendErrorWithFormat("Invalid thread index '%s'",
11845ffd83dbSDimitry Andric                                    command.GetArgumentAtIndex(0));
11855ffd83dbSDimitry Andric       return false;
11865ffd83dbSDimitry Andric     }
1187*0b57cec5SDimitry Andric 
1188*0b57cec5SDimitry Andric     Thread *new_thread =
1189*0b57cec5SDimitry Andric         process->GetThreadList().FindThreadByIndexID(index_id).get();
1190*0b57cec5SDimitry Andric     if (new_thread == nullptr) {
1191*0b57cec5SDimitry Andric       result.AppendErrorWithFormat("invalid thread #%s.\n",
1192*0b57cec5SDimitry Andric                                    command.GetArgumentAtIndex(0));
1193*0b57cec5SDimitry Andric       return false;
1194*0b57cec5SDimitry Andric     }
1195*0b57cec5SDimitry Andric 
1196*0b57cec5SDimitry Andric     process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true);
1197*0b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishNoResult);
1198*0b57cec5SDimitry Andric 
1199*0b57cec5SDimitry Andric     return result.Succeeded();
1200*0b57cec5SDimitry Andric   }
1201*0b57cec5SDimitry Andric };
1202*0b57cec5SDimitry Andric 
1203*0b57cec5SDimitry Andric // CommandObjectThreadList
1204*0b57cec5SDimitry Andric 
1205*0b57cec5SDimitry Andric class CommandObjectThreadList : public CommandObjectParsed {
1206*0b57cec5SDimitry Andric public:
1207*0b57cec5SDimitry Andric   CommandObjectThreadList(CommandInterpreter &interpreter)
1208*0b57cec5SDimitry Andric       : CommandObjectParsed(
1209*0b57cec5SDimitry Andric             interpreter, "thread list",
1210*0b57cec5SDimitry Andric             "Show a summary of each thread in the current target process.  "
1211*0b57cec5SDimitry Andric             "Use 'settings set thread-format' to customize the individual "
1212*0b57cec5SDimitry Andric             "thread listings.",
1213*0b57cec5SDimitry Andric             "thread list",
1214*0b57cec5SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
1215*0b57cec5SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
1216*0b57cec5SDimitry Andric 
1217*0b57cec5SDimitry Andric   ~CommandObjectThreadList() override = default;
1218*0b57cec5SDimitry Andric 
1219*0b57cec5SDimitry Andric protected:
1220*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
1221*0b57cec5SDimitry Andric     Stream &strm = result.GetOutputStream();
1222*0b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishNoResult);
1223*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
1224*0b57cec5SDimitry Andric     const bool only_threads_with_stop_reason = false;
1225*0b57cec5SDimitry Andric     const uint32_t start_frame = 0;
1226*0b57cec5SDimitry Andric     const uint32_t num_frames = 0;
1227*0b57cec5SDimitry Andric     const uint32_t num_frames_with_source = 0;
1228*0b57cec5SDimitry Andric     process->GetStatus(strm);
1229*0b57cec5SDimitry Andric     process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame,
1230*0b57cec5SDimitry Andric                              num_frames, num_frames_with_source, false);
1231*0b57cec5SDimitry Andric     return result.Succeeded();
1232*0b57cec5SDimitry Andric   }
1233*0b57cec5SDimitry Andric };
1234*0b57cec5SDimitry Andric 
1235*0b57cec5SDimitry Andric // CommandObjectThreadInfo
1236*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_info
1237*0b57cec5SDimitry Andric #include "CommandOptions.inc"
1238*0b57cec5SDimitry Andric 
1239*0b57cec5SDimitry Andric class CommandObjectThreadInfo : public CommandObjectIterateOverThreads {
1240*0b57cec5SDimitry Andric public:
1241*0b57cec5SDimitry Andric   class CommandOptions : public Options {
1242*0b57cec5SDimitry Andric   public:
124304eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
1244*0b57cec5SDimitry Andric 
1245*0b57cec5SDimitry Andric     ~CommandOptions() override = default;
1246*0b57cec5SDimitry Andric 
1247*0b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
1248*0b57cec5SDimitry Andric       m_json_thread = false;
1249*0b57cec5SDimitry Andric       m_json_stopinfo = false;
1250*0b57cec5SDimitry Andric     }
1251*0b57cec5SDimitry Andric 
1252*0b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1253*0b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
1254*0b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
1255*0b57cec5SDimitry Andric       Status error;
1256*0b57cec5SDimitry Andric 
1257*0b57cec5SDimitry Andric       switch (short_option) {
1258*0b57cec5SDimitry Andric       case 'j':
1259*0b57cec5SDimitry Andric         m_json_thread = true;
1260*0b57cec5SDimitry Andric         break;
1261*0b57cec5SDimitry Andric 
1262*0b57cec5SDimitry Andric       case 's':
1263*0b57cec5SDimitry Andric         m_json_stopinfo = true;
1264*0b57cec5SDimitry Andric         break;
1265*0b57cec5SDimitry Andric 
1266*0b57cec5SDimitry Andric       default:
12679dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
1268*0b57cec5SDimitry Andric       }
1269*0b57cec5SDimitry Andric       return error;
1270*0b57cec5SDimitry Andric     }
1271*0b57cec5SDimitry Andric 
1272*0b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1273*0b57cec5SDimitry Andric       return llvm::makeArrayRef(g_thread_info_options);
1274*0b57cec5SDimitry Andric     }
1275*0b57cec5SDimitry Andric 
1276*0b57cec5SDimitry Andric     bool m_json_thread;
1277*0b57cec5SDimitry Andric     bool m_json_stopinfo;
1278*0b57cec5SDimitry Andric   };
1279*0b57cec5SDimitry Andric 
1280*0b57cec5SDimitry Andric   CommandObjectThreadInfo(CommandInterpreter &interpreter)
1281*0b57cec5SDimitry Andric       : CommandObjectIterateOverThreads(
1282480093f4SDimitry Andric             interpreter, "thread info",
1283480093f4SDimitry Andric             "Show an extended summary of one or "
1284*0b57cec5SDimitry Andric             "more threads.  Defaults to the "
1285*0b57cec5SDimitry Andric             "current thread.",
1286*0b57cec5SDimitry Andric             "thread info",
1287*0b57cec5SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
128804eeddc0SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {
1289*0b57cec5SDimitry Andric     m_add_return = false;
1290*0b57cec5SDimitry Andric   }
1291*0b57cec5SDimitry Andric 
1292*0b57cec5SDimitry Andric   ~CommandObjectThreadInfo() override = default;
1293*0b57cec5SDimitry Andric 
1294e8d8bef9SDimitry Andric   void
1295e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
1296e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
1297e8d8bef9SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
1298e8d8bef9SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion,
1299e8d8bef9SDimitry Andric         request, nullptr);
1300e8d8bef9SDimitry Andric   }
1301e8d8bef9SDimitry Andric 
1302*0b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
1303*0b57cec5SDimitry Andric 
1304*0b57cec5SDimitry Andric   bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override {
1305*0b57cec5SDimitry Andric     ThreadSP thread_sp =
1306*0b57cec5SDimitry Andric         m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
1307*0b57cec5SDimitry Andric     if (!thread_sp) {
1308*0b57cec5SDimitry Andric       result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n",
1309*0b57cec5SDimitry Andric                                    tid);
1310*0b57cec5SDimitry Andric       return false;
1311*0b57cec5SDimitry Andric     }
1312*0b57cec5SDimitry Andric 
1313*0b57cec5SDimitry Andric     Thread *thread = thread_sp.get();
1314*0b57cec5SDimitry Andric 
1315*0b57cec5SDimitry Andric     Stream &strm = result.GetOutputStream();
1316*0b57cec5SDimitry Andric     if (!thread->GetDescription(strm, eDescriptionLevelFull,
1317*0b57cec5SDimitry Andric                                 m_options.m_json_thread,
1318*0b57cec5SDimitry Andric                                 m_options.m_json_stopinfo)) {
1319*0b57cec5SDimitry Andric       result.AppendErrorWithFormat("error displaying info for thread: \"%d\"\n",
1320*0b57cec5SDimitry Andric                                    thread->GetIndexID());
1321*0b57cec5SDimitry Andric       return false;
1322*0b57cec5SDimitry Andric     }
1323*0b57cec5SDimitry Andric     return true;
1324*0b57cec5SDimitry Andric   }
1325*0b57cec5SDimitry Andric 
1326*0b57cec5SDimitry Andric   CommandOptions m_options;
1327*0b57cec5SDimitry Andric };
1328*0b57cec5SDimitry Andric 
1329*0b57cec5SDimitry Andric // CommandObjectThreadException
1330*0b57cec5SDimitry Andric 
1331*0b57cec5SDimitry Andric class CommandObjectThreadException : public CommandObjectIterateOverThreads {
1332*0b57cec5SDimitry Andric public:
1333*0b57cec5SDimitry Andric   CommandObjectThreadException(CommandInterpreter &interpreter)
1334*0b57cec5SDimitry Andric       : CommandObjectIterateOverThreads(
1335*0b57cec5SDimitry Andric             interpreter, "thread exception",
1336*0b57cec5SDimitry Andric             "Display the current exception object for a thread. Defaults to "
1337*0b57cec5SDimitry Andric             "the current thread.",
1338*0b57cec5SDimitry Andric             "thread exception",
1339*0b57cec5SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
1340*0b57cec5SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
1341*0b57cec5SDimitry Andric 
1342*0b57cec5SDimitry Andric   ~CommandObjectThreadException() override = default;
1343*0b57cec5SDimitry Andric 
1344e8d8bef9SDimitry Andric   void
1345e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
1346e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
1347e8d8bef9SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
1348e8d8bef9SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion,
1349e8d8bef9SDimitry Andric         request, nullptr);
1350e8d8bef9SDimitry Andric   }
1351e8d8bef9SDimitry Andric 
1352*0b57cec5SDimitry Andric   bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override {
1353*0b57cec5SDimitry Andric     ThreadSP thread_sp =
1354*0b57cec5SDimitry Andric         m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
1355*0b57cec5SDimitry Andric     if (!thread_sp) {
1356*0b57cec5SDimitry Andric       result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n",
1357*0b57cec5SDimitry Andric                                    tid);
1358*0b57cec5SDimitry Andric       return false;
1359*0b57cec5SDimitry Andric     }
1360*0b57cec5SDimitry Andric 
1361*0b57cec5SDimitry Andric     Stream &strm = result.GetOutputStream();
1362*0b57cec5SDimitry Andric     ValueObjectSP exception_object_sp = thread_sp->GetCurrentException();
1363*0b57cec5SDimitry Andric     if (exception_object_sp) {
1364*0b57cec5SDimitry Andric       exception_object_sp->Dump(strm);
1365*0b57cec5SDimitry Andric     }
1366*0b57cec5SDimitry Andric 
1367*0b57cec5SDimitry Andric     ThreadSP exception_thread_sp = thread_sp->GetCurrentExceptionBacktrace();
1368*0b57cec5SDimitry Andric     if (exception_thread_sp && exception_thread_sp->IsValid()) {
1369*0b57cec5SDimitry Andric       const uint32_t num_frames_with_source = 0;
1370*0b57cec5SDimitry Andric       const bool stop_format = false;
1371*0b57cec5SDimitry Andric       exception_thread_sp->GetStatus(strm, 0, UINT32_MAX,
1372*0b57cec5SDimitry Andric                                      num_frames_with_source, stop_format);
1373*0b57cec5SDimitry Andric     }
1374*0b57cec5SDimitry Andric 
1375*0b57cec5SDimitry Andric     return true;
1376*0b57cec5SDimitry Andric   }
1377*0b57cec5SDimitry Andric };
1378*0b57cec5SDimitry Andric 
1379d56accc7SDimitry Andric class CommandObjectThreadSiginfo : public CommandObjectIterateOverThreads {
1380d56accc7SDimitry Andric public:
1381d56accc7SDimitry Andric   CommandObjectThreadSiginfo(CommandInterpreter &interpreter)
1382d56accc7SDimitry Andric       : CommandObjectIterateOverThreads(
1383d56accc7SDimitry Andric             interpreter, "thread siginfo",
1384d56accc7SDimitry Andric             "Display the current siginfo object for a thread. Defaults to "
1385d56accc7SDimitry Andric             "the current thread.",
1386d56accc7SDimitry Andric             "thread siginfo",
1387d56accc7SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
1388d56accc7SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
1389d56accc7SDimitry Andric 
1390d56accc7SDimitry Andric   ~CommandObjectThreadSiginfo() override = default;
1391d56accc7SDimitry Andric 
1392d56accc7SDimitry Andric   void
1393d56accc7SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
1394d56accc7SDimitry Andric                            OptionElementVector &opt_element_vector) override {
1395d56accc7SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
1396d56accc7SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eThreadIndexCompletion,
1397d56accc7SDimitry Andric         request, nullptr);
1398d56accc7SDimitry Andric   }
1399d56accc7SDimitry Andric 
1400d56accc7SDimitry Andric   bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override {
1401d56accc7SDimitry Andric     ThreadSP thread_sp =
1402d56accc7SDimitry Andric         m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
1403d56accc7SDimitry Andric     if (!thread_sp) {
1404d56accc7SDimitry Andric       result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n",
1405d56accc7SDimitry Andric                                    tid);
1406d56accc7SDimitry Andric       return false;
1407d56accc7SDimitry Andric     }
1408d56accc7SDimitry Andric 
1409d56accc7SDimitry Andric     Stream &strm = result.GetOutputStream();
1410d56accc7SDimitry Andric     if (!thread_sp->GetDescription(strm, eDescriptionLevelFull, false, false)) {
1411d56accc7SDimitry Andric       result.AppendErrorWithFormat("error displaying info for thread: \"%d\"\n",
1412d56accc7SDimitry Andric                                    thread_sp->GetIndexID());
1413d56accc7SDimitry Andric       return false;
1414d56accc7SDimitry Andric     }
1415d56accc7SDimitry Andric     ValueObjectSP exception_object_sp = thread_sp->GetSiginfoValue();
1416d56accc7SDimitry Andric     if (exception_object_sp)
1417d56accc7SDimitry Andric       exception_object_sp->Dump(strm);
1418d56accc7SDimitry Andric     else
1419d56accc7SDimitry Andric       strm.Printf("(no siginfo)\n");
1420d56accc7SDimitry Andric     strm.PutChar('\n');
1421d56accc7SDimitry Andric 
1422d56accc7SDimitry Andric     return true;
1423d56accc7SDimitry Andric   }
1424d56accc7SDimitry Andric };
1425d56accc7SDimitry Andric 
1426*0b57cec5SDimitry Andric // CommandObjectThreadReturn
1427*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_return
1428*0b57cec5SDimitry Andric #include "CommandOptions.inc"
1429*0b57cec5SDimitry Andric 
1430*0b57cec5SDimitry Andric class CommandObjectThreadReturn : public CommandObjectRaw {
1431*0b57cec5SDimitry Andric public:
1432*0b57cec5SDimitry Andric   class CommandOptions : public Options {
1433*0b57cec5SDimitry Andric   public:
143404eeddc0SDimitry Andric     CommandOptions() {
1435*0b57cec5SDimitry Andric       // Keep default values of all options in one place: OptionParsingStarting
1436*0b57cec5SDimitry Andric       // ()
1437*0b57cec5SDimitry Andric       OptionParsingStarting(nullptr);
1438*0b57cec5SDimitry Andric     }
1439*0b57cec5SDimitry Andric 
1440*0b57cec5SDimitry Andric     ~CommandOptions() override = default;
1441*0b57cec5SDimitry Andric 
1442*0b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1443*0b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
1444*0b57cec5SDimitry Andric       Status error;
1445*0b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
1446*0b57cec5SDimitry Andric 
1447*0b57cec5SDimitry Andric       switch (short_option) {
1448*0b57cec5SDimitry Andric       case 'x': {
1449*0b57cec5SDimitry Andric         bool success;
1450*0b57cec5SDimitry Andric         bool tmp_value =
1451*0b57cec5SDimitry Andric             OptionArgParser::ToBoolean(option_arg, false, &success);
1452*0b57cec5SDimitry Andric         if (success)
1453*0b57cec5SDimitry Andric           m_from_expression = tmp_value;
1454*0b57cec5SDimitry Andric         else {
1455*0b57cec5SDimitry Andric           error.SetErrorStringWithFormat(
1456*0b57cec5SDimitry Andric               "invalid boolean value '%s' for 'x' option",
1457*0b57cec5SDimitry Andric               option_arg.str().c_str());
1458*0b57cec5SDimitry Andric         }
1459*0b57cec5SDimitry Andric       } break;
1460*0b57cec5SDimitry Andric       default:
14619dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
1462*0b57cec5SDimitry Andric       }
1463*0b57cec5SDimitry Andric       return error;
1464*0b57cec5SDimitry Andric     }
1465*0b57cec5SDimitry Andric 
1466*0b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
1467*0b57cec5SDimitry Andric       m_from_expression = false;
1468*0b57cec5SDimitry Andric     }
1469*0b57cec5SDimitry Andric 
1470*0b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1471*0b57cec5SDimitry Andric       return llvm::makeArrayRef(g_thread_return_options);
1472*0b57cec5SDimitry Andric     }
1473*0b57cec5SDimitry Andric 
1474fe6060f1SDimitry Andric     bool m_from_expression = false;
1475*0b57cec5SDimitry Andric 
1476*0b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
1477*0b57cec5SDimitry Andric   };
1478*0b57cec5SDimitry Andric 
1479*0b57cec5SDimitry Andric   CommandObjectThreadReturn(CommandInterpreter &interpreter)
1480*0b57cec5SDimitry Andric       : CommandObjectRaw(interpreter, "thread return",
1481*0b57cec5SDimitry Andric                          "Prematurely return from a stack frame, "
1482*0b57cec5SDimitry Andric                          "short-circuiting execution of newer frames "
1483*0b57cec5SDimitry Andric                          "and optionally yielding a specified value.  Defaults "
1484*0b57cec5SDimitry Andric                          "to the exiting the current stack "
1485*0b57cec5SDimitry Andric                          "frame.",
1486*0b57cec5SDimitry Andric                          "thread return",
1487*0b57cec5SDimitry Andric                          eCommandRequiresFrame | eCommandTryTargetAPILock |
1488*0b57cec5SDimitry Andric                              eCommandProcessMustBeLaunched |
148904eeddc0SDimitry Andric                              eCommandProcessMustBePaused) {
1490*0b57cec5SDimitry Andric     CommandArgumentEntry arg;
1491*0b57cec5SDimitry Andric     CommandArgumentData expression_arg;
1492*0b57cec5SDimitry Andric 
1493*0b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
1494*0b57cec5SDimitry Andric     expression_arg.arg_type = eArgTypeExpression;
1495*0b57cec5SDimitry Andric     expression_arg.arg_repetition = eArgRepeatOptional;
1496*0b57cec5SDimitry Andric 
1497*0b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
1498*0b57cec5SDimitry Andric     // argument entry.
1499*0b57cec5SDimitry Andric     arg.push_back(expression_arg);
1500*0b57cec5SDimitry Andric 
1501*0b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
1502*0b57cec5SDimitry Andric     m_arguments.push_back(arg);
1503*0b57cec5SDimitry Andric   }
1504*0b57cec5SDimitry Andric 
1505*0b57cec5SDimitry Andric   ~CommandObjectThreadReturn() override = default;
1506*0b57cec5SDimitry Andric 
1507*0b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
1508*0b57cec5SDimitry Andric 
1509*0b57cec5SDimitry Andric protected:
1510*0b57cec5SDimitry Andric   bool DoExecute(llvm::StringRef command,
1511*0b57cec5SDimitry Andric                  CommandReturnObject &result) override {
1512*0b57cec5SDimitry Andric     // I am going to handle this by hand, because I don't want you to have to
1513*0b57cec5SDimitry Andric     // say:
1514*0b57cec5SDimitry Andric     // "thread return -- -5".
1515*0b57cec5SDimitry Andric     if (command.startswith("-x")) {
1516*0b57cec5SDimitry Andric       if (command.size() != 2U)
1517*0b57cec5SDimitry Andric         result.AppendWarning("Return values ignored when returning from user "
1518*0b57cec5SDimitry Andric                              "called expressions");
1519*0b57cec5SDimitry Andric 
1520*0b57cec5SDimitry Andric       Thread *thread = m_exe_ctx.GetThreadPtr();
1521*0b57cec5SDimitry Andric       Status error;
1522*0b57cec5SDimitry Andric       error = thread->UnwindInnermostExpression();
1523*0b57cec5SDimitry Andric       if (!error.Success()) {
1524*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("Unwinding expression failed - %s.",
1525*0b57cec5SDimitry Andric                                      error.AsCString());
1526*0b57cec5SDimitry Andric       } else {
1527*0b57cec5SDimitry Andric         bool success =
1528*0b57cec5SDimitry Andric             thread->SetSelectedFrameByIndexNoisily(0, result.GetOutputStream());
1529*0b57cec5SDimitry Andric         if (success) {
1530*0b57cec5SDimitry Andric           m_exe_ctx.SetFrameSP(thread->GetSelectedFrame());
1531*0b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessFinishResult);
1532*0b57cec5SDimitry Andric         } else {
1533*0b57cec5SDimitry Andric           result.AppendErrorWithFormat(
1534*0b57cec5SDimitry Andric               "Could not select 0th frame after unwinding expression.");
1535*0b57cec5SDimitry Andric         }
1536*0b57cec5SDimitry Andric       }
1537*0b57cec5SDimitry Andric       return result.Succeeded();
1538*0b57cec5SDimitry Andric     }
1539*0b57cec5SDimitry Andric 
1540*0b57cec5SDimitry Andric     ValueObjectSP return_valobj_sp;
1541*0b57cec5SDimitry Andric 
1542*0b57cec5SDimitry Andric     StackFrameSP frame_sp = m_exe_ctx.GetFrameSP();
1543*0b57cec5SDimitry Andric     uint32_t frame_idx = frame_sp->GetFrameIndex();
1544*0b57cec5SDimitry Andric 
1545*0b57cec5SDimitry Andric     if (frame_sp->IsInlined()) {
1546*0b57cec5SDimitry Andric       result.AppendError("Don't know how to return from inlined frames.");
1547*0b57cec5SDimitry Andric       return false;
1548*0b57cec5SDimitry Andric     }
1549*0b57cec5SDimitry Andric 
1550*0b57cec5SDimitry Andric     if (!command.empty()) {
1551*0b57cec5SDimitry Andric       Target *target = m_exe_ctx.GetTargetPtr();
1552*0b57cec5SDimitry Andric       EvaluateExpressionOptions options;
1553*0b57cec5SDimitry Andric 
1554*0b57cec5SDimitry Andric       options.SetUnwindOnError(true);
1555*0b57cec5SDimitry Andric       options.SetUseDynamic(eNoDynamicValues);
1556*0b57cec5SDimitry Andric 
1557*0b57cec5SDimitry Andric       ExpressionResults exe_results = eExpressionSetupError;
1558*0b57cec5SDimitry Andric       exe_results = target->EvaluateExpression(command, frame_sp.get(),
1559*0b57cec5SDimitry Andric                                                return_valobj_sp, options);
1560*0b57cec5SDimitry Andric       if (exe_results != eExpressionCompleted) {
1561*0b57cec5SDimitry Andric         if (return_valobj_sp)
1562*0b57cec5SDimitry Andric           result.AppendErrorWithFormat(
1563*0b57cec5SDimitry Andric               "Error evaluating result expression: %s",
1564*0b57cec5SDimitry Andric               return_valobj_sp->GetError().AsCString());
1565*0b57cec5SDimitry Andric         else
1566*0b57cec5SDimitry Andric           result.AppendErrorWithFormat(
1567*0b57cec5SDimitry Andric               "Unknown error evaluating result expression.");
1568*0b57cec5SDimitry Andric         return false;
1569*0b57cec5SDimitry Andric       }
1570*0b57cec5SDimitry Andric     }
1571*0b57cec5SDimitry Andric 
1572*0b57cec5SDimitry Andric     Status error;
1573*0b57cec5SDimitry Andric     ThreadSP thread_sp = m_exe_ctx.GetThreadSP();
1574*0b57cec5SDimitry Andric     const bool broadcast = true;
1575*0b57cec5SDimitry Andric     error = thread_sp->ReturnFromFrame(frame_sp, return_valobj_sp, broadcast);
1576*0b57cec5SDimitry Andric     if (!error.Success()) {
1577*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
1578*0b57cec5SDimitry Andric           "Error returning from frame %d of thread %d: %s.", frame_idx,
1579*0b57cec5SDimitry Andric           thread_sp->GetIndexID(), error.AsCString());
1580*0b57cec5SDimitry Andric       return false;
1581*0b57cec5SDimitry Andric     }
1582*0b57cec5SDimitry Andric 
1583*0b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
1584*0b57cec5SDimitry Andric     return true;
1585*0b57cec5SDimitry Andric   }
1586*0b57cec5SDimitry Andric 
1587*0b57cec5SDimitry Andric   CommandOptions m_options;
1588*0b57cec5SDimitry Andric };
1589*0b57cec5SDimitry Andric 
1590*0b57cec5SDimitry Andric // CommandObjectThreadJump
1591*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_jump
1592*0b57cec5SDimitry Andric #include "CommandOptions.inc"
1593*0b57cec5SDimitry Andric 
1594*0b57cec5SDimitry Andric class CommandObjectThreadJump : public CommandObjectParsed {
1595*0b57cec5SDimitry Andric public:
1596*0b57cec5SDimitry Andric   class CommandOptions : public Options {
1597*0b57cec5SDimitry Andric   public:
159804eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
1599*0b57cec5SDimitry Andric 
1600*0b57cec5SDimitry Andric     ~CommandOptions() override = default;
1601*0b57cec5SDimitry Andric 
1602*0b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
1603*0b57cec5SDimitry Andric       m_filenames.Clear();
1604*0b57cec5SDimitry Andric       m_line_num = 0;
1605*0b57cec5SDimitry Andric       m_line_offset = 0;
1606*0b57cec5SDimitry Andric       m_load_addr = LLDB_INVALID_ADDRESS;
1607*0b57cec5SDimitry Andric       m_force = false;
1608*0b57cec5SDimitry Andric     }
1609*0b57cec5SDimitry Andric 
1610*0b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1611*0b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
1612*0b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
1613*0b57cec5SDimitry Andric       Status error;
1614*0b57cec5SDimitry Andric 
1615*0b57cec5SDimitry Andric       switch (short_option) {
1616*0b57cec5SDimitry Andric       case 'f':
1617*0b57cec5SDimitry Andric         m_filenames.AppendIfUnique(FileSpec(option_arg));
1618*0b57cec5SDimitry Andric         if (m_filenames.GetSize() > 1)
1619*0b57cec5SDimitry Andric           return Status("only one source file expected.");
1620*0b57cec5SDimitry Andric         break;
1621*0b57cec5SDimitry Andric       case 'l':
1622*0b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, m_line_num))
1623*0b57cec5SDimitry Andric           return Status("invalid line number: '%s'.", option_arg.str().c_str());
1624*0b57cec5SDimitry Andric         break;
1625*0b57cec5SDimitry Andric       case 'b':
1626*0b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, m_line_offset))
1627*0b57cec5SDimitry Andric           return Status("invalid line offset: '%s'.", option_arg.str().c_str());
1628*0b57cec5SDimitry Andric         break;
1629*0b57cec5SDimitry Andric       case 'a':
1630*0b57cec5SDimitry Andric         m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg,
1631*0b57cec5SDimitry Andric                                                  LLDB_INVALID_ADDRESS, &error);
1632*0b57cec5SDimitry Andric         break;
1633*0b57cec5SDimitry Andric       case 'r':
1634*0b57cec5SDimitry Andric         m_force = true;
1635*0b57cec5SDimitry Andric         break;
1636*0b57cec5SDimitry Andric       default:
16379dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
1638*0b57cec5SDimitry Andric       }
1639*0b57cec5SDimitry Andric       return error;
1640*0b57cec5SDimitry Andric     }
1641*0b57cec5SDimitry Andric 
1642*0b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1643*0b57cec5SDimitry Andric       return llvm::makeArrayRef(g_thread_jump_options);
1644*0b57cec5SDimitry Andric     }
1645*0b57cec5SDimitry Andric 
1646*0b57cec5SDimitry Andric     FileSpecList m_filenames;
1647*0b57cec5SDimitry Andric     uint32_t m_line_num;
1648*0b57cec5SDimitry Andric     int32_t m_line_offset;
1649*0b57cec5SDimitry Andric     lldb::addr_t m_load_addr;
1650*0b57cec5SDimitry Andric     bool m_force;
1651*0b57cec5SDimitry Andric   };
1652*0b57cec5SDimitry Andric 
1653*0b57cec5SDimitry Andric   CommandObjectThreadJump(CommandInterpreter &interpreter)
1654*0b57cec5SDimitry Andric       : CommandObjectParsed(
1655*0b57cec5SDimitry Andric             interpreter, "thread jump",
1656*0b57cec5SDimitry Andric             "Sets the program counter to a new address.", "thread jump",
1657*0b57cec5SDimitry Andric             eCommandRequiresFrame | eCommandTryTargetAPILock |
165804eeddc0SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
1659*0b57cec5SDimitry Andric 
1660*0b57cec5SDimitry Andric   ~CommandObjectThreadJump() override = default;
1661*0b57cec5SDimitry Andric 
1662*0b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
1663*0b57cec5SDimitry Andric 
1664*0b57cec5SDimitry Andric protected:
1665*0b57cec5SDimitry Andric   bool DoExecute(Args &args, CommandReturnObject &result) override {
1666*0b57cec5SDimitry Andric     RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext();
1667*0b57cec5SDimitry Andric     StackFrame *frame = m_exe_ctx.GetFramePtr();
1668*0b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
1669*0b57cec5SDimitry Andric     Target *target = m_exe_ctx.GetTargetPtr();
1670*0b57cec5SDimitry Andric     const SymbolContext &sym_ctx =
1671*0b57cec5SDimitry Andric         frame->GetSymbolContext(eSymbolContextLineEntry);
1672*0b57cec5SDimitry Andric 
1673*0b57cec5SDimitry Andric     if (m_options.m_load_addr != LLDB_INVALID_ADDRESS) {
1674*0b57cec5SDimitry Andric       // Use this address directly.
1675*0b57cec5SDimitry Andric       Address dest = Address(m_options.m_load_addr);
1676*0b57cec5SDimitry Andric 
1677*0b57cec5SDimitry Andric       lldb::addr_t callAddr = dest.GetCallableLoadAddress(target);
1678*0b57cec5SDimitry Andric       if (callAddr == LLDB_INVALID_ADDRESS) {
1679*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("Invalid destination address.");
1680*0b57cec5SDimitry Andric         return false;
1681*0b57cec5SDimitry Andric       }
1682*0b57cec5SDimitry Andric 
1683*0b57cec5SDimitry Andric       if (!reg_ctx->SetPC(callAddr)) {
1684*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("Error changing PC value for thread %d.",
1685*0b57cec5SDimitry Andric                                      thread->GetIndexID());
1686*0b57cec5SDimitry Andric         return false;
1687*0b57cec5SDimitry Andric       }
1688*0b57cec5SDimitry Andric     } else {
1689*0b57cec5SDimitry Andric       // Pick either the absolute line, or work out a relative one.
1690*0b57cec5SDimitry Andric       int32_t line = (int32_t)m_options.m_line_num;
1691*0b57cec5SDimitry Andric       if (line == 0)
1692*0b57cec5SDimitry Andric         line = sym_ctx.line_entry.line + m_options.m_line_offset;
1693*0b57cec5SDimitry Andric 
1694*0b57cec5SDimitry Andric       // Try the current file, but override if asked.
1695*0b57cec5SDimitry Andric       FileSpec file = sym_ctx.line_entry.file;
1696*0b57cec5SDimitry Andric       if (m_options.m_filenames.GetSize() == 1)
1697*0b57cec5SDimitry Andric         file = m_options.m_filenames.GetFileSpecAtIndex(0);
1698*0b57cec5SDimitry Andric 
1699*0b57cec5SDimitry Andric       if (!file) {
1700*0b57cec5SDimitry Andric         result.AppendErrorWithFormat(
1701*0b57cec5SDimitry Andric             "No source file available for the current location.");
1702*0b57cec5SDimitry Andric         return false;
1703*0b57cec5SDimitry Andric       }
1704*0b57cec5SDimitry Andric 
1705*0b57cec5SDimitry Andric       std::string warnings;
1706*0b57cec5SDimitry Andric       Status err = thread->JumpToLine(file, line, m_options.m_force, &warnings);
1707*0b57cec5SDimitry Andric 
1708*0b57cec5SDimitry Andric       if (err.Fail()) {
1709*0b57cec5SDimitry Andric         result.SetError(err);
1710*0b57cec5SDimitry Andric         return false;
1711*0b57cec5SDimitry Andric       }
1712*0b57cec5SDimitry Andric 
1713*0b57cec5SDimitry Andric       if (!warnings.empty())
1714*0b57cec5SDimitry Andric         result.AppendWarning(warnings.c_str());
1715*0b57cec5SDimitry Andric     }
1716*0b57cec5SDimitry Andric 
1717*0b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
1718*0b57cec5SDimitry Andric     return true;
1719*0b57cec5SDimitry Andric   }
1720*0b57cec5SDimitry Andric 
1721*0b57cec5SDimitry Andric   CommandOptions m_options;
1722*0b57cec5SDimitry Andric };
1723*0b57cec5SDimitry Andric 
1724*0b57cec5SDimitry Andric // Next are the subcommands of CommandObjectMultiwordThreadPlan
1725*0b57cec5SDimitry Andric 
1726*0b57cec5SDimitry Andric // CommandObjectThreadPlanList
1727*0b57cec5SDimitry Andric #define LLDB_OPTIONS_thread_plan_list
1728*0b57cec5SDimitry Andric #include "CommandOptions.inc"
1729*0b57cec5SDimitry Andric 
1730*0b57cec5SDimitry Andric class CommandObjectThreadPlanList : public CommandObjectIterateOverThreads {
1731*0b57cec5SDimitry Andric public:
1732*0b57cec5SDimitry Andric   class CommandOptions : public Options {
1733*0b57cec5SDimitry Andric   public:
173404eeddc0SDimitry Andric     CommandOptions() {
1735*0b57cec5SDimitry Andric       // Keep default values of all options in one place: OptionParsingStarting
1736*0b57cec5SDimitry Andric       // ()
1737*0b57cec5SDimitry Andric       OptionParsingStarting(nullptr);
1738*0b57cec5SDimitry Andric     }
1739*0b57cec5SDimitry Andric 
1740*0b57cec5SDimitry Andric     ~CommandOptions() override = default;
1741*0b57cec5SDimitry Andric 
1742*0b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1743*0b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
1744*0b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
1745*0b57cec5SDimitry Andric 
1746*0b57cec5SDimitry Andric       switch (short_option) {
1747*0b57cec5SDimitry Andric       case 'i':
1748*0b57cec5SDimitry Andric         m_internal = true;
1749*0b57cec5SDimitry Andric         break;
17505ffd83dbSDimitry Andric       case 't':
17515ffd83dbSDimitry Andric         lldb::tid_t tid;
17525ffd83dbSDimitry Andric         if (option_arg.getAsInteger(0, tid))
17535ffd83dbSDimitry Andric           return Status("invalid tid: '%s'.", option_arg.str().c_str());
17545ffd83dbSDimitry Andric         m_tids.push_back(tid);
17555ffd83dbSDimitry Andric         break;
17565ffd83dbSDimitry Andric       case 'u':
17575ffd83dbSDimitry Andric         m_unreported = false;
17585ffd83dbSDimitry Andric         break;
1759*0b57cec5SDimitry Andric       case 'v':
1760*0b57cec5SDimitry Andric         m_verbose = true;
1761*0b57cec5SDimitry Andric         break;
1762*0b57cec5SDimitry Andric       default:
17639dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
1764*0b57cec5SDimitry Andric       }
17655ffd83dbSDimitry Andric       return {};
1766*0b57cec5SDimitry Andric     }
1767*0b57cec5SDimitry Andric 
1768*0b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
1769*0b57cec5SDimitry Andric       m_verbose = false;
1770*0b57cec5SDimitry Andric       m_internal = false;
17715ffd83dbSDimitry Andric       m_unreported = true; // The variable is "skip unreported" and we want to
17725ffd83dbSDimitry Andric                            // skip unreported by default.
17735ffd83dbSDimitry Andric       m_tids.clear();
1774*0b57cec5SDimitry Andric     }
1775*0b57cec5SDimitry Andric 
1776*0b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1777*0b57cec5SDimitry Andric       return llvm::makeArrayRef(g_thread_plan_list_options);
1778*0b57cec5SDimitry Andric     }
1779*0b57cec5SDimitry Andric 
1780*0b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
1781*0b57cec5SDimitry Andric     bool m_verbose;
1782*0b57cec5SDimitry Andric     bool m_internal;
17835ffd83dbSDimitry Andric     bool m_unreported;
17845ffd83dbSDimitry Andric     std::vector<lldb::tid_t> m_tids;
1785*0b57cec5SDimitry Andric   };
1786*0b57cec5SDimitry Andric 
1787*0b57cec5SDimitry Andric   CommandObjectThreadPlanList(CommandInterpreter &interpreter)
1788*0b57cec5SDimitry Andric       : CommandObjectIterateOverThreads(
1789*0b57cec5SDimitry Andric             interpreter, "thread plan list",
1790*0b57cec5SDimitry Andric             "Show thread plans for one or more threads.  If no threads are "
1791*0b57cec5SDimitry Andric             "specified, show the "
1792*0b57cec5SDimitry Andric             "current thread.  Use the thread-index \"all\" to see all threads.",
1793*0b57cec5SDimitry Andric             nullptr,
1794*0b57cec5SDimitry Andric             eCommandRequiresProcess | eCommandRequiresThread |
1795*0b57cec5SDimitry Andric                 eCommandTryTargetAPILock | eCommandProcessMustBeLaunched |
179604eeddc0SDimitry Andric                 eCommandProcessMustBePaused) {}
1797*0b57cec5SDimitry Andric 
1798*0b57cec5SDimitry Andric   ~CommandObjectThreadPlanList() override = default;
1799*0b57cec5SDimitry Andric 
1800*0b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
1801*0b57cec5SDimitry Andric 
18025ffd83dbSDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
18035ffd83dbSDimitry Andric     // If we are reporting all threads, dispatch to the Process to do that:
18045ffd83dbSDimitry Andric     if (command.GetArgumentCount() == 0 && m_options.m_tids.empty()) {
18055ffd83dbSDimitry Andric       Stream &strm = result.GetOutputStream();
18065ffd83dbSDimitry Andric       DescriptionLevel desc_level = m_options.m_verbose
18075ffd83dbSDimitry Andric                                         ? eDescriptionLevelVerbose
18085ffd83dbSDimitry Andric                                         : eDescriptionLevelFull;
18095ffd83dbSDimitry Andric       m_exe_ctx.GetProcessPtr()->DumpThreadPlans(
18105ffd83dbSDimitry Andric           strm, desc_level, m_options.m_internal, true, m_options.m_unreported);
18115ffd83dbSDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
18125ffd83dbSDimitry Andric       return true;
18135ffd83dbSDimitry Andric     } else {
18145ffd83dbSDimitry Andric       // Do any TID's that the user may have specified as TID, then do any
18155ffd83dbSDimitry Andric       // Thread Indexes...
18165ffd83dbSDimitry Andric       if (!m_options.m_tids.empty()) {
18175ffd83dbSDimitry Andric         Process *process = m_exe_ctx.GetProcessPtr();
18185ffd83dbSDimitry Andric         StreamString tmp_strm;
18195ffd83dbSDimitry Andric         for (lldb::tid_t tid : m_options.m_tids) {
18205ffd83dbSDimitry Andric           bool success = process->DumpThreadPlansForTID(
18215ffd83dbSDimitry Andric               tmp_strm, tid, eDescriptionLevelFull, m_options.m_internal,
18225ffd83dbSDimitry Andric               true /* condense_trivial */, m_options.m_unreported);
18235ffd83dbSDimitry Andric           // If we didn't find a TID, stop here and return an error.
18245ffd83dbSDimitry Andric           if (!success) {
1825fe6060f1SDimitry Andric             result.AppendError("Error dumping plans:");
18265ffd83dbSDimitry Andric             result.AppendError(tmp_strm.GetString());
1827*0b57cec5SDimitry Andric             return false;
1828*0b57cec5SDimitry Andric           }
18295ffd83dbSDimitry Andric           // Otherwise, add our data to the output:
18305ffd83dbSDimitry Andric           result.GetOutputStream() << tmp_strm.GetString();
18315ffd83dbSDimitry Andric         }
18325ffd83dbSDimitry Andric       }
18335ffd83dbSDimitry Andric       return CommandObjectIterateOverThreads::DoExecute(command, result);
18345ffd83dbSDimitry Andric     }
18355ffd83dbSDimitry Andric   }
1836*0b57cec5SDimitry Andric 
18375ffd83dbSDimitry Andric protected:
18385ffd83dbSDimitry Andric   bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override {
18395ffd83dbSDimitry Andric     // If we have already handled this from a -t option, skip it here.
1840e8d8bef9SDimitry Andric     if (llvm::is_contained(m_options.m_tids, tid))
18415ffd83dbSDimitry Andric       return true;
18425ffd83dbSDimitry Andric 
18435ffd83dbSDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
1844*0b57cec5SDimitry Andric 
1845*0b57cec5SDimitry Andric     Stream &strm = result.GetOutputStream();
1846*0b57cec5SDimitry Andric     DescriptionLevel desc_level = eDescriptionLevelFull;
1847*0b57cec5SDimitry Andric     if (m_options.m_verbose)
1848*0b57cec5SDimitry Andric       desc_level = eDescriptionLevelVerbose;
1849*0b57cec5SDimitry Andric 
18505ffd83dbSDimitry Andric     process->DumpThreadPlansForTID(strm, tid, desc_level, m_options.m_internal,
18515ffd83dbSDimitry Andric                                    true /* condense_trivial */,
18525ffd83dbSDimitry Andric                                    m_options.m_unreported);
1853*0b57cec5SDimitry Andric     return true;
1854*0b57cec5SDimitry Andric   }
1855*0b57cec5SDimitry Andric 
1856*0b57cec5SDimitry Andric   CommandOptions m_options;
1857*0b57cec5SDimitry Andric };
1858*0b57cec5SDimitry Andric 
1859*0b57cec5SDimitry Andric class CommandObjectThreadPlanDiscard : public CommandObjectParsed {
1860*0b57cec5SDimitry Andric public:
1861*0b57cec5SDimitry Andric   CommandObjectThreadPlanDiscard(CommandInterpreter &interpreter)
1862*0b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "thread plan discard",
1863*0b57cec5SDimitry Andric                             "Discards thread plans up to and including the "
1864*0b57cec5SDimitry Andric                             "specified index (see 'thread plan list'.)  "
1865*0b57cec5SDimitry Andric                             "Only user visible plans can be discarded.",
1866*0b57cec5SDimitry Andric                             nullptr,
1867*0b57cec5SDimitry Andric                             eCommandRequiresProcess | eCommandRequiresThread |
1868*0b57cec5SDimitry Andric                                 eCommandTryTargetAPILock |
1869*0b57cec5SDimitry Andric                                 eCommandProcessMustBeLaunched |
1870*0b57cec5SDimitry Andric                                 eCommandProcessMustBePaused) {
1871*0b57cec5SDimitry Andric     CommandArgumentEntry arg;
1872*0b57cec5SDimitry Andric     CommandArgumentData plan_index_arg;
1873*0b57cec5SDimitry Andric 
1874*0b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
1875*0b57cec5SDimitry Andric     plan_index_arg.arg_type = eArgTypeUnsignedInteger;
1876*0b57cec5SDimitry Andric     plan_index_arg.arg_repetition = eArgRepeatPlain;
1877*0b57cec5SDimitry Andric 
1878*0b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
1879*0b57cec5SDimitry Andric     // argument entry.
1880*0b57cec5SDimitry Andric     arg.push_back(plan_index_arg);
1881*0b57cec5SDimitry Andric 
1882*0b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
1883*0b57cec5SDimitry Andric     m_arguments.push_back(arg);
1884*0b57cec5SDimitry Andric   }
1885*0b57cec5SDimitry Andric 
1886*0b57cec5SDimitry Andric   ~CommandObjectThreadPlanDiscard() override = default;
1887*0b57cec5SDimitry Andric 
1888e8d8bef9SDimitry Andric   void
1889e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
1890e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
1891e8d8bef9SDimitry Andric     if (!m_exe_ctx.HasThreadScope() || request.GetCursorIndex())
1892e8d8bef9SDimitry Andric       return;
1893e8d8bef9SDimitry Andric 
1894e8d8bef9SDimitry Andric     m_exe_ctx.GetThreadPtr()->AutoCompleteThreadPlans(request);
1895e8d8bef9SDimitry Andric   }
1896e8d8bef9SDimitry Andric 
1897*0b57cec5SDimitry Andric   bool DoExecute(Args &args, CommandReturnObject &result) override {
1898*0b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
1899*0b57cec5SDimitry Andric     if (args.GetArgumentCount() != 1) {
1900*0b57cec5SDimitry Andric       result.AppendErrorWithFormat("Too many arguments, expected one - the "
1901*0b57cec5SDimitry Andric                                    "thread plan index - but got %zu.",
1902*0b57cec5SDimitry Andric                                    args.GetArgumentCount());
1903*0b57cec5SDimitry Andric       return false;
1904*0b57cec5SDimitry Andric     }
1905*0b57cec5SDimitry Andric 
19065ffd83dbSDimitry Andric     uint32_t thread_plan_idx;
19075ffd83dbSDimitry Andric     if (!llvm::to_integer(args.GetArgumentAtIndex(0), thread_plan_idx)) {
1908*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
1909*0b57cec5SDimitry Andric           "Invalid thread index: \"%s\" - should be unsigned int.",
1910*0b57cec5SDimitry Andric           args.GetArgumentAtIndex(0));
1911*0b57cec5SDimitry Andric       return false;
1912*0b57cec5SDimitry Andric     }
1913*0b57cec5SDimitry Andric 
1914*0b57cec5SDimitry Andric     if (thread_plan_idx == 0) {
1915*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
1916*0b57cec5SDimitry Andric           "You wouldn't really want me to discard the base thread plan.");
1917*0b57cec5SDimitry Andric       return false;
1918*0b57cec5SDimitry Andric     }
1919*0b57cec5SDimitry Andric 
1920*0b57cec5SDimitry Andric     if (thread->DiscardUserThreadPlansUpToIndex(thread_plan_idx)) {
1921*0b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1922*0b57cec5SDimitry Andric       return true;
1923*0b57cec5SDimitry Andric     } else {
1924*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
1925*0b57cec5SDimitry Andric           "Could not find User thread plan with index %s.",
1926*0b57cec5SDimitry Andric           args.GetArgumentAtIndex(0));
1927*0b57cec5SDimitry Andric       return false;
1928*0b57cec5SDimitry Andric     }
1929*0b57cec5SDimitry Andric   }
1930*0b57cec5SDimitry Andric };
1931*0b57cec5SDimitry Andric 
19325ffd83dbSDimitry Andric class CommandObjectThreadPlanPrune : public CommandObjectParsed {
19335ffd83dbSDimitry Andric public:
19345ffd83dbSDimitry Andric   CommandObjectThreadPlanPrune(CommandInterpreter &interpreter)
19355ffd83dbSDimitry Andric       : CommandObjectParsed(interpreter, "thread plan prune",
19365ffd83dbSDimitry Andric                             "Removes any thread plans associated with "
19375ffd83dbSDimitry Andric                             "currently unreported threads.  "
19385ffd83dbSDimitry Andric                             "Specify one or more TID's to remove, or if no "
19395ffd83dbSDimitry Andric                             "TID's are provides, remove threads for all "
19405ffd83dbSDimitry Andric                             "unreported threads",
19415ffd83dbSDimitry Andric                             nullptr,
19425ffd83dbSDimitry Andric                             eCommandRequiresProcess |
19435ffd83dbSDimitry Andric                                 eCommandTryTargetAPILock |
19445ffd83dbSDimitry Andric                                 eCommandProcessMustBeLaunched |
19455ffd83dbSDimitry Andric                                 eCommandProcessMustBePaused) {
19465ffd83dbSDimitry Andric     CommandArgumentEntry arg;
19475ffd83dbSDimitry Andric     CommandArgumentData tid_arg;
19485ffd83dbSDimitry Andric 
19495ffd83dbSDimitry Andric     // Define the first (and only) variant of this arg.
19505ffd83dbSDimitry Andric     tid_arg.arg_type = eArgTypeThreadID;
19515ffd83dbSDimitry Andric     tid_arg.arg_repetition = eArgRepeatStar;
19525ffd83dbSDimitry Andric 
19535ffd83dbSDimitry Andric     // There is only one variant this argument could be; put it into the
19545ffd83dbSDimitry Andric     // argument entry.
19555ffd83dbSDimitry Andric     arg.push_back(tid_arg);
19565ffd83dbSDimitry Andric 
19575ffd83dbSDimitry Andric     // Push the data for the first argument into the m_arguments vector.
19585ffd83dbSDimitry Andric     m_arguments.push_back(arg);
19595ffd83dbSDimitry Andric   }
19605ffd83dbSDimitry Andric 
19615ffd83dbSDimitry Andric   ~CommandObjectThreadPlanPrune() override = default;
19625ffd83dbSDimitry Andric 
19635ffd83dbSDimitry Andric   bool DoExecute(Args &args, CommandReturnObject &result) override {
19645ffd83dbSDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
19655ffd83dbSDimitry Andric 
19665ffd83dbSDimitry Andric     if (args.GetArgumentCount() == 0) {
19675ffd83dbSDimitry Andric       process->PruneThreadPlans();
19685ffd83dbSDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
19695ffd83dbSDimitry Andric       return true;
19705ffd83dbSDimitry Andric     }
19715ffd83dbSDimitry Andric 
19725ffd83dbSDimitry Andric     const size_t num_args = args.GetArgumentCount();
19735ffd83dbSDimitry Andric 
19745ffd83dbSDimitry Andric     std::lock_guard<std::recursive_mutex> guard(
19755ffd83dbSDimitry Andric         process->GetThreadList().GetMutex());
19765ffd83dbSDimitry Andric 
19775ffd83dbSDimitry Andric     for (size_t i = 0; i < num_args; i++) {
19785ffd83dbSDimitry Andric       lldb::tid_t tid;
19795ffd83dbSDimitry Andric       if (!llvm::to_integer(args.GetArgumentAtIndex(i), tid)) {
19805ffd83dbSDimitry Andric         result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n",
19815ffd83dbSDimitry Andric                                      args.GetArgumentAtIndex(i));
19825ffd83dbSDimitry Andric         return false;
19835ffd83dbSDimitry Andric       }
19845ffd83dbSDimitry Andric       if (!process->PruneThreadPlansForTID(tid)) {
19855ffd83dbSDimitry Andric         result.AppendErrorWithFormat("Could not find unreported tid: \"%s\"\n",
19865ffd83dbSDimitry Andric                                      args.GetArgumentAtIndex(i));
19875ffd83dbSDimitry Andric         return false;
19885ffd83dbSDimitry Andric       }
19895ffd83dbSDimitry Andric     }
19905ffd83dbSDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishNoResult);
19915ffd83dbSDimitry Andric     return true;
19925ffd83dbSDimitry Andric   }
19935ffd83dbSDimitry Andric };
19945ffd83dbSDimitry Andric 
1995*0b57cec5SDimitry Andric // CommandObjectMultiwordThreadPlan
1996*0b57cec5SDimitry Andric 
1997*0b57cec5SDimitry Andric class CommandObjectMultiwordThreadPlan : public CommandObjectMultiword {
1998*0b57cec5SDimitry Andric public:
1999*0b57cec5SDimitry Andric   CommandObjectMultiwordThreadPlan(CommandInterpreter &interpreter)
2000*0b57cec5SDimitry Andric       : CommandObjectMultiword(
2001*0b57cec5SDimitry Andric             interpreter, "plan",
2002*0b57cec5SDimitry Andric             "Commands for managing thread plans that control execution.",
2003*0b57cec5SDimitry Andric             "thread plan <subcommand> [<subcommand objects]") {
2004*0b57cec5SDimitry Andric     LoadSubCommand(
2005*0b57cec5SDimitry Andric         "list", CommandObjectSP(new CommandObjectThreadPlanList(interpreter)));
2006*0b57cec5SDimitry Andric     LoadSubCommand(
2007*0b57cec5SDimitry Andric         "discard",
2008*0b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectThreadPlanDiscard(interpreter)));
20095ffd83dbSDimitry Andric     LoadSubCommand(
20105ffd83dbSDimitry Andric         "prune",
20115ffd83dbSDimitry Andric         CommandObjectSP(new CommandObjectThreadPlanPrune(interpreter)));
2012*0b57cec5SDimitry Andric   }
2013*0b57cec5SDimitry Andric 
2014*0b57cec5SDimitry Andric   ~CommandObjectMultiwordThreadPlan() override = default;
2015*0b57cec5SDimitry Andric };
2016*0b57cec5SDimitry Andric 
2017e8d8bef9SDimitry Andric // Next are the subcommands of CommandObjectMultiwordTrace
2018e8d8bef9SDimitry Andric 
2019fe6060f1SDimitry Andric // CommandObjectTraceExport
2020fe6060f1SDimitry Andric 
2021fe6060f1SDimitry Andric class CommandObjectTraceExport : public CommandObjectMultiword {
2022fe6060f1SDimitry Andric public:
2023fe6060f1SDimitry Andric   CommandObjectTraceExport(CommandInterpreter &interpreter)
2024fe6060f1SDimitry Andric       : CommandObjectMultiword(
2025fe6060f1SDimitry Andric             interpreter, "trace thread export",
2026fe6060f1SDimitry Andric             "Commands for exporting traces of the threads in the current "
2027fe6060f1SDimitry Andric             "process to different formats.",
2028fe6060f1SDimitry Andric             "thread trace export <export-plugin> [<subcommand objects>]") {
2029fe6060f1SDimitry Andric 
2030349cc55cSDimitry Andric     unsigned i = 0;
2031349cc55cSDimitry Andric     for (llvm::StringRef plugin_name =
203281ad6265SDimitry Andric              PluginManager::GetTraceExporterPluginNameAtIndex(i);
2033349cc55cSDimitry Andric          !plugin_name.empty();
2034349cc55cSDimitry Andric          plugin_name = PluginManager::GetTraceExporterPluginNameAtIndex(i++)) {
2035fe6060f1SDimitry Andric       if (ThreadTraceExportCommandCreator command_creator =
2036fe6060f1SDimitry Andric               PluginManager::GetThreadTraceExportCommandCreatorAtIndex(i)) {
2037fe6060f1SDimitry Andric         LoadSubCommand(plugin_name, command_creator(interpreter));
2038fe6060f1SDimitry Andric       }
2039fe6060f1SDimitry Andric     }
2040fe6060f1SDimitry Andric   }
2041fe6060f1SDimitry Andric };
2042fe6060f1SDimitry Andric 
2043e8d8bef9SDimitry Andric // CommandObjectTraceStart
2044e8d8bef9SDimitry Andric 
2045fe6060f1SDimitry Andric class CommandObjectTraceStart : public CommandObjectTraceProxy {
2046e8d8bef9SDimitry Andric public:
2047e8d8bef9SDimitry Andric   CommandObjectTraceStart(CommandInterpreter &interpreter)
2048fe6060f1SDimitry Andric       : CommandObjectTraceProxy(
2049fe6060f1SDimitry Andric             /*live_debug_session_only=*/true, interpreter, "thread trace start",
2050e8d8bef9SDimitry Andric             "Start tracing threads with the corresponding trace "
2051e8d8bef9SDimitry Andric             "plug-in for the current process.",
2052e8d8bef9SDimitry Andric             "thread trace start [<trace-options>]") {}
2053e8d8bef9SDimitry Andric 
2054e8d8bef9SDimitry Andric protected:
2055fe6060f1SDimitry Andric   lldb::CommandObjectSP GetDelegateCommand(Trace &trace) override {
2056fe6060f1SDimitry Andric     return trace.GetThreadTraceStartCommand(m_interpreter);
2057e8d8bef9SDimitry Andric   }
2058e8d8bef9SDimitry Andric };
2059e8d8bef9SDimitry Andric 
2060e8d8bef9SDimitry Andric // CommandObjectTraceStop
2061e8d8bef9SDimitry Andric 
2062fe6060f1SDimitry Andric class CommandObjectTraceStop : public CommandObjectMultipleThreads {
2063e8d8bef9SDimitry Andric public:
2064e8d8bef9SDimitry Andric   CommandObjectTraceStop(CommandInterpreter &interpreter)
2065fe6060f1SDimitry Andric       : CommandObjectMultipleThreads(
2066e8d8bef9SDimitry Andric             interpreter, "thread trace stop",
2067fe6060f1SDimitry Andric             "Stop tracing threads, including the ones traced with the "
2068fe6060f1SDimitry Andric             "\"process trace start\" command."
2069e8d8bef9SDimitry Andric             "Defaults to the current thread. Thread indices can be "
2070fe6060f1SDimitry Andric             "specified as arguments.\n Use the thread-index \"all\" to stop "
2071fe6060f1SDimitry Andric             "tracing "
2072fe6060f1SDimitry Andric             "for all existing threads.",
2073e8d8bef9SDimitry Andric             "thread trace stop [<thread-index> <thread-index> ...]",
2074e8d8bef9SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
2075e8d8bef9SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused |
2076e8d8bef9SDimitry Andric                 eCommandProcessMustBeTraced) {}
2077e8d8bef9SDimitry Andric 
2078e8d8bef9SDimitry Andric   ~CommandObjectTraceStop() override = default;
2079e8d8bef9SDimitry Andric 
2080fe6060f1SDimitry Andric   bool DoExecuteOnThreads(Args &command, CommandReturnObject &result,
2081fe6060f1SDimitry Andric                           llvm::ArrayRef<lldb::tid_t> tids) override {
2082fe6060f1SDimitry Andric     ProcessSP process_sp = m_exe_ctx.GetProcessSP();
2083e8d8bef9SDimitry Andric 
2084fe6060f1SDimitry Andric     TraceSP trace_sp = process_sp->GetTarget().GetTrace();
2085e8d8bef9SDimitry Andric 
2086fe6060f1SDimitry Andric     if (llvm::Error err = trace_sp->Stop(tids))
2087fe6060f1SDimitry Andric       result.AppendError(toString(std::move(err)));
2088fe6060f1SDimitry Andric     else
2089fe6060f1SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
2090fe6060f1SDimitry Andric 
2091fe6060f1SDimitry Andric     return result.Succeeded();
2092e8d8bef9SDimitry Andric   }
2093e8d8bef9SDimitry Andric };
2094e8d8bef9SDimitry Andric 
2095e8d8bef9SDimitry Andric // CommandObjectTraceDumpInstructions
2096e8d8bef9SDimitry Andric #define LLDB_OPTIONS_thread_trace_dump_instructions
2097e8d8bef9SDimitry Andric #include "CommandOptions.inc"
2098e8d8bef9SDimitry Andric 
209981ad6265SDimitry Andric class CommandObjectTraceDumpInstructions : public CommandObjectParsed {
2100e8d8bef9SDimitry Andric public:
2101e8d8bef9SDimitry Andric   class CommandOptions : public Options {
2102e8d8bef9SDimitry Andric   public:
210304eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
2104e8d8bef9SDimitry Andric 
2105e8d8bef9SDimitry Andric     ~CommandOptions() override = default;
2106e8d8bef9SDimitry Andric 
2107e8d8bef9SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2108e8d8bef9SDimitry Andric                           ExecutionContext *execution_context) override {
2109e8d8bef9SDimitry Andric       Status error;
2110e8d8bef9SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
2111e8d8bef9SDimitry Andric 
2112e8d8bef9SDimitry Andric       switch (short_option) {
2113e8d8bef9SDimitry Andric       case 'c': {
2114e8d8bef9SDimitry Andric         int32_t count;
2115e8d8bef9SDimitry Andric         if (option_arg.empty() || option_arg.getAsInteger(0, count) ||
2116e8d8bef9SDimitry Andric             count < 0)
2117e8d8bef9SDimitry Andric           error.SetErrorStringWithFormat(
2118e8d8bef9SDimitry Andric               "invalid integer value for option '%s'",
2119e8d8bef9SDimitry Andric               option_arg.str().c_str());
2120e8d8bef9SDimitry Andric         else
2121e8d8bef9SDimitry Andric           m_count = count;
2122e8d8bef9SDimitry Andric         break;
2123e8d8bef9SDimitry Andric       }
212481ad6265SDimitry Andric       case 'a': {
212581ad6265SDimitry Andric         m_count = std::numeric_limits<decltype(m_count)>::max();
212681ad6265SDimitry Andric         break;
212781ad6265SDimitry Andric       }
2128fe6060f1SDimitry Andric       case 's': {
2129fe6060f1SDimitry Andric         int32_t skip;
2130fe6060f1SDimitry Andric         if (option_arg.empty() || option_arg.getAsInteger(0, skip) || skip < 0)
2131e8d8bef9SDimitry Andric           error.SetErrorStringWithFormat(
2132e8d8bef9SDimitry Andric               "invalid integer value for option '%s'",
2133e8d8bef9SDimitry Andric               option_arg.str().c_str());
2134e8d8bef9SDimitry Andric         else
213581ad6265SDimitry Andric           m_dumper_options.skip = skip;
213681ad6265SDimitry Andric         break;
213781ad6265SDimitry Andric       }
213881ad6265SDimitry Andric       case 'i': {
213981ad6265SDimitry Andric         uint64_t id;
214081ad6265SDimitry Andric         if (option_arg.empty() || option_arg.getAsInteger(0, id))
214181ad6265SDimitry Andric           error.SetErrorStringWithFormat(
214281ad6265SDimitry Andric               "invalid integer value for option '%s'",
214381ad6265SDimitry Andric               option_arg.str().c_str());
214481ad6265SDimitry Andric         else
214581ad6265SDimitry Andric           m_dumper_options.id = id;
214681ad6265SDimitry Andric         break;
214781ad6265SDimitry Andric       }
214881ad6265SDimitry Andric       case 'F': {
214981ad6265SDimitry Andric         m_output_file.emplace(option_arg);
2150e8d8bef9SDimitry Andric         break;
2151e8d8bef9SDimitry Andric       }
2152e8d8bef9SDimitry Andric       case 'r': {
215381ad6265SDimitry Andric         m_dumper_options.raw = true;
2154e8d8bef9SDimitry Andric         break;
2155e8d8bef9SDimitry Andric       }
2156fe6060f1SDimitry Andric       case 'f': {
215781ad6265SDimitry Andric         m_dumper_options.forwards = true;
2158fe6060f1SDimitry Andric         break;
2159fe6060f1SDimitry Andric       }
2160753f127fSDimitry Andric       case 'k': {
2161753f127fSDimitry Andric         m_dumper_options.show_control_flow_kind = true;
2162753f127fSDimitry Andric         break;
2163753f127fSDimitry Andric       }
2164fe6060f1SDimitry Andric       case 't': {
216581ad6265SDimitry Andric         m_dumper_options.show_tsc = true;
216681ad6265SDimitry Andric         break;
216781ad6265SDimitry Andric       }
216881ad6265SDimitry Andric       case 'e': {
216981ad6265SDimitry Andric         m_dumper_options.show_events = true;
217081ad6265SDimitry Andric         break;
217181ad6265SDimitry Andric       }
217281ad6265SDimitry Andric       case 'j': {
217381ad6265SDimitry Andric         m_dumper_options.json = true;
217481ad6265SDimitry Andric         break;
217581ad6265SDimitry Andric       }
217681ad6265SDimitry Andric       case 'J': {
217781ad6265SDimitry Andric         m_dumper_options.pretty_print_json = true;
217881ad6265SDimitry Andric         m_dumper_options.json = true;
217981ad6265SDimitry Andric         break;
218081ad6265SDimitry Andric       }
218181ad6265SDimitry Andric       case 'C': {
218281ad6265SDimitry Andric         m_continue = true;
2183fe6060f1SDimitry Andric         break;
2184fe6060f1SDimitry Andric       }
2185e8d8bef9SDimitry Andric       default:
2186e8d8bef9SDimitry Andric         llvm_unreachable("Unimplemented option");
2187e8d8bef9SDimitry Andric       }
2188e8d8bef9SDimitry Andric       return error;
2189e8d8bef9SDimitry Andric     }
2190e8d8bef9SDimitry Andric 
2191e8d8bef9SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
2192e8d8bef9SDimitry Andric       m_count = kDefaultCount;
219381ad6265SDimitry Andric       m_continue = false;
219481ad6265SDimitry Andric       m_output_file = llvm::None;
219581ad6265SDimitry Andric       m_dumper_options = {};
2196e8d8bef9SDimitry Andric     }
2197e8d8bef9SDimitry Andric 
2198e8d8bef9SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
2199e8d8bef9SDimitry Andric       return llvm::makeArrayRef(g_thread_trace_dump_instructions_options);
2200e8d8bef9SDimitry Andric     }
2201e8d8bef9SDimitry Andric 
2202e8d8bef9SDimitry Andric     static const size_t kDefaultCount = 20;
2203e8d8bef9SDimitry Andric 
2204e8d8bef9SDimitry Andric     // Instance variables to hold the values for command options.
2205e8d8bef9SDimitry Andric     size_t m_count;
220681ad6265SDimitry Andric     size_t m_continue;
220781ad6265SDimitry Andric     llvm::Optional<FileSpec> m_output_file;
220881ad6265SDimitry Andric     TraceDumperOptions m_dumper_options;
2209e8d8bef9SDimitry Andric   };
2210e8d8bef9SDimitry Andric 
2211e8d8bef9SDimitry Andric   CommandObjectTraceDumpInstructions(CommandInterpreter &interpreter)
221281ad6265SDimitry Andric       : CommandObjectParsed(
2213e8d8bef9SDimitry Andric             interpreter, "thread trace dump instructions",
221481ad6265SDimitry Andric             "Dump the traced instructions for one thread. If no "
221581ad6265SDimitry Andric             "thread is specified, show the current thread.",
2216e8d8bef9SDimitry Andric             nullptr,
221781ad6265SDimitry Andric             eCommandRequiresProcess | eCommandRequiresThread |
221881ad6265SDimitry Andric                 eCommandTryTargetAPILock | eCommandProcessMustBeLaunched |
221981ad6265SDimitry Andric                 eCommandProcessMustBePaused | eCommandProcessMustBeTraced) {
222081ad6265SDimitry Andric     CommandArgumentData thread_arg{eArgTypeThreadIndex, eArgRepeatOptional};
222181ad6265SDimitry Andric     m_arguments.push_back({thread_arg});
222281ad6265SDimitry Andric   }
2223e8d8bef9SDimitry Andric 
2224e8d8bef9SDimitry Andric   ~CommandObjectTraceDumpInstructions() override = default;
2225e8d8bef9SDimitry Andric 
2226e8d8bef9SDimitry Andric   Options *GetOptions() override { return &m_options; }
2227e8d8bef9SDimitry Andric 
222881ad6265SDimitry Andric   llvm::Optional<std::string> GetRepeatCommand(Args &current_command_args,
2229e8d8bef9SDimitry Andric                                                uint32_t index) override {
223081ad6265SDimitry Andric     std::string cmd;
223181ad6265SDimitry Andric     current_command_args.GetCommandString(cmd);
223281ad6265SDimitry Andric     if (cmd.find(" --continue") == std::string::npos)
223381ad6265SDimitry Andric       cmd += " --continue";
223481ad6265SDimitry Andric     return cmd;
2235e8d8bef9SDimitry Andric   }
2236e8d8bef9SDimitry Andric 
2237e8d8bef9SDimitry Andric protected:
223881ad6265SDimitry Andric   ThreadSP GetThread(Args &args, CommandReturnObject &result) {
223981ad6265SDimitry Andric     if (args.GetArgumentCount() == 0)
224081ad6265SDimitry Andric       return m_exe_ctx.GetThreadSP();
2241fe6060f1SDimitry Andric 
224281ad6265SDimitry Andric     const char *arg = args.GetArgumentAtIndex(0);
224381ad6265SDimitry Andric     uint32_t thread_idx;
2244e8d8bef9SDimitry Andric 
224581ad6265SDimitry Andric     if (!llvm::to_integer(arg, thread_idx)) {
224681ad6265SDimitry Andric       result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n",
224781ad6265SDimitry Andric                                    arg);
224881ad6265SDimitry Andric       return nullptr;
2249e8d8bef9SDimitry Andric     }
2250e8d8bef9SDimitry Andric     ThreadSP thread_sp =
225181ad6265SDimitry Andric         m_exe_ctx.GetProcessRef().GetThreadList().FindThreadByIndexID(
225281ad6265SDimitry Andric             thread_idx);
225381ad6265SDimitry Andric     if (!thread_sp)
225481ad6265SDimitry Andric       result.AppendErrorWithFormat("no thread with index: \"%s\"\n", arg);
225581ad6265SDimitry Andric     return thread_sp;
2256fe6060f1SDimitry Andric   }
2257fe6060f1SDimitry Andric 
225881ad6265SDimitry Andric   bool DoExecute(Args &args, CommandReturnObject &result) override {
225981ad6265SDimitry Andric     ThreadSP thread_sp = GetThread(args, result);
226081ad6265SDimitry Andric     if (!thread_sp) {
226181ad6265SDimitry Andric       result.AppendError("invalid thread\n");
226281ad6265SDimitry Andric       return false;
226381ad6265SDimitry Andric     }
226481ad6265SDimitry Andric 
226581ad6265SDimitry Andric     if (m_options.m_continue && m_last_id) {
226681ad6265SDimitry Andric       // We set up the options to continue one instruction past where
226781ad6265SDimitry Andric       // the previous iteration stopped.
226881ad6265SDimitry Andric       m_options.m_dumper_options.skip = 1;
226981ad6265SDimitry Andric       m_options.m_dumper_options.id = m_last_id;
227081ad6265SDimitry Andric     }
227181ad6265SDimitry Andric 
227281ad6265SDimitry Andric     llvm::Expected<TraceCursorUP> cursor_or_error =
227381ad6265SDimitry Andric         m_exe_ctx.GetTargetSP()->GetTrace()->CreateNewCursor(*thread_sp);
227481ad6265SDimitry Andric 
227581ad6265SDimitry Andric     if (!cursor_or_error) {
227681ad6265SDimitry Andric       result.AppendError(llvm::toString(cursor_or_error.takeError()));
227781ad6265SDimitry Andric       return false;
227881ad6265SDimitry Andric     }
227981ad6265SDimitry Andric     TraceCursorUP &cursor_up = *cursor_or_error;
228081ad6265SDimitry Andric 
228181ad6265SDimitry Andric     if (m_options.m_dumper_options.id &&
228281ad6265SDimitry Andric         !cursor_up->HasId(*m_options.m_dumper_options.id)) {
228381ad6265SDimitry Andric       result.AppendError("invalid instruction id\n");
228481ad6265SDimitry Andric       return false;
228581ad6265SDimitry Andric     }
228681ad6265SDimitry Andric 
228781ad6265SDimitry Andric     llvm::Optional<StreamFile> out_file;
228881ad6265SDimitry Andric     if (m_options.m_output_file) {
228981ad6265SDimitry Andric       out_file.emplace(m_options.m_output_file->GetPath().c_str(),
229081ad6265SDimitry Andric                        File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate,
229181ad6265SDimitry Andric                        lldb::eFilePermissionsFileDefault);
229281ad6265SDimitry Andric     }
229381ad6265SDimitry Andric 
229481ad6265SDimitry Andric     if (m_options.m_continue && !m_last_id) {
229581ad6265SDimitry Andric       // We need to stop processing data when we already ran out of instructions
229681ad6265SDimitry Andric       // in a previous command. We can fake this by setting the cursor past the
229781ad6265SDimitry Andric       // end of the trace.
229881ad6265SDimitry Andric       cursor_up->Seek(1, TraceCursor::SeekType::End);
229981ad6265SDimitry Andric     }
230081ad6265SDimitry Andric 
230181ad6265SDimitry Andric     TraceDumper dumper(std::move(cursor_up),
230281ad6265SDimitry Andric                        out_file ? *out_file : result.GetOutputStream(),
230381ad6265SDimitry Andric                        m_options.m_dumper_options);
230481ad6265SDimitry Andric 
230581ad6265SDimitry Andric     m_last_id = dumper.DumpInstructions(m_options.m_count);
2306e8d8bef9SDimitry Andric     return true;
2307e8d8bef9SDimitry Andric   }
2308e8d8bef9SDimitry Andric 
2309e8d8bef9SDimitry Andric   CommandOptions m_options;
231081ad6265SDimitry Andric   // Last traversed id used to continue a repeat command. None means
231181ad6265SDimitry Andric   // that all the trace has been consumed.
231281ad6265SDimitry Andric   llvm::Optional<lldb::user_id_t> m_last_id;
2313fe6060f1SDimitry Andric };
2314fe6060f1SDimitry Andric 
2315fe6060f1SDimitry Andric // CommandObjectTraceDumpInfo
2316fe6060f1SDimitry Andric #define LLDB_OPTIONS_thread_trace_dump_info
2317fe6060f1SDimitry Andric #include "CommandOptions.inc"
2318fe6060f1SDimitry Andric 
2319fe6060f1SDimitry Andric class CommandObjectTraceDumpInfo : public CommandObjectIterateOverThreads {
2320fe6060f1SDimitry Andric public:
2321fe6060f1SDimitry Andric   class CommandOptions : public Options {
2322fe6060f1SDimitry Andric   public:
232304eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
2324fe6060f1SDimitry Andric 
2325fe6060f1SDimitry Andric     ~CommandOptions() override = default;
2326fe6060f1SDimitry Andric 
2327fe6060f1SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2328fe6060f1SDimitry Andric                           ExecutionContext *execution_context) override {
2329fe6060f1SDimitry Andric       Status error;
2330fe6060f1SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
2331fe6060f1SDimitry Andric 
2332fe6060f1SDimitry Andric       switch (short_option) {
2333fe6060f1SDimitry Andric       case 'v': {
2334fe6060f1SDimitry Andric         m_verbose = true;
2335fe6060f1SDimitry Andric         break;
2336fe6060f1SDimitry Andric       }
2337753f127fSDimitry Andric       case 'j': {
2338753f127fSDimitry Andric         m_json = true;
2339753f127fSDimitry Andric         break;
2340753f127fSDimitry Andric       }
2341fe6060f1SDimitry Andric       default:
2342fe6060f1SDimitry Andric         llvm_unreachable("Unimplemented option");
2343fe6060f1SDimitry Andric       }
2344fe6060f1SDimitry Andric       return error;
2345fe6060f1SDimitry Andric     }
2346fe6060f1SDimitry Andric 
2347fe6060f1SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
2348fe6060f1SDimitry Andric       m_verbose = false;
2349753f127fSDimitry Andric       m_json = false;
2350fe6060f1SDimitry Andric     }
2351fe6060f1SDimitry Andric 
2352fe6060f1SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
2353fe6060f1SDimitry Andric       return llvm::makeArrayRef(g_thread_trace_dump_info_options);
2354fe6060f1SDimitry Andric     }
2355fe6060f1SDimitry Andric 
2356fe6060f1SDimitry Andric     // Instance variables to hold the values for command options.
2357fe6060f1SDimitry Andric     bool m_verbose;
2358753f127fSDimitry Andric     bool m_json;
2359fe6060f1SDimitry Andric   };
2360fe6060f1SDimitry Andric 
2361fe6060f1SDimitry Andric   CommandObjectTraceDumpInfo(CommandInterpreter &interpreter)
2362fe6060f1SDimitry Andric       : CommandObjectIterateOverThreads(
2363fe6060f1SDimitry Andric             interpreter, "thread trace dump info",
2364fe6060f1SDimitry Andric             "Dump the traced information for one or more threads.  If no "
2365fe6060f1SDimitry Andric             "threads are specified, show the current thread. Use the "
2366fe6060f1SDimitry Andric             "thread-index \"all\" to see all threads.",
2367fe6060f1SDimitry Andric             nullptr,
2368fe6060f1SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
2369fe6060f1SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused |
237004eeddc0SDimitry Andric                 eCommandProcessMustBeTraced) {}
2371fe6060f1SDimitry Andric 
2372fe6060f1SDimitry Andric   ~CommandObjectTraceDumpInfo() override = default;
2373fe6060f1SDimitry Andric 
2374fe6060f1SDimitry Andric   Options *GetOptions() override { return &m_options; }
2375fe6060f1SDimitry Andric 
2376fe6060f1SDimitry Andric protected:
2377fe6060f1SDimitry Andric   bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override {
2378fe6060f1SDimitry Andric     const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace();
2379fe6060f1SDimitry Andric     ThreadSP thread_sp =
2380fe6060f1SDimitry Andric         m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
2381fe6060f1SDimitry Andric     trace_sp->DumpTraceInfo(*thread_sp, result.GetOutputStream(),
2382753f127fSDimitry Andric                             m_options.m_verbose, m_options.m_json);
2383fe6060f1SDimitry Andric     return true;
2384fe6060f1SDimitry Andric   }
2385fe6060f1SDimitry Andric 
2386fe6060f1SDimitry Andric   CommandOptions m_options;
2387e8d8bef9SDimitry Andric };
2388e8d8bef9SDimitry Andric 
2389e8d8bef9SDimitry Andric // CommandObjectMultiwordTraceDump
2390e8d8bef9SDimitry Andric class CommandObjectMultiwordTraceDump : public CommandObjectMultiword {
2391e8d8bef9SDimitry Andric public:
2392e8d8bef9SDimitry Andric   CommandObjectMultiwordTraceDump(CommandInterpreter &interpreter)
2393e8d8bef9SDimitry Andric       : CommandObjectMultiword(
2394e8d8bef9SDimitry Andric             interpreter, "dump",
2395e8d8bef9SDimitry Andric             "Commands for displaying trace information of the threads "
2396e8d8bef9SDimitry Andric             "in the current process.",
2397e8d8bef9SDimitry Andric             "thread trace dump <subcommand> [<subcommand objects>]") {
2398e8d8bef9SDimitry Andric     LoadSubCommand(
2399e8d8bef9SDimitry Andric         "instructions",
2400e8d8bef9SDimitry Andric         CommandObjectSP(new CommandObjectTraceDumpInstructions(interpreter)));
2401fe6060f1SDimitry Andric     LoadSubCommand(
2402fe6060f1SDimitry Andric         "info", CommandObjectSP(new CommandObjectTraceDumpInfo(interpreter)));
2403e8d8bef9SDimitry Andric   }
2404e8d8bef9SDimitry Andric   ~CommandObjectMultiwordTraceDump() override = default;
2405e8d8bef9SDimitry Andric };
2406e8d8bef9SDimitry Andric 
2407e8d8bef9SDimitry Andric // CommandObjectMultiwordTrace
2408e8d8bef9SDimitry Andric class CommandObjectMultiwordTrace : public CommandObjectMultiword {
2409e8d8bef9SDimitry Andric public:
2410e8d8bef9SDimitry Andric   CommandObjectMultiwordTrace(CommandInterpreter &interpreter)
2411e8d8bef9SDimitry Andric       : CommandObjectMultiword(
2412e8d8bef9SDimitry Andric             interpreter, "trace",
2413e8d8bef9SDimitry Andric             "Commands for operating on traces of the threads in the current "
2414e8d8bef9SDimitry Andric             "process.",
2415e8d8bef9SDimitry Andric             "thread trace <subcommand> [<subcommand objects>]") {
2416e8d8bef9SDimitry Andric     LoadSubCommand("dump", CommandObjectSP(new CommandObjectMultiwordTraceDump(
2417e8d8bef9SDimitry Andric                                interpreter)));
2418e8d8bef9SDimitry Andric     LoadSubCommand("start",
2419e8d8bef9SDimitry Andric                    CommandObjectSP(new CommandObjectTraceStart(interpreter)));
2420e8d8bef9SDimitry Andric     LoadSubCommand("stop",
2421e8d8bef9SDimitry Andric                    CommandObjectSP(new CommandObjectTraceStop(interpreter)));
2422fe6060f1SDimitry Andric     LoadSubCommand("export",
2423fe6060f1SDimitry Andric                    CommandObjectSP(new CommandObjectTraceExport(interpreter)));
2424e8d8bef9SDimitry Andric   }
2425e8d8bef9SDimitry Andric 
2426e8d8bef9SDimitry Andric   ~CommandObjectMultiwordTrace() override = default;
2427e8d8bef9SDimitry Andric };
2428e8d8bef9SDimitry Andric 
2429*0b57cec5SDimitry Andric // CommandObjectMultiwordThread
2430*0b57cec5SDimitry Andric 
2431*0b57cec5SDimitry Andric CommandObjectMultiwordThread::CommandObjectMultiwordThread(
2432*0b57cec5SDimitry Andric     CommandInterpreter &interpreter)
2433480093f4SDimitry Andric     : CommandObjectMultiword(interpreter, "thread",
2434480093f4SDimitry Andric                              "Commands for operating on "
2435*0b57cec5SDimitry Andric                              "one or more threads in "
2436*0b57cec5SDimitry Andric                              "the current process.",
2437*0b57cec5SDimitry Andric                              "thread <subcommand> [<subcommand-options>]") {
2438*0b57cec5SDimitry Andric   LoadSubCommand("backtrace", CommandObjectSP(new CommandObjectThreadBacktrace(
2439*0b57cec5SDimitry Andric                                   interpreter)));
2440*0b57cec5SDimitry Andric   LoadSubCommand("continue",
2441*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectThreadContinue(interpreter)));
2442*0b57cec5SDimitry Andric   LoadSubCommand("list",
2443*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectThreadList(interpreter)));
2444*0b57cec5SDimitry Andric   LoadSubCommand("return",
2445*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectThreadReturn(interpreter)));
2446*0b57cec5SDimitry Andric   LoadSubCommand("jump",
2447*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectThreadJump(interpreter)));
2448*0b57cec5SDimitry Andric   LoadSubCommand("select",
2449*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectThreadSelect(interpreter)));
2450*0b57cec5SDimitry Andric   LoadSubCommand("until",
2451*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectThreadUntil(interpreter)));
2452*0b57cec5SDimitry Andric   LoadSubCommand("info",
2453*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectThreadInfo(interpreter)));
2454480093f4SDimitry Andric   LoadSubCommand("exception", CommandObjectSP(new CommandObjectThreadException(
2455480093f4SDimitry Andric                                   interpreter)));
2456d56accc7SDimitry Andric   LoadSubCommand("siginfo",
2457d56accc7SDimitry Andric                  CommandObjectSP(new CommandObjectThreadSiginfo(interpreter)));
2458*0b57cec5SDimitry Andric   LoadSubCommand("step-in",
2459*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2460*0b57cec5SDimitry Andric                      interpreter, "thread step-in",
2461*0b57cec5SDimitry Andric                      "Source level single step, stepping into calls.  Defaults "
2462*0b57cec5SDimitry Andric                      "to current thread unless specified.",
2463*0b57cec5SDimitry Andric                      nullptr, eStepTypeInto, eStepScopeSource)));
2464*0b57cec5SDimitry Andric 
2465*0b57cec5SDimitry Andric   LoadSubCommand("step-out",
2466*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2467*0b57cec5SDimitry Andric                      interpreter, "thread step-out",
2468*0b57cec5SDimitry Andric                      "Finish executing the current stack frame and stop after "
2469*0b57cec5SDimitry Andric                      "returning.  Defaults to current thread unless specified.",
2470*0b57cec5SDimitry Andric                      nullptr, eStepTypeOut, eStepScopeSource)));
2471*0b57cec5SDimitry Andric 
2472*0b57cec5SDimitry Andric   LoadSubCommand("step-over",
2473*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2474*0b57cec5SDimitry Andric                      interpreter, "thread step-over",
2475*0b57cec5SDimitry Andric                      "Source level single step, stepping over calls.  Defaults "
2476*0b57cec5SDimitry Andric                      "to current thread unless specified.",
2477*0b57cec5SDimitry Andric                      nullptr, eStepTypeOver, eStepScopeSource)));
2478*0b57cec5SDimitry Andric 
2479*0b57cec5SDimitry Andric   LoadSubCommand("step-inst",
2480*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2481*0b57cec5SDimitry Andric                      interpreter, "thread step-inst",
2482*0b57cec5SDimitry Andric                      "Instruction level single step, stepping into calls.  "
2483*0b57cec5SDimitry Andric                      "Defaults to current thread unless specified.",
2484*0b57cec5SDimitry Andric                      nullptr, eStepTypeTrace, eStepScopeInstruction)));
2485*0b57cec5SDimitry Andric 
2486*0b57cec5SDimitry Andric   LoadSubCommand("step-inst-over",
2487*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2488*0b57cec5SDimitry Andric                      interpreter, "thread step-inst-over",
2489*0b57cec5SDimitry Andric                      "Instruction level single step, stepping over calls.  "
2490*0b57cec5SDimitry Andric                      "Defaults to current thread unless specified.",
2491*0b57cec5SDimitry Andric                      nullptr, eStepTypeTraceOver, eStepScopeInstruction)));
2492*0b57cec5SDimitry Andric 
2493*0b57cec5SDimitry Andric   LoadSubCommand(
2494*0b57cec5SDimitry Andric       "step-scripted",
2495*0b57cec5SDimitry Andric       CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2496*0b57cec5SDimitry Andric           interpreter, "thread step-scripted",
24979dba64beSDimitry Andric           "Step as instructed by the script class passed in the -C option.  "
24989dba64beSDimitry Andric           "You can also specify a dictionary of key (-k) and value (-v) pairs "
24999dba64beSDimitry Andric           "that will be used to populate an SBStructuredData Dictionary, which "
25009dba64beSDimitry Andric           "will be passed to the constructor of the class implementing the "
25019dba64beSDimitry Andric           "scripted step.  See the Python Reference for more details.",
2502*0b57cec5SDimitry Andric           nullptr, eStepTypeScripted, eStepScopeSource)));
2503*0b57cec5SDimitry Andric 
2504*0b57cec5SDimitry Andric   LoadSubCommand("plan", CommandObjectSP(new CommandObjectMultiwordThreadPlan(
2505*0b57cec5SDimitry Andric                              interpreter)));
2506e8d8bef9SDimitry Andric   LoadSubCommand("trace",
2507e8d8bef9SDimitry Andric                  CommandObjectSP(new CommandObjectMultiwordTrace(interpreter)));
2508*0b57cec5SDimitry Andric }
2509*0b57cec5SDimitry Andric 
2510*0b57cec5SDimitry Andric CommandObjectMultiwordThread::~CommandObjectMultiwordThread() = default;
2511