1 //===-- CommandObjectThread.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "CommandObjectThread.h"
16 // Other libraries and framework includes
18 #include "lldb/lldb-private.h"
19 #include "lldb/Core/State.h"
20 #include "lldb/Core/SourceManager.h"
21 #include "lldb/Host/Host.h"
22 #include "lldb/Interpreter/CommandInterpreter.h"
23 #include "lldb/Interpreter/CommandReturnObject.h"
24 #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"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Target/RegisterContext.h"
31 #include "lldb/Target/SystemRuntime.h"
32 #include "lldb/Target/Target.h"
33 #include "lldb/Target/Thread.h"
34 #include "lldb/Target/ThreadPlan.h"
35 #include "lldb/Target/ThreadPlanStepInstruction.h"
36 #include "lldb/Target/ThreadPlanStepOut.h"
37 #include "lldb/Target/ThreadPlanStepRange.h"
38 #include "lldb/Target/ThreadPlanStepInRange.h"
42 using namespace lldb_private;
45 //-------------------------------------------------------------------------
46 // CommandObjectThreadBacktrace
47 //-------------------------------------------------------------------------
49 class CommandObjectThreadBacktrace : public CommandObjectParsed
53 class CommandOptions : public Options
57 CommandOptions (CommandInterpreter &interpreter) :
60 // Keep default values of all options in one place: OptionParsingStarting ()
61 OptionParsingStarting ();
70 SetOptionValue (uint32_t option_idx, const char *option_arg)
73 const int short_option = m_getopt_table[option_idx].val;
80 int32_t input_count = Args::StringToSInt32 (option_arg, -1, 0, &success);
82 error.SetErrorStringWithFormat("invalid integer value for option '%c'", short_option);
86 m_count = input_count;
92 m_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
94 error.SetErrorStringWithFormat("invalid integer value for option '%c'", short_option);
99 m_extended_backtrace = Args::StringToBoolean (option_arg, false, &success);
101 error.SetErrorStringWithFormat("invalid boolean value for option '%c'", short_option);
105 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
113 OptionParsingStarting ()
115 m_count = UINT32_MAX;
117 m_extended_backtrace = false;
120 const OptionDefinition*
123 return g_option_table;
126 // Options table: Required for subclasses of Options.
128 static OptionDefinition g_option_table[];
130 // Instance variables to hold the values for command options.
133 bool m_extended_backtrace;
136 CommandObjectThreadBacktrace (CommandInterpreter &interpreter) :
137 CommandObjectParsed (interpreter,
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.",
141 eFlagRequiresProcess |
142 eFlagRequiresThread |
143 eFlagTryTargetAPILock |
144 eFlagProcessMustBeLaunched |
145 eFlagProcessMustBePaused ),
146 m_options(interpreter)
148 CommandArgumentEntry arg;
149 CommandArgumentData thread_idx_arg;
151 // Define the first (and only) variant of this arg.
152 thread_idx_arg.arg_type = eArgTypeThreadIndex;
153 thread_idx_arg.arg_repetition = eArgRepeatStar;
155 // There is only one variant this argument could be; put it into the argument entry.
156 arg.push_back (thread_idx_arg);
158 // Push the data for the first argument into the m_arguments vector.
159 m_arguments.push_back (arg);
162 ~CommandObjectThreadBacktrace()
174 DoExtendedBacktrace (Thread *thread, CommandReturnObject &result)
176 SystemRuntime *runtime = thread->GetProcess()->GetSystemRuntime();
179 Stream &strm = result.GetOutputStream();
180 const std::vector<ConstString> &types = runtime->GetExtendedBacktraceTypes();
181 for (auto type : types)
183 ThreadSP ext_thread_sp = runtime->GetExtendedBacktraceThread (thread->shared_from_this(), type);
184 if (ext_thread_sp && ext_thread_sp->IsValid ())
186 const uint32_t num_frames_with_source = 0;
187 if (ext_thread_sp->GetStatus (strm,
190 num_frames_with_source))
192 DoExtendedBacktrace (ext_thread_sp.get(), result);
200 DoExecute (Args& command, CommandReturnObject &result)
202 result.SetStatus (eReturnStatusSuccessFinishResult);
203 Stream &strm = result.GetOutputStream();
205 // Don't show source context when doing backtraces.
206 const uint32_t num_frames_with_source = 0;
207 if (command.GetArgumentCount() == 0)
209 Thread *thread = m_exe_ctx.GetThreadPtr();
210 // Thread::GetStatus() returns the number of frames shown.
211 if (thread->GetStatus (strm,
214 num_frames_with_source))
216 result.SetStatus (eReturnStatusSuccessFinishResult);
217 if (m_options.m_extended_backtrace)
219 DoExtendedBacktrace (thread, result);
223 else if (command.GetArgumentCount() == 1 && ::strcmp (command.GetArgumentAtIndex(0), "all") == 0)
225 Process *process = m_exe_ctx.GetProcessPtr();
227 for (ThreadSP thread_sp : process->Threads())
230 result.AppendMessage("");
232 if (!thread_sp->GetStatus (strm,
235 num_frames_with_source))
237 result.AppendErrorWithFormat ("error displaying backtrace for thread: \"0x%4.4x\"\n", idx);
238 result.SetStatus (eReturnStatusFailed);
241 if (m_options.m_extended_backtrace)
243 DoExtendedBacktrace (thread_sp.get(), result);
251 const size_t num_args = command.GetArgumentCount();
252 Process *process = m_exe_ctx.GetProcessPtr();
253 Mutex::Locker locker (process->GetThreadList().GetMutex());
254 std::vector<ThreadSP> thread_sps;
256 for (size_t i = 0; i < num_args; i++)
260 uint32_t thread_idx = Args::StringToUInt32(command.GetArgumentAtIndex(i), 0, 0, &success);
263 result.AppendErrorWithFormat ("invalid thread specification: \"%s\"\n", command.GetArgumentAtIndex(i));
264 result.SetStatus (eReturnStatusFailed);
268 thread_sps.push_back(process->GetThreadList().FindThreadByIndexID(thread_idx));
272 result.AppendErrorWithFormat ("no thread with index: \"%s\"\n", command.GetArgumentAtIndex(i));
273 result.SetStatus (eReturnStatusFailed);
279 for (uint32_t i = 0; i < num_args; i++)
281 if (!thread_sps[i]->GetStatus (strm,
284 num_frames_with_source))
286 result.AppendErrorWithFormat ("error displaying backtrace for thread: \"%s\"\n", command.GetArgumentAtIndex(i));
287 result.SetStatus (eReturnStatusFailed);
290 if (m_options.m_extended_backtrace)
292 DoExtendedBacktrace (thread_sps[i].get(), result);
295 if (i < num_args - 1)
296 result.AppendMessage("");
299 return result.Succeeded();
302 CommandOptions m_options;
306 CommandObjectThreadBacktrace::CommandOptions::g_option_table[] =
308 { 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"},
310 { LLDB_OPT_SET_1, false, "extended", 'e', OptionParser::eRequiredArgument, NULL, 0, eArgTypeBoolean, "Show the extended backtrace, if available"},
311 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
317 eStepScopeInstruction
320 class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed
324 class CommandOptions : public Options
328 CommandOptions (CommandInterpreter &interpreter) :
329 Options (interpreter)
331 // Keep default values of all options in one place: OptionParsingStarting ()
332 OptionParsingStarting ();
341 SetOptionValue (uint32_t option_idx, const char *option_arg)
344 const int short_option = m_getopt_table[option_idx].val;
346 switch (short_option)
351 m_avoid_no_debug = Args::StringToBoolean (option_arg, true, &success);
353 error.SetErrorStringWithFormat("invalid boolean value for option '%c'", short_option);
359 OptionEnumValueElement *enum_values = g_option_table[option_idx].enum_values;
360 m_run_mode = (lldb::RunMode) Args::StringToOptionEnum(option_arg, enum_values, eOnlyDuringStepping, error);
366 m_avoid_regexp.clear();
367 m_avoid_regexp.assign(option_arg);
373 m_step_in_target.clear();
374 m_step_in_target.assign(option_arg);
379 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
387 OptionParsingStarting ()
389 m_avoid_no_debug = true;
390 m_run_mode = eOnlyDuringStepping;
391 m_avoid_regexp.clear();
392 m_step_in_target.clear();
395 const OptionDefinition*
398 return g_option_table;
401 // Options table: Required for subclasses of Options.
403 static OptionDefinition g_option_table[];
405 // Instance variables to hold the values for command options.
406 bool m_avoid_no_debug;
408 std::string m_avoid_regexp;
409 std::string m_step_in_target;
412 CommandObjectThreadStepWithTypeAndScope (CommandInterpreter &interpreter,
417 StepScope step_scope) :
418 CommandObjectParsed (interpreter, name, help, syntax,
419 eFlagRequiresProcess |
420 eFlagRequiresThread |
421 eFlagTryTargetAPILock |
422 eFlagProcessMustBeLaunched |
423 eFlagProcessMustBePaused ),
424 m_step_type (step_type),
425 m_step_scope (step_scope),
426 m_options (interpreter)
428 CommandArgumentEntry arg;
429 CommandArgumentData thread_id_arg;
431 // Define the first (and only) variant of this arg.
432 thread_id_arg.arg_type = eArgTypeThreadID;
433 thread_id_arg.arg_repetition = eArgRepeatOptional;
435 // There is only one variant this argument could be; put it into the argument entry.
436 arg.push_back (thread_id_arg);
438 // Push the data for the first argument into the m_arguments vector.
439 m_arguments.push_back (arg);
443 ~CommandObjectThreadStepWithTypeAndScope ()
456 DoExecute (Args& command, CommandReturnObject &result)
458 Process *process = m_exe_ctx.GetProcessPtr();
459 bool synchronous_execution = m_interpreter.GetSynchronous();
461 const uint32_t num_threads = process->GetThreadList().GetSize();
462 Thread *thread = NULL;
464 if (command.GetArgumentCount() == 0)
466 thread = process->GetThreadList().GetSelectedThread().get();
469 result.AppendError ("no selected thread in process");
470 result.SetStatus (eReturnStatusFailed);
476 const char *thread_idx_cstr = command.GetArgumentAtIndex(0);
477 uint32_t step_thread_idx = Args::StringToUInt32 (thread_idx_cstr, LLDB_INVALID_INDEX32);
478 if (step_thread_idx == LLDB_INVALID_INDEX32)
480 result.AppendErrorWithFormat ("invalid thread index '%s'.\n", thread_idx_cstr);
481 result.SetStatus (eReturnStatusFailed);
484 thread = process->GetThreadList().FindThreadByIndexID(step_thread_idx).get();
487 result.AppendErrorWithFormat ("Thread index %u is out of range (valid values are 0 - %u).\n",
488 step_thread_idx, num_threads);
489 result.SetStatus (eReturnStatusFailed);
494 const bool abort_other_plans = false;
495 const lldb::RunMode stop_other_threads = m_options.m_run_mode;
497 // This is a bit unfortunate, but not all the commands in this command object support
498 // only while stepping, so I use the bool for them.
499 bool bool_stop_other_threads;
500 if (m_options.m_run_mode == eAllThreads)
501 bool_stop_other_threads = false;
502 else if (m_options.m_run_mode == eOnlyDuringStepping)
504 if (m_step_type == eStepTypeOut)
505 bool_stop_other_threads = false;
507 bool_stop_other_threads = true;
510 bool_stop_other_threads = true;
512 ThreadPlanSP new_plan_sp;
514 if (m_step_type == eStepTypeInto)
516 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
518 if (frame->HasDebugInformation ())
520 new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
521 frame->GetSymbolContext(eSymbolContextEverything).line_entry.range,
522 frame->GetSymbolContext(eSymbolContextEverything),
523 m_options.m_step_in_target.c_str(),
525 m_options.m_avoid_no_debug);
526 if (new_plan_sp && !m_options.m_avoid_regexp.empty())
528 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (new_plan_sp.get());
529 step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str());
533 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false, abort_other_plans, bool_stop_other_threads);
536 else if (m_step_type == eStepTypeOver)
538 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
540 if (frame->HasDebugInformation())
541 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
542 frame->GetSymbolContext(eSymbolContextEverything).line_entry.range,
543 frame->GetSymbolContext(eSymbolContextEverything),
546 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
548 bool_stop_other_threads);
551 else if (m_step_type == eStepTypeTrace)
553 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false, abort_other_plans, bool_stop_other_threads);
555 else if (m_step_type == eStepTypeTraceOver)
557 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true, abort_other_plans, bool_stop_other_threads);
559 else if (m_step_type == eStepTypeOut)
561 new_plan_sp = thread->QueueThreadPlanForStepOut (abort_other_plans,
564 bool_stop_other_threads,
567 thread->GetSelectedFrameIndex());
571 result.AppendError ("step type is not supported");
572 result.SetStatus (eReturnStatusFailed);
576 // If we got a new plan, then set it to be a master plan (User level Plans should be master plans
577 // so that they can be interruptible). Then resume the process.
581 new_plan_sp->SetIsMasterPlan (true);
582 new_plan_sp->SetOkayToDiscard (false);
584 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
588 if (synchronous_execution)
590 StateType state = process->WaitForProcessToStop (NULL);
593 //StateType state = process->WaitForStateChangedEvents (NULL, event_sp);
594 //while (! StateIsStoppedState (state))
596 // state = process->WaitForStateChangedEvents (NULL, event_sp);
598 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
599 result.SetDidChangeProcessState (true);
600 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
601 result.SetStatus (eReturnStatusSuccessFinishNoResult);
605 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
610 result.AppendError ("Couldn't find thread plan to implement step type.");
611 result.SetStatus (eReturnStatusFailed);
613 return result.Succeeded();
617 StepType m_step_type;
618 StepScope m_step_scope;
619 CommandOptions m_options;
622 static OptionEnumValueElement
623 g_tri_running_mode[] =
625 { eOnlyThisThread, "this-thread", "Run only this thread"},
626 { eAllThreads, "all-threads", "Run all threads"},
627 { eOnlyDuringStepping, "while-stepping", "Run only this thread while stepping"},
631 static OptionEnumValueElement
632 g_duo_running_mode[] =
634 { eOnlyThisThread, "this-thread", "Run only this thread"},
635 { eAllThreads, "all-threads", "Run all threads"},
640 CommandObjectThreadStepWithTypeAndScope::CommandOptions::g_option_table[] =
642 { LLDB_OPT_SET_1, false, "avoid-no-debug", 'a', OptionParser::eRequiredArgument, NULL, 0, eArgTypeBoolean, "A boolean value that sets whether step-in will step over functions with no debug information."},
643 { 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."},
644 { 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."},
645 { 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."},
646 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
650 //-------------------------------------------------------------------------
651 // CommandObjectThreadContinue
652 //-------------------------------------------------------------------------
654 class CommandObjectThreadContinue : public CommandObjectParsed
658 CommandObjectThreadContinue (CommandInterpreter &interpreter) :
659 CommandObjectParsed (interpreter,
661 "Continue execution of one or more threads in an active process.",
663 eFlagRequiresThread |
664 eFlagTryTargetAPILock |
665 eFlagProcessMustBeLaunched |
666 eFlagProcessMustBePaused)
668 CommandArgumentEntry arg;
669 CommandArgumentData thread_idx_arg;
671 // Define the first (and only) variant of this arg.
672 thread_idx_arg.arg_type = eArgTypeThreadIndex;
673 thread_idx_arg.arg_repetition = eArgRepeatPlus;
675 // There is only one variant this argument could be; put it into the argument entry.
676 arg.push_back (thread_idx_arg);
678 // Push the data for the first argument into the m_arguments vector.
679 m_arguments.push_back (arg);
684 ~CommandObjectThreadContinue ()
689 DoExecute (Args& command, CommandReturnObject &result)
691 bool synchronous_execution = m_interpreter.GetSynchronous ();
693 if (!m_interpreter.GetDebugger().GetSelectedTarget().get())
695 result.AppendError ("invalid target, create a debug target using the 'target create' command");
696 result.SetStatus (eReturnStatusFailed);
700 Process *process = m_exe_ctx.GetProcessPtr();
703 result.AppendError ("no process exists. Cannot continue");
704 result.SetStatus (eReturnStatusFailed);
708 StateType state = process->GetState();
709 if ((state == eStateCrashed) || (state == eStateStopped) || (state == eStateSuspended))
711 const size_t argc = command.GetArgumentCount();
714 // These two lines appear at the beginning of both blocks in
715 // this if..else, but that is because we need to release the
716 // lock before calling process->Resume below.
717 Mutex::Locker locker (process->GetThreadList().GetMutex());
718 const uint32_t num_threads = process->GetThreadList().GetSize();
719 std::vector<Thread *> resume_threads;
720 for (uint32_t i=0; i<argc; ++i)
724 uint32_t thread_idx = Args::StringToUInt32 (command.GetArgumentAtIndex(i), LLDB_INVALID_INDEX32, base, &success);
727 Thread *thread = process->GetThreadList().FindThreadByIndexID(thread_idx).get();
731 resume_threads.push_back(thread);
735 result.AppendErrorWithFormat("invalid thread index %u.\n", thread_idx);
736 result.SetStatus (eReturnStatusFailed);
742 result.AppendErrorWithFormat ("invalid thread index argument: \"%s\".\n", command.GetArgumentAtIndex(i));
743 result.SetStatus (eReturnStatusFailed);
748 if (resume_threads.empty())
750 result.AppendError ("no valid thread indexes were specified");
751 result.SetStatus (eReturnStatusFailed);
756 if (resume_threads.size() == 1)
757 result.AppendMessageWithFormat ("Resuming thread: ");
759 result.AppendMessageWithFormat ("Resuming threads: ");
761 for (uint32_t idx=0; idx<num_threads; ++idx)
763 Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get();
764 std::vector<Thread *>::iterator this_thread_pos = find(resume_threads.begin(), resume_threads.end(), thread);
766 if (this_thread_pos != resume_threads.end())
768 resume_threads.erase(this_thread_pos);
769 if (resume_threads.size() > 0)
770 result.AppendMessageWithFormat ("%u, ", thread->GetIndexID());
772 result.AppendMessageWithFormat ("%u ", thread->GetIndexID());
774 thread->SetResumeState (eStateRunning);
778 thread->SetResumeState (eStateSuspended);
781 result.AppendMessageWithFormat ("in process %" PRIu64 "\n", process->GetID());
786 // These two lines appear at the beginning of both blocks in
787 // this if..else, but that is because we need to release the
788 // lock before calling process->Resume below.
789 Mutex::Locker locker (process->GetThreadList().GetMutex());
790 const uint32_t num_threads = process->GetThreadList().GetSize();
791 Thread *current_thread = process->GetThreadList().GetSelectedThread().get();
792 if (current_thread == NULL)
794 result.AppendError ("the process doesn't have a current thread");
795 result.SetStatus (eReturnStatusFailed);
798 // Set the actions that the threads should each take when resuming
799 for (uint32_t idx=0; idx<num_threads; ++idx)
801 Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get();
802 if (thread == current_thread)
804 result.AppendMessageWithFormat ("Resuming thread 0x%4.4" PRIx64 " in process %" PRIu64 "\n", thread->GetID(), process->GetID());
805 thread->SetResumeState (eStateRunning);
809 thread->SetResumeState (eStateSuspended);
814 // We should not be holding the thread list lock when we do this.
815 Error error (process->Resume());
818 result.AppendMessageWithFormat ("Process %" PRIu64 " resuming\n", process->GetID());
819 if (synchronous_execution)
821 state = process->WaitForProcessToStop (NULL);
823 result.SetDidChangeProcessState (true);
824 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
825 result.SetStatus (eReturnStatusSuccessFinishNoResult);
829 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
834 result.AppendErrorWithFormat("Failed to resume process: %s\n", error.AsCString());
835 result.SetStatus (eReturnStatusFailed);
840 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
841 StateAsCString(state));
842 result.SetStatus (eReturnStatusFailed);
845 return result.Succeeded();
850 //-------------------------------------------------------------------------
851 // CommandObjectThreadUntil
852 //-------------------------------------------------------------------------
854 class CommandObjectThreadUntil : public CommandObjectParsed
858 class CommandOptions : public Options
861 uint32_t m_thread_idx;
862 uint32_t m_frame_idx;
864 CommandOptions (CommandInterpreter &interpreter) :
865 Options (interpreter),
866 m_thread_idx(LLDB_INVALID_THREAD_ID),
867 m_frame_idx(LLDB_INVALID_FRAME_ID)
869 // Keep default values of all options in one place: OptionParsingStarting ()
870 OptionParsingStarting ();
879 SetOptionValue (uint32_t option_idx, const char *option_arg)
882 const int short_option = m_getopt_table[option_idx].val;
884 switch (short_option)
888 m_thread_idx = Args::StringToUInt32 (option_arg, LLDB_INVALID_INDEX32);
889 if (m_thread_idx == LLDB_INVALID_INDEX32)
891 error.SetErrorStringWithFormat ("invalid thread index '%s'", option_arg);
897 m_frame_idx = Args::StringToUInt32 (option_arg, LLDB_INVALID_FRAME_ID);
898 if (m_frame_idx == LLDB_INVALID_FRAME_ID)
900 error.SetErrorStringWithFormat ("invalid frame index '%s'", option_arg);
906 OptionEnumValueElement *enum_values = g_option_table[option_idx].enum_values;
907 lldb::RunMode run_mode = (lldb::RunMode) Args::StringToOptionEnum(option_arg, enum_values, eOnlyDuringStepping, error);
911 if (run_mode == eAllThreads)
912 m_stop_others = false;
914 m_stop_others = true;
919 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
927 OptionParsingStarting ()
929 m_thread_idx = LLDB_INVALID_THREAD_ID;
931 m_stop_others = false;
934 const OptionDefinition*
937 return g_option_table;
940 uint32_t m_step_thread_idx;
943 // Options table: Required for subclasses of Options.
945 static OptionDefinition g_option_table[];
947 // Instance variables to hold the values for command options.
950 CommandObjectThreadUntil (CommandInterpreter &interpreter) :
951 CommandObjectParsed (interpreter,
953 "Run the current or specified thread until it reaches a given line number or leaves the current function.",
955 eFlagRequiresThread |
956 eFlagTryTargetAPILock |
957 eFlagProcessMustBeLaunched |
958 eFlagProcessMustBePaused ),
959 m_options (interpreter)
961 CommandArgumentEntry arg;
962 CommandArgumentData line_num_arg;
964 // Define the first (and only) variant of this arg.
965 line_num_arg.arg_type = eArgTypeLineNum;
966 line_num_arg.arg_repetition = eArgRepeatPlain;
968 // There is only one variant this argument could be; put it into the argument entry.
969 arg.push_back (line_num_arg);
971 // Push the data for the first argument into the m_arguments vector.
972 m_arguments.push_back (arg);
977 ~CommandObjectThreadUntil ()
990 DoExecute (Args& command, CommandReturnObject &result)
992 bool synchronous_execution = m_interpreter.GetSynchronous ();
994 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
997 result.AppendError ("invalid target, create a debug target using the 'target create' command");
998 result.SetStatus (eReturnStatusFailed);
1002 Process *process = m_exe_ctx.GetProcessPtr();
1003 if (process == NULL)
1005 result.AppendError ("need a valid process to step");
1006 result.SetStatus (eReturnStatusFailed);
1011 Thread *thread = NULL;
1012 uint32_t line_number;
1014 if (command.GetArgumentCount() != 1)
1016 result.AppendErrorWithFormat ("No line number provided:\n%s", GetSyntax());
1017 result.SetStatus (eReturnStatusFailed);
1021 line_number = Args::StringToUInt32 (command.GetArgumentAtIndex(0), UINT32_MAX);
1022 if (line_number == UINT32_MAX)
1024 result.AppendErrorWithFormat ("invalid line number: '%s'.\n", command.GetArgumentAtIndex(0));
1025 result.SetStatus (eReturnStatusFailed);
1029 if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID)
1031 thread = process->GetThreadList().GetSelectedThread().get();
1035 thread = process->GetThreadList().FindThreadByIndexID(m_options.m_thread_idx).get();
1040 const uint32_t num_threads = process->GetThreadList().GetSize();
1041 result.AppendErrorWithFormat ("Thread index %u is out of range (valid values are 0 - %u).\n",
1042 m_options.m_thread_idx,
1044 result.SetStatus (eReturnStatusFailed);
1048 const bool abort_other_plans = false;
1050 StackFrame *frame = thread->GetStackFrameAtIndex(m_options.m_frame_idx).get();
1054 result.AppendErrorWithFormat ("Frame index %u is out of range for thread %u.\n",
1055 m_options.m_frame_idx,
1056 m_options.m_thread_idx);
1057 result.SetStatus (eReturnStatusFailed);
1061 ThreadPlanSP new_plan_sp;
1063 if (frame->HasDebugInformation ())
1065 // Finally we got here... Translate the given line number to a bunch of addresses:
1066 SymbolContext sc(frame->GetSymbolContext (eSymbolContextCompUnit));
1067 LineTable *line_table = NULL;
1069 line_table = sc.comp_unit->GetLineTable();
1071 if (line_table == NULL)
1073 result.AppendErrorWithFormat ("Failed to resolve the line table for frame %u of thread index %u.\n",
1074 m_options.m_frame_idx, m_options.m_thread_idx);
1075 result.SetStatus (eReturnStatusFailed);
1079 LineEntry function_start;
1080 uint32_t index_ptr = 0, end_ptr;
1081 std::vector<addr_t> address_list;
1083 // Find the beginning & end index of the
1084 AddressRange fun_addr_range = sc.function->GetAddressRange();
1085 Address fun_start_addr = fun_addr_range.GetBaseAddress();
1086 line_table->FindLineEntryByAddress (fun_start_addr, function_start, &index_ptr);
1088 Address fun_end_addr(fun_start_addr.GetSection(),
1089 fun_start_addr.GetOffset() + fun_addr_range.GetByteSize());
1090 line_table->FindLineEntryByAddress (fun_end_addr, function_start, &end_ptr);
1092 bool all_in_function = true;
1094 while (index_ptr <= end_ptr)
1096 LineEntry line_entry;
1097 const bool exact = false;
1098 index_ptr = sc.comp_unit->FindLineEntry(index_ptr, line_number, sc.comp_unit, exact, &line_entry);
1099 if (index_ptr == UINT32_MAX)
1102 addr_t address = line_entry.range.GetBaseAddress().GetLoadAddress(target);
1103 if (address != LLDB_INVALID_ADDRESS)
1105 if (fun_addr_range.ContainsLoadAddress (address, target))
1106 address_list.push_back (address);
1108 all_in_function = false;
1113 if (address_list.size() == 0)
1115 if (all_in_function)
1116 result.AppendErrorWithFormat ("No line entries matching until target.\n");
1118 result.AppendErrorWithFormat ("Until target outside of the current function.\n");
1120 result.SetStatus (eReturnStatusFailed);
1124 new_plan_sp = thread->QueueThreadPlanForStepUntil (abort_other_plans,
1125 &address_list.front(),
1126 address_list.size(),
1127 m_options.m_stop_others,
1128 m_options.m_frame_idx);
1129 // User level plans should be master plans so they can be interrupted (e.g. by hitting a breakpoint)
1130 // and other plans executed by the user (stepping around the breakpoint) and then a "continue"
1131 // will resume the original plan.
1132 new_plan_sp->SetIsMasterPlan (true);
1133 new_plan_sp->SetOkayToDiscard(false);
1137 result.AppendErrorWithFormat ("Frame index %u of thread %u has no debug information.\n",
1138 m_options.m_frame_idx,
1139 m_options.m_thread_idx);
1140 result.SetStatus (eReturnStatusFailed);
1145 process->GetThreadList().SetSelectedThreadByID (m_options.m_thread_idx);
1146 Error error (process->Resume ());
1147 if (error.Success())
1149 result.AppendMessageWithFormat ("Process %" PRIu64 " resuming\n", process->GetID());
1150 if (synchronous_execution)
1152 StateType state = process->WaitForProcessToStop (NULL);
1154 result.SetDidChangeProcessState (true);
1155 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
1156 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1160 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
1165 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
1166 result.SetStatus (eReturnStatusFailed);
1170 return result.Succeeded();
1173 CommandOptions m_options;
1178 CommandObjectThreadUntil::CommandOptions::g_option_table[] =
1180 { LLDB_OPT_SET_1, false, "frame", 'f', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFrameIndex, "Frame index for until operation - defaults to 0"},
1181 { LLDB_OPT_SET_1, false, "thread", 't', OptionParser::eRequiredArgument, NULL, 0, eArgTypeThreadIndex, "Thread index for the thread for until operation"},
1182 { 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"},
1183 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1187 //-------------------------------------------------------------------------
1188 // CommandObjectThreadSelect
1189 //-------------------------------------------------------------------------
1191 class CommandObjectThreadSelect : public CommandObjectParsed
1195 CommandObjectThreadSelect (CommandInterpreter &interpreter) :
1196 CommandObjectParsed (interpreter,
1198 "Select a thread as the currently active thread.",
1200 eFlagRequiresProcess |
1201 eFlagTryTargetAPILock |
1202 eFlagProcessMustBeLaunched |
1203 eFlagProcessMustBePaused )
1205 CommandArgumentEntry arg;
1206 CommandArgumentData thread_idx_arg;
1208 // Define the first (and only) variant of this arg.
1209 thread_idx_arg.arg_type = eArgTypeThreadIndex;
1210 thread_idx_arg.arg_repetition = eArgRepeatPlain;
1212 // There is only one variant this argument could be; put it into the argument entry.
1213 arg.push_back (thread_idx_arg);
1215 // Push the data for the first argument into the m_arguments vector.
1216 m_arguments.push_back (arg);
1221 ~CommandObjectThreadSelect ()
1227 DoExecute (Args& command, CommandReturnObject &result)
1229 Process *process = m_exe_ctx.GetProcessPtr();
1230 if (process == NULL)
1232 result.AppendError ("no process");
1233 result.SetStatus (eReturnStatusFailed);
1236 else if (command.GetArgumentCount() != 1)
1238 result.AppendErrorWithFormat("'%s' takes exactly one thread index argument:\nUsage: %s\n", m_cmd_name.c_str(), m_cmd_syntax.c_str());
1239 result.SetStatus (eReturnStatusFailed);
1243 uint32_t index_id = Args::StringToUInt32(command.GetArgumentAtIndex(0), 0, 0);
1245 Thread *new_thread = process->GetThreadList().FindThreadByIndexID(index_id).get();
1246 if (new_thread == NULL)
1248 result.AppendErrorWithFormat ("invalid thread #%s.\n", command.GetArgumentAtIndex(0));
1249 result.SetStatus (eReturnStatusFailed);
1253 process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true);
1254 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1256 return result.Succeeded();
1262 //-------------------------------------------------------------------------
1263 // CommandObjectThreadList
1264 //-------------------------------------------------------------------------
1266 class CommandObjectThreadList : public CommandObjectParsed
1271 CommandObjectThreadList (CommandInterpreter &interpreter):
1272 CommandObjectParsed (interpreter,
1274 "Show a summary of all current threads in a process.",
1276 eFlagRequiresProcess |
1277 eFlagTryTargetAPILock |
1278 eFlagProcessMustBeLaunched |
1279 eFlagProcessMustBePaused )
1283 ~CommandObjectThreadList()
1289 DoExecute (Args& command, CommandReturnObject &result)
1291 Stream &strm = result.GetOutputStream();
1292 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1293 Process *process = m_exe_ctx.GetProcessPtr();
1294 const bool only_threads_with_stop_reason = false;
1295 const uint32_t start_frame = 0;
1296 const uint32_t num_frames = 0;
1297 const uint32_t num_frames_with_source = 0;
1298 process->GetStatus(strm);
1299 process->GetThreadStatus (strm,
1300 only_threads_with_stop_reason,
1303 num_frames_with_source);
1304 return result.Succeeded();
1308 //-------------------------------------------------------------------------
1309 // CommandObjectThreadReturn
1310 //-------------------------------------------------------------------------
1312 class CommandObjectThreadReturn : public CommandObjectRaw
1315 class CommandOptions : public Options
1319 CommandOptions (CommandInterpreter &interpreter) :
1320 Options (interpreter),
1321 m_from_expression (false)
1323 // Keep default values of all options in one place: OptionParsingStarting ()
1324 OptionParsingStarting ();
1333 SetOptionValue (uint32_t option_idx, const char *option_arg)
1336 const int short_option = m_getopt_table[option_idx].val;
1338 switch (short_option)
1343 bool tmp_value = Args::StringToBoolean (option_arg, false, &success);
1345 m_from_expression = tmp_value;
1348 error.SetErrorStringWithFormat ("invalid boolean value '%s' for 'x' option", option_arg);
1353 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
1361 OptionParsingStarting ()
1363 m_from_expression = false;
1366 const OptionDefinition*
1369 return g_option_table;
1372 bool m_from_expression;
1374 // Options table: Required for subclasses of Options.
1376 static OptionDefinition g_option_table[];
1378 // Instance variables to hold the values for command options.
1388 CommandObjectThreadReturn (CommandInterpreter &interpreter) :
1389 CommandObjectRaw (interpreter,
1391 "Return from the currently selected frame, short-circuiting execution of the frames below it, with an optional return value,"
1392 " or with the -x option from the innermost function evaluation.",
1394 eFlagRequiresFrame |
1395 eFlagTryTargetAPILock |
1396 eFlagProcessMustBeLaunched |
1397 eFlagProcessMustBePaused ),
1398 m_options (interpreter)
1400 CommandArgumentEntry arg;
1401 CommandArgumentData expression_arg;
1403 // Define the first (and only) variant of this arg.
1404 expression_arg.arg_type = eArgTypeExpression;
1405 expression_arg.arg_repetition = eArgRepeatOptional;
1407 // There is only one variant this argument could be; put it into the argument entry.
1408 arg.push_back (expression_arg);
1410 // Push the data for the first argument into the m_arguments vector.
1411 m_arguments.push_back (arg);
1416 ~CommandObjectThreadReturn()
1424 const char *command,
1425 CommandReturnObject &result
1428 // I am going to handle this by hand, because I don't want you to have to say:
1429 // "thread return -- -5".
1430 if (command[0] == '-' && command[1] == 'x')
1432 if (command && command[2] != '\0')
1433 result.AppendWarning("Return values ignored when returning from user called expressions");
1435 Thread *thread = m_exe_ctx.GetThreadPtr();
1437 error = thread->UnwindInnermostExpression();
1438 if (!error.Success())
1440 result.AppendErrorWithFormat ("Unwinding expression failed - %s.", error.AsCString());
1441 result.SetStatus (eReturnStatusFailed);
1445 bool success = thread->SetSelectedFrameByIndexNoisily (0, result.GetOutputStream());
1448 m_exe_ctx.SetFrameSP(thread->GetSelectedFrame ());
1449 result.SetStatus (eReturnStatusSuccessFinishResult);
1453 result.AppendErrorWithFormat ("Could not select 0th frame after unwinding expression.");
1454 result.SetStatus (eReturnStatusFailed);
1457 return result.Succeeded();
1460 ValueObjectSP return_valobj_sp;
1462 StackFrameSP frame_sp = m_exe_ctx.GetFrameSP();
1463 uint32_t frame_idx = frame_sp->GetFrameIndex();
1465 if (frame_sp->IsInlined())
1467 result.AppendError("Don't know how to return from inlined frames.");
1468 result.SetStatus (eReturnStatusFailed);
1472 if (command && command[0] != '\0')
1474 Target *target = m_exe_ctx.GetTargetPtr();
1475 EvaluateExpressionOptions options;
1477 options.SetUnwindOnError(true);
1478 options.SetUseDynamic(eNoDynamicValues);
1480 ExecutionResults exe_results = eExecutionSetupError;
1481 exe_results = target->EvaluateExpression (command,
1485 if (exe_results != eExecutionCompleted)
1487 if (return_valobj_sp)
1488 result.AppendErrorWithFormat("Error evaluating result expression: %s", return_valobj_sp->GetError().AsCString());
1490 result.AppendErrorWithFormat("Unknown error evaluating result expression.");
1491 result.SetStatus (eReturnStatusFailed);
1498 ThreadSP thread_sp = m_exe_ctx.GetThreadSP();
1499 const bool broadcast = true;
1500 error = thread_sp->ReturnFromFrame (frame_sp, return_valobj_sp, broadcast);
1501 if (!error.Success())
1503 result.AppendErrorWithFormat("Error returning from frame %d of thread %d: %s.", frame_idx, thread_sp->GetIndexID(), error.AsCString());
1504 result.SetStatus (eReturnStatusFailed);
1508 result.SetStatus (eReturnStatusSuccessFinishResult);
1512 CommandOptions m_options;
1516 CommandObjectThreadReturn::CommandOptions::g_option_table[] =
1518 { LLDB_OPT_SET_ALL, false, "from-expression", 'x', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Return from the innermost expression evaluation."},
1519 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1522 //-------------------------------------------------------------------------
1523 // CommandObjectThreadJump
1524 //-------------------------------------------------------------------------
1526 class CommandObjectThreadJump : public CommandObjectParsed
1529 class CommandOptions : public Options
1533 CommandOptions (CommandInterpreter &interpreter) :
1534 Options (interpreter)
1536 OptionParsingStarting ();
1540 OptionParsingStarting ()
1542 m_filenames.Clear();
1545 m_load_addr = LLDB_INVALID_ADDRESS;
1555 SetOptionValue (uint32_t option_idx, const char *option_arg)
1558 const int short_option = m_getopt_table[option_idx].val;
1561 switch (short_option)
1564 m_filenames.AppendIfUnique (FileSpec(option_arg, false));
1565 if (m_filenames.GetSize() > 1)
1566 return Error("only one source file expected.");
1569 m_line_num = Args::StringToUInt32 (option_arg, 0, 0, &success);
1570 if (!success || m_line_num == 0)
1571 return Error("invalid line number: '%s'.", option_arg);
1574 m_line_offset = Args::StringToSInt32 (option_arg, 0, 0, &success);
1576 return Error("invalid line offset: '%s'.", option_arg);
1580 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
1581 m_load_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
1589 return Error("invalid short option character '%c'", short_option);
1595 const OptionDefinition*
1598 return g_option_table;
1601 FileSpecList m_filenames;
1602 uint32_t m_line_num;
1603 int32_t m_line_offset;
1604 lldb::addr_t m_load_addr;
1607 static OptionDefinition g_option_table[];
1617 CommandObjectThreadJump (CommandInterpreter &interpreter) :
1618 CommandObjectParsed (interpreter,
1620 "Sets the program counter to a new address.",
1622 eFlagRequiresFrame |
1623 eFlagTryTargetAPILock |
1624 eFlagProcessMustBeLaunched |
1625 eFlagProcessMustBePaused ),
1626 m_options (interpreter)
1630 ~CommandObjectThreadJump()
1636 bool DoExecute (Args& args, CommandReturnObject &result)
1638 RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext();
1639 StackFrame *frame = m_exe_ctx.GetFramePtr();
1640 Thread *thread = m_exe_ctx.GetThreadPtr();
1641 Target *target = m_exe_ctx.GetTargetPtr();
1642 const SymbolContext &sym_ctx = frame->GetSymbolContext (eSymbolContextLineEntry);
1644 if (m_options.m_load_addr != LLDB_INVALID_ADDRESS)
1646 // Use this address directly.
1647 Address dest = Address(m_options.m_load_addr);
1649 lldb::addr_t callAddr = dest.GetCallableLoadAddress (target);
1650 if (callAddr == LLDB_INVALID_ADDRESS)
1652 result.AppendErrorWithFormat ("Invalid destination address.");
1653 result.SetStatus (eReturnStatusFailed);
1657 if (!reg_ctx->SetPC (callAddr))
1659 result.AppendErrorWithFormat ("Error changing PC value for thread %d.", thread->GetIndexID());
1660 result.SetStatus (eReturnStatusFailed);
1666 // Pick either the absolute line, or work out a relative one.
1667 int32_t line = (int32_t)m_options.m_line_num;
1669 line = sym_ctx.line_entry.line + m_options.m_line_offset;
1671 // Try the current file, but override if asked.
1672 FileSpec file = sym_ctx.line_entry.file;
1673 if (m_options.m_filenames.GetSize() == 1)
1674 file = m_options.m_filenames.GetFileSpecAtIndex(0);
1678 result.AppendErrorWithFormat ("No source file available for the current location.");
1679 result.SetStatus (eReturnStatusFailed);
1683 std::string warnings;
1684 Error err = thread->JumpToLine (file, line, m_options.m_force, &warnings);
1688 result.SetError (err);
1692 if (!warnings.empty())
1693 result.AppendWarning (warnings.c_str());
1696 result.SetStatus (eReturnStatusSuccessFinishResult);
1700 CommandOptions m_options;
1703 CommandObjectThreadJump::CommandOptions::g_option_table[] =
1705 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
1706 "Specifies the source file to jump to."},
1708 { LLDB_OPT_SET_1, true, "line", 'l', OptionParser::eRequiredArgument, NULL, 0, eArgTypeLineNum,
1709 "Specifies the line number to jump to."},
1711 { LLDB_OPT_SET_2, true, "by", 'b', OptionParser::eRequiredArgument, NULL, 0, eArgTypeOffset,
1712 "Jumps by a relative line offset from the current line."},
1714 { LLDB_OPT_SET_3, true, "address", 'a', OptionParser::eRequiredArgument, NULL, 0, eArgTypeAddressOrExpression,
1715 "Jumps to a specific address."},
1719 LLDB_OPT_SET_3, false, "force",'r', OptionParser::eNoArgument, NULL, 0, eArgTypeNone,"Allows the PC to leave the current function."},
1721 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1724 //-------------------------------------------------------------------------
1725 // CommandObjectMultiwordThread
1726 //-------------------------------------------------------------------------
1728 CommandObjectMultiwordThread::CommandObjectMultiwordThread (CommandInterpreter &interpreter) :
1729 CommandObjectMultiword (interpreter,
1731 "A set of commands for operating on one or more threads within a running process.",
1732 "thread <subcommand> [<subcommand-options>]")
1734 LoadSubCommand ("backtrace", CommandObjectSP (new CommandObjectThreadBacktrace (interpreter)));
1735 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectThreadContinue (interpreter)));
1736 LoadSubCommand ("list", CommandObjectSP (new CommandObjectThreadList (interpreter)));
1737 LoadSubCommand ("return", CommandObjectSP (new CommandObjectThreadReturn (interpreter)));
1738 LoadSubCommand ("jump", CommandObjectSP (new CommandObjectThreadJump (interpreter)));
1739 LoadSubCommand ("select", CommandObjectSP (new CommandObjectThreadSelect (interpreter)));
1740 LoadSubCommand ("until", CommandObjectSP (new CommandObjectThreadUntil (interpreter)));
1741 LoadSubCommand ("step-in", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1744 "Source level single step in specified thread (current thread, if none specified).",
1747 eStepScopeSource)));
1749 LoadSubCommand ("step-out", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1752 "Finish executing the function of the currently selected frame and return to its call site in specified thread (current thread, if none specified).",
1755 eStepScopeSource)));
1757 LoadSubCommand ("step-over", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1760 "Source level single step in specified thread (current thread, if none specified), stepping over calls.",
1763 eStepScopeSource)));
1765 LoadSubCommand ("step-inst", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1768 "Single step one instruction in specified thread (current thread, if none specified).",
1771 eStepScopeInstruction)));
1773 LoadSubCommand ("step-inst-over", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1775 "thread step-inst-over",
1776 "Single step one instruction in specified thread (current thread, if none specified), stepping over calls.",
1779 eStepScopeInstruction)));
1782 CommandObjectMultiwordThread::~CommandObjectMultiwordThread ()