blob: b585ef9ef8ad9813e721cc5fbd8b1357727fc304 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandObjectThread.cpp ---------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "CommandObjectThread.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/SourceManager.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include "lldb/Core/State.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000018#include "lldb/Core/ValueObject.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000019#include "lldb/Host/Host.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000020#include "lldb/Host/OptionParser.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000021#include "lldb/Host/StringConvert.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Interpreter/CommandInterpreter.h"
23#include "lldb/Interpreter/CommandReturnObject.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "lldb/Interpreter/Options.h"
25#include "lldb/Symbol/CompileUnit.h"
26#include "lldb/Symbol/Function.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Symbol/LineEntry.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000028#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Process.h"
30#include "lldb/Target/RegisterContext.h"
Jason Molenda750ea692013-11-12 07:02:07 +000031#include "lldb/Target/SystemRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Target/Target.h"
33#include "lldb/Target/Thread.h"
34#include "lldb/Target/ThreadPlan.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000035#include "lldb/Target/ThreadPlanStepInRange.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Target/ThreadPlanStepInstruction.h"
37#include "lldb/Target/ThreadPlanStepOut.h"
38#include "lldb/Target/ThreadPlanStepRange.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000039#include "lldb/lldb-private.h"
Greg Clayton1f746072012-08-29 21:13:06 +000040
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041using namespace lldb;
42using namespace lldb_private;
43
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044//-------------------------------------------------------------------------
45// CommandObjectThreadBacktrace
46//-------------------------------------------------------------------------
47
Kate Stoneb9c1b512016-09-06 20:57:50 +000048class CommandObjectIterateOverThreads : public CommandObjectParsed {
Jim Ingham2bdbfd52014-09-29 23:17:18 +000049public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 CommandObjectIterateOverThreads(CommandInterpreter &interpreter,
51 const char *name, const char *help,
52 const char *syntax, uint32_t flags)
53 : CommandObjectParsed(interpreter, name, help, syntax, flags) {}
54
55 ~CommandObjectIterateOverThreads() override = default;
56
57 bool DoExecute(Args &command, CommandReturnObject &result) override {
58 result.SetStatus(m_success_return);
59
60 if (command.GetArgumentCount() == 0) {
61 Thread *thread = m_exe_ctx.GetThreadPtr();
62 if (!HandleOneThread(thread->GetID(), result))
63 return false;
64 return result.Succeeded();
Jim Ingham2bdbfd52014-09-29 23:17:18 +000065 }
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 // Use tids instead of ThreadSPs to prevent deadlocking problems which
68 // result from JIT-ing
69 // code while iterating over the (locked) ThreadSP list.
70 std::vector<lldb::tid_t> tids;
Bruce Mitchener13d21e92015-10-07 16:56:17 +000071
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 if (command.GetArgumentCount() == 1 &&
73 ::strcmp(command.GetArgumentAtIndex(0), "all") == 0) {
74 Process *process = m_exe_ctx.GetProcessPtr();
Jim Ingham2bdbfd52014-09-29 23:17:18 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 for (ThreadSP thread_sp : process->Threads())
77 tids.push_back(thread_sp->GetID());
78 } else {
79 const size_t num_args = command.GetArgumentCount();
80 Process *process = m_exe_ctx.GetProcessPtr();
81
82 std::lock_guard<std::recursive_mutex> guard(
83 process->GetThreadList().GetMutex());
84
85 for (size_t i = 0; i < num_args; i++) {
86 bool success;
87
88 uint32_t thread_idx = StringConvert::ToUInt32(
89 command.GetArgumentAtIndex(i), 0, 0, &success);
90 if (!success) {
91 result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n",
92 command.GetArgumentAtIndex(i));
93 result.SetStatus(eReturnStatusFailed);
94 return false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +000095 }
Stephane Sezerf8104912016-03-17 18:52:41 +000096
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 ThreadSP thread =
98 process->GetThreadList().FindThreadByIndexID(thread_idx);
Stephane Sezerf8104912016-03-17 18:52:41 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 if (!thread) {
101 result.AppendErrorWithFormat("no thread with index: \"%s\"\n",
102 command.GetArgumentAtIndex(i));
103 result.SetStatus(eReturnStatusFailed);
104 return false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000105 }
Stephane Sezerf8104912016-03-17 18:52:41 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 tids.push_back(thread->GetID());
108 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000109 }
110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 uint32_t idx = 0;
112 for (const lldb::tid_t &tid : tids) {
113 if (idx != 0 && m_add_return)
114 result.AppendMessage("");
115
116 if (!HandleOneThread(tid, result))
117 return false;
118
119 ++idx;
120 }
121 return result.Succeeded();
122 }
123
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000124protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 // Override this to do whatever you need to do for one thread.
126 //
127 // If you return false, the iteration will stop, otherwise it will proceed.
128 // The result is set to m_success_return (defaults to
129 // eReturnStatusSuccessFinishResult) before the iteration,
130 // so you only need to set the return status in HandleOneThread if you want to
131 // indicate an error.
132 // If m_add_return is true, a blank line will be inserted between each of the
133 // listings (except the last one.)
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 virtual bool HandleOneThread(lldb::tid_t, CommandReturnObject &result) = 0;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 ReturnStatus m_success_return = eReturnStatusSuccessFinishResult;
138 bool m_add_return = true;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000139};
140
141//-------------------------------------------------------------------------
142// CommandObjectThreadBacktrace
143//-------------------------------------------------------------------------
144
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000145static OptionDefinition g_thread_backtrace_options[] = {
146 // clang-format off
147 { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCount, "How many frames to display (-1 for all)" },
148 { LLDB_OPT_SET_1, false, "start", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFrameIndex, "Frame in which to start the backtrace" },
149 { LLDB_OPT_SET_1, false, "extended", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Show the extended backtrace, if available" }
150 // clang-format on
151};
152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153class CommandObjectThreadBacktrace : public CommandObjectIterateOverThreads {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 class CommandOptions : public Options {
156 public:
157 CommandOptions() : Options() {
158 // Keep default values of all options in one place: OptionParsingStarting
159 // ()
160 OptionParsingStarting(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 }
162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 ~CommandOptions() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164
Zachary Turner97206d52017-05-12 04:51:55 +0000165 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
166 ExecutionContext *execution_context) override {
167 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 const int short_option = m_getopt_table[option_idx].val;
169
170 switch (short_option) {
171 case 'c': {
Zachary Turnerfe114832016-11-12 16:56:47 +0000172 int32_t input_count = 0;
173 if (option_arg.getAsInteger(0, m_count)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 m_count = UINT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 error.SetErrorStringWithFormat(
176 "invalid integer value for option '%c'", short_option);
Zachary Turnerfe114832016-11-12 16:56:47 +0000177 } else if (input_count < 0)
178 m_count = UINT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 } break;
Zachary Turnerfe114832016-11-12 16:56:47 +0000180 case 's':
181 if (option_arg.getAsInteger(0, m_start))
182 error.SetErrorStringWithFormat(
183 "invalid integer value for option '%c'", short_option);
184 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 case 'e': {
186 bool success;
187 m_extended_backtrace =
Zachary Turnerfe114832016-11-12 16:56:47 +0000188 Args::StringToBoolean(option_arg, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 if (!success)
190 error.SetErrorStringWithFormat(
191 "invalid boolean value for option '%c'", short_option);
192 } break;
193 default:
194 error.SetErrorStringWithFormat("invalid short option character '%c'",
195 short_option);
196 break;
197 }
198 return error;
Jim Inghame2e0b452010-08-26 23:36:03 +0000199 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 void OptionParsingStarting(ExecutionContext *execution_context) override {
202 m_count = UINT32_MAX;
203 m_start = 0;
204 m_extended_backtrace = false;
205 }
206
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000207 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000208 return llvm::makeArrayRef(g_thread_backtrace_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000209 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210
211 // Instance variables to hold the values for command options.
212 uint32_t m_count;
213 uint32_t m_start;
214 bool m_extended_backtrace;
215 };
216
217 CommandObjectThreadBacktrace(CommandInterpreter &interpreter)
218 : CommandObjectIterateOverThreads(
219 interpreter, "thread backtrace",
220 "Show thread call stacks. Defaults to the current thread, thread "
221 "indexes can be specified as arguments. Use the thread-index "
222 "\"all\" "
223 "to see all threads.",
224 nullptr,
225 eCommandRequiresProcess | eCommandRequiresThread |
226 eCommandTryTargetAPILock | eCommandProcessMustBeLaunched |
227 eCommandProcessMustBePaused),
228 m_options() {}
229
230 ~CommandObjectThreadBacktrace() override = default;
231
232 Options *GetOptions() override { return &m_options; }
233
Jim Ingham5a988412012-06-08 21:56:10 +0000234protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 void DoExtendedBacktrace(Thread *thread, CommandReturnObject &result) {
236 SystemRuntime *runtime = thread->GetProcess()->GetSystemRuntime();
237 if (runtime) {
238 Stream &strm = result.GetOutputStream();
239 const std::vector<ConstString> &types =
240 runtime->GetExtendedBacktraceTypes();
241 for (auto type : types) {
242 ThreadSP ext_thread_sp = runtime->GetExtendedBacktraceThread(
243 thread->shared_from_this(), type);
244 if (ext_thread_sp && ext_thread_sp->IsValid()) {
245 const uint32_t num_frames_with_source = 0;
Jim Ingham6a9767c2016-11-08 20:36:40 +0000246 const bool stop_format = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 if (ext_thread_sp->GetStatus(strm, m_options.m_start,
248 m_options.m_count,
Jim Ingham6a9767c2016-11-08 20:36:40 +0000249 num_frames_with_source,
250 stop_format)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 DoExtendedBacktrace(ext_thread_sp.get(), result);
252 }
Jason Molenda750ea692013-11-12 07:02:07 +0000253 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 }
255 }
256 }
257
258 bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override {
259 ThreadSP thread_sp =
260 m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
261 if (!thread_sp) {
262 result.AppendErrorWithFormat(
263 "thread disappeared while computing backtraces: 0x%" PRIx64 "\n",
264 tid);
265 result.SetStatus(eReturnStatusFailed);
266 return false;
Jason Molenda750ea692013-11-12 07:02:07 +0000267 }
268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 Thread *thread = thread_sp.get();
Stephane Sezerf8104912016-03-17 18:52:41 +0000270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 Stream &strm = result.GetOutputStream();
Stephane Sezerf8104912016-03-17 18:52:41 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 // Don't show source context when doing backtraces.
274 const uint32_t num_frames_with_source = 0;
Jim Ingham4f243e82016-11-08 23:43:36 +0000275 const bool stop_format = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 if (!thread->GetStatus(strm, m_options.m_start, m_options.m_count,
Jim Ingham4f243e82016-11-08 23:43:36 +0000277 num_frames_with_source, stop_format)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 result.AppendErrorWithFormat(
279 "error displaying backtrace for thread: \"0x%4.4x\"\n",
280 thread->GetIndexID());
281 result.SetStatus(eReturnStatusFailed);
282 return false;
283 }
284 if (m_options.m_extended_backtrace) {
285 DoExtendedBacktrace(thread, result);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 }
Jim Ingham5a988412012-06-08 21:56:10 +0000287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 return true;
289 }
290
291 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292};
293
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294enum StepScope { eStepScopeSource, eStepScopeInstruction };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000296static OptionEnumValueElement g_tri_running_mode[] = {
297 {eOnlyThisThread, "this-thread", "Run only this thread"},
298 {eAllThreads, "all-threads", "Run all threads"},
299 {eOnlyDuringStepping, "while-stepping",
300 "Run only this thread while stepping"},
301 {0, nullptr, nullptr}};
302
303static OptionEnumValueElement g_duo_running_mode[] = {
304 {eOnlyThisThread, "this-thread", "Run only this thread"},
305 {eAllThreads, "all-threads", "Run all threads"},
306 {0, nullptr, nullptr}};
307
308static OptionDefinition g_thread_step_scope_options[] = {
309 // clang-format off
310 { LLDB_OPT_SET_1, false, "step-in-avoids-no-debug", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "A boolean value that sets whether stepping into functions will step over functions with no debug information." },
311 { LLDB_OPT_SET_1, false, "step-out-avoids-no-debug", 'A', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "A boolean value, if true stepping out of functions will continue to step out till it hits a function with debug information." },
312 { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 1, eArgTypeCount, "How many times to perform the stepping operation - currently only supported for step-inst and next-inst." },
313 { LLDB_OPT_SET_1, false, "end-linenumber", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 1, eArgTypeLineNum, "The line at which to stop stepping - defaults to the next line and only supported for step-in and step-over. You can also pass the string 'block' to step to the end of the current block. This is particularly useful in conjunction with --step-target to step through a complex calling sequence." },
314 { LLDB_OPT_SET_1, false, "run-mode", 'm', OptionParser::eRequiredArgument, nullptr, g_tri_running_mode, 0, eArgTypeRunMode, "Determine how to run other threads while stepping the current thread." },
315 { LLDB_OPT_SET_1, false, "step-over-regexp", 'r', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeRegularExpression, "A regular expression that defines function names to not to stop at when stepping in." },
316 { LLDB_OPT_SET_1, false, "step-in-target", 't', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFunctionName, "The name of the directly called function step in should stop at when stepping into." },
317 { LLDB_OPT_SET_2, false, "python-class", 'C', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonClass, "The name of the class that will manage this step - only supported for Scripted Step." }
318 // clang-format on
319};
320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 class CommandOptions : public Options {
324 public:
325 CommandOptions() : Options() {
326 // Keep default values of all options in one place: OptionParsingStarting
327 // ()
328 OptionParsingStarting(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329 }
330
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331 ~CommandOptions() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332
Zachary Turner97206d52017-05-12 04:51:55 +0000333 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
334 ExecutionContext *execution_context) override {
335 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 const int short_option = m_getopt_table[option_idx].val;
337
338 switch (short_option) {
339 case 'a': {
340 bool success;
Zachary Turnerfe114832016-11-12 16:56:47 +0000341 bool avoid_no_debug = Args::StringToBoolean(option_arg, true, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 if (!success)
343 error.SetErrorStringWithFormat(
344 "invalid boolean value for option '%c'", short_option);
345 else {
346 m_step_in_avoid_no_debug =
347 avoid_no_debug ? eLazyBoolYes : eLazyBoolNo;
348 }
349 } break;
350
351 case 'A': {
352 bool success;
Zachary Turnerfe114832016-11-12 16:56:47 +0000353 bool avoid_no_debug = Args::StringToBoolean(option_arg, true, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 if (!success)
355 error.SetErrorStringWithFormat(
356 "invalid boolean value for option '%c'", short_option);
357 else {
358 m_step_out_avoid_no_debug =
359 avoid_no_debug ? eLazyBoolYes : eLazyBoolNo;
360 }
361 } break;
362
363 case 'c':
Zachary Turnerfe114832016-11-12 16:56:47 +0000364 if (option_arg.getAsInteger(0, m_step_count))
365 error.SetErrorStringWithFormat("invalid step count '%s'",
366 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 break;
368
369 case 'C':
370 m_class_name.clear();
371 m_class_name.assign(option_arg);
372 break;
373
374 case 'm': {
375 OptionEnumValueElement *enum_values =
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000376 GetDefinitions()[option_idx].enum_values;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 m_run_mode = (lldb::RunMode)Args::StringToOptionEnum(
Zachary Turnerfe114832016-11-12 16:56:47 +0000378 option_arg, enum_values, eOnlyDuringStepping, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 } break;
380
Zachary Turnerfe114832016-11-12 16:56:47 +0000381 case 'e':
382 if (option_arg == "block") {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 m_end_line_is_block_end = 1;
384 break;
385 }
Zachary Turnerfe114832016-11-12 16:56:47 +0000386 if (option_arg.getAsInteger(0, m_end_line))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 error.SetErrorStringWithFormat("invalid end line number '%s'",
Zachary Turnerfe114832016-11-12 16:56:47 +0000388 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390
391 case 'r':
392 m_avoid_regexp.clear();
393 m_avoid_regexp.assign(option_arg);
394 break;
395
396 case 't':
397 m_step_in_target.clear();
398 m_step_in_target.assign(option_arg);
399 break;
400
401 default:
402 error.SetErrorStringWithFormat("invalid short option character '%c'",
403 short_option);
404 break;
405 }
406 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 }
408
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 void OptionParsingStarting(ExecutionContext *execution_context) override {
410 m_step_in_avoid_no_debug = eLazyBoolCalculate;
411 m_step_out_avoid_no_debug = eLazyBoolCalculate;
412 m_run_mode = eOnlyDuringStepping;
413
414 // Check if we are in Non-Stop mode
415 TargetSP target_sp =
416 execution_context ? execution_context->GetTargetSP() : TargetSP();
417 if (target_sp && target_sp->GetNonStopModeEnabled())
418 m_run_mode = eOnlyThisThread;
419
420 m_avoid_regexp.clear();
421 m_step_in_target.clear();
422 m_class_name.clear();
423 m_step_count = 1;
424 m_end_line = LLDB_INVALID_LINE_NUMBER;
425 m_end_line_is_block_end = false;
426 }
427
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000428 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000429 return llvm::makeArrayRef(g_thread_step_scope_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000430 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431
432 // Instance variables to hold the values for command options.
433 LazyBool m_step_in_avoid_no_debug;
434 LazyBool m_step_out_avoid_no_debug;
435 RunMode m_run_mode;
436 std::string m_avoid_regexp;
437 std::string m_step_in_target;
438 std::string m_class_name;
439 uint32_t m_step_count;
440 uint32_t m_end_line;
441 bool m_end_line_is_block_end;
442 };
443
444 CommandObjectThreadStepWithTypeAndScope(CommandInterpreter &interpreter,
445 const char *name, const char *help,
446 const char *syntax,
447 StepType step_type,
448 StepScope step_scope)
449 : CommandObjectParsed(interpreter, name, help, syntax,
450 eCommandRequiresProcess | eCommandRequiresThread |
451 eCommandTryTargetAPILock |
452 eCommandProcessMustBeLaunched |
453 eCommandProcessMustBePaused),
454 m_step_type(step_type), m_step_scope(step_scope), m_options() {
455 CommandArgumentEntry arg;
456 CommandArgumentData thread_id_arg;
457
458 // Define the first (and only) variant of this arg.
459 thread_id_arg.arg_type = eArgTypeThreadID;
460 thread_id_arg.arg_repetition = eArgRepeatOptional;
461
462 // There is only one variant this argument could be; put it into the
463 // argument entry.
464 arg.push_back(thread_id_arg);
465
466 // Push the data for the first argument into the m_arguments vector.
467 m_arguments.push_back(arg);
468 }
469
470 ~CommandObjectThreadStepWithTypeAndScope() override = default;
471
472 Options *GetOptions() override { return &m_options; }
473
Jim Ingham5a988412012-06-08 21:56:10 +0000474protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475 bool DoExecute(Args &command, CommandReturnObject &result) override {
476 Process *process = m_exe_ctx.GetProcessPtr();
477 bool synchronous_execution = m_interpreter.GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479 const uint32_t num_threads = process->GetThreadList().GetSize();
480 Thread *thread = nullptr;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000481
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482 if (command.GetArgumentCount() == 0) {
483 thread = GetDefaultThread();
Jim Ingham8d94ba02016-03-12 02:45:34 +0000484
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485 if (thread == nullptr) {
486 result.AppendError("no selected thread in process");
487 result.SetStatus(eReturnStatusFailed);
488 return false;
489 }
490 } else {
491 const char *thread_idx_cstr = command.GetArgumentAtIndex(0);
492 uint32_t step_thread_idx =
493 StringConvert::ToUInt32(thread_idx_cstr, LLDB_INVALID_INDEX32);
494 if (step_thread_idx == LLDB_INVALID_INDEX32) {
495 result.AppendErrorWithFormat("invalid thread index '%s'.\n",
496 thread_idx_cstr);
497 result.SetStatus(eReturnStatusFailed);
498 return false;
499 }
500 thread =
501 process->GetThreadList().FindThreadByIndexID(step_thread_idx).get();
502 if (thread == nullptr) {
503 result.AppendErrorWithFormat(
504 "Thread index %u is out of range (valid values are 0 - %u).\n",
505 step_thread_idx, num_threads);
506 result.SetStatus(eReturnStatusFailed);
507 return false;
508 }
509 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 if (m_step_type == eStepTypeScripted) {
512 if (m_options.m_class_name.empty()) {
513 result.AppendErrorWithFormat("empty class name for scripted step.");
514 result.SetStatus(eReturnStatusFailed);
515 return false;
516 } else if (!m_interpreter.GetScriptInterpreter()->CheckObjectExists(
517 m_options.m_class_name.c_str())) {
518 result.AppendErrorWithFormat(
519 "class for scripted step: \"%s\" does not exist.",
520 m_options.m_class_name.c_str());
521 result.SetStatus(eReturnStatusFailed);
522 return false;
523 }
524 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000525
Kate Stoneb9c1b512016-09-06 20:57:50 +0000526 if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER &&
527 m_step_type != eStepTypeInto) {
528 result.AppendErrorWithFormat(
529 "end line option is only valid for step into");
530 result.SetStatus(eReturnStatusFailed);
531 return false;
532 }
533
534 const bool abort_other_plans = false;
535 const lldb::RunMode stop_other_threads = m_options.m_run_mode;
536
537 // This is a bit unfortunate, but not all the commands in this command
538 // object support
539 // only while stepping, so I use the bool for them.
540 bool bool_stop_other_threads;
541 if (m_options.m_run_mode == eAllThreads)
542 bool_stop_other_threads = false;
543 else if (m_options.m_run_mode == eOnlyDuringStepping)
544 bool_stop_other_threads =
545 (m_step_type != eStepTypeOut && m_step_type != eStepTypeScripted);
546 else
547 bool_stop_other_threads = true;
548
549 ThreadPlanSP new_plan_sp;
550
551 if (m_step_type == eStepTypeInto) {
552 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
553 assert(frame != nullptr);
554
555 if (frame->HasDebugInformation()) {
556 AddressRange range;
557 SymbolContext sc = frame->GetSymbolContext(eSymbolContextEverything);
558 if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER) {
Zachary Turner97206d52017-05-12 04:51:55 +0000559 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560 if (!sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range,
561 error)) {
562 result.AppendErrorWithFormat("invalid end-line option: %s.",
563 error.AsCString());
Jim Inghamc17d6bd2016-02-10 03:25:24 +0000564 result.SetStatus(eReturnStatusFailed);
565 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566 }
567 } else if (m_options.m_end_line_is_block_end) {
Zachary Turner97206d52017-05-12 04:51:55 +0000568 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 Block *block = frame->GetSymbolContext(eSymbolContextBlock).block;
570 if (!block) {
571 result.AppendErrorWithFormat("Could not find the current block.");
572 result.SetStatus(eReturnStatusFailed);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000573 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574 }
575
576 AddressRange block_range;
577 Address pc_address = frame->GetFrameCodeAddress();
578 block->GetRangeContainingAddress(pc_address, block_range);
579 if (!block_range.GetBaseAddress().IsValid()) {
580 result.AppendErrorWithFormat(
581 "Could not find the current block address.");
582 result.SetStatus(eReturnStatusFailed);
583 return false;
584 }
585 lldb::addr_t pc_offset_in_block =
586 pc_address.GetFileAddress() -
587 block_range.GetBaseAddress().GetFileAddress();
588 lldb::addr_t range_length =
589 block_range.GetByteSize() - pc_offset_in_block;
590 range = AddressRange(pc_address, range_length);
591 } else {
592 range = sc.line_entry.range;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000593 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000594
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595 new_plan_sp = thread->QueueThreadPlanForStepInRange(
596 abort_other_plans, range,
597 frame->GetSymbolContext(eSymbolContextEverything),
598 m_options.m_step_in_target.c_str(), stop_other_threads,
599 m_options.m_step_in_avoid_no_debug,
600 m_options.m_step_out_avoid_no_debug);
Greg Claytondc6224e2014-10-21 01:00:42 +0000601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 if (new_plan_sp && !m_options.m_avoid_regexp.empty()) {
603 ThreadPlanStepInRange *step_in_range_plan =
604 static_cast<ThreadPlanStepInRange *>(new_plan_sp.get());
605 step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000606 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 } else
608 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
609 false, abort_other_plans, bool_stop_other_threads);
610 } else if (m_step_type == eStepTypeOver) {
611 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
612
613 if (frame->HasDebugInformation())
614 new_plan_sp = thread->QueueThreadPlanForStepOverRange(
615 abort_other_plans,
616 frame->GetSymbolContext(eSymbolContextEverything).line_entry,
617 frame->GetSymbolContext(eSymbolContextEverything),
618 stop_other_threads, m_options.m_step_out_avoid_no_debug);
619 else
620 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
621 true, abort_other_plans, bool_stop_other_threads);
622 } else if (m_step_type == eStepTypeTrace) {
623 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
624 false, abort_other_plans, bool_stop_other_threads);
625 } else if (m_step_type == eStepTypeTraceOver) {
626 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
627 true, abort_other_plans, bool_stop_other_threads);
628 } else if (m_step_type == eStepTypeOut) {
629 new_plan_sp = thread->QueueThreadPlanForStepOut(
630 abort_other_plans, nullptr, false, bool_stop_other_threads, eVoteYes,
631 eVoteNoOpinion, thread->GetSelectedFrameIndex(),
632 m_options.m_step_out_avoid_no_debug);
633 } else if (m_step_type == eStepTypeScripted) {
634 new_plan_sp = thread->QueueThreadPlanForStepScripted(
635 abort_other_plans, m_options.m_class_name.c_str(),
636 bool_stop_other_threads);
637 } else {
638 result.AppendError("step type is not supported");
639 result.SetStatus(eReturnStatusFailed);
640 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641 }
642
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 // If we got a new plan, then set it to be a master plan (User level Plans
644 // should be master plans
645 // so that they can be interruptible). Then resume the process.
646
647 if (new_plan_sp) {
648 new_plan_sp->SetIsMasterPlan(true);
649 new_plan_sp->SetOkayToDiscard(false);
650
651 if (m_options.m_step_count > 1) {
Jim Inghamd2a7e852017-05-25 02:24:18 +0000652 if (!new_plan_sp->SetIterationCount(m_options.m_step_count)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653 result.AppendWarning(
654 "step operation does not support iteration count.");
655 }
656 }
657
658 process->GetThreadList().SetSelectedThreadByID(thread->GetID());
659
660 const uint32_t iohandler_id = process->GetIOHandlerID();
661
662 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +0000663 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000664 if (synchronous_execution)
665 error = process->ResumeSynchronous(&stream);
666 else
667 error = process->Resume();
668
669 // There is a race condition where this thread will return up the call
670 // stack to the main command handler
671 // and show an (lldb) prompt before HandlePrivateEvent (from
672 // PrivateStateThread) has
673 // a chance to call PushProcessIOHandler().
674 process->SyncIOHandler(iohandler_id, 2000);
675
676 if (synchronous_execution) {
677 // If any state changed events had anything to say, add that to the
678 // result
Zachary Turnerc1564272016-11-16 21:15:24 +0000679 if (stream.GetSize() > 0)
680 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681
682 process->GetThreadList().SetSelectedThreadByID(thread->GetID());
683 result.SetDidChangeProcessState(true);
684 result.SetStatus(eReturnStatusSuccessFinishNoResult);
685 } else {
686 result.SetStatus(eReturnStatusSuccessContinuingNoResult);
687 }
688 } else {
689 result.AppendError("Couldn't find thread plan to implement step type.");
690 result.SetStatus(eReturnStatusFailed);
691 }
692 return result.Succeeded();
693 }
694
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696 StepType m_step_type;
697 StepScope m_step_scope;
698 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699};
700
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701//-------------------------------------------------------------------------
702// CommandObjectThreadContinue
703//-------------------------------------------------------------------------
704
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705class CommandObjectThreadContinue : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707 CommandObjectThreadContinue(CommandInterpreter &interpreter)
708 : CommandObjectParsed(
709 interpreter, "thread continue",
710 "Continue execution of the current target process. One "
711 "or more threads may be specified, by default all "
712 "threads continue.",
713 nullptr,
714 eCommandRequiresThread | eCommandTryTargetAPILock |
715 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {
716 CommandArgumentEntry arg;
717 CommandArgumentData thread_idx_arg;
718
719 // Define the first (and only) variant of this arg.
720 thread_idx_arg.arg_type = eArgTypeThreadIndex;
721 thread_idx_arg.arg_repetition = eArgRepeatPlus;
722
723 // There is only one variant this argument could be; put it into the
724 // argument entry.
725 arg.push_back(thread_idx_arg);
726
727 // Push the data for the first argument into the m_arguments vector.
728 m_arguments.push_back(arg);
729 }
730
731 ~CommandObjectThreadContinue() override = default;
732
733 bool DoExecute(Args &command, CommandReturnObject &result) override {
734 bool synchronous_execution = m_interpreter.GetSynchronous();
735
736 if (!m_interpreter.GetDebugger().GetSelectedTarget()) {
737 result.AppendError("invalid target, create a debug target using the "
738 "'target create' command");
739 result.SetStatus(eReturnStatusFailed);
740 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741 }
742
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743 Process *process = m_exe_ctx.GetProcessPtr();
744 if (process == nullptr) {
745 result.AppendError("no process exists. Cannot continue");
746 result.SetStatus(eReturnStatusFailed);
747 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749
750 StateType state = process->GetState();
751 if ((state == eStateCrashed) || (state == eStateStopped) ||
752 (state == eStateSuspended)) {
753 const size_t argc = command.GetArgumentCount();
754 if (argc > 0) {
755 // These two lines appear at the beginning of both blocks in
756 // this if..else, but that is because we need to release the
757 // lock before calling process->Resume below.
758 std::lock_guard<std::recursive_mutex> guard(
759 process->GetThreadList().GetMutex());
760 const uint32_t num_threads = process->GetThreadList().GetSize();
761 std::vector<Thread *> resume_threads;
Zachary Turner97d2c402016-10-05 23:40:23 +0000762 for (auto &entry : command.entries()) {
763 uint32_t thread_idx;
764 if (entry.ref.getAsInteger(0, thread_idx)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765 result.AppendErrorWithFormat(
Zachary Turner97d2c402016-10-05 23:40:23 +0000766 "invalid thread index argument: \"%s\".\n", entry.c_str());
767 result.SetStatus(eReturnStatusFailed);
768 return false;
769 }
770 Thread *thread =
771 process->GetThreadList().FindThreadByIndexID(thread_idx).get();
772
773 if (thread) {
774 resume_threads.push_back(thread);
775 } else {
776 result.AppendErrorWithFormat("invalid thread index %u.\n",
777 thread_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 result.SetStatus(eReturnStatusFailed);
779 return false;
780 }
781 }
782
783 if (resume_threads.empty()) {
784 result.AppendError("no valid thread indexes were specified");
785 result.SetStatus(eReturnStatusFailed);
786 return false;
787 } else {
788 if (resume_threads.size() == 1)
789 result.AppendMessageWithFormat("Resuming thread: ");
790 else
791 result.AppendMessageWithFormat("Resuming threads: ");
792
793 for (uint32_t idx = 0; idx < num_threads; ++idx) {
794 Thread *thread =
795 process->GetThreadList().GetThreadAtIndex(idx).get();
796 std::vector<Thread *>::iterator this_thread_pos =
797 find(resume_threads.begin(), resume_threads.end(), thread);
798
799 if (this_thread_pos != resume_threads.end()) {
800 resume_threads.erase(this_thread_pos);
801 if (!resume_threads.empty())
802 result.AppendMessageWithFormat("%u, ", thread->GetIndexID());
803 else
804 result.AppendMessageWithFormat("%u ", thread->GetIndexID());
805
806 const bool override_suspend = true;
807 thread->SetResumeState(eStateRunning, override_suspend);
808 } else {
809 thread->SetResumeState(eStateSuspended);
810 }
811 }
812 result.AppendMessageWithFormat("in process %" PRIu64 "\n",
813 process->GetID());
814 }
815 } else {
816 // These two lines appear at the beginning of both blocks in
817 // this if..else, but that is because we need to release the
818 // lock before calling process->Resume below.
819 std::lock_guard<std::recursive_mutex> guard(
820 process->GetThreadList().GetMutex());
821 const uint32_t num_threads = process->GetThreadList().GetSize();
822 Thread *current_thread = GetDefaultThread();
823 if (current_thread == nullptr) {
824 result.AppendError("the process doesn't have a current thread");
825 result.SetStatus(eReturnStatusFailed);
826 return false;
827 }
828 // Set the actions that the threads should each take when resuming
829 for (uint32_t idx = 0; idx < num_threads; ++idx) {
830 Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get();
831 if (thread == current_thread) {
832 result.AppendMessageWithFormat("Resuming thread 0x%4.4" PRIx64
833 " in process %" PRIu64 "\n",
834 thread->GetID(), process->GetID());
835 const bool override_suspend = true;
836 thread->SetResumeState(eStateRunning, override_suspend);
837 } else {
838 thread->SetResumeState(eStateSuspended);
839 }
840 }
841 }
842
843 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +0000844 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000845 if (synchronous_execution)
846 error = process->ResumeSynchronous(&stream);
847 else
848 error = process->Resume();
849
850 // We should not be holding the thread list lock when we do this.
851 if (error.Success()) {
852 result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
853 process->GetID());
854 if (synchronous_execution) {
855 // If any state changed events had anything to say, add that to the
856 // result
Zachary Turnerc1564272016-11-16 21:15:24 +0000857 if (stream.GetSize() > 0)
858 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859
860 result.SetDidChangeProcessState(true);
861 result.SetStatus(eReturnStatusSuccessFinishNoResult);
862 } else {
863 result.SetStatus(eReturnStatusSuccessContinuingNoResult);
864 }
865 } else {
866 result.AppendErrorWithFormat("Failed to resume process: %s\n",
867 error.AsCString());
868 result.SetStatus(eReturnStatusFailed);
869 }
870 } else {
871 result.AppendErrorWithFormat(
872 "Process cannot be continued from its current state (%s).\n",
873 StateAsCString(state));
874 result.SetStatus(eReturnStatusFailed);
875 }
876
877 return result.Succeeded();
878 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879};
880
881//-------------------------------------------------------------------------
882// CommandObjectThreadUntil
883//-------------------------------------------------------------------------
884
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000885static OptionDefinition g_thread_until_options[] = {
886 // clang-format off
887 { LLDB_OPT_SET_1, false, "frame", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFrameIndex, "Frame index for until operation - defaults to 0" },
888 { LLDB_OPT_SET_1, false, "thread", 't', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadIndex, "Thread index for the thread for until operation" },
889 { LLDB_OPT_SET_1, false, "run-mode",'m', OptionParser::eRequiredArgument, nullptr, g_duo_running_mode, 0, eArgTypeRunMode, "Determine how to run other threads while stepping this one" },
890 { LLDB_OPT_SET_1, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Run until we reach the specified address, or leave the function - can be specified multiple times." }
891 // clang-format on
892};
893
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894class CommandObjectThreadUntil : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896 class CommandOptions : public Options {
897 public:
898 uint32_t m_thread_idx;
899 uint32_t m_frame_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901 CommandOptions()
902 : Options(), m_thread_idx(LLDB_INVALID_THREAD_ID),
903 m_frame_idx(LLDB_INVALID_FRAME_ID) {
904 // Keep default values of all options in one place: OptionParsingStarting
905 // ()
906 OptionParsingStarting(nullptr);
907 }
908
909 ~CommandOptions() override = default;
910
Zachary Turner97206d52017-05-12 04:51:55 +0000911 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
912 ExecutionContext *execution_context) override {
913 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000914 const int short_option = m_getopt_table[option_idx].val;
915
916 switch (short_option) {
917 case 'a': {
918 lldb::addr_t tmp_addr = Args::StringToAddress(
919 execution_context, option_arg, LLDB_INVALID_ADDRESS, &error);
920 if (error.Success())
921 m_until_addrs.push_back(tmp_addr);
922 } break;
923 case 't':
Zachary Turnerfe114832016-11-12 16:56:47 +0000924 if (option_arg.getAsInteger(0, m_thread_idx)) {
925 m_thread_idx = LLDB_INVALID_INDEX32;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926 error.SetErrorStringWithFormat("invalid thread index '%s'",
Zachary Turnerfe114832016-11-12 16:56:47 +0000927 option_arg.str().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929 break;
930 case 'f':
Zachary Turnerfe114832016-11-12 16:56:47 +0000931 if (option_arg.getAsInteger(0, m_frame_idx)) {
932 m_frame_idx = LLDB_INVALID_FRAME_ID;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933 error.SetErrorStringWithFormat("invalid frame index '%s'",
Zachary Turnerfe114832016-11-12 16:56:47 +0000934 option_arg.str().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000935 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936 break;
937 case 'm': {
938 OptionEnumValueElement *enum_values =
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000939 GetDefinitions()[option_idx].enum_values;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000940 lldb::RunMode run_mode = (lldb::RunMode)Args::StringToOptionEnum(
Zachary Turnerfe114832016-11-12 16:56:47 +0000941 option_arg, enum_values, eOnlyDuringStepping, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 if (error.Success()) {
944 if (run_mode == eAllThreads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000945 m_stop_others = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946 else
947 m_stop_others = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000948 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949 } break;
950 default:
951 error.SetErrorStringWithFormat("invalid short option character '%c'",
952 short_option);
953 break;
954 }
955 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956 }
957
Kate Stoneb9c1b512016-09-06 20:57:50 +0000958 void OptionParsingStarting(ExecutionContext *execution_context) override {
959 m_thread_idx = LLDB_INVALID_THREAD_ID;
960 m_frame_idx = 0;
961 m_stop_others = false;
962 m_until_addrs.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963 }
964
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000965 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000966 return llvm::makeArrayRef(g_thread_until_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000967 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968
969 uint32_t m_step_thread_idx;
970 bool m_stop_others;
971 std::vector<lldb::addr_t> m_until_addrs;
972
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973 // Instance variables to hold the values for command options.
974 };
975
976 CommandObjectThreadUntil(CommandInterpreter &interpreter)
977 : CommandObjectParsed(
978 interpreter, "thread until",
979 "Continue until a line number or address is reached by the "
980 "current or specified thread. Stops when returning from "
Jim Ingham9ac82602016-11-18 22:06:10 +0000981 "the current function as a safety measure. "
982 "The target line number(s) are given as arguments, and if more than one"
Adrian Kuegelecd760c2016-11-24 10:01:34 +0000983 " is provided, stepping will stop when the first one is hit.",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984 nullptr,
985 eCommandRequiresThread | eCommandTryTargetAPILock |
986 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
987 m_options() {
988 CommandArgumentEntry arg;
989 CommandArgumentData line_num_arg;
990
991 // Define the first (and only) variant of this arg.
992 line_num_arg.arg_type = eArgTypeLineNum;
993 line_num_arg.arg_repetition = eArgRepeatPlain;
994
995 // There is only one variant this argument could be; put it into the
996 // argument entry.
997 arg.push_back(line_num_arg);
998
999 // Push the data for the first argument into the m_arguments vector.
1000 m_arguments.push_back(arg);
1001 }
1002
1003 ~CommandObjectThreadUntil() override = default;
1004
1005 Options *GetOptions() override { return &m_options; }
1006
Jim Ingham5a988412012-06-08 21:56:10 +00001007protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 bool DoExecute(Args &command, CommandReturnObject &result) override {
1009 bool synchronous_execution = m_interpreter.GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010
Kate Stoneb9c1b512016-09-06 20:57:50 +00001011 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1012 if (target == nullptr) {
1013 result.AppendError("invalid target, create a debug target using the "
1014 "'target create' command");
1015 result.SetStatus(eReturnStatusFailed);
1016 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017 }
Jim Ingham5a988412012-06-08 21:56:10 +00001018
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019 Process *process = m_exe_ctx.GetProcessPtr();
1020 if (process == nullptr) {
1021 result.AppendError("need a valid process to step");
1022 result.SetStatus(eReturnStatusFailed);
1023 } else {
1024 Thread *thread = nullptr;
1025 std::vector<uint32_t> line_numbers;
1026
1027 if (command.GetArgumentCount() >= 1) {
1028 size_t num_args = command.GetArgumentCount();
1029 for (size_t i = 0; i < num_args; i++) {
1030 uint32_t line_number;
Jim Ingham9ac82602016-11-18 22:06:10 +00001031 line_number = StringConvert::ToUInt32(command.GetArgumentAtIndex(i),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032 UINT32_MAX);
1033 if (line_number == UINT32_MAX) {
1034 result.AppendErrorWithFormat("invalid line number: '%s'.\n",
Jim Ingham9ac82602016-11-18 22:06:10 +00001035 command.GetArgumentAtIndex(i));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 result.SetStatus(eReturnStatusFailed);
1037 return false;
1038 } else
1039 line_numbers.push_back(line_number);
1040 }
1041 } else if (m_options.m_until_addrs.empty()) {
1042 result.AppendErrorWithFormat("No line number or address provided:\n%s",
Zachary Turner1e8016b2016-11-15 00:45:18 +00001043 GetSyntax().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044 result.SetStatus(eReturnStatusFailed);
1045 return false;
1046 }
1047
1048 if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID) {
1049 thread = GetDefaultThread();
1050 } else {
1051 thread = process->GetThreadList()
1052 .FindThreadByIndexID(m_options.m_thread_idx)
1053 .get();
1054 }
1055
1056 if (thread == nullptr) {
1057 const uint32_t num_threads = process->GetThreadList().GetSize();
1058 result.AppendErrorWithFormat(
1059 "Thread index %u is out of range (valid values are 0 - %u).\n",
1060 m_options.m_thread_idx, num_threads);
1061 result.SetStatus(eReturnStatusFailed);
1062 return false;
1063 }
1064
1065 const bool abort_other_plans = false;
1066
1067 StackFrame *frame =
1068 thread->GetStackFrameAtIndex(m_options.m_frame_idx).get();
1069 if (frame == nullptr) {
1070 result.AppendErrorWithFormat(
1071 "Frame index %u is out of range for thread %u.\n",
1072 m_options.m_frame_idx, m_options.m_thread_idx);
1073 result.SetStatus(eReturnStatusFailed);
1074 return false;
1075 }
1076
1077 ThreadPlanSP new_plan_sp;
1078
1079 if (frame->HasDebugInformation()) {
1080 // Finally we got here... Translate the given line number to a bunch of
1081 // addresses:
1082 SymbolContext sc(frame->GetSymbolContext(eSymbolContextCompUnit));
1083 LineTable *line_table = nullptr;
1084 if (sc.comp_unit)
1085 line_table = sc.comp_unit->GetLineTable();
1086
1087 if (line_table == nullptr) {
1088 result.AppendErrorWithFormat("Failed to resolve the line table for "
1089 "frame %u of thread index %u.\n",
1090 m_options.m_frame_idx,
1091 m_options.m_thread_idx);
1092 result.SetStatus(eReturnStatusFailed);
1093 return false;
1094 }
1095
1096 LineEntry function_start;
1097 uint32_t index_ptr = 0, end_ptr;
1098 std::vector<addr_t> address_list;
1099
1100 // Find the beginning & end index of the
1101 AddressRange fun_addr_range = sc.function->GetAddressRange();
1102 Address fun_start_addr = fun_addr_range.GetBaseAddress();
1103 line_table->FindLineEntryByAddress(fun_start_addr, function_start,
1104 &index_ptr);
1105
1106 Address fun_end_addr(fun_start_addr.GetSection(),
1107 fun_start_addr.GetOffset() +
1108 fun_addr_range.GetByteSize());
1109
1110 bool all_in_function = true;
1111
1112 line_table->FindLineEntryByAddress(fun_end_addr, function_start,
1113 &end_ptr);
1114
1115 for (uint32_t line_number : line_numbers) {
1116 uint32_t start_idx_ptr = index_ptr;
1117 while (start_idx_ptr <= end_ptr) {
1118 LineEntry line_entry;
1119 const bool exact = false;
1120 start_idx_ptr = sc.comp_unit->FindLineEntry(
1121 start_idx_ptr, line_number, sc.comp_unit, exact, &line_entry);
1122 if (start_idx_ptr == UINT32_MAX)
1123 break;
1124
1125 addr_t address =
1126 line_entry.range.GetBaseAddress().GetLoadAddress(target);
1127 if (address != LLDB_INVALID_ADDRESS) {
1128 if (fun_addr_range.ContainsLoadAddress(address, target))
1129 address_list.push_back(address);
1130 else
1131 all_in_function = false;
1132 }
1133 start_idx_ptr++;
1134 }
1135 }
1136
1137 for (lldb::addr_t address : m_options.m_until_addrs) {
1138 if (fun_addr_range.ContainsLoadAddress(address, target))
1139 address_list.push_back(address);
1140 else
1141 all_in_function = false;
1142 }
1143
1144 if (address_list.empty()) {
1145 if (all_in_function)
1146 result.AppendErrorWithFormat(
1147 "No line entries matching until target.\n");
1148 else
1149 result.AppendErrorWithFormat(
1150 "Until target outside of the current function.\n");
1151
1152 result.SetStatus(eReturnStatusFailed);
1153 return false;
1154 }
1155
1156 new_plan_sp = thread->QueueThreadPlanForStepUntil(
1157 abort_other_plans, &address_list.front(), address_list.size(),
1158 m_options.m_stop_others, m_options.m_frame_idx);
1159 // User level plans should be master plans so they can be interrupted
1160 // (e.g. by hitting a breakpoint)
1161 // and other plans executed by the user (stepping around the breakpoint)
1162 // and then a "continue"
1163 // will resume the original plan.
1164 new_plan_sp->SetIsMasterPlan(true);
1165 new_plan_sp->SetOkayToDiscard(false);
1166 } else {
1167 result.AppendErrorWithFormat(
1168 "Frame index %u of thread %u has no debug information.\n",
1169 m_options.m_frame_idx, m_options.m_thread_idx);
1170 result.SetStatus(eReturnStatusFailed);
1171 return false;
1172 }
1173
1174 process->GetThreadList().SetSelectedThreadByID(m_options.m_thread_idx);
1175
1176 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +00001177 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001178 if (synchronous_execution)
1179 error = process->ResumeSynchronous(&stream);
1180 else
1181 error = process->Resume();
1182
1183 if (error.Success()) {
1184 result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
1185 process->GetID());
1186 if (synchronous_execution) {
1187 // If any state changed events had anything to say, add that to the
1188 // result
Zachary Turnerc1564272016-11-16 21:15:24 +00001189 if (stream.GetSize() > 0)
1190 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001191
1192 result.SetDidChangeProcessState(true);
1193 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1194 } else {
1195 result.SetStatus(eReturnStatusSuccessContinuingNoResult);
1196 }
1197 } else {
1198 result.AppendErrorWithFormat("Failed to resume process: %s.\n",
1199 error.AsCString());
1200 result.SetStatus(eReturnStatusFailed);
1201 }
1202 }
1203 return result.Succeeded();
1204 }
1205
1206 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001207};
1208
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209//-------------------------------------------------------------------------
1210// CommandObjectThreadSelect
1211//-------------------------------------------------------------------------
1212
Kate Stoneb9c1b512016-09-06 20:57:50 +00001213class CommandObjectThreadSelect : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215 CommandObjectThreadSelect(CommandInterpreter &interpreter)
1216 : CommandObjectParsed(interpreter, "thread select",
1217 "Change the currently selected thread.", nullptr,
1218 eCommandRequiresProcess | eCommandTryTargetAPILock |
1219 eCommandProcessMustBeLaunched |
1220 eCommandProcessMustBePaused) {
1221 CommandArgumentEntry arg;
1222 CommandArgumentData thread_idx_arg;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001223
Kate Stoneb9c1b512016-09-06 20:57:50 +00001224 // Define the first (and only) variant of this arg.
1225 thread_idx_arg.arg_type = eArgTypeThreadIndex;
1226 thread_idx_arg.arg_repetition = eArgRepeatPlain;
1227
1228 // There is only one variant this argument could be; put it into the
1229 // argument entry.
1230 arg.push_back(thread_idx_arg);
1231
1232 // Push the data for the first argument into the m_arguments vector.
1233 m_arguments.push_back(arg);
1234 }
1235
1236 ~CommandObjectThreadSelect() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237
Jim Ingham5a988412012-06-08 21:56:10 +00001238protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239 bool DoExecute(Args &command, CommandReturnObject &result) override {
1240 Process *process = m_exe_ctx.GetProcessPtr();
1241 if (process == nullptr) {
1242 result.AppendError("no process");
1243 result.SetStatus(eReturnStatusFailed);
1244 return false;
1245 } else if (command.GetArgumentCount() != 1) {
1246 result.AppendErrorWithFormat(
1247 "'%s' takes exactly one thread index argument:\nUsage: %s\n",
1248 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1249 result.SetStatus(eReturnStatusFailed);
1250 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252
1253 uint32_t index_id =
1254 StringConvert::ToUInt32(command.GetArgumentAtIndex(0), 0, 0);
1255
1256 Thread *new_thread =
1257 process->GetThreadList().FindThreadByIndexID(index_id).get();
1258 if (new_thread == nullptr) {
1259 result.AppendErrorWithFormat("invalid thread #%s.\n",
1260 command.GetArgumentAtIndex(0));
1261 result.SetStatus(eReturnStatusFailed);
1262 return false;
1263 }
1264
1265 process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true);
1266 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1267
1268 return result.Succeeded();
1269 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270};
1271
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001272//-------------------------------------------------------------------------
1273// CommandObjectThreadList
1274//-------------------------------------------------------------------------
1275
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276class CommandObjectThreadList : public CommandObjectParsed {
Greg Clayton66111032010-06-23 01:19:29 +00001277public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001278 CommandObjectThreadList(CommandInterpreter &interpreter)
1279 : CommandObjectParsed(
1280 interpreter, "thread list",
1281 "Show a summary of each thread in the current target process.",
1282 "thread list",
1283 eCommandRequiresProcess | eCommandTryTargetAPILock |
1284 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001285
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286 ~CommandObjectThreadList() override = default;
Greg Clayton66111032010-06-23 01:19:29 +00001287
Jim Ingham5a988412012-06-08 21:56:10 +00001288protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001289 bool DoExecute(Args &command, CommandReturnObject &result) override {
1290 Stream &strm = result.GetOutputStream();
1291 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1292 Process *process = m_exe_ctx.GetProcessPtr();
1293 const bool only_threads_with_stop_reason = false;
1294 const uint32_t start_frame = 0;
1295 const uint32_t num_frames = 0;
1296 const uint32_t num_frames_with_source = 0;
1297 process->GetStatus(strm);
1298 process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame,
Jim Ingham6a9767c2016-11-08 20:36:40 +00001299 num_frames, num_frames_with_source, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001300 return result.Succeeded();
1301 }
Greg Clayton66111032010-06-23 01:19:29 +00001302};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303
Jim Ingham93208b82013-01-31 21:46:01 +00001304//-------------------------------------------------------------------------
Jason Molenda705b1802014-06-13 02:37:02 +00001305// CommandObjectThreadInfo
1306//-------------------------------------------------------------------------
1307
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001308static OptionDefinition g_thread_info_options[] = {
1309 // clang-format off
1310 { LLDB_OPT_SET_ALL, false, "json", 'j', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the thread info in JSON format." },
1311 { LLDB_OPT_SET_ALL, false, "stop-info", 's', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the extended stop info in JSON format." }
1312 // clang-format on
1313};
1314
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315class CommandObjectThreadInfo : public CommandObjectIterateOverThreads {
Jason Molenda705b1802014-06-13 02:37:02 +00001316public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317 class CommandOptions : public Options {
1318 public:
1319 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
Jason Molenda705b1802014-06-13 02:37:02 +00001320
Kate Stoneb9c1b512016-09-06 20:57:50 +00001321 ~CommandOptions() override = default;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001322
Kate Stoneb9c1b512016-09-06 20:57:50 +00001323 void OptionParsingStarting(ExecutionContext *execution_context) override {
1324 m_json_thread = false;
1325 m_json_stopinfo = false;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001326 }
1327
Zachary Turner97206d52017-05-12 04:51:55 +00001328 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1329 ExecutionContext *execution_context) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330 const int short_option = m_getopt_table[option_idx].val;
Zachary Turner97206d52017-05-12 04:51:55 +00001331 Status error;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001332
Kate Stoneb9c1b512016-09-06 20:57:50 +00001333 switch (short_option) {
1334 case 'j':
1335 m_json_thread = true;
1336 break;
1337
1338 case 's':
1339 m_json_stopinfo = true;
1340 break;
1341
1342 default:
Zachary Turner97206d52017-05-12 04:51:55 +00001343 return Status("invalid short option character '%c'", short_option);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001344 }
1345 return error;
Jason Molenda705b1802014-06-13 02:37:02 +00001346 }
1347
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001348 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001349 return llvm::makeArrayRef(g_thread_info_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001350 }
Stephane Sezerf8104912016-03-17 18:52:41 +00001351
Kate Stoneb9c1b512016-09-06 20:57:50 +00001352 bool m_json_thread;
1353 bool m_json_stopinfo;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001354 };
1355
1356 CommandObjectThreadInfo(CommandInterpreter &interpreter)
1357 : CommandObjectIterateOverThreads(
1358 interpreter, "thread info", "Show an extended summary of one or "
1359 "more threads. Defaults to the "
1360 "current thread.",
1361 "thread info",
1362 eCommandRequiresProcess | eCommandTryTargetAPILock |
1363 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
1364 m_options() {
1365 m_add_return = false;
1366 }
1367
1368 ~CommandObjectThreadInfo() override = default;
1369
1370 Options *GetOptions() override { return &m_options; }
1371
1372 bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override {
1373 ThreadSP thread_sp =
1374 m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
1375 if (!thread_sp) {
1376 result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n",
1377 tid);
1378 result.SetStatus(eReturnStatusFailed);
1379 return false;
Jason Molenda705b1802014-06-13 02:37:02 +00001380 }
1381
Kate Stoneb9c1b512016-09-06 20:57:50 +00001382 Thread *thread = thread_sp.get();
1383
1384 Stream &strm = result.GetOutputStream();
1385 if (!thread->GetDescription(strm, eDescriptionLevelFull,
1386 m_options.m_json_thread,
1387 m_options.m_json_stopinfo)) {
1388 result.AppendErrorWithFormat("error displaying info for thread: \"%d\"\n",
1389 thread->GetIndexID());
1390 result.SetStatus(eReturnStatusFailed);
1391 return false;
1392 }
1393 return true;
1394 }
1395
1396 CommandOptions m_options;
Jason Molenda705b1802014-06-13 02:37:02 +00001397};
1398
Jason Molenda705b1802014-06-13 02:37:02 +00001399//-------------------------------------------------------------------------
Jim Ingham93208b82013-01-31 21:46:01 +00001400// CommandObjectThreadReturn
1401//-------------------------------------------------------------------------
1402
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001403static OptionDefinition g_thread_return_options[] = {
1404 // clang-format off
1405 { LLDB_OPT_SET_ALL, false, "from-expression", 'x', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Return from the innermost expression evaluation." }
1406 // clang-format on
1407};
1408
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409class CommandObjectThreadReturn : public CommandObjectRaw {
Jim Inghamcb640dd2012-09-14 02:14:15 +00001410public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001411 class CommandOptions : public Options {
1412 public:
1413 CommandOptions() : Options(), m_from_expression(false) {
1414 // Keep default values of all options in one place: OptionParsingStarting
1415 // ()
1416 OptionParsingStarting(nullptr);
Jim Inghamcb640dd2012-09-14 02:14:15 +00001417 }
Jim Inghamcb640dd2012-09-14 02:14:15 +00001418
Kate Stoneb9c1b512016-09-06 20:57:50 +00001419 ~CommandOptions() override = default;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001420
Zachary Turner97206d52017-05-12 04:51:55 +00001421 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1422 ExecutionContext *execution_context) override {
1423 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424 const int short_option = m_getopt_table[option_idx].val;
1425
1426 switch (short_option) {
1427 case 'x': {
1428 bool success;
Zachary Turnerfe114832016-11-12 16:56:47 +00001429 bool tmp_value = Args::StringToBoolean(option_arg, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001430 if (success)
1431 m_from_expression = tmp_value;
1432 else {
1433 error.SetErrorStringWithFormat(
Zachary Turnerfe114832016-11-12 16:56:47 +00001434 "invalid boolean value '%s' for 'x' option",
1435 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436 }
1437 } break;
1438 default:
1439 error.SetErrorStringWithFormat("invalid short option character '%c'",
1440 short_option);
1441 break;
1442 }
1443 return error;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001444 }
1445
Kate Stoneb9c1b512016-09-06 20:57:50 +00001446 void OptionParsingStarting(ExecutionContext *execution_context) override {
1447 m_from_expression = false;
1448 }
1449
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001450 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001451 return llvm::makeArrayRef(g_thread_return_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001452 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001453
1454 bool m_from_expression;
1455
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456 // Instance variables to hold the values for command options.
1457 };
1458
1459 CommandObjectThreadReturn(CommandInterpreter &interpreter)
1460 : CommandObjectRaw(interpreter, "thread return",
1461 "Prematurely return from a stack frame, "
1462 "short-circuiting execution of newer frames "
1463 "and optionally yielding a specified value. Defaults "
1464 "to the exiting the current stack "
1465 "frame.",
1466 "thread return",
1467 eCommandRequiresFrame | eCommandTryTargetAPILock |
1468 eCommandProcessMustBeLaunched |
1469 eCommandProcessMustBePaused),
1470 m_options() {
1471 CommandArgumentEntry arg;
1472 CommandArgumentData expression_arg;
1473
1474 // Define the first (and only) variant of this arg.
1475 expression_arg.arg_type = eArgTypeExpression;
1476 expression_arg.arg_repetition = eArgRepeatOptional;
1477
1478 // There is only one variant this argument could be; put it into the
1479 // argument entry.
1480 arg.push_back(expression_arg);
1481
1482 // Push the data for the first argument into the m_arguments vector.
1483 m_arguments.push_back(arg);
1484 }
1485
1486 ~CommandObjectThreadReturn() override = default;
1487
1488 Options *GetOptions() override { return &m_options; }
1489
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001490protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 bool DoExecute(const char *command, CommandReturnObject &result) override {
1492 // I am going to handle this by hand, because I don't want you to have to
1493 // say:
1494 // "thread return -- -5".
1495 if (command[0] == '-' && command[1] == 'x') {
1496 if (command && command[2] != '\0')
1497 result.AppendWarning("Return values ignored when returning from user "
1498 "called expressions");
Jim Inghamcb640dd2012-09-14 02:14:15 +00001499
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500 Thread *thread = m_exe_ctx.GetThreadPtr();
Zachary Turner97206d52017-05-12 04:51:55 +00001501 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001502 error = thread->UnwindInnermostExpression();
1503 if (!error.Success()) {
1504 result.AppendErrorWithFormat("Unwinding expression failed - %s.",
1505 error.AsCString());
1506 result.SetStatus(eReturnStatusFailed);
1507 } else {
1508 bool success =
1509 thread->SetSelectedFrameByIndexNoisily(0, result.GetOutputStream());
1510 if (success) {
1511 m_exe_ctx.SetFrameSP(thread->GetSelectedFrame());
1512 result.SetStatus(eReturnStatusSuccessFinishResult);
1513 } else {
1514 result.AppendErrorWithFormat(
1515 "Could not select 0th frame after unwinding expression.");
1516 result.SetStatus(eReturnStatusFailed);
Jim Inghamcb640dd2012-09-14 02:14:15 +00001517 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001518 }
1519 return result.Succeeded();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001520 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001521
1522 ValueObjectSP return_valobj_sp;
1523
1524 StackFrameSP frame_sp = m_exe_ctx.GetFrameSP();
1525 uint32_t frame_idx = frame_sp->GetFrameIndex();
1526
1527 if (frame_sp->IsInlined()) {
1528 result.AppendError("Don't know how to return from inlined frames.");
1529 result.SetStatus(eReturnStatusFailed);
1530 return false;
1531 }
1532
1533 if (command && command[0] != '\0') {
1534 Target *target = m_exe_ctx.GetTargetPtr();
1535 EvaluateExpressionOptions options;
1536
1537 options.SetUnwindOnError(true);
1538 options.SetUseDynamic(eNoDynamicValues);
1539
1540 ExpressionResults exe_results = eExpressionSetupError;
1541 exe_results = target->EvaluateExpression(command, frame_sp.get(),
1542 return_valobj_sp, options);
1543 if (exe_results != eExpressionCompleted) {
1544 if (return_valobj_sp)
1545 result.AppendErrorWithFormat(
1546 "Error evaluating result expression: %s",
1547 return_valobj_sp->GetError().AsCString());
1548 else
1549 result.AppendErrorWithFormat(
1550 "Unknown error evaluating result expression.");
1551 result.SetStatus(eReturnStatusFailed);
1552 return false;
1553 }
1554 }
1555
Zachary Turner97206d52017-05-12 04:51:55 +00001556 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001557 ThreadSP thread_sp = m_exe_ctx.GetThreadSP();
1558 const bool broadcast = true;
1559 error = thread_sp->ReturnFromFrame(frame_sp, return_valobj_sp, broadcast);
1560 if (!error.Success()) {
1561 result.AppendErrorWithFormat(
1562 "Error returning from frame %d of thread %d: %s.", frame_idx,
1563 thread_sp->GetIndexID(), error.AsCString());
1564 result.SetStatus(eReturnStatusFailed);
1565 return false;
1566 }
1567
1568 result.SetStatus(eReturnStatusSuccessFinishResult);
1569 return true;
1570 }
1571
1572 CommandOptions m_options;
Jim Inghamcb640dd2012-09-14 02:14:15 +00001573};
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001574
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001575//-------------------------------------------------------------------------
Richard Mittonf86248d2013-09-12 02:20:34 +00001576// CommandObjectThreadJump
1577//-------------------------------------------------------------------------
1578
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001579static OptionDefinition g_thread_jump_options[] = {
1580 // clang-format off
1581 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "Specifies the source file to jump to." },
1582 { LLDB_OPT_SET_1, true, "line", 'l', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLineNum, "Specifies the line number to jump to." },
1583 { LLDB_OPT_SET_2, true, "by", 'b', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeOffset, "Jumps by a relative line offset from the current line." },
1584 { LLDB_OPT_SET_3, true, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Jumps to a specific address." },
1585 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3, false, "force", 'r', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Allows the PC to leave the current function." }
1586 // clang-format on
1587};
1588
Kate Stoneb9c1b512016-09-06 20:57:50 +00001589class CommandObjectThreadJump : public CommandObjectParsed {
Richard Mittonf86248d2013-09-12 02:20:34 +00001590public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591 class CommandOptions : public Options {
1592 public:
1593 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
Richard Mittonf86248d2013-09-12 02:20:34 +00001594
Kate Stoneb9c1b512016-09-06 20:57:50 +00001595 ~CommandOptions() override = default;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001596
Kate Stoneb9c1b512016-09-06 20:57:50 +00001597 void OptionParsingStarting(ExecutionContext *execution_context) override {
1598 m_filenames.Clear();
1599 m_line_num = 0;
1600 m_line_offset = 0;
1601 m_load_addr = LLDB_INVALID_ADDRESS;
1602 m_force = false;
Richard Mittonf86248d2013-09-12 02:20:34 +00001603 }
1604
Zachary Turner97206d52017-05-12 04:51:55 +00001605 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1606 ExecutionContext *execution_context) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001607 const int short_option = m_getopt_table[option_idx].val;
Zachary Turner97206d52017-05-12 04:51:55 +00001608 Status error;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001609
Kate Stoneb9c1b512016-09-06 20:57:50 +00001610 switch (short_option) {
1611 case 'f':
1612 m_filenames.AppendIfUnique(FileSpec(option_arg, false));
1613 if (m_filenames.GetSize() > 1)
Zachary Turner97206d52017-05-12 04:51:55 +00001614 return Status("only one source file expected.");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001615 break;
1616 case 'l':
Zachary Turnerfe114832016-11-12 16:56:47 +00001617 if (option_arg.getAsInteger(0, m_line_num))
Zachary Turner97206d52017-05-12 04:51:55 +00001618 return Status("invalid line number: '%s'.", option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619 break;
1620 case 'b':
Zachary Turnerfe114832016-11-12 16:56:47 +00001621 if (option_arg.getAsInteger(0, m_line_offset))
Zachary Turner97206d52017-05-12 04:51:55 +00001622 return Status("invalid line offset: '%s'.", option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001623 break;
1624 case 'a':
1625 m_load_addr = Args::StringToAddress(execution_context, option_arg,
1626 LLDB_INVALID_ADDRESS, &error);
1627 break;
1628 case 'r':
1629 m_force = true;
1630 break;
1631 default:
Zachary Turner97206d52017-05-12 04:51:55 +00001632 return Status("invalid short option character '%c'", short_option);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001633 }
1634 return error;
Richard Mittonf86248d2013-09-12 02:20:34 +00001635 }
1636
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001637 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001638 return llvm::makeArrayRef(g_thread_jump_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001639 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001640
1641 FileSpecList m_filenames;
1642 uint32_t m_line_num;
1643 int32_t m_line_offset;
1644 lldb::addr_t m_load_addr;
1645 bool m_force;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001646 };
1647
1648 CommandObjectThreadJump(CommandInterpreter &interpreter)
1649 : CommandObjectParsed(
1650 interpreter, "thread jump",
1651 "Sets the program counter to a new address.", "thread jump",
1652 eCommandRequiresFrame | eCommandTryTargetAPILock |
1653 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
1654 m_options() {}
1655
1656 ~CommandObjectThreadJump() override = default;
1657
1658 Options *GetOptions() override { return &m_options; }
1659
Richard Mittonf86248d2013-09-12 02:20:34 +00001660protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001661 bool DoExecute(Args &args, CommandReturnObject &result) override {
1662 RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext();
1663 StackFrame *frame = m_exe_ctx.GetFramePtr();
1664 Thread *thread = m_exe_ctx.GetThreadPtr();
1665 Target *target = m_exe_ctx.GetTargetPtr();
1666 const SymbolContext &sym_ctx =
1667 frame->GetSymbolContext(eSymbolContextLineEntry);
Richard Mittonf86248d2013-09-12 02:20:34 +00001668
Kate Stoneb9c1b512016-09-06 20:57:50 +00001669 if (m_options.m_load_addr != LLDB_INVALID_ADDRESS) {
1670 // Use this address directly.
1671 Address dest = Address(m_options.m_load_addr);
Richard Mittonf86248d2013-09-12 02:20:34 +00001672
Kate Stoneb9c1b512016-09-06 20:57:50 +00001673 lldb::addr_t callAddr = dest.GetCallableLoadAddress(target);
1674 if (callAddr == LLDB_INVALID_ADDRESS) {
1675 result.AppendErrorWithFormat("Invalid destination address.");
1676 result.SetStatus(eReturnStatusFailed);
1677 return false;
1678 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001679
Kate Stoneb9c1b512016-09-06 20:57:50 +00001680 if (!reg_ctx->SetPC(callAddr)) {
1681 result.AppendErrorWithFormat("Error changing PC value for thread %d.",
1682 thread->GetIndexID());
1683 result.SetStatus(eReturnStatusFailed);
1684 return false;
1685 }
1686 } else {
1687 // Pick either the absolute line, or work out a relative one.
1688 int32_t line = (int32_t)m_options.m_line_num;
1689 if (line == 0)
1690 line = sym_ctx.line_entry.line + m_options.m_line_offset;
Richard Mittonf86248d2013-09-12 02:20:34 +00001691
Kate Stoneb9c1b512016-09-06 20:57:50 +00001692 // Try the current file, but override if asked.
1693 FileSpec file = sym_ctx.line_entry.file;
1694 if (m_options.m_filenames.GetSize() == 1)
1695 file = m_options.m_filenames.GetFileSpecAtIndex(0);
Richard Mittonf86248d2013-09-12 02:20:34 +00001696
Kate Stoneb9c1b512016-09-06 20:57:50 +00001697 if (!file) {
1698 result.AppendErrorWithFormat(
1699 "No source file available for the current location.");
1700 result.SetStatus(eReturnStatusFailed);
1701 return false;
1702 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001703
Kate Stoneb9c1b512016-09-06 20:57:50 +00001704 std::string warnings;
Zachary Turner97206d52017-05-12 04:51:55 +00001705 Status err = thread->JumpToLine(file, line, m_options.m_force, &warnings);
Richard Mittonf86248d2013-09-12 02:20:34 +00001706
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707 if (err.Fail()) {
1708 result.SetError(err);
1709 return false;
1710 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001711
Kate Stoneb9c1b512016-09-06 20:57:50 +00001712 if (!warnings.empty())
1713 result.AppendWarning(warnings.c_str());
Richard Mittonf86248d2013-09-12 02:20:34 +00001714 }
1715
Kate Stoneb9c1b512016-09-06 20:57:50 +00001716 result.SetStatus(eReturnStatusSuccessFinishResult);
1717 return true;
1718 }
1719
1720 CommandOptions m_options;
Richard Mittonf86248d2013-09-12 02:20:34 +00001721};
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001722
Richard Mittonf86248d2013-09-12 02:20:34 +00001723//-------------------------------------------------------------------------
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001724// Next are the subcommands of CommandObjectMultiwordThreadPlan
1725//-------------------------------------------------------------------------
1726
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001727//-------------------------------------------------------------------------
1728// CommandObjectThreadPlanList
1729//-------------------------------------------------------------------------
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001730
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001731static OptionDefinition g_thread_plan_list_options[] = {
1732 // clang-format off
1733 { LLDB_OPT_SET_1, false, "verbose", 'v', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display more information about the thread plans" },
1734 { LLDB_OPT_SET_1, false, "internal", 'i', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display internal as well as user thread plans" }
1735 // clang-format on
1736};
1737
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738class CommandObjectThreadPlanList : public CommandObjectIterateOverThreads {
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001739public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001740 class CommandOptions : public Options {
1741 public:
1742 CommandOptions() : Options() {
1743 // Keep default values of all options in one place: OptionParsingStarting
1744 // ()
1745 OptionParsingStarting(nullptr);
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001746 }
1747
Kate Stoneb9c1b512016-09-06 20:57:50 +00001748 ~CommandOptions() override = default;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001749
Zachary Turner97206d52017-05-12 04:51:55 +00001750 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1751 ExecutionContext *execution_context) override {
1752 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001753 const int short_option = m_getopt_table[option_idx].val;
1754
1755 switch (short_option) {
1756 case 'i':
1757 m_internal = true;
1758 break;
1759 case 'v':
1760 m_verbose = true;
1761 break;
1762 default:
1763 error.SetErrorStringWithFormat("invalid short option character '%c'",
1764 short_option);
1765 break;
1766 }
1767 return error;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001768 }
1769
Kate Stoneb9c1b512016-09-06 20:57:50 +00001770 void OptionParsingStarting(ExecutionContext *execution_context) override {
1771 m_verbose = false;
1772 m_internal = false;
1773 }
1774
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001775 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001776 return llvm::makeArrayRef(g_thread_plan_list_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001777 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001778
1779 // Instance variables to hold the values for command options.
1780 bool m_verbose;
1781 bool m_internal;
1782 };
1783
1784 CommandObjectThreadPlanList(CommandInterpreter &interpreter)
1785 : CommandObjectIterateOverThreads(
1786 interpreter, "thread plan list",
1787 "Show thread plans for one or more threads. If no threads are "
1788 "specified, show the "
1789 "current thread. Use the thread-index \"all\" to see all threads.",
1790 nullptr,
1791 eCommandRequiresProcess | eCommandRequiresThread |
1792 eCommandTryTargetAPILock | eCommandProcessMustBeLaunched |
1793 eCommandProcessMustBePaused),
1794 m_options() {}
1795
1796 ~CommandObjectThreadPlanList() override = default;
1797
1798 Options *GetOptions() override { return &m_options; }
1799
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001800protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001801 bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override {
1802 ThreadSP thread_sp =
1803 m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
1804 if (!thread_sp) {
1805 result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n",
1806 tid);
1807 result.SetStatus(eReturnStatusFailed);
1808 return false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001809 }
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001810
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811 Thread *thread = thread_sp.get();
1812
1813 Stream &strm = result.GetOutputStream();
1814 DescriptionLevel desc_level = eDescriptionLevelFull;
1815 if (m_options.m_verbose)
1816 desc_level = eDescriptionLevelVerbose;
1817
1818 thread->DumpThreadPlans(&strm, desc_level, m_options.m_internal, true);
1819 return true;
1820 }
1821
1822 CommandOptions m_options;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001823};
1824
Kate Stoneb9c1b512016-09-06 20:57:50 +00001825class CommandObjectThreadPlanDiscard : public CommandObjectParsed {
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001826public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001827 CommandObjectThreadPlanDiscard(CommandInterpreter &interpreter)
1828 : CommandObjectParsed(interpreter, "thread plan discard",
1829 "Discards thread plans up to and including the "
1830 "specified index (see 'thread plan list'.) "
1831 "Only user visible plans can be discarded.",
1832 nullptr,
1833 eCommandRequiresProcess | eCommandRequiresThread |
1834 eCommandTryTargetAPILock |
1835 eCommandProcessMustBeLaunched |
1836 eCommandProcessMustBePaused) {
1837 CommandArgumentEntry arg;
1838 CommandArgumentData plan_index_arg;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001839
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840 // Define the first (and only) variant of this arg.
1841 plan_index_arg.arg_type = eArgTypeUnsignedInteger;
1842 plan_index_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001843
Kate Stoneb9c1b512016-09-06 20:57:50 +00001844 // There is only one variant this argument could be; put it into the
1845 // argument entry.
1846 arg.push_back(plan_index_arg);
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001847
Kate Stoneb9c1b512016-09-06 20:57:50 +00001848 // Push the data for the first argument into the m_arguments vector.
1849 m_arguments.push_back(arg);
1850 }
1851
1852 ~CommandObjectThreadPlanDiscard() override = default;
1853
1854 bool DoExecute(Args &args, CommandReturnObject &result) override {
1855 Thread *thread = m_exe_ctx.GetThreadPtr();
1856 if (args.GetArgumentCount() != 1) {
1857 result.AppendErrorWithFormat("Too many arguments, expected one - the "
1858 "thread plan index - but got %zu.",
1859 args.GetArgumentCount());
1860 result.SetStatus(eReturnStatusFailed);
1861 return false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001862 }
1863
Kate Stoneb9c1b512016-09-06 20:57:50 +00001864 bool success;
1865 uint32_t thread_plan_idx =
1866 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), 0, 0, &success);
1867 if (!success) {
1868 result.AppendErrorWithFormat(
1869 "Invalid thread index: \"%s\" - should be unsigned int.",
1870 args.GetArgumentAtIndex(0));
1871 result.SetStatus(eReturnStatusFailed);
1872 return false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001873 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001874
1875 if (thread_plan_idx == 0) {
1876 result.AppendErrorWithFormat(
1877 "You wouldn't really want me to discard the base thread plan.");
1878 result.SetStatus(eReturnStatusFailed);
1879 return false;
1880 }
1881
1882 if (thread->DiscardUserThreadPlansUpToIndex(thread_plan_idx)) {
1883 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1884 return true;
1885 } else {
1886 result.AppendErrorWithFormat(
1887 "Could not find User thread plan with index %s.",
1888 args.GetArgumentAtIndex(0));
1889 result.SetStatus(eReturnStatusFailed);
1890 return false;
1891 }
1892 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001893};
1894
1895//-------------------------------------------------------------------------
1896// CommandObjectMultiwordThreadPlan
1897//-------------------------------------------------------------------------
1898
Kate Stoneb9c1b512016-09-06 20:57:50 +00001899class CommandObjectMultiwordThreadPlan : public CommandObjectMultiword {
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001900public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001901 CommandObjectMultiwordThreadPlan(CommandInterpreter &interpreter)
1902 : CommandObjectMultiword(
1903 interpreter, "plan",
1904 "Commands for managing thread plans that control execution.",
1905 "thread plan <subcommand> [<subcommand objects]") {
1906 LoadSubCommand(
1907 "list", CommandObjectSP(new CommandObjectThreadPlanList(interpreter)));
1908 LoadSubCommand(
1909 "discard",
1910 CommandObjectSP(new CommandObjectThreadPlanDiscard(interpreter)));
1911 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001912
Kate Stoneb9c1b512016-09-06 20:57:50 +00001913 ~CommandObjectMultiwordThreadPlan() override = default;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001914};
1915
1916//-------------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001917// CommandObjectMultiwordThread
1918//-------------------------------------------------------------------------
1919
Kate Stoneb9c1b512016-09-06 20:57:50 +00001920CommandObjectMultiwordThread::CommandObjectMultiwordThread(
1921 CommandInterpreter &interpreter)
1922 : CommandObjectMultiword(interpreter, "thread", "Commands for operating on "
1923 "one or more threads in "
1924 "the current process.",
1925 "thread <subcommand> [<subcommand-options>]") {
1926 LoadSubCommand("backtrace", CommandObjectSP(new CommandObjectThreadBacktrace(
1927 interpreter)));
1928 LoadSubCommand("continue",
1929 CommandObjectSP(new CommandObjectThreadContinue(interpreter)));
1930 LoadSubCommand("list",
1931 CommandObjectSP(new CommandObjectThreadList(interpreter)));
1932 LoadSubCommand("return",
1933 CommandObjectSP(new CommandObjectThreadReturn(interpreter)));
1934 LoadSubCommand("jump",
1935 CommandObjectSP(new CommandObjectThreadJump(interpreter)));
1936 LoadSubCommand("select",
1937 CommandObjectSP(new CommandObjectThreadSelect(interpreter)));
1938 LoadSubCommand("until",
1939 CommandObjectSP(new CommandObjectThreadUntil(interpreter)));
1940 LoadSubCommand("info",
1941 CommandObjectSP(new CommandObjectThreadInfo(interpreter)));
1942 LoadSubCommand("step-in",
1943 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
1944 interpreter, "thread step-in",
1945 "Source level single step, stepping into calls. Defaults "
1946 "to current thread unless specified.",
1947 nullptr, eStepTypeInto, eStepScopeSource)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001948
Kate Stoneb9c1b512016-09-06 20:57:50 +00001949 LoadSubCommand("step-out",
1950 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
1951 interpreter, "thread step-out",
1952 "Finish executing the current stack frame and stop after "
1953 "returning. Defaults to current thread unless specified.",
1954 nullptr, eStepTypeOut, eStepScopeSource)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001955
Kate Stoneb9c1b512016-09-06 20:57:50 +00001956 LoadSubCommand("step-over",
1957 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
1958 interpreter, "thread step-over",
1959 "Source level single step, stepping over calls. Defaults "
1960 "to current thread unless specified.",
1961 nullptr, eStepTypeOver, eStepScopeSource)));
Greg Clayton66111032010-06-23 01:19:29 +00001962
Kate Stoneb9c1b512016-09-06 20:57:50 +00001963 LoadSubCommand("step-inst",
1964 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
1965 interpreter, "thread step-inst",
1966 "Instruction level single step, stepping into calls. "
1967 "Defaults to current thread unless specified.",
1968 nullptr, eStepTypeTrace, eStepScopeInstruction)));
Kate Stone7428a182016-07-14 22:03:10 +00001969
Kate Stoneb9c1b512016-09-06 20:57:50 +00001970 LoadSubCommand("step-inst-over",
1971 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
1972 interpreter, "thread step-inst-over",
1973 "Instruction level single step, stepping over calls. "
1974 "Defaults to current thread unless specified.",
1975 nullptr, eStepTypeTraceOver, eStepScopeInstruction)));
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001976
Kate Stoneb9c1b512016-09-06 20:57:50 +00001977 LoadSubCommand(
1978 "step-scripted",
1979 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
1980 interpreter, "thread step-scripted",
1981 "Step as instructed by the script class passed in the -C option.",
1982 nullptr, eStepTypeScripted, eStepScopeSource)));
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001983
Kate Stoneb9c1b512016-09-06 20:57:50 +00001984 LoadSubCommand("plan", CommandObjectSP(new CommandObjectMultiwordThreadPlan(
1985 interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001986}
1987
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001988CommandObjectMultiwordThread::~CommandObjectMultiwordThread() = default;