blob: 9e4bffdeb6a558a93ce909f29406888262cf81f2 [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
Jim Inghamcb640dd2012-09-14 02:14:15 +000016#include "lldb/lldb-private.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/State.h"
18#include "lldb/Core/SourceManager.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000019#include "lldb/Core/ValueObject.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000020#include "lldb/Host/Host.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"
27#include "lldb/Symbol/LineTable.h"
28#include "lldb/Symbol/LineEntry.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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Target/ThreadPlanStepInstruction.h"
36#include "lldb/Target/ThreadPlanStepOut.h"
37#include "lldb/Target/ThreadPlanStepRange.h"
38#include "lldb/Target/ThreadPlanStepInRange.h"
Greg Clayton1f746072012-08-29 21:13:06 +000039
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040using namespace lldb;
41using namespace lldb_private;
42
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043//-------------------------------------------------------------------------
44// CommandObjectThreadBacktrace
45//-------------------------------------------------------------------------
46
Jim Ingham2bdbfd52014-09-29 23:17:18 +000047class CommandObjectIterateOverThreads : public CommandObjectParsed
48{
49public:
50 CommandObjectIterateOverThreads (CommandInterpreter &interpreter,
51 const char *name,
52 const char *help,
53 const char *syntax,
54 uint32_t flags) :
55 CommandObjectParsed (interpreter, name, help, syntax, flags)
56 {
57 }
58
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +000059 ~CommandObjectIterateOverThreads() override = default;
Bruce Mitchener13d21e92015-10-07 16:56:17 +000060
61 bool
62 DoExecute (Args& command, CommandReturnObject &result) override
Jim Ingham2bdbfd52014-09-29 23:17:18 +000063 {
64 result.SetStatus (m_success_return);
65
66 if (command.GetArgumentCount() == 0)
67 {
68 Thread *thread = m_exe_ctx.GetThreadPtr();
Stephane Sezerf8104912016-03-17 18:52:41 +000069 if (!HandleOneThread (thread->GetID(), result))
Jim Ingham2bdbfd52014-09-29 23:17:18 +000070 return false;
Stephane Sezerf8104912016-03-17 18:52:41 +000071 return result.Succeeded();
Jim Ingham2bdbfd52014-09-29 23:17:18 +000072 }
Stephane Sezerf8104912016-03-17 18:52:41 +000073
74 // Use tids instead of ThreadSPs to prevent deadlocking problems which result from JIT-ing
75 // code while iterating over the (locked) ThreadSP list.
76 std::vector<lldb::tid_t> tids;
77
78 if (command.GetArgumentCount() == 1 && ::strcmp (command.GetArgumentAtIndex(0), "all") == 0)
Jim Ingham2bdbfd52014-09-29 23:17:18 +000079 {
80 Process *process = m_exe_ctx.GetProcessPtr();
Jim Ingham2bdbfd52014-09-29 23:17:18 +000081
Stephane Sezerf8104912016-03-17 18:52:41 +000082 for (ThreadSP thread_sp : process->Threads())
83 tids.push_back(thread_sp->GetID());
Jim Ingham2bdbfd52014-09-29 23:17:18 +000084 }
85 else
86 {
87 const size_t num_args = command.GetArgumentCount();
88 Process *process = m_exe_ctx.GetProcessPtr();
Stephane Sezerf8104912016-03-17 18:52:41 +000089
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000090 std::lock_guard<std::recursive_mutex> guard(process->GetThreadList().GetMutex());
Jim Ingham2bdbfd52014-09-29 23:17:18 +000091
92 for (size_t i = 0; i < num_args; i++)
93 {
94 bool success;
Stephane Sezerf8104912016-03-17 18:52:41 +000095
Vince Harron5275aaa2015-01-15 20:08:35 +000096 uint32_t thread_idx = StringConvert::ToUInt32(command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham2bdbfd52014-09-29 23:17:18 +000097 if (!success)
98 {
99 result.AppendErrorWithFormat ("invalid thread specification: \"%s\"\n", command.GetArgumentAtIndex(i));
100 result.SetStatus (eReturnStatusFailed);
101 return false;
102 }
Stephane Sezerf8104912016-03-17 18:52:41 +0000103
104 ThreadSP thread = process->GetThreadList().FindThreadByIndexID(thread_idx);
105
106 if (!thread)
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000107 {
108 result.AppendErrorWithFormat ("no thread with index: \"%s\"\n", command.GetArgumentAtIndex(i));
109 result.SetStatus (eReturnStatusFailed);
110 return false;
111 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000112
Stephane Sezerf8104912016-03-17 18:52:41 +0000113 tids.push_back(thread->GetID());
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000114 }
115 }
Stephane Sezerf8104912016-03-17 18:52:41 +0000116
117 uint32_t idx = 0;
118 for (const lldb::tid_t &tid : tids)
119 {
120 if (idx != 0 && m_add_return)
121 result.AppendMessage("");
122
123 if (!HandleOneThread (tid, result))
124 return false;
125
126 ++idx;
127 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000128 return result.Succeeded();
129 }
130
131protected:
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000132 // Override this to do whatever you need to do for one thread.
133 //
134 // If you return false, the iteration will stop, otherwise it will proceed.
135 // The result is set to m_success_return (defaults to eReturnStatusSuccessFinishResult) before the iteration,
136 // so you only need to set the return status in HandleOneThread if you want to indicate an error.
137 // If m_add_return is true, a blank line will be inserted between each of the listings (except the last one.)
138
139 virtual bool
Stephane Sezerf8104912016-03-17 18:52:41 +0000140 HandleOneThread (lldb::tid_t, CommandReturnObject &result) = 0;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000141
142 ReturnStatus m_success_return = eReturnStatusSuccessFinishResult;
143 bool m_add_return = true;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000144};
145
146//-------------------------------------------------------------------------
147// CommandObjectThreadBacktrace
148//-------------------------------------------------------------------------
149
150class CommandObjectThreadBacktrace : public CommandObjectIterateOverThreads
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151{
152public:
Jim Inghame2e0b452010-08-26 23:36:03 +0000153 class CommandOptions : public Options
154 {
155 public:
Greg Claytoneb0103f2011-04-07 22:46:35 +0000156 CommandOptions (CommandInterpreter &interpreter) :
157 Options(interpreter)
Jim Inghame2e0b452010-08-26 23:36:03 +0000158 {
Greg Claytonf6b8b582011-04-13 00:18:08 +0000159 // Keep default values of all options in one place: OptionParsingStarting ()
160 OptionParsingStarting ();
Jim Inghame2e0b452010-08-26 23:36:03 +0000161 }
162
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000163 ~CommandOptions() override = default;
Jim Inghame2e0b452010-08-26 23:36:03 +0000164
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000165 Error
166 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Jim Inghame2e0b452010-08-26 23:36:03 +0000167 {
168 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000169 const int short_option = m_getopt_table[option_idx].val;
Jim Inghame2e0b452010-08-26 23:36:03 +0000170
171 switch (short_option)
172 {
173 case 'c':
174 {
175 bool success;
Vince Harron5275aaa2015-01-15 20:08:35 +0000176 int32_t input_count = StringConvert::ToSInt32 (option_arg, -1, 0, &success);
Jim Inghame2e0b452010-08-26 23:36:03 +0000177 if (!success)
Greg Clayton86edbf42011-10-26 00:56:27 +0000178 error.SetErrorStringWithFormat("invalid integer value for option '%c'", short_option);
Jim Inghame2e0b452010-08-26 23:36:03 +0000179 if (input_count < -1)
180 m_count = UINT32_MAX;
181 else
182 m_count = input_count;
183 }
184 break;
185 case 's':
186 {
187 bool success;
Vince Harron5275aaa2015-01-15 20:08:35 +0000188 m_start = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
Jim Inghame2e0b452010-08-26 23:36:03 +0000189 if (!success)
Greg Clayton86edbf42011-10-26 00:56:27 +0000190 error.SetErrorStringWithFormat("invalid integer value for option '%c'", short_option);
Jim Inghame2e0b452010-08-26 23:36:03 +0000191 }
Jim Ingham1f5fcf82016-02-06 00:31:23 +0000192 break;
Jason Molenda750ea692013-11-12 07:02:07 +0000193 case 'e':
194 {
195 bool success;
196 m_extended_backtrace = Args::StringToBoolean (option_arg, false, &success);
197 if (!success)
198 error.SetErrorStringWithFormat("invalid boolean value for option '%c'", short_option);
199 }
Jim Inghame2e0b452010-08-26 23:36:03 +0000200 break;
201 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000202 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Jim Inghame2e0b452010-08-26 23:36:03 +0000203 break;
Jim Inghame2e0b452010-08-26 23:36:03 +0000204 }
205 return error;
206 }
207
208 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000209 OptionParsingStarting () override
Jim Inghame2e0b452010-08-26 23:36:03 +0000210 {
Greg Clayton7260f622011-04-18 08:33:37 +0000211 m_count = UINT32_MAX;
Jim Inghame2e0b452010-08-26 23:36:03 +0000212 m_start = 0;
Jason Molenda750ea692013-11-12 07:02:07 +0000213 m_extended_backtrace = false;
Jim Inghame2e0b452010-08-26 23:36:03 +0000214 }
215
Greg Claytone0d378b2011-03-24 21:19:54 +0000216 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000217 GetDefinitions () override
Jim Inghame2e0b452010-08-26 23:36:03 +0000218 {
219 return g_option_table;
220 }
221
222 // Options table: Required for subclasses of Options.
223
Greg Claytone0d378b2011-03-24 21:19:54 +0000224 static OptionDefinition g_option_table[];
Jim Inghame2e0b452010-08-26 23:36:03 +0000225
226 // Instance variables to hold the values for command options.
227 uint32_t m_count;
228 uint32_t m_start;
Jason Molenda750ea692013-11-12 07:02:07 +0000229 bool m_extended_backtrace;
Jim Inghame2e0b452010-08-26 23:36:03 +0000230 };
231
Kate Stone7428a182016-07-14 22:03:10 +0000232 CommandObjectThreadBacktrace(CommandInterpreter &interpreter)
233 : CommandObjectIterateOverThreads(
234 interpreter, "thread backtrace", "Show thread call stacks. Defaults to the current thread, thread "
235 "indexes can be specified as arguments. Use the thread-index \"all\" "
236 "to see all threads.",
237 nullptr, eCommandRequiresProcess | eCommandRequiresThread | eCommandTryTargetAPILock |
238 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
239 m_options(interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240 {
241 }
242
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000243 ~CommandObjectThreadBacktrace() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000245 Options *
246 GetOptions () override
Jim Inghame2e0b452010-08-26 23:36:03 +0000247 {
248 return &m_options;
249 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250
Jim Ingham5a988412012-06-08 21:56:10 +0000251protected:
Jason Molenda750ea692013-11-12 07:02:07 +0000252 void
253 DoExtendedBacktrace (Thread *thread, CommandReturnObject &result)
254 {
255 SystemRuntime *runtime = thread->GetProcess()->GetSystemRuntime();
256 if (runtime)
257 {
258 Stream &strm = result.GetOutputStream();
259 const std::vector<ConstString> &types = runtime->GetExtendedBacktraceTypes();
260 for (auto type : types)
261 {
Jason Molenda008c45f2013-11-12 23:33:32 +0000262 ThreadSP ext_thread_sp = runtime->GetExtendedBacktraceThread (thread->shared_from_this(), type);
Jason Molenda750ea692013-11-12 07:02:07 +0000263 if (ext_thread_sp && ext_thread_sp->IsValid ())
264 {
265 const uint32_t num_frames_with_source = 0;
266 if (ext_thread_sp->GetStatus (strm,
267 m_options.m_start,
268 m_options.m_count,
269 num_frames_with_source))
270 {
271 DoExtendedBacktrace (ext_thread_sp.get(), result);
272 }
273 }
274 }
275 }
276 }
277
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000278 bool
Stephane Sezerf8104912016-03-17 18:52:41 +0000279 HandleOneThread (lldb::tid_t tid, CommandReturnObject &result) override
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000280 {
Stephane Sezerf8104912016-03-17 18:52:41 +0000281 ThreadSP thread_sp = m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
282 if (!thread_sp)
283 {
284 result.AppendErrorWithFormat ("thread disappeared while computing backtraces: 0x%" PRIx64 "\n", tid);
285 result.SetStatus (eReturnStatusFailed);
286 return false;
287 }
288
289 Thread *thread = thread_sp.get();
290
Greg Clayton7260f622011-04-18 08:33:37 +0000291 Stream &strm = result.GetOutputStream();
292
293 // Don't show source context when doing backtraces.
294 const uint32_t num_frames_with_source = 0;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000295
Stephane Sezerf8104912016-03-17 18:52:41 +0000296 if (!thread->GetStatus (strm,
297 m_options.m_start,
298 m_options.m_count,
299 num_frames_with_source))
Jim Ingham09b263e2010-08-27 00:58:05 +0000300 {
Stephane Sezerf8104912016-03-17 18:52:41 +0000301 result.AppendErrorWithFormat ("error displaying backtrace for thread: \"0x%4.4x\"\n", thread->GetIndexID());
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000302 result.SetStatus (eReturnStatusFailed);
303 return false;
Jim Ingham09b263e2010-08-27 00:58:05 +0000304 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000305 if (m_options.m_extended_backtrace)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306 {
Stephane Sezerf8104912016-03-17 18:52:41 +0000307 DoExtendedBacktrace (thread, result);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000309
310 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311 }
Jim Ingham5a988412012-06-08 21:56:10 +0000312
Jim Inghame2e0b452010-08-26 23:36:03 +0000313 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314};
315
Greg Claytone0d378b2011-03-24 21:19:54 +0000316OptionDefinition
Jim Inghame2e0b452010-08-26 23:36:03 +0000317CommandObjectThreadBacktrace::CommandOptions::g_option_table[] =
318{
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000319{ LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCount, "How many frames to display (-1 for all)"},
320{ LLDB_OPT_SET_1, false, "start", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFrameIndex, "Frame in which to start the backtrace"},
321{ LLDB_OPT_SET_1, false, "extended", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Show the extended backtrace, if available"},
322{ 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Jim Inghame2e0b452010-08-26 23:36:03 +0000323};
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324
Greg Clayton69b518f2010-07-07 17:07:17 +0000325enum StepScope
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326{
327 eStepScopeSource,
328 eStepScopeInstruction
329};
330
Jim Ingham5a988412012-06-08 21:56:10 +0000331class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332{
333public:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 class CommandOptions : public Options
335 {
336 public:
Greg Claytoneb0103f2011-04-07 22:46:35 +0000337 CommandOptions (CommandInterpreter &interpreter) :
338 Options (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339 {
Greg Claytonf6b8b582011-04-13 00:18:08 +0000340 // Keep default values of all options in one place: OptionParsingStarting ()
341 OptionParsingStarting ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342 }
343
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000344 ~CommandOptions() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000346 Error
347 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 {
349 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000350 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351
352 switch (short_option)
353 {
Greg Clayton8087ca22010-10-08 04:20:14 +0000354 case 'a':
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355 {
356 bool success;
Jim Ingham4b4b2472014-03-13 02:47:14 +0000357 bool avoid_no_debug = Args::StringToBoolean (option_arg, true, &success);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 if (!success)
Greg Clayton86edbf42011-10-26 00:56:27 +0000359 error.SetErrorStringWithFormat("invalid boolean value for option '%c'", short_option);
Jim Ingham4b4b2472014-03-13 02:47:14 +0000360 else
361 {
362 m_step_in_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo;
363 }
364 }
365 break;
366
367 case 'A':
368 {
369 bool success;
370 bool avoid_no_debug = Args::StringToBoolean (option_arg, true, &success);
371 if (!success)
372 error.SetErrorStringWithFormat("invalid boolean value for option '%c'", short_option);
373 else
374 {
375 m_step_out_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo;
376 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 }
378 break;
Greg Clayton8087ca22010-10-08 04:20:14 +0000379
Jim Ingham7a88ec92014-07-08 19:28:57 +0000380 case 'c':
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000381 m_step_count = StringConvert::ToUInt32(option_arg, UINT32_MAX, 0);
382 if (m_step_count == UINT32_MAX)
383 error.SetErrorStringWithFormat ("invalid step count '%s'", option_arg);
Jim Ingham7a88ec92014-07-08 19:28:57 +0000384 break;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000385
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000386 case 'C':
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000387 m_class_name.clear();
388 m_class_name.assign(option_arg);
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000389 break;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000390
Greg Clayton8087ca22010-10-08 04:20:14 +0000391 case 'm':
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393 OptionEnumValueElement *enum_values = g_option_table[option_idx].enum_values;
Greg Claytoncf0e4f02011-10-07 18:58:12 +0000394 m_run_mode = (lldb::RunMode) Args::StringToOptionEnum(option_arg, enum_values, eOnlyDuringStepping, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395 }
396 break;
Greg Clayton8087ca22010-10-08 04:20:14 +0000397
Jim Inghamc17d6bd2016-02-10 03:25:24 +0000398 case 'e':
399 {
Jim Ingham970bb9e2016-02-26 01:37:30 +0000400 if (strcmp(option_arg, "block") == 0)
401 {
402 m_end_line_is_block_end = 1;
403 break;
404 }
Jim Inghamc17d6bd2016-02-10 03:25:24 +0000405 uint32_t tmp_end_line = StringConvert::ToUInt32(option_arg, UINT32_MAX, 0);
406 if (tmp_end_line == UINT32_MAX)
407 error.SetErrorStringWithFormat ("invalid end line number '%s'", option_arg);
408 else
409 m_end_line = tmp_end_line;
410 break;
411 }
412 break;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000413
Greg Clayton8087ca22010-10-08 04:20:14 +0000414 case 'r':
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000415 m_avoid_regexp.clear();
416 m_avoid_regexp.assign(option_arg);
Jim Inghama56c8002010-07-10 02:27:39 +0000417 break;
Greg Clayton8087ca22010-10-08 04:20:14 +0000418
Jim Inghamc6276822012-12-12 19:58:40 +0000419 case 't':
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000420 m_step_in_target.clear();
421 m_step_in_target.assign(option_arg);
Jim Inghamc6276822012-12-12 19:58:40 +0000422 break;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000423
Greg Clayton8087ca22010-10-08 04:20:14 +0000424 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000425 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Clayton8087ca22010-10-08 04:20:14 +0000426 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 }
428 return error;
429 }
430
431 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000432 OptionParsingStarting () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433 {
Jim Ingham4b4b2472014-03-13 02:47:14 +0000434 m_step_in_avoid_no_debug = eLazyBoolCalculate;
435 m_step_out_avoid_no_debug = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436 m_run_mode = eOnlyDuringStepping;
Ewan Crawford78baa192015-05-13 09:18:18 +0000437
438 // Check if we are in Non-Stop mode
439 lldb::TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000440 if (target_sp && target_sp->GetNonStopModeEnabled())
Ewan Crawford78baa192015-05-13 09:18:18 +0000441 m_run_mode = eOnlyThisThread;
442
Jim Inghama56c8002010-07-10 02:27:39 +0000443 m_avoid_regexp.clear();
Jim Inghamc6276822012-12-12 19:58:40 +0000444 m_step_in_target.clear();
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000445 m_class_name.clear();
Jim Ingham7a88ec92014-07-08 19:28:57 +0000446 m_step_count = 1;
Jim Inghamc17d6bd2016-02-10 03:25:24 +0000447 m_end_line = LLDB_INVALID_LINE_NUMBER;
Jim Ingham970bb9e2016-02-26 01:37:30 +0000448 m_end_line_is_block_end = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449 }
450
Greg Claytone0d378b2011-03-24 21:19:54 +0000451 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000452 GetDefinitions () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 {
454 return g_option_table;
455 }
456
457 // Options table: Required for subclasses of Options.
458
Greg Claytone0d378b2011-03-24 21:19:54 +0000459 static OptionDefinition g_option_table[];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460
461 // Instance variables to hold the values for command options.
Jim Ingham4b4b2472014-03-13 02:47:14 +0000462 LazyBool m_step_in_avoid_no_debug;
463 LazyBool m_step_out_avoid_no_debug;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 RunMode m_run_mode;
Jim Inghama56c8002010-07-10 02:27:39 +0000465 std::string m_avoid_regexp;
Jim Inghamc6276822012-12-12 19:58:40 +0000466 std::string m_step_in_target;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000467 std::string m_class_name;
Zachary Turner898e10e2015-01-09 20:15:21 +0000468 uint32_t m_step_count;
Jim Inghamc17d6bd2016-02-10 03:25:24 +0000469 uint32_t m_end_line;
Jim Ingham970bb9e2016-02-26 01:37:30 +0000470 bool m_end_line_is_block_end;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471 };
472
Greg Claytona7015092010-09-18 01:14:36 +0000473 CommandObjectThreadStepWithTypeAndScope (CommandInterpreter &interpreter,
474 const char *name,
475 const char *help,
476 const char *syntax,
Greg Claytona7015092010-09-18 01:14:36 +0000477 StepType step_type,
478 StepScope step_scope) :
Greg Claytonf9fc6092013-01-09 19:44:40 +0000479 CommandObjectParsed (interpreter, name, help, syntax,
Enrico Granatae87764f2015-05-27 05:04:35 +0000480 eCommandRequiresProcess |
481 eCommandRequiresThread |
482 eCommandTryTargetAPILock |
483 eCommandProcessMustBeLaunched |
484 eCommandProcessMustBePaused ),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485 m_step_type (step_type),
486 m_step_scope (step_scope),
Greg Claytoneb0103f2011-04-07 22:46:35 +0000487 m_options (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 {
Caroline Tice405fe672010-10-04 22:28:36 +0000489 CommandArgumentEntry arg;
490 CommandArgumentData thread_id_arg;
491
492 // Define the first (and only) variant of this arg.
493 thread_id_arg.arg_type = eArgTypeThreadID;
494 thread_id_arg.arg_repetition = eArgRepeatOptional;
495
496 // There is only one variant this argument could be; put it into the argument entry.
497 arg.push_back (thread_id_arg);
498
499 // Push the data for the first argument into the m_arguments vector.
500 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501 }
502
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000503 ~CommandObjectThreadStepWithTypeAndScope() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000506 GetOptions () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507 {
508 return &m_options;
509 }
510
Jim Ingham5a988412012-06-08 21:56:10 +0000511protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000512 bool
513 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000515 Process *process = m_exe_ctx.GetProcessPtr();
Greg Claytona7015092010-09-18 01:14:36 +0000516 bool synchronous_execution = m_interpreter.GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517
Greg Claytonf9fc6092013-01-09 19:44:40 +0000518 const uint32_t num_threads = process->GetThreadList().GetSize();
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000519 Thread *thread = nullptr;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000520
521 if (command.GetArgumentCount() == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522 {
Jim Ingham8d94ba02016-03-12 02:45:34 +0000523 thread = GetDefaultThread();
524
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000525 if (thread == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000527 result.AppendError ("no selected thread in process");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528 result.SetStatus (eReturnStatusFailed);
Jim Ingham64e7ead2012-05-03 21:19:36 +0000529 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000531 }
532 else
533 {
534 const char *thread_idx_cstr = command.GetArgumentAtIndex(0);
Vince Harron5275aaa2015-01-15 20:08:35 +0000535 uint32_t step_thread_idx = StringConvert::ToUInt32 (thread_idx_cstr, LLDB_INVALID_INDEX32);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000536 if (step_thread_idx == LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000537 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000538 result.AppendErrorWithFormat ("invalid thread index '%s'.\n", thread_idx_cstr);
539 result.SetStatus (eReturnStatusFailed);
540 return false;
541 }
542 thread = process->GetThreadList().FindThreadByIndexID(step_thread_idx).get();
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000543 if (thread == nullptr)
Greg Claytonf9fc6092013-01-09 19:44:40 +0000544 {
545 result.AppendErrorWithFormat ("Thread index %u is out of range (valid values are 0 - %u).\n",
546 step_thread_idx, num_threads);
547 result.SetStatus (eReturnStatusFailed);
548 return false;
549 }
550 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000551
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000552 if (m_step_type == eStepTypeScripted)
553 {
554 if (m_options.m_class_name.empty())
555 {
556 result.AppendErrorWithFormat ("empty class name for scripted step.");
557 result.SetStatus(eReturnStatusFailed);
558 return false;
559 }
560 else if (!m_interpreter.GetScriptInterpreter()->CheckObjectExists(m_options.m_class_name.c_str()))
561 {
562 result.AppendErrorWithFormat ("class for scripted step: \"%s\" does not exist.", m_options.m_class_name.c_str());
563 result.SetStatus(eReturnStatusFailed);
564 return false;
565 }
566 }
567
Jim Inghamc17d6bd2016-02-10 03:25:24 +0000568 if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER
569 && m_step_type != eStepTypeInto)
570 {
571 result.AppendErrorWithFormat("end line option is only valid for step into");
572 result.SetStatus(eReturnStatusFailed);
573 return false;
574 }
575
Greg Claytonf9fc6092013-01-09 19:44:40 +0000576 const bool abort_other_plans = false;
577 const lldb::RunMode stop_other_threads = m_options.m_run_mode;
578
579 // This is a bit unfortunate, but not all the commands in this command object support
580 // only while stepping, so I use the bool for them.
581 bool bool_stop_other_threads;
582 if (m_options.m_run_mode == eAllThreads)
583 bool_stop_other_threads = false;
584 else if (m_options.m_run_mode == eOnlyDuringStepping)
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000585 bool_stop_other_threads = (m_step_type != eStepTypeOut && m_step_type != eStepTypeScripted);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000586 else
587 bool_stop_other_threads = true;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000588
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000589 ThreadPlanSP new_plan_sp;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000590
591 if (m_step_type == eStepTypeInto)
592 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000593 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
Stephane Sezerca05ae22015-03-26 17:47:34 +0000594 assert(frame != nullptr);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000595
Stephane Sezerca05ae22015-03-26 17:47:34 +0000596 if (frame->HasDebugInformation ())
Greg Claytonf9fc6092013-01-09 19:44:40 +0000597 {
Jim Inghamcbf6f9b2016-02-13 00:31:47 +0000598 AddressRange range;
599 SymbolContext sc = frame->GetSymbolContext(eSymbolContextEverything);
Jim Inghamc17d6bd2016-02-10 03:25:24 +0000600 if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER)
601 {
Jim Inghamcbf6f9b2016-02-13 00:31:47 +0000602 Error error;
603 if (!sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range, error))
Jim Inghamc17d6bd2016-02-10 03:25:24 +0000604 {
Jim Inghamcbf6f9b2016-02-13 00:31:47 +0000605 result.AppendErrorWithFormat("invalid end-line option: %s.", error.AsCString());
Jim Inghamc17d6bd2016-02-10 03:25:24 +0000606 result.SetStatus(eReturnStatusFailed);
607 return false;
608 }
Jim Inghamcbf6f9b2016-02-13 00:31:47 +0000609 }
Jim Ingham970bb9e2016-02-26 01:37:30 +0000610 else if (m_options.m_end_line_is_block_end)
611 {
612 Error error;
613 Block *block = frame->GetSymbolContext(eSymbolContextBlock).block;
614 if (!block)
615 {
616 result.AppendErrorWithFormat("Could not find the current block.");
617 result.SetStatus(eReturnStatusFailed);
618 return false;
619 }
620
621 AddressRange block_range;
622 Address pc_address = frame->GetFrameCodeAddress();
623 block->GetRangeContainingAddress(pc_address, block_range);
624 if (!block_range.GetBaseAddress().IsValid())
625 {
626 result.AppendErrorWithFormat("Could not find the current block address.");
627 result.SetStatus(eReturnStatusFailed);
628 return false;
629 }
630 lldb::addr_t pc_offset_in_block = pc_address.GetFileAddress() - block_range.GetBaseAddress().GetFileAddress();
631 lldb::addr_t range_length = block_range.GetByteSize() - pc_offset_in_block;
632 range = AddressRange(pc_address, range_length);
633 }
Jim Inghamcbf6f9b2016-02-13 00:31:47 +0000634 else
635 {
636 range = sc.line_entry.range;
Jim Inghamc17d6bd2016-02-10 03:25:24 +0000637 }
638
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000639 new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
Jim Inghamcbf6f9b2016-02-13 00:31:47 +0000640 range,
641 frame->GetSymbolContext(eSymbolContextEverything),
642 m_options.m_step_in_target.c_str(),
643 stop_other_threads,
644 m_options.m_step_in_avoid_no_debug,
645 m_options.m_step_out_avoid_no_debug);
Jim Ingham4b4b2472014-03-13 02:47:14 +0000646
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000647 if (new_plan_sp && !m_options.m_avoid_regexp.empty())
Jim Ingham64e7ead2012-05-03 21:19:36 +0000648 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000649 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (new_plan_sp.get());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000650 step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str());
Jim Ingham29412d12012-05-16 00:37:40 +0000651 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000652 }
653 else
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000654 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false, abort_other_plans, bool_stop_other_threads);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000655 }
656 else if (m_step_type == eStepTypeOver)
657 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000658 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
Greg Claytonf9fc6092013-01-09 19:44:40 +0000659
660 if (frame->HasDebugInformation())
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000661 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
Jason Molenda25d5b102015-12-15 00:40:30 +0000662 frame->GetSymbolContext(eSymbolContextEverything).line_entry,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000663 frame->GetSymbolContext(eSymbolContextEverything),
Jim Ingham4b4b2472014-03-13 02:47:14 +0000664 stop_other_threads,
665 m_options.m_step_out_avoid_no_debug);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000666 else
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000667 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000668 abort_other_plans,
669 bool_stop_other_threads);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000670 }
671 else if (m_step_type == eStepTypeTrace)
672 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000673 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false, abort_other_plans, bool_stop_other_threads);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000674 }
675 else if (m_step_type == eStepTypeTraceOver)
676 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000677 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true, abort_other_plans, bool_stop_other_threads);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000678 }
679 else if (m_step_type == eStepTypeOut)
680 {
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000681 new_plan_sp = thread->QueueThreadPlanForStepOut(abort_other_plans,
682 nullptr,
683 false,
684 bool_stop_other_threads,
685 eVoteYes,
686 eVoteNoOpinion,
687 thread->GetSelectedFrameIndex(),
688 m_options.m_step_out_avoid_no_debug);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000689 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000690 else if (m_step_type == eStepTypeScripted)
691 {
692 new_plan_sp = thread->QueueThreadPlanForStepScripted (abort_other_plans,
693 m_options.m_class_name.c_str(),
694 bool_stop_other_threads);
695 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000696 else
697 {
698 result.AppendError ("step type is not supported");
699 result.SetStatus (eReturnStatusFailed);
700 return false;
701 }
702
703 // If we got a new plan, then set it to be a master plan (User level Plans should be master plans
704 // so that they can be interruptible). Then resume the process.
705
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000706 if (new_plan_sp)
Greg Claytonf9fc6092013-01-09 19:44:40 +0000707 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000708 new_plan_sp->SetIsMasterPlan (true);
709 new_plan_sp->SetOkayToDiscard (false);
Jim Ingham7a88ec92014-07-08 19:28:57 +0000710
711 if (m_options.m_step_count > 1)
712 {
Zachary Turner40411162014-07-16 20:28:24 +0000713 if (new_plan_sp->SetIterationCount(m_options.m_step_count))
Jim Ingham7a88ec92014-07-08 19:28:57 +0000714 {
715 result.AppendWarning ("step operation does not support iteration count.");
716 }
717 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000718
719 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
Greg Claytondc6224e2014-10-21 01:00:42 +0000720
Pavel Labath44464872015-05-27 12:40:32 +0000721 const uint32_t iohandler_id = process->GetIOHandlerID();
722
Greg Claytondc6224e2014-10-21 01:00:42 +0000723 StreamString stream;
724 Error error;
725 if (synchronous_execution)
726 error = process->ResumeSynchronous (&stream);
727 else
728 error = process->Resume ();
Todd Fialaa3b89e22014-08-12 14:33:19 +0000729
730 // There is a race condition where this thread will return up the call stack to the main command handler
731 // and show an (lldb) prompt before HandlePrivateEvent (from PrivateStateThread) has
732 // a chance to call PushProcessIOHandler().
Pavel Labath44464872015-05-27 12:40:32 +0000733 process->SyncIOHandler(iohandler_id, 2000);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000734
735 if (synchronous_execution)
Jim Ingham64e7ead2012-05-03 21:19:36 +0000736 {
Greg Claytondc6224e2014-10-21 01:00:42 +0000737 // If any state changed events had anything to say, add that to the result
738 if (stream.GetData())
739 result.AppendMessage(stream.GetData());
740
Greg Claytonf9fc6092013-01-09 19:44:40 +0000741 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
742 result.SetDidChangeProcessState (true);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000743 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000745 else
746 {
747 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
748 }
749 }
750 else
751 {
752 result.AppendError ("Couldn't find thread plan to implement step type.");
753 result.SetStatus (eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754 }
755 return result.Succeeded();
756 }
757
758protected:
759 StepType m_step_type;
760 StepScope m_step_scope;
761 CommandOptions m_options;
762};
763
Greg Claytone0d378b2011-03-24 21:19:54 +0000764static OptionEnumValueElement
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765g_tri_running_mode[] =
766{
Greg Claytoned8a7052010-09-18 03:37:20 +0000767{ eOnlyThisThread, "this-thread", "Run only this thread"},
768{ eAllThreads, "all-threads", "Run all threads"},
769{ eOnlyDuringStepping, "while-stepping", "Run only this thread while stepping"},
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000770{ 0, nullptr, nullptr }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771};
772
Greg Claytone0d378b2011-03-24 21:19:54 +0000773static OptionEnumValueElement
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774g_duo_running_mode[] =
775{
Greg Claytoned8a7052010-09-18 03:37:20 +0000776{ eOnlyThisThread, "this-thread", "Run only this thread"},
777{ eAllThreads, "all-threads", "Run all threads"},
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000778{ 0, nullptr, nullptr }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779};
780
Greg Claytone0d378b2011-03-24 21:19:54 +0000781OptionDefinition
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782CommandObjectThreadStepWithTypeAndScope::CommandOptions::g_option_table[] =
783{
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000784{ 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."},
785{ 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."},
786{ 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."},
Jim Ingham970bb9e2016-02-26 01:37:30 +0000787{ 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."
788 " You can also pass the string 'block' to step to the end of the current block."
789 " This is particularly useful in conjunction with --step-target to step through a complex calling sequence."},
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000790{ 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."},
791{ 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."},
792{ 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."},
793{ 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."},
794{ 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795};
796
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797//-------------------------------------------------------------------------
798// CommandObjectThreadContinue
799//-------------------------------------------------------------------------
800
Jim Ingham5a988412012-06-08 21:56:10 +0000801class CommandObjectThreadContinue : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802{
803public:
Kate Stone7428a182016-07-14 22:03:10 +0000804 CommandObjectThreadContinue(CommandInterpreter &interpreter)
805 : CommandObjectParsed(interpreter, "thread continue", "Continue execution of the current target process. One "
806 "or more threads may be specified, by default all "
807 "threads continue.",
808 nullptr, eCommandRequiresThread | eCommandTryTargetAPILock |
809 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 {
Caroline Tice405fe672010-10-04 22:28:36 +0000811 CommandArgumentEntry arg;
812 CommandArgumentData thread_idx_arg;
813
814 // Define the first (and only) variant of this arg.
815 thread_idx_arg.arg_type = eArgTypeThreadIndex;
816 thread_idx_arg.arg_repetition = eArgRepeatPlus;
817
818 // There is only one variant this argument could be; put it into the argument entry.
819 arg.push_back (thread_idx_arg);
820
821 // Push the data for the first argument into the m_arguments vector.
822 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000823 }
824
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000825 ~CommandObjectThreadContinue() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000827 bool
828 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 {
Greg Claytona7015092010-09-18 01:14:36 +0000830 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000831
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000832 if (!m_interpreter.GetDebugger().GetSelectedTarget())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833 {
Greg Claytoneffe5c92011-05-03 22:09:39 +0000834 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835 result.SetStatus (eReturnStatusFailed);
836 return false;
837 }
838
Greg Claytonf9fc6092013-01-09 19:44:40 +0000839 Process *process = m_exe_ctx.GetProcessPtr();
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000840 if (process == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 {
842 result.AppendError ("no process exists. Cannot continue");
843 result.SetStatus (eReturnStatusFailed);
844 return false;
845 }
846
847 StateType state = process->GetState();
848 if ((state == eStateCrashed) || (state == eStateStopped) || (state == eStateSuspended))
849 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000850 const size_t argc = command.GetArgumentCount();
851 if (argc > 0)
852 {
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000853 // These two lines appear at the beginning of both blocks in
854 // this if..else, but that is because we need to release the
855 // lock before calling process->Resume below.
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000856 std::lock_guard<std::recursive_mutex> guard(process->GetThreadList().GetMutex());
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000857 const uint32_t num_threads = process->GetThreadList().GetSize();
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000858 std::vector<Thread *> resume_threads;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000859 for (uint32_t i = 0; i < argc; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860 {
Jim Inghamce76c622012-05-31 20:48:41 +0000861 bool success;
862 const int base = 0;
Vince Harron5275aaa2015-01-15 20:08:35 +0000863 uint32_t thread_idx = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), LLDB_INVALID_INDEX32, base, &success);
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000864 if (success)
Jim Inghamce76c622012-05-31 20:48:41 +0000865 {
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000866 Thread *thread = process->GetThreadList().FindThreadByIndexID(thread_idx).get();
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000867
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000868 if (thread)
869 {
870 resume_threads.push_back(thread);
871 }
872 else
873 {
874 result.AppendErrorWithFormat("invalid thread index %u.\n", thread_idx);
875 result.SetStatus (eReturnStatusFailed);
876 return false;
877 }
Jim Inghamce76c622012-05-31 20:48:41 +0000878 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879 else
Jim Inghamce76c622012-05-31 20:48:41 +0000880 {
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000881 result.AppendErrorWithFormat ("invalid thread index argument: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamce76c622012-05-31 20:48:41 +0000882 result.SetStatus (eReturnStatusFailed);
883 return false;
884 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000885 }
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000886
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000887 if (resume_threads.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000888 {
889 result.AppendError ("no valid thread indexes were specified");
890 result.SetStatus (eReturnStatusFailed);
891 return false;
892 }
893 else
894 {
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000895 if (resume_threads.size() == 1)
Jim Inghamce76c622012-05-31 20:48:41 +0000896 result.AppendMessageWithFormat ("Resuming thread: ");
897 else
898 result.AppendMessageWithFormat ("Resuming threads: ");
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000899
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000900 for (uint32_t idx = 0; idx < num_threads; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901 {
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000902 Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get();
903 std::vector<Thread *>::iterator this_thread_pos = find(resume_threads.begin(), resume_threads.end(), thread);
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000904
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000905 if (this_thread_pos != resume_threads.end())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000906 {
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000907 resume_threads.erase(this_thread_pos);
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000908 if (!resume_threads.empty())
Jim Inghamce76c622012-05-31 20:48:41 +0000909 result.AppendMessageWithFormat ("%u, ", thread->GetIndexID());
910 else
911 result.AppendMessageWithFormat ("%u ", thread->GetIndexID());
Jim Ingham6c9ed912014-04-03 01:26:14 +0000912
913 const bool override_suspend = true;
914 thread->SetResumeState (eStateRunning, override_suspend);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000915 }
916 else
917 {
918 thread->SetResumeState (eStateSuspended);
919 }
920 }
Daniel Malead01b2952012-11-29 21:49:15 +0000921 result.AppendMessageWithFormat ("in process %" PRIu64 "\n", process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000922 }
923 }
924 else
925 {
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000926 // These two lines appear at the beginning of both blocks in
927 // this if..else, but that is because we need to release the
928 // lock before calling process->Resume below.
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000929 std::lock_guard<std::recursive_mutex> guard(process->GetThreadList().GetMutex());
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000930 const uint32_t num_threads = process->GetThreadList().GetSize();
Jim Ingham8d94ba02016-03-12 02:45:34 +0000931 Thread *current_thread = GetDefaultThread();
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000932 if (current_thread == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933 {
934 result.AppendError ("the process doesn't have a current thread");
935 result.SetStatus (eReturnStatusFailed);
936 return false;
937 }
938 // Set the actions that the threads should each take when resuming
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +0000939 for (uint32_t idx = 0; idx < num_threads; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940 {
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000941 Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942 if (thread == current_thread)
943 {
Daniel Malead01b2952012-11-29 21:49:15 +0000944 result.AppendMessageWithFormat ("Resuming thread 0x%4.4" PRIx64 " in process %" PRIu64 "\n", thread->GetID(), process->GetID());
Jim Ingham6c9ed912014-04-03 01:26:14 +0000945 const bool override_suspend = true;
946 thread->SetResumeState (eStateRunning, override_suspend);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000947 }
948 else
949 {
950 thread->SetResumeState (eStateSuspended);
951 }
952 }
953 }
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000954
Greg Claytondc6224e2014-10-21 01:00:42 +0000955 StreamString stream;
956 Error error;
957 if (synchronous_execution)
958 error = process->ResumeSynchronous (&stream);
959 else
960 error = process->Resume ();
961
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000962 // We should not be holding the thread list lock when we do this.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963 if (error.Success())
964 {
Daniel Malead01b2952012-11-29 21:49:15 +0000965 result.AppendMessageWithFormat ("Process %" PRIu64 " resuming\n", process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966 if (synchronous_execution)
967 {
Greg Claytondc6224e2014-10-21 01:00:42 +0000968 // If any state changed events had anything to say, add that to the result
969 if (stream.GetData())
970 result.AppendMessage(stream.GetData());
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000971
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972 result.SetDidChangeProcessState (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000973 result.SetStatus (eReturnStatusSuccessFinishNoResult);
974 }
975 else
976 {
977 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
978 }
979 }
980 else
981 {
982 result.AppendErrorWithFormat("Failed to resume process: %s\n", error.AsCString());
983 result.SetStatus (eReturnStatusFailed);
984 }
985 }
986 else
987 {
988 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
989 StateAsCString(state));
990 result.SetStatus (eReturnStatusFailed);
991 }
992
993 return result.Succeeded();
994 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995};
996
997//-------------------------------------------------------------------------
998// CommandObjectThreadUntil
999//-------------------------------------------------------------------------
1000
Jim Ingham5a988412012-06-08 21:56:10 +00001001class CommandObjectThreadUntil : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002{
1003public:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004 class CommandOptions : public Options
1005 {
1006 public:
1007 uint32_t m_thread_idx;
1008 uint32_t m_frame_idx;
1009
Greg Claytoneb0103f2011-04-07 22:46:35 +00001010 CommandOptions (CommandInterpreter &interpreter) :
1011 Options (interpreter),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012 m_thread_idx(LLDB_INVALID_THREAD_ID),
1013 m_frame_idx(LLDB_INVALID_FRAME_ID)
1014 {
Greg Claytonf6b8b582011-04-13 00:18:08 +00001015 // Keep default values of all options in one place: OptionParsingStarting ()
1016 OptionParsingStarting ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017 }
1018
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001019 ~CommandOptions() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001021 Error
1022 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023 {
1024 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001025 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026
1027 switch (short_option)
1028 {
Jim Ingham9bdea542015-02-06 02:10:56 +00001029 case 'a':
1030 {
1031 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
1032 lldb::addr_t tmp_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
1033 if (error.Success())
1034 m_until_addrs.push_back(tmp_addr);
1035 }
1036 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001037 case 't':
Vince Harron5275aaa2015-01-15 20:08:35 +00001038 m_thread_idx = StringConvert::ToUInt32 (option_arg, LLDB_INVALID_INDEX32);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039 if (m_thread_idx == LLDB_INVALID_INDEX32)
1040 {
Greg Clayton86edbf42011-10-26 00:56:27 +00001041 error.SetErrorStringWithFormat ("invalid thread index '%s'", option_arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042 }
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001043 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001044 case 'f':
Vince Harron5275aaa2015-01-15 20:08:35 +00001045 m_frame_idx = StringConvert::ToUInt32 (option_arg, LLDB_INVALID_FRAME_ID);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046 if (m_frame_idx == LLDB_INVALID_FRAME_ID)
1047 {
Greg Clayton86edbf42011-10-26 00:56:27 +00001048 error.SetErrorStringWithFormat ("invalid frame index '%s'", option_arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001049 }
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001050 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051 case 'm':
1052 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001053 OptionEnumValueElement *enum_values = g_option_table[option_idx].enum_values;
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001054 lldb::RunMode run_mode = (lldb::RunMode) Args::StringToOptionEnum(option_arg, enum_values, eOnlyDuringStepping, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001055
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001056 if (error.Success())
1057 {
1058 if (run_mode == eAllThreads)
1059 m_stop_others = false;
1060 else
1061 m_stop_others = true;
1062 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063 }
1064 break;
1065 default:
Greg Clayton86edbf42011-10-26 00:56:27 +00001066 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001067 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068 }
1069 return error;
1070 }
1071
1072 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001073 OptionParsingStarting () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075 m_thread_idx = LLDB_INVALID_THREAD_ID;
1076 m_frame_idx = 0;
1077 m_stop_others = false;
Jim Ingham9bdea542015-02-06 02:10:56 +00001078 m_until_addrs.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079 }
1080
Greg Claytone0d378b2011-03-24 21:19:54 +00001081 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001082 GetDefinitions () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083 {
1084 return g_option_table;
1085 }
1086
1087 uint32_t m_step_thread_idx;
1088 bool m_stop_others;
Jim Ingham9bdea542015-02-06 02:10:56 +00001089 std::vector<lldb::addr_t> m_until_addrs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090
1091 // Options table: Required for subclasses of Options.
1092
Greg Claytone0d378b2011-03-24 21:19:54 +00001093 static OptionDefinition g_option_table[];
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094
1095 // Instance variables to hold the values for command options.
1096 };
1097
Kate Stone7428a182016-07-14 22:03:10 +00001098 CommandObjectThreadUntil(CommandInterpreter &interpreter)
1099 : CommandObjectParsed(interpreter, "thread until", "Continue until a line number or address is reached by the "
1100 "current or specified thread. Stops when returning from "
1101 "the current function as a safety measure.",
1102 nullptr, eCommandRequiresThread | eCommandTryTargetAPILock |
1103 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
1104 m_options(interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001105 {
Caroline Tice405fe672010-10-04 22:28:36 +00001106 CommandArgumentEntry arg;
1107 CommandArgumentData line_num_arg;
1108
1109 // Define the first (and only) variant of this arg.
1110 line_num_arg.arg_type = eArgTypeLineNum;
1111 line_num_arg.arg_repetition = eArgRepeatPlain;
1112
1113 // There is only one variant this argument could be; put it into the argument entry.
1114 arg.push_back (line_num_arg);
1115
1116 // Push the data for the first argument into the m_arguments vector.
1117 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118 }
1119
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001120 ~CommandObjectThreadUntil() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001123 GetOptions () override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001124 {
1125 return &m_options;
1126 }
1127
Jim Ingham5a988412012-06-08 21:56:10 +00001128protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001129 bool
1130 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131 {
Greg Claytona7015092010-09-18 01:14:36 +00001132 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133
Greg Claytona7015092010-09-18 01:14:36 +00001134 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001135 if (target == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001136 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00001137 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138 result.SetStatus (eReturnStatusFailed);
1139 return false;
1140 }
1141
Greg Claytonf9fc6092013-01-09 19:44:40 +00001142 Process *process = m_exe_ctx.GetProcessPtr();
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001143 if (process == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 {
1145 result.AppendError ("need a valid process to step");
1146 result.SetStatus (eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147 }
1148 else
1149 {
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001150 Thread *thread = nullptr;
Jim Ingham9bdea542015-02-06 02:10:56 +00001151 std::vector<uint32_t> line_numbers;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001152
Jim Ingham9bdea542015-02-06 02:10:56 +00001153 if (command.GetArgumentCount() >= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001154 {
Jim Ingham9bdea542015-02-06 02:10:56 +00001155 size_t num_args = command.GetArgumentCount();
1156 for (size_t i = 0; i < num_args; i++)
1157 {
1158 uint32_t line_number;
1159 line_number = StringConvert::ToUInt32 (command.GetArgumentAtIndex(0), UINT32_MAX);
1160 if (line_number == UINT32_MAX)
1161 {
1162 result.AppendErrorWithFormat ("invalid line number: '%s'.\n", command.GetArgumentAtIndex(0));
1163 result.SetStatus (eReturnStatusFailed);
1164 return false;
1165 }
1166 else
1167 line_numbers.push_back(line_number);
1168 }
1169 }
1170 else if (m_options.m_until_addrs.empty())
1171 {
1172 result.AppendErrorWithFormat ("No line number or address provided:\n%s", GetSyntax());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173 result.SetStatus (eReturnStatusFailed);
1174 return false;
1175 }
1176
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177 if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID)
1178 {
Jim Ingham8d94ba02016-03-12 02:45:34 +00001179 thread = GetDefaultThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180 }
1181 else
1182 {
Greg Clayton76927ee2012-05-31 00:29:20 +00001183 thread = process->GetThreadList().FindThreadByIndexID(m_options.m_thread_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001184 }
1185
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001186 if (thread == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187 {
1188 const uint32_t num_threads = process->GetThreadList().GetSize();
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001189 result.AppendErrorWithFormat ("Thread index %u is out of range (valid values are 0 - %u).\n",
1190 m_options.m_thread_idx,
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001191 num_threads);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192 result.SetStatus (eReturnStatusFailed);
1193 return false;
1194 }
1195
Jim Ingham7ba6e992012-05-11 23:47:32 +00001196 const bool abort_other_plans = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001197
Jason Molendab57e4a12013-11-04 09:33:30 +00001198 StackFrame *frame = thread->GetStackFrameAtIndex(m_options.m_frame_idx).get();
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001199 if (frame == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001200 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001201 result.AppendErrorWithFormat ("Frame index %u is out of range for thread %u.\n",
1202 m_options.m_frame_idx,
1203 m_options.m_thread_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001204 result.SetStatus (eReturnStatusFailed);
1205 return false;
1206 }
1207
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001208 ThreadPlanSP new_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209
1210 if (frame->HasDebugInformation ())
1211 {
1212 // Finally we got here... Translate the given line number to a bunch of addresses:
1213 SymbolContext sc(frame->GetSymbolContext (eSymbolContextCompUnit));
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001214 LineTable *line_table = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215 if (sc.comp_unit)
1216 line_table = sc.comp_unit->GetLineTable();
1217
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001218 if (line_table == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001219 {
1220 result.AppendErrorWithFormat ("Failed to resolve the line table for frame %u of thread index %u.\n",
1221 m_options.m_frame_idx, m_options.m_thread_idx);
1222 result.SetStatus (eReturnStatusFailed);
1223 return false;
1224 }
1225
1226 LineEntry function_start;
1227 uint32_t index_ptr = 0, end_ptr;
1228 std::vector<addr_t> address_list;
1229
1230 // Find the beginning & end index of the
1231 AddressRange fun_addr_range = sc.function->GetAddressRange();
1232 Address fun_start_addr = fun_addr_range.GetBaseAddress();
1233 line_table->FindLineEntryByAddress (fun_start_addr, function_start, &index_ptr);
1234
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001235 Address fun_end_addr(fun_start_addr.GetSection(),
1236 fun_start_addr.GetOffset() + fun_addr_range.GetByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001238 bool all_in_function = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239
Jim Ingham9bdea542015-02-06 02:10:56 +00001240 line_table->FindLineEntryByAddress (fun_end_addr, function_start, &end_ptr);
1241
1242 for (uint32_t line_number : line_numbers)
1243 {
1244 uint32_t start_idx_ptr = index_ptr;
1245 while (start_idx_ptr <= end_ptr)
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001246 {
Jim Ingham9bdea542015-02-06 02:10:56 +00001247 LineEntry line_entry;
1248 const bool exact = false;
1249 start_idx_ptr = sc.comp_unit->FindLineEntry(start_idx_ptr, line_number, sc.comp_unit, exact, &line_entry);
1250 if (start_idx_ptr == UINT32_MAX)
1251 break;
1252
1253 addr_t address = line_entry.range.GetBaseAddress().GetLoadAddress(target);
1254 if (address != LLDB_INVALID_ADDRESS)
1255 {
1256 if (fun_addr_range.ContainsLoadAddress (address, target))
1257 address_list.push_back (address);
1258 else
1259 all_in_function = false;
1260 }
1261 start_idx_ptr++;
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001262 }
Jim Ingham9bdea542015-02-06 02:10:56 +00001263 }
1264
1265 for (lldb::addr_t address : m_options.m_until_addrs)
1266 {
1267 if (fun_addr_range.ContainsLoadAddress (address, target))
1268 address_list.push_back (address);
1269 else
1270 all_in_function = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001271 }
1272
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001273 if (address_list.empty())
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001274 {
1275 if (all_in_function)
1276 result.AppendErrorWithFormat ("No line entries matching until target.\n");
1277 else
1278 result.AppendErrorWithFormat ("Until target outside of the current function.\n");
1279
1280 result.SetStatus (eReturnStatusFailed);
1281 return false;
1282 }
1283
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001284 new_plan_sp = thread->QueueThreadPlanForStepUntil (abort_other_plans,
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001285 &address_list.front(),
1286 address_list.size(),
1287 m_options.m_stop_others,
Jim Inghamf76ab672012-09-14 20:48:14 +00001288 m_options.m_frame_idx);
Jim Ingham64e7ead2012-05-03 21:19:36 +00001289 // User level plans should be master plans so they can be interrupted (e.g. by hitting a breakpoint)
1290 // and other plans executed by the user (stepping around the breakpoint) and then a "continue"
1291 // will resume the original plan.
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001292 new_plan_sp->SetIsMasterPlan (true);
1293 new_plan_sp->SetOkayToDiscard(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001294 }
1295 else
1296 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001297 result.AppendErrorWithFormat ("Frame index %u of thread %u has no debug information.\n",
1298 m_options.m_frame_idx,
1299 m_options.m_thread_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001300 result.SetStatus (eReturnStatusFailed);
1301 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001302 }
1303
Jim Ingham2976d002010-08-26 21:32:51 +00001304 process->GetThreadList().SetSelectedThreadByID (m_options.m_thread_idx);
Greg Claytondc6224e2014-10-21 01:00:42 +00001305
1306 StreamString stream;
1307 Error error;
1308 if (synchronous_execution)
1309 error = process->ResumeSynchronous (&stream);
1310 else
1311 error = process->Resume ();
1312
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001313 if (error.Success())
1314 {
Daniel Malead01b2952012-11-29 21:49:15 +00001315 result.AppendMessageWithFormat ("Process %" PRIu64 " resuming\n", process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316 if (synchronous_execution)
1317 {
Greg Claytondc6224e2014-10-21 01:00:42 +00001318 // If any state changed events had anything to say, add that to the result
1319 if (stream.GetData())
1320 result.AppendMessage(stream.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321
1322 result.SetDidChangeProcessState (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001323 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1324 }
1325 else
1326 {
1327 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
1328 }
1329 }
1330 else
1331 {
1332 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
1333 result.SetStatus (eReturnStatusFailed);
1334 }
1335
1336 }
1337 return result.Succeeded();
1338 }
Jim Ingham5a988412012-06-08 21:56:10 +00001339
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001340 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341};
1342
Greg Claytone0d378b2011-03-24 21:19:54 +00001343OptionDefinition
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001344CommandObjectThreadUntil::CommandOptions::g_option_table[] =
1345{
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001346{ LLDB_OPT_SET_1, false, "frame", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFrameIndex, "Frame index for until operation - defaults to 0"},
1347{ LLDB_OPT_SET_1, false, "thread", 't', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadIndex, "Thread index for the thread for until operation"},
1348{ 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"},
1349{ 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."},
1350{ 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001351};
1352
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001353//-------------------------------------------------------------------------
1354// CommandObjectThreadSelect
1355//-------------------------------------------------------------------------
1356
Jim Ingham5a988412012-06-08 21:56:10 +00001357class CommandObjectThreadSelect : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358{
1359public:
Kate Stone7428a182016-07-14 22:03:10 +00001360 CommandObjectThreadSelect(CommandInterpreter &interpreter)
1361 : CommandObjectParsed(interpreter, "thread select", "Change the currently selected thread.", nullptr,
1362 eCommandRequiresProcess | eCommandTryTargetAPILock | eCommandProcessMustBeLaunched |
1363 eCommandProcessMustBePaused)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001364 {
Caroline Tice405fe672010-10-04 22:28:36 +00001365 CommandArgumentEntry arg;
1366 CommandArgumentData thread_idx_arg;
1367
1368 // Define the first (and only) variant of this arg.
1369 thread_idx_arg.arg_type = eArgTypeThreadIndex;
1370 thread_idx_arg.arg_repetition = eArgRepeatPlain;
1371
1372 // There is only one variant this argument could be; put it into the argument entry.
1373 arg.push_back (thread_idx_arg);
1374
1375 // Push the data for the first argument into the m_arguments vector.
1376 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001377 }
1378
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001379 ~CommandObjectThreadSelect() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001380
Jim Ingham5a988412012-06-08 21:56:10 +00001381protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001382 bool
1383 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001384 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001385 Process *process = m_exe_ctx.GetProcessPtr();
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001386 if (process == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001387 {
1388 result.AppendError ("no process");
1389 result.SetStatus (eReturnStatusFailed);
1390 return false;
1391 }
1392 else if (command.GetArgumentCount() != 1)
1393 {
Jason Molendafd54b362011-09-20 21:44:10 +00001394 result.AppendErrorWithFormat("'%s' takes exactly one thread index argument:\nUsage: %s\n", m_cmd_name.c_str(), m_cmd_syntax.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395 result.SetStatus (eReturnStatusFailed);
1396 return false;
1397 }
1398
Vince Harron5275aaa2015-01-15 20:08:35 +00001399 uint32_t index_id = StringConvert::ToUInt32(command.GetArgumentAtIndex(0), 0, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001400
1401 Thread *new_thread = process->GetThreadList().FindThreadByIndexID(index_id).get();
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001402 if (new_thread == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001403 {
Greg Clayton86edbf42011-10-26 00:56:27 +00001404 result.AppendErrorWithFormat ("invalid thread #%s.\n", command.GetArgumentAtIndex(0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405 result.SetStatus (eReturnStatusFailed);
1406 return false;
1407 }
1408
Jim Inghamc3faa192012-12-11 02:31:48 +00001409 process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true);
Johnny Chenc13ee522010-09-14 00:53:53 +00001410 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412 return result.Succeeded();
1413 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414};
1415
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001416//-------------------------------------------------------------------------
1417// CommandObjectThreadList
1418//-------------------------------------------------------------------------
1419
Jim Ingham5a988412012-06-08 21:56:10 +00001420class CommandObjectThreadList : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001421{
Greg Clayton66111032010-06-23 01:19:29 +00001422public:
Kate Stone7428a182016-07-14 22:03:10 +00001423 CommandObjectThreadList(CommandInterpreter &interpreter)
1424 : CommandObjectParsed(interpreter, "thread list",
1425 "Show a summary of each thread in the current target process.", "thread list",
1426 eCommandRequiresProcess | eCommandTryTargetAPILock | eCommandProcessMustBeLaunched |
1427 eCommandProcessMustBePaused)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428 {
Greg Clayton66111032010-06-23 01:19:29 +00001429 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001430
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001431 ~CommandObjectThreadList() override = default;
Greg Clayton66111032010-06-23 01:19:29 +00001432
Jim Ingham5a988412012-06-08 21:56:10 +00001433protected:
Greg Clayton66111032010-06-23 01:19:29 +00001434 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001435 DoExecute (Args& command, CommandReturnObject &result) override
Greg Clayton66111032010-06-23 01:19:29 +00001436 {
Jim Ingham85e8b812011-02-19 02:53:09 +00001437 Stream &strm = result.GetOutputStream();
Greg Clayton66111032010-06-23 01:19:29 +00001438 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonf9fc6092013-01-09 19:44:40 +00001439 Process *process = m_exe_ctx.GetProcessPtr();
1440 const bool only_threads_with_stop_reason = false;
1441 const uint32_t start_frame = 0;
1442 const uint32_t num_frames = 0;
1443 const uint32_t num_frames_with_source = 0;
1444 process->GetStatus(strm);
1445 process->GetThreadStatus (strm,
1446 only_threads_with_stop_reason,
1447 start_frame,
1448 num_frames,
1449 num_frames_with_source);
Greg Clayton66111032010-06-23 01:19:29 +00001450 return result.Succeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451 }
Greg Clayton66111032010-06-23 01:19:29 +00001452};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001453
Jim Ingham93208b82013-01-31 21:46:01 +00001454//-------------------------------------------------------------------------
Jason Molenda705b1802014-06-13 02:37:02 +00001455// CommandObjectThreadInfo
1456//-------------------------------------------------------------------------
1457
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001458class CommandObjectThreadInfo : public CommandObjectIterateOverThreads
Jason Molenda705b1802014-06-13 02:37:02 +00001459{
1460public:
Jason Molenda705b1802014-06-13 02:37:02 +00001461 class CommandOptions : public Options
1462 {
1463 public:
Jason Molenda705b1802014-06-13 02:37:02 +00001464 CommandOptions (CommandInterpreter &interpreter) :
1465 Options (interpreter)
1466 {
1467 OptionParsingStarting ();
1468 }
1469
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001470 ~CommandOptions() override = default;
1471
Jason Molenda705b1802014-06-13 02:37:02 +00001472 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001473 OptionParsingStarting () override
Jason Molenda705b1802014-06-13 02:37:02 +00001474 {
Kuba Breckaafdf8422014-10-10 23:43:03 +00001475 m_json_thread = false;
1476 m_json_stopinfo = false;
Jason Molenda705b1802014-06-13 02:37:02 +00001477 }
1478
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001479 Error
1480 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Jason Molenda705b1802014-06-13 02:37:02 +00001481 {
1482 const int short_option = m_getopt_table[option_idx].val;
1483 Error error;
1484
1485 switch (short_option)
1486 {
1487 case 'j':
Kuba Breckaafdf8422014-10-10 23:43:03 +00001488 m_json_thread = true;
1489 break;
1490
1491 case 's':
1492 m_json_stopinfo = true;
Jason Molenda705b1802014-06-13 02:37:02 +00001493 break;
1494
Kuba Breckaafdf8422014-10-10 23:43:03 +00001495 default:
Jason Molenda705b1802014-06-13 02:37:02 +00001496 return Error("invalid short option character '%c'", short_option);
Jason Molenda705b1802014-06-13 02:37:02 +00001497 }
1498 return error;
1499 }
1500
1501 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001502 GetDefinitions () override
Jason Molenda705b1802014-06-13 02:37:02 +00001503 {
1504 return g_option_table;
1505 }
1506
Kuba Breckaafdf8422014-10-10 23:43:03 +00001507 bool m_json_thread;
1508 bool m_json_stopinfo;
Jason Molenda705b1802014-06-13 02:37:02 +00001509
1510 static OptionDefinition g_option_table[];
1511 };
1512
Kate Stone7428a182016-07-14 22:03:10 +00001513 CommandObjectThreadInfo(CommandInterpreter &interpreter)
1514 : CommandObjectIterateOverThreads(
1515 interpreter, "thread info",
1516 "Show an extended summary of one or more threads. Defaults to the current thread.", "thread info",
1517 eCommandRequiresProcess | eCommandTryTargetAPILock | eCommandProcessMustBeLaunched |
1518 eCommandProcessMustBePaused),
1519 m_options(interpreter)
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001520 {
1521 m_add_return = false;
1522 }
1523
1524 ~CommandObjectThreadInfo() override = default;
1525
Jason Molenda705b1802014-06-13 02:37:02 +00001526 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001527 GetOptions () override
Jason Molenda705b1802014-06-13 02:37:02 +00001528 {
1529 return &m_options;
1530 }
1531
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001532 bool
Stephane Sezerf8104912016-03-17 18:52:41 +00001533 HandleOneThread (lldb::tid_t tid, CommandReturnObject &result) override
Jason Molenda705b1802014-06-13 02:37:02 +00001534 {
Stephane Sezerf8104912016-03-17 18:52:41 +00001535 ThreadSP thread_sp = m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
1536 if (!thread_sp)
Jason Molenda705b1802014-06-13 02:37:02 +00001537 {
Stephane Sezerf8104912016-03-17 18:52:41 +00001538 result.AppendErrorWithFormat ("thread no longer exists: 0x%" PRIx64 "\n", tid);
1539 result.SetStatus (eReturnStatusFailed);
1540 return false;
1541 }
1542
1543 Thread *thread = thread_sp.get();
1544
1545 Stream &strm = result.GetOutputStream();
1546 if (!thread->GetDescription (strm, eDescriptionLevelFull, m_options.m_json_thread, m_options.m_json_stopinfo))
1547 {
1548 result.AppendErrorWithFormat ("error displaying info for thread: \"%d\"\n", thread->GetIndexID());
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001549 result.SetStatus (eReturnStatusFailed);
1550 return false;
Jason Molenda705b1802014-06-13 02:37:02 +00001551 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001552 return true;
Jason Molenda705b1802014-06-13 02:37:02 +00001553 }
1554
1555 CommandOptions m_options;
Jason Molenda705b1802014-06-13 02:37:02 +00001556};
1557
1558OptionDefinition
1559CommandObjectThreadInfo::CommandOptions::g_option_table[] =
1560{
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001561 { LLDB_OPT_SET_ALL, false, "json",'j', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the thread info in JSON format."},
1562 { LLDB_OPT_SET_ALL, false, "stop-info",'s', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the extended stop info in JSON format."},
Jason Molenda705b1802014-06-13 02:37:02 +00001563
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001564 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Jason Molenda705b1802014-06-13 02:37:02 +00001565};
1566
Jason Molenda705b1802014-06-13 02:37:02 +00001567//-------------------------------------------------------------------------
Jim Ingham93208b82013-01-31 21:46:01 +00001568// CommandObjectThreadReturn
1569//-------------------------------------------------------------------------
1570
Jim Inghamcb640dd2012-09-14 02:14:15 +00001571class CommandObjectThreadReturn : public CommandObjectRaw
1572{
1573public:
Jim Ingham93208b82013-01-31 21:46:01 +00001574 class CommandOptions : public Options
1575 {
1576 public:
Jim Ingham93208b82013-01-31 21:46:01 +00001577 CommandOptions (CommandInterpreter &interpreter) :
1578 Options (interpreter),
1579 m_from_expression (false)
1580 {
1581 // Keep default values of all options in one place: OptionParsingStarting ()
1582 OptionParsingStarting ();
1583 }
1584
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001585 ~CommandOptions() override = default;
Jim Ingham93208b82013-01-31 21:46:01 +00001586
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001587 Error
1588 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Jim Ingham93208b82013-01-31 21:46:01 +00001589 {
1590 Error error;
1591 const int short_option = m_getopt_table[option_idx].val;
1592
1593 switch (short_option)
1594 {
1595 case 'x':
1596 {
1597 bool success;
1598 bool tmp_value = Args::StringToBoolean (option_arg, false, &success);
1599 if (success)
1600 m_from_expression = tmp_value;
1601 else
1602 {
1603 error.SetErrorStringWithFormat ("invalid boolean value '%s' for 'x' option", option_arg);
1604 }
1605 }
1606 break;
1607 default:
1608 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
1609 break;
Jim Ingham93208b82013-01-31 21:46:01 +00001610 }
1611 return error;
1612 }
1613
1614 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001615 OptionParsingStarting () override
Jim Ingham93208b82013-01-31 21:46:01 +00001616 {
1617 m_from_expression = false;
1618 }
1619
1620 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001621 GetDefinitions () override
Jim Ingham93208b82013-01-31 21:46:01 +00001622 {
1623 return g_option_table;
1624 }
1625
1626 bool m_from_expression;
1627
1628 // Options table: Required for subclasses of Options.
1629
1630 static OptionDefinition g_option_table[];
1631
1632 // Instance variables to hold the values for command options.
1633 };
1634
Kate Stone7428a182016-07-14 22:03:10 +00001635 CommandObjectThreadReturn(CommandInterpreter &interpreter)
1636 : CommandObjectRaw(interpreter, "thread return",
1637 "Prematurely return from a stack frame, short-circuiting execution of newer frames "
1638 "and optionally yielding a specified value. Defaults to the exiting the current stack "
1639 "frame.",
1640 "thread return", eCommandRequiresFrame | eCommandTryTargetAPILock |
1641 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
1642 m_options(interpreter)
Jim Inghamcb640dd2012-09-14 02:14:15 +00001643 {
1644 CommandArgumentEntry arg;
1645 CommandArgumentData expression_arg;
1646
1647 // Define the first (and only) variant of this arg.
1648 expression_arg.arg_type = eArgTypeExpression;
Jim Ingham93208b82013-01-31 21:46:01 +00001649 expression_arg.arg_repetition = eArgRepeatOptional;
Jim Inghamcb640dd2012-09-14 02:14:15 +00001650
1651 // There is only one variant this argument could be; put it into the argument entry.
1652 arg.push_back (expression_arg);
1653
1654 // Push the data for the first argument into the m_arguments vector.
1655 m_arguments.push_back (arg);
Jim Inghamcb640dd2012-09-14 02:14:15 +00001656 }
Jim Inghamcb640dd2012-09-14 02:14:15 +00001657
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001658 ~CommandObjectThreadReturn() override = default;
1659
1660 Options *
1661 GetOptions() override
1662 {
1663 return &m_options;
1664 }
1665
1666protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001667 bool
1668 DoExecute (const char *command, CommandReturnObject &result) override
Jim Inghamcb640dd2012-09-14 02:14:15 +00001669 {
Jim Ingham93208b82013-01-31 21:46:01 +00001670 // I am going to handle this by hand, because I don't want you to have to say:
1671 // "thread return -- -5".
1672 if (command[0] == '-' && command[1] == 'x')
1673 {
1674 if (command && command[2] != '\0')
1675 result.AppendWarning("Return values ignored when returning from user called expressions");
1676
1677 Thread *thread = m_exe_ctx.GetThreadPtr();
1678 Error error;
1679 error = thread->UnwindInnermostExpression();
1680 if (!error.Success())
1681 {
1682 result.AppendErrorWithFormat ("Unwinding expression failed - %s.", error.AsCString());
1683 result.SetStatus (eReturnStatusFailed);
1684 }
1685 else
1686 {
1687 bool success = thread->SetSelectedFrameByIndexNoisily (0, result.GetOutputStream());
1688 if (success)
1689 {
1690 m_exe_ctx.SetFrameSP(thread->GetSelectedFrame ());
1691 result.SetStatus (eReturnStatusSuccessFinishResult);
1692 }
1693 else
1694 {
1695 result.AppendErrorWithFormat ("Could not select 0th frame after unwinding expression.");
1696 result.SetStatus (eReturnStatusFailed);
1697 }
1698 }
1699 return result.Succeeded();
1700 }
1701
Jim Inghamcb640dd2012-09-14 02:14:15 +00001702 ValueObjectSP return_valobj_sp;
1703
Jason Molendab57e4a12013-11-04 09:33:30 +00001704 StackFrameSP frame_sp = m_exe_ctx.GetFrameSP();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001705 uint32_t frame_idx = frame_sp->GetFrameIndex();
1706
1707 if (frame_sp->IsInlined())
1708 {
1709 result.AppendError("Don't know how to return from inlined frames.");
1710 result.SetStatus (eReturnStatusFailed);
1711 return false;
1712 }
1713
1714 if (command && command[0] != '\0')
1715 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001716 Target *target = m_exe_ctx.GetTargetPtr();
Jim Ingham35e1bda2012-10-16 21:41:58 +00001717 EvaluateExpressionOptions options;
Jim Inghamcb640dd2012-09-14 02:14:15 +00001718
1719 options.SetUnwindOnError(true);
1720 options.SetUseDynamic(eNoDynamicValues);
1721
Jim Ingham8646d3c2014-05-05 02:47:44 +00001722 ExpressionResults exe_results = eExpressionSetupError;
Jim Inghamcb640dd2012-09-14 02:14:15 +00001723 exe_results = target->EvaluateExpression (command,
1724 frame_sp.get(),
1725 return_valobj_sp,
1726 options);
Jim Ingham8646d3c2014-05-05 02:47:44 +00001727 if (exe_results != eExpressionCompleted)
Jim Inghamcb640dd2012-09-14 02:14:15 +00001728 {
1729 if (return_valobj_sp)
1730 result.AppendErrorWithFormat("Error evaluating result expression: %s", return_valobj_sp->GetError().AsCString());
1731 else
1732 result.AppendErrorWithFormat("Unknown error evaluating result expression.");
1733 result.SetStatus (eReturnStatusFailed);
1734 return false;
Jim Inghamcb640dd2012-09-14 02:14:15 +00001735 }
1736 }
1737
1738 Error error;
Greg Claytonf9fc6092013-01-09 19:44:40 +00001739 ThreadSP thread_sp = m_exe_ctx.GetThreadSP();
Jim Ingham4f465cf2012-10-10 18:32:14 +00001740 const bool broadcast = true;
1741 error = thread_sp->ReturnFromFrame (frame_sp, return_valobj_sp, broadcast);
Jim Inghamcb640dd2012-09-14 02:14:15 +00001742 if (!error.Success())
1743 {
1744 result.AppendErrorWithFormat("Error returning from frame %d of thread %d: %s.", frame_idx, thread_sp->GetIndexID(), error.AsCString());
1745 result.SetStatus (eReturnStatusFailed);
1746 return false;
1747 }
1748
Jim Inghamcb640dd2012-09-14 02:14:15 +00001749 result.SetStatus (eReturnStatusSuccessFinishResult);
1750 return true;
1751 }
Jim Ingham93208b82013-01-31 21:46:01 +00001752
1753 CommandOptions m_options;
Jim Inghamcb640dd2012-09-14 02:14:15 +00001754};
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001755
Jim Ingham93208b82013-01-31 21:46:01 +00001756OptionDefinition
1757CommandObjectThreadReturn::CommandOptions::g_option_table[] =
1758{
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001759{ LLDB_OPT_SET_ALL, false, "from-expression", 'x', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Return from the innermost expression evaluation."},
1760{ 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Jim Ingham93208b82013-01-31 21:46:01 +00001761};
Jim Inghamcb640dd2012-09-14 02:14:15 +00001762
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001763//-------------------------------------------------------------------------
Richard Mittonf86248d2013-09-12 02:20:34 +00001764// CommandObjectThreadJump
1765//-------------------------------------------------------------------------
1766
1767class CommandObjectThreadJump : public CommandObjectParsed
1768{
1769public:
1770 class CommandOptions : public Options
1771 {
1772 public:
Richard Mittonf86248d2013-09-12 02:20:34 +00001773 CommandOptions (CommandInterpreter &interpreter) :
1774 Options (interpreter)
1775 {
1776 OptionParsingStarting ();
1777 }
1778
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001779 ~CommandOptions() override = default;
1780
Richard Mittonf86248d2013-09-12 02:20:34 +00001781 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001782 OptionParsingStarting () override
Richard Mittonf86248d2013-09-12 02:20:34 +00001783 {
1784 m_filenames.Clear();
1785 m_line_num = 0;
1786 m_line_offset = 0;
1787 m_load_addr = LLDB_INVALID_ADDRESS;
1788 m_force = false;
1789 }
1790
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001791 Error
1792 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Richard Mittonf86248d2013-09-12 02:20:34 +00001793 {
1794 bool success;
1795 const int short_option = m_getopt_table[option_idx].val;
1796 Error error;
1797
1798 switch (short_option)
1799 {
1800 case 'f':
1801 m_filenames.AppendIfUnique (FileSpec(option_arg, false));
1802 if (m_filenames.GetSize() > 1)
1803 return Error("only one source file expected.");
1804 break;
1805 case 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00001806 m_line_num = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
Richard Mittonf86248d2013-09-12 02:20:34 +00001807 if (!success || m_line_num == 0)
1808 return Error("invalid line number: '%s'.", option_arg);
1809 break;
1810 case 'b':
Vince Harron5275aaa2015-01-15 20:08:35 +00001811 m_line_offset = StringConvert::ToSInt32 (option_arg, 0, 0, &success);
Richard Mittonf86248d2013-09-12 02:20:34 +00001812 if (!success)
1813 return Error("invalid line offset: '%s'.", option_arg);
1814 break;
1815 case 'a':
1816 {
1817 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
1818 m_load_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
1819 }
1820 break;
1821 case 'r':
1822 m_force = true;
1823 break;
Richard Mittonf86248d2013-09-12 02:20:34 +00001824 default:
1825 return Error("invalid short option character '%c'", short_option);
Richard Mittonf86248d2013-09-12 02:20:34 +00001826 }
1827 return error;
1828 }
1829
1830 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001831 GetDefinitions () override
Richard Mittonf86248d2013-09-12 02:20:34 +00001832 {
1833 return g_option_table;
1834 }
1835
1836 FileSpecList m_filenames;
1837 uint32_t m_line_num;
1838 int32_t m_line_offset;
1839 lldb::addr_t m_load_addr;
1840 bool m_force;
1841
1842 static OptionDefinition g_option_table[];
1843 };
1844
Richard Mittonf86248d2013-09-12 02:20:34 +00001845 CommandObjectThreadJump (CommandInterpreter &interpreter) :
1846 CommandObjectParsed (interpreter,
1847 "thread jump",
1848 "Sets the program counter to a new address.",
1849 "thread jump",
Enrico Granatae87764f2015-05-27 05:04:35 +00001850 eCommandRequiresFrame |
1851 eCommandTryTargetAPILock |
1852 eCommandProcessMustBeLaunched |
1853 eCommandProcessMustBePaused ),
Richard Mittonf86248d2013-09-12 02:20:34 +00001854 m_options (interpreter)
1855 {
1856 }
1857
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001858 ~CommandObjectThreadJump() override = default;
1859
1860 Options *
1861 GetOptions() override
Richard Mittonf86248d2013-09-12 02:20:34 +00001862 {
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001863 return &m_options;
Richard Mittonf86248d2013-09-12 02:20:34 +00001864 }
1865
1866protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001867 bool DoExecute (Args& args, CommandReturnObject &result) override
Richard Mittonf86248d2013-09-12 02:20:34 +00001868 {
1869 RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00001870 StackFrame *frame = m_exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00001871 Thread *thread = m_exe_ctx.GetThreadPtr();
1872 Target *target = m_exe_ctx.GetTargetPtr();
1873 const SymbolContext &sym_ctx = frame->GetSymbolContext (eSymbolContextLineEntry);
1874
1875 if (m_options.m_load_addr != LLDB_INVALID_ADDRESS)
1876 {
1877 // Use this address directly.
1878 Address dest = Address(m_options.m_load_addr);
1879
1880 lldb::addr_t callAddr = dest.GetCallableLoadAddress (target);
1881 if (callAddr == LLDB_INVALID_ADDRESS)
1882 {
1883 result.AppendErrorWithFormat ("Invalid destination address.");
1884 result.SetStatus (eReturnStatusFailed);
1885 return false;
1886 }
1887
1888 if (!reg_ctx->SetPC (callAddr))
1889 {
1890 result.AppendErrorWithFormat ("Error changing PC value for thread %d.", thread->GetIndexID());
1891 result.SetStatus (eReturnStatusFailed);
1892 return false;
1893 }
1894 }
1895 else
1896 {
1897 // Pick either the absolute line, or work out a relative one.
1898 int32_t line = (int32_t)m_options.m_line_num;
1899 if (line == 0)
1900 line = sym_ctx.line_entry.line + m_options.m_line_offset;
1901
1902 // Try the current file, but override if asked.
1903 FileSpec file = sym_ctx.line_entry.file;
1904 if (m_options.m_filenames.GetSize() == 1)
1905 file = m_options.m_filenames.GetFileSpecAtIndex(0);
1906
1907 if (!file)
1908 {
1909 result.AppendErrorWithFormat ("No source file available for the current location.");
1910 result.SetStatus (eReturnStatusFailed);
1911 return false;
1912 }
1913
1914 std::string warnings;
1915 Error err = thread->JumpToLine (file, line, m_options.m_force, &warnings);
1916
1917 if (err.Fail())
1918 {
1919 result.SetError (err);
1920 return false;
1921 }
1922
1923 if (!warnings.empty())
1924 result.AppendWarning (warnings.c_str());
1925 }
1926
1927 result.SetStatus (eReturnStatusSuccessFinishResult);
1928 return true;
1929 }
1930
1931 CommandOptions m_options;
1932};
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001933
Richard Mittonf86248d2013-09-12 02:20:34 +00001934OptionDefinition
1935CommandObjectThreadJump::CommandOptions::g_option_table[] =
1936{
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001937 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
Richard Mittonf86248d2013-09-12 02:20:34 +00001938 "Specifies the source file to jump to."},
1939
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001940 { LLDB_OPT_SET_1, true, "line", 'l', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLineNum,
Richard Mittonf86248d2013-09-12 02:20:34 +00001941 "Specifies the line number to jump to."},
1942
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001943 { LLDB_OPT_SET_2, true, "by", 'b', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeOffset,
Richard Mittonf86248d2013-09-12 02:20:34 +00001944 "Jumps by a relative line offset from the current line."},
1945
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001946 { LLDB_OPT_SET_3, true, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression,
Richard Mittonf86248d2013-09-12 02:20:34 +00001947 "Jumps to a specific address."},
1948
1949 { LLDB_OPT_SET_1|
1950 LLDB_OPT_SET_2|
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001951 LLDB_OPT_SET_3, false, "force",'r', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone,"Allows the PC to leave the current function."},
Richard Mittonf86248d2013-09-12 02:20:34 +00001952
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001953 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Richard Mittonf86248d2013-09-12 02:20:34 +00001954};
1955
1956//-------------------------------------------------------------------------
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001957// Next are the subcommands of CommandObjectMultiwordThreadPlan
1958//-------------------------------------------------------------------------
1959
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001960//-------------------------------------------------------------------------
1961// CommandObjectThreadPlanList
1962//-------------------------------------------------------------------------
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001963
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001964class CommandObjectThreadPlanList : public CommandObjectIterateOverThreads
1965{
1966public:
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001967 class CommandOptions : public Options
1968 {
1969 public:
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001970 CommandOptions (CommandInterpreter &interpreter) :
1971 Options(interpreter)
1972 {
1973 // Keep default values of all options in one place: OptionParsingStarting ()
1974 OptionParsingStarting ();
1975 }
1976
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001977 ~CommandOptions() override = default;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001978
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001979 Error
1980 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001981 {
1982 Error error;
1983 const int short_option = m_getopt_table[option_idx].val;
1984
1985 switch (short_option)
1986 {
1987 case 'i':
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001988 m_internal = true;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001989 break;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001990 case 'v':
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001991 m_verbose = true;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001992 break;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001993 default:
1994 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
1995 break;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001996 }
1997 return error;
1998 }
1999
2000 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002001 OptionParsingStarting () override
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002002 {
2003 m_verbose = false;
2004 m_internal = false;
2005 }
2006
2007 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002008 GetDefinitions () override
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002009 {
2010 return g_option_table;
2011 }
2012
2013 // Options table: Required for subclasses of Options.
2014
2015 static OptionDefinition g_option_table[];
2016
2017 // Instance variables to hold the values for command options.
2018 bool m_verbose;
2019 bool m_internal;
2020 };
2021
Kate Stone7428a182016-07-14 22:03:10 +00002022 CommandObjectThreadPlanList(CommandInterpreter &interpreter)
2023 : CommandObjectIterateOverThreads(
2024 interpreter, "thread plan list",
2025 "Show thread plans for one or more threads. If no threads are specified, show the "
2026 "current thread. Use the thread-index \"all\" to see all threads.",
2027 nullptr, eCommandRequiresProcess | eCommandRequiresThread | eCommandTryTargetAPILock |
2028 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
2029 m_options(interpreter)
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002030 {
2031 }
2032
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00002033 ~CommandObjectThreadPlanList() override = default;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002034
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002035 Options *
2036 GetOptions () override
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002037 {
2038 return &m_options;
2039 }
2040
2041protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002042 bool
Stephane Sezerf8104912016-03-17 18:52:41 +00002043 HandleOneThread (lldb::tid_t tid, CommandReturnObject &result) override
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002044 {
Stephane Sezerf8104912016-03-17 18:52:41 +00002045 ThreadSP thread_sp = m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
2046 if (!thread_sp)
2047 {
2048 result.AppendErrorWithFormat ("thread no longer exists: 0x%" PRIx64 "\n", tid);
2049 result.SetStatus (eReturnStatusFailed);
2050 return false;
2051 }
2052
2053 Thread *thread = thread_sp.get();
2054
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002055 Stream &strm = result.GetOutputStream();
2056 DescriptionLevel desc_level = eDescriptionLevelFull;
2057 if (m_options.m_verbose)
2058 desc_level = eDescriptionLevelVerbose;
2059
Stephane Sezerf8104912016-03-17 18:52:41 +00002060 thread->DumpThreadPlans (&strm, desc_level, m_options.m_internal, true);
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002061 return true;
2062 }
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00002063
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002064 CommandOptions m_options;
2065};
2066
2067OptionDefinition
2068CommandObjectThreadPlanList::CommandOptions::g_option_table[] =
2069{
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00002070{ LLDB_OPT_SET_1, false, "verbose", 'v', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display more information about the thread plans"},
2071{ LLDB_OPT_SET_1, false, "internal", 'i', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display internal as well as user thread plans"},
2072{ 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002073};
2074
2075class CommandObjectThreadPlanDiscard : public CommandObjectParsed
2076{
2077public:
Kate Stone7428a182016-07-14 22:03:10 +00002078 CommandObjectThreadPlanDiscard(CommandInterpreter &interpreter)
2079 : CommandObjectParsed(
2080 interpreter, "thread plan discard",
2081 "Discards thread plans up to and including the specified index (see 'thread plan list'.) "
2082 "Only user visible plans can be discarded.",
2083 nullptr, eCommandRequiresProcess | eCommandRequiresThread | eCommandTryTargetAPILock |
2084 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused)
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002085 {
2086 CommandArgumentEntry arg;
2087 CommandArgumentData plan_index_arg;
2088
2089 // Define the first (and only) variant of this arg.
2090 plan_index_arg.arg_type = eArgTypeUnsignedInteger;
2091 plan_index_arg.arg_repetition = eArgRepeatPlain;
2092
2093 // There is only one variant this argument could be; put it into the argument entry.
2094 arg.push_back (plan_index_arg);
2095
2096 // Push the data for the first argument into the m_arguments vector.
2097 m_arguments.push_back (arg);
2098 }
2099
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00002100 ~CommandObjectThreadPlanDiscard() override = default;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002101
2102 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002103 DoExecute (Args& args, CommandReturnObject &result) override
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002104 {
2105 Thread *thread = m_exe_ctx.GetThreadPtr();
2106 if (args.GetArgumentCount() != 1)
2107 {
2108 result.AppendErrorWithFormat("Too many arguments, expected one - the thread plan index - but got %zu.",
2109 args.GetArgumentCount());
2110 result.SetStatus (eReturnStatusFailed);
2111 return false;
2112 }
2113
2114 bool success;
Vince Harron5275aaa2015-01-15 20:08:35 +00002115 uint32_t thread_plan_idx = StringConvert::ToUInt32(args.GetArgumentAtIndex(0), 0, 0, &success);
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002116 if (!success)
2117 {
2118 result.AppendErrorWithFormat("Invalid thread index: \"%s\" - should be unsigned int.",
2119 args.GetArgumentAtIndex(0));
2120 result.SetStatus (eReturnStatusFailed);
2121 return false;
2122 }
2123
2124 if (thread_plan_idx == 0)
2125 {
2126 result.AppendErrorWithFormat("You wouldn't really want me to discard the base thread plan.");
2127 result.SetStatus (eReturnStatusFailed);
2128 return false;
2129 }
2130
2131 if (thread->DiscardUserThreadPlansUpToIndex(thread_plan_idx))
2132 {
2133 result.SetStatus(eReturnStatusSuccessFinishNoResult);
2134 return true;
2135 }
2136 else
2137 {
2138 result.AppendErrorWithFormat("Could not find User thread plan with index %s.",
2139 args.GetArgumentAtIndex(0));
2140 result.SetStatus (eReturnStatusFailed);
2141 return false;
2142 }
2143 }
2144};
2145
2146//-------------------------------------------------------------------------
2147// CommandObjectMultiwordThreadPlan
2148//-------------------------------------------------------------------------
2149
2150class CommandObjectMultiwordThreadPlan : public CommandObjectMultiword
2151{
2152public:
Kate Stone7428a182016-07-14 22:03:10 +00002153 CommandObjectMultiwordThreadPlan(CommandInterpreter &interpreter)
2154 : CommandObjectMultiword(interpreter, "plan", "Commands for managing thread plans that control execution.",
2155 "thread plan <subcommand> [<subcommand objects]")
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002156 {
2157 LoadSubCommand ("list", CommandObjectSP (new CommandObjectThreadPlanList (interpreter)));
2158 LoadSubCommand ("discard", CommandObjectSP (new CommandObjectThreadPlanDiscard (interpreter)));
2159 }
2160
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00002161 ~CommandObjectMultiwordThreadPlan() override = default;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002162};
2163
2164//-------------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002165// CommandObjectMultiwordThread
2166//-------------------------------------------------------------------------
2167
Kate Stone7428a182016-07-14 22:03:10 +00002168CommandObjectMultiwordThread::CommandObjectMultiwordThread(CommandInterpreter &interpreter)
2169 : CommandObjectMultiword(interpreter, "thread",
2170 "Commands for operating on one or more threads in the current process.",
2171 "thread <subcommand> [<subcommand-options>]")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002172{
Greg Claytona7015092010-09-18 01:14:36 +00002173 LoadSubCommand ("backtrace", CommandObjectSP (new CommandObjectThreadBacktrace (interpreter)));
2174 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectThreadContinue (interpreter)));
2175 LoadSubCommand ("list", CommandObjectSP (new CommandObjectThreadList (interpreter)));
Jim Inghamcb640dd2012-09-14 02:14:15 +00002176 LoadSubCommand ("return", CommandObjectSP (new CommandObjectThreadReturn (interpreter)));
Richard Mittonf86248d2013-09-12 02:20:34 +00002177 LoadSubCommand ("jump", CommandObjectSP (new CommandObjectThreadJump (interpreter)));
Greg Claytona7015092010-09-18 01:14:36 +00002178 LoadSubCommand ("select", CommandObjectSP (new CommandObjectThreadSelect (interpreter)));
2179 LoadSubCommand ("until", CommandObjectSP (new CommandObjectThreadUntil (interpreter)));
Jason Molenda705b1802014-06-13 02:37:02 +00002180 LoadSubCommand ("info", CommandObjectSP (new CommandObjectThreadInfo (interpreter)));
Kate Stone7428a182016-07-14 22:03:10 +00002181 LoadSubCommand("step-in",
2182 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2183 interpreter, "thread step-in",
2184 "Source level single step, stepping into calls. Defaults to current thread unless specified.",
2185 nullptr, eStepTypeInto, eStepScopeSource)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002186
Kate Stone7428a182016-07-14 22:03:10 +00002187 LoadSubCommand("step-out",
2188 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2189 interpreter, "thread step-out", "Finish executing the current stack frame and stop after "
2190 "returning. Defaults to current thread unless specified.",
2191 nullptr, eStepTypeOut, eStepScopeSource)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002192
Kate Stone7428a182016-07-14 22:03:10 +00002193 LoadSubCommand("step-over",
2194 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2195 interpreter, "thread step-over",
2196 "Source level single step, stepping over calls. Defaults to current thread unless specified.",
2197 nullptr, eStepTypeOver, eStepScopeSource)));
Greg Clayton66111032010-06-23 01:19:29 +00002198
Kate Stone7428a182016-07-14 22:03:10 +00002199 LoadSubCommand(
2200 "step-inst",
2201 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2202 interpreter, "thread step-inst",
2203 "Instruction level single step, stepping into calls. Defaults to current thread unless specified.",
2204 nullptr, eStepTypeTrace, eStepScopeInstruction)));
2205
2206 LoadSubCommand(
2207 "step-inst-over",
2208 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2209 interpreter, "thread step-inst-over",
2210 "Instruction level single step, stepping over calls. Defaults to current thread unless specified.",
2211 nullptr, eStepTypeTraceOver, eStepScopeInstruction)));
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002212
2213 LoadSubCommand ("step-scripted", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
2214 interpreter,
2215 "thread step-scripted",
2216 "Step as instructed by the script class passed in the -C option.",
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00002217 nullptr,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002218 eStepTypeScripted,
2219 eStepScopeSource)));
2220
2221 LoadSubCommand ("plan", CommandObjectSP (new CommandObjectMultiwordThreadPlan(interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002222}
2223
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00002224CommandObjectMultiwordThread::~CommandObjectMultiwordThread() = default;