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/Target.h"
32 #include "lldb/Target/Thread.h"
33 #include "lldb/Target/ThreadPlan.h"
34 #include "lldb/Target/ThreadPlanStepInstruction.h"
35 #include "lldb/Target/ThreadPlanStepOut.h"
36 #include "lldb/Target/ThreadPlanStepRange.h"
37 #include "lldb/Target/ThreadPlanStepInRange.h"
41 using namespace lldb_private;
44 //-------------------------------------------------------------------------
45 // CommandObjectThreadBacktrace
46 //-------------------------------------------------------------------------
48 class CommandObjectThreadBacktrace : public CommandObjectParsed
52 class CommandOptions : public Options
56 CommandOptions (CommandInterpreter &interpreter) :
59 // Keep default values of all options in one place: OptionParsingStarting ()
60 OptionParsingStarting ();
69 SetOptionValue (uint32_t option_idx, const char *option_arg)
72 const int short_option = m_getopt_table[option_idx].val;
79 int32_t input_count = Args::StringToSInt32 (option_arg, -1, 0, &success);
81 error.SetErrorStringWithFormat("invalid integer value for option '%c'", short_option);
85 m_count = input_count;
91 m_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
93 error.SetErrorStringWithFormat("invalid integer value for option '%c'", short_option);
97 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
105 OptionParsingStarting ()
107 m_count = UINT32_MAX;
111 const OptionDefinition*
114 return g_option_table;
117 // Options table: Required for subclasses of Options.
119 static OptionDefinition g_option_table[];
121 // Instance variables to hold the values for command options.
126 CommandObjectThreadBacktrace (CommandInterpreter &interpreter) :
127 CommandObjectParsed (interpreter,
129 "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.",
131 eFlagRequiresProcess |
132 eFlagRequiresThread |
133 eFlagTryTargetAPILock |
134 eFlagProcessMustBeLaunched |
135 eFlagProcessMustBePaused ),
136 m_options(interpreter)
138 CommandArgumentEntry arg;
139 CommandArgumentData thread_idx_arg;
141 // Define the first (and only) variant of this arg.
142 thread_idx_arg.arg_type = eArgTypeThreadIndex;
143 thread_idx_arg.arg_repetition = eArgRepeatStar;
145 // There is only one variant this argument could be; put it into the argument entry.
146 arg.push_back (thread_idx_arg);
148 // Push the data for the first argument into the m_arguments vector.
149 m_arguments.push_back (arg);
152 ~CommandObjectThreadBacktrace()
164 DoExecute (Args& command, CommandReturnObject &result)
166 result.SetStatus (eReturnStatusSuccessFinishResult);
167 Stream &strm = result.GetOutputStream();
169 // Don't show source context when doing backtraces.
170 const uint32_t num_frames_with_source = 0;
171 if (command.GetArgumentCount() == 0)
173 Thread *thread = m_exe_ctx.GetThreadPtr();
174 // Thread::GetStatus() returns the number of frames shown.
175 if (thread->GetStatus (strm,
178 num_frames_with_source))
180 result.SetStatus (eReturnStatusSuccessFinishResult);
183 else if (command.GetArgumentCount() == 1 && ::strcmp (command.GetArgumentAtIndex(0), "all") == 0)
185 Process *process = m_exe_ctx.GetProcessPtr();
186 Mutex::Locker locker (process->GetThreadList().GetMutex());
187 uint32_t num_threads = process->GetThreadList().GetSize();
188 for (uint32_t i = 0; i < num_threads; i++)
190 ThreadSP thread_sp = process->GetThreadList().GetThreadAtIndex(i);
191 if (!thread_sp->GetStatus (strm,
194 num_frames_with_source))
196 result.AppendErrorWithFormat ("error displaying backtrace for thread: \"0x%4.4x\"\n", i);
197 result.SetStatus (eReturnStatusFailed);
201 if (i < num_threads - 1)
202 result.AppendMessage("");
208 const size_t num_args = command.GetArgumentCount();
209 Process *process = m_exe_ctx.GetProcessPtr();
210 Mutex::Locker locker (process->GetThreadList().GetMutex());
211 std::vector<ThreadSP> thread_sps;
213 for (size_t i = 0; i < num_args; i++)
217 uint32_t thread_idx = Args::StringToUInt32(command.GetArgumentAtIndex(i), 0, 0, &success);
220 result.AppendErrorWithFormat ("invalid thread specification: \"%s\"\n", command.GetArgumentAtIndex(i));
221 result.SetStatus (eReturnStatusFailed);
225 thread_sps.push_back(process->GetThreadList().FindThreadByIndexID(thread_idx));
229 result.AppendErrorWithFormat ("no thread with index: \"%s\"\n", command.GetArgumentAtIndex(i));
230 result.SetStatus (eReturnStatusFailed);
236 for (uint32_t i = 0; i < num_args; i++)
238 if (!thread_sps[i]->GetStatus (strm,
241 num_frames_with_source))
243 result.AppendErrorWithFormat ("error displaying backtrace for thread: \"%s\"\n", command.GetArgumentAtIndex(i));
244 result.SetStatus (eReturnStatusFailed);
248 if (i < num_args - 1)
249 result.AppendMessage("");
252 return result.Succeeded();
255 CommandOptions m_options;
259 CommandObjectThreadBacktrace::CommandOptions::g_option_table[] =
261 { LLDB_OPT_SET_1, false, "count", 'c', required_argument, NULL, 0, eArgTypeCount, "How many frames to display (-1 for all)"},
262 { LLDB_OPT_SET_1, false, "start", 's', required_argument, NULL, 0, eArgTypeFrameIndex, "Frame in which to start the backtrace"},
263 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
269 eStepScopeInstruction
272 class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed
276 class CommandOptions : public Options
280 CommandOptions (CommandInterpreter &interpreter) :
281 Options (interpreter)
283 // Keep default values of all options in one place: OptionParsingStarting ()
284 OptionParsingStarting ();
293 SetOptionValue (uint32_t option_idx, const char *option_arg)
296 const int short_option = m_getopt_table[option_idx].val;
298 switch (short_option)
303 m_avoid_no_debug = Args::StringToBoolean (option_arg, true, &success);
305 error.SetErrorStringWithFormat("invalid boolean value for option '%c'", short_option);
311 OptionEnumValueElement *enum_values = g_option_table[option_idx].enum_values;
312 m_run_mode = (lldb::RunMode) Args::StringToOptionEnum(option_arg, enum_values, eOnlyDuringStepping, error);
318 m_avoid_regexp.clear();
319 m_avoid_regexp.assign(option_arg);
325 m_step_in_target.clear();
326 m_step_in_target.assign(option_arg);
331 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
339 OptionParsingStarting ()
341 m_avoid_no_debug = true;
342 m_run_mode = eOnlyDuringStepping;
343 m_avoid_regexp.clear();
344 m_step_in_target.clear();
347 const OptionDefinition*
350 return g_option_table;
353 // Options table: Required for subclasses of Options.
355 static OptionDefinition g_option_table[];
357 // Instance variables to hold the values for command options.
358 bool m_avoid_no_debug;
360 std::string m_avoid_regexp;
361 std::string m_step_in_target;
364 CommandObjectThreadStepWithTypeAndScope (CommandInterpreter &interpreter,
369 StepScope step_scope) :
370 CommandObjectParsed (interpreter, name, help, syntax,
371 eFlagRequiresProcess |
372 eFlagRequiresThread |
373 eFlagTryTargetAPILock |
374 eFlagProcessMustBeLaunched |
375 eFlagProcessMustBePaused ),
376 m_step_type (step_type),
377 m_step_scope (step_scope),
378 m_options (interpreter)
380 CommandArgumentEntry arg;
381 CommandArgumentData thread_id_arg;
383 // Define the first (and only) variant of this arg.
384 thread_id_arg.arg_type = eArgTypeThreadID;
385 thread_id_arg.arg_repetition = eArgRepeatOptional;
387 // There is only one variant this argument could be; put it into the argument entry.
388 arg.push_back (thread_id_arg);
390 // Push the data for the first argument into the m_arguments vector.
391 m_arguments.push_back (arg);
395 ~CommandObjectThreadStepWithTypeAndScope ()
408 DoExecute (Args& command, CommandReturnObject &result)
410 Process *process = m_exe_ctx.GetProcessPtr();
411 bool synchronous_execution = m_interpreter.GetSynchronous();
413 const uint32_t num_threads = process->GetThreadList().GetSize();
414 Thread *thread = NULL;
416 if (command.GetArgumentCount() == 0)
418 thread = process->GetThreadList().GetSelectedThread().get();
421 result.AppendError ("no selected thread in process");
422 result.SetStatus (eReturnStatusFailed);
428 const char *thread_idx_cstr = command.GetArgumentAtIndex(0);
429 uint32_t step_thread_idx = Args::StringToUInt32 (thread_idx_cstr, LLDB_INVALID_INDEX32);
430 if (step_thread_idx == LLDB_INVALID_INDEX32)
432 result.AppendErrorWithFormat ("invalid thread index '%s'.\n", thread_idx_cstr);
433 result.SetStatus (eReturnStatusFailed);
436 thread = process->GetThreadList().FindThreadByIndexID(step_thread_idx).get();
439 result.AppendErrorWithFormat ("Thread index %u is out of range (valid values are 0 - %u).\n",
440 step_thread_idx, num_threads);
441 result.SetStatus (eReturnStatusFailed);
446 const bool abort_other_plans = false;
447 const lldb::RunMode stop_other_threads = m_options.m_run_mode;
449 // This is a bit unfortunate, but not all the commands in this command object support
450 // only while stepping, so I use the bool for them.
451 bool bool_stop_other_threads;
452 if (m_options.m_run_mode == eAllThreads)
453 bool_stop_other_threads = false;
454 else if (m_options.m_run_mode == eOnlyDuringStepping)
456 if (m_step_type == eStepTypeOut)
457 bool_stop_other_threads = false;
459 bool_stop_other_threads = true;
462 bool_stop_other_threads = true;
464 ThreadPlanSP new_plan_sp;
466 if (m_step_type == eStepTypeInto)
468 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
470 if (frame->HasDebugInformation ())
472 new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
473 frame->GetSymbolContext(eSymbolContextEverything).line_entry.range,
474 frame->GetSymbolContext(eSymbolContextEverything),
475 m_options.m_step_in_target.c_str(),
477 m_options.m_avoid_no_debug);
478 if (new_plan_sp && !m_options.m_avoid_regexp.empty())
480 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (new_plan_sp.get());
481 step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str());
485 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false, abort_other_plans, bool_stop_other_threads);
488 else if (m_step_type == eStepTypeOver)
490 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
492 if (frame->HasDebugInformation())
493 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
494 frame->GetSymbolContext(eSymbolContextEverything).line_entry.range,
495 frame->GetSymbolContext(eSymbolContextEverything),
498 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
500 bool_stop_other_threads);
503 else if (m_step_type == eStepTypeTrace)
505 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false, abort_other_plans, bool_stop_other_threads);
507 else if (m_step_type == eStepTypeTraceOver)
509 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true, abort_other_plans, bool_stop_other_threads);
511 else if (m_step_type == eStepTypeOut)
513 new_plan_sp = thread->QueueThreadPlanForStepOut (abort_other_plans,
516 bool_stop_other_threads,
519 thread->GetSelectedFrameIndex());
523 result.AppendError ("step type is not supported");
524 result.SetStatus (eReturnStatusFailed);
528 // If we got a new plan, then set it to be a master plan (User level Plans should be master plans
529 // so that they can be interruptible). Then resume the process.
533 new_plan_sp->SetIsMasterPlan (true);
534 new_plan_sp->SetOkayToDiscard (false);
536 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
540 if (synchronous_execution)
542 StateType state = process->WaitForProcessToStop (NULL);
545 //StateType state = process->WaitForStateChangedEvents (NULL, event_sp);
546 //while (! StateIsStoppedState (state))
548 // state = process->WaitForStateChangedEvents (NULL, event_sp);
550 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
551 result.SetDidChangeProcessState (true);
552 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
553 result.SetStatus (eReturnStatusSuccessFinishNoResult);
557 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
562 result.AppendError ("Couldn't find thread plan to implement step type.");
563 result.SetStatus (eReturnStatusFailed);
565 return result.Succeeded();
569 StepType m_step_type;
570 StepScope m_step_scope;
571 CommandOptions m_options;
574 static OptionEnumValueElement
575 g_tri_running_mode[] =
577 { eOnlyThisThread, "this-thread", "Run only this thread"},
578 { eAllThreads, "all-threads", "Run all threads"},
579 { eOnlyDuringStepping, "while-stepping", "Run only this thread while stepping"},
583 static OptionEnumValueElement
584 g_duo_running_mode[] =
586 { eOnlyThisThread, "this-thread", "Run only this thread"},
587 { eAllThreads, "all-threads", "Run all threads"},
592 CommandObjectThreadStepWithTypeAndScope::CommandOptions::g_option_table[] =
594 { 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."},
595 { 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."},
596 { LLDB_OPT_SET_1, false, "step-over-regexp",'r', required_argument, NULL, 0, eArgTypeRegularExpression, "A regular expression that defines function names to not to stop at when stepping in."},
597 { LLDB_OPT_SET_1, false, "step-in-target", 't', required_argument, NULL, 0, eArgTypeFunctionName, "The name of the directly called function step in should stop at when stepping into."},
598 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
602 //-------------------------------------------------------------------------
603 // CommandObjectThreadContinue
604 //-------------------------------------------------------------------------
606 class CommandObjectThreadContinue : public CommandObjectParsed
610 CommandObjectThreadContinue (CommandInterpreter &interpreter) :
611 CommandObjectParsed (interpreter,
613 "Continue execution of one or more threads in an active process.",
615 eFlagRequiresThread |
616 eFlagTryTargetAPILock |
617 eFlagProcessMustBeLaunched |
618 eFlagProcessMustBePaused)
620 CommandArgumentEntry arg;
621 CommandArgumentData thread_idx_arg;
623 // Define the first (and only) variant of this arg.
624 thread_idx_arg.arg_type = eArgTypeThreadIndex;
625 thread_idx_arg.arg_repetition = eArgRepeatPlus;
627 // There is only one variant this argument could be; put it into the argument entry.
628 arg.push_back (thread_idx_arg);
630 // Push the data for the first argument into the m_arguments vector.
631 m_arguments.push_back (arg);
636 ~CommandObjectThreadContinue ()
641 DoExecute (Args& command, CommandReturnObject &result)
643 bool synchronous_execution = m_interpreter.GetSynchronous ();
645 if (!m_interpreter.GetDebugger().GetSelectedTarget().get())
647 result.AppendError ("invalid target, create a debug target using the 'target create' command");
648 result.SetStatus (eReturnStatusFailed);
652 Process *process = m_exe_ctx.GetProcessPtr();
655 result.AppendError ("no process exists. Cannot continue");
656 result.SetStatus (eReturnStatusFailed);
660 StateType state = process->GetState();
661 if ((state == eStateCrashed) || (state == eStateStopped) || (state == eStateSuspended))
663 Mutex::Locker locker (process->GetThreadList().GetMutex());
664 const uint32_t num_threads = process->GetThreadList().GetSize();
665 const size_t argc = command.GetArgumentCount();
668 std::vector<Thread *> resume_threads;
669 for (uint32_t i=0; i<argc; ++i)
673 uint32_t thread_idx = Args::StringToUInt32 (command.GetArgumentAtIndex(i), LLDB_INVALID_INDEX32, base, &success);
676 Thread *thread = process->GetThreadList().FindThreadByIndexID(thread_idx).get();
680 resume_threads.push_back(thread);
684 result.AppendErrorWithFormat("invalid thread index %u.\n", thread_idx);
685 result.SetStatus (eReturnStatusFailed);
691 result.AppendErrorWithFormat ("invalid thread index argument: \"%s\".\n", command.GetArgumentAtIndex(i));
692 result.SetStatus (eReturnStatusFailed);
697 if (resume_threads.empty())
699 result.AppendError ("no valid thread indexes were specified");
700 result.SetStatus (eReturnStatusFailed);
705 if (resume_threads.size() == 1)
706 result.AppendMessageWithFormat ("Resuming thread: ");
708 result.AppendMessageWithFormat ("Resuming threads: ");
710 for (uint32_t idx=0; idx<num_threads; ++idx)
712 Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get();
713 std::vector<Thread *>::iterator this_thread_pos = find(resume_threads.begin(), resume_threads.end(), thread);
715 if (this_thread_pos != resume_threads.end())
717 resume_threads.erase(this_thread_pos);
718 if (resume_threads.size() > 0)
719 result.AppendMessageWithFormat ("%u, ", thread->GetIndexID());
721 result.AppendMessageWithFormat ("%u ", thread->GetIndexID());
723 thread->SetResumeState (eStateRunning);
727 thread->SetResumeState (eStateSuspended);
730 result.AppendMessageWithFormat ("in process %" PRIu64 "\n", process->GetID());
735 Thread *current_thread = process->GetThreadList().GetSelectedThread().get();
736 if (current_thread == NULL)
738 result.AppendError ("the process doesn't have a current thread");
739 result.SetStatus (eReturnStatusFailed);
742 // Set the actions that the threads should each take when resuming
743 for (uint32_t idx=0; idx<num_threads; ++idx)
745 Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get();
746 if (thread == current_thread)
748 result.AppendMessageWithFormat ("Resuming thread 0x%4.4" PRIx64 " in process %" PRIu64 "\n", thread->GetID(), process->GetID());
749 thread->SetResumeState (eStateRunning);
753 thread->SetResumeState (eStateSuspended);
758 Error error (process->Resume());
761 result.AppendMessageWithFormat ("Process %" PRIu64 " resuming\n", process->GetID());
762 if (synchronous_execution)
764 state = process->WaitForProcessToStop (NULL);
766 result.SetDidChangeProcessState (true);
767 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
768 result.SetStatus (eReturnStatusSuccessFinishNoResult);
772 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
777 result.AppendErrorWithFormat("Failed to resume process: %s\n", error.AsCString());
778 result.SetStatus (eReturnStatusFailed);
783 result.AppendErrorWithFormat ("Process cannot be continued from its current state (%s).\n",
784 StateAsCString(state));
785 result.SetStatus (eReturnStatusFailed);
788 return result.Succeeded();
793 //-------------------------------------------------------------------------
794 // CommandObjectThreadUntil
795 //-------------------------------------------------------------------------
797 class CommandObjectThreadUntil : public CommandObjectParsed
801 class CommandOptions : public Options
804 uint32_t m_thread_idx;
805 uint32_t m_frame_idx;
807 CommandOptions (CommandInterpreter &interpreter) :
808 Options (interpreter),
809 m_thread_idx(LLDB_INVALID_THREAD_ID),
810 m_frame_idx(LLDB_INVALID_FRAME_ID)
812 // Keep default values of all options in one place: OptionParsingStarting ()
813 OptionParsingStarting ();
822 SetOptionValue (uint32_t option_idx, const char *option_arg)
825 const int short_option = m_getopt_table[option_idx].val;
827 switch (short_option)
831 m_thread_idx = Args::StringToUInt32 (option_arg, LLDB_INVALID_INDEX32);
832 if (m_thread_idx == LLDB_INVALID_INDEX32)
834 error.SetErrorStringWithFormat ("invalid thread index '%s'", option_arg);
840 m_frame_idx = Args::StringToUInt32 (option_arg, LLDB_INVALID_FRAME_ID);
841 if (m_frame_idx == LLDB_INVALID_FRAME_ID)
843 error.SetErrorStringWithFormat ("invalid frame index '%s'", option_arg);
849 OptionEnumValueElement *enum_values = g_option_table[option_idx].enum_values;
850 lldb::RunMode run_mode = (lldb::RunMode) Args::StringToOptionEnum(option_arg, enum_values, eOnlyDuringStepping, error);
854 if (run_mode == eAllThreads)
855 m_stop_others = false;
857 m_stop_others = true;
862 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
870 OptionParsingStarting ()
872 m_thread_idx = LLDB_INVALID_THREAD_ID;
874 m_stop_others = false;
877 const OptionDefinition*
880 return g_option_table;
883 uint32_t m_step_thread_idx;
886 // Options table: Required for subclasses of Options.
888 static OptionDefinition g_option_table[];
890 // Instance variables to hold the values for command options.
893 CommandObjectThreadUntil (CommandInterpreter &interpreter) :
894 CommandObjectParsed (interpreter,
896 "Run the current or specified thread until it reaches a given line number or leaves the current function.",
898 eFlagRequiresThread |
899 eFlagTryTargetAPILock |
900 eFlagProcessMustBeLaunched |
901 eFlagProcessMustBePaused ),
902 m_options (interpreter)
904 CommandArgumentEntry arg;
905 CommandArgumentData line_num_arg;
907 // Define the first (and only) variant of this arg.
908 line_num_arg.arg_type = eArgTypeLineNum;
909 line_num_arg.arg_repetition = eArgRepeatPlain;
911 // There is only one variant this argument could be; put it into the argument entry.
912 arg.push_back (line_num_arg);
914 // Push the data for the first argument into the m_arguments vector.
915 m_arguments.push_back (arg);
920 ~CommandObjectThreadUntil ()
933 DoExecute (Args& command, CommandReturnObject &result)
935 bool synchronous_execution = m_interpreter.GetSynchronous ();
937 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
940 result.AppendError ("invalid target, create a debug target using the 'target create' command");
941 result.SetStatus (eReturnStatusFailed);
945 Process *process = m_exe_ctx.GetProcessPtr();
948 result.AppendError ("need a valid process to step");
949 result.SetStatus (eReturnStatusFailed);
954 Thread *thread = NULL;
955 uint32_t line_number;
957 if (command.GetArgumentCount() != 1)
959 result.AppendErrorWithFormat ("No line number provided:\n%s", GetSyntax());
960 result.SetStatus (eReturnStatusFailed);
964 line_number = Args::StringToUInt32 (command.GetArgumentAtIndex(0), UINT32_MAX);
965 if (line_number == UINT32_MAX)
967 result.AppendErrorWithFormat ("invalid line number: '%s'.\n", command.GetArgumentAtIndex(0));
968 result.SetStatus (eReturnStatusFailed);
972 if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID)
974 thread = process->GetThreadList().GetSelectedThread().get();
978 thread = process->GetThreadList().FindThreadByIndexID(m_options.m_thread_idx).get();
983 const uint32_t num_threads = process->GetThreadList().GetSize();
984 result.AppendErrorWithFormat ("Thread index %u is out of range (valid values are 0 - %u).\n",
985 m_options.m_thread_idx,
987 result.SetStatus (eReturnStatusFailed);
991 const bool abort_other_plans = false;
993 StackFrame *frame = thread->GetStackFrameAtIndex(m_options.m_frame_idx).get();
997 result.AppendErrorWithFormat ("Frame index %u is out of range for thread %u.\n",
998 m_options.m_frame_idx,
999 m_options.m_thread_idx);
1000 result.SetStatus (eReturnStatusFailed);
1004 ThreadPlanSP new_plan_sp;
1006 if (frame->HasDebugInformation ())
1008 // Finally we got here... Translate the given line number to a bunch of addresses:
1009 SymbolContext sc(frame->GetSymbolContext (eSymbolContextCompUnit));
1010 LineTable *line_table = NULL;
1012 line_table = sc.comp_unit->GetLineTable();
1014 if (line_table == NULL)
1016 result.AppendErrorWithFormat ("Failed to resolve the line table for frame %u of thread index %u.\n",
1017 m_options.m_frame_idx, m_options.m_thread_idx);
1018 result.SetStatus (eReturnStatusFailed);
1022 LineEntry function_start;
1023 uint32_t index_ptr = 0, end_ptr;
1024 std::vector<addr_t> address_list;
1026 // Find the beginning & end index of the
1027 AddressRange fun_addr_range = sc.function->GetAddressRange();
1028 Address fun_start_addr = fun_addr_range.GetBaseAddress();
1029 line_table->FindLineEntryByAddress (fun_start_addr, function_start, &index_ptr);
1031 Address fun_end_addr(fun_start_addr.GetSection(),
1032 fun_start_addr.GetOffset() + fun_addr_range.GetByteSize());
1033 line_table->FindLineEntryByAddress (fun_end_addr, function_start, &end_ptr);
1035 bool all_in_function = true;
1037 while (index_ptr <= end_ptr)
1039 LineEntry line_entry;
1040 const bool exact = false;
1041 index_ptr = sc.comp_unit->FindLineEntry(index_ptr, line_number, sc.comp_unit, exact, &line_entry);
1042 if (index_ptr == UINT32_MAX)
1045 addr_t address = line_entry.range.GetBaseAddress().GetLoadAddress(target);
1046 if (address != LLDB_INVALID_ADDRESS)
1048 if (fun_addr_range.ContainsLoadAddress (address, target))
1049 address_list.push_back (address);
1051 all_in_function = false;
1056 if (address_list.size() == 0)
1058 if (all_in_function)
1059 result.AppendErrorWithFormat ("No line entries matching until target.\n");
1061 result.AppendErrorWithFormat ("Until target outside of the current function.\n");
1063 result.SetStatus (eReturnStatusFailed);
1067 new_plan_sp = thread->QueueThreadPlanForStepUntil (abort_other_plans,
1068 &address_list.front(),
1069 address_list.size(),
1070 m_options.m_stop_others,
1071 m_options.m_frame_idx);
1072 // User level plans should be master plans so they can be interrupted (e.g. by hitting a breakpoint)
1073 // and other plans executed by the user (stepping around the breakpoint) and then a "continue"
1074 // will resume the original plan.
1075 new_plan_sp->SetIsMasterPlan (true);
1076 new_plan_sp->SetOkayToDiscard(false);
1080 result.AppendErrorWithFormat ("Frame index %u of thread %u has no debug information.\n",
1081 m_options.m_frame_idx,
1082 m_options.m_thread_idx);
1083 result.SetStatus (eReturnStatusFailed);
1088 process->GetThreadList().SetSelectedThreadByID (m_options.m_thread_idx);
1089 Error error (process->Resume ());
1090 if (error.Success())
1092 result.AppendMessageWithFormat ("Process %" PRIu64 " resuming\n", process->GetID());
1093 if (synchronous_execution)
1095 StateType state = process->WaitForProcessToStop (NULL);
1097 result.SetDidChangeProcessState (true);
1098 result.AppendMessageWithFormat ("Process %" PRIu64 " %s\n", process->GetID(), StateAsCString (state));
1099 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1103 result.SetStatus (eReturnStatusSuccessContinuingNoResult);
1108 result.AppendErrorWithFormat("Failed to resume process: %s.\n", error.AsCString());
1109 result.SetStatus (eReturnStatusFailed);
1113 return result.Succeeded();
1116 CommandOptions m_options;
1121 CommandObjectThreadUntil::CommandOptions::g_option_table[] =
1123 { LLDB_OPT_SET_1, false, "frame", 'f', required_argument, NULL, 0, eArgTypeFrameIndex, "Frame index for until operation - defaults to 0"},
1124 { LLDB_OPT_SET_1, false, "thread", 't', required_argument, NULL, 0, eArgTypeThreadIndex, "Thread index for the thread for until operation"},
1125 { 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"},
1126 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1130 //-------------------------------------------------------------------------
1131 // CommandObjectThreadSelect
1132 //-------------------------------------------------------------------------
1134 class CommandObjectThreadSelect : public CommandObjectParsed
1138 CommandObjectThreadSelect (CommandInterpreter &interpreter) :
1139 CommandObjectParsed (interpreter,
1141 "Select a thread as the currently active thread.",
1143 eFlagRequiresProcess |
1144 eFlagTryTargetAPILock |
1145 eFlagProcessMustBeLaunched |
1146 eFlagProcessMustBePaused )
1148 CommandArgumentEntry arg;
1149 CommandArgumentData thread_idx_arg;
1151 // Define the first (and only) variant of this arg.
1152 thread_idx_arg.arg_type = eArgTypeThreadIndex;
1153 thread_idx_arg.arg_repetition = eArgRepeatPlain;
1155 // There is only one variant this argument could be; put it into the argument entry.
1156 arg.push_back (thread_idx_arg);
1158 // Push the data for the first argument into the m_arguments vector.
1159 m_arguments.push_back (arg);
1164 ~CommandObjectThreadSelect ()
1170 DoExecute (Args& command, CommandReturnObject &result)
1172 Process *process = m_exe_ctx.GetProcessPtr();
1173 if (process == NULL)
1175 result.AppendError ("no process");
1176 result.SetStatus (eReturnStatusFailed);
1179 else if (command.GetArgumentCount() != 1)
1181 result.AppendErrorWithFormat("'%s' takes exactly one thread index argument:\nUsage: %s\n", m_cmd_name.c_str(), m_cmd_syntax.c_str());
1182 result.SetStatus (eReturnStatusFailed);
1186 uint32_t index_id = Args::StringToUInt32(command.GetArgumentAtIndex(0), 0, 0);
1188 Thread *new_thread = process->GetThreadList().FindThreadByIndexID(index_id).get();
1189 if (new_thread == NULL)
1191 result.AppendErrorWithFormat ("invalid thread #%s.\n", command.GetArgumentAtIndex(0));
1192 result.SetStatus (eReturnStatusFailed);
1196 process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true);
1197 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1199 return result.Succeeded();
1205 //-------------------------------------------------------------------------
1206 // CommandObjectThreadList
1207 //-------------------------------------------------------------------------
1209 class CommandObjectThreadList : public CommandObjectParsed
1214 CommandObjectThreadList (CommandInterpreter &interpreter):
1215 CommandObjectParsed (interpreter,
1217 "Show a summary of all current threads in a process.",
1219 eFlagRequiresProcess |
1220 eFlagTryTargetAPILock |
1221 eFlagProcessMustBeLaunched |
1222 eFlagProcessMustBePaused )
1226 ~CommandObjectThreadList()
1232 DoExecute (Args& command, CommandReturnObject &result)
1234 Stream &strm = result.GetOutputStream();
1235 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1236 Process *process = m_exe_ctx.GetProcessPtr();
1237 const bool only_threads_with_stop_reason = false;
1238 const uint32_t start_frame = 0;
1239 const uint32_t num_frames = 0;
1240 const uint32_t num_frames_with_source = 0;
1241 process->GetStatus(strm);
1242 process->GetThreadStatus (strm,
1243 only_threads_with_stop_reason,
1246 num_frames_with_source);
1247 return result.Succeeded();
1251 //-------------------------------------------------------------------------
1252 // CommandObjectThreadReturn
1253 //-------------------------------------------------------------------------
1255 class CommandObjectThreadReturn : public CommandObjectRaw
1258 class CommandOptions : public Options
1262 CommandOptions (CommandInterpreter &interpreter) :
1263 Options (interpreter),
1264 m_from_expression (false)
1266 // Keep default values of all options in one place: OptionParsingStarting ()
1267 OptionParsingStarting ();
1276 SetOptionValue (uint32_t option_idx, const char *option_arg)
1279 const int short_option = m_getopt_table[option_idx].val;
1281 switch (short_option)
1286 bool tmp_value = Args::StringToBoolean (option_arg, false, &success);
1288 m_from_expression = tmp_value;
1291 error.SetErrorStringWithFormat ("invalid boolean value '%s' for 'x' option", option_arg);
1296 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
1304 OptionParsingStarting ()
1306 m_from_expression = false;
1309 const OptionDefinition*
1312 return g_option_table;
1315 bool m_from_expression;
1317 // Options table: Required for subclasses of Options.
1319 static OptionDefinition g_option_table[];
1321 // Instance variables to hold the values for command options.
1331 CommandObjectThreadReturn (CommandInterpreter &interpreter) :
1332 CommandObjectRaw (interpreter,
1334 "Return from the currently selected frame, short-circuiting execution of the frames below it, with an optional return value,"
1335 " or with the -x option from the innermost function evaluation.",
1337 eFlagRequiresFrame |
1338 eFlagTryTargetAPILock |
1339 eFlagProcessMustBeLaunched |
1340 eFlagProcessMustBePaused ),
1341 m_options (interpreter)
1343 CommandArgumentEntry arg;
1344 CommandArgumentData expression_arg;
1346 // Define the first (and only) variant of this arg.
1347 expression_arg.arg_type = eArgTypeExpression;
1348 expression_arg.arg_repetition = eArgRepeatOptional;
1350 // There is only one variant this argument could be; put it into the argument entry.
1351 arg.push_back (expression_arg);
1353 // Push the data for the first argument into the m_arguments vector.
1354 m_arguments.push_back (arg);
1359 ~CommandObjectThreadReturn()
1367 const char *command,
1368 CommandReturnObject &result
1371 // I am going to handle this by hand, because I don't want you to have to say:
1372 // "thread return -- -5".
1373 if (command[0] == '-' && command[1] == 'x')
1375 if (command && command[2] != '\0')
1376 result.AppendWarning("Return values ignored when returning from user called expressions");
1378 Thread *thread = m_exe_ctx.GetThreadPtr();
1380 error = thread->UnwindInnermostExpression();
1381 if (!error.Success())
1383 result.AppendErrorWithFormat ("Unwinding expression failed - %s.", error.AsCString());
1384 result.SetStatus (eReturnStatusFailed);
1388 bool success = thread->SetSelectedFrameByIndexNoisily (0, result.GetOutputStream());
1391 m_exe_ctx.SetFrameSP(thread->GetSelectedFrame ());
1392 result.SetStatus (eReturnStatusSuccessFinishResult);
1396 result.AppendErrorWithFormat ("Could not select 0th frame after unwinding expression.");
1397 result.SetStatus (eReturnStatusFailed);
1400 return result.Succeeded();
1403 ValueObjectSP return_valobj_sp;
1405 StackFrameSP frame_sp = m_exe_ctx.GetFrameSP();
1406 uint32_t frame_idx = frame_sp->GetFrameIndex();
1408 if (frame_sp->IsInlined())
1410 result.AppendError("Don't know how to return from inlined frames.");
1411 result.SetStatus (eReturnStatusFailed);
1415 if (command && command[0] != '\0')
1417 Target *target = m_exe_ctx.GetTargetPtr();
1418 EvaluateExpressionOptions options;
1420 options.SetUnwindOnError(true);
1421 options.SetUseDynamic(eNoDynamicValues);
1423 ExecutionResults exe_results = eExecutionSetupError;
1424 exe_results = target->EvaluateExpression (command,
1428 if (exe_results != eExecutionCompleted)
1430 if (return_valobj_sp)
1431 result.AppendErrorWithFormat("Error evaluating result expression: %s", return_valobj_sp->GetError().AsCString());
1433 result.AppendErrorWithFormat("Unknown error evaluating result expression.");
1434 result.SetStatus (eReturnStatusFailed);
1441 ThreadSP thread_sp = m_exe_ctx.GetThreadSP();
1442 const bool broadcast = true;
1443 error = thread_sp->ReturnFromFrame (frame_sp, return_valobj_sp, broadcast);
1444 if (!error.Success())
1446 result.AppendErrorWithFormat("Error returning from frame %d of thread %d: %s.", frame_idx, thread_sp->GetIndexID(), error.AsCString());
1447 result.SetStatus (eReturnStatusFailed);
1451 result.SetStatus (eReturnStatusSuccessFinishResult);
1455 CommandOptions m_options;
1459 CommandObjectThreadReturn::CommandOptions::g_option_table[] =
1461 { LLDB_OPT_SET_ALL, false, "from-expression", 'x', no_argument, NULL, 0, eArgTypeNone, "Return from the innermost expression evaluation."},
1462 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1465 //-------------------------------------------------------------------------
1466 // CommandObjectMultiwordThread
1467 //-------------------------------------------------------------------------
1469 CommandObjectMultiwordThread::CommandObjectMultiwordThread (CommandInterpreter &interpreter) :
1470 CommandObjectMultiword (interpreter,
1472 "A set of commands for operating on one or more threads within a running process.",
1473 "thread <subcommand> [<subcommand-options>]")
1475 LoadSubCommand ("backtrace", CommandObjectSP (new CommandObjectThreadBacktrace (interpreter)));
1476 LoadSubCommand ("continue", CommandObjectSP (new CommandObjectThreadContinue (interpreter)));
1477 LoadSubCommand ("list", CommandObjectSP (new CommandObjectThreadList (interpreter)));
1478 LoadSubCommand ("return", CommandObjectSP (new CommandObjectThreadReturn (interpreter)));
1479 LoadSubCommand ("select", CommandObjectSP (new CommandObjectThreadSelect (interpreter)));
1480 LoadSubCommand ("until", CommandObjectSP (new CommandObjectThreadUntil (interpreter)));
1481 LoadSubCommand ("step-in", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1484 "Source level single step in specified thread (current thread, if none specified).",
1487 eStepScopeSource)));
1489 LoadSubCommand ("step-out", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1492 "Finish executing the function of the currently selected frame and return to its call site in specified thread (current thread, if none specified).",
1495 eStepScopeSource)));
1497 LoadSubCommand ("step-over", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1500 "Source level single step in specified thread (current thread, if none specified), stepping over calls.",
1503 eStepScopeSource)));
1505 LoadSubCommand ("step-inst", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1508 "Single step one instruction in specified thread (current thread, if none specified).",
1511 eStepScopeInstruction)));
1513 LoadSubCommand ("step-inst-over", CommandObjectSP (new CommandObjectThreadStepWithTypeAndScope (
1515 "thread step-inst-over",
1516 "Single step one instruction in specified thread (current thread, if none specified), stepping over calls.",
1519 eStepScopeInstruction)));
1522 CommandObjectMultiwordThread::~CommandObjectMultiwordThread ()