blob: b693cb37345f6d0d3832e124bc3e87b2a92db139 [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "CommandObjectThread.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
Jim Inghamcb640dd2012-09-14 02:14:15 +000018#include "lldb/lldb-private.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/State.h"
20#include "lldb/Core/SourceManager.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000021#include "lldb/Host/Host.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 +000040
41using namespace lldb;
42using namespace lldb_private;
43
44
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045//-------------------------------------------------------------------------
46// CommandObjectThreadBacktrace
47//-------------------------------------------------------------------------
48
Jim Ingham5a988412012-06-08 21:56:10 +000049class CommandObjectThreadBacktrace : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050{
51public:
52
Jim Inghame2e0b452010-08-26 23:36:03 +000053 class CommandOptions : public Options
54 {
55 public:
56
Greg Claytoneb0103f2011-04-07 22:46:35 +000057 CommandOptions (CommandInterpreter &interpreter) :
58 Options(interpreter)
Jim Inghame2e0b452010-08-26 23:36:03 +000059 {
Greg Claytonf6b8b582011-04-13 00:18:08 +000060 // Keep default values of all options in one place: OptionParsingStarting ()
61 OptionParsingStarting ();
Jim Inghame2e0b452010-08-26 23:36:03 +000062 }
63
64 virtual
65 ~CommandOptions ()
66 {
67 }
68
69 virtual Error
Greg Claytonf6b8b582011-04-13 00:18:08 +000070 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghame2e0b452010-08-26 23:36:03 +000071 {
72 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +000073 const int short_option = m_getopt_table[option_idx].val;
Jim Inghame2e0b452010-08-26 23:36:03 +000074
75 switch (short_option)
76 {
77 case 'c':
78 {
79 bool success;
80 int32_t input_count = Args::StringToSInt32 (option_arg, -1, 0, &success);
81 if (!success)
Greg Clayton86edbf42011-10-26 00:56:27 +000082 error.SetErrorStringWithFormat("invalid integer value for option '%c'", short_option);
Jim Inghame2e0b452010-08-26 23:36:03 +000083 if (input_count < -1)
84 m_count = UINT32_MAX;
85 else
86 m_count = input_count;
87 }
88 break;
89 case 's':
90 {
91 bool success;
92 m_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
93 if (!success)
Greg Clayton86edbf42011-10-26 00:56:27 +000094 error.SetErrorStringWithFormat("invalid integer value for option '%c'", short_option);
Jim Inghame2e0b452010-08-26 23:36:03 +000095 }
Jason Molenda750ea692013-11-12 07:02:07 +000096 case 'e':
97 {
98 bool success;
99 m_extended_backtrace = Args::StringToBoolean (option_arg, false, &success);
100 if (!success)
101 error.SetErrorStringWithFormat("invalid boolean value for option '%c'", short_option);
102 }
Jim Inghame2e0b452010-08-26 23:36:03 +0000103 break;
104 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000105 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Jim Inghame2e0b452010-08-26 23:36:03 +0000106 break;
107
108 }
109 return error;
110 }
111
112 void
Greg Claytonf6b8b582011-04-13 00:18:08 +0000113 OptionParsingStarting ()
Jim Inghame2e0b452010-08-26 23:36:03 +0000114 {
Greg Clayton7260f622011-04-18 08:33:37 +0000115 m_count = UINT32_MAX;
Jim Inghame2e0b452010-08-26 23:36:03 +0000116 m_start = 0;
Jason Molenda750ea692013-11-12 07:02:07 +0000117 m_extended_backtrace = false;
Jim Inghame2e0b452010-08-26 23:36:03 +0000118 }
119
Greg Claytone0d378b2011-03-24 21:19:54 +0000120 const OptionDefinition*
Jim Inghame2e0b452010-08-26 23:36:03 +0000121 GetDefinitions ()
122 {
123 return g_option_table;
124 }
125
126 // Options table: Required for subclasses of Options.
127
Greg Claytone0d378b2011-03-24 21:19:54 +0000128 static OptionDefinition g_option_table[];
Jim Inghame2e0b452010-08-26 23:36:03 +0000129
130 // Instance variables to hold the values for command options.
131 uint32_t m_count;
132 uint32_t m_start;
Jason Molenda750ea692013-11-12 07:02:07 +0000133 bool m_extended_backtrace;
Jim Inghame2e0b452010-08-26 23:36:03 +0000134 };
135
Greg Claytona7015092010-09-18 01:14:36 +0000136 CommandObjectThreadBacktrace (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000137 CommandObjectParsed (interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000138 "thread backtrace",
139 "Show the stack for one or more threads. If no threads are specified, show the currently selected thread. Use the thread-index \"all\" to see all threads.",
140 NULL,
141 eFlagRequiresProcess |
142 eFlagRequiresThread |
143 eFlagTryTargetAPILock |
144 eFlagProcessMustBeLaunched |
145 eFlagProcessMustBePaused ),
Greg Claytoneb0103f2011-04-07 22:46:35 +0000146 m_options(interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147 {
Caroline Tice405fe672010-10-04 22:28:36 +0000148 CommandArgumentEntry arg;
149 CommandArgumentData thread_idx_arg;
150
151 // Define the first (and only) variant of this arg.
152 thread_idx_arg.arg_type = eArgTypeThreadIndex;
153 thread_idx_arg.arg_repetition = eArgRepeatStar;
154
155 // There is only one variant this argument could be; put it into the argument entry.
156 arg.push_back (thread_idx_arg);
157
158 // Push the data for the first argument into the m_arguments vector.
159 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160 }
161
162 ~CommandObjectThreadBacktrace()
163 {
164 }
165
Jim Inghame2e0b452010-08-26 23:36:03 +0000166 virtual Options *
167 GetOptions ()
168 {
169 return &m_options;
170 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171
Jim Ingham5a988412012-06-08 21:56:10 +0000172protected:
Jason Molenda750ea692013-11-12 07:02:07 +0000173 void
174 DoExtendedBacktrace (Thread *thread, CommandReturnObject &result)
175 {
176 SystemRuntime *runtime = thread->GetProcess()->GetSystemRuntime();
177 if (runtime)
178 {
179 Stream &strm = result.GetOutputStream();
180 const std::vector<ConstString> &types = runtime->GetExtendedBacktraceTypes();
181 for (auto type : types)
182 {
Jason Molenda008c45f2013-11-12 23:33:32 +0000183 ThreadSP ext_thread_sp = runtime->GetExtendedBacktraceThread (thread->shared_from_this(), type);
Jason Molenda750ea692013-11-12 07:02:07 +0000184 if (ext_thread_sp && ext_thread_sp->IsValid ())
185 {
186 const uint32_t num_frames_with_source = 0;
187 if (ext_thread_sp->GetStatus (strm,
188 m_options.m_start,
189 m_options.m_count,
190 num_frames_with_source))
191 {
192 DoExtendedBacktrace (ext_thread_sp.get(), result);
193 }
194 }
195 }
196 }
197 }
198
Greg Clayton66111032010-06-23 01:19:29 +0000199 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000200 DoExecute (Args& command, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000201 {
Jim Ingham09b263e2010-08-27 00:58:05 +0000202 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000203 Stream &strm = result.GetOutputStream();
204
205 // Don't show source context when doing backtraces.
206 const uint32_t num_frames_with_source = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207 if (command.GetArgumentCount() == 0)
208 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000209 Thread *thread = m_exe_ctx.GetThreadPtr();
210 // Thread::GetStatus() returns the number of frames shown.
211 if (thread->GetStatus (strm,
212 m_options.m_start,
213 m_options.m_count,
214 num_frames_with_source))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000216 result.SetStatus (eReturnStatusSuccessFinishResult);
Jason Molenda750ea692013-11-12 07:02:07 +0000217 if (m_options.m_extended_backtrace)
218 {
219 DoExtendedBacktrace (thread, result);
220 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221 }
222 }
Jim Ingham09b263e2010-08-27 00:58:05 +0000223 else if (command.GetArgumentCount() == 1 && ::strcmp (command.GetArgumentAtIndex(0), "all") == 0)
224 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000225 Process *process = m_exe_ctx.GetProcessPtr();
Sean Callanan5c19eac2013-11-06 19:28:40 +0000226 uint32_t idx = 0;
227 for (ThreadSP thread_sp : process->Threads())
Jim Ingham09b263e2010-08-27 00:58:05 +0000228 {
Sean Callanan5c19eac2013-11-06 19:28:40 +0000229 if (idx != 0)
230 result.AppendMessage("");
231
Johnny Chenf2ddc712011-06-01 23:19:52 +0000232 if (!thread_sp->GetStatus (strm,
233 m_options.m_start,
234 m_options.m_count,
235 num_frames_with_source))
Jim Ingham09b263e2010-08-27 00:58:05 +0000236 {
Sean Callanan5c19eac2013-11-06 19:28:40 +0000237 result.AppendErrorWithFormat ("error displaying backtrace for thread: \"0x%4.4x\"\n", idx);
Jim Ingham09b263e2010-08-27 00:58:05 +0000238 result.SetStatus (eReturnStatusFailed);
239 return false;
240 }
Jason Molenda750ea692013-11-12 07:02:07 +0000241 if (m_options.m_extended_backtrace)
242 {
243 DoExtendedBacktrace (thread_sp.get(), result);
244 }
Jim Ingham5c4df7a2011-07-26 02:39:59 +0000245
Sean Callanan5c19eac2013-11-06 19:28:40 +0000246 ++idx;
Jim Ingham09b263e2010-08-27 00:58:05 +0000247 }
248 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 else
250 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000251 const size_t num_args = command.GetArgumentCount();
Greg Claytonf9fc6092013-01-09 19:44:40 +0000252 Process *process = m_exe_ctx.GetProcessPtr();
Jim Ingham41f2b942012-09-10 20:50:15 +0000253 Mutex::Locker locker (process->GetThreadList().GetMutex());
Jim Ingham09b263e2010-08-27 00:58:05 +0000254 std::vector<ThreadSP> thread_sps;
255
Greg Claytonc7bece562013-01-25 18:06:21 +0000256 for (size_t i = 0; i < num_args; i++)
Jim Ingham09b263e2010-08-27 00:58:05 +0000257 {
258 bool success;
259
260 uint32_t thread_idx = Args::StringToUInt32(command.GetArgumentAtIndex(i), 0, 0, &success);
261 if (!success)
262 {
263 result.AppendErrorWithFormat ("invalid thread specification: \"%s\"\n", command.GetArgumentAtIndex(i));
264 result.SetStatus (eReturnStatusFailed);
265 return false;
266 }
267
268 thread_sps.push_back(process->GetThreadList().FindThreadByIndexID(thread_idx));
269
270 if (!thread_sps[i])
271 {
272 result.AppendErrorWithFormat ("no thread with index: \"%s\"\n", command.GetArgumentAtIndex(i));
273 result.SetStatus (eReturnStatusFailed);
274 return false;
275 }
276
277 }
278
279 for (uint32_t i = 0; i < num_args; i++)
280 {
Greg Clayton7260f622011-04-18 08:33:37 +0000281 if (!thread_sps[i]->GetStatus (strm,
282 m_options.m_start,
283 m_options.m_count,
284 num_frames_with_source))
Jim Ingham09b263e2010-08-27 00:58:05 +0000285 {
286 result.AppendErrorWithFormat ("error displaying backtrace for thread: \"%s\"\n", command.GetArgumentAtIndex(i));
287 result.SetStatus (eReturnStatusFailed);
288 return false;
289 }
Jason Molenda750ea692013-11-12 07:02:07 +0000290 if (m_options.m_extended_backtrace)
291 {
292 DoExtendedBacktrace (thread_sps[i].get(), result);
293 }
Jim Ingham09b263e2010-08-27 00:58:05 +0000294
295 if (i < num_args - 1)
296 result.AppendMessage("");
297 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 }
299 return result.Succeeded();
300 }
Jim Ingham5a988412012-06-08 21:56:10 +0000301
Jim Inghame2e0b452010-08-26 23:36:03 +0000302 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303};
304
Greg Claytone0d378b2011-03-24 21:19:54 +0000305OptionDefinition
Jim Inghame2e0b452010-08-26 23:36:03 +0000306CommandObjectThreadBacktrace::CommandOptions::g_option_table[] =
307{
Virgile Belloe2607b52013-09-05 16:42:23 +0000308{ LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, NULL, 0, eArgTypeCount, "How many frames to display (-1 for all)"},
309{ LLDB_OPT_SET_1, false, "start", 's', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFrameIndex, "Frame in which to start the backtrace"},
Jason Molenda750ea692013-11-12 07:02:07 +0000310{ LLDB_OPT_SET_1, false, "extended", 'e', OptionParser::eRequiredArgument, NULL, 0, eArgTypeBoolean, "Show the extended backtrace, if available"},
Caroline Ticedeaab222010-10-01 19:59:14 +0000311{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Jim Inghame2e0b452010-08-26 23:36:03 +0000312};
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313
Greg Clayton69b518f2010-07-07 17:07:17 +0000314enum StepScope
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315{
316 eStepScopeSource,
317 eStepScopeInstruction
318};
319
Jim Ingham5a988412012-06-08 21:56:10 +0000320class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321{
322public:
323
324 class CommandOptions : public Options
325 {
326 public:
327
Greg Claytoneb0103f2011-04-07 22:46:35 +0000328 CommandOptions (CommandInterpreter &interpreter) :
329 Options (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330 {
Greg Claytonf6b8b582011-04-13 00:18:08 +0000331 // Keep default values of all options in one place: OptionParsingStarting ()
332 OptionParsingStarting ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333 }
334
335 virtual
336 ~CommandOptions ()
337 {
338 }
339
340 virtual Error
Greg Claytonf6b8b582011-04-13 00:18:08 +0000341 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342 {
343 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000344 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345
346 switch (short_option)
347 {
Greg Clayton8087ca22010-10-08 04:20:14 +0000348 case 'a':
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 {
350 bool success;
Jim Ingham4b4b2472014-03-13 02:47:14 +0000351 bool avoid_no_debug = Args::StringToBoolean (option_arg, true, &success);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 if (!success)
Greg Clayton86edbf42011-10-26 00:56:27 +0000353 error.SetErrorStringWithFormat("invalid boolean value for option '%c'", short_option);
Jim Ingham4b4b2472014-03-13 02:47:14 +0000354 else
355 {
356 m_step_in_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo;
357 }
358 }
359 break;
360
361 case 'A':
362 {
363 bool success;
364 bool avoid_no_debug = Args::StringToBoolean (option_arg, true, &success);
365 if (!success)
366 error.SetErrorStringWithFormat("invalid boolean value for option '%c'", short_option);
367 else
368 {
369 m_step_out_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo;
370 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 }
372 break;
Greg Clayton8087ca22010-10-08 04:20:14 +0000373
374 case 'm':
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 OptionEnumValueElement *enum_values = g_option_table[option_idx].enum_values;
Greg Claytoncf0e4f02011-10-07 18:58:12 +0000377 m_run_mode = (lldb::RunMode) Args::StringToOptionEnum(option_arg, enum_values, eOnlyDuringStepping, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 }
379 break;
Greg Clayton8087ca22010-10-08 04:20:14 +0000380
381 case 'r':
Jim Inghama56c8002010-07-10 02:27:39 +0000382 {
383 m_avoid_regexp.clear();
384 m_avoid_regexp.assign(option_arg);
385 }
386 break;
Greg Clayton8087ca22010-10-08 04:20:14 +0000387
Jim Inghamc6276822012-12-12 19:58:40 +0000388 case 't':
389 {
390 m_step_in_target.clear();
391 m_step_in_target.assign(option_arg);
392
393 }
394 break;
Greg Clayton8087ca22010-10-08 04:20:14 +0000395 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000396 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Clayton8087ca22010-10-08 04:20:14 +0000397 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398
399 }
400 return error;
401 }
402
403 void
Greg Claytonf6b8b582011-04-13 00:18:08 +0000404 OptionParsingStarting ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405 {
Jim Ingham4b4b2472014-03-13 02:47:14 +0000406 m_step_in_avoid_no_debug = eLazyBoolCalculate;
407 m_step_out_avoid_no_debug = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408 m_run_mode = eOnlyDuringStepping;
Jim Inghama56c8002010-07-10 02:27:39 +0000409 m_avoid_regexp.clear();
Jim Inghamc6276822012-12-12 19:58:40 +0000410 m_step_in_target.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 }
412
Greg Claytone0d378b2011-03-24 21:19:54 +0000413 const OptionDefinition*
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 GetDefinitions ()
415 {
416 return g_option_table;
417 }
418
419 // Options table: Required for subclasses of Options.
420
Greg Claytone0d378b2011-03-24 21:19:54 +0000421 static OptionDefinition g_option_table[];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422
423 // Instance variables to hold the values for command options.
Jim Ingham4b4b2472014-03-13 02:47:14 +0000424 LazyBool m_step_in_avoid_no_debug;
425 LazyBool m_step_out_avoid_no_debug;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 RunMode m_run_mode;
Jim Inghama56c8002010-07-10 02:27:39 +0000427 std::string m_avoid_regexp;
Jim Inghamc6276822012-12-12 19:58:40 +0000428 std::string m_step_in_target;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429 };
430
Greg Claytona7015092010-09-18 01:14:36 +0000431 CommandObjectThreadStepWithTypeAndScope (CommandInterpreter &interpreter,
432 const char *name,
433 const char *help,
434 const char *syntax,
Greg Claytona7015092010-09-18 01:14:36 +0000435 StepType step_type,
436 StepScope step_scope) :
Greg Claytonf9fc6092013-01-09 19:44:40 +0000437 CommandObjectParsed (interpreter, name, help, syntax,
438 eFlagRequiresProcess |
439 eFlagRequiresThread |
440 eFlagTryTargetAPILock |
441 eFlagProcessMustBeLaunched |
442 eFlagProcessMustBePaused ),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443 m_step_type (step_type),
444 m_step_scope (step_scope),
Greg Claytoneb0103f2011-04-07 22:46:35 +0000445 m_options (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 {
Caroline Tice405fe672010-10-04 22:28:36 +0000447 CommandArgumentEntry arg;
448 CommandArgumentData thread_id_arg;
449
450 // Define the first (and only) variant of this arg.
451 thread_id_arg.arg_type = eArgTypeThreadID;
452 thread_id_arg.arg_repetition = eArgRepeatOptional;
453
454 // There is only one variant this argument could be; put it into the argument entry.
455 arg.push_back (thread_id_arg);
456
457 // Push the data for the first argument into the m_arguments vector.
458 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459 }
460
461 virtual
462 ~CommandObjectThreadStepWithTypeAndScope ()
463 {
464 }
465
466 virtual
467 Options *
468 GetOptions ()
469 {
470 return &m_options;
471 }
472
Jim Ingham5a988412012-06-08 21:56:10 +0000473protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000475 DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000477 Process *process = m_exe_ctx.GetProcessPtr();
Greg Claytona7015092010-09-18 01:14:36 +0000478 bool synchronous_execution = m_interpreter.GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479
Greg Claytonf9fc6092013-01-09 19:44:40 +0000480 const uint32_t num_threads = process->GetThreadList().GetSize();
481 Thread *thread = NULL;
482
483 if (command.GetArgumentCount() == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000485 thread = process->GetThreadList().GetSelectedThread().get();
486 if (thread == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000488 result.AppendError ("no selected thread in process");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 result.SetStatus (eReturnStatusFailed);
Jim Ingham64e7ead2012-05-03 21:19:36 +0000490 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000492 }
493 else
494 {
495 const char *thread_idx_cstr = command.GetArgumentAtIndex(0);
496 uint32_t step_thread_idx = Args::StringToUInt32 (thread_idx_cstr, LLDB_INVALID_INDEX32);
497 if (step_thread_idx == LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000499 result.AppendErrorWithFormat ("invalid thread index '%s'.\n", thread_idx_cstr);
500 result.SetStatus (eReturnStatusFailed);
501 return false;
502 }
503 thread = process->GetThreadList().FindThreadByIndexID(step_thread_idx).get();
504 if (thread == NULL)
505 {
506 result.AppendErrorWithFormat ("Thread index %u is out of range (valid values are 0 - %u).\n",
507 step_thread_idx, num_threads);
508 result.SetStatus (eReturnStatusFailed);
509 return false;
510 }
511 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000512
Greg Claytonf9fc6092013-01-09 19:44:40 +0000513 const bool abort_other_plans = false;
514 const lldb::RunMode stop_other_threads = m_options.m_run_mode;
515
516 // This is a bit unfortunate, but not all the commands in this command object support
517 // only while stepping, so I use the bool for them.
518 bool bool_stop_other_threads;
519 if (m_options.m_run_mode == eAllThreads)
520 bool_stop_other_threads = false;
521 else if (m_options.m_run_mode == eOnlyDuringStepping)
522 {
523 if (m_step_type == eStepTypeOut)
524 bool_stop_other_threads = false;
525 else
526 bool_stop_other_threads = true;
527 }
528 else
529 bool_stop_other_threads = true;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000530
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000531 ThreadPlanSP new_plan_sp;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000532
533 if (m_step_type == eStepTypeInto)
534 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000535 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
Greg Claytonf9fc6092013-01-09 19:44:40 +0000536
537 if (frame->HasDebugInformation ())
538 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000539 new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000540 frame->GetSymbolContext(eSymbolContextEverything).line_entry.range,
541 frame->GetSymbolContext(eSymbolContextEverything),
542 m_options.m_step_in_target.c_str(),
543 stop_other_threads,
Jim Ingham4b4b2472014-03-13 02:47:14 +0000544 m_options.m_step_in_avoid_no_debug,
545 m_options.m_step_out_avoid_no_debug);
546
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000547 if (new_plan_sp && !m_options.m_avoid_regexp.empty())
Jim Ingham64e7ead2012-05-03 21:19:36 +0000548 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000549 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (new_plan_sp.get());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000550 step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str());
Jim Ingham29412d12012-05-16 00:37:40 +0000551 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000552 }
553 else
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000554 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false, abort_other_plans, bool_stop_other_threads);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000555
556 }
557 else if (m_step_type == eStepTypeOver)
558 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000559 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
Greg Claytonf9fc6092013-01-09 19:44:40 +0000560
561 if (frame->HasDebugInformation())
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000562 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000563 frame->GetSymbolContext(eSymbolContextEverything).line_entry.range,
564 frame->GetSymbolContext(eSymbolContextEverything),
Jim Ingham4b4b2472014-03-13 02:47:14 +0000565 stop_other_threads,
566 m_options.m_step_out_avoid_no_debug);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000567 else
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000568 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000569 abort_other_plans,
570 bool_stop_other_threads);
571
572 }
573 else if (m_step_type == eStepTypeTrace)
574 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000575 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false, abort_other_plans, bool_stop_other_threads);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000576 }
577 else if (m_step_type == eStepTypeTraceOver)
578 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000579 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true, abort_other_plans, bool_stop_other_threads);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000580 }
581 else if (m_step_type == eStepTypeOut)
582 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000583 new_plan_sp = thread->QueueThreadPlanForStepOut (abort_other_plans,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000584 NULL,
585 false,
586 bool_stop_other_threads,
587 eVoteYes,
588 eVoteNoOpinion,
Jim Ingham4b4b2472014-03-13 02:47:14 +0000589 thread->GetSelectedFrameIndex(),
590 m_options.m_step_out_avoid_no_debug);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000591 }
592 else
593 {
594 result.AppendError ("step type is not supported");
595 result.SetStatus (eReturnStatusFailed);
596 return false;
597 }
598
599 // If we got a new plan, then set it to be a master plan (User level Plans should be master plans
600 // so that they can be interruptible). Then resume the process.
601
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000602 if (new_plan_sp)
Greg Claytonf9fc6092013-01-09 19:44:40 +0000603 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000604 new_plan_sp->SetIsMasterPlan (true);
605 new_plan_sp->SetOkayToDiscard (false);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000606
607 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
608 process->Resume ();
609
610
611 if (synchronous_execution)
Jim Ingham64e7ead2012-05-03 21:19:36 +0000612 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000613 StateType state = process->WaitForProcessToStop (NULL);
614
615 //EventSP event_sp;
616 //StateType state = process->WaitForStateChangedEvents (NULL, event_sp);
617 //while (! StateIsStoppedState (state))
618 // {
619 // state = process->WaitForStateChangedEvents (NULL, event_sp);
620 // }
621 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
622 result.SetDidChangeProcessState (true);
623 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
624 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000626 else
627 {
628 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
629 }
630 }
631 else
632 {
633 result.AppendError ("Couldn't find thread plan to implement step type.");
634 result.SetStatus (eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 }
636 return result.Succeeded();
637 }
638
639protected:
640 StepType m_step_type;
641 StepScope m_step_scope;
642 CommandOptions m_options;
643};
644
Greg Claytone0d378b2011-03-24 21:19:54 +0000645static OptionEnumValueElement
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646g_tri_running_mode[] =
647{
Greg Claytoned8a7052010-09-18 03:37:20 +0000648{ eOnlyThisThread, "this-thread", "Run only this thread"},
649{ eAllThreads, "all-threads", "Run all threads"},
650{ eOnlyDuringStepping, "while-stepping", "Run only this thread while stepping"},
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651{ 0, NULL, NULL }
652};
653
Greg Claytone0d378b2011-03-24 21:19:54 +0000654static OptionEnumValueElement
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655g_duo_running_mode[] =
656{
Greg Claytoned8a7052010-09-18 03:37:20 +0000657{ eOnlyThisThread, "this-thread", "Run only this thread"},
658{ eAllThreads, "all-threads", "Run all threads"},
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659{ 0, NULL, NULL }
660};
661
Greg Claytone0d378b2011-03-24 21:19:54 +0000662OptionDefinition
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663CommandObjectThreadStepWithTypeAndScope::CommandOptions::g_option_table[] =
664{
Jim Ingham4b4b2472014-03-13 02:47:14 +0000665{ LLDB_OPT_SET_1, false, "step-in-avoids-no-debug", 'a', OptionParser::eRequiredArgument, NULL, 0, eArgTypeBoolean, "A boolean value that sets whether stepping into functions will step over functions with no debug information."},
666{ LLDB_OPT_SET_1, false, "step-out-avoids-no-debug", 'A', OptionParser::eRequiredArgument, NULL, 0, eArgTypeBoolean, "A boolean value, if true stepping out of functions will continue to step out till it hits a function with debug information."},
Virgile Belloe2607b52013-09-05 16:42:23 +0000667{ LLDB_OPT_SET_1, false, "run-mode", 'm', OptionParser::eRequiredArgument, g_tri_running_mode, 0, eArgTypeRunMode, "Determine how to run other threads while stepping the current thread."},
668{ LLDB_OPT_SET_1, false, "step-over-regexp",'r', OptionParser::eRequiredArgument, NULL, 0, eArgTypeRegularExpression, "A regular expression that defines function names to not to stop at when stepping in."},
669{ LLDB_OPT_SET_1, false, "step-in-target", 't', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFunctionName, "The name of the directly called function step in should stop at when stepping into."},
Caroline Ticedeaab222010-10-01 19:59:14 +0000670{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671};
672
673
674//-------------------------------------------------------------------------
675// CommandObjectThreadContinue
676//-------------------------------------------------------------------------
677
Jim Ingham5a988412012-06-08 21:56:10 +0000678class CommandObjectThreadContinue : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679{
680public:
681
Greg Claytona7015092010-09-18 01:14:36 +0000682 CommandObjectThreadContinue (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000683 CommandObjectParsed (interpreter,
684 "thread continue",
685 "Continue execution of one or more threads in an active process.",
686 NULL,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000687 eFlagRequiresThread |
688 eFlagTryTargetAPILock |
689 eFlagProcessMustBeLaunched |
690 eFlagProcessMustBePaused)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691 {
Caroline Tice405fe672010-10-04 22:28:36 +0000692 CommandArgumentEntry arg;
693 CommandArgumentData thread_idx_arg;
694
695 // Define the first (and only) variant of this arg.
696 thread_idx_arg.arg_type = eArgTypeThreadIndex;
697 thread_idx_arg.arg_repetition = eArgRepeatPlus;
698
699 // There is only one variant this argument could be; put it into the argument entry.
700 arg.push_back (thread_idx_arg);
701
702 // Push the data for the first argument into the m_arguments vector.
703 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 }
705
706
707 virtual
708 ~CommandObjectThreadContinue ()
709 {
710 }
711
712 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000713 DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 {
Greg Claytona7015092010-09-18 01:14:36 +0000715 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716
Greg Claytona7015092010-09-18 01:14:36 +0000717 if (!m_interpreter.GetDebugger().GetSelectedTarget().get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 {
Greg Claytoneffe5c92011-05-03 22:09:39 +0000719 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720 result.SetStatus (eReturnStatusFailed);
721 return false;
722 }
723
Greg Claytonf9fc6092013-01-09 19:44:40 +0000724 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 if (process == NULL)
726 {
727 result.AppendError ("no process exists. Cannot continue");
728 result.SetStatus (eReturnStatusFailed);
729 return false;
730 }
731
732 StateType state = process->GetState();
733 if ((state == eStateCrashed) || (state == eStateStopped) || (state == eStateSuspended))
734 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735 const size_t argc = command.GetArgumentCount();
736 if (argc > 0)
737 {
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000738 // These two lines appear at the beginning of both blocks in
739 // this if..else, but that is because we need to release the
740 // lock before calling process->Resume below.
741 Mutex::Locker locker (process->GetThreadList().GetMutex());
742 const uint32_t num_threads = process->GetThreadList().GetSize();
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000743 std::vector<Thread *> resume_threads;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 for (uint32_t i=0; i<argc; ++i)
745 {
Jim Inghamce76c622012-05-31 20:48:41 +0000746 bool success;
747 const int base = 0;
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000748 uint32_t thread_idx = Args::StringToUInt32 (command.GetArgumentAtIndex(i), LLDB_INVALID_INDEX32, base, &success);
749 if (success)
Jim Inghamce76c622012-05-31 20:48:41 +0000750 {
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000751 Thread *thread = process->GetThreadList().FindThreadByIndexID(thread_idx).get();
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000752
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000753 if (thread)
754 {
755 resume_threads.push_back(thread);
756 }
757 else
758 {
759 result.AppendErrorWithFormat("invalid thread index %u.\n", thread_idx);
760 result.SetStatus (eReturnStatusFailed);
761 return false;
762 }
Jim Inghamce76c622012-05-31 20:48:41 +0000763 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764 else
Jim Inghamce76c622012-05-31 20:48:41 +0000765 {
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000766 result.AppendErrorWithFormat ("invalid thread index argument: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamce76c622012-05-31 20:48:41 +0000767 result.SetStatus (eReturnStatusFailed);
768 return false;
769 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770 }
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000771
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000772 if (resume_threads.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773 {
774 result.AppendError ("no valid thread indexes were specified");
775 result.SetStatus (eReturnStatusFailed);
776 return false;
777 }
778 else
779 {
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000780 if (resume_threads.size() == 1)
Jim Inghamce76c622012-05-31 20:48:41 +0000781 result.AppendMessageWithFormat ("Resuming thread: ");
782 else
783 result.AppendMessageWithFormat ("Resuming threads: ");
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000784
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000785 for (uint32_t idx=0; idx<num_threads; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786 {
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000787 Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get();
788 std::vector<Thread *>::iterator this_thread_pos = find(resume_threads.begin(), resume_threads.end(), thread);
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000789
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000790 if (this_thread_pos != resume_threads.end())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791 {
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000792 resume_threads.erase(this_thread_pos);
793 if (resume_threads.size() > 0)
Jim Inghamce76c622012-05-31 20:48:41 +0000794 result.AppendMessageWithFormat ("%u, ", thread->GetIndexID());
795 else
796 result.AppendMessageWithFormat ("%u ", thread->GetIndexID());
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000797
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798 thread->SetResumeState (eStateRunning);
799 }
800 else
801 {
802 thread->SetResumeState (eStateSuspended);
803 }
804 }
Daniel Malead01b2952012-11-29 21:49:15 +0000805 result.AppendMessageWithFormat ("in process %" PRIu64 "\n", process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806 }
807 }
808 else
809 {
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000810 // These two lines appear at the beginning of both blocks in
811 // this if..else, but that is because we need to release the
812 // lock before calling process->Resume below.
813 Mutex::Locker locker (process->GetThreadList().GetMutex());
814 const uint32_t num_threads = process->GetThreadList().GetSize();
Jim Ingham2976d002010-08-26 21:32:51 +0000815 Thread *current_thread = process->GetThreadList().GetSelectedThread().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 if (current_thread == NULL)
817 {
818 result.AppendError ("the process doesn't have a current thread");
819 result.SetStatus (eReturnStatusFailed);
820 return false;
821 }
822 // Set the actions that the threads should each take when resuming
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000823 for (uint32_t idx=0; idx<num_threads; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000824 {
Greg Claytonc8a0ce02012-07-03 20:54:16 +0000825 Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 if (thread == current_thread)
827 {
Daniel Malead01b2952012-11-29 21:49:15 +0000828 result.AppendMessageWithFormat ("Resuming thread 0x%4.4" PRIx64 " in process %" PRIu64 "\n", thread->GetID(), process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 thread->SetResumeState (eStateRunning);
830 }
831 else
832 {
833 thread->SetResumeState (eStateSuspended);
834 }
835 }
836 }
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000837
838 // We should not be holding the thread list lock when we do this.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839 Error error (process->Resume());
840 if (error.Success())
841 {
Daniel Malead01b2952012-11-29 21:49:15 +0000842 result.AppendMessageWithFormat ("Process %" PRIu64 " resuming\n", process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000843 if (synchronous_execution)
844 {
Greg Claytonb1320972010-07-14 00:18:15 +0000845 state = process->WaitForProcessToStop (NULL);
Andrew Kaylor9063bf42013-09-12 19:15:05 +0000846
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847 result.SetDidChangeProcessState (true);
Daniel Malead01b2952012-11-29 21:49:15 +0000848 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849 result.SetStatus (eReturnStatusSuccessFinishNoResult);
850 }
851 else
852 {
853 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
854 }
855 }
856 else
857 {
858 result.AppendErrorWithFormat("Failed to resume process: %s\n", error.AsCString());
859 result.SetStatus (eReturnStatusFailed);
860 }
861 }
862 else
863 {
864 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
865 StateAsCString(state));
866 result.SetStatus (eReturnStatusFailed);
867 }
868
869 return result.Succeeded();
870 }
871
872};
873
874//-------------------------------------------------------------------------
875// CommandObjectThreadUntil
876//-------------------------------------------------------------------------
877
Jim Ingham5a988412012-06-08 21:56:10 +0000878class CommandObjectThreadUntil : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879{
880public:
881
882 class CommandOptions : public Options
883 {
884 public:
885 uint32_t m_thread_idx;
886 uint32_t m_frame_idx;
887
Greg Claytoneb0103f2011-04-07 22:46:35 +0000888 CommandOptions (CommandInterpreter &interpreter) :
889 Options (interpreter),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000890 m_thread_idx(LLDB_INVALID_THREAD_ID),
891 m_frame_idx(LLDB_INVALID_FRAME_ID)
892 {
Greg Claytonf6b8b582011-04-13 00:18:08 +0000893 // Keep default values of all options in one place: OptionParsingStarting ()
894 OptionParsingStarting ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895 }
896
897 virtual
898 ~CommandOptions ()
899 {
900 }
901
902 virtual Error
Greg Claytonf6b8b582011-04-13 00:18:08 +0000903 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904 {
905 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000906 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907
908 switch (short_option)
909 {
910 case 't':
911 {
Greg Claytonb1320972010-07-14 00:18:15 +0000912 m_thread_idx = Args::StringToUInt32 (option_arg, LLDB_INVALID_INDEX32);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913 if (m_thread_idx == LLDB_INVALID_INDEX32)
914 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000915 error.SetErrorStringWithFormat ("invalid thread index '%s'", option_arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000916 }
917 }
918 break;
919 case 'f':
920 {
921 m_frame_idx = Args::StringToUInt32 (option_arg, LLDB_INVALID_FRAME_ID);
922 if (m_frame_idx == LLDB_INVALID_FRAME_ID)
923 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000924 error.SetErrorStringWithFormat ("invalid frame index '%s'", option_arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925 }
926 }
927 break;
928 case 'm':
929 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000930 OptionEnumValueElement *enum_values = g_option_table[option_idx].enum_values;
Greg Claytoncf0e4f02011-10-07 18:58:12 +0000931 lldb::RunMode run_mode = (lldb::RunMode) Args::StringToOptionEnum(option_arg, enum_values, eOnlyDuringStepping, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932
Greg Claytoncf0e4f02011-10-07 18:58:12 +0000933 if (error.Success())
934 {
935 if (run_mode == eAllThreads)
936 m_stop_others = false;
937 else
938 m_stop_others = true;
939 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940 }
941 break;
942 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000943 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000944 break;
945
946 }
947 return error;
948 }
949
950 void
Greg Claytonf6b8b582011-04-13 00:18:08 +0000951 OptionParsingStarting ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000953 m_thread_idx = LLDB_INVALID_THREAD_ID;
954 m_frame_idx = 0;
955 m_stop_others = false;
956 }
957
Greg Claytone0d378b2011-03-24 21:19:54 +0000958 const OptionDefinition*
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000959 GetDefinitions ()
960 {
961 return g_option_table;
962 }
963
964 uint32_t m_step_thread_idx;
965 bool m_stop_others;
966
967 // Options table: Required for subclasses of Options.
968
Greg Claytone0d378b2011-03-24 21:19:54 +0000969 static OptionDefinition g_option_table[];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000970
971 // Instance variables to hold the values for command options.
972 };
973
Greg Claytona7015092010-09-18 01:14:36 +0000974 CommandObjectThreadUntil (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000975 CommandObjectParsed (interpreter,
976 "thread until",
977 "Run the current or specified thread until it reaches a given line number or leaves the current function.",
978 NULL,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000979 eFlagRequiresThread |
980 eFlagTryTargetAPILock |
981 eFlagProcessMustBeLaunched |
982 eFlagProcessMustBePaused ),
Greg Claytoneb0103f2011-04-07 22:46:35 +0000983 m_options (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000984 {
Caroline Tice405fe672010-10-04 22:28:36 +0000985 CommandArgumentEntry arg;
986 CommandArgumentData line_num_arg;
987
988 // Define the first (and only) variant of this arg.
989 line_num_arg.arg_type = eArgTypeLineNum;
990 line_num_arg.arg_repetition = eArgRepeatPlain;
991
992 // There is only one variant this argument could be; put it into the argument entry.
993 arg.push_back (line_num_arg);
994
995 // Push the data for the first argument into the m_arguments vector.
996 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997 }
998
999
1000 virtual
1001 ~CommandObjectThreadUntil ()
1002 {
1003 }
1004
1005 virtual
1006 Options *
1007 GetOptions ()
1008 {
1009 return &m_options;
1010 }
1011
Jim Ingham5a988412012-06-08 21:56:10 +00001012protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00001014 DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015 {
Greg Claytona7015092010-09-18 01:14:36 +00001016 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017
Greg Claytona7015092010-09-18 01:14:36 +00001018 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Claytonf5e56de2010-09-14 23:36:40 +00001019 if (target == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00001021 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022 result.SetStatus (eReturnStatusFailed);
1023 return false;
1024 }
1025
Greg Claytonf9fc6092013-01-09 19:44:40 +00001026 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027 if (process == NULL)
1028 {
1029 result.AppendError ("need a valid process to step");
1030 result.SetStatus (eReturnStatusFailed);
1031
1032 }
1033 else
1034 {
1035 Thread *thread = NULL;
1036 uint32_t line_number;
1037
1038 if (command.GetArgumentCount() != 1)
1039 {
1040 result.AppendErrorWithFormat ("No line number provided:\n%s", GetSyntax());
1041 result.SetStatus (eReturnStatusFailed);
1042 return false;
1043 }
1044
1045 line_number = Args::StringToUInt32 (command.GetArgumentAtIndex(0), UINT32_MAX);
1046 if (line_number == UINT32_MAX)
1047 {
Greg Clayton86edbf42011-10-26 00:56:27 +00001048 result.AppendErrorWithFormat ("invalid line number: '%s'.\n", command.GetArgumentAtIndex(0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001049 result.SetStatus (eReturnStatusFailed);
1050 return false;
1051 }
1052
1053 if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID)
1054 {
Jim Ingham2976d002010-08-26 21:32:51 +00001055 thread = process->GetThreadList().GetSelectedThread().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001056 }
1057 else
1058 {
Greg Clayton76927ee2012-05-31 00:29:20 +00001059 thread = process->GetThreadList().FindThreadByIndexID(m_options.m_thread_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001060 }
1061
1062 if (thread == NULL)
1063 {
1064 const uint32_t num_threads = process->GetThreadList().GetSize();
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001065 result.AppendErrorWithFormat ("Thread index %u is out of range (valid values are 0 - %u).\n",
1066 m_options.m_thread_idx,
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001067 num_threads);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068 result.SetStatus (eReturnStatusFailed);
1069 return false;
1070 }
1071
Jim Ingham7ba6e992012-05-11 23:47:32 +00001072 const bool abort_other_plans = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073
Jason Molendab57e4a12013-11-04 09:33:30 +00001074 StackFrame *frame = thread->GetStackFrameAtIndex(m_options.m_frame_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075 if (frame == NULL)
1076 {
1077
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001078 result.AppendErrorWithFormat ("Frame index %u is out of range for thread %u.\n",
1079 m_options.m_frame_idx,
1080 m_options.m_thread_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081 result.SetStatus (eReturnStatusFailed);
1082 return false;
1083 }
1084
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001085 ThreadPlanSP new_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086
1087 if (frame->HasDebugInformation ())
1088 {
1089 // Finally we got here... Translate the given line number to a bunch of addresses:
1090 SymbolContext sc(frame->GetSymbolContext (eSymbolContextCompUnit));
1091 LineTable *line_table = NULL;
1092 if (sc.comp_unit)
1093 line_table = sc.comp_unit->GetLineTable();
1094
1095 if (line_table == NULL)
1096 {
1097 result.AppendErrorWithFormat ("Failed to resolve the line table for frame %u of thread index %u.\n",
1098 m_options.m_frame_idx, m_options.m_thread_idx);
1099 result.SetStatus (eReturnStatusFailed);
1100 return false;
1101 }
1102
1103 LineEntry function_start;
1104 uint32_t index_ptr = 0, end_ptr;
1105 std::vector<addr_t> address_list;
1106
1107 // Find the beginning & end index of the
1108 AddressRange fun_addr_range = sc.function->GetAddressRange();
1109 Address fun_start_addr = fun_addr_range.GetBaseAddress();
1110 line_table->FindLineEntryByAddress (fun_start_addr, function_start, &index_ptr);
1111
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001112 Address fun_end_addr(fun_start_addr.GetSection(),
1113 fun_start_addr.GetOffset() + fun_addr_range.GetByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114 line_table->FindLineEntryByAddress (fun_end_addr, function_start, &end_ptr);
1115
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001116 bool all_in_function = true;
1117
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118 while (index_ptr <= end_ptr)
1119 {
1120 LineEntry line_entry;
Jim Ingham87df91b2011-09-23 00:54:11 +00001121 const bool exact = false;
1122 index_ptr = sc.comp_unit->FindLineEntry(index_ptr, line_number, sc.comp_unit, exact, &line_entry);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123 if (index_ptr == UINT32_MAX)
1124 break;
1125
Greg Claytonf5e56de2010-09-14 23:36:40 +00001126 addr_t address = line_entry.range.GetBaseAddress().GetLoadAddress(target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127 if (address != LLDB_INVALID_ADDRESS)
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001128 {
1129 if (fun_addr_range.ContainsLoadAddress (address, target))
1130 address_list.push_back (address);
1131 else
1132 all_in_function = false;
1133 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134 index_ptr++;
1135 }
1136
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001137 if (address_list.size() == 0)
1138 {
1139 if (all_in_function)
1140 result.AppendErrorWithFormat ("No line entries matching until target.\n");
1141 else
1142 result.AppendErrorWithFormat ("Until target outside of the current function.\n");
1143
1144 result.SetStatus (eReturnStatusFailed);
1145 return false;
1146 }
1147
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001148 new_plan_sp = thread->QueueThreadPlanForStepUntil (abort_other_plans,
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001149 &address_list.front(),
1150 address_list.size(),
1151 m_options.m_stop_others,
Jim Inghamf76ab672012-09-14 20:48:14 +00001152 m_options.m_frame_idx);
Jim Ingham64e7ead2012-05-03 21:19:36 +00001153 // User level plans should be master plans so they can be interrupted (e.g. by hitting a breakpoint)
1154 // and other plans executed by the user (stepping around the breakpoint) and then a "continue"
1155 // will resume the original plan.
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001156 new_plan_sp->SetIsMasterPlan (true);
1157 new_plan_sp->SetOkayToDiscard(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 }
1159 else
1160 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001161 result.AppendErrorWithFormat ("Frame index %u of thread %u has no debug information.\n",
1162 m_options.m_frame_idx,
1163 m_options.m_thread_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001164 result.SetStatus (eReturnStatusFailed);
1165 return false;
1166
1167 }
1168
Jim Ingham2976d002010-08-26 21:32:51 +00001169 process->GetThreadList().SetSelectedThreadByID (m_options.m_thread_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001170 Error error (process->Resume ());
1171 if (error.Success())
1172 {
Daniel Malead01b2952012-11-29 21:49:15 +00001173 result.AppendMessageWithFormat ("Process %" PRIu64 " resuming\n", process->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174 if (synchronous_execution)
1175 {
1176 StateType state = process->WaitForProcessToStop (NULL);
1177
1178 result.SetDidChangeProcessState (true);
Daniel Malead01b2952012-11-29 21:49:15 +00001179 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1181 }
1182 else
1183 {
1184 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
1185 }
1186 }
1187 else
1188 {
1189 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
1190 result.SetStatus (eReturnStatusFailed);
1191 }
1192
1193 }
1194 return result.Succeeded();
1195 }
Jim Ingham5a988412012-06-08 21:56:10 +00001196
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001197 CommandOptions m_options;
1198
1199};
1200
Greg Claytone0d378b2011-03-24 21:19:54 +00001201OptionDefinition
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202CommandObjectThreadUntil::CommandOptions::g_option_table[] =
1203{
Virgile Belloe2607b52013-09-05 16:42:23 +00001204{ LLDB_OPT_SET_1, false, "frame", 'f', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFrameIndex, "Frame index for until operation - defaults to 0"},
1205{ LLDB_OPT_SET_1, false, "thread", 't', OptionParser::eRequiredArgument, NULL, 0, eArgTypeThreadIndex, "Thread index for the thread for until operation"},
1206{ LLDB_OPT_SET_1, false, "run-mode",'m', OptionParser::eRequiredArgument, g_duo_running_mode, 0, eArgTypeRunMode,"Determine how to run other threads while stepping this one"},
Caroline Ticedeaab222010-10-01 19:59:14 +00001207{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208};
1209
1210
1211//-------------------------------------------------------------------------
1212// CommandObjectThreadSelect
1213//-------------------------------------------------------------------------
1214
Jim Ingham5a988412012-06-08 21:56:10 +00001215class CommandObjectThreadSelect : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216{
1217public:
1218
Greg Claytona7015092010-09-18 01:14:36 +00001219 CommandObjectThreadSelect (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001220 CommandObjectParsed (interpreter,
1221 "thread select",
1222 "Select a thread as the currently active thread.",
1223 NULL,
Greg Claytonf9fc6092013-01-09 19:44:40 +00001224 eFlagRequiresProcess |
1225 eFlagTryTargetAPILock |
1226 eFlagProcessMustBeLaunched |
1227 eFlagProcessMustBePaused )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001228 {
Caroline Tice405fe672010-10-04 22:28:36 +00001229 CommandArgumentEntry arg;
1230 CommandArgumentData thread_idx_arg;
1231
1232 // Define the first (and only) variant of this arg.
1233 thread_idx_arg.arg_type = eArgTypeThreadIndex;
1234 thread_idx_arg.arg_repetition = eArgRepeatPlain;
1235
1236 // There is only one variant this argument could be; put it into the argument entry.
1237 arg.push_back (thread_idx_arg);
1238
1239 // Push the data for the first argument into the m_arguments vector.
1240 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001241 }
1242
1243
1244 virtual
1245 ~CommandObjectThreadSelect ()
1246 {
1247 }
1248
Jim Ingham5a988412012-06-08 21:56:10 +00001249protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00001251 DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001253 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254 if (process == NULL)
1255 {
1256 result.AppendError ("no process");
1257 result.SetStatus (eReturnStatusFailed);
1258 return false;
1259 }
1260 else if (command.GetArgumentCount() != 1)
1261 {
Jason Molendafd54b362011-09-20 21:44:10 +00001262 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 +00001263 result.SetStatus (eReturnStatusFailed);
1264 return false;
1265 }
1266
1267 uint32_t index_id = Args::StringToUInt32(command.GetArgumentAtIndex(0), 0, 0);
1268
1269 Thread *new_thread = process->GetThreadList().FindThreadByIndexID(index_id).get();
1270 if (new_thread == NULL)
1271 {
Greg Clayton86edbf42011-10-26 00:56:27 +00001272 result.AppendErrorWithFormat ("invalid thread #%s.\n", command.GetArgumentAtIndex(0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273 result.SetStatus (eReturnStatusFailed);
1274 return false;
1275 }
1276
Jim Inghamc3faa192012-12-11 02:31:48 +00001277 process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true);
Johnny Chenc13ee522010-09-14 00:53:53 +00001278 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001279
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001280 return result.Succeeded();
1281 }
1282
1283};
1284
1285
1286//-------------------------------------------------------------------------
1287// CommandObjectThreadList
1288//-------------------------------------------------------------------------
1289
Jim Ingham5a988412012-06-08 21:56:10 +00001290class CommandObjectThreadList : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291{
Greg Clayton66111032010-06-23 01:19:29 +00001292public:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001293
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001294
Greg Claytona7015092010-09-18 01:14:36 +00001295 CommandObjectThreadList (CommandInterpreter &interpreter):
Jim Ingham5a988412012-06-08 21:56:10 +00001296 CommandObjectParsed (interpreter,
1297 "thread list",
1298 "Show a summary of all current threads in a process.",
1299 "thread list",
Greg Claytonf9fc6092013-01-09 19:44:40 +00001300 eFlagRequiresProcess |
1301 eFlagTryTargetAPILock |
1302 eFlagProcessMustBeLaunched |
1303 eFlagProcessMustBePaused )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001304 {
Greg Clayton66111032010-06-23 01:19:29 +00001305 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001306
Greg Clayton66111032010-06-23 01:19:29 +00001307 ~CommandObjectThreadList()
1308 {
1309 }
1310
Jim Ingham5a988412012-06-08 21:56:10 +00001311protected:
Greg Clayton66111032010-06-23 01:19:29 +00001312 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001313 DoExecute (Args& command, CommandReturnObject &result)
Greg Clayton66111032010-06-23 01:19:29 +00001314 {
Jim Ingham85e8b812011-02-19 02:53:09 +00001315 Stream &strm = result.GetOutputStream();
Greg Clayton66111032010-06-23 01:19:29 +00001316 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonf9fc6092013-01-09 19:44:40 +00001317 Process *process = m_exe_ctx.GetProcessPtr();
1318 const bool only_threads_with_stop_reason = false;
1319 const uint32_t start_frame = 0;
1320 const uint32_t num_frames = 0;
1321 const uint32_t num_frames_with_source = 0;
1322 process->GetStatus(strm);
1323 process->GetThreadStatus (strm,
1324 only_threads_with_stop_reason,
1325 start_frame,
1326 num_frames,
1327 num_frames_with_source);
Greg Clayton66111032010-06-23 01:19:29 +00001328 return result.Succeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001329 }
Greg Clayton66111032010-06-23 01:19:29 +00001330};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331
Jim Ingham93208b82013-01-31 21:46:01 +00001332//-------------------------------------------------------------------------
1333// CommandObjectThreadReturn
1334//-------------------------------------------------------------------------
1335
Jim Inghamcb640dd2012-09-14 02:14:15 +00001336class CommandObjectThreadReturn : public CommandObjectRaw
1337{
1338public:
Jim Ingham93208b82013-01-31 21:46:01 +00001339 class CommandOptions : public Options
1340 {
1341 public:
1342
1343 CommandOptions (CommandInterpreter &interpreter) :
1344 Options (interpreter),
1345 m_from_expression (false)
1346 {
1347 // Keep default values of all options in one place: OptionParsingStarting ()
1348 OptionParsingStarting ();
1349 }
1350
1351 virtual
1352 ~CommandOptions ()
1353 {
1354 }
1355
1356 virtual Error
1357 SetOptionValue (uint32_t option_idx, const char *option_arg)
1358 {
1359 Error error;
1360 const int short_option = m_getopt_table[option_idx].val;
1361
1362 switch (short_option)
1363 {
1364 case 'x':
1365 {
1366 bool success;
1367 bool tmp_value = Args::StringToBoolean (option_arg, false, &success);
1368 if (success)
1369 m_from_expression = tmp_value;
1370 else
1371 {
1372 error.SetErrorStringWithFormat ("invalid boolean value '%s' for 'x' option", option_arg);
1373 }
1374 }
1375 break;
1376 default:
1377 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
1378 break;
1379
1380 }
1381 return error;
1382 }
1383
1384 void
1385 OptionParsingStarting ()
1386 {
1387 m_from_expression = false;
1388 }
1389
1390 const OptionDefinition*
1391 GetDefinitions ()
1392 {
1393 return g_option_table;
1394 }
1395
1396 bool m_from_expression;
1397
1398 // Options table: Required for subclasses of Options.
1399
1400 static OptionDefinition g_option_table[];
1401
1402 // Instance variables to hold the values for command options.
1403 };
1404
1405 virtual
1406 Options *
1407 GetOptions ()
1408 {
1409 return &m_options;
1410 }
1411
Jim Inghamcb640dd2012-09-14 02:14:15 +00001412 CommandObjectThreadReturn (CommandInterpreter &interpreter) :
1413 CommandObjectRaw (interpreter,
1414 "thread return",
Jim Ingham93208b82013-01-31 21:46:01 +00001415 "Return from the currently selected frame, short-circuiting execution of the frames below it, with an optional return value,"
1416 " or with the -x option from the innermost function evaluation.",
Jim Inghamcb640dd2012-09-14 02:14:15 +00001417 "thread return",
Greg Claytonf9fc6092013-01-09 19:44:40 +00001418 eFlagRequiresFrame |
1419 eFlagTryTargetAPILock |
1420 eFlagProcessMustBeLaunched |
Jim Ingham93208b82013-01-31 21:46:01 +00001421 eFlagProcessMustBePaused ),
1422 m_options (interpreter)
Jim Inghamcb640dd2012-09-14 02:14:15 +00001423 {
1424 CommandArgumentEntry arg;
1425 CommandArgumentData expression_arg;
1426
1427 // Define the first (and only) variant of this arg.
1428 expression_arg.arg_type = eArgTypeExpression;
Jim Ingham93208b82013-01-31 21:46:01 +00001429 expression_arg.arg_repetition = eArgRepeatOptional;
Jim Inghamcb640dd2012-09-14 02:14:15 +00001430
1431 // There is only one variant this argument could be; put it into the argument entry.
1432 arg.push_back (expression_arg);
1433
1434 // Push the data for the first argument into the m_arguments vector.
1435 m_arguments.push_back (arg);
1436
1437
1438 }
1439
1440 ~CommandObjectThreadReturn()
1441 {
1442 }
1443
1444protected:
1445
1446 bool DoExecute
1447 (
1448 const char *command,
1449 CommandReturnObject &result
1450 )
1451 {
Jim Ingham93208b82013-01-31 21:46:01 +00001452 // I am going to handle this by hand, because I don't want you to have to say:
1453 // "thread return -- -5".
1454 if (command[0] == '-' && command[1] == 'x')
1455 {
1456 if (command && command[2] != '\0')
1457 result.AppendWarning("Return values ignored when returning from user called expressions");
1458
1459 Thread *thread = m_exe_ctx.GetThreadPtr();
1460 Error error;
1461 error = thread->UnwindInnermostExpression();
1462 if (!error.Success())
1463 {
1464 result.AppendErrorWithFormat ("Unwinding expression failed - %s.", error.AsCString());
1465 result.SetStatus (eReturnStatusFailed);
1466 }
1467 else
1468 {
1469 bool success = thread->SetSelectedFrameByIndexNoisily (0, result.GetOutputStream());
1470 if (success)
1471 {
1472 m_exe_ctx.SetFrameSP(thread->GetSelectedFrame ());
1473 result.SetStatus (eReturnStatusSuccessFinishResult);
1474 }
1475 else
1476 {
1477 result.AppendErrorWithFormat ("Could not select 0th frame after unwinding expression.");
1478 result.SetStatus (eReturnStatusFailed);
1479 }
1480 }
1481 return result.Succeeded();
1482 }
1483
Jim Inghamcb640dd2012-09-14 02:14:15 +00001484 ValueObjectSP return_valobj_sp;
1485
Jason Molendab57e4a12013-11-04 09:33:30 +00001486 StackFrameSP frame_sp = m_exe_ctx.GetFrameSP();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001487 uint32_t frame_idx = frame_sp->GetFrameIndex();
1488
1489 if (frame_sp->IsInlined())
1490 {
1491 result.AppendError("Don't know how to return from inlined frames.");
1492 result.SetStatus (eReturnStatusFailed);
1493 return false;
1494 }
1495
1496 if (command && command[0] != '\0')
1497 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001498 Target *target = m_exe_ctx.GetTargetPtr();
Jim Ingham35e1bda2012-10-16 21:41:58 +00001499 EvaluateExpressionOptions options;
Jim Inghamcb640dd2012-09-14 02:14:15 +00001500
1501 options.SetUnwindOnError(true);
1502 options.SetUseDynamic(eNoDynamicValues);
1503
1504 ExecutionResults exe_results = eExecutionSetupError;
1505 exe_results = target->EvaluateExpression (command,
1506 frame_sp.get(),
1507 return_valobj_sp,
1508 options);
1509 if (exe_results != eExecutionCompleted)
1510 {
1511 if (return_valobj_sp)
1512 result.AppendErrorWithFormat("Error evaluating result expression: %s", return_valobj_sp->GetError().AsCString());
1513 else
1514 result.AppendErrorWithFormat("Unknown error evaluating result expression.");
1515 result.SetStatus (eReturnStatusFailed);
1516 return false;
1517
1518 }
1519 }
1520
1521 Error error;
Greg Claytonf9fc6092013-01-09 19:44:40 +00001522 ThreadSP thread_sp = m_exe_ctx.GetThreadSP();
Jim Ingham4f465cf2012-10-10 18:32:14 +00001523 const bool broadcast = true;
1524 error = thread_sp->ReturnFromFrame (frame_sp, return_valobj_sp, broadcast);
Jim Inghamcb640dd2012-09-14 02:14:15 +00001525 if (!error.Success())
1526 {
1527 result.AppendErrorWithFormat("Error returning from frame %d of thread %d: %s.", frame_idx, thread_sp->GetIndexID(), error.AsCString());
1528 result.SetStatus (eReturnStatusFailed);
1529 return false;
1530 }
1531
Jim Inghamcb640dd2012-09-14 02:14:15 +00001532 result.SetStatus (eReturnStatusSuccessFinishResult);
1533 return true;
1534 }
Jim Ingham93208b82013-01-31 21:46:01 +00001535
1536 CommandOptions m_options;
Jim Inghamcb640dd2012-09-14 02:14:15 +00001537
1538};
Jim Ingham93208b82013-01-31 21:46:01 +00001539OptionDefinition
1540CommandObjectThreadReturn::CommandOptions::g_option_table[] =
1541{
Virgile Belloe2607b52013-09-05 16:42:23 +00001542{ LLDB_OPT_SET_ALL, false, "from-expression", 'x', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Return from the innermost expression evaluation."},
Jim Ingham93208b82013-01-31 21:46:01 +00001543{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1544};
Jim Inghamcb640dd2012-09-14 02:14:15 +00001545
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001546//-------------------------------------------------------------------------
Richard Mittonf86248d2013-09-12 02:20:34 +00001547// CommandObjectThreadJump
1548//-------------------------------------------------------------------------
1549
1550class CommandObjectThreadJump : public CommandObjectParsed
1551{
1552public:
1553 class CommandOptions : public Options
1554 {
1555 public:
1556
1557 CommandOptions (CommandInterpreter &interpreter) :
1558 Options (interpreter)
1559 {
1560 OptionParsingStarting ();
1561 }
1562
1563 void
1564 OptionParsingStarting ()
1565 {
1566 m_filenames.Clear();
1567 m_line_num = 0;
1568 m_line_offset = 0;
1569 m_load_addr = LLDB_INVALID_ADDRESS;
1570 m_force = false;
1571 }
1572
1573 virtual
1574 ~CommandOptions ()
1575 {
1576 }
1577
1578 virtual Error
1579 SetOptionValue (uint32_t option_idx, const char *option_arg)
1580 {
1581 bool success;
1582 const int short_option = m_getopt_table[option_idx].val;
1583 Error error;
1584
1585 switch (short_option)
1586 {
1587 case 'f':
1588 m_filenames.AppendIfUnique (FileSpec(option_arg, false));
1589 if (m_filenames.GetSize() > 1)
1590 return Error("only one source file expected.");
1591 break;
1592 case 'l':
1593 m_line_num = Args::StringToUInt32 (option_arg, 0, 0, &success);
1594 if (!success || m_line_num == 0)
1595 return Error("invalid line number: '%s'.", option_arg);
1596 break;
1597 case 'b':
1598 m_line_offset = Args::StringToSInt32 (option_arg, 0, 0, &success);
1599 if (!success)
1600 return Error("invalid line offset: '%s'.", option_arg);
1601 break;
1602 case 'a':
1603 {
1604 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
1605 m_load_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
1606 }
1607 break;
1608 case 'r':
1609 m_force = true;
1610 break;
1611
1612 default:
1613 return Error("invalid short option character '%c'", short_option);
1614
1615 }
1616 return error;
1617 }
1618
1619 const OptionDefinition*
1620 GetDefinitions ()
1621 {
1622 return g_option_table;
1623 }
1624
1625 FileSpecList m_filenames;
1626 uint32_t m_line_num;
1627 int32_t m_line_offset;
1628 lldb::addr_t m_load_addr;
1629 bool m_force;
1630
1631 static OptionDefinition g_option_table[];
1632 };
1633
1634 virtual
1635 Options *
1636 GetOptions ()
1637 {
1638 return &m_options;
1639 }
1640
1641 CommandObjectThreadJump (CommandInterpreter &interpreter) :
1642 CommandObjectParsed (interpreter,
1643 "thread jump",
1644 "Sets the program counter to a new address.",
1645 "thread jump",
1646 eFlagRequiresFrame |
1647 eFlagTryTargetAPILock |
1648 eFlagProcessMustBeLaunched |
1649 eFlagProcessMustBePaused ),
1650 m_options (interpreter)
1651 {
1652 }
1653
1654 ~CommandObjectThreadJump()
1655 {
1656 }
1657
1658protected:
1659
1660 bool DoExecute (Args& args, CommandReturnObject &result)
1661 {
1662 RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00001663 StackFrame *frame = m_exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00001664 Thread *thread = m_exe_ctx.GetThreadPtr();
1665 Target *target = m_exe_ctx.GetTargetPtr();
1666 const SymbolContext &sym_ctx = frame->GetSymbolContext (eSymbolContextLineEntry);
1667
1668 if (m_options.m_load_addr != LLDB_INVALID_ADDRESS)
1669 {
1670 // Use this address directly.
1671 Address dest = Address(m_options.m_load_addr);
1672
1673 lldb::addr_t callAddr = dest.GetCallableLoadAddress (target);
1674 if (callAddr == LLDB_INVALID_ADDRESS)
1675 {
1676 result.AppendErrorWithFormat ("Invalid destination address.");
1677 result.SetStatus (eReturnStatusFailed);
1678 return false;
1679 }
1680
1681 if (!reg_ctx->SetPC (callAddr))
1682 {
1683 result.AppendErrorWithFormat ("Error changing PC value for thread %d.", thread->GetIndexID());
1684 result.SetStatus (eReturnStatusFailed);
1685 return false;
1686 }
1687 }
1688 else
1689 {
1690 // Pick either the absolute line, or work out a relative one.
1691 int32_t line = (int32_t)m_options.m_line_num;
1692 if (line == 0)
1693 line = sym_ctx.line_entry.line + m_options.m_line_offset;
1694
1695 // Try the current file, but override if asked.
1696 FileSpec file = sym_ctx.line_entry.file;
1697 if (m_options.m_filenames.GetSize() == 1)
1698 file = m_options.m_filenames.GetFileSpecAtIndex(0);
1699
1700 if (!file)
1701 {
1702 result.AppendErrorWithFormat ("No source file available for the current location.");
1703 result.SetStatus (eReturnStatusFailed);
1704 return false;
1705 }
1706
1707 std::string warnings;
1708 Error err = thread->JumpToLine (file, line, m_options.m_force, &warnings);
1709
1710 if (err.Fail())
1711 {
1712 result.SetError (err);
1713 return false;
1714 }
1715
1716 if (!warnings.empty())
1717 result.AppendWarning (warnings.c_str());
1718 }
1719
1720 result.SetStatus (eReturnStatusSuccessFinishResult);
1721 return true;
1722 }
1723
1724 CommandOptions m_options;
1725};
1726OptionDefinition
1727CommandObjectThreadJump::CommandOptions::g_option_table[] =
1728{
1729 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
1730 "Specifies the source file to jump to."},
1731
1732 { LLDB_OPT_SET_1, true, "line", 'l', OptionParser::eRequiredArgument, NULL, 0, eArgTypeLineNum,
1733 "Specifies the line number to jump to."},
1734
1735 { LLDB_OPT_SET_2, true, "by", 'b', OptionParser::eRequiredArgument, NULL, 0, eArgTypeOffset,
1736 "Jumps by a relative line offset from the current line."},
1737
1738 { LLDB_OPT_SET_3, true, "address", 'a', OptionParser::eRequiredArgument, NULL, 0, eArgTypeAddressOrExpression,
1739 "Jumps to a specific address."},
1740
1741 { LLDB_OPT_SET_1|
1742 LLDB_OPT_SET_2|
1743 LLDB_OPT_SET_3, false, "force",'r', OptionParser::eNoArgument, NULL, 0, eArgTypeNone,"Allows the PC to leave the current function."},
1744
1745 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1746};
1747
1748//-------------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001749// CommandObjectMultiwordThread
1750//-------------------------------------------------------------------------
1751
Greg Clayton66111032010-06-23 01:19:29 +00001752CommandObjectMultiwordThread::CommandObjectMultiwordThread (CommandInterpreter &interpreter) :
Greg Claytona7015092010-09-18 01:14:36 +00001753 CommandObjectMultiword (interpreter,
1754 "thread",
Caroline Tice3f4c09c2010-09-07 22:38:08 +00001755 "A set of commands for operating on one or more threads within a running process.",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001756 "thread <subcommand> [<subcommand-options>]")
1757{
Greg Claytona7015092010-09-18 01:14:36 +00001758 LoadSubCommand ("backtrace", CommandObjectSP (new CommandObjectThreadBacktrace (interpreter)));
1759 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectThreadContinue (interpreter)));
1760 LoadSubCommand ("list", CommandObjectSP (new CommandObjectThreadList (interpreter)));
Jim Inghamcb640dd2012-09-14 02:14:15 +00001761 LoadSubCommand ("return", CommandObjectSP (new CommandObjectThreadReturn (interpreter)));
Richard Mittonf86248d2013-09-12 02:20:34 +00001762 LoadSubCommand ("jump", CommandObjectSP (new CommandObjectThreadJump (interpreter)));
Greg Claytona7015092010-09-18 01:14:36 +00001763 LoadSubCommand ("select", CommandObjectSP (new CommandObjectThreadSelect (interpreter)));
1764 LoadSubCommand ("until", CommandObjectSP (new CommandObjectThreadUntil (interpreter)));
1765 LoadSubCommand ("step-in", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1766 interpreter,
Greg Clayton66111032010-06-23 01:19:29 +00001767 "thread step-in",
Greg Claytona7015092010-09-18 01:14:36 +00001768 "Source level single step in specified thread (current thread, if none specified).",
Caroline Tice405fe672010-10-04 22:28:36 +00001769 NULL,
Greg Claytona7015092010-09-18 01:14:36 +00001770 eStepTypeInto,
1771 eStepScopeSource)));
Greg Clayton66111032010-06-23 01:19:29 +00001772
Greg Claytona7015092010-09-18 01:14:36 +00001773 LoadSubCommand ("step-out", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1774 interpreter,
1775 "thread step-out",
Jim Ingham73ca05a2011-12-17 01:35:57 +00001776 "Finish executing the function of the currently selected frame and return to its call site in specified thread (current thread, if none specified).",
Caroline Tice405fe672010-10-04 22:28:36 +00001777 NULL,
Greg Claytona7015092010-09-18 01:14:36 +00001778 eStepTypeOut,
1779 eStepScopeSource)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001780
Greg Claytona7015092010-09-18 01:14:36 +00001781 LoadSubCommand ("step-over", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1782 interpreter,
1783 "thread step-over",
1784 "Source level single step in specified thread (current thread, if none specified), stepping over calls.",
Caroline Tice405fe672010-10-04 22:28:36 +00001785 NULL,
Greg Claytona7015092010-09-18 01:14:36 +00001786 eStepTypeOver,
1787 eStepScopeSource)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001788
Greg Claytona7015092010-09-18 01:14:36 +00001789 LoadSubCommand ("step-inst", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1790 interpreter,
1791 "thread step-inst",
1792 "Single step one instruction in specified thread (current thread, if none specified).",
Caroline Tice405fe672010-10-04 22:28:36 +00001793 NULL,
Greg Claytona7015092010-09-18 01:14:36 +00001794 eStepTypeTrace,
1795 eStepScopeInstruction)));
Greg Clayton66111032010-06-23 01:19:29 +00001796
Greg Claytona7015092010-09-18 01:14:36 +00001797 LoadSubCommand ("step-inst-over", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1798 interpreter,
1799 "thread step-inst-over",
1800 "Single step one instruction in specified thread (current thread, if none specified), stepping over calls.",
Caroline Tice405fe672010-10-04 22:28:36 +00001801 NULL,
Greg Claytona7015092010-09-18 01:14:36 +00001802 eStepTypeTraceOver,
1803 eStepScopeInstruction)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001804}
1805
1806CommandObjectMultiwordThread::~CommandObjectMultiwordThread ()
1807{
1808}
1809
1810