blob: 94cba9da6967bde1de25115b726bcdefe017585d [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandObjectThread.cpp ---------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "CommandObjectThread.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/SourceManager.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000013#include "lldb/Core/ValueObject.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000014#include "lldb/Host/Host.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000015#include "lldb/Host/OptionParser.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000016#include "lldb/Host/StringConvert.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Interpreter/CommandInterpreter.h"
18#include "lldb/Interpreter/CommandReturnObject.h"
Pavel Labath47cbf4a2018-04-10 09:03:59 +000019#include "lldb/Interpreter/OptionArgParser.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Interpreter/Options.h"
21#include "lldb/Symbol/CompileUnit.h"
22#include "lldb/Symbol/Function.h"
Greg Clayton1f746072012-08-29 21:13:06 +000023#include "lldb/Symbol/LineEntry.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Target/Process.h"
26#include "lldb/Target/RegisterContext.h"
Jason Molenda750ea692013-11-12 07:02:07 +000027#include "lldb/Target/SystemRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Target/Target.h"
29#include "lldb/Target/Thread.h"
30#include "lldb/Target/ThreadPlan.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000031#include "lldb/Target/ThreadPlanStepInRange.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Target/ThreadPlanStepInstruction.h"
33#include "lldb/Target/ThreadPlanStepOut.h"
34#include "lldb/Target/ThreadPlanStepRange.h"
Pavel Labathd821c992018-08-07 11:07:21 +000035#include "lldb/Utility/State.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000036#include "lldb/lldb-private.h"
Greg Clayton1f746072012-08-29 21:13:06 +000037
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038using namespace lldb;
39using namespace lldb_private;
40
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041//-------------------------------------------------------------------------
Pavel Labath7f1c1212017-06-12 16:25:24 +000042// CommandObjectIterateOverThreads
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043//-------------------------------------------------------------------------
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045class CommandObjectIterateOverThreads : public CommandObjectParsed {
Pavel Labath7f1c1212017-06-12 16:25:24 +000046
47 class UniqueStack {
48
49 public:
50 UniqueStack(std::stack<lldb::addr_t> stack_frames, uint32_t thread_index_id)
51 : m_stack_frames(stack_frames) {
52 m_thread_index_ids.push_back(thread_index_id);
53 }
54
55 void AddThread(uint32_t thread_index_id) const {
56 m_thread_index_ids.push_back(thread_index_id);
57 }
58
59 const std::vector<uint32_t> &GetUniqueThreadIndexIDs() const {
60 return m_thread_index_ids;
61 }
62
63 lldb::tid_t GetRepresentativeThread() const {
64 return m_thread_index_ids.front();
65 }
66
67 friend bool inline operator<(const UniqueStack &lhs,
68 const UniqueStack &rhs) {
69 return lhs.m_stack_frames < rhs.m_stack_frames;
70 }
71
72 protected:
73 // Mark the thread index as mutable, as we don't care about it from a const
74 // perspective, we only care about m_stack_frames so we keep our std::set
75 // sorted.
76 mutable std::vector<uint32_t> m_thread_index_ids;
77 std::stack<lldb::addr_t> m_stack_frames;
78 };
79
Jim Ingham2bdbfd52014-09-29 23:17:18 +000080public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 CommandObjectIterateOverThreads(CommandInterpreter &interpreter,
82 const char *name, const char *help,
83 const char *syntax, uint32_t flags)
84 : CommandObjectParsed(interpreter, name, help, syntax, flags) {}
85
86 ~CommandObjectIterateOverThreads() override = default;
87
88 bool DoExecute(Args &command, CommandReturnObject &result) override {
89 result.SetStatus(m_success_return);
90
Pavel Labath7f1c1212017-06-12 16:25:24 +000091 bool all_threads = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 if (command.GetArgumentCount() == 0) {
93 Thread *thread = m_exe_ctx.GetThreadPtr();
Adrian McCarthy3887ba82017-09-19 18:07:33 +000094 if (!thread || !HandleOneThread(thread->GetID(), result))
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 return false;
96 return result.Succeeded();
Pavel Labath7f1c1212017-06-12 16:25:24 +000097 } else if (command.GetArgumentCount() == 1) {
98 all_threads = ::strcmp(command.GetArgumentAtIndex(0), "all") == 0;
99 m_unique_stacks = ::strcmp(command.GetArgumentAtIndex(0), "unique") == 0;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000100 }
101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 // Use tids instead of ThreadSPs to prevent deadlocking problems which
Adrian Prantl05097242018-04-30 16:49:04 +0000103 // result from JIT-ing code while iterating over the (locked) ThreadSP
104 // list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 std::vector<lldb::tid_t> tids;
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000106
Pavel Labath7f1c1212017-06-12 16:25:24 +0000107 if (all_threads || m_unique_stacks) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 Process *process = m_exe_ctx.GetProcessPtr();
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 for (ThreadSP thread_sp : process->Threads())
111 tids.push_back(thread_sp->GetID());
112 } else {
113 const size_t num_args = command.GetArgumentCount();
114 Process *process = m_exe_ctx.GetProcessPtr();
115
116 std::lock_guard<std::recursive_mutex> guard(
117 process->GetThreadList().GetMutex());
118
119 for (size_t i = 0; i < num_args; i++) {
120 bool success;
121
122 uint32_t thread_idx = StringConvert::ToUInt32(
123 command.GetArgumentAtIndex(i), 0, 0, &success);
124 if (!success) {
125 result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n",
126 command.GetArgumentAtIndex(i));
127 result.SetStatus(eReturnStatusFailed);
128 return false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000129 }
Stephane Sezerf8104912016-03-17 18:52:41 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 ThreadSP thread =
132 process->GetThreadList().FindThreadByIndexID(thread_idx);
Stephane Sezerf8104912016-03-17 18:52:41 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 if (!thread) {
135 result.AppendErrorWithFormat("no thread with index: \"%s\"\n",
136 command.GetArgumentAtIndex(i));
137 result.SetStatus(eReturnStatusFailed);
138 return false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000139 }
Stephane Sezerf8104912016-03-17 18:52:41 +0000140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 tids.push_back(thread->GetID());
142 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000143 }
144
Pavel Labath7f1c1212017-06-12 16:25:24 +0000145 if (m_unique_stacks) {
146 // Iterate over threads, finding unique stack buckets.
147 std::set<UniqueStack> unique_stacks;
148 for (const lldb::tid_t &tid : tids) {
149 if (!BucketThread(tid, unique_stacks, result)) {
150 return false;
151 }
152 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153
Pavel Labath7f1c1212017-06-12 16:25:24 +0000154 // Write the thread id's and unique call stacks to the output stream
155 Stream &strm = result.GetOutputStream();
156 Process *process = m_exe_ctx.GetProcessPtr();
157 for (const UniqueStack &stack : unique_stacks) {
158 // List the common thread ID's
159 const std::vector<uint32_t> &thread_index_ids =
160 stack.GetUniqueThreadIndexIDs();
Pavel Labathef7aff52017-07-05 14:54:46 +0000161 strm.Format("{0} thread(s) ", thread_index_ids.size());
Pavel Labath7f1c1212017-06-12 16:25:24 +0000162 for (const uint32_t &thread_index_id : thread_index_ids) {
Pavel Labathef7aff52017-07-05 14:54:46 +0000163 strm.Format("#{0} ", thread_index_id);
Pavel Labath7f1c1212017-06-12 16:25:24 +0000164 }
165 strm.EOL();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166
Pavel Labath7f1c1212017-06-12 16:25:24 +0000167 // List the shared call stack for this set of threads
168 uint32_t representative_thread_id = stack.GetRepresentativeThread();
169 ThreadSP thread = process->GetThreadList().FindThreadByIndexID(
170 representative_thread_id);
171 if (!HandleOneThread(thread->GetID(), result)) {
172 return false;
173 }
174 }
175 } else {
176 uint32_t idx = 0;
177 for (const lldb::tid_t &tid : tids) {
178 if (idx != 0 && m_add_return)
179 result.AppendMessage("");
180
181 if (!HandleOneThread(tid, result))
182 return false;
183
184 ++idx;
185 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 }
187 return result.Succeeded();
188 }
189
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000190protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 // Override this to do whatever you need to do for one thread.
192 //
193 // If you return false, the iteration will stop, otherwise it will proceed.
194 // The result is set to m_success_return (defaults to
Adrian Prantl05097242018-04-30 16:49:04 +0000195 // eReturnStatusSuccessFinishResult) before the iteration, so you only need
196 // to set the return status in HandleOneThread if you want to indicate an
197 // error. If m_add_return is true, a blank line will be inserted between each
198 // of the listings (except the last one.)
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 virtual bool HandleOneThread(lldb::tid_t, CommandReturnObject &result) = 0;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000201
Pavel Labath7f1c1212017-06-12 16:25:24 +0000202 bool BucketThread(lldb::tid_t tid, std::set<UniqueStack> &unique_stacks,
203 CommandReturnObject &result) {
204 // Grab the corresponding thread for the given thread id.
205 Process *process = m_exe_ctx.GetProcessPtr();
206 Thread *thread = process->GetThreadList().FindThreadByID(tid).get();
207 if (thread == nullptr) {
Pavel Labathef7aff52017-07-05 14:54:46 +0000208 result.AppendErrorWithFormatv("Failed to process thread #{0}.\n", tid);
Pavel Labath7f1c1212017-06-12 16:25:24 +0000209 result.SetStatus(eReturnStatusFailed);
210 return false;
211 }
212
213 // Collect the each frame's address for this call-stack
214 std::stack<lldb::addr_t> stack_frames;
215 const uint32_t frame_count = thread->GetStackFrameCount();
216 for (uint32_t frame_index = 0; frame_index < frame_count; frame_index++) {
217 const lldb::StackFrameSP frame_sp =
218 thread->GetStackFrameAtIndex(frame_index);
219 const lldb::addr_t pc = frame_sp->GetStackID().GetPC();
220 stack_frames.push(pc);
221 }
222
223 uint32_t thread_index_id = thread->GetIndexID();
224 UniqueStack new_unique_stack(stack_frames, thread_index_id);
225
226 // Try to match the threads stack to and existing entry.
227 std::set<UniqueStack>::iterator matching_stack =
228 unique_stacks.find(new_unique_stack);
229 if (matching_stack != unique_stacks.end()) {
230 matching_stack->AddThread(thread_index_id);
231 } else {
232 unique_stacks.insert(new_unique_stack);
233 }
234 return true;
235 }
236
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 ReturnStatus m_success_return = eReturnStatusSuccessFinishResult;
Pavel Labath7f1c1212017-06-12 16:25:24 +0000238 bool m_unique_stacks = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 bool m_add_return = true;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000240};
241
242//-------------------------------------------------------------------------
243// CommandObjectThreadBacktrace
244//-------------------------------------------------------------------------
245
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000246static constexpr OptionDefinition g_thread_backtrace_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000247 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000248 { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCount, "How many frames to display (-1 for all)" },
249 { LLDB_OPT_SET_1, false, "start", 's', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeFrameIndex, "Frame in which to start the backtrace" },
250 { LLDB_OPT_SET_1, false, "extended", 'e', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Show the extended backtrace, if available" }
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000251 // clang-format on
252};
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254class CommandObjectThreadBacktrace : public CommandObjectIterateOverThreads {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 class CommandOptions : public Options {
257 public:
258 CommandOptions() : Options() {
259 // Keep default values of all options in one place: OptionParsingStarting
260 // ()
261 OptionParsingStarting(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262 }
263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 ~CommandOptions() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265
Zachary Turner97206d52017-05-12 04:51:55 +0000266 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
267 ExecutionContext *execution_context) override {
268 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 const int short_option = m_getopt_table[option_idx].val;
270
271 switch (short_option) {
272 case 'c': {
Zachary Turnerfe114832016-11-12 16:56:47 +0000273 int32_t input_count = 0;
274 if (option_arg.getAsInteger(0, m_count)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 m_count = UINT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 error.SetErrorStringWithFormat(
277 "invalid integer value for option '%c'", short_option);
Zachary Turnerfe114832016-11-12 16:56:47 +0000278 } else if (input_count < 0)
279 m_count = UINT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 } break;
Zachary Turnerfe114832016-11-12 16:56:47 +0000281 case 's':
282 if (option_arg.getAsInteger(0, m_start))
283 error.SetErrorStringWithFormat(
284 "invalid integer value for option '%c'", short_option);
285 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 case 'e': {
287 bool success;
288 m_extended_backtrace =
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000289 OptionArgParser::ToBoolean(option_arg, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 if (!success)
291 error.SetErrorStringWithFormat(
292 "invalid boolean value for option '%c'", short_option);
293 } break;
294 default:
295 error.SetErrorStringWithFormat("invalid short option character '%c'",
296 short_option);
297 break;
298 }
299 return error;
Jim Inghame2e0b452010-08-26 23:36:03 +0000300 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 void OptionParsingStarting(ExecutionContext *execution_context) override {
303 m_count = UINT32_MAX;
304 m_start = 0;
305 m_extended_backtrace = false;
306 }
307
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000308 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000309 return llvm::makeArrayRef(g_thread_backtrace_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000310 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311
312 // Instance variables to hold the values for command options.
313 uint32_t m_count;
314 uint32_t m_start;
315 bool m_extended_backtrace;
316 };
317
318 CommandObjectThreadBacktrace(CommandInterpreter &interpreter)
319 : CommandObjectIterateOverThreads(
320 interpreter, "thread backtrace",
321 "Show thread call stacks. Defaults to the current thread, thread "
Pavel Labath7f1c1212017-06-12 16:25:24 +0000322 "indexes can be specified as arguments.\n"
323 "Use the thread-index \"all\" to see all threads.\n"
324 "Use the thread-index \"unique\" to see threads grouped by unique "
325 "call stacks.",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 nullptr,
327 eCommandRequiresProcess | eCommandRequiresThread |
328 eCommandTryTargetAPILock | eCommandProcessMustBeLaunched |
329 eCommandProcessMustBePaused),
330 m_options() {}
331
332 ~CommandObjectThreadBacktrace() override = default;
333
334 Options *GetOptions() override { return &m_options; }
335
Jim Ingham5a988412012-06-08 21:56:10 +0000336protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337 void DoExtendedBacktrace(Thread *thread, CommandReturnObject &result) {
338 SystemRuntime *runtime = thread->GetProcess()->GetSystemRuntime();
339 if (runtime) {
340 Stream &strm = result.GetOutputStream();
341 const std::vector<ConstString> &types =
342 runtime->GetExtendedBacktraceTypes();
343 for (auto type : types) {
344 ThreadSP ext_thread_sp = runtime->GetExtendedBacktraceThread(
345 thread->shared_from_this(), type);
346 if (ext_thread_sp && ext_thread_sp->IsValid()) {
347 const uint32_t num_frames_with_source = 0;
Jim Ingham6a9767c2016-11-08 20:36:40 +0000348 const bool stop_format = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 if (ext_thread_sp->GetStatus(strm, m_options.m_start,
350 m_options.m_count,
Jim Ingham6a9767c2016-11-08 20:36:40 +0000351 num_frames_with_source,
352 stop_format)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 DoExtendedBacktrace(ext_thread_sp.get(), result);
354 }
Jason Molenda750ea692013-11-12 07:02:07 +0000355 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 }
357 }
358 }
359
360 bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override {
361 ThreadSP thread_sp =
362 m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
363 if (!thread_sp) {
364 result.AppendErrorWithFormat(
365 "thread disappeared while computing backtraces: 0x%" PRIx64 "\n",
366 tid);
367 result.SetStatus(eReturnStatusFailed);
368 return false;
Jason Molenda750ea692013-11-12 07:02:07 +0000369 }
370
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 Thread *thread = thread_sp.get();
Stephane Sezerf8104912016-03-17 18:52:41 +0000372
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373 Stream &strm = result.GetOutputStream();
Stephane Sezerf8104912016-03-17 18:52:41 +0000374
Pavel Labath7f1c1212017-06-12 16:25:24 +0000375 // Only dump stack info if we processing unique stacks.
376 const bool only_stacks = m_unique_stacks;
377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 // Don't show source context when doing backtraces.
379 const uint32_t num_frames_with_source = 0;
Jim Ingham4f243e82016-11-08 23:43:36 +0000380 const bool stop_format = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381 if (!thread->GetStatus(strm, m_options.m_start, m_options.m_count,
Pavel Labath7f1c1212017-06-12 16:25:24 +0000382 num_frames_with_source, stop_format, only_stacks)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 result.AppendErrorWithFormat(
384 "error displaying backtrace for thread: \"0x%4.4x\"\n",
385 thread->GetIndexID());
386 result.SetStatus(eReturnStatusFailed);
387 return false;
388 }
389 if (m_options.m_extended_backtrace) {
390 DoExtendedBacktrace(thread, result);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391 }
Jim Ingham5a988412012-06-08 21:56:10 +0000392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 return true;
394 }
395
396 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397};
398
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399enum StepScope { eStepScopeSource, eStepScopeInstruction };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000401static constexpr OptionEnumValueElement g_tri_running_mode[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000402 {eOnlyThisThread, "this-thread", "Run only this thread"},
403 {eAllThreads, "all-threads", "Run all threads"},
404 {eOnlyDuringStepping, "while-stepping",
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000405 "Run only this thread while stepping"} };
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000406
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000407static constexpr OptionEnumValues TriRunningModes() {
408 return OptionEnumValues(g_tri_running_mode);
409}
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000410
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000411static constexpr OptionDefinition g_thread_step_scope_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000412 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000413 { LLDB_OPT_SET_1, false, "step-in-avoids-no-debug", 'a', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "A boolean value that sets whether stepping into functions will step over functions with no debug information." },
414 { LLDB_OPT_SET_1, false, "step-out-avoids-no-debug", 'A', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "A boolean value, if true stepping out of functions will continue to step out till it hits a function with debug information." },
415 { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, {}, 1, eArgTypeCount, "How many times to perform the stepping operation - currently only supported for step-inst and next-inst." },
416 { LLDB_OPT_SET_1, false, "end-linenumber", 'e', OptionParser::eRequiredArgument, nullptr, {}, 1, eArgTypeLineNum, "The line at which to stop stepping - defaults to the next line and only supported for step-in and step-over. You can also pass the string 'block' to step to the end of the current block. This is particularly useful in conjunction with --step-target to step through a complex calling sequence." },
417 { LLDB_OPT_SET_1, false, "run-mode", 'm', OptionParser::eRequiredArgument, nullptr, TriRunningModes(), 0, eArgTypeRunMode, "Determine how to run other threads while stepping the current thread." },
418 { LLDB_OPT_SET_1, false, "step-over-regexp", 'r', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeRegularExpression, "A regular expression that defines function names to not to stop at when stepping in." },
419 { LLDB_OPT_SET_1, false, "step-in-target", 't', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeFunctionName, "The name of the directly called function step in should stop at when stepping into." },
420 { LLDB_OPT_SET_2, false, "python-class", 'C', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePythonClass, "The name of the class that will manage this step - only supported for Scripted Step." }
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000421 // clang-format on
422};
423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 class CommandOptions : public Options {
427 public:
428 CommandOptions() : Options() {
429 // Keep default values of all options in one place: OptionParsingStarting
430 // ()
431 OptionParsingStarting(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 }
433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 ~CommandOptions() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435
Zachary Turner97206d52017-05-12 04:51:55 +0000436 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
437 ExecutionContext *execution_context) override {
438 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 const int short_option = m_getopt_table[option_idx].val;
440
441 switch (short_option) {
442 case 'a': {
443 bool success;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000444 bool avoid_no_debug =
445 OptionArgParser::ToBoolean(option_arg, true, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 if (!success)
447 error.SetErrorStringWithFormat(
448 "invalid boolean value for option '%c'", short_option);
449 else {
450 m_step_in_avoid_no_debug =
451 avoid_no_debug ? eLazyBoolYes : eLazyBoolNo;
452 }
453 } break;
454
455 case 'A': {
456 bool success;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000457 bool avoid_no_debug =
458 OptionArgParser::ToBoolean(option_arg, true, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 if (!success)
460 error.SetErrorStringWithFormat(
461 "invalid boolean value for option '%c'", short_option);
462 else {
463 m_step_out_avoid_no_debug =
464 avoid_no_debug ? eLazyBoolYes : eLazyBoolNo;
465 }
466 } break;
467
468 case 'c':
Zachary Turnerfe114832016-11-12 16:56:47 +0000469 if (option_arg.getAsInteger(0, m_step_count))
470 error.SetErrorStringWithFormat("invalid step count '%s'",
471 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472 break;
473
474 case 'C':
475 m_class_name.clear();
476 m_class_name.assign(option_arg);
477 break;
478
479 case 'm': {
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000480 auto enum_values = GetDefinitions()[option_idx].enum_values;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000481 m_run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum(
Zachary Turnerfe114832016-11-12 16:56:47 +0000482 option_arg, enum_values, eOnlyDuringStepping, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 } break;
484
Zachary Turnerfe114832016-11-12 16:56:47 +0000485 case 'e':
486 if (option_arg == "block") {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487 m_end_line_is_block_end = 1;
488 break;
489 }
Zachary Turnerfe114832016-11-12 16:56:47 +0000490 if (option_arg.getAsInteger(0, m_end_line))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 error.SetErrorStringWithFormat("invalid end line number '%s'",
Zachary Turnerfe114832016-11-12 16:56:47 +0000492 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494
495 case 'r':
496 m_avoid_regexp.clear();
497 m_avoid_regexp.assign(option_arg);
498 break;
499
500 case 't':
501 m_step_in_target.clear();
502 m_step_in_target.assign(option_arg);
503 break;
504
505 default:
506 error.SetErrorStringWithFormat("invalid short option character '%c'",
507 short_option);
508 break;
509 }
510 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511 }
512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 void OptionParsingStarting(ExecutionContext *execution_context) override {
514 m_step_in_avoid_no_debug = eLazyBoolCalculate;
515 m_step_out_avoid_no_debug = eLazyBoolCalculate;
516 m_run_mode = eOnlyDuringStepping;
517
518 // Check if we are in Non-Stop mode
519 TargetSP target_sp =
520 execution_context ? execution_context->GetTargetSP() : TargetSP();
521 if (target_sp && target_sp->GetNonStopModeEnabled())
522 m_run_mode = eOnlyThisThread;
523
524 m_avoid_regexp.clear();
525 m_step_in_target.clear();
526 m_class_name.clear();
527 m_step_count = 1;
528 m_end_line = LLDB_INVALID_LINE_NUMBER;
529 m_end_line_is_block_end = false;
530 }
531
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000532 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000533 return llvm::makeArrayRef(g_thread_step_scope_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000534 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535
536 // Instance variables to hold the values for command options.
537 LazyBool m_step_in_avoid_no_debug;
538 LazyBool m_step_out_avoid_no_debug;
539 RunMode m_run_mode;
540 std::string m_avoid_regexp;
541 std::string m_step_in_target;
542 std::string m_class_name;
543 uint32_t m_step_count;
544 uint32_t m_end_line;
545 bool m_end_line_is_block_end;
546 };
547
548 CommandObjectThreadStepWithTypeAndScope(CommandInterpreter &interpreter,
549 const char *name, const char *help,
550 const char *syntax,
551 StepType step_type,
552 StepScope step_scope)
553 : CommandObjectParsed(interpreter, name, help, syntax,
554 eCommandRequiresProcess | eCommandRequiresThread |
555 eCommandTryTargetAPILock |
556 eCommandProcessMustBeLaunched |
557 eCommandProcessMustBePaused),
558 m_step_type(step_type), m_step_scope(step_scope), m_options() {
559 CommandArgumentEntry arg;
560 CommandArgumentData thread_id_arg;
561
562 // Define the first (and only) variant of this arg.
563 thread_id_arg.arg_type = eArgTypeThreadID;
564 thread_id_arg.arg_repetition = eArgRepeatOptional;
565
566 // There is only one variant this argument could be; put it into the
567 // argument entry.
568 arg.push_back(thread_id_arg);
569
570 // Push the data for the first argument into the m_arguments vector.
571 m_arguments.push_back(arg);
572 }
573
574 ~CommandObjectThreadStepWithTypeAndScope() override = default;
575
576 Options *GetOptions() override { return &m_options; }
577
Jim Ingham5a988412012-06-08 21:56:10 +0000578protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 bool DoExecute(Args &command, CommandReturnObject &result) override {
580 Process *process = m_exe_ctx.GetProcessPtr();
581 bool synchronous_execution = m_interpreter.GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 const uint32_t num_threads = process->GetThreadList().GetSize();
584 Thread *thread = nullptr;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 if (command.GetArgumentCount() == 0) {
587 thread = GetDefaultThread();
Jim Ingham8d94ba02016-03-12 02:45:34 +0000588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 if (thread == nullptr) {
590 result.AppendError("no selected thread in process");
591 result.SetStatus(eReturnStatusFailed);
592 return false;
593 }
594 } else {
595 const char *thread_idx_cstr = command.GetArgumentAtIndex(0);
596 uint32_t step_thread_idx =
597 StringConvert::ToUInt32(thread_idx_cstr, LLDB_INVALID_INDEX32);
598 if (step_thread_idx == LLDB_INVALID_INDEX32) {
599 result.AppendErrorWithFormat("invalid thread index '%s'.\n",
600 thread_idx_cstr);
601 result.SetStatus(eReturnStatusFailed);
602 return false;
603 }
604 thread =
605 process->GetThreadList().FindThreadByIndexID(step_thread_idx).get();
606 if (thread == nullptr) {
607 result.AppendErrorWithFormat(
608 "Thread index %u is out of range (valid values are 0 - %u).\n",
609 step_thread_idx, num_threads);
610 result.SetStatus(eReturnStatusFailed);
611 return false;
612 }
613 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000614
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 if (m_step_type == eStepTypeScripted) {
616 if (m_options.m_class_name.empty()) {
617 result.AppendErrorWithFormat("empty class name for scripted step.");
618 result.SetStatus(eReturnStatusFailed);
619 return false;
620 } else if (!m_interpreter.GetScriptInterpreter()->CheckObjectExists(
621 m_options.m_class_name.c_str())) {
622 result.AppendErrorWithFormat(
623 "class for scripted step: \"%s\" does not exist.",
624 m_options.m_class_name.c_str());
625 result.SetStatus(eReturnStatusFailed);
626 return false;
627 }
628 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000629
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630 if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER &&
631 m_step_type != eStepTypeInto) {
632 result.AppendErrorWithFormat(
633 "end line option is only valid for step into");
634 result.SetStatus(eReturnStatusFailed);
635 return false;
636 }
637
638 const bool abort_other_plans = false;
639 const lldb::RunMode stop_other_threads = m_options.m_run_mode;
640
641 // This is a bit unfortunate, but not all the commands in this command
Adrian Prantl05097242018-04-30 16:49:04 +0000642 // object support only while stepping, so I use the bool for them.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 bool bool_stop_other_threads;
644 if (m_options.m_run_mode == eAllThreads)
645 bool_stop_other_threads = false;
646 else if (m_options.m_run_mode == eOnlyDuringStepping)
647 bool_stop_other_threads =
648 (m_step_type != eStepTypeOut && m_step_type != eStepTypeScripted);
649 else
650 bool_stop_other_threads = true;
651
652 ThreadPlanSP new_plan_sp;
653
654 if (m_step_type == eStepTypeInto) {
655 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
656 assert(frame != nullptr);
657
658 if (frame->HasDebugInformation()) {
659 AddressRange range;
660 SymbolContext sc = frame->GetSymbolContext(eSymbolContextEverything);
661 if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER) {
Zachary Turner97206d52017-05-12 04:51:55 +0000662 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000663 if (!sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range,
664 error)) {
665 result.AppendErrorWithFormat("invalid end-line option: %s.",
666 error.AsCString());
Jim Inghamc17d6bd2016-02-10 03:25:24 +0000667 result.SetStatus(eReturnStatusFailed);
668 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 }
670 } else if (m_options.m_end_line_is_block_end) {
Zachary Turner97206d52017-05-12 04:51:55 +0000671 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672 Block *block = frame->GetSymbolContext(eSymbolContextBlock).block;
673 if (!block) {
674 result.AppendErrorWithFormat("Could not find the current block.");
675 result.SetStatus(eReturnStatusFailed);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000676 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000677 }
678
679 AddressRange block_range;
680 Address pc_address = frame->GetFrameCodeAddress();
681 block->GetRangeContainingAddress(pc_address, block_range);
682 if (!block_range.GetBaseAddress().IsValid()) {
683 result.AppendErrorWithFormat(
684 "Could not find the current block address.");
685 result.SetStatus(eReturnStatusFailed);
686 return false;
687 }
688 lldb::addr_t pc_offset_in_block =
689 pc_address.GetFileAddress() -
690 block_range.GetBaseAddress().GetFileAddress();
691 lldb::addr_t range_length =
692 block_range.GetByteSize() - pc_offset_in_block;
693 range = AddressRange(pc_address, range_length);
694 } else {
695 range = sc.line_entry.range;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000696 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000697
Kate Stoneb9c1b512016-09-06 20:57:50 +0000698 new_plan_sp = thread->QueueThreadPlanForStepInRange(
699 abort_other_plans, range,
700 frame->GetSymbolContext(eSymbolContextEverything),
701 m_options.m_step_in_target.c_str(), stop_other_threads,
702 m_options.m_step_in_avoid_no_debug,
703 m_options.m_step_out_avoid_no_debug);
Greg Claytondc6224e2014-10-21 01:00:42 +0000704
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705 if (new_plan_sp && !m_options.m_avoid_regexp.empty()) {
706 ThreadPlanStepInRange *step_in_range_plan =
707 static_cast<ThreadPlanStepInRange *>(new_plan_sp.get());
708 step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000709 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 } else
711 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
712 false, abort_other_plans, bool_stop_other_threads);
713 } else if (m_step_type == eStepTypeOver) {
714 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
715
716 if (frame->HasDebugInformation())
717 new_plan_sp = thread->QueueThreadPlanForStepOverRange(
718 abort_other_plans,
719 frame->GetSymbolContext(eSymbolContextEverything).line_entry,
720 frame->GetSymbolContext(eSymbolContextEverything),
721 stop_other_threads, m_options.m_step_out_avoid_no_debug);
722 else
723 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
724 true, abort_other_plans, bool_stop_other_threads);
725 } else if (m_step_type == eStepTypeTrace) {
726 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
727 false, abort_other_plans, bool_stop_other_threads);
728 } else if (m_step_type == eStepTypeTraceOver) {
729 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction(
730 true, abort_other_plans, bool_stop_other_threads);
731 } else if (m_step_type == eStepTypeOut) {
732 new_plan_sp = thread->QueueThreadPlanForStepOut(
733 abort_other_plans, nullptr, false, bool_stop_other_threads, eVoteYes,
734 eVoteNoOpinion, thread->GetSelectedFrameIndex(),
735 m_options.m_step_out_avoid_no_debug);
736 } else if (m_step_type == eStepTypeScripted) {
737 new_plan_sp = thread->QueueThreadPlanForStepScripted(
738 abort_other_plans, m_options.m_class_name.c_str(),
739 bool_stop_other_threads);
740 } else {
741 result.AppendError("step type is not supported");
742 result.SetStatus(eReturnStatusFailed);
743 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 }
745
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746 // If we got a new plan, then set it to be a master plan (User level Plans
Adrian Prantl05097242018-04-30 16:49:04 +0000747 // should be master plans so that they can be interruptible). Then resume
748 // the process.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749
750 if (new_plan_sp) {
751 new_plan_sp->SetIsMasterPlan(true);
752 new_plan_sp->SetOkayToDiscard(false);
753
754 if (m_options.m_step_count > 1) {
Jim Inghamd2a7e852017-05-25 02:24:18 +0000755 if (!new_plan_sp->SetIterationCount(m_options.m_step_count)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756 result.AppendWarning(
757 "step operation does not support iteration count.");
758 }
759 }
760
761 process->GetThreadList().SetSelectedThreadByID(thread->GetID());
762
763 const uint32_t iohandler_id = process->GetIOHandlerID();
764
765 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +0000766 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 if (synchronous_execution)
768 error = process->ResumeSynchronous(&stream);
769 else
770 error = process->Resume();
771
Adrian McCarthy3887ba82017-09-19 18:07:33 +0000772 if (!error.Success()) {
773 result.AppendMessage(error.AsCString());
774 result.SetStatus(eReturnStatusFailed);
775 return false;
776 }
777
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 // There is a race condition where this thread will return up the call
Adrian Prantl05097242018-04-30 16:49:04 +0000779 // stack to the main command handler and show an (lldb) prompt before
780 // HandlePrivateEvent (from PrivateStateThread) has a chance to call
781 // PushProcessIOHandler().
Pavel Labath3879fe02018-05-09 14:29:30 +0000782 process->SyncIOHandler(iohandler_id, std::chrono::seconds(2));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783
784 if (synchronous_execution) {
785 // If any state changed events had anything to say, add that to the
786 // result
Zachary Turnerc1564272016-11-16 21:15:24 +0000787 if (stream.GetSize() > 0)
788 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789
790 process->GetThreadList().SetSelectedThreadByID(thread->GetID());
791 result.SetDidChangeProcessState(true);
792 result.SetStatus(eReturnStatusSuccessFinishNoResult);
793 } else {
794 result.SetStatus(eReturnStatusSuccessContinuingNoResult);
795 }
796 } else {
797 result.AppendError("Couldn't find thread plan to implement step type.");
798 result.SetStatus(eReturnStatusFailed);
799 }
800 return result.Succeeded();
801 }
802
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000804 StepType m_step_type;
805 StepScope m_step_scope;
806 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807};
808
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809//-------------------------------------------------------------------------
810// CommandObjectThreadContinue
811//-------------------------------------------------------------------------
812
Kate Stoneb9c1b512016-09-06 20:57:50 +0000813class CommandObjectThreadContinue : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000815 CommandObjectThreadContinue(CommandInterpreter &interpreter)
816 : CommandObjectParsed(
817 interpreter, "thread continue",
818 "Continue execution of the current target process. One "
819 "or more threads may be specified, by default all "
820 "threads continue.",
821 nullptr,
822 eCommandRequiresThread | eCommandTryTargetAPILock |
823 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {
824 CommandArgumentEntry arg;
825 CommandArgumentData thread_idx_arg;
826
827 // Define the first (and only) variant of this arg.
828 thread_idx_arg.arg_type = eArgTypeThreadIndex;
829 thread_idx_arg.arg_repetition = eArgRepeatPlus;
830
831 // There is only one variant this argument could be; put it into the
832 // argument entry.
833 arg.push_back(thread_idx_arg);
834
835 // Push the data for the first argument into the m_arguments vector.
836 m_arguments.push_back(arg);
837 }
838
839 ~CommandObjectThreadContinue() override = default;
840
841 bool DoExecute(Args &command, CommandReturnObject &result) override {
842 bool synchronous_execution = m_interpreter.GetSynchronous();
843
844 if (!m_interpreter.GetDebugger().GetSelectedTarget()) {
845 result.AppendError("invalid target, create a debug target using the "
846 "'target create' command");
847 result.SetStatus(eReturnStatusFailed);
848 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849 }
850
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851 Process *process = m_exe_ctx.GetProcessPtr();
852 if (process == nullptr) {
853 result.AppendError("no process exists. Cannot continue");
854 result.SetStatus(eReturnStatusFailed);
855 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000857
858 StateType state = process->GetState();
859 if ((state == eStateCrashed) || (state == eStateStopped) ||
860 (state == eStateSuspended)) {
861 const size_t argc = command.GetArgumentCount();
862 if (argc > 0) {
Adrian Prantl05097242018-04-30 16:49:04 +0000863 // These two lines appear at the beginning of both blocks in this
864 // if..else, but that is because we need to release the lock before
865 // calling process->Resume below.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000866 std::lock_guard<std::recursive_mutex> guard(
867 process->GetThreadList().GetMutex());
868 const uint32_t num_threads = process->GetThreadList().GetSize();
869 std::vector<Thread *> resume_threads;
Zachary Turner97d2c402016-10-05 23:40:23 +0000870 for (auto &entry : command.entries()) {
871 uint32_t thread_idx;
872 if (entry.ref.getAsInteger(0, thread_idx)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000873 result.AppendErrorWithFormat(
Zachary Turner97d2c402016-10-05 23:40:23 +0000874 "invalid thread index argument: \"%s\".\n", entry.c_str());
875 result.SetStatus(eReturnStatusFailed);
876 return false;
877 }
878 Thread *thread =
879 process->GetThreadList().FindThreadByIndexID(thread_idx).get();
880
881 if (thread) {
882 resume_threads.push_back(thread);
883 } else {
884 result.AppendErrorWithFormat("invalid thread index %u.\n",
885 thread_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886 result.SetStatus(eReturnStatusFailed);
887 return false;
888 }
889 }
890
891 if (resume_threads.empty()) {
892 result.AppendError("no valid thread indexes were specified");
893 result.SetStatus(eReturnStatusFailed);
894 return false;
895 } else {
896 if (resume_threads.size() == 1)
897 result.AppendMessageWithFormat("Resuming thread: ");
898 else
899 result.AppendMessageWithFormat("Resuming threads: ");
900
901 for (uint32_t idx = 0; idx < num_threads; ++idx) {
902 Thread *thread =
903 process->GetThreadList().GetThreadAtIndex(idx).get();
904 std::vector<Thread *>::iterator this_thread_pos =
905 find(resume_threads.begin(), resume_threads.end(), thread);
906
907 if (this_thread_pos != resume_threads.end()) {
908 resume_threads.erase(this_thread_pos);
909 if (!resume_threads.empty())
910 result.AppendMessageWithFormat("%u, ", thread->GetIndexID());
911 else
912 result.AppendMessageWithFormat("%u ", thread->GetIndexID());
913
914 const bool override_suspend = true;
915 thread->SetResumeState(eStateRunning, override_suspend);
916 } else {
917 thread->SetResumeState(eStateSuspended);
918 }
919 }
920 result.AppendMessageWithFormat("in process %" PRIu64 "\n",
921 process->GetID());
922 }
923 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000924 // These two lines appear at the beginning of both blocks in this
925 // if..else, but that is because we need to release the lock before
926 // calling process->Resume below.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000927 std::lock_guard<std::recursive_mutex> guard(
928 process->GetThreadList().GetMutex());
929 const uint32_t num_threads = process->GetThreadList().GetSize();
930 Thread *current_thread = GetDefaultThread();
931 if (current_thread == nullptr) {
932 result.AppendError("the process doesn't have a current thread");
933 result.SetStatus(eReturnStatusFailed);
934 return false;
935 }
936 // Set the actions that the threads should each take when resuming
937 for (uint32_t idx = 0; idx < num_threads; ++idx) {
938 Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get();
939 if (thread == current_thread) {
940 result.AppendMessageWithFormat("Resuming thread 0x%4.4" PRIx64
941 " in process %" PRIu64 "\n",
942 thread->GetID(), process->GetID());
943 const bool override_suspend = true;
944 thread->SetResumeState(eStateRunning, override_suspend);
945 } else {
946 thread->SetResumeState(eStateSuspended);
947 }
948 }
949 }
950
951 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +0000952 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 if (synchronous_execution)
954 error = process->ResumeSynchronous(&stream);
955 else
956 error = process->Resume();
957
958 // We should not be holding the thread list lock when we do this.
959 if (error.Success()) {
960 result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
961 process->GetID());
962 if (synchronous_execution) {
963 // If any state changed events had anything to say, add that to the
964 // result
Zachary Turnerc1564272016-11-16 21:15:24 +0000965 if (stream.GetSize() > 0)
966 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000967
968 result.SetDidChangeProcessState(true);
969 result.SetStatus(eReturnStatusSuccessFinishNoResult);
970 } else {
971 result.SetStatus(eReturnStatusSuccessContinuingNoResult);
972 }
973 } else {
974 result.AppendErrorWithFormat("Failed to resume process: %s\n",
975 error.AsCString());
976 result.SetStatus(eReturnStatusFailed);
977 }
978 } else {
979 result.AppendErrorWithFormat(
980 "Process cannot be continued from its current state (%s).\n",
981 StateAsCString(state));
982 result.SetStatus(eReturnStatusFailed);
983 }
984
985 return result.Succeeded();
986 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987};
988
989//-------------------------------------------------------------------------
990// CommandObjectThreadUntil
991//-------------------------------------------------------------------------
992
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000993static constexpr OptionEnumValueElement g_duo_running_mode[] = {
994 {eOnlyThisThread, "this-thread", "Run only this thread"},
995 {eAllThreads, "all-threads", "Run all threads"} };
996
997static constexpr OptionEnumValues DuoRunningModes() {
998 return OptionEnumValues(g_duo_running_mode);
999}
1000
1001static constexpr OptionDefinition g_thread_until_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001002 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001003 { LLDB_OPT_SET_1, false, "frame", 'f', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeFrameIndex, "Frame index for until operation - defaults to 0" },
1004 { LLDB_OPT_SET_1, false, "thread", 't', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeThreadIndex, "Thread index for the thread for until operation" },
1005 { LLDB_OPT_SET_1, false, "run-mode",'m', OptionParser::eRequiredArgument, nullptr, DuoRunningModes(), 0, eArgTypeRunMode, "Determine how to run other threads while stepping this one" },
1006 { LLDB_OPT_SET_1, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeAddressOrExpression, "Run until we reach the specified address, or leave the function - can be specified multiple times." }
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001007 // clang-format on
1008};
1009
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010class CommandObjectThreadUntil : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 class CommandOptions : public Options {
1013 public:
1014 uint32_t m_thread_idx;
1015 uint32_t m_frame_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016
Kate Stoneb9c1b512016-09-06 20:57:50 +00001017 CommandOptions()
1018 : Options(), m_thread_idx(LLDB_INVALID_THREAD_ID),
1019 m_frame_idx(LLDB_INVALID_FRAME_ID) {
1020 // Keep default values of all options in one place: OptionParsingStarting
1021 // ()
1022 OptionParsingStarting(nullptr);
1023 }
1024
1025 ~CommandOptions() override = default;
1026
Zachary Turner97206d52017-05-12 04:51:55 +00001027 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1028 ExecutionContext *execution_context) override {
1029 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030 const int short_option = m_getopt_table[option_idx].val;
1031
1032 switch (short_option) {
1033 case 'a': {
Pavel Labath47cbf4a2018-04-10 09:03:59 +00001034 lldb::addr_t tmp_addr = OptionArgParser::ToAddress(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035 execution_context, option_arg, LLDB_INVALID_ADDRESS, &error);
1036 if (error.Success())
1037 m_until_addrs.push_back(tmp_addr);
1038 } break;
1039 case 't':
Zachary Turnerfe114832016-11-12 16:56:47 +00001040 if (option_arg.getAsInteger(0, m_thread_idx)) {
1041 m_thread_idx = LLDB_INVALID_INDEX32;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001042 error.SetErrorStringWithFormat("invalid thread index '%s'",
Zachary Turnerfe114832016-11-12 16:56:47 +00001043 option_arg.str().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001044 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 break;
1046 case 'f':
Zachary Turnerfe114832016-11-12 16:56:47 +00001047 if (option_arg.getAsInteger(0, m_frame_idx)) {
1048 m_frame_idx = LLDB_INVALID_FRAME_ID;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001049 error.SetErrorStringWithFormat("invalid frame index '%s'",
Zachary Turnerfe114832016-11-12 16:56:47 +00001050 option_arg.str().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 break;
1053 case 'm': {
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001054 auto enum_values = GetDefinitions()[option_idx].enum_values;
Pavel Labath47cbf4a2018-04-10 09:03:59 +00001055 lldb::RunMode run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum(
Zachary Turnerfe114832016-11-12 16:56:47 +00001056 option_arg, enum_values, eOnlyDuringStepping, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 if (error.Success()) {
1059 if (run_mode == eAllThreads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001060 m_stop_others = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 else
1062 m_stop_others = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 } break;
1065 default:
1066 error.SetErrorStringWithFormat("invalid short option character '%c'",
1067 short_option);
1068 break;
1069 }
1070 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001071 }
1072
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 void OptionParsingStarting(ExecutionContext *execution_context) override {
1074 m_thread_idx = LLDB_INVALID_THREAD_ID;
1075 m_frame_idx = 0;
1076 m_stop_others = false;
1077 m_until_addrs.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001078 }
1079
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001080 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001081 return llvm::makeArrayRef(g_thread_until_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001082 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001083
1084 uint32_t m_step_thread_idx;
1085 bool m_stop_others;
1086 std::vector<lldb::addr_t> m_until_addrs;
1087
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 // Instance variables to hold the values for command options.
1089 };
1090
1091 CommandObjectThreadUntil(CommandInterpreter &interpreter)
1092 : CommandObjectParsed(
1093 interpreter, "thread until",
1094 "Continue until a line number or address is reached by the "
1095 "current or specified thread. Stops when returning from "
Jim Ingham9ac82602016-11-18 22:06:10 +00001096 "the current function as a safety measure. "
1097 "The target line number(s) are given as arguments, and if more than one"
Adrian Kuegelecd760c2016-11-24 10:01:34 +00001098 " is provided, stepping will stop when the first one is hit.",
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099 nullptr,
1100 eCommandRequiresThread | eCommandTryTargetAPILock |
1101 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
1102 m_options() {
1103 CommandArgumentEntry arg;
1104 CommandArgumentData line_num_arg;
1105
1106 // Define the first (and only) variant of this arg.
1107 line_num_arg.arg_type = eArgTypeLineNum;
1108 line_num_arg.arg_repetition = eArgRepeatPlain;
1109
1110 // There is only one variant this argument could be; put it into the
1111 // argument entry.
1112 arg.push_back(line_num_arg);
1113
1114 // Push the data for the first argument into the m_arguments vector.
1115 m_arguments.push_back(arg);
1116 }
1117
1118 ~CommandObjectThreadUntil() override = default;
1119
1120 Options *GetOptions() override { return &m_options; }
1121
Jim Ingham5a988412012-06-08 21:56:10 +00001122protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123 bool DoExecute(Args &command, CommandReturnObject &result) override {
1124 bool synchronous_execution = m_interpreter.GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1127 if (target == nullptr) {
1128 result.AppendError("invalid target, create a debug target using the "
1129 "'target create' command");
1130 result.SetStatus(eReturnStatusFailed);
1131 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132 }
Jim Ingham5a988412012-06-08 21:56:10 +00001133
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134 Process *process = m_exe_ctx.GetProcessPtr();
1135 if (process == nullptr) {
1136 result.AppendError("need a valid process to step");
1137 result.SetStatus(eReturnStatusFailed);
1138 } else {
1139 Thread *thread = nullptr;
1140 std::vector<uint32_t> line_numbers;
1141
1142 if (command.GetArgumentCount() >= 1) {
1143 size_t num_args = command.GetArgumentCount();
1144 for (size_t i = 0; i < num_args; i++) {
1145 uint32_t line_number;
Jim Ingham9ac82602016-11-18 22:06:10 +00001146 line_number = StringConvert::ToUInt32(command.GetArgumentAtIndex(i),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147 UINT32_MAX);
1148 if (line_number == UINT32_MAX) {
1149 result.AppendErrorWithFormat("invalid line number: '%s'.\n",
Jim Ingham9ac82602016-11-18 22:06:10 +00001150 command.GetArgumentAtIndex(i));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151 result.SetStatus(eReturnStatusFailed);
1152 return false;
1153 } else
1154 line_numbers.push_back(line_number);
1155 }
1156 } else if (m_options.m_until_addrs.empty()) {
1157 result.AppendErrorWithFormat("No line number or address provided:\n%s",
Zachary Turner1e8016b2016-11-15 00:45:18 +00001158 GetSyntax().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 result.SetStatus(eReturnStatusFailed);
1160 return false;
1161 }
1162
1163 if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID) {
1164 thread = GetDefaultThread();
1165 } else {
1166 thread = process->GetThreadList()
1167 .FindThreadByIndexID(m_options.m_thread_idx)
1168 .get();
1169 }
1170
1171 if (thread == nullptr) {
1172 const uint32_t num_threads = process->GetThreadList().GetSize();
1173 result.AppendErrorWithFormat(
1174 "Thread index %u is out of range (valid values are 0 - %u).\n",
1175 m_options.m_thread_idx, num_threads);
1176 result.SetStatus(eReturnStatusFailed);
1177 return false;
1178 }
1179
1180 const bool abort_other_plans = false;
1181
1182 StackFrame *frame =
1183 thread->GetStackFrameAtIndex(m_options.m_frame_idx).get();
1184 if (frame == nullptr) {
1185 result.AppendErrorWithFormat(
1186 "Frame index %u is out of range for thread %u.\n",
1187 m_options.m_frame_idx, m_options.m_thread_idx);
1188 result.SetStatus(eReturnStatusFailed);
1189 return false;
1190 }
1191
1192 ThreadPlanSP new_plan_sp;
1193
1194 if (frame->HasDebugInformation()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001195 // Finally we got here... Translate the given line number to a bunch
1196 // of addresses:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197 SymbolContext sc(frame->GetSymbolContext(eSymbolContextCompUnit));
1198 LineTable *line_table = nullptr;
1199 if (sc.comp_unit)
1200 line_table = sc.comp_unit->GetLineTable();
1201
1202 if (line_table == nullptr) {
1203 result.AppendErrorWithFormat("Failed to resolve the line table for "
1204 "frame %u of thread index %u.\n",
1205 m_options.m_frame_idx,
1206 m_options.m_thread_idx);
1207 result.SetStatus(eReturnStatusFailed);
1208 return false;
1209 }
1210
1211 LineEntry function_start;
1212 uint32_t index_ptr = 0, end_ptr;
1213 std::vector<addr_t> address_list;
1214
1215 // Find the beginning & end index of the
1216 AddressRange fun_addr_range = sc.function->GetAddressRange();
1217 Address fun_start_addr = fun_addr_range.GetBaseAddress();
1218 line_table->FindLineEntryByAddress(fun_start_addr, function_start,
1219 &index_ptr);
1220
1221 Address fun_end_addr(fun_start_addr.GetSection(),
1222 fun_start_addr.GetOffset() +
1223 fun_addr_range.GetByteSize());
1224
1225 bool all_in_function = true;
1226
1227 line_table->FindLineEntryByAddress(fun_end_addr, function_start,
1228 &end_ptr);
1229
1230 for (uint32_t line_number : line_numbers) {
1231 uint32_t start_idx_ptr = index_ptr;
1232 while (start_idx_ptr <= end_ptr) {
1233 LineEntry line_entry;
1234 const bool exact = false;
1235 start_idx_ptr = sc.comp_unit->FindLineEntry(
1236 start_idx_ptr, line_number, sc.comp_unit, exact, &line_entry);
1237 if (start_idx_ptr == UINT32_MAX)
1238 break;
1239
1240 addr_t address =
1241 line_entry.range.GetBaseAddress().GetLoadAddress(target);
1242 if (address != LLDB_INVALID_ADDRESS) {
1243 if (fun_addr_range.ContainsLoadAddress(address, target))
1244 address_list.push_back(address);
1245 else
1246 all_in_function = false;
1247 }
1248 start_idx_ptr++;
1249 }
1250 }
1251
1252 for (lldb::addr_t address : m_options.m_until_addrs) {
1253 if (fun_addr_range.ContainsLoadAddress(address, target))
1254 address_list.push_back(address);
1255 else
1256 all_in_function = false;
1257 }
1258
1259 if (address_list.empty()) {
1260 if (all_in_function)
1261 result.AppendErrorWithFormat(
1262 "No line entries matching until target.\n");
1263 else
1264 result.AppendErrorWithFormat(
1265 "Until target outside of the current function.\n");
1266
1267 result.SetStatus(eReturnStatusFailed);
1268 return false;
1269 }
1270
1271 new_plan_sp = thread->QueueThreadPlanForStepUntil(
1272 abort_other_plans, &address_list.front(), address_list.size(),
1273 m_options.m_stop_others, m_options.m_frame_idx);
1274 // User level plans should be master plans so they can be interrupted
Adrian Prantl05097242018-04-30 16:49:04 +00001275 // (e.g. by hitting a breakpoint) and other plans executed by the user
1276 // (stepping around the breakpoint) and then a "continue" will resume
1277 // the original plan.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001278 new_plan_sp->SetIsMasterPlan(true);
1279 new_plan_sp->SetOkayToDiscard(false);
1280 } else {
1281 result.AppendErrorWithFormat(
1282 "Frame index %u of thread %u has no debug information.\n",
1283 m_options.m_frame_idx, m_options.m_thread_idx);
1284 result.SetStatus(eReturnStatusFailed);
1285 return false;
1286 }
1287
1288 process->GetThreadList().SetSelectedThreadByID(m_options.m_thread_idx);
1289
1290 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +00001291 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001292 if (synchronous_execution)
1293 error = process->ResumeSynchronous(&stream);
1294 else
1295 error = process->Resume();
1296
1297 if (error.Success()) {
1298 result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
1299 process->GetID());
1300 if (synchronous_execution) {
1301 // If any state changed events had anything to say, add that to the
1302 // result
Zachary Turnerc1564272016-11-16 21:15:24 +00001303 if (stream.GetSize() > 0)
1304 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001305
1306 result.SetDidChangeProcessState(true);
1307 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1308 } else {
1309 result.SetStatus(eReturnStatusSuccessContinuingNoResult);
1310 }
1311 } else {
1312 result.AppendErrorWithFormat("Failed to resume process: %s.\n",
1313 error.AsCString());
1314 result.SetStatus(eReturnStatusFailed);
1315 }
1316 }
1317 return result.Succeeded();
1318 }
1319
1320 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321};
1322
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001323//-------------------------------------------------------------------------
1324// CommandObjectThreadSelect
1325//-------------------------------------------------------------------------
1326
Kate Stoneb9c1b512016-09-06 20:57:50 +00001327class CommandObjectThreadSelect : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329 CommandObjectThreadSelect(CommandInterpreter &interpreter)
1330 : CommandObjectParsed(interpreter, "thread select",
1331 "Change the currently selected thread.", nullptr,
1332 eCommandRequiresProcess | eCommandTryTargetAPILock |
1333 eCommandProcessMustBeLaunched |
1334 eCommandProcessMustBePaused) {
1335 CommandArgumentEntry arg;
1336 CommandArgumentData thread_idx_arg;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001337
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 // Define the first (and only) variant of this arg.
1339 thread_idx_arg.arg_type = eArgTypeThreadIndex;
1340 thread_idx_arg.arg_repetition = eArgRepeatPlain;
1341
1342 // There is only one variant this argument could be; put it into the
1343 // argument entry.
1344 arg.push_back(thread_idx_arg);
1345
1346 // Push the data for the first argument into the m_arguments vector.
1347 m_arguments.push_back(arg);
1348 }
1349
1350 ~CommandObjectThreadSelect() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001351
Jim Ingham5a988412012-06-08 21:56:10 +00001352protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353 bool DoExecute(Args &command, CommandReturnObject &result) override {
1354 Process *process = m_exe_ctx.GetProcessPtr();
1355 if (process == nullptr) {
1356 result.AppendError("no process");
1357 result.SetStatus(eReturnStatusFailed);
1358 return false;
1359 } else if (command.GetArgumentCount() != 1) {
1360 result.AppendErrorWithFormat(
1361 "'%s' takes exactly one thread index argument:\nUsage: %s\n",
1362 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1363 result.SetStatus(eReturnStatusFailed);
1364 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366
1367 uint32_t index_id =
1368 StringConvert::ToUInt32(command.GetArgumentAtIndex(0), 0, 0);
1369
1370 Thread *new_thread =
1371 process->GetThreadList().FindThreadByIndexID(index_id).get();
1372 if (new_thread == nullptr) {
1373 result.AppendErrorWithFormat("invalid thread #%s.\n",
1374 command.GetArgumentAtIndex(0));
1375 result.SetStatus(eReturnStatusFailed);
1376 return false;
1377 }
1378
1379 process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true);
1380 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1381
1382 return result.Succeeded();
1383 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001384};
1385
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001386//-------------------------------------------------------------------------
1387// CommandObjectThreadList
1388//-------------------------------------------------------------------------
1389
Kate Stoneb9c1b512016-09-06 20:57:50 +00001390class CommandObjectThreadList : public CommandObjectParsed {
Greg Clayton66111032010-06-23 01:19:29 +00001391public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392 CommandObjectThreadList(CommandInterpreter &interpreter)
1393 : CommandObjectParsed(
1394 interpreter, "thread list",
1395 "Show a summary of each thread in the current target process.",
1396 "thread list",
1397 eCommandRequiresProcess | eCommandTryTargetAPILock |
1398 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001399
Kate Stoneb9c1b512016-09-06 20:57:50 +00001400 ~CommandObjectThreadList() override = default;
Greg Clayton66111032010-06-23 01:19:29 +00001401
Jim Ingham5a988412012-06-08 21:56:10 +00001402protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403 bool DoExecute(Args &command, CommandReturnObject &result) override {
1404 Stream &strm = result.GetOutputStream();
1405 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1406 Process *process = m_exe_ctx.GetProcessPtr();
1407 const bool only_threads_with_stop_reason = false;
1408 const uint32_t start_frame = 0;
1409 const uint32_t num_frames = 0;
1410 const uint32_t num_frames_with_source = 0;
1411 process->GetStatus(strm);
1412 process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame,
Jim Ingham6a9767c2016-11-08 20:36:40 +00001413 num_frames, num_frames_with_source, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001414 return result.Succeeded();
1415 }
Greg Clayton66111032010-06-23 01:19:29 +00001416};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001417
Jim Ingham93208b82013-01-31 21:46:01 +00001418//-------------------------------------------------------------------------
Jason Molenda705b1802014-06-13 02:37:02 +00001419// CommandObjectThreadInfo
1420//-------------------------------------------------------------------------
1421
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001422static constexpr OptionDefinition g_thread_info_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001423 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001424 { LLDB_OPT_SET_ALL, false, "json", 'j', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Display the thread info in JSON format." },
1425 { LLDB_OPT_SET_ALL, false, "stop-info", 's', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Display the extended stop info in JSON format." }
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001426 // clang-format on
1427};
1428
Kate Stoneb9c1b512016-09-06 20:57:50 +00001429class CommandObjectThreadInfo : public CommandObjectIterateOverThreads {
Jason Molenda705b1802014-06-13 02:37:02 +00001430public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431 class CommandOptions : public Options {
1432 public:
1433 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
Jason Molenda705b1802014-06-13 02:37:02 +00001434
Kate Stoneb9c1b512016-09-06 20:57:50 +00001435 ~CommandOptions() override = default;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001436
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437 void OptionParsingStarting(ExecutionContext *execution_context) override {
1438 m_json_thread = false;
1439 m_json_stopinfo = false;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001440 }
1441
Zachary Turner97206d52017-05-12 04:51:55 +00001442 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1443 ExecutionContext *execution_context) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 const int short_option = m_getopt_table[option_idx].val;
Zachary Turner97206d52017-05-12 04:51:55 +00001445 Status error;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001446
Kate Stoneb9c1b512016-09-06 20:57:50 +00001447 switch (short_option) {
1448 case 'j':
1449 m_json_thread = true;
1450 break;
1451
1452 case 's':
1453 m_json_stopinfo = true;
1454 break;
1455
1456 default:
Zachary Turner97206d52017-05-12 04:51:55 +00001457 return Status("invalid short option character '%c'", short_option);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458 }
1459 return error;
Jason Molenda705b1802014-06-13 02:37:02 +00001460 }
1461
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001462 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001463 return llvm::makeArrayRef(g_thread_info_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001464 }
Stephane Sezerf8104912016-03-17 18:52:41 +00001465
Kate Stoneb9c1b512016-09-06 20:57:50 +00001466 bool m_json_thread;
1467 bool m_json_stopinfo;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468 };
1469
1470 CommandObjectThreadInfo(CommandInterpreter &interpreter)
1471 : CommandObjectIterateOverThreads(
1472 interpreter, "thread info", "Show an extended summary of one or "
1473 "more threads. Defaults to the "
1474 "current thread.",
1475 "thread info",
1476 eCommandRequiresProcess | eCommandTryTargetAPILock |
1477 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
1478 m_options() {
1479 m_add_return = false;
1480 }
1481
1482 ~CommandObjectThreadInfo() override = default;
1483
1484 Options *GetOptions() override { return &m_options; }
1485
1486 bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override {
1487 ThreadSP thread_sp =
1488 m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
1489 if (!thread_sp) {
1490 result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n",
1491 tid);
1492 result.SetStatus(eReturnStatusFailed);
1493 return false;
Jason Molenda705b1802014-06-13 02:37:02 +00001494 }
1495
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496 Thread *thread = thread_sp.get();
1497
1498 Stream &strm = result.GetOutputStream();
1499 if (!thread->GetDescription(strm, eDescriptionLevelFull,
1500 m_options.m_json_thread,
1501 m_options.m_json_stopinfo)) {
1502 result.AppendErrorWithFormat("error displaying info for thread: \"%d\"\n",
1503 thread->GetIndexID());
1504 result.SetStatus(eReturnStatusFailed);
1505 return false;
1506 }
1507 return true;
1508 }
1509
1510 CommandOptions m_options;
Jason Molenda705b1802014-06-13 02:37:02 +00001511};
1512
Jason Molenda705b1802014-06-13 02:37:02 +00001513//-------------------------------------------------------------------------
Jim Ingham93208b82013-01-31 21:46:01 +00001514// CommandObjectThreadReturn
1515//-------------------------------------------------------------------------
1516
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001517static constexpr OptionDefinition g_thread_return_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001518 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001519 { LLDB_OPT_SET_ALL, false, "from-expression", 'x', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Return from the innermost expression evaluation." }
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001520 // clang-format on
1521};
1522
Kate Stoneb9c1b512016-09-06 20:57:50 +00001523class CommandObjectThreadReturn : public CommandObjectRaw {
Jim Inghamcb640dd2012-09-14 02:14:15 +00001524public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001525 class CommandOptions : public Options {
1526 public:
1527 CommandOptions() : Options(), m_from_expression(false) {
1528 // Keep default values of all options in one place: OptionParsingStarting
1529 // ()
1530 OptionParsingStarting(nullptr);
Jim Inghamcb640dd2012-09-14 02:14:15 +00001531 }
Jim Inghamcb640dd2012-09-14 02:14:15 +00001532
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 ~CommandOptions() override = default;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001534
Zachary Turner97206d52017-05-12 04:51:55 +00001535 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1536 ExecutionContext *execution_context) override {
1537 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001538 const int short_option = m_getopt_table[option_idx].val;
1539
1540 switch (short_option) {
1541 case 'x': {
1542 bool success;
Pavel Labath47cbf4a2018-04-10 09:03:59 +00001543 bool tmp_value =
1544 OptionArgParser::ToBoolean(option_arg, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545 if (success)
1546 m_from_expression = tmp_value;
1547 else {
1548 error.SetErrorStringWithFormat(
Zachary Turnerfe114832016-11-12 16:56:47 +00001549 "invalid boolean value '%s' for 'x' option",
1550 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001551 }
1552 } break;
1553 default:
1554 error.SetErrorStringWithFormat("invalid short option character '%c'",
1555 short_option);
1556 break;
1557 }
1558 return error;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001559 }
1560
Kate Stoneb9c1b512016-09-06 20:57:50 +00001561 void OptionParsingStarting(ExecutionContext *execution_context) override {
1562 m_from_expression = false;
1563 }
1564
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001565 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001566 return llvm::makeArrayRef(g_thread_return_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001568
1569 bool m_from_expression;
1570
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 // Instance variables to hold the values for command options.
1572 };
1573
1574 CommandObjectThreadReturn(CommandInterpreter &interpreter)
1575 : CommandObjectRaw(interpreter, "thread return",
1576 "Prematurely return from a stack frame, "
1577 "short-circuiting execution of newer frames "
1578 "and optionally yielding a specified value. Defaults "
1579 "to the exiting the current stack "
1580 "frame.",
1581 "thread return",
1582 eCommandRequiresFrame | eCommandTryTargetAPILock |
1583 eCommandProcessMustBeLaunched |
1584 eCommandProcessMustBePaused),
1585 m_options() {
1586 CommandArgumentEntry arg;
1587 CommandArgumentData expression_arg;
1588
1589 // Define the first (and only) variant of this arg.
1590 expression_arg.arg_type = eArgTypeExpression;
1591 expression_arg.arg_repetition = eArgRepeatOptional;
1592
1593 // There is only one variant this argument could be; put it into the
1594 // argument entry.
1595 arg.push_back(expression_arg);
1596
1597 // Push the data for the first argument into the m_arguments vector.
1598 m_arguments.push_back(arg);
1599 }
1600
1601 ~CommandObjectThreadReturn() override = default;
1602
1603 Options *GetOptions() override { return &m_options; }
1604
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001605protected:
Raphael Isemann4d51a902018-07-12 22:28:52 +00001606 bool DoExecute(llvm::StringRef command,
1607 CommandReturnObject &result) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001608 // I am going to handle this by hand, because I don't want you to have to
1609 // say:
1610 // "thread return -- -5".
Raphael Isemann4d51a902018-07-12 22:28:52 +00001611 if (command.startswith("-x")) {
1612 if (command.size() != 2U)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001613 result.AppendWarning("Return values ignored when returning from user "
1614 "called expressions");
Jim Inghamcb640dd2012-09-14 02:14:15 +00001615
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616 Thread *thread = m_exe_ctx.GetThreadPtr();
Zachary Turner97206d52017-05-12 04:51:55 +00001617 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001618 error = thread->UnwindInnermostExpression();
1619 if (!error.Success()) {
1620 result.AppendErrorWithFormat("Unwinding expression failed - %s.",
1621 error.AsCString());
1622 result.SetStatus(eReturnStatusFailed);
1623 } else {
1624 bool success =
1625 thread->SetSelectedFrameByIndexNoisily(0, result.GetOutputStream());
1626 if (success) {
1627 m_exe_ctx.SetFrameSP(thread->GetSelectedFrame());
1628 result.SetStatus(eReturnStatusSuccessFinishResult);
1629 } else {
1630 result.AppendErrorWithFormat(
1631 "Could not select 0th frame after unwinding expression.");
1632 result.SetStatus(eReturnStatusFailed);
Jim Inghamcb640dd2012-09-14 02:14:15 +00001633 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001634 }
1635 return result.Succeeded();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001636 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637
1638 ValueObjectSP return_valobj_sp;
1639
1640 StackFrameSP frame_sp = m_exe_ctx.GetFrameSP();
1641 uint32_t frame_idx = frame_sp->GetFrameIndex();
1642
1643 if (frame_sp->IsInlined()) {
1644 result.AppendError("Don't know how to return from inlined frames.");
1645 result.SetStatus(eReturnStatusFailed);
1646 return false;
1647 }
1648
Raphael Isemann4d51a902018-07-12 22:28:52 +00001649 if (!command.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001650 Target *target = m_exe_ctx.GetTargetPtr();
1651 EvaluateExpressionOptions options;
1652
1653 options.SetUnwindOnError(true);
1654 options.SetUseDynamic(eNoDynamicValues);
1655
1656 ExpressionResults exe_results = eExpressionSetupError;
1657 exe_results = target->EvaluateExpression(command, frame_sp.get(),
1658 return_valobj_sp, options);
1659 if (exe_results != eExpressionCompleted) {
1660 if (return_valobj_sp)
1661 result.AppendErrorWithFormat(
1662 "Error evaluating result expression: %s",
1663 return_valobj_sp->GetError().AsCString());
1664 else
1665 result.AppendErrorWithFormat(
1666 "Unknown error evaluating result expression.");
1667 result.SetStatus(eReturnStatusFailed);
1668 return false;
1669 }
1670 }
1671
Zachary Turner97206d52017-05-12 04:51:55 +00001672 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001673 ThreadSP thread_sp = m_exe_ctx.GetThreadSP();
1674 const bool broadcast = true;
1675 error = thread_sp->ReturnFromFrame(frame_sp, return_valobj_sp, broadcast);
1676 if (!error.Success()) {
1677 result.AppendErrorWithFormat(
1678 "Error returning from frame %d of thread %d: %s.", frame_idx,
1679 thread_sp->GetIndexID(), error.AsCString());
1680 result.SetStatus(eReturnStatusFailed);
1681 return false;
1682 }
1683
1684 result.SetStatus(eReturnStatusSuccessFinishResult);
1685 return true;
1686 }
1687
1688 CommandOptions m_options;
Jim Inghamcb640dd2012-09-14 02:14:15 +00001689};
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001690
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001691//-------------------------------------------------------------------------
Richard Mittonf86248d2013-09-12 02:20:34 +00001692// CommandObjectThreadJump
1693//-------------------------------------------------------------------------
1694
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001695static constexpr OptionDefinition g_thread_jump_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001696 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001697 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, {}, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "Specifies the source file to jump to." },
1698 { LLDB_OPT_SET_1, true, "line", 'l', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeLineNum, "Specifies the line number to jump to." },
1699 { LLDB_OPT_SET_2, true, "by", 'b', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeOffset, "Jumps by a relative line offset from the current line." },
1700 { LLDB_OPT_SET_3, true, "address", 'a', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeAddressOrExpression, "Jumps to a specific address." },
1701 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3, false, "force", 'r', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Allows the PC to leave the current function." }
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001702 // clang-format on
1703};
1704
Kate Stoneb9c1b512016-09-06 20:57:50 +00001705class CommandObjectThreadJump : public CommandObjectParsed {
Richard Mittonf86248d2013-09-12 02:20:34 +00001706public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707 class CommandOptions : public Options {
1708 public:
1709 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
Richard Mittonf86248d2013-09-12 02:20:34 +00001710
Kate Stoneb9c1b512016-09-06 20:57:50 +00001711 ~CommandOptions() override = default;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001712
Kate Stoneb9c1b512016-09-06 20:57:50 +00001713 void OptionParsingStarting(ExecutionContext *execution_context) override {
1714 m_filenames.Clear();
1715 m_line_num = 0;
1716 m_line_offset = 0;
1717 m_load_addr = LLDB_INVALID_ADDRESS;
1718 m_force = false;
Richard Mittonf86248d2013-09-12 02:20:34 +00001719 }
1720
Zachary Turner97206d52017-05-12 04:51:55 +00001721 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1722 ExecutionContext *execution_context) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001723 const int short_option = m_getopt_table[option_idx].val;
Zachary Turner97206d52017-05-12 04:51:55 +00001724 Status error;
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001725
Kate Stoneb9c1b512016-09-06 20:57:50 +00001726 switch (short_option) {
1727 case 'f':
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001728 m_filenames.AppendIfUnique(FileSpec(option_arg));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001729 if (m_filenames.GetSize() > 1)
Zachary Turner97206d52017-05-12 04:51:55 +00001730 return Status("only one source file expected.");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001731 break;
1732 case 'l':
Zachary Turnerfe114832016-11-12 16:56:47 +00001733 if (option_arg.getAsInteger(0, m_line_num))
Zachary Turner97206d52017-05-12 04:51:55 +00001734 return Status("invalid line number: '%s'.", option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001735 break;
1736 case 'b':
Zachary Turnerfe114832016-11-12 16:56:47 +00001737 if (option_arg.getAsInteger(0, m_line_offset))
Zachary Turner97206d52017-05-12 04:51:55 +00001738 return Status("invalid line offset: '%s'.", option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001739 break;
1740 case 'a':
Pavel Labath47cbf4a2018-04-10 09:03:59 +00001741 m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg,
1742 LLDB_INVALID_ADDRESS, &error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001743 break;
1744 case 'r':
1745 m_force = true;
1746 break;
1747 default:
Zachary Turner97206d52017-05-12 04:51:55 +00001748 return Status("invalid short option character '%c'", short_option);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001749 }
1750 return error;
Richard Mittonf86248d2013-09-12 02:20:34 +00001751 }
1752
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001753 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001754 return llvm::makeArrayRef(g_thread_jump_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001755 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001756
1757 FileSpecList m_filenames;
1758 uint32_t m_line_num;
1759 int32_t m_line_offset;
1760 lldb::addr_t m_load_addr;
1761 bool m_force;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001762 };
1763
1764 CommandObjectThreadJump(CommandInterpreter &interpreter)
1765 : CommandObjectParsed(
1766 interpreter, "thread jump",
1767 "Sets the program counter to a new address.", "thread jump",
1768 eCommandRequiresFrame | eCommandTryTargetAPILock |
1769 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
1770 m_options() {}
1771
1772 ~CommandObjectThreadJump() override = default;
1773
1774 Options *GetOptions() override { return &m_options; }
1775
Richard Mittonf86248d2013-09-12 02:20:34 +00001776protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001777 bool DoExecute(Args &args, CommandReturnObject &result) override {
1778 RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext();
1779 StackFrame *frame = m_exe_ctx.GetFramePtr();
1780 Thread *thread = m_exe_ctx.GetThreadPtr();
1781 Target *target = m_exe_ctx.GetTargetPtr();
1782 const SymbolContext &sym_ctx =
1783 frame->GetSymbolContext(eSymbolContextLineEntry);
Richard Mittonf86248d2013-09-12 02:20:34 +00001784
Kate Stoneb9c1b512016-09-06 20:57:50 +00001785 if (m_options.m_load_addr != LLDB_INVALID_ADDRESS) {
1786 // Use this address directly.
1787 Address dest = Address(m_options.m_load_addr);
Richard Mittonf86248d2013-09-12 02:20:34 +00001788
Kate Stoneb9c1b512016-09-06 20:57:50 +00001789 lldb::addr_t callAddr = dest.GetCallableLoadAddress(target);
1790 if (callAddr == LLDB_INVALID_ADDRESS) {
1791 result.AppendErrorWithFormat("Invalid destination address.");
1792 result.SetStatus(eReturnStatusFailed);
1793 return false;
1794 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001795
Kate Stoneb9c1b512016-09-06 20:57:50 +00001796 if (!reg_ctx->SetPC(callAddr)) {
1797 result.AppendErrorWithFormat("Error changing PC value for thread %d.",
1798 thread->GetIndexID());
1799 result.SetStatus(eReturnStatusFailed);
1800 return false;
1801 }
1802 } else {
1803 // Pick either the absolute line, or work out a relative one.
1804 int32_t line = (int32_t)m_options.m_line_num;
1805 if (line == 0)
1806 line = sym_ctx.line_entry.line + m_options.m_line_offset;
Richard Mittonf86248d2013-09-12 02:20:34 +00001807
Kate Stoneb9c1b512016-09-06 20:57:50 +00001808 // Try the current file, but override if asked.
1809 FileSpec file = sym_ctx.line_entry.file;
1810 if (m_options.m_filenames.GetSize() == 1)
1811 file = m_options.m_filenames.GetFileSpecAtIndex(0);
Richard Mittonf86248d2013-09-12 02:20:34 +00001812
Kate Stoneb9c1b512016-09-06 20:57:50 +00001813 if (!file) {
1814 result.AppendErrorWithFormat(
1815 "No source file available for the current location.");
1816 result.SetStatus(eReturnStatusFailed);
1817 return false;
1818 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001819
Kate Stoneb9c1b512016-09-06 20:57:50 +00001820 std::string warnings;
Zachary Turner97206d52017-05-12 04:51:55 +00001821 Status err = thread->JumpToLine(file, line, m_options.m_force, &warnings);
Richard Mittonf86248d2013-09-12 02:20:34 +00001822
Kate Stoneb9c1b512016-09-06 20:57:50 +00001823 if (err.Fail()) {
1824 result.SetError(err);
1825 return false;
1826 }
Richard Mittonf86248d2013-09-12 02:20:34 +00001827
Kate Stoneb9c1b512016-09-06 20:57:50 +00001828 if (!warnings.empty())
1829 result.AppendWarning(warnings.c_str());
Richard Mittonf86248d2013-09-12 02:20:34 +00001830 }
1831
Kate Stoneb9c1b512016-09-06 20:57:50 +00001832 result.SetStatus(eReturnStatusSuccessFinishResult);
1833 return true;
1834 }
1835
1836 CommandOptions m_options;
Richard Mittonf86248d2013-09-12 02:20:34 +00001837};
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001838
Richard Mittonf86248d2013-09-12 02:20:34 +00001839//-------------------------------------------------------------------------
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001840// Next are the subcommands of CommandObjectMultiwordThreadPlan
1841//-------------------------------------------------------------------------
1842
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001843//-------------------------------------------------------------------------
1844// CommandObjectThreadPlanList
1845//-------------------------------------------------------------------------
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001846
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001847static constexpr OptionDefinition g_thread_plan_list_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001848 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001849 { LLDB_OPT_SET_1, false, "verbose", 'v', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Display more information about the thread plans" },
1850 { LLDB_OPT_SET_1, false, "internal", 'i', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Display internal as well as user thread plans" }
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001851 // clang-format on
1852};
1853
Kate Stoneb9c1b512016-09-06 20:57:50 +00001854class CommandObjectThreadPlanList : public CommandObjectIterateOverThreads {
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001855public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856 class CommandOptions : public Options {
1857 public:
1858 CommandOptions() : Options() {
1859 // Keep default values of all options in one place: OptionParsingStarting
1860 // ()
1861 OptionParsingStarting(nullptr);
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001862 }
1863
Kate Stoneb9c1b512016-09-06 20:57:50 +00001864 ~CommandOptions() override = default;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001865
Zachary Turner97206d52017-05-12 04:51:55 +00001866 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1867 ExecutionContext *execution_context) override {
1868 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001869 const int short_option = m_getopt_table[option_idx].val;
1870
1871 switch (short_option) {
1872 case 'i':
1873 m_internal = true;
1874 break;
1875 case 'v':
1876 m_verbose = true;
1877 break;
1878 default:
1879 error.SetErrorStringWithFormat("invalid short option character '%c'",
1880 short_option);
1881 break;
1882 }
1883 return error;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001884 }
1885
Kate Stoneb9c1b512016-09-06 20:57:50 +00001886 void OptionParsingStarting(ExecutionContext *execution_context) override {
1887 m_verbose = false;
1888 m_internal = false;
1889 }
1890
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001891 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001892 return llvm::makeArrayRef(g_thread_plan_list_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001893 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001894
1895 // Instance variables to hold the values for command options.
1896 bool m_verbose;
1897 bool m_internal;
1898 };
1899
1900 CommandObjectThreadPlanList(CommandInterpreter &interpreter)
1901 : CommandObjectIterateOverThreads(
1902 interpreter, "thread plan list",
1903 "Show thread plans for one or more threads. If no threads are "
1904 "specified, show the "
1905 "current thread. Use the thread-index \"all\" to see all threads.",
1906 nullptr,
1907 eCommandRequiresProcess | eCommandRequiresThread |
1908 eCommandTryTargetAPILock | eCommandProcessMustBeLaunched |
1909 eCommandProcessMustBePaused),
1910 m_options() {}
1911
1912 ~CommandObjectThreadPlanList() override = default;
1913
1914 Options *GetOptions() override { return &m_options; }
1915
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001916protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001917 bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override {
1918 ThreadSP thread_sp =
1919 m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid);
1920 if (!thread_sp) {
1921 result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n",
1922 tid);
1923 result.SetStatus(eReturnStatusFailed);
1924 return false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001925 }
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00001926
Kate Stoneb9c1b512016-09-06 20:57:50 +00001927 Thread *thread = thread_sp.get();
1928
1929 Stream &strm = result.GetOutputStream();
1930 DescriptionLevel desc_level = eDescriptionLevelFull;
1931 if (m_options.m_verbose)
1932 desc_level = eDescriptionLevelVerbose;
1933
1934 thread->DumpThreadPlans(&strm, desc_level, m_options.m_internal, true);
1935 return true;
1936 }
1937
1938 CommandOptions m_options;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001939};
1940
Kate Stoneb9c1b512016-09-06 20:57:50 +00001941class CommandObjectThreadPlanDiscard : public CommandObjectParsed {
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001942public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001943 CommandObjectThreadPlanDiscard(CommandInterpreter &interpreter)
1944 : CommandObjectParsed(interpreter, "thread plan discard",
1945 "Discards thread plans up to and including the "
1946 "specified index (see 'thread plan list'.) "
1947 "Only user visible plans can be discarded.",
1948 nullptr,
1949 eCommandRequiresProcess | eCommandRequiresThread |
1950 eCommandTryTargetAPILock |
1951 eCommandProcessMustBeLaunched |
1952 eCommandProcessMustBePaused) {
1953 CommandArgumentEntry arg;
1954 CommandArgumentData plan_index_arg;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001955
Kate Stoneb9c1b512016-09-06 20:57:50 +00001956 // Define the first (and only) variant of this arg.
1957 plan_index_arg.arg_type = eArgTypeUnsignedInteger;
1958 plan_index_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001959
Kate Stoneb9c1b512016-09-06 20:57:50 +00001960 // There is only one variant this argument could be; put it into the
1961 // argument entry.
1962 arg.push_back(plan_index_arg);
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001963
Kate Stoneb9c1b512016-09-06 20:57:50 +00001964 // Push the data for the first argument into the m_arguments vector.
1965 m_arguments.push_back(arg);
1966 }
1967
1968 ~CommandObjectThreadPlanDiscard() override = default;
1969
1970 bool DoExecute(Args &args, CommandReturnObject &result) override {
1971 Thread *thread = m_exe_ctx.GetThreadPtr();
1972 if (args.GetArgumentCount() != 1) {
1973 result.AppendErrorWithFormat("Too many arguments, expected one - the "
1974 "thread plan index - but got %zu.",
1975 args.GetArgumentCount());
1976 result.SetStatus(eReturnStatusFailed);
1977 return false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001978 }
1979
Kate Stoneb9c1b512016-09-06 20:57:50 +00001980 bool success;
1981 uint32_t thread_plan_idx =
1982 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), 0, 0, &success);
1983 if (!success) {
1984 result.AppendErrorWithFormat(
1985 "Invalid thread index: \"%s\" - should be unsigned int.",
1986 args.GetArgumentAtIndex(0));
1987 result.SetStatus(eReturnStatusFailed);
1988 return false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001989 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001990
1991 if (thread_plan_idx == 0) {
1992 result.AppendErrorWithFormat(
1993 "You wouldn't really want me to discard the base thread plan.");
1994 result.SetStatus(eReturnStatusFailed);
1995 return false;
1996 }
1997
1998 if (thread->DiscardUserThreadPlansUpToIndex(thread_plan_idx)) {
1999 result.SetStatus(eReturnStatusSuccessFinishNoResult);
2000 return true;
2001 } else {
2002 result.AppendErrorWithFormat(
2003 "Could not find User thread plan with index %s.",
2004 args.GetArgumentAtIndex(0));
2005 result.SetStatus(eReturnStatusFailed);
2006 return false;
2007 }
2008 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002009};
2010
2011//-------------------------------------------------------------------------
2012// CommandObjectMultiwordThreadPlan
2013//-------------------------------------------------------------------------
2014
Kate Stoneb9c1b512016-09-06 20:57:50 +00002015class CommandObjectMultiwordThreadPlan : public CommandObjectMultiword {
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002016public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002017 CommandObjectMultiwordThreadPlan(CommandInterpreter &interpreter)
2018 : CommandObjectMultiword(
2019 interpreter, "plan",
2020 "Commands for managing thread plans that control execution.",
2021 "thread plan <subcommand> [<subcommand objects]") {
2022 LoadSubCommand(
2023 "list", CommandObjectSP(new CommandObjectThreadPlanList(interpreter)));
2024 LoadSubCommand(
2025 "discard",
2026 CommandObjectSP(new CommandObjectThreadPlanDiscard(interpreter)));
2027 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002028
Kate Stoneb9c1b512016-09-06 20:57:50 +00002029 ~CommandObjectMultiwordThreadPlan() override = default;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002030};
2031
2032//-------------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002033// CommandObjectMultiwordThread
2034//-------------------------------------------------------------------------
2035
Kate Stoneb9c1b512016-09-06 20:57:50 +00002036CommandObjectMultiwordThread::CommandObjectMultiwordThread(
2037 CommandInterpreter &interpreter)
2038 : CommandObjectMultiword(interpreter, "thread", "Commands for operating on "
2039 "one or more threads in "
2040 "the current process.",
2041 "thread <subcommand> [<subcommand-options>]") {
2042 LoadSubCommand("backtrace", CommandObjectSP(new CommandObjectThreadBacktrace(
2043 interpreter)));
2044 LoadSubCommand("continue",
2045 CommandObjectSP(new CommandObjectThreadContinue(interpreter)));
2046 LoadSubCommand("list",
2047 CommandObjectSP(new CommandObjectThreadList(interpreter)));
2048 LoadSubCommand("return",
2049 CommandObjectSP(new CommandObjectThreadReturn(interpreter)));
2050 LoadSubCommand("jump",
2051 CommandObjectSP(new CommandObjectThreadJump(interpreter)));
2052 LoadSubCommand("select",
2053 CommandObjectSP(new CommandObjectThreadSelect(interpreter)));
2054 LoadSubCommand("until",
2055 CommandObjectSP(new CommandObjectThreadUntil(interpreter)));
2056 LoadSubCommand("info",
2057 CommandObjectSP(new CommandObjectThreadInfo(interpreter)));
2058 LoadSubCommand("step-in",
2059 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2060 interpreter, "thread step-in",
2061 "Source level single step, stepping into calls. Defaults "
2062 "to current thread unless specified.",
2063 nullptr, eStepTypeInto, eStepScopeSource)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002064
Kate Stoneb9c1b512016-09-06 20:57:50 +00002065 LoadSubCommand("step-out",
2066 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2067 interpreter, "thread step-out",
2068 "Finish executing the current stack frame and stop after "
2069 "returning. Defaults to current thread unless specified.",
2070 nullptr, eStepTypeOut, eStepScopeSource)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002071
Kate Stoneb9c1b512016-09-06 20:57:50 +00002072 LoadSubCommand("step-over",
2073 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2074 interpreter, "thread step-over",
2075 "Source level single step, stepping over calls. Defaults "
2076 "to current thread unless specified.",
2077 nullptr, eStepTypeOver, eStepScopeSource)));
Greg Clayton66111032010-06-23 01:19:29 +00002078
Kate Stoneb9c1b512016-09-06 20:57:50 +00002079 LoadSubCommand("step-inst",
2080 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2081 interpreter, "thread step-inst",
2082 "Instruction level single step, stepping into calls. "
2083 "Defaults to current thread unless specified.",
2084 nullptr, eStepTypeTrace, eStepScopeInstruction)));
Kate Stone7428a182016-07-14 22:03:10 +00002085
Kate Stoneb9c1b512016-09-06 20:57:50 +00002086 LoadSubCommand("step-inst-over",
2087 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2088 interpreter, "thread step-inst-over",
2089 "Instruction level single step, stepping over calls. "
2090 "Defaults to current thread unless specified.",
2091 nullptr, eStepTypeTraceOver, eStepScopeInstruction)));
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002092
Kate Stoneb9c1b512016-09-06 20:57:50 +00002093 LoadSubCommand(
2094 "step-scripted",
2095 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope(
2096 interpreter, "thread step-scripted",
2097 "Step as instructed by the script class passed in the -C option.",
2098 nullptr, eStepTypeScripted, eStepScopeSource)));
Jim Ingham2bdbfd52014-09-29 23:17:18 +00002099
Kate Stoneb9c1b512016-09-06 20:57:50 +00002100 LoadSubCommand("plan", CommandObjectSP(new CommandObjectMultiwordThreadPlan(
2101 interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002102}
2103
Eugene Zelenko50ff9fe2016-02-25 23:46:36 +00002104CommandObjectMultiwordThread::~CommandObjectMultiwordThread() = default;