blob: 48435134ed34e6cecdac73fd6fe8519a86897749 [file] [log] [blame]
Chris Lattner24943d22010-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 Ingham84cdc152010-06-15 19:49:27 +000016#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/State.h"
18#include "lldb/Core/SourceManager.h"
19
Greg Claytoncd548032011-02-01 01:31:41 +000020#include "lldb/Host/Host.h"
21
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Interpreter/CommandInterpreter.h"
23#include "lldb/Interpreter/CommandReturnObject.h"
24
25#include "lldb/Target/Process.h"
26#include "lldb/Target/RegisterContext.h"
27#include "lldb/Target/Target.h"
28#include "lldb/Target/Thread.h"
29#include "lldb/Target/ThreadPlan.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Target/ThreadPlanStepInstruction.h"
31#include "lldb/Target/ThreadPlanStepOut.h"
32#include "lldb/Target/ThreadPlanStepRange.h"
33#include "lldb/Target/ThreadPlanStepInRange.h"
34#include "lldb/Symbol/LineTable.h"
35#include "lldb/Symbol/LineEntry.h"
36
37using namespace lldb;
38using namespace lldb_private;
39
40
Chris Lattner24943d22010-06-08 16:52:24 +000041//-------------------------------------------------------------------------
42// CommandObjectThreadBacktrace
43//-------------------------------------------------------------------------
44
45class CommandObjectThreadBacktrace : public CommandObject
46{
47public:
48
Jim Ingham8ab1a802010-08-26 23:36:03 +000049 class CommandOptions : public Options
50 {
51 public:
52
Greg Claytonf15996e2011-04-07 22:46:35 +000053 CommandOptions (CommandInterpreter &interpreter) :
54 Options(interpreter)
Jim Ingham8ab1a802010-08-26 23:36:03 +000055 {
Greg Clayton143fcc32011-04-13 00:18:08 +000056 // Keep default values of all options in one place: OptionParsingStarting ()
57 OptionParsingStarting ();
Jim Ingham8ab1a802010-08-26 23:36:03 +000058 }
59
60 virtual
61 ~CommandOptions ()
62 {
63 }
64
65 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +000066 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham8ab1a802010-08-26 23:36:03 +000067 {
68 Error error;
69 char short_option = (char) m_getopt_table[option_idx].val;
70
71 switch (short_option)
72 {
73 case 'c':
74 {
75 bool success;
76 int32_t input_count = Args::StringToSInt32 (option_arg, -1, 0, &success);
77 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +000078 error.SetErrorStringWithFormat("invalid integer value for option '%c'", short_option);
Jim Ingham8ab1a802010-08-26 23:36:03 +000079 if (input_count < -1)
80 m_count = UINT32_MAX;
81 else
82 m_count = input_count;
83 }
84 break;
85 case 's':
86 {
87 bool success;
88 m_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
89 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +000090 error.SetErrorStringWithFormat("invalid integer value for option '%c'", short_option);
Jim Ingham8ab1a802010-08-26 23:36:03 +000091 }
92 break;
93 default:
Greg Clayton9c236732011-10-26 00:56:27 +000094 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Jim Ingham8ab1a802010-08-26 23:36:03 +000095 break;
96
97 }
98 return error;
99 }
100
101 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000102 OptionParsingStarting ()
Jim Ingham8ab1a802010-08-26 23:36:03 +0000103 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000104 m_count = UINT32_MAX;
Jim Ingham8ab1a802010-08-26 23:36:03 +0000105 m_start = 0;
106 }
107
Greg Claytonb3448432011-03-24 21:19:54 +0000108 const OptionDefinition*
Jim Ingham8ab1a802010-08-26 23:36:03 +0000109 GetDefinitions ()
110 {
111 return g_option_table;
112 }
113
114 // Options table: Required for subclasses of Options.
115
Greg Claytonb3448432011-03-24 21:19:54 +0000116 static OptionDefinition g_option_table[];
Jim Ingham8ab1a802010-08-26 23:36:03 +0000117
118 // Instance variables to hold the values for command options.
119 uint32_t m_count;
120 uint32_t m_start;
121 };
122
Greg Clayton238c0a12010-09-18 01:14:36 +0000123 CommandObjectThreadBacktrace (CommandInterpreter &interpreter) :
124 CommandObject (interpreter,
125 "thread backtrace",
Caroline Tice31fbb642010-09-08 22:08:58 +0000126 "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.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000127 NULL,
Chris Lattner24943d22010-06-08 16:52:24 +0000128 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused),
Greg Claytonf15996e2011-04-07 22:46:35 +0000129 m_options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000130 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000131 CommandArgumentEntry arg;
132 CommandArgumentData thread_idx_arg;
133
134 // Define the first (and only) variant of this arg.
135 thread_idx_arg.arg_type = eArgTypeThreadIndex;
136 thread_idx_arg.arg_repetition = eArgRepeatStar;
137
138 // There is only one variant this argument could be; put it into the argument entry.
139 arg.push_back (thread_idx_arg);
140
141 // Push the data for the first argument into the m_arguments vector.
142 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000143 }
144
145 ~CommandObjectThreadBacktrace()
146 {
147 }
148
Jim Ingham8ab1a802010-08-26 23:36:03 +0000149 virtual Options *
150 GetOptions ()
151 {
152 return &m_options;
153 }
Chris Lattner24943d22010-06-08 16:52:24 +0000154
Greg Clayton63094e02010-06-23 01:19:29 +0000155 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000156 Execute (Args& command, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000157 {
Jim Inghameb10f7b2010-08-27 00:58:05 +0000158 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000159 Stream &strm = result.GetOutputStream();
160
161 // Don't show source context when doing backtraces.
162 const uint32_t num_frames_with_source = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000163 if (command.GetArgumentCount() == 0)
164 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000165 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +0000166 Thread *thread = exe_ctx.GetThreadPtr();
167 if (thread)
Chris Lattner24943d22010-06-08 16:52:24 +0000168 {
Johnny Chen2d268cb2011-06-02 18:02:15 +0000169 // Thread::GetStatus() returns the number of frames shown.
Greg Clayton567e7f32011-09-22 04:58:26 +0000170 if (thread->GetStatus (strm,
171 m_options.m_start,
172 m_options.m_count,
173 num_frames_with_source))
Chris Lattner24943d22010-06-08 16:52:24 +0000174 {
175 result.SetStatus (eReturnStatusSuccessFinishResult);
176 }
177 }
178 else
179 {
180 result.AppendError ("invalid thread");
181 result.SetStatus (eReturnStatusFailed);
182 }
183 }
Jim Inghameb10f7b2010-08-27 00:58:05 +0000184 else if (command.GetArgumentCount() == 1 && ::strcmp (command.GetArgumentAtIndex(0), "all") == 0)
185 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000186 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Jim Inghameb10f7b2010-08-27 00:58:05 +0000187 uint32_t num_threads = process->GetThreadList().GetSize();
188 for (uint32_t i = 0; i < num_threads; i++)
189 {
190 ThreadSP thread_sp = process->GetThreadList().GetThreadAtIndex(i);
Johnny Chen05750a62011-06-01 23:19:52 +0000191 if (!thread_sp->GetStatus (strm,
192 m_options.m_start,
193 m_options.m_count,
194 num_frames_with_source))
Jim Inghameb10f7b2010-08-27 00:58:05 +0000195 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000196 result.AppendErrorWithFormat ("error displaying backtrace for thread: \"0x%4.4x\"\n", i);
Jim Inghameb10f7b2010-08-27 00:58:05 +0000197 result.SetStatus (eReturnStatusFailed);
198 return false;
199 }
Jim Ingham7868bcc2011-07-26 02:39:59 +0000200
201 if (i < num_threads - 1)
202 result.AppendMessage("");
203
Jim Inghameb10f7b2010-08-27 00:58:05 +0000204 }
205 }
Chris Lattner24943d22010-06-08 16:52:24 +0000206 else
207 {
Jim Inghameb10f7b2010-08-27 00:58:05 +0000208 uint32_t num_args = command.GetArgumentCount();
Greg Clayton567e7f32011-09-22 04:58:26 +0000209 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Jim Inghameb10f7b2010-08-27 00:58:05 +0000210 std::vector<ThreadSP> thread_sps;
211
212 for (uint32_t i = 0; i < num_args; i++)
213 {
214 bool success;
215
216 uint32_t thread_idx = Args::StringToUInt32(command.GetArgumentAtIndex(i), 0, 0, &success);
217 if (!success)
218 {
219 result.AppendErrorWithFormat ("invalid thread specification: \"%s\"\n", command.GetArgumentAtIndex(i));
220 result.SetStatus (eReturnStatusFailed);
221 return false;
222 }
223
224 thread_sps.push_back(process->GetThreadList().FindThreadByIndexID(thread_idx));
225
226 if (!thread_sps[i])
227 {
228 result.AppendErrorWithFormat ("no thread with index: \"%s\"\n", command.GetArgumentAtIndex(i));
229 result.SetStatus (eReturnStatusFailed);
230 return false;
231 }
232
233 }
234
235 for (uint32_t i = 0; i < num_args; i++)
236 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000237 if (!thread_sps[i]->GetStatus (strm,
238 m_options.m_start,
239 m_options.m_count,
240 num_frames_with_source))
Jim Inghameb10f7b2010-08-27 00:58:05 +0000241 {
242 result.AppendErrorWithFormat ("error displaying backtrace for thread: \"%s\"\n", command.GetArgumentAtIndex(i));
243 result.SetStatus (eReturnStatusFailed);
244 return false;
245 }
246
247 if (i < num_args - 1)
248 result.AppendMessage("");
249 }
Chris Lattner24943d22010-06-08 16:52:24 +0000250 }
251 return result.Succeeded();
252 }
253protected:
Jim Ingham8ab1a802010-08-26 23:36:03 +0000254 CommandOptions m_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000255};
256
Greg Claytonb3448432011-03-24 21:19:54 +0000257OptionDefinition
Jim Ingham8ab1a802010-08-26 23:36:03 +0000258CommandObjectThreadBacktrace::CommandOptions::g_option_table[] =
259{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000260{ LLDB_OPT_SET_1, false, "count", 'c', required_argument, NULL, 0, eArgTypeCount, "How many frames to display (-1 for all)"},
Caroline Tice43b014a2010-10-04 22:28:36 +0000261{ LLDB_OPT_SET_1, false, "start", 's', required_argument, NULL, 0, eArgTypeFrameIndex, "Frame in which to start the backtrace"},
Caroline Tice4d6675c2010-10-01 19:59:14 +0000262{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Jim Ingham8ab1a802010-08-26 23:36:03 +0000263};
Chris Lattner24943d22010-06-08 16:52:24 +0000264
Greg Claytonc0418152010-07-07 17:07:17 +0000265enum StepScope
Chris Lattner24943d22010-06-08 16:52:24 +0000266{
267 eStepScopeSource,
268 eStepScopeInstruction
269};
270
271class CommandObjectThreadStepWithTypeAndScope : public CommandObject
272{
273public:
274
275 class CommandOptions : public Options
276 {
277 public:
278
Greg Claytonf15996e2011-04-07 22:46:35 +0000279 CommandOptions (CommandInterpreter &interpreter) :
280 Options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000281 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000282 // Keep default values of all options in one place: OptionParsingStarting ()
283 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000284 }
285
286 virtual
287 ~CommandOptions ()
288 {
289 }
290
291 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000292 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000293 {
294 Error error;
295 char short_option = (char) m_getopt_table[option_idx].val;
296
297 switch (short_option)
298 {
Greg Clayton8d3802d2010-10-08 04:20:14 +0000299 case 'a':
Chris Lattner24943d22010-06-08 16:52:24 +0000300 {
301 bool success;
302 m_avoid_no_debug = Args::StringToBoolean (option_arg, true, &success);
303 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +0000304 error.SetErrorStringWithFormat("invalid boolean value for option '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000305 }
306 break;
Greg Clayton8d3802d2010-10-08 04:20:14 +0000307
308 case 'm':
Chris Lattner24943d22010-06-08 16:52:24 +0000309 {
Chris Lattner24943d22010-06-08 16:52:24 +0000310 OptionEnumValueElement *enum_values = g_option_table[option_idx].enum_values;
Greg Clayton61aca5d2011-10-07 18:58:12 +0000311 m_run_mode = (lldb::RunMode) Args::StringToOptionEnum(option_arg, enum_values, eOnlyDuringStepping, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000312 }
313 break;
Greg Clayton8d3802d2010-10-08 04:20:14 +0000314
315 case 'r':
Jim Ingham809ab9b2010-07-10 02:27:39 +0000316 {
317 m_avoid_regexp.clear();
318 m_avoid_regexp.assign(option_arg);
319 }
320 break;
Greg Clayton8d3802d2010-10-08 04:20:14 +0000321
322 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000323 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Clayton8d3802d2010-10-08 04:20:14 +0000324 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000325
326 }
327 return error;
328 }
329
330 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000331 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000332 {
Chris Lattner24943d22010-06-08 16:52:24 +0000333 m_avoid_no_debug = true;
334 m_run_mode = eOnlyDuringStepping;
Jim Ingham809ab9b2010-07-10 02:27:39 +0000335 m_avoid_regexp.clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000336 }
337
Greg Claytonb3448432011-03-24 21:19:54 +0000338 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000339 GetDefinitions ()
340 {
341 return g_option_table;
342 }
343
344 // Options table: Required for subclasses of Options.
345
Greg Claytonb3448432011-03-24 21:19:54 +0000346 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000347
348 // Instance variables to hold the values for command options.
349 bool m_avoid_no_debug;
350 RunMode m_run_mode;
Jim Ingham809ab9b2010-07-10 02:27:39 +0000351 std::string m_avoid_regexp;
Chris Lattner24943d22010-06-08 16:52:24 +0000352 };
353
Greg Clayton238c0a12010-09-18 01:14:36 +0000354 CommandObjectThreadStepWithTypeAndScope (CommandInterpreter &interpreter,
355 const char *name,
356 const char *help,
357 const char *syntax,
358 uint32_t flags,
359 StepType step_type,
360 StepScope step_scope) :
361 CommandObject (interpreter, name, help, syntax, flags),
Chris Lattner24943d22010-06-08 16:52:24 +0000362 m_step_type (step_type),
363 m_step_scope (step_scope),
Greg Claytonf15996e2011-04-07 22:46:35 +0000364 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000365 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000366 CommandArgumentEntry arg;
367 CommandArgumentData thread_id_arg;
368
369 // Define the first (and only) variant of this arg.
370 thread_id_arg.arg_type = eArgTypeThreadID;
371 thread_id_arg.arg_repetition = eArgRepeatOptional;
372
373 // There is only one variant this argument could be; put it into the argument entry.
374 arg.push_back (thread_id_arg);
375
376 // Push the data for the first argument into the m_arguments vector.
377 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000378 }
379
380 virtual
381 ~CommandObjectThreadStepWithTypeAndScope ()
382 {
383 }
384
385 virtual
386 Options *
387 GetOptions ()
388 {
389 return &m_options;
390 }
391
392 virtual bool
Greg Clayton63094e02010-06-23 01:19:29 +0000393 Execute
394 (
Greg Clayton63094e02010-06-23 01:19:29 +0000395 Args& command,
396 CommandReturnObject &result
397 )
Chris Lattner24943d22010-06-08 16:52:24 +0000398 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000399 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton238c0a12010-09-18 01:14:36 +0000400 bool synchronous_execution = m_interpreter.GetSynchronous();
Chris Lattner24943d22010-06-08 16:52:24 +0000401
402 if (process == NULL)
403 {
404 result.AppendError ("need a valid process to step");
405 result.SetStatus (eReturnStatusFailed);
406
407 }
408 else
409 {
410 const uint32_t num_threads = process->GetThreadList().GetSize();
411 Thread *thread = NULL;
412
413 if (command.GetArgumentCount() == 0)
414 {
Jim Inghamc8332952010-08-26 21:32:51 +0000415 thread = process->GetThreadList().GetSelectedThread().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000416 if (thread == NULL)
417 {
Jim Ingham8ab1a802010-08-26 23:36:03 +0000418 result.AppendError ("no selected thread in process");
Chris Lattner24943d22010-06-08 16:52:24 +0000419 result.SetStatus (eReturnStatusFailed);
420 return false;
421 }
422 }
423 else
424 {
425 const char *thread_idx_cstr = command.GetArgumentAtIndex(0);
426 uint32_t step_thread_idx = Args::StringToUInt32 (thread_idx_cstr, LLDB_INVALID_INDEX32);
427 if (step_thread_idx == LLDB_INVALID_INDEX32)
428 {
Greg Clayton9c236732011-10-26 00:56:27 +0000429 result.AppendErrorWithFormat ("invalid thread index '%s'.\n", thread_idx_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +0000430 result.SetStatus (eReturnStatusFailed);
431 return false;
432 }
433 thread = process->GetThreadList().FindThreadByIndexID(step_thread_idx).get();
434 if (thread == NULL)
435 {
436 result.AppendErrorWithFormat ("Thread index %u is out of range (valid values are 0 - %u).\n",
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000437 step_thread_idx, num_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000438 result.SetStatus (eReturnStatusFailed);
439 return false;
440 }
441 }
442
443 const bool abort_other_plans = false;
444 const lldb::RunMode stop_other_threads = m_options.m_run_mode;
445
446 // This is a bit unfortunate, but not all the commands in this command object support
447 // only while stepping, so I use the bool for them.
448 bool bool_stop_other_threads;
449 if (m_options.m_run_mode == eAllThreads)
450 bool_stop_other_threads = false;
451 else
452 bool_stop_other_threads = true;
453
Jim Ingham88e3de22012-05-03 21:19:36 +0000454 ThreadPlan *new_plan = NULL;
455
Chris Lattner24943d22010-06-08 16:52:24 +0000456 if (m_step_type == eStepTypeInto)
457 {
458 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000459
460 if (frame->HasDebugInformation ())
461 {
462 new_plan = thread->QueueThreadPlanForStepRange (abort_other_plans, m_step_type,
463 frame->GetSymbolContext(eSymbolContextEverything).line_entry.range,
464 frame->GetSymbolContext(eSymbolContextEverything),
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000465 stop_other_threads,
466 m_options.m_avoid_no_debug);
Jim Ingham809ab9b2010-07-10 02:27:39 +0000467 if (new_plan && !m_options.m_avoid_regexp.empty())
468 {
469 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (new_plan);
470 step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str());
471 }
Chris Lattner24943d22010-06-08 16:52:24 +0000472 }
473 else
474 new_plan = thread->QueueThreadPlanForStepSingleInstruction (false, abort_other_plans, bool_stop_other_threads);
Jim Ingham88e3de22012-05-03 21:19:36 +0000475
Chris Lattner24943d22010-06-08 16:52:24 +0000476 }
477 else if (m_step_type == eStepTypeOver)
478 {
479 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000480
481 if (frame->HasDebugInformation())
482 new_plan = thread->QueueThreadPlanForStepRange (abort_other_plans,
483 m_step_type,
484 frame->GetSymbolContext(eSymbolContextEverything).line_entry.range,
485 frame->GetSymbolContext(eSymbolContextEverything),
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000486 stop_other_threads,
487 false);
Chris Lattner24943d22010-06-08 16:52:24 +0000488 else
489 new_plan = thread->QueueThreadPlanForStepSingleInstruction (true,
490 abort_other_plans,
491 bool_stop_other_threads);
492
Chris Lattner24943d22010-06-08 16:52:24 +0000493 }
494 else if (m_step_type == eStepTypeTrace)
495 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000496 new_plan = thread->QueueThreadPlanForStepSingleInstruction (false, abort_other_plans, bool_stop_other_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000497 }
498 else if (m_step_type == eStepTypeTraceOver)
499 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000500 new_plan = thread->QueueThreadPlanForStepSingleInstruction (true, abort_other_plans, bool_stop_other_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000501 }
502 else if (m_step_type == eStepTypeOut)
503 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000504 new_plan = thread->QueueThreadPlanForStepOut (abort_other_plans,
505 NULL,
506 false,
507 bool_stop_other_threads,
508 eVoteYes,
509 eVoteNoOpinion,
510 thread->GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000511 }
512 else
513 {
514 result.AppendError ("step type is not supported");
515 result.SetStatus (eReturnStatusFailed);
Jim Ingham88e3de22012-05-03 21:19:36 +0000516 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000517 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000518
519 // If we got a new plan, then set it to be a master plan (User level Plans should be master plans
520 // so that they can be interruptible). Then resume the process.
521
522 if (new_plan != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000523 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000524 new_plan->SetIsMasterPlan (true);
525 new_plan->SetOkayToDiscard (false);
526
Jim Inghamc8332952010-08-26 21:32:51 +0000527 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
Jim Ingham88e3de22012-05-03 21:19:36 +0000528 process->Resume ();
529
530
531 if (synchronous_execution)
532 {
533 StateType state = process->WaitForProcessToStop (NULL);
534
535 //EventSP event_sp;
536 //StateType state = process->WaitForStateChangedEvents (NULL, event_sp);
537 //while (! StateIsStoppedState (state))
538 // {
539 // state = process->WaitForStateChangedEvents (NULL, event_sp);
540 // }
541 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
542 result.SetDidChangeProcessState (true);
543 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
544 result.SetStatus (eReturnStatusSuccessFinishNoResult);
545 }
Jim Ingham53628e72012-05-16 00:37:40 +0000546 else
547 {
548 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
549 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000550 }
551 else
552 {
553 result.AppendError ("Couldn't find thread plan to implement step type.");
554 result.SetStatus (eReturnStatusFailed);
Chris Lattner24943d22010-06-08 16:52:24 +0000555 }
556 }
557 return result.Succeeded();
558 }
559
560protected:
561 StepType m_step_type;
562 StepScope m_step_scope;
563 CommandOptions m_options;
564};
565
Greg Claytonb3448432011-03-24 21:19:54 +0000566static OptionEnumValueElement
Chris Lattner24943d22010-06-08 16:52:24 +0000567g_tri_running_mode[] =
568{
Greg Claytonfe424a92010-09-18 03:37:20 +0000569{ eOnlyThisThread, "this-thread", "Run only this thread"},
570{ eAllThreads, "all-threads", "Run all threads"},
571{ eOnlyDuringStepping, "while-stepping", "Run only this thread while stepping"},
Chris Lattner24943d22010-06-08 16:52:24 +0000572{ 0, NULL, NULL }
573};
574
Greg Claytonb3448432011-03-24 21:19:54 +0000575static OptionEnumValueElement
Chris Lattner24943d22010-06-08 16:52:24 +0000576g_duo_running_mode[] =
577{
Greg Claytonfe424a92010-09-18 03:37:20 +0000578{ eOnlyThisThread, "this-thread", "Run only this thread"},
579{ eAllThreads, "all-threads", "Run all threads"},
Chris Lattner24943d22010-06-08 16:52:24 +0000580{ 0, NULL, NULL }
581};
582
Greg Claytonb3448432011-03-24 21:19:54 +0000583OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000584CommandObjectThreadStepWithTypeAndScope::CommandOptions::g_option_table[] =
585{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000586{ LLDB_OPT_SET_1, false, "avoid-no-debug", 'a', required_argument, NULL, 0, eArgTypeBoolean, "A boolean value that sets whether step-in will step over functions with no debug information."},
587{ LLDB_OPT_SET_1, false, "run-mode", 'm', required_argument, g_tri_running_mode, 0, eArgTypeRunMode, "Determine how to run other threads while stepping the current thread."},
588{ LLDB_OPT_SET_1, false, "step-over-regexp",'r', required_argument, NULL, 0, eArgTypeRegularExpression, "A regular expression that defines function names to step over."},
589{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000590};
591
592
593//-------------------------------------------------------------------------
594// CommandObjectThreadContinue
595//-------------------------------------------------------------------------
596
597class CommandObjectThreadContinue : public CommandObject
598{
599public:
600
Greg Clayton238c0a12010-09-18 01:14:36 +0000601 CommandObjectThreadContinue (CommandInterpreter &interpreter) :
602 CommandObject (interpreter,
603 "thread continue",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000604 "Continue execution of one or more threads in an active process.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000605 NULL,
Chris Lattner24943d22010-06-08 16:52:24 +0000606 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
607 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000608 CommandArgumentEntry arg;
609 CommandArgumentData thread_idx_arg;
610
611 // Define the first (and only) variant of this arg.
612 thread_idx_arg.arg_type = eArgTypeThreadIndex;
613 thread_idx_arg.arg_repetition = eArgRepeatPlus;
614
615 // There is only one variant this argument could be; put it into the argument entry.
616 arg.push_back (thread_idx_arg);
617
618 // Push the data for the first argument into the m_arguments vector.
619 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000620 }
621
622
623 virtual
624 ~CommandObjectThreadContinue ()
625 {
626 }
627
628 virtual bool
Greg Clayton63094e02010-06-23 01:19:29 +0000629 Execute
630 (
Greg Clayton63094e02010-06-23 01:19:29 +0000631 Args& command,
632 CommandReturnObject &result
633 )
Chris Lattner24943d22010-06-08 16:52:24 +0000634 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000635 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000636
Greg Clayton238c0a12010-09-18 01:14:36 +0000637 if (!m_interpreter.GetDebugger().GetSelectedTarget().get())
Chris Lattner24943d22010-06-08 16:52:24 +0000638 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000639 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +0000640 result.SetStatus (eReturnStatusFailed);
641 return false;
642 }
643
Greg Clayton567e7f32011-09-22 04:58:26 +0000644 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000645 if (process == NULL)
646 {
647 result.AppendError ("no process exists. Cannot continue");
648 result.SetStatus (eReturnStatusFailed);
649 return false;
650 }
651
652 StateType state = process->GetState();
653 if ((state == eStateCrashed) || (state == eStateStopped) || (state == eStateSuspended))
654 {
655 const uint32_t num_threads = process->GetThreadList().GetSize();
656 uint32_t idx;
657 const size_t argc = command.GetArgumentCount();
658 if (argc > 0)
659 {
660 std::vector<uint32_t> resume_thread_indexes;
661 for (uint32_t i=0; i<argc; ++i)
662 {
Jim Inghamd07b4f52012-05-31 20:48:41 +0000663 bool success;
664 const int base = 0;
665 idx = Args::StringToUInt32 (command.GetArgumentAtIndex(i), LLDB_INVALID_INDEX32, base, &success);
666 if (!success)
667 {
668 result.AppendErrorWithFormat ("invalid value for thread index: %s.", command.GetArgumentAtIndex(i));
669 result.SetStatus (eReturnStatusFailed);
670 return false;
671 }
672 else if (process->GetThreadList().FindThreadByIndexID(idx))
673 {
674 if (find(resume_thread_indexes.begin(), resume_thread_indexes.end(), idx) == resume_thread_indexes.end())
675 resume_thread_indexes.push_back(idx);
676 }
Chris Lattner24943d22010-06-08 16:52:24 +0000677 else
Jim Inghamd07b4f52012-05-31 20:48:41 +0000678 {
679 result.AppendErrorWithFormat("thread index %u out of range.\n", idx);
680 result.SetStatus (eReturnStatusFailed);
681 return false;
682 }
Chris Lattner24943d22010-06-08 16:52:24 +0000683 }
684
685 if (resume_thread_indexes.empty())
686 {
687 result.AppendError ("no valid thread indexes were specified");
688 result.SetStatus (eReturnStatusFailed);
689 return false;
690 }
691 else
692 {
Jim Inghamd07b4f52012-05-31 20:48:41 +0000693 if (resume_thread_indexes.size() == 1)
694 result.AppendMessageWithFormat ("Resuming thread: ");
695 else
696 result.AppendMessageWithFormat ("Resuming threads: ");
697
Chris Lattner24943d22010-06-08 16:52:24 +0000698 for (idx=0; idx<num_threads; ++idx)
699 {
Greg Claytona2243772012-05-31 00:29:20 +0000700 Thread *thread = process->GetThreadList().FindThreadByIndexID(idx).get();
Jim Inghamd07b4f52012-05-31 20:48:41 +0000701 std::vector<uint32_t>::iterator this_thread_pos = find(resume_thread_indexes.begin(), resume_thread_indexes.end(), thread->GetIndexID());
702
703 if (this_thread_pos != resume_thread_indexes.end())
Chris Lattner24943d22010-06-08 16:52:24 +0000704 {
Jim Inghamd07b4f52012-05-31 20:48:41 +0000705 resume_thread_indexes.erase(this_thread_pos);
706 if (resume_thread_indexes.size() > 0)
707 result.AppendMessageWithFormat ("%u, ", thread->GetIndexID());
708 else
709 result.AppendMessageWithFormat ("%u ", thread->GetIndexID());
710
Chris Lattner24943d22010-06-08 16:52:24 +0000711 thread->SetResumeState (eStateRunning);
712 }
713 else
714 {
715 thread->SetResumeState (eStateSuspended);
716 }
717 }
Greg Clayton444e35b2011-10-19 18:09:39 +0000718 result.AppendMessageWithFormat ("in process %llu\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000719 }
720 }
721 else
722 {
Jim Inghamc8332952010-08-26 21:32:51 +0000723 Thread *current_thread = process->GetThreadList().GetSelectedThread().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000724 if (current_thread == NULL)
725 {
726 result.AppendError ("the process doesn't have a current thread");
727 result.SetStatus (eReturnStatusFailed);
728 return false;
729 }
730 // Set the actions that the threads should each take when resuming
731 for (idx=0; idx<num_threads; ++idx)
732 {
Greg Claytona2243772012-05-31 00:29:20 +0000733 Thread *thread = process->GetThreadList().FindThreadByIndexID(idx).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000734 if (thread == current_thread)
735 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000736 result.AppendMessageWithFormat ("Resuming thread 0x%4.4llx in process %llu\n", thread->GetID(), process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000737 thread->SetResumeState (eStateRunning);
738 }
739 else
740 {
741 thread->SetResumeState (eStateSuspended);
742 }
743 }
744 }
745
746 Error error (process->Resume());
747 if (error.Success())
748 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000749 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000750 if (synchronous_execution)
751 {
Greg Claytonbef15832010-07-14 00:18:15 +0000752 state = process->WaitForProcessToStop (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000753
754 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +0000755 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +0000756 result.SetStatus (eReturnStatusSuccessFinishNoResult);
757 }
758 else
759 {
760 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
761 }
762 }
763 else
764 {
765 result.AppendErrorWithFormat("Failed to resume process: %s\n", error.AsCString());
766 result.SetStatus (eReturnStatusFailed);
767 }
768 }
769 else
770 {
771 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
772 StateAsCString(state));
773 result.SetStatus (eReturnStatusFailed);
774 }
775
776 return result.Succeeded();
777 }
778
779};
780
781//-------------------------------------------------------------------------
782// CommandObjectThreadUntil
783//-------------------------------------------------------------------------
784
785class CommandObjectThreadUntil : public CommandObject
786{
787public:
788
789 class CommandOptions : public Options
790 {
791 public:
792 uint32_t m_thread_idx;
793 uint32_t m_frame_idx;
794
Greg Claytonf15996e2011-04-07 22:46:35 +0000795 CommandOptions (CommandInterpreter &interpreter) :
796 Options (interpreter),
Chris Lattner24943d22010-06-08 16:52:24 +0000797 m_thread_idx(LLDB_INVALID_THREAD_ID),
798 m_frame_idx(LLDB_INVALID_FRAME_ID)
799 {
Greg Clayton143fcc32011-04-13 00:18:08 +0000800 // Keep default values of all options in one place: OptionParsingStarting ()
801 OptionParsingStarting ();
Chris Lattner24943d22010-06-08 16:52:24 +0000802 }
803
804 virtual
805 ~CommandOptions ()
806 {
807 }
808
809 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000810 SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000811 {
812 Error error;
813 char short_option = (char) m_getopt_table[option_idx].val;
814
815 switch (short_option)
816 {
817 case 't':
818 {
Greg Claytonbef15832010-07-14 00:18:15 +0000819 m_thread_idx = Args::StringToUInt32 (option_arg, LLDB_INVALID_INDEX32);
Chris Lattner24943d22010-06-08 16:52:24 +0000820 if (m_thread_idx == LLDB_INVALID_INDEX32)
821 {
Greg Clayton9c236732011-10-26 00:56:27 +0000822 error.SetErrorStringWithFormat ("invalid thread index '%s'", option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000823 }
824 }
825 break;
826 case 'f':
827 {
828 m_frame_idx = Args::StringToUInt32 (option_arg, LLDB_INVALID_FRAME_ID);
829 if (m_frame_idx == LLDB_INVALID_FRAME_ID)
830 {
Greg Clayton9c236732011-10-26 00:56:27 +0000831 error.SetErrorStringWithFormat ("invalid frame index '%s'", option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000832 }
833 }
834 break;
835 case 'm':
836 {
Chris Lattner24943d22010-06-08 16:52:24 +0000837 OptionEnumValueElement *enum_values = g_option_table[option_idx].enum_values;
Greg Clayton61aca5d2011-10-07 18:58:12 +0000838 lldb::RunMode run_mode = (lldb::RunMode) Args::StringToOptionEnum(option_arg, enum_values, eOnlyDuringStepping, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000839
Greg Clayton61aca5d2011-10-07 18:58:12 +0000840 if (error.Success())
841 {
842 if (run_mode == eAllThreads)
843 m_stop_others = false;
844 else
845 m_stop_others = true;
846 }
Chris Lattner24943d22010-06-08 16:52:24 +0000847 }
848 break;
849 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000850 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000851 break;
852
853 }
854 return error;
855 }
856
857 void
Greg Clayton143fcc32011-04-13 00:18:08 +0000858 OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000859 {
Chris Lattner24943d22010-06-08 16:52:24 +0000860 m_thread_idx = LLDB_INVALID_THREAD_ID;
861 m_frame_idx = 0;
862 m_stop_others = false;
863 }
864
Greg Claytonb3448432011-03-24 21:19:54 +0000865 const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000866 GetDefinitions ()
867 {
868 return g_option_table;
869 }
870
871 uint32_t m_step_thread_idx;
872 bool m_stop_others;
873
874 // Options table: Required for subclasses of Options.
875
Greg Claytonb3448432011-03-24 21:19:54 +0000876 static OptionDefinition g_option_table[];
Chris Lattner24943d22010-06-08 16:52:24 +0000877
878 // Instance variables to hold the values for command options.
879 };
880
Greg Clayton238c0a12010-09-18 01:14:36 +0000881 CommandObjectThreadUntil (CommandInterpreter &interpreter) :
882 CommandObject (interpreter,
883 "thread until",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000884 "Run the current or specified thread until it reaches a given line number or leaves the current function.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000885 NULL,
Chris Lattner24943d22010-06-08 16:52:24 +0000886 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused),
Greg Claytonf15996e2011-04-07 22:46:35 +0000887 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000888 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000889 CommandArgumentEntry arg;
890 CommandArgumentData line_num_arg;
891
892 // Define the first (and only) variant of this arg.
893 line_num_arg.arg_type = eArgTypeLineNum;
894 line_num_arg.arg_repetition = eArgRepeatPlain;
895
896 // There is only one variant this argument could be; put it into the argument entry.
897 arg.push_back (line_num_arg);
898
899 // Push the data for the first argument into the m_arguments vector.
900 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000901 }
902
903
904 virtual
905 ~CommandObjectThreadUntil ()
906 {
907 }
908
909 virtual
910 Options *
911 GetOptions ()
912 {
913 return &m_options;
914 }
915
916 virtual bool
Greg Clayton63094e02010-06-23 01:19:29 +0000917 Execute
918 (
Greg Clayton63094e02010-06-23 01:19:29 +0000919 Args& command,
920 CommandReturnObject &result
921 )
Chris Lattner24943d22010-06-08 16:52:24 +0000922 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000923 bool synchronous_execution = m_interpreter.GetSynchronous ();
Chris Lattner24943d22010-06-08 16:52:24 +0000924
Greg Clayton238c0a12010-09-18 01:14:36 +0000925 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Claytoneea26402010-09-14 23:36:40 +0000926 if (target == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000927 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000928 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +0000929 result.SetStatus (eReturnStatusFailed);
930 return false;
931 }
932
Greg Clayton567e7f32011-09-22 04:58:26 +0000933 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000934 if (process == NULL)
935 {
936 result.AppendError ("need a valid process to step");
937 result.SetStatus (eReturnStatusFailed);
938
939 }
940 else
941 {
942 Thread *thread = NULL;
943 uint32_t line_number;
944
945 if (command.GetArgumentCount() != 1)
946 {
947 result.AppendErrorWithFormat ("No line number provided:\n%s", GetSyntax());
948 result.SetStatus (eReturnStatusFailed);
949 return false;
950 }
951
952 line_number = Args::StringToUInt32 (command.GetArgumentAtIndex(0), UINT32_MAX);
953 if (line_number == UINT32_MAX)
954 {
Greg Clayton9c236732011-10-26 00:56:27 +0000955 result.AppendErrorWithFormat ("invalid line number: '%s'.\n", command.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000956 result.SetStatus (eReturnStatusFailed);
957 return false;
958 }
959
960 if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID)
961 {
Jim Inghamc8332952010-08-26 21:32:51 +0000962 thread = process->GetThreadList().GetSelectedThread().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000963 }
964 else
965 {
Greg Claytona2243772012-05-31 00:29:20 +0000966 thread = process->GetThreadList().FindThreadByIndexID(m_options.m_thread_idx).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000967 }
968
969 if (thread == NULL)
970 {
971 const uint32_t num_threads = process->GetThreadList().GetSize();
Jim Inghamb07c62a2011-05-08 00:56:32 +0000972 result.AppendErrorWithFormat ("Thread index %u is out of range (valid values are 0 - %u).\n",
973 m_options.m_thread_idx,
Jim Inghamb07c62a2011-05-08 00:56:32 +0000974 num_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000975 result.SetStatus (eReturnStatusFailed);
976 return false;
977 }
978
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000979 const bool abort_other_plans = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000980
981 StackFrame *frame = thread->GetStackFrameAtIndex(m_options.m_frame_idx).get();
982 if (frame == NULL)
983 {
984
Jim Inghamb07c62a2011-05-08 00:56:32 +0000985 result.AppendErrorWithFormat ("Frame index %u is out of range for thread %u.\n",
986 m_options.m_frame_idx,
987 m_options.m_thread_idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000988 result.SetStatus (eReturnStatusFailed);
989 return false;
990 }
991
Jim Ingham88e3de22012-05-03 21:19:36 +0000992 ThreadPlan *new_plan = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000993
994 if (frame->HasDebugInformation ())
995 {
996 // Finally we got here... Translate the given line number to a bunch of addresses:
997 SymbolContext sc(frame->GetSymbolContext (eSymbolContextCompUnit));
998 LineTable *line_table = NULL;
999 if (sc.comp_unit)
1000 line_table = sc.comp_unit->GetLineTable();
1001
1002 if (line_table == NULL)
1003 {
1004 result.AppendErrorWithFormat ("Failed to resolve the line table for frame %u of thread index %u.\n",
1005 m_options.m_frame_idx, m_options.m_thread_idx);
1006 result.SetStatus (eReturnStatusFailed);
1007 return false;
1008 }
1009
1010 LineEntry function_start;
1011 uint32_t index_ptr = 0, end_ptr;
1012 std::vector<addr_t> address_list;
1013
1014 // Find the beginning & end index of the
1015 AddressRange fun_addr_range = sc.function->GetAddressRange();
1016 Address fun_start_addr = fun_addr_range.GetBaseAddress();
1017 line_table->FindLineEntryByAddress (fun_start_addr, function_start, &index_ptr);
1018
Jim Inghamb07c62a2011-05-08 00:56:32 +00001019 Address fun_end_addr(fun_start_addr.GetSection(),
1020 fun_start_addr.GetOffset() + fun_addr_range.GetByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +00001021 line_table->FindLineEntryByAddress (fun_end_addr, function_start, &end_ptr);
1022
Jim Inghamb07c62a2011-05-08 00:56:32 +00001023 bool all_in_function = true;
1024
Chris Lattner24943d22010-06-08 16:52:24 +00001025 while (index_ptr <= end_ptr)
1026 {
1027 LineEntry line_entry;
Jim Inghamd6d47972011-09-23 00:54:11 +00001028 const bool exact = false;
1029 index_ptr = sc.comp_unit->FindLineEntry(index_ptr, line_number, sc.comp_unit, exact, &line_entry);
Chris Lattner24943d22010-06-08 16:52:24 +00001030 if (index_ptr == UINT32_MAX)
1031 break;
1032
Greg Claytoneea26402010-09-14 23:36:40 +00001033 addr_t address = line_entry.range.GetBaseAddress().GetLoadAddress(target);
Chris Lattner24943d22010-06-08 16:52:24 +00001034 if (address != LLDB_INVALID_ADDRESS)
Jim Inghamb07c62a2011-05-08 00:56:32 +00001035 {
1036 if (fun_addr_range.ContainsLoadAddress (address, target))
1037 address_list.push_back (address);
1038 else
1039 all_in_function = false;
1040 }
Chris Lattner24943d22010-06-08 16:52:24 +00001041 index_ptr++;
1042 }
1043
Jim Inghamb07c62a2011-05-08 00:56:32 +00001044 if (address_list.size() == 0)
1045 {
1046 if (all_in_function)
1047 result.AppendErrorWithFormat ("No line entries matching until target.\n");
1048 else
1049 result.AppendErrorWithFormat ("Until target outside of the current function.\n");
1050
1051 result.SetStatus (eReturnStatusFailed);
1052 return false;
1053 }
1054
1055 new_plan = thread->QueueThreadPlanForStepUntil (abort_other_plans,
1056 &address_list.front(),
1057 address_list.size(),
1058 m_options.m_stop_others,
1059 thread->GetSelectedFrameIndex ());
Jim Ingham88e3de22012-05-03 21:19:36 +00001060 // User level plans should be master plans so they can be interrupted (e.g. by hitting a breakpoint)
1061 // and other plans executed by the user (stepping around the breakpoint) and then a "continue"
1062 // will resume the original plan.
1063 new_plan->SetIsMasterPlan (true);
Chris Lattner24943d22010-06-08 16:52:24 +00001064 new_plan->SetOkayToDiscard(false);
1065 }
1066 else
1067 {
Jim Inghamb07c62a2011-05-08 00:56:32 +00001068 result.AppendErrorWithFormat ("Frame index %u of thread %u has no debug information.\n",
1069 m_options.m_frame_idx,
1070 m_options.m_thread_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001071 result.SetStatus (eReturnStatusFailed);
1072 return false;
1073
1074 }
1075
Jim Inghamc8332952010-08-26 21:32:51 +00001076 process->GetThreadList().SetSelectedThreadByID (m_options.m_thread_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001077 Error error (process->Resume ());
1078 if (error.Success())
1079 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001080 result.AppendMessageWithFormat ("Process %llu resuming\n", process->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00001081 if (synchronous_execution)
1082 {
1083 StateType state = process->WaitForProcessToStop (NULL);
1084
1085 result.SetDidChangeProcessState (true);
Greg Clayton444e35b2011-10-19 18:09:39 +00001086 result.AppendMessageWithFormat ("Process %llu %s\n", process->GetID(), StateAsCString (state));
Chris Lattner24943d22010-06-08 16:52:24 +00001087 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1088 }
1089 else
1090 {
1091 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
1092 }
1093 }
1094 else
1095 {
1096 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
1097 result.SetStatus (eReturnStatusFailed);
1098 }
1099
1100 }
1101 return result.Succeeded();
1102 }
1103protected:
1104 CommandOptions m_options;
1105
1106};
1107
Greg Claytonb3448432011-03-24 21:19:54 +00001108OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +00001109CommandObjectThreadUntil::CommandOptions::g_option_table[] =
1110{
Caroline Tice43b014a2010-10-04 22:28:36 +00001111{ LLDB_OPT_SET_1, false, "frame", 'f', required_argument, NULL, 0, eArgTypeFrameIndex, "Frame index for until operation - defaults to 0"},
Caroline Tice4d6675c2010-10-01 19:59:14 +00001112{ LLDB_OPT_SET_1, false, "thread", 't', required_argument, NULL, 0, eArgTypeThreadIndex, "Thread index for the thread for until operation"},
1113{ LLDB_OPT_SET_1, false, "run-mode",'m', required_argument, g_duo_running_mode, 0, eArgTypeRunMode,"Determine how to run other threads while stepping this one"},
1114{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +00001115};
1116
1117
1118//-------------------------------------------------------------------------
1119// CommandObjectThreadSelect
1120//-------------------------------------------------------------------------
1121
1122class CommandObjectThreadSelect : public CommandObject
1123{
1124public:
1125
Greg Clayton238c0a12010-09-18 01:14:36 +00001126 CommandObjectThreadSelect (CommandInterpreter &interpreter) :
1127 CommandObject (interpreter,
1128 "thread select",
1129 "Select a thread as the currently active thread.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001130 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +00001131 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Chris Lattner24943d22010-06-08 16:52:24 +00001132 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001133 CommandArgumentEntry arg;
1134 CommandArgumentData thread_idx_arg;
1135
1136 // Define the first (and only) variant of this arg.
1137 thread_idx_arg.arg_type = eArgTypeThreadIndex;
1138 thread_idx_arg.arg_repetition = eArgRepeatPlain;
1139
1140 // There is only one variant this argument could be; put it into the argument entry.
1141 arg.push_back (thread_idx_arg);
1142
1143 // Push the data for the first argument into the m_arguments vector.
1144 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001145 }
1146
1147
1148 virtual
1149 ~CommandObjectThreadSelect ()
1150 {
1151 }
1152
1153 virtual bool
Greg Clayton63094e02010-06-23 01:19:29 +00001154 Execute
1155 (
Greg Clayton63094e02010-06-23 01:19:29 +00001156 Args& command,
1157 CommandReturnObject &result
1158 )
Chris Lattner24943d22010-06-08 16:52:24 +00001159 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001160 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001161 if (process == NULL)
1162 {
1163 result.AppendError ("no process");
1164 result.SetStatus (eReturnStatusFailed);
1165 return false;
1166 }
1167 else if (command.GetArgumentCount() != 1)
1168 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001169 result.AppendErrorWithFormat("'%s' takes exactly one thread index argument:\nUsage: %s\n", m_cmd_name.c_str(), m_cmd_syntax.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +00001170 result.SetStatus (eReturnStatusFailed);
1171 return false;
1172 }
1173
1174 uint32_t index_id = Args::StringToUInt32(command.GetArgumentAtIndex(0), 0, 0);
1175
1176 Thread *new_thread = process->GetThreadList().FindThreadByIndexID(index_id).get();
1177 if (new_thread == NULL)
1178 {
Greg Clayton9c236732011-10-26 00:56:27 +00001179 result.AppendErrorWithFormat ("invalid thread #%s.\n", command.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +00001180 result.SetStatus (eReturnStatusFailed);
1181 return false;
1182 }
1183
Jim Inghamc8332952010-08-26 21:32:51 +00001184 process->GetThreadList().SetSelectedThreadByID(new_thread->GetID());
Johnny Chen8dbb6e82010-09-14 00:53:53 +00001185 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001186
Greg Claytonabe0fed2011-04-18 08:33:37 +00001187 const uint32_t start_frame = 0;
1188 const uint32_t num_frames = 1;
1189 const uint32_t num_frames_with_source = 1;
1190 new_thread->GetStatus (result.GetOutputStream(),
1191 start_frame,
1192 num_frames,
1193 num_frames_with_source);
Chris Lattner24943d22010-06-08 16:52:24 +00001194
1195 return result.Succeeded();
1196 }
1197
1198};
1199
1200
1201//-------------------------------------------------------------------------
1202// CommandObjectThreadList
1203//-------------------------------------------------------------------------
1204
Greg Clayton63094e02010-06-23 01:19:29 +00001205class CommandObjectThreadList : public CommandObject
Chris Lattner24943d22010-06-08 16:52:24 +00001206{
Greg Clayton63094e02010-06-23 01:19:29 +00001207public:
Chris Lattner24943d22010-06-08 16:52:24 +00001208
Chris Lattner24943d22010-06-08 16:52:24 +00001209
Greg Clayton238c0a12010-09-18 01:14:36 +00001210 CommandObjectThreadList (CommandInterpreter &interpreter):
1211 CommandObject (interpreter,
1212 "thread list",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001213 "Show a summary of all current threads in a process.",
Greg Clayton63094e02010-06-23 01:19:29 +00001214 "thread list",
1215 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
Chris Lattner24943d22010-06-08 16:52:24 +00001216 {
Greg Clayton63094e02010-06-23 01:19:29 +00001217 }
Chris Lattner24943d22010-06-08 16:52:24 +00001218
Greg Clayton63094e02010-06-23 01:19:29 +00001219 ~CommandObjectThreadList()
1220 {
1221 }
1222
1223 bool
1224 Execute
1225 (
Greg Clayton63094e02010-06-23 01:19:29 +00001226 Args& command,
1227 CommandReturnObject &result
1228 )
1229 {
Jim Ingham2e8cb8a2011-02-19 02:53:09 +00001230 Stream &strm = result.GetOutputStream();
Greg Clayton63094e02010-06-23 01:19:29 +00001231 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001232 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +00001233 Process *process = exe_ctx.GetProcessPtr();
1234 if (process)
Chris Lattner24943d22010-06-08 16:52:24 +00001235 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001236 const bool only_threads_with_stop_reason = false;
1237 const uint32_t start_frame = 0;
1238 const uint32_t num_frames = 0;
1239 const uint32_t num_frames_with_source = 0;
Greg Clayton567e7f32011-09-22 04:58:26 +00001240 process->GetStatus(strm);
1241 process->GetThreadStatus (strm,
1242 only_threads_with_stop_reason,
1243 start_frame,
1244 num_frames,
1245 num_frames_with_source);
Chris Lattner24943d22010-06-08 16:52:24 +00001246 }
1247 else
1248 {
Greg Clayton63094e02010-06-23 01:19:29 +00001249 result.AppendError ("no current location or status available");
Chris Lattner24943d22010-06-08 16:52:24 +00001250 result.SetStatus (eReturnStatusFailed);
1251 }
Greg Clayton63094e02010-06-23 01:19:29 +00001252 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +00001253 }
Greg Clayton63094e02010-06-23 01:19:29 +00001254};
Chris Lattner24943d22010-06-08 16:52:24 +00001255
1256//-------------------------------------------------------------------------
1257// CommandObjectMultiwordThread
1258//-------------------------------------------------------------------------
1259
Greg Clayton63094e02010-06-23 01:19:29 +00001260CommandObjectMultiwordThread::CommandObjectMultiwordThread (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001261 CommandObjectMultiword (interpreter,
1262 "thread",
Caroline Ticec1ad82e2010-09-07 22:38:08 +00001263 "A set of commands for operating on one or more threads within a running process.",
Chris Lattner24943d22010-06-08 16:52:24 +00001264 "thread <subcommand> [<subcommand-options>]")
1265{
Greg Clayton238c0a12010-09-18 01:14:36 +00001266 LoadSubCommand ("backtrace", CommandObjectSP (new CommandObjectThreadBacktrace (interpreter)));
1267 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectThreadContinue (interpreter)));
1268 LoadSubCommand ("list", CommandObjectSP (new CommandObjectThreadList (interpreter)));
1269 LoadSubCommand ("select", CommandObjectSP (new CommandObjectThreadSelect (interpreter)));
1270 LoadSubCommand ("until", CommandObjectSP (new CommandObjectThreadUntil (interpreter)));
1271 LoadSubCommand ("step-in", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1272 interpreter,
Greg Clayton63094e02010-06-23 01:19:29 +00001273 "thread step-in",
Greg Clayton238c0a12010-09-18 01:14:36 +00001274 "Source level single step in specified thread (current thread, if none specified).",
Caroline Tice43b014a2010-10-04 22:28:36 +00001275 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +00001276 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused,
1277 eStepTypeInto,
1278 eStepScopeSource)));
Greg Clayton63094e02010-06-23 01:19:29 +00001279
Greg Clayton238c0a12010-09-18 01:14:36 +00001280 LoadSubCommand ("step-out", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1281 interpreter,
1282 "thread step-out",
Jim Ingham1586d972011-12-17 01:35:57 +00001283 "Finish executing the function of the currently selected frame and return to its call site in specified thread (current thread, if none specified).",
Caroline Tice43b014a2010-10-04 22:28:36 +00001284 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +00001285 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused,
1286 eStepTypeOut,
1287 eStepScopeSource)));
Chris Lattner24943d22010-06-08 16:52:24 +00001288
Greg Clayton238c0a12010-09-18 01:14:36 +00001289 LoadSubCommand ("step-over", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1290 interpreter,
1291 "thread step-over",
1292 "Source level single step in specified thread (current thread, if none specified), stepping over calls.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001293 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +00001294 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused,
1295 eStepTypeOver,
1296 eStepScopeSource)));
Chris Lattner24943d22010-06-08 16:52:24 +00001297
Greg Clayton238c0a12010-09-18 01:14:36 +00001298 LoadSubCommand ("step-inst", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1299 interpreter,
1300 "thread step-inst",
1301 "Single step one instruction in specified thread (current thread, if none specified).",
Caroline Tice43b014a2010-10-04 22:28:36 +00001302 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +00001303 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused,
1304 eStepTypeTrace,
1305 eStepScopeInstruction)));
Greg Clayton63094e02010-06-23 01:19:29 +00001306
Greg Clayton238c0a12010-09-18 01:14:36 +00001307 LoadSubCommand ("step-inst-over", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1308 interpreter,
1309 "thread step-inst-over",
1310 "Single step one instruction in specified thread (current thread, if none specified), stepping over calls.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001311 NULL,
Greg Clayton238c0a12010-09-18 01:14:36 +00001312 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused,
1313 eStepTypeTraceOver,
1314 eStepScopeInstruction)));
Chris Lattner24943d22010-06-08 16:52:24 +00001315}
1316
1317CommandObjectMultiwordThread::~CommandObjectMultiwordThread ()
1318{
1319}
1320
1321