Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectThread.cpp ---------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "CommandObjectThread.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Core/SourceManager.h" |
Zachary Turner | a78bd7f | 2015-03-03 23:11:11 +0000 | [diff] [blame] | 17 | #include "lldb/Core/ValueObject.h" |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 18 | #include "lldb/Host/Host.h" |
Zachary Turner | 3eb2b44 | 2017-03-22 23:33:16 +0000 | [diff] [blame] | 19 | #include "lldb/Host/OptionParser.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 20 | #include "lldb/Host/StringConvert.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 22 | #include "lldb/Interpreter/CommandReturnObject.h" |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 23 | #include "lldb/Interpreter/OptionArgParser.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 24 | #include "lldb/Interpreter/Options.h" |
| 25 | #include "lldb/Symbol/CompileUnit.h" |
| 26 | #include "lldb/Symbol/Function.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 27 | #include "lldb/Symbol/LineEntry.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | #include "lldb/Symbol/LineTable.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Target/Process.h" |
| 30 | #include "lldb/Target/RegisterContext.h" |
Jason Molenda | 750ea69 | 2013-11-12 07:02:07 +0000 | [diff] [blame] | 31 | #include "lldb/Target/SystemRuntime.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | #include "lldb/Target/Target.h" |
| 33 | #include "lldb/Target/Thread.h" |
| 34 | #include "lldb/Target/ThreadPlan.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 35 | #include "lldb/Target/ThreadPlanStepInRange.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | #include "lldb/Target/ThreadPlanStepInstruction.h" |
| 37 | #include "lldb/Target/ThreadPlanStepOut.h" |
| 38 | #include "lldb/Target/ThreadPlanStepRange.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 39 | #include "lldb/Utility/State.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | #include "lldb/lldb-private.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | using namespace lldb; |
| 43 | using namespace lldb_private; |
| 44 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 45 | //------------------------------------------------------------------------- |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 46 | // CommandObjectIterateOverThreads |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | //------------------------------------------------------------------------- |
| 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | class CommandObjectIterateOverThreads : public CommandObjectParsed { |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 50 | |
| 51 | class UniqueStack { |
| 52 | |
| 53 | public: |
| 54 | UniqueStack(std::stack<lldb::addr_t> stack_frames, uint32_t thread_index_id) |
| 55 | : m_stack_frames(stack_frames) { |
| 56 | m_thread_index_ids.push_back(thread_index_id); |
| 57 | } |
| 58 | |
| 59 | void AddThread(uint32_t thread_index_id) const { |
| 60 | m_thread_index_ids.push_back(thread_index_id); |
| 61 | } |
| 62 | |
| 63 | const std::vector<uint32_t> &GetUniqueThreadIndexIDs() const { |
| 64 | return m_thread_index_ids; |
| 65 | } |
| 66 | |
| 67 | lldb::tid_t GetRepresentativeThread() const { |
| 68 | return m_thread_index_ids.front(); |
| 69 | } |
| 70 | |
| 71 | friend bool inline operator<(const UniqueStack &lhs, |
| 72 | const UniqueStack &rhs) { |
| 73 | return lhs.m_stack_frames < rhs.m_stack_frames; |
| 74 | } |
| 75 | |
| 76 | protected: |
| 77 | // Mark the thread index as mutable, as we don't care about it from a const |
| 78 | // perspective, we only care about m_stack_frames so we keep our std::set |
| 79 | // sorted. |
| 80 | mutable std::vector<uint32_t> m_thread_index_ids; |
| 81 | std::stack<lldb::addr_t> m_stack_frames; |
| 82 | }; |
| 83 | |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 84 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | CommandObjectIterateOverThreads(CommandInterpreter &interpreter, |
| 86 | const char *name, const char *help, |
| 87 | const char *syntax, uint32_t flags) |
| 88 | : CommandObjectParsed(interpreter, name, help, syntax, flags) {} |
| 89 | |
| 90 | ~CommandObjectIterateOverThreads() override = default; |
| 91 | |
| 92 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 93 | result.SetStatus(m_success_return); |
| 94 | |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 95 | bool all_threads = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | if (command.GetArgumentCount() == 0) { |
| 97 | Thread *thread = m_exe_ctx.GetThreadPtr(); |
Adrian McCarthy | 3887ba8 | 2017-09-19 18:07:33 +0000 | [diff] [blame] | 98 | if (!thread || !HandleOneThread(thread->GetID(), result)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | return false; |
| 100 | return result.Succeeded(); |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 101 | } else if (command.GetArgumentCount() == 1) { |
| 102 | all_threads = ::strcmp(command.GetArgumentAtIndex(0), "all") == 0; |
| 103 | m_unique_stacks = ::strcmp(command.GetArgumentAtIndex(0), "unique") == 0; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | // Use tids instead of ThreadSPs to prevent deadlocking problems which |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 107 | // result from JIT-ing code while iterating over the (locked) ThreadSP |
| 108 | // list. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | std::vector<lldb::tid_t> tids; |
Bruce Mitchener | 13d21e9 | 2015-10-07 16:56:17 +0000 | [diff] [blame] | 110 | |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 111 | if (all_threads || m_unique_stacks) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | Process *process = m_exe_ctx.GetProcessPtr(); |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | for (ThreadSP thread_sp : process->Threads()) |
| 115 | tids.push_back(thread_sp->GetID()); |
| 116 | } else { |
| 117 | const size_t num_args = command.GetArgumentCount(); |
| 118 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 119 | |
| 120 | std::lock_guard<std::recursive_mutex> guard( |
| 121 | process->GetThreadList().GetMutex()); |
| 122 | |
| 123 | for (size_t i = 0; i < num_args; i++) { |
| 124 | bool success; |
| 125 | |
| 126 | uint32_t thread_idx = StringConvert::ToUInt32( |
| 127 | command.GetArgumentAtIndex(i), 0, 0, &success); |
| 128 | if (!success) { |
| 129 | result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n", |
| 130 | command.GetArgumentAtIndex(i)); |
| 131 | result.SetStatus(eReturnStatusFailed); |
| 132 | return false; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 133 | } |
Stephane Sezer | f810491 | 2016-03-17 18:52:41 +0000 | [diff] [blame] | 134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | ThreadSP thread = |
| 136 | process->GetThreadList().FindThreadByIndexID(thread_idx); |
Stephane Sezer | f810491 | 2016-03-17 18:52:41 +0000 | [diff] [blame] | 137 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | if (!thread) { |
| 139 | result.AppendErrorWithFormat("no thread with index: \"%s\"\n", |
| 140 | command.GetArgumentAtIndex(i)); |
| 141 | result.SetStatus(eReturnStatusFailed); |
| 142 | return false; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 143 | } |
Stephane Sezer | f810491 | 2016-03-17 18:52:41 +0000 | [diff] [blame] | 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | tids.push_back(thread->GetID()); |
| 146 | } |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 149 | if (m_unique_stacks) { |
| 150 | // Iterate over threads, finding unique stack buckets. |
| 151 | std::set<UniqueStack> unique_stacks; |
| 152 | for (const lldb::tid_t &tid : tids) { |
| 153 | if (!BucketThread(tid, unique_stacks, result)) { |
| 154 | return false; |
| 155 | } |
| 156 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 158 | // Write the thread id's and unique call stacks to the output stream |
| 159 | Stream &strm = result.GetOutputStream(); |
| 160 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 161 | for (const UniqueStack &stack : unique_stacks) { |
| 162 | // List the common thread ID's |
| 163 | const std::vector<uint32_t> &thread_index_ids = |
| 164 | stack.GetUniqueThreadIndexIDs(); |
Pavel Labath | ef7aff5 | 2017-07-05 14:54:46 +0000 | [diff] [blame] | 165 | strm.Format("{0} thread(s) ", thread_index_ids.size()); |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 166 | for (const uint32_t &thread_index_id : thread_index_ids) { |
Pavel Labath | ef7aff5 | 2017-07-05 14:54:46 +0000 | [diff] [blame] | 167 | strm.Format("#{0} ", thread_index_id); |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 168 | } |
| 169 | strm.EOL(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 171 | // List the shared call stack for this set of threads |
| 172 | uint32_t representative_thread_id = stack.GetRepresentativeThread(); |
| 173 | ThreadSP thread = process->GetThreadList().FindThreadByIndexID( |
| 174 | representative_thread_id); |
| 175 | if (!HandleOneThread(thread->GetID(), result)) { |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | } else { |
| 180 | uint32_t idx = 0; |
| 181 | for (const lldb::tid_t &tid : tids) { |
| 182 | if (idx != 0 && m_add_return) |
| 183 | result.AppendMessage(""); |
| 184 | |
| 185 | if (!HandleOneThread(tid, result)) |
| 186 | return false; |
| 187 | |
| 188 | ++idx; |
| 189 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 190 | } |
| 191 | return result.Succeeded(); |
| 192 | } |
| 193 | |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 194 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 195 | // Override this to do whatever you need to do for one thread. |
| 196 | // |
| 197 | // If you return false, the iteration will stop, otherwise it will proceed. |
| 198 | // The result is set to m_success_return (defaults to |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 199 | // eReturnStatusSuccessFinishResult) before the iteration, so you only need |
| 200 | // to set the return status in HandleOneThread if you want to indicate an |
| 201 | // error. If m_add_return is true, a blank line will be inserted between each |
| 202 | // of the listings (except the last one.) |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 203 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 204 | virtual bool HandleOneThread(lldb::tid_t, CommandReturnObject &result) = 0; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 205 | |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 206 | bool BucketThread(lldb::tid_t tid, std::set<UniqueStack> &unique_stacks, |
| 207 | CommandReturnObject &result) { |
| 208 | // Grab the corresponding thread for the given thread id. |
| 209 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 210 | Thread *thread = process->GetThreadList().FindThreadByID(tid).get(); |
| 211 | if (thread == nullptr) { |
Pavel Labath | ef7aff5 | 2017-07-05 14:54:46 +0000 | [diff] [blame] | 212 | result.AppendErrorWithFormatv("Failed to process thread #{0}.\n", tid); |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 213 | result.SetStatus(eReturnStatusFailed); |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | // Collect the each frame's address for this call-stack |
| 218 | std::stack<lldb::addr_t> stack_frames; |
| 219 | const uint32_t frame_count = thread->GetStackFrameCount(); |
| 220 | for (uint32_t frame_index = 0; frame_index < frame_count; frame_index++) { |
| 221 | const lldb::StackFrameSP frame_sp = |
| 222 | thread->GetStackFrameAtIndex(frame_index); |
| 223 | const lldb::addr_t pc = frame_sp->GetStackID().GetPC(); |
| 224 | stack_frames.push(pc); |
| 225 | } |
| 226 | |
| 227 | uint32_t thread_index_id = thread->GetIndexID(); |
| 228 | UniqueStack new_unique_stack(stack_frames, thread_index_id); |
| 229 | |
| 230 | // Try to match the threads stack to and existing entry. |
| 231 | std::set<UniqueStack>::iterator matching_stack = |
| 232 | unique_stacks.find(new_unique_stack); |
| 233 | if (matching_stack != unique_stacks.end()) { |
| 234 | matching_stack->AddThread(thread_index_id); |
| 235 | } else { |
| 236 | unique_stacks.insert(new_unique_stack); |
| 237 | } |
| 238 | return true; |
| 239 | } |
| 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | ReturnStatus m_success_return = eReturnStatusSuccessFinishResult; |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 242 | bool m_unique_stacks = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 243 | bool m_add_return = true; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | //------------------------------------------------------------------------- |
| 247 | // CommandObjectThreadBacktrace |
| 248 | //------------------------------------------------------------------------- |
| 249 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 250 | static constexpr OptionDefinition g_thread_backtrace_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 251 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 252 | { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCount, "How many frames to display (-1 for all)" }, |
| 253 | { LLDB_OPT_SET_1, false, "start", 's', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeFrameIndex, "Frame in which to start the backtrace" }, |
| 254 | { LLDB_OPT_SET_1, false, "extended", 'e', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Show the extended backtrace, if available" } |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 255 | // clang-format on |
| 256 | }; |
| 257 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | class CommandObjectThreadBacktrace : public CommandObjectIterateOverThreads { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 259 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 260 | class CommandOptions : public Options { |
| 261 | public: |
| 262 | CommandOptions() : Options() { |
| 263 | // Keep default values of all options in one place: OptionParsingStarting |
| 264 | // () |
| 265 | OptionParsingStarting(nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 268 | ~CommandOptions() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 269 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 270 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 271 | ExecutionContext *execution_context) override { |
| 272 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | const int short_option = m_getopt_table[option_idx].val; |
| 274 | |
| 275 | switch (short_option) { |
| 276 | case 'c': { |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 277 | int32_t input_count = 0; |
| 278 | if (option_arg.getAsInteger(0, m_count)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 279 | m_count = UINT32_MAX; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 280 | error.SetErrorStringWithFormat( |
| 281 | "invalid integer value for option '%c'", short_option); |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 282 | } else if (input_count < 0) |
| 283 | m_count = UINT32_MAX; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 284 | } break; |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 285 | case 's': |
| 286 | if (option_arg.getAsInteger(0, m_start)) |
| 287 | error.SetErrorStringWithFormat( |
| 288 | "invalid integer value for option '%c'", short_option); |
| 289 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 290 | case 'e': { |
| 291 | bool success; |
| 292 | m_extended_backtrace = |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 293 | OptionArgParser::ToBoolean(option_arg, false, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 294 | if (!success) |
| 295 | error.SetErrorStringWithFormat( |
| 296 | "invalid boolean value for option '%c'", short_option); |
| 297 | } break; |
| 298 | default: |
| 299 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 300 | short_option); |
| 301 | break; |
| 302 | } |
| 303 | return error; |
Jim Ingham | e2e0b45 | 2010-08-26 23:36:03 +0000 | [diff] [blame] | 304 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 305 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 306 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 307 | m_count = UINT32_MAX; |
| 308 | m_start = 0; |
| 309 | m_extended_backtrace = false; |
| 310 | } |
| 311 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 312 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 313 | return llvm::makeArrayRef(g_thread_backtrace_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 314 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 315 | |
| 316 | // Instance variables to hold the values for command options. |
| 317 | uint32_t m_count; |
| 318 | uint32_t m_start; |
| 319 | bool m_extended_backtrace; |
| 320 | }; |
| 321 | |
| 322 | CommandObjectThreadBacktrace(CommandInterpreter &interpreter) |
| 323 | : CommandObjectIterateOverThreads( |
| 324 | interpreter, "thread backtrace", |
| 325 | "Show thread call stacks. Defaults to the current thread, thread " |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 326 | "indexes can be specified as arguments.\n" |
| 327 | "Use the thread-index \"all\" to see all threads.\n" |
| 328 | "Use the thread-index \"unique\" to see threads grouped by unique " |
| 329 | "call stacks.", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 330 | nullptr, |
| 331 | eCommandRequiresProcess | eCommandRequiresThread | |
| 332 | eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | |
| 333 | eCommandProcessMustBePaused), |
| 334 | m_options() {} |
| 335 | |
| 336 | ~CommandObjectThreadBacktrace() override = default; |
| 337 | |
| 338 | Options *GetOptions() override { return &m_options; } |
| 339 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 340 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 341 | void DoExtendedBacktrace(Thread *thread, CommandReturnObject &result) { |
| 342 | SystemRuntime *runtime = thread->GetProcess()->GetSystemRuntime(); |
| 343 | if (runtime) { |
| 344 | Stream &strm = result.GetOutputStream(); |
| 345 | const std::vector<ConstString> &types = |
| 346 | runtime->GetExtendedBacktraceTypes(); |
| 347 | for (auto type : types) { |
| 348 | ThreadSP ext_thread_sp = runtime->GetExtendedBacktraceThread( |
| 349 | thread->shared_from_this(), type); |
| 350 | if (ext_thread_sp && ext_thread_sp->IsValid()) { |
| 351 | const uint32_t num_frames_with_source = 0; |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 352 | const bool stop_format = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 353 | if (ext_thread_sp->GetStatus(strm, m_options.m_start, |
| 354 | m_options.m_count, |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 355 | num_frames_with_source, |
| 356 | stop_format)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | DoExtendedBacktrace(ext_thread_sp.get(), result); |
| 358 | } |
Jason Molenda | 750ea69 | 2013-11-12 07:02:07 +0000 | [diff] [blame] | 359 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { |
| 365 | ThreadSP thread_sp = |
| 366 | m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); |
| 367 | if (!thread_sp) { |
| 368 | result.AppendErrorWithFormat( |
| 369 | "thread disappeared while computing backtraces: 0x%" PRIx64 "\n", |
| 370 | tid); |
| 371 | result.SetStatus(eReturnStatusFailed); |
| 372 | return false; |
Jason Molenda | 750ea69 | 2013-11-12 07:02:07 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 375 | Thread *thread = thread_sp.get(); |
Stephane Sezer | f810491 | 2016-03-17 18:52:41 +0000 | [diff] [blame] | 376 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 377 | Stream &strm = result.GetOutputStream(); |
Stephane Sezer | f810491 | 2016-03-17 18:52:41 +0000 | [diff] [blame] | 378 | |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 379 | // Only dump stack info if we processing unique stacks. |
| 380 | const bool only_stacks = m_unique_stacks; |
| 381 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 382 | // Don't show source context when doing backtraces. |
| 383 | const uint32_t num_frames_with_source = 0; |
Jim Ingham | 4f243e8 | 2016-11-08 23:43:36 +0000 | [diff] [blame] | 384 | const bool stop_format = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 385 | if (!thread->GetStatus(strm, m_options.m_start, m_options.m_count, |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 +0000 | [diff] [blame] | 386 | num_frames_with_source, stop_format, only_stacks)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 387 | result.AppendErrorWithFormat( |
| 388 | "error displaying backtrace for thread: \"0x%4.4x\"\n", |
| 389 | thread->GetIndexID()); |
| 390 | result.SetStatus(eReturnStatusFailed); |
| 391 | return false; |
| 392 | } |
| 393 | if (m_options.m_extended_backtrace) { |
| 394 | DoExtendedBacktrace(thread, result); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 395 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 396 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 397 | return true; |
| 398 | } |
| 399 | |
| 400 | CommandOptions m_options; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 401 | }; |
| 402 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 403 | enum StepScope { eStepScopeSource, eStepScopeInstruction }; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 404 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 405 | static constexpr OptionEnumValueElement g_tri_running_mode[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 406 | {eOnlyThisThread, "this-thread", "Run only this thread"}, |
| 407 | {eAllThreads, "all-threads", "Run all threads"}, |
| 408 | {eOnlyDuringStepping, "while-stepping", |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 409 | "Run only this thread while stepping"} }; |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 410 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 411 | static constexpr OptionEnumValues TriRunningModes() { |
| 412 | return OptionEnumValues(g_tri_running_mode); |
| 413 | } |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 414 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 415 | static constexpr OptionDefinition g_thread_step_scope_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 416 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 417 | { 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." }, |
| 418 | { 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." }, |
| 419 | { 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." }, |
| 420 | { 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." }, |
| 421 | { 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." }, |
| 422 | { 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." }, |
| 423 | { 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." }, |
| 424 | { 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 Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 425 | // clang-format on |
| 426 | }; |
| 427 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 428 | class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 429 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 430 | class CommandOptions : public Options { |
| 431 | public: |
| 432 | CommandOptions() : Options() { |
| 433 | // Keep default values of all options in one place: OptionParsingStarting |
| 434 | // () |
| 435 | OptionParsingStarting(nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 438 | ~CommandOptions() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 439 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 440 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 441 | ExecutionContext *execution_context) override { |
| 442 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 443 | const int short_option = m_getopt_table[option_idx].val; |
| 444 | |
| 445 | switch (short_option) { |
| 446 | case 'a': { |
| 447 | bool success; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 448 | bool avoid_no_debug = |
| 449 | OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 450 | if (!success) |
| 451 | error.SetErrorStringWithFormat( |
| 452 | "invalid boolean value for option '%c'", short_option); |
| 453 | else { |
| 454 | m_step_in_avoid_no_debug = |
| 455 | avoid_no_debug ? eLazyBoolYes : eLazyBoolNo; |
| 456 | } |
| 457 | } break; |
| 458 | |
| 459 | case 'A': { |
| 460 | bool success; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 461 | bool avoid_no_debug = |
| 462 | OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 463 | if (!success) |
| 464 | error.SetErrorStringWithFormat( |
| 465 | "invalid boolean value for option '%c'", short_option); |
| 466 | else { |
| 467 | m_step_out_avoid_no_debug = |
| 468 | avoid_no_debug ? eLazyBoolYes : eLazyBoolNo; |
| 469 | } |
| 470 | } break; |
| 471 | |
| 472 | case 'c': |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 473 | if (option_arg.getAsInteger(0, m_step_count)) |
| 474 | error.SetErrorStringWithFormat("invalid step count '%s'", |
| 475 | option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 476 | break; |
| 477 | |
| 478 | case 'C': |
| 479 | m_class_name.clear(); |
| 480 | m_class_name.assign(option_arg); |
| 481 | break; |
| 482 | |
| 483 | case 'm': { |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 484 | auto enum_values = GetDefinitions()[option_idx].enum_values; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 485 | m_run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum( |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 486 | option_arg, enum_values, eOnlyDuringStepping, error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 487 | } break; |
| 488 | |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 489 | case 'e': |
| 490 | if (option_arg == "block") { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 491 | m_end_line_is_block_end = 1; |
| 492 | break; |
| 493 | } |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 494 | if (option_arg.getAsInteger(0, m_end_line)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 495 | error.SetErrorStringWithFormat("invalid end line number '%s'", |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 496 | option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 497 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 498 | |
| 499 | case 'r': |
| 500 | m_avoid_regexp.clear(); |
| 501 | m_avoid_regexp.assign(option_arg); |
| 502 | break; |
| 503 | |
| 504 | case 't': |
| 505 | m_step_in_target.clear(); |
| 506 | m_step_in_target.assign(option_arg); |
| 507 | break; |
| 508 | |
| 509 | default: |
| 510 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 511 | short_option); |
| 512 | break; |
| 513 | } |
| 514 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 517 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 518 | m_step_in_avoid_no_debug = eLazyBoolCalculate; |
| 519 | m_step_out_avoid_no_debug = eLazyBoolCalculate; |
| 520 | m_run_mode = eOnlyDuringStepping; |
| 521 | |
| 522 | // Check if we are in Non-Stop mode |
| 523 | TargetSP target_sp = |
| 524 | execution_context ? execution_context->GetTargetSP() : TargetSP(); |
| 525 | if (target_sp && target_sp->GetNonStopModeEnabled()) |
| 526 | m_run_mode = eOnlyThisThread; |
| 527 | |
| 528 | m_avoid_regexp.clear(); |
| 529 | m_step_in_target.clear(); |
| 530 | m_class_name.clear(); |
| 531 | m_step_count = 1; |
| 532 | m_end_line = LLDB_INVALID_LINE_NUMBER; |
| 533 | m_end_line_is_block_end = false; |
| 534 | } |
| 535 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 536 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 537 | return llvm::makeArrayRef(g_thread_step_scope_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 538 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 539 | |
| 540 | // Instance variables to hold the values for command options. |
| 541 | LazyBool m_step_in_avoid_no_debug; |
| 542 | LazyBool m_step_out_avoid_no_debug; |
| 543 | RunMode m_run_mode; |
| 544 | std::string m_avoid_regexp; |
| 545 | std::string m_step_in_target; |
| 546 | std::string m_class_name; |
| 547 | uint32_t m_step_count; |
| 548 | uint32_t m_end_line; |
| 549 | bool m_end_line_is_block_end; |
| 550 | }; |
| 551 | |
| 552 | CommandObjectThreadStepWithTypeAndScope(CommandInterpreter &interpreter, |
| 553 | const char *name, const char *help, |
| 554 | const char *syntax, |
| 555 | StepType step_type, |
| 556 | StepScope step_scope) |
| 557 | : CommandObjectParsed(interpreter, name, help, syntax, |
| 558 | eCommandRequiresProcess | eCommandRequiresThread | |
| 559 | eCommandTryTargetAPILock | |
| 560 | eCommandProcessMustBeLaunched | |
| 561 | eCommandProcessMustBePaused), |
| 562 | m_step_type(step_type), m_step_scope(step_scope), m_options() { |
| 563 | CommandArgumentEntry arg; |
| 564 | CommandArgumentData thread_id_arg; |
| 565 | |
| 566 | // Define the first (and only) variant of this arg. |
| 567 | thread_id_arg.arg_type = eArgTypeThreadID; |
| 568 | thread_id_arg.arg_repetition = eArgRepeatOptional; |
| 569 | |
| 570 | // There is only one variant this argument could be; put it into the |
| 571 | // argument entry. |
| 572 | arg.push_back(thread_id_arg); |
| 573 | |
| 574 | // Push the data for the first argument into the m_arguments vector. |
| 575 | m_arguments.push_back(arg); |
| 576 | } |
| 577 | |
| 578 | ~CommandObjectThreadStepWithTypeAndScope() override = default; |
| 579 | |
| 580 | Options *GetOptions() override { return &m_options; } |
| 581 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 582 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 583 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 584 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 585 | bool synchronous_execution = m_interpreter.GetSynchronous(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 586 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 587 | const uint32_t num_threads = process->GetThreadList().GetSize(); |
| 588 | Thread *thread = nullptr; |
Greg Clayton | f9fc609 | 2013-01-09 19:44:40 +0000 | [diff] [blame] | 589 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 590 | if (command.GetArgumentCount() == 0) { |
| 591 | thread = GetDefaultThread(); |
Jim Ingham | 8d94ba0 | 2016-03-12 02:45:34 +0000 | [diff] [blame] | 592 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 593 | if (thread == nullptr) { |
| 594 | result.AppendError("no selected thread in process"); |
| 595 | result.SetStatus(eReturnStatusFailed); |
| 596 | return false; |
| 597 | } |
| 598 | } else { |
| 599 | const char *thread_idx_cstr = command.GetArgumentAtIndex(0); |
| 600 | uint32_t step_thread_idx = |
| 601 | StringConvert::ToUInt32(thread_idx_cstr, LLDB_INVALID_INDEX32); |
| 602 | if (step_thread_idx == LLDB_INVALID_INDEX32) { |
| 603 | result.AppendErrorWithFormat("invalid thread index '%s'.\n", |
| 604 | thread_idx_cstr); |
| 605 | result.SetStatus(eReturnStatusFailed); |
| 606 | return false; |
| 607 | } |
| 608 | thread = |
| 609 | process->GetThreadList().FindThreadByIndexID(step_thread_idx).get(); |
| 610 | if (thread == nullptr) { |
| 611 | result.AppendErrorWithFormat( |
| 612 | "Thread index %u is out of range (valid values are 0 - %u).\n", |
| 613 | step_thread_idx, num_threads); |
| 614 | result.SetStatus(eReturnStatusFailed); |
| 615 | return false; |
| 616 | } |
| 617 | } |
Jim Ingham | 64e7ead | 2012-05-03 21:19:36 +0000 | [diff] [blame] | 618 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 619 | if (m_step_type == eStepTypeScripted) { |
| 620 | if (m_options.m_class_name.empty()) { |
| 621 | result.AppendErrorWithFormat("empty class name for scripted step."); |
| 622 | result.SetStatus(eReturnStatusFailed); |
| 623 | return false; |
| 624 | } else if (!m_interpreter.GetScriptInterpreter()->CheckObjectExists( |
| 625 | m_options.m_class_name.c_str())) { |
| 626 | result.AppendErrorWithFormat( |
| 627 | "class for scripted step: \"%s\" does not exist.", |
| 628 | m_options.m_class_name.c_str()); |
| 629 | result.SetStatus(eReturnStatusFailed); |
| 630 | return false; |
| 631 | } |
| 632 | } |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 633 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 634 | if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER && |
| 635 | m_step_type != eStepTypeInto) { |
| 636 | result.AppendErrorWithFormat( |
| 637 | "end line option is only valid for step into"); |
| 638 | result.SetStatus(eReturnStatusFailed); |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | const bool abort_other_plans = false; |
| 643 | const lldb::RunMode stop_other_threads = m_options.m_run_mode; |
| 644 | |
| 645 | // This is a bit unfortunate, but not all the commands in this command |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 646 | // object support only while stepping, so I use the bool for them. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 647 | bool bool_stop_other_threads; |
| 648 | if (m_options.m_run_mode == eAllThreads) |
| 649 | bool_stop_other_threads = false; |
| 650 | else if (m_options.m_run_mode == eOnlyDuringStepping) |
| 651 | bool_stop_other_threads = |
| 652 | (m_step_type != eStepTypeOut && m_step_type != eStepTypeScripted); |
| 653 | else |
| 654 | bool_stop_other_threads = true; |
| 655 | |
| 656 | ThreadPlanSP new_plan_sp; |
| 657 | |
| 658 | if (m_step_type == eStepTypeInto) { |
| 659 | StackFrame *frame = thread->GetStackFrameAtIndex(0).get(); |
| 660 | assert(frame != nullptr); |
| 661 | |
| 662 | if (frame->HasDebugInformation()) { |
| 663 | AddressRange range; |
| 664 | SymbolContext sc = frame->GetSymbolContext(eSymbolContextEverything); |
| 665 | if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 666 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 667 | if (!sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range, |
| 668 | error)) { |
| 669 | result.AppendErrorWithFormat("invalid end-line option: %s.", |
| 670 | error.AsCString()); |
Jim Ingham | c17d6bd | 2016-02-10 03:25:24 +0000 | [diff] [blame] | 671 | result.SetStatus(eReturnStatusFailed); |
| 672 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 673 | } |
| 674 | } else if (m_options.m_end_line_is_block_end) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 675 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 676 | Block *block = frame->GetSymbolContext(eSymbolContextBlock).block; |
| 677 | if (!block) { |
| 678 | result.AppendErrorWithFormat("Could not find the current block."); |
| 679 | result.SetStatus(eReturnStatusFailed); |
Greg Clayton | f9fc609 | 2013-01-09 19:44:40 +0000 | [diff] [blame] | 680 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | AddressRange block_range; |
| 684 | Address pc_address = frame->GetFrameCodeAddress(); |
| 685 | block->GetRangeContainingAddress(pc_address, block_range); |
| 686 | if (!block_range.GetBaseAddress().IsValid()) { |
| 687 | result.AppendErrorWithFormat( |
| 688 | "Could not find the current block address."); |
| 689 | result.SetStatus(eReturnStatusFailed); |
| 690 | return false; |
| 691 | } |
| 692 | lldb::addr_t pc_offset_in_block = |
| 693 | pc_address.GetFileAddress() - |
| 694 | block_range.GetBaseAddress().GetFileAddress(); |
| 695 | lldb::addr_t range_length = |
| 696 | block_range.GetByteSize() - pc_offset_in_block; |
| 697 | range = AddressRange(pc_address, range_length); |
| 698 | } else { |
| 699 | range = sc.line_entry.range; |
Greg Clayton | f9fc609 | 2013-01-09 19:44:40 +0000 | [diff] [blame] | 700 | } |
Greg Clayton | f9fc609 | 2013-01-09 19:44:40 +0000 | [diff] [blame] | 701 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 702 | new_plan_sp = thread->QueueThreadPlanForStepInRange( |
| 703 | abort_other_plans, range, |
| 704 | frame->GetSymbolContext(eSymbolContextEverything), |
| 705 | m_options.m_step_in_target.c_str(), stop_other_threads, |
| 706 | m_options.m_step_in_avoid_no_debug, |
| 707 | m_options.m_step_out_avoid_no_debug); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 708 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 709 | if (new_plan_sp && !m_options.m_avoid_regexp.empty()) { |
| 710 | ThreadPlanStepInRange *step_in_range_plan = |
| 711 | static_cast<ThreadPlanStepInRange *>(new_plan_sp.get()); |
| 712 | step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str()); |
Greg Clayton | f9fc609 | 2013-01-09 19:44:40 +0000 | [diff] [blame] | 713 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 714 | } else |
| 715 | new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( |
| 716 | false, abort_other_plans, bool_stop_other_threads); |
| 717 | } else if (m_step_type == eStepTypeOver) { |
| 718 | StackFrame *frame = thread->GetStackFrameAtIndex(0).get(); |
| 719 | |
| 720 | if (frame->HasDebugInformation()) |
| 721 | new_plan_sp = thread->QueueThreadPlanForStepOverRange( |
| 722 | abort_other_plans, |
| 723 | frame->GetSymbolContext(eSymbolContextEverything).line_entry, |
| 724 | frame->GetSymbolContext(eSymbolContextEverything), |
| 725 | stop_other_threads, m_options.m_step_out_avoid_no_debug); |
| 726 | else |
| 727 | new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( |
| 728 | true, abort_other_plans, bool_stop_other_threads); |
| 729 | } else if (m_step_type == eStepTypeTrace) { |
| 730 | new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( |
| 731 | false, abort_other_plans, bool_stop_other_threads); |
| 732 | } else if (m_step_type == eStepTypeTraceOver) { |
| 733 | new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( |
| 734 | true, abort_other_plans, bool_stop_other_threads); |
| 735 | } else if (m_step_type == eStepTypeOut) { |
| 736 | new_plan_sp = thread->QueueThreadPlanForStepOut( |
| 737 | abort_other_plans, nullptr, false, bool_stop_other_threads, eVoteYes, |
| 738 | eVoteNoOpinion, thread->GetSelectedFrameIndex(), |
| 739 | m_options.m_step_out_avoid_no_debug); |
| 740 | } else if (m_step_type == eStepTypeScripted) { |
| 741 | new_plan_sp = thread->QueueThreadPlanForStepScripted( |
| 742 | abort_other_plans, m_options.m_class_name.c_str(), |
| 743 | bool_stop_other_threads); |
| 744 | } else { |
| 745 | result.AppendError("step type is not supported"); |
| 746 | result.SetStatus(eReturnStatusFailed); |
| 747 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 750 | // If we got a new plan, then set it to be a master plan (User level Plans |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 751 | // should be master plans so that they can be interruptible). Then resume |
| 752 | // the process. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 753 | |
| 754 | if (new_plan_sp) { |
| 755 | new_plan_sp->SetIsMasterPlan(true); |
| 756 | new_plan_sp->SetOkayToDiscard(false); |
| 757 | |
| 758 | if (m_options.m_step_count > 1) { |
Jim Ingham | d2a7e85 | 2017-05-25 02:24:18 +0000 | [diff] [blame] | 759 | if (!new_plan_sp->SetIterationCount(m_options.m_step_count)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 760 | result.AppendWarning( |
| 761 | "step operation does not support iteration count."); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | process->GetThreadList().SetSelectedThreadByID(thread->GetID()); |
| 766 | |
| 767 | const uint32_t iohandler_id = process->GetIOHandlerID(); |
| 768 | |
| 769 | StreamString stream; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 770 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 771 | if (synchronous_execution) |
| 772 | error = process->ResumeSynchronous(&stream); |
| 773 | else |
| 774 | error = process->Resume(); |
| 775 | |
Adrian McCarthy | 3887ba8 | 2017-09-19 18:07:33 +0000 | [diff] [blame] | 776 | if (!error.Success()) { |
| 777 | result.AppendMessage(error.AsCString()); |
| 778 | result.SetStatus(eReturnStatusFailed); |
| 779 | return false; |
| 780 | } |
| 781 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 782 | // There is a race condition where this thread will return up the call |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 783 | // stack to the main command handler and show an (lldb) prompt before |
| 784 | // HandlePrivateEvent (from PrivateStateThread) has a chance to call |
| 785 | // PushProcessIOHandler(). |
Pavel Labath | 3879fe0 | 2018-05-09 14:29:30 +0000 | [diff] [blame] | 786 | process->SyncIOHandler(iohandler_id, std::chrono::seconds(2)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 787 | |
| 788 | if (synchronous_execution) { |
| 789 | // If any state changed events had anything to say, add that to the |
| 790 | // result |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 791 | if (stream.GetSize() > 0) |
| 792 | result.AppendMessage(stream.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 793 | |
| 794 | process->GetThreadList().SetSelectedThreadByID(thread->GetID()); |
| 795 | result.SetDidChangeProcessState(true); |
| 796 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 797 | } else { |
| 798 | result.SetStatus(eReturnStatusSuccessContinuingNoResult); |
| 799 | } |
| 800 | } else { |
| 801 | result.AppendError("Couldn't find thread plan to implement step type."); |
| 802 | result.SetStatus(eReturnStatusFailed); |
| 803 | } |
| 804 | return result.Succeeded(); |
| 805 | } |
| 806 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 807 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 808 | StepType m_step_type; |
| 809 | StepScope m_step_scope; |
| 810 | CommandOptions m_options; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 811 | }; |
| 812 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 813 | //------------------------------------------------------------------------- |
| 814 | // CommandObjectThreadContinue |
| 815 | //------------------------------------------------------------------------- |
| 816 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 817 | class CommandObjectThreadContinue : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 818 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 819 | CommandObjectThreadContinue(CommandInterpreter &interpreter) |
| 820 | : CommandObjectParsed( |
| 821 | interpreter, "thread continue", |
| 822 | "Continue execution of the current target process. One " |
| 823 | "or more threads may be specified, by default all " |
| 824 | "threads continue.", |
| 825 | nullptr, |
| 826 | eCommandRequiresThread | eCommandTryTargetAPILock | |
| 827 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) { |
| 828 | CommandArgumentEntry arg; |
| 829 | CommandArgumentData thread_idx_arg; |
| 830 | |
| 831 | // Define the first (and only) variant of this arg. |
| 832 | thread_idx_arg.arg_type = eArgTypeThreadIndex; |
| 833 | thread_idx_arg.arg_repetition = eArgRepeatPlus; |
| 834 | |
| 835 | // There is only one variant this argument could be; put it into the |
| 836 | // argument entry. |
| 837 | arg.push_back(thread_idx_arg); |
| 838 | |
| 839 | // Push the data for the first argument into the m_arguments vector. |
| 840 | m_arguments.push_back(arg); |
| 841 | } |
| 842 | |
| 843 | ~CommandObjectThreadContinue() override = default; |
| 844 | |
| 845 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 846 | bool synchronous_execution = m_interpreter.GetSynchronous(); |
| 847 | |
| 848 | if (!m_interpreter.GetDebugger().GetSelectedTarget()) { |
| 849 | result.AppendError("invalid target, create a debug target using the " |
| 850 | "'target create' command"); |
| 851 | result.SetStatus(eReturnStatusFailed); |
| 852 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 855 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 856 | if (process == nullptr) { |
| 857 | result.AppendError("no process exists. Cannot continue"); |
| 858 | result.SetStatus(eReturnStatusFailed); |
| 859 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 860 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 861 | |
| 862 | StateType state = process->GetState(); |
| 863 | if ((state == eStateCrashed) || (state == eStateStopped) || |
| 864 | (state == eStateSuspended)) { |
| 865 | const size_t argc = command.GetArgumentCount(); |
| 866 | if (argc > 0) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 867 | // These two lines appear at the beginning of both blocks in this |
| 868 | // if..else, but that is because we need to release the lock before |
| 869 | // calling process->Resume below. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 870 | std::lock_guard<std::recursive_mutex> guard( |
| 871 | process->GetThreadList().GetMutex()); |
| 872 | const uint32_t num_threads = process->GetThreadList().GetSize(); |
| 873 | std::vector<Thread *> resume_threads; |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 874 | for (auto &entry : command.entries()) { |
| 875 | uint32_t thread_idx; |
| 876 | if (entry.ref.getAsInteger(0, thread_idx)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 877 | result.AppendErrorWithFormat( |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 878 | "invalid thread index argument: \"%s\".\n", entry.c_str()); |
| 879 | result.SetStatus(eReturnStatusFailed); |
| 880 | return false; |
| 881 | } |
| 882 | Thread *thread = |
| 883 | process->GetThreadList().FindThreadByIndexID(thread_idx).get(); |
| 884 | |
| 885 | if (thread) { |
| 886 | resume_threads.push_back(thread); |
| 887 | } else { |
| 888 | result.AppendErrorWithFormat("invalid thread index %u.\n", |
| 889 | thread_idx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 890 | result.SetStatus(eReturnStatusFailed); |
| 891 | return false; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | if (resume_threads.empty()) { |
| 896 | result.AppendError("no valid thread indexes were specified"); |
| 897 | result.SetStatus(eReturnStatusFailed); |
| 898 | return false; |
| 899 | } else { |
| 900 | if (resume_threads.size() == 1) |
| 901 | result.AppendMessageWithFormat("Resuming thread: "); |
| 902 | else |
| 903 | result.AppendMessageWithFormat("Resuming threads: "); |
| 904 | |
| 905 | for (uint32_t idx = 0; idx < num_threads; ++idx) { |
| 906 | Thread *thread = |
| 907 | process->GetThreadList().GetThreadAtIndex(idx).get(); |
| 908 | std::vector<Thread *>::iterator this_thread_pos = |
| 909 | find(resume_threads.begin(), resume_threads.end(), thread); |
| 910 | |
| 911 | if (this_thread_pos != resume_threads.end()) { |
| 912 | resume_threads.erase(this_thread_pos); |
| 913 | if (!resume_threads.empty()) |
| 914 | result.AppendMessageWithFormat("%u, ", thread->GetIndexID()); |
| 915 | else |
| 916 | result.AppendMessageWithFormat("%u ", thread->GetIndexID()); |
| 917 | |
| 918 | const bool override_suspend = true; |
| 919 | thread->SetResumeState(eStateRunning, override_suspend); |
| 920 | } else { |
| 921 | thread->SetResumeState(eStateSuspended); |
| 922 | } |
| 923 | } |
| 924 | result.AppendMessageWithFormat("in process %" PRIu64 "\n", |
| 925 | process->GetID()); |
| 926 | } |
| 927 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 928 | // These two lines appear at the beginning of both blocks in this |
| 929 | // if..else, but that is because we need to release the lock before |
| 930 | // calling process->Resume below. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 931 | std::lock_guard<std::recursive_mutex> guard( |
| 932 | process->GetThreadList().GetMutex()); |
| 933 | const uint32_t num_threads = process->GetThreadList().GetSize(); |
| 934 | Thread *current_thread = GetDefaultThread(); |
| 935 | if (current_thread == nullptr) { |
| 936 | result.AppendError("the process doesn't have a current thread"); |
| 937 | result.SetStatus(eReturnStatusFailed); |
| 938 | return false; |
| 939 | } |
| 940 | // Set the actions that the threads should each take when resuming |
| 941 | for (uint32_t idx = 0; idx < num_threads; ++idx) { |
| 942 | Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get(); |
| 943 | if (thread == current_thread) { |
| 944 | result.AppendMessageWithFormat("Resuming thread 0x%4.4" PRIx64 |
| 945 | " in process %" PRIu64 "\n", |
| 946 | thread->GetID(), process->GetID()); |
| 947 | const bool override_suspend = true; |
| 948 | thread->SetResumeState(eStateRunning, override_suspend); |
| 949 | } else { |
| 950 | thread->SetResumeState(eStateSuspended); |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | StreamString stream; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 956 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 957 | if (synchronous_execution) |
| 958 | error = process->ResumeSynchronous(&stream); |
| 959 | else |
| 960 | error = process->Resume(); |
| 961 | |
| 962 | // We should not be holding the thread list lock when we do this. |
| 963 | if (error.Success()) { |
| 964 | result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n", |
| 965 | process->GetID()); |
| 966 | if (synchronous_execution) { |
| 967 | // If any state changed events had anything to say, add that to the |
| 968 | // result |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 969 | if (stream.GetSize() > 0) |
| 970 | result.AppendMessage(stream.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 971 | |
| 972 | result.SetDidChangeProcessState(true); |
| 973 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 974 | } else { |
| 975 | result.SetStatus(eReturnStatusSuccessContinuingNoResult); |
| 976 | } |
| 977 | } else { |
| 978 | result.AppendErrorWithFormat("Failed to resume process: %s\n", |
| 979 | error.AsCString()); |
| 980 | result.SetStatus(eReturnStatusFailed); |
| 981 | } |
| 982 | } else { |
| 983 | result.AppendErrorWithFormat( |
| 984 | "Process cannot be continued from its current state (%s).\n", |
| 985 | StateAsCString(state)); |
| 986 | result.SetStatus(eReturnStatusFailed); |
| 987 | } |
| 988 | |
| 989 | return result.Succeeded(); |
| 990 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 991 | }; |
| 992 | |
| 993 | //------------------------------------------------------------------------- |
| 994 | // CommandObjectThreadUntil |
| 995 | //------------------------------------------------------------------------- |
| 996 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 997 | static constexpr OptionEnumValueElement g_duo_running_mode[] = { |
| 998 | {eOnlyThisThread, "this-thread", "Run only this thread"}, |
| 999 | {eAllThreads, "all-threads", "Run all threads"} }; |
| 1000 | |
| 1001 | static constexpr OptionEnumValues DuoRunningModes() { |
| 1002 | return OptionEnumValues(g_duo_running_mode); |
| 1003 | } |
| 1004 | |
| 1005 | static constexpr OptionDefinition g_thread_until_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1006 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1007 | { LLDB_OPT_SET_1, false, "frame", 'f', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeFrameIndex, "Frame index for until operation - defaults to 0" }, |
| 1008 | { LLDB_OPT_SET_1, false, "thread", 't', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeThreadIndex, "Thread index for the thread for until operation" }, |
| 1009 | { LLDB_OPT_SET_1, false, "run-mode",'m', OptionParser::eRequiredArgument, nullptr, DuoRunningModes(), 0, eArgTypeRunMode, "Determine how to run other threads while stepping this one" }, |
| 1010 | { 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 Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1011 | // clang-format on |
| 1012 | }; |
| 1013 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1014 | class CommandObjectThreadUntil : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1015 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1016 | class CommandOptions : public Options { |
| 1017 | public: |
| 1018 | uint32_t m_thread_idx; |
| 1019 | uint32_t m_frame_idx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1020 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1021 | CommandOptions() |
| 1022 | : Options(), m_thread_idx(LLDB_INVALID_THREAD_ID), |
| 1023 | m_frame_idx(LLDB_INVALID_FRAME_ID) { |
| 1024 | // Keep default values of all options in one place: OptionParsingStarting |
| 1025 | // () |
| 1026 | OptionParsingStarting(nullptr); |
| 1027 | } |
| 1028 | |
| 1029 | ~CommandOptions() override = default; |
| 1030 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1031 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 1032 | ExecutionContext *execution_context) override { |
| 1033 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1034 | const int short_option = m_getopt_table[option_idx].val; |
| 1035 | |
| 1036 | switch (short_option) { |
| 1037 | case 'a': { |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 1038 | lldb::addr_t tmp_addr = OptionArgParser::ToAddress( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1039 | execution_context, option_arg, LLDB_INVALID_ADDRESS, &error); |
| 1040 | if (error.Success()) |
| 1041 | m_until_addrs.push_back(tmp_addr); |
| 1042 | } break; |
| 1043 | case 't': |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 1044 | if (option_arg.getAsInteger(0, m_thread_idx)) { |
| 1045 | m_thread_idx = LLDB_INVALID_INDEX32; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1046 | error.SetErrorStringWithFormat("invalid thread index '%s'", |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 1047 | option_arg.str().c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1048 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1049 | break; |
| 1050 | case 'f': |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 1051 | if (option_arg.getAsInteger(0, m_frame_idx)) { |
| 1052 | m_frame_idx = LLDB_INVALID_FRAME_ID; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1053 | error.SetErrorStringWithFormat("invalid frame index '%s'", |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 1054 | option_arg.str().c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1055 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1056 | break; |
| 1057 | case 'm': { |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1058 | auto enum_values = GetDefinitions()[option_idx].enum_values; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 1059 | lldb::RunMode run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum( |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 1060 | option_arg, enum_values, eOnlyDuringStepping, error); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1061 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1062 | if (error.Success()) { |
| 1063 | if (run_mode == eAllThreads) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1064 | m_stop_others = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1065 | else |
| 1066 | m_stop_others = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1067 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1068 | } break; |
| 1069 | default: |
| 1070 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 1071 | short_option); |
| 1072 | break; |
| 1073 | } |
| 1074 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1077 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 1078 | m_thread_idx = LLDB_INVALID_THREAD_ID; |
| 1079 | m_frame_idx = 0; |
| 1080 | m_stop_others = false; |
| 1081 | m_until_addrs.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1084 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 1085 | return llvm::makeArrayRef(g_thread_until_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1086 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1087 | |
| 1088 | uint32_t m_step_thread_idx; |
| 1089 | bool m_stop_others; |
| 1090 | std::vector<lldb::addr_t> m_until_addrs; |
| 1091 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1092 | // Instance variables to hold the values for command options. |
| 1093 | }; |
| 1094 | |
| 1095 | CommandObjectThreadUntil(CommandInterpreter &interpreter) |
| 1096 | : CommandObjectParsed( |
| 1097 | interpreter, "thread until", |
| 1098 | "Continue until a line number or address is reached by the " |
| 1099 | "current or specified thread. Stops when returning from " |
Jim Ingham | 9ac8260 | 2016-11-18 22:06:10 +0000 | [diff] [blame] | 1100 | "the current function as a safety measure. " |
| 1101 | "The target line number(s) are given as arguments, and if more than one" |
Adrian Kuegel | ecd760c | 2016-11-24 10:01:34 +0000 | [diff] [blame] | 1102 | " is provided, stepping will stop when the first one is hit.", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1103 | nullptr, |
| 1104 | eCommandRequiresThread | eCommandTryTargetAPILock | |
| 1105 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), |
| 1106 | m_options() { |
| 1107 | CommandArgumentEntry arg; |
| 1108 | CommandArgumentData line_num_arg; |
| 1109 | |
| 1110 | // Define the first (and only) variant of this arg. |
| 1111 | line_num_arg.arg_type = eArgTypeLineNum; |
| 1112 | line_num_arg.arg_repetition = eArgRepeatPlain; |
| 1113 | |
| 1114 | // There is only one variant this argument could be; put it into the |
| 1115 | // argument entry. |
| 1116 | arg.push_back(line_num_arg); |
| 1117 | |
| 1118 | // Push the data for the first argument into the m_arguments vector. |
| 1119 | m_arguments.push_back(arg); |
| 1120 | } |
| 1121 | |
| 1122 | ~CommandObjectThreadUntil() override = default; |
| 1123 | |
| 1124 | Options *GetOptions() override { return &m_options; } |
| 1125 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1126 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1127 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1128 | bool synchronous_execution = m_interpreter.GetSynchronous(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1129 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1130 | Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); |
| 1131 | if (target == nullptr) { |
| 1132 | result.AppendError("invalid target, create a debug target using the " |
| 1133 | "'target create' command"); |
| 1134 | result.SetStatus(eReturnStatusFailed); |
| 1135 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1136 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1137 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1138 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 1139 | if (process == nullptr) { |
| 1140 | result.AppendError("need a valid process to step"); |
| 1141 | result.SetStatus(eReturnStatusFailed); |
| 1142 | } else { |
| 1143 | Thread *thread = nullptr; |
| 1144 | std::vector<uint32_t> line_numbers; |
| 1145 | |
| 1146 | if (command.GetArgumentCount() >= 1) { |
| 1147 | size_t num_args = command.GetArgumentCount(); |
| 1148 | for (size_t i = 0; i < num_args; i++) { |
| 1149 | uint32_t line_number; |
Jim Ingham | 9ac8260 | 2016-11-18 22:06:10 +0000 | [diff] [blame] | 1150 | line_number = StringConvert::ToUInt32(command.GetArgumentAtIndex(i), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1151 | UINT32_MAX); |
| 1152 | if (line_number == UINT32_MAX) { |
| 1153 | result.AppendErrorWithFormat("invalid line number: '%s'.\n", |
Jim Ingham | 9ac8260 | 2016-11-18 22:06:10 +0000 | [diff] [blame] | 1154 | command.GetArgumentAtIndex(i)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1155 | result.SetStatus(eReturnStatusFailed); |
| 1156 | return false; |
| 1157 | } else |
| 1158 | line_numbers.push_back(line_number); |
| 1159 | } |
| 1160 | } else if (m_options.m_until_addrs.empty()) { |
| 1161 | result.AppendErrorWithFormat("No line number or address provided:\n%s", |
Zachary Turner | 1e8016b | 2016-11-15 00:45:18 +0000 | [diff] [blame] | 1162 | GetSyntax().str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1163 | result.SetStatus(eReturnStatusFailed); |
| 1164 | return false; |
| 1165 | } |
| 1166 | |
| 1167 | if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID) { |
| 1168 | thread = GetDefaultThread(); |
| 1169 | } else { |
| 1170 | thread = process->GetThreadList() |
| 1171 | .FindThreadByIndexID(m_options.m_thread_idx) |
| 1172 | .get(); |
| 1173 | } |
| 1174 | |
| 1175 | if (thread == nullptr) { |
| 1176 | const uint32_t num_threads = process->GetThreadList().GetSize(); |
| 1177 | result.AppendErrorWithFormat( |
| 1178 | "Thread index %u is out of range (valid values are 0 - %u).\n", |
| 1179 | m_options.m_thread_idx, num_threads); |
| 1180 | result.SetStatus(eReturnStatusFailed); |
| 1181 | return false; |
| 1182 | } |
| 1183 | |
| 1184 | const bool abort_other_plans = false; |
| 1185 | |
| 1186 | StackFrame *frame = |
| 1187 | thread->GetStackFrameAtIndex(m_options.m_frame_idx).get(); |
| 1188 | if (frame == nullptr) { |
| 1189 | result.AppendErrorWithFormat( |
| 1190 | "Frame index %u is out of range for thread %u.\n", |
| 1191 | m_options.m_frame_idx, m_options.m_thread_idx); |
| 1192 | result.SetStatus(eReturnStatusFailed); |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
| 1196 | ThreadPlanSP new_plan_sp; |
| 1197 | |
| 1198 | if (frame->HasDebugInformation()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1199 | // Finally we got here... Translate the given line number to a bunch |
| 1200 | // of addresses: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1201 | SymbolContext sc(frame->GetSymbolContext(eSymbolContextCompUnit)); |
| 1202 | LineTable *line_table = nullptr; |
| 1203 | if (sc.comp_unit) |
| 1204 | line_table = sc.comp_unit->GetLineTable(); |
| 1205 | |
| 1206 | if (line_table == nullptr) { |
| 1207 | result.AppendErrorWithFormat("Failed to resolve the line table for " |
| 1208 | "frame %u of thread index %u.\n", |
| 1209 | m_options.m_frame_idx, |
| 1210 | m_options.m_thread_idx); |
| 1211 | result.SetStatus(eReturnStatusFailed); |
| 1212 | return false; |
| 1213 | } |
| 1214 | |
| 1215 | LineEntry function_start; |
| 1216 | uint32_t index_ptr = 0, end_ptr; |
| 1217 | std::vector<addr_t> address_list; |
| 1218 | |
| 1219 | // Find the beginning & end index of the |
| 1220 | AddressRange fun_addr_range = sc.function->GetAddressRange(); |
| 1221 | Address fun_start_addr = fun_addr_range.GetBaseAddress(); |
| 1222 | line_table->FindLineEntryByAddress(fun_start_addr, function_start, |
| 1223 | &index_ptr); |
| 1224 | |
| 1225 | Address fun_end_addr(fun_start_addr.GetSection(), |
| 1226 | fun_start_addr.GetOffset() + |
| 1227 | fun_addr_range.GetByteSize()); |
| 1228 | |
| 1229 | bool all_in_function = true; |
| 1230 | |
| 1231 | line_table->FindLineEntryByAddress(fun_end_addr, function_start, |
| 1232 | &end_ptr); |
| 1233 | |
| 1234 | for (uint32_t line_number : line_numbers) { |
| 1235 | uint32_t start_idx_ptr = index_ptr; |
| 1236 | while (start_idx_ptr <= end_ptr) { |
| 1237 | LineEntry line_entry; |
| 1238 | const bool exact = false; |
| 1239 | start_idx_ptr = sc.comp_unit->FindLineEntry( |
| 1240 | start_idx_ptr, line_number, sc.comp_unit, exact, &line_entry); |
| 1241 | if (start_idx_ptr == UINT32_MAX) |
| 1242 | break; |
| 1243 | |
| 1244 | addr_t address = |
| 1245 | line_entry.range.GetBaseAddress().GetLoadAddress(target); |
| 1246 | if (address != LLDB_INVALID_ADDRESS) { |
| 1247 | if (fun_addr_range.ContainsLoadAddress(address, target)) |
| 1248 | address_list.push_back(address); |
| 1249 | else |
| 1250 | all_in_function = false; |
| 1251 | } |
| 1252 | start_idx_ptr++; |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | for (lldb::addr_t address : m_options.m_until_addrs) { |
| 1257 | if (fun_addr_range.ContainsLoadAddress(address, target)) |
| 1258 | address_list.push_back(address); |
| 1259 | else |
| 1260 | all_in_function = false; |
| 1261 | } |
| 1262 | |
| 1263 | if (address_list.empty()) { |
| 1264 | if (all_in_function) |
| 1265 | result.AppendErrorWithFormat( |
| 1266 | "No line entries matching until target.\n"); |
| 1267 | else |
| 1268 | result.AppendErrorWithFormat( |
| 1269 | "Until target outside of the current function.\n"); |
| 1270 | |
| 1271 | result.SetStatus(eReturnStatusFailed); |
| 1272 | return false; |
| 1273 | } |
| 1274 | |
| 1275 | new_plan_sp = thread->QueueThreadPlanForStepUntil( |
| 1276 | abort_other_plans, &address_list.front(), address_list.size(), |
| 1277 | m_options.m_stop_others, m_options.m_frame_idx); |
| 1278 | // User level plans should be master plans so they can be interrupted |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1279 | // (e.g. by hitting a breakpoint) and other plans executed by the user |
| 1280 | // (stepping around the breakpoint) and then a "continue" will resume |
| 1281 | // the original plan. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1282 | new_plan_sp->SetIsMasterPlan(true); |
| 1283 | new_plan_sp->SetOkayToDiscard(false); |
| 1284 | } else { |
| 1285 | result.AppendErrorWithFormat( |
| 1286 | "Frame index %u of thread %u has no debug information.\n", |
| 1287 | m_options.m_frame_idx, m_options.m_thread_idx); |
| 1288 | result.SetStatus(eReturnStatusFailed); |
| 1289 | return false; |
| 1290 | } |
| 1291 | |
| 1292 | process->GetThreadList().SetSelectedThreadByID(m_options.m_thread_idx); |
| 1293 | |
| 1294 | StreamString stream; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1295 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1296 | if (synchronous_execution) |
| 1297 | error = process->ResumeSynchronous(&stream); |
| 1298 | else |
| 1299 | error = process->Resume(); |
| 1300 | |
| 1301 | if (error.Success()) { |
| 1302 | result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n", |
| 1303 | process->GetID()); |
| 1304 | if (synchronous_execution) { |
| 1305 | // If any state changed events had anything to say, add that to the |
| 1306 | // result |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 1307 | if (stream.GetSize() > 0) |
| 1308 | result.AppendMessage(stream.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1309 | |
| 1310 | result.SetDidChangeProcessState(true); |
| 1311 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1312 | } else { |
| 1313 | result.SetStatus(eReturnStatusSuccessContinuingNoResult); |
| 1314 | } |
| 1315 | } else { |
| 1316 | result.AppendErrorWithFormat("Failed to resume process: %s.\n", |
| 1317 | error.AsCString()); |
| 1318 | result.SetStatus(eReturnStatusFailed); |
| 1319 | } |
| 1320 | } |
| 1321 | return result.Succeeded(); |
| 1322 | } |
| 1323 | |
| 1324 | CommandOptions m_options; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1325 | }; |
| 1326 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1327 | //------------------------------------------------------------------------- |
| 1328 | // CommandObjectThreadSelect |
| 1329 | //------------------------------------------------------------------------- |
| 1330 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1331 | class CommandObjectThreadSelect : public CommandObjectParsed { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1332 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1333 | CommandObjectThreadSelect(CommandInterpreter &interpreter) |
| 1334 | : CommandObjectParsed(interpreter, "thread select", |
| 1335 | "Change the currently selected thread.", nullptr, |
| 1336 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
| 1337 | eCommandProcessMustBeLaunched | |
| 1338 | eCommandProcessMustBePaused) { |
| 1339 | CommandArgumentEntry arg; |
| 1340 | CommandArgumentData thread_idx_arg; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1341 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1342 | // Define the first (and only) variant of this arg. |
| 1343 | thread_idx_arg.arg_type = eArgTypeThreadIndex; |
| 1344 | thread_idx_arg.arg_repetition = eArgRepeatPlain; |
| 1345 | |
| 1346 | // There is only one variant this argument could be; put it into the |
| 1347 | // argument entry. |
| 1348 | arg.push_back(thread_idx_arg); |
| 1349 | |
| 1350 | // Push the data for the first argument into the m_arguments vector. |
| 1351 | m_arguments.push_back(arg); |
| 1352 | } |
| 1353 | |
| 1354 | ~CommandObjectThreadSelect() override = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1355 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1356 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1357 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1358 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 1359 | if (process == nullptr) { |
| 1360 | result.AppendError("no process"); |
| 1361 | result.SetStatus(eReturnStatusFailed); |
| 1362 | return false; |
| 1363 | } else if (command.GetArgumentCount() != 1) { |
| 1364 | result.AppendErrorWithFormat( |
| 1365 | "'%s' takes exactly one thread index argument:\nUsage: %s\n", |
| 1366 | m_cmd_name.c_str(), m_cmd_syntax.c_str()); |
| 1367 | result.SetStatus(eReturnStatusFailed); |
| 1368 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1369 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1370 | |
| 1371 | uint32_t index_id = |
| 1372 | StringConvert::ToUInt32(command.GetArgumentAtIndex(0), 0, 0); |
| 1373 | |
| 1374 | Thread *new_thread = |
| 1375 | process->GetThreadList().FindThreadByIndexID(index_id).get(); |
| 1376 | if (new_thread == nullptr) { |
| 1377 | result.AppendErrorWithFormat("invalid thread #%s.\n", |
| 1378 | command.GetArgumentAtIndex(0)); |
| 1379 | result.SetStatus(eReturnStatusFailed); |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
| 1383 | process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true); |
| 1384 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1385 | |
| 1386 | return result.Succeeded(); |
| 1387 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1388 | }; |
| 1389 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1390 | //------------------------------------------------------------------------- |
| 1391 | // CommandObjectThreadList |
| 1392 | //------------------------------------------------------------------------- |
| 1393 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1394 | class CommandObjectThreadList : public CommandObjectParsed { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1395 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1396 | CommandObjectThreadList(CommandInterpreter &interpreter) |
| 1397 | : CommandObjectParsed( |
| 1398 | interpreter, "thread list", |
| 1399 | "Show a summary of each thread in the current target process.", |
| 1400 | "thread list", |
| 1401 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
| 1402 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1403 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1404 | ~CommandObjectThreadList() override = default; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1405 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1406 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1407 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1408 | Stream &strm = result.GetOutputStream(); |
| 1409 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1410 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 1411 | const bool only_threads_with_stop_reason = false; |
| 1412 | const uint32_t start_frame = 0; |
| 1413 | const uint32_t num_frames = 0; |
| 1414 | const uint32_t num_frames_with_source = 0; |
| 1415 | process->GetStatus(strm); |
| 1416 | process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame, |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 1417 | num_frames, num_frames_with_source, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1418 | return result.Succeeded(); |
| 1419 | } |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1420 | }; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1421 | |
Jim Ingham | 93208b8 | 2013-01-31 21:46:01 +0000 | [diff] [blame] | 1422 | //------------------------------------------------------------------------- |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 1423 | // CommandObjectThreadInfo |
| 1424 | //------------------------------------------------------------------------- |
| 1425 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1426 | static constexpr OptionDefinition g_thread_info_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1427 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1428 | { LLDB_OPT_SET_ALL, false, "json", 'j', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Display the thread info in JSON format." }, |
| 1429 | { LLDB_OPT_SET_ALL, false, "stop-info", 's', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Display the extended stop info in JSON format." } |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1430 | // clang-format on |
| 1431 | }; |
| 1432 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1433 | class CommandObjectThreadInfo : public CommandObjectIterateOverThreads { |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 1434 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1435 | class CommandOptions : public Options { |
| 1436 | public: |
| 1437 | CommandOptions() : Options() { OptionParsingStarting(nullptr); } |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 1438 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1439 | ~CommandOptions() override = default; |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 1440 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1441 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 1442 | m_json_thread = false; |
| 1443 | m_json_stopinfo = false; |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1446 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 1447 | ExecutionContext *execution_context) override { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1448 | const int short_option = m_getopt_table[option_idx].val; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1449 | Status error; |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 1450 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1451 | switch (short_option) { |
| 1452 | case 'j': |
| 1453 | m_json_thread = true; |
| 1454 | break; |
| 1455 | |
| 1456 | case 's': |
| 1457 | m_json_stopinfo = true; |
| 1458 | break; |
| 1459 | |
| 1460 | default: |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1461 | return Status("invalid short option character '%c'", short_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1462 | } |
| 1463 | return error; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1466 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 1467 | return llvm::makeArrayRef(g_thread_info_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1468 | } |
Stephane Sezer | f810491 | 2016-03-17 18:52:41 +0000 | [diff] [blame] | 1469 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1470 | bool m_json_thread; |
| 1471 | bool m_json_stopinfo; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1472 | }; |
| 1473 | |
| 1474 | CommandObjectThreadInfo(CommandInterpreter &interpreter) |
| 1475 | : CommandObjectIterateOverThreads( |
| 1476 | interpreter, "thread info", "Show an extended summary of one or " |
| 1477 | "more threads. Defaults to the " |
| 1478 | "current thread.", |
| 1479 | "thread info", |
| 1480 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
| 1481 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), |
| 1482 | m_options() { |
| 1483 | m_add_return = false; |
| 1484 | } |
| 1485 | |
| 1486 | ~CommandObjectThreadInfo() override = default; |
| 1487 | |
| 1488 | Options *GetOptions() override { return &m_options; } |
| 1489 | |
| 1490 | bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { |
| 1491 | ThreadSP thread_sp = |
| 1492 | m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); |
| 1493 | if (!thread_sp) { |
| 1494 | result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n", |
| 1495 | tid); |
| 1496 | result.SetStatus(eReturnStatusFailed); |
| 1497 | return false; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1500 | Thread *thread = thread_sp.get(); |
| 1501 | |
| 1502 | Stream &strm = result.GetOutputStream(); |
| 1503 | if (!thread->GetDescription(strm, eDescriptionLevelFull, |
| 1504 | m_options.m_json_thread, |
| 1505 | m_options.m_json_stopinfo)) { |
| 1506 | result.AppendErrorWithFormat("error displaying info for thread: \"%d\"\n", |
| 1507 | thread->GetIndexID()); |
| 1508 | result.SetStatus(eReturnStatusFailed); |
| 1509 | return false; |
| 1510 | } |
| 1511 | return true; |
| 1512 | } |
| 1513 | |
| 1514 | CommandOptions m_options; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 1515 | }; |
| 1516 | |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 1517 | //------------------------------------------------------------------------- |
Jim Ingham | 93208b8 | 2013-01-31 21:46:01 +0000 | [diff] [blame] | 1518 | // CommandObjectThreadReturn |
| 1519 | //------------------------------------------------------------------------- |
| 1520 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1521 | static constexpr OptionDefinition g_thread_return_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1522 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1523 | { LLDB_OPT_SET_ALL, false, "from-expression", 'x', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Return from the innermost expression evaluation." } |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1524 | // clang-format on |
| 1525 | }; |
| 1526 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1527 | class CommandObjectThreadReturn : public CommandObjectRaw { |
Jim Ingham | cb640dd | 2012-09-14 02:14:15 +0000 | [diff] [blame] | 1528 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1529 | class CommandOptions : public Options { |
| 1530 | public: |
| 1531 | CommandOptions() : Options(), m_from_expression(false) { |
| 1532 | // Keep default values of all options in one place: OptionParsingStarting |
| 1533 | // () |
| 1534 | OptionParsingStarting(nullptr); |
Jim Ingham | cb640dd | 2012-09-14 02:14:15 +0000 | [diff] [blame] | 1535 | } |
Jim Ingham | cb640dd | 2012-09-14 02:14:15 +0000 | [diff] [blame] | 1536 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1537 | ~CommandOptions() override = default; |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 1538 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1539 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 1540 | ExecutionContext *execution_context) override { |
| 1541 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1542 | const int short_option = m_getopt_table[option_idx].val; |
| 1543 | |
| 1544 | switch (short_option) { |
| 1545 | case 'x': { |
| 1546 | bool success; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 1547 | bool tmp_value = |
| 1548 | OptionArgParser::ToBoolean(option_arg, false, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1549 | if (success) |
| 1550 | m_from_expression = tmp_value; |
| 1551 | else { |
| 1552 | error.SetErrorStringWithFormat( |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 1553 | "invalid boolean value '%s' for 'x' option", |
| 1554 | option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1555 | } |
| 1556 | } break; |
| 1557 | default: |
| 1558 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 1559 | short_option); |
| 1560 | break; |
| 1561 | } |
| 1562 | return error; |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1565 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 1566 | m_from_expression = false; |
| 1567 | } |
| 1568 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1569 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 1570 | return llvm::makeArrayRef(g_thread_return_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1571 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1572 | |
| 1573 | bool m_from_expression; |
| 1574 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1575 | // Instance variables to hold the values for command options. |
| 1576 | }; |
| 1577 | |
| 1578 | CommandObjectThreadReturn(CommandInterpreter &interpreter) |
| 1579 | : CommandObjectRaw(interpreter, "thread return", |
| 1580 | "Prematurely return from a stack frame, " |
| 1581 | "short-circuiting execution of newer frames " |
| 1582 | "and optionally yielding a specified value. Defaults " |
| 1583 | "to the exiting the current stack " |
| 1584 | "frame.", |
| 1585 | "thread return", |
| 1586 | eCommandRequiresFrame | eCommandTryTargetAPILock | |
| 1587 | eCommandProcessMustBeLaunched | |
| 1588 | eCommandProcessMustBePaused), |
| 1589 | m_options() { |
| 1590 | CommandArgumentEntry arg; |
| 1591 | CommandArgumentData expression_arg; |
| 1592 | |
| 1593 | // Define the first (and only) variant of this arg. |
| 1594 | expression_arg.arg_type = eArgTypeExpression; |
| 1595 | expression_arg.arg_repetition = eArgRepeatOptional; |
| 1596 | |
| 1597 | // There is only one variant this argument could be; put it into the |
| 1598 | // argument entry. |
| 1599 | arg.push_back(expression_arg); |
| 1600 | |
| 1601 | // Push the data for the first argument into the m_arguments vector. |
| 1602 | m_arguments.push_back(arg); |
| 1603 | } |
| 1604 | |
| 1605 | ~CommandObjectThreadReturn() override = default; |
| 1606 | |
| 1607 | Options *GetOptions() override { return &m_options; } |
| 1608 | |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 1609 | protected: |
Raphael Isemann | 4d51a90 | 2018-07-12 22:28:52 +0000 | [diff] [blame] | 1610 | bool DoExecute(llvm::StringRef command, |
| 1611 | CommandReturnObject &result) override { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1612 | // I am going to handle this by hand, because I don't want you to have to |
| 1613 | // say: |
| 1614 | // "thread return -- -5". |
Raphael Isemann | 4d51a90 | 2018-07-12 22:28:52 +0000 | [diff] [blame] | 1615 | if (command.startswith("-x")) { |
| 1616 | if (command.size() != 2U) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1617 | result.AppendWarning("Return values ignored when returning from user " |
| 1618 | "called expressions"); |
Jim Ingham | cb640dd | 2012-09-14 02:14:15 +0000 | [diff] [blame] | 1619 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1620 | Thread *thread = m_exe_ctx.GetThreadPtr(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1621 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1622 | error = thread->UnwindInnermostExpression(); |
| 1623 | if (!error.Success()) { |
| 1624 | result.AppendErrorWithFormat("Unwinding expression failed - %s.", |
| 1625 | error.AsCString()); |
| 1626 | result.SetStatus(eReturnStatusFailed); |
| 1627 | } else { |
| 1628 | bool success = |
| 1629 | thread->SetSelectedFrameByIndexNoisily(0, result.GetOutputStream()); |
| 1630 | if (success) { |
| 1631 | m_exe_ctx.SetFrameSP(thread->GetSelectedFrame()); |
| 1632 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1633 | } else { |
| 1634 | result.AppendErrorWithFormat( |
| 1635 | "Could not select 0th frame after unwinding expression."); |
| 1636 | result.SetStatus(eReturnStatusFailed); |
Jim Ingham | cb640dd | 2012-09-14 02:14:15 +0000 | [diff] [blame] | 1637 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1638 | } |
| 1639 | return result.Succeeded(); |
Jim Ingham | cb640dd | 2012-09-14 02:14:15 +0000 | [diff] [blame] | 1640 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1641 | |
| 1642 | ValueObjectSP return_valobj_sp; |
| 1643 | |
| 1644 | StackFrameSP frame_sp = m_exe_ctx.GetFrameSP(); |
| 1645 | uint32_t frame_idx = frame_sp->GetFrameIndex(); |
| 1646 | |
| 1647 | if (frame_sp->IsInlined()) { |
| 1648 | result.AppendError("Don't know how to return from inlined frames."); |
| 1649 | result.SetStatus(eReturnStatusFailed); |
| 1650 | return false; |
| 1651 | } |
| 1652 | |
Raphael Isemann | 4d51a90 | 2018-07-12 22:28:52 +0000 | [diff] [blame] | 1653 | if (!command.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1654 | Target *target = m_exe_ctx.GetTargetPtr(); |
| 1655 | EvaluateExpressionOptions options; |
| 1656 | |
| 1657 | options.SetUnwindOnError(true); |
| 1658 | options.SetUseDynamic(eNoDynamicValues); |
| 1659 | |
| 1660 | ExpressionResults exe_results = eExpressionSetupError; |
| 1661 | exe_results = target->EvaluateExpression(command, frame_sp.get(), |
| 1662 | return_valobj_sp, options); |
| 1663 | if (exe_results != eExpressionCompleted) { |
| 1664 | if (return_valobj_sp) |
| 1665 | result.AppendErrorWithFormat( |
| 1666 | "Error evaluating result expression: %s", |
| 1667 | return_valobj_sp->GetError().AsCString()); |
| 1668 | else |
| 1669 | result.AppendErrorWithFormat( |
| 1670 | "Unknown error evaluating result expression."); |
| 1671 | result.SetStatus(eReturnStatusFailed); |
| 1672 | return false; |
| 1673 | } |
| 1674 | } |
| 1675 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1676 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1677 | ThreadSP thread_sp = m_exe_ctx.GetThreadSP(); |
| 1678 | const bool broadcast = true; |
| 1679 | error = thread_sp->ReturnFromFrame(frame_sp, return_valobj_sp, broadcast); |
| 1680 | if (!error.Success()) { |
| 1681 | result.AppendErrorWithFormat( |
| 1682 | "Error returning from frame %d of thread %d: %s.", frame_idx, |
| 1683 | thread_sp->GetIndexID(), error.AsCString()); |
| 1684 | result.SetStatus(eReturnStatusFailed); |
| 1685 | return false; |
| 1686 | } |
| 1687 | |
| 1688 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1689 | return true; |
| 1690 | } |
| 1691 | |
| 1692 | CommandOptions m_options; |
Jim Ingham | cb640dd | 2012-09-14 02:14:15 +0000 | [diff] [blame] | 1693 | }; |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 1694 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1695 | //------------------------------------------------------------------------- |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1696 | // CommandObjectThreadJump |
| 1697 | //------------------------------------------------------------------------- |
| 1698 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1699 | static constexpr OptionDefinition g_thread_jump_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1700 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1701 | { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, {}, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "Specifies the source file to jump to." }, |
| 1702 | { LLDB_OPT_SET_1, true, "line", 'l', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeLineNum, "Specifies the line number to jump to." }, |
| 1703 | { LLDB_OPT_SET_2, true, "by", 'b', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeOffset, "Jumps by a relative line offset from the current line." }, |
| 1704 | { LLDB_OPT_SET_3, true, "address", 'a', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeAddressOrExpression, "Jumps to a specific address." }, |
| 1705 | { 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 Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1706 | // clang-format on |
| 1707 | }; |
| 1708 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1709 | class CommandObjectThreadJump : public CommandObjectParsed { |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1710 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1711 | class CommandOptions : public Options { |
| 1712 | public: |
| 1713 | CommandOptions() : Options() { OptionParsingStarting(nullptr); } |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1714 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1715 | ~CommandOptions() override = default; |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 1716 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1717 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 1718 | m_filenames.Clear(); |
| 1719 | m_line_num = 0; |
| 1720 | m_line_offset = 0; |
| 1721 | m_load_addr = LLDB_INVALID_ADDRESS; |
| 1722 | m_force = false; |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1725 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 1726 | ExecutionContext *execution_context) override { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1727 | const int short_option = m_getopt_table[option_idx].val; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1728 | Status error; |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 1729 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1730 | switch (short_option) { |
| 1731 | case 'f': |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 1732 | m_filenames.AppendIfUnique(FileSpec(option_arg)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1733 | if (m_filenames.GetSize() > 1) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1734 | return Status("only one source file expected."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1735 | break; |
| 1736 | case 'l': |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 1737 | if (option_arg.getAsInteger(0, m_line_num)) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1738 | return Status("invalid line number: '%s'.", option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1739 | break; |
| 1740 | case 'b': |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 1741 | if (option_arg.getAsInteger(0, m_line_offset)) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1742 | return Status("invalid line offset: '%s'.", option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1743 | break; |
| 1744 | case 'a': |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 1745 | m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg, |
| 1746 | LLDB_INVALID_ADDRESS, &error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1747 | break; |
| 1748 | case 'r': |
| 1749 | m_force = true; |
| 1750 | break; |
| 1751 | default: |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1752 | return Status("invalid short option character '%c'", short_option); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1753 | } |
| 1754 | return error; |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1757 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 1758 | return llvm::makeArrayRef(g_thread_jump_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1759 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1760 | |
| 1761 | FileSpecList m_filenames; |
| 1762 | uint32_t m_line_num; |
| 1763 | int32_t m_line_offset; |
| 1764 | lldb::addr_t m_load_addr; |
| 1765 | bool m_force; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1766 | }; |
| 1767 | |
| 1768 | CommandObjectThreadJump(CommandInterpreter &interpreter) |
| 1769 | : CommandObjectParsed( |
| 1770 | interpreter, "thread jump", |
| 1771 | "Sets the program counter to a new address.", "thread jump", |
| 1772 | eCommandRequiresFrame | eCommandTryTargetAPILock | |
| 1773 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), |
| 1774 | m_options() {} |
| 1775 | |
| 1776 | ~CommandObjectThreadJump() override = default; |
| 1777 | |
| 1778 | Options *GetOptions() override { return &m_options; } |
| 1779 | |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1780 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1781 | bool DoExecute(Args &args, CommandReturnObject &result) override { |
| 1782 | RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext(); |
| 1783 | StackFrame *frame = m_exe_ctx.GetFramePtr(); |
| 1784 | Thread *thread = m_exe_ctx.GetThreadPtr(); |
| 1785 | Target *target = m_exe_ctx.GetTargetPtr(); |
| 1786 | const SymbolContext &sym_ctx = |
| 1787 | frame->GetSymbolContext(eSymbolContextLineEntry); |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1788 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1789 | if (m_options.m_load_addr != LLDB_INVALID_ADDRESS) { |
| 1790 | // Use this address directly. |
| 1791 | Address dest = Address(m_options.m_load_addr); |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1792 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1793 | lldb::addr_t callAddr = dest.GetCallableLoadAddress(target); |
| 1794 | if (callAddr == LLDB_INVALID_ADDRESS) { |
| 1795 | result.AppendErrorWithFormat("Invalid destination address."); |
| 1796 | result.SetStatus(eReturnStatusFailed); |
| 1797 | return false; |
| 1798 | } |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1799 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1800 | if (!reg_ctx->SetPC(callAddr)) { |
| 1801 | result.AppendErrorWithFormat("Error changing PC value for thread %d.", |
| 1802 | thread->GetIndexID()); |
| 1803 | result.SetStatus(eReturnStatusFailed); |
| 1804 | return false; |
| 1805 | } |
| 1806 | } else { |
| 1807 | // Pick either the absolute line, or work out a relative one. |
| 1808 | int32_t line = (int32_t)m_options.m_line_num; |
| 1809 | if (line == 0) |
| 1810 | line = sym_ctx.line_entry.line + m_options.m_line_offset; |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1811 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1812 | // Try the current file, but override if asked. |
| 1813 | FileSpec file = sym_ctx.line_entry.file; |
| 1814 | if (m_options.m_filenames.GetSize() == 1) |
| 1815 | file = m_options.m_filenames.GetFileSpecAtIndex(0); |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1816 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1817 | if (!file) { |
| 1818 | result.AppendErrorWithFormat( |
| 1819 | "No source file available for the current location."); |
| 1820 | result.SetStatus(eReturnStatusFailed); |
| 1821 | return false; |
| 1822 | } |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1823 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1824 | std::string warnings; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1825 | Status err = thread->JumpToLine(file, line, m_options.m_force, &warnings); |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1826 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1827 | if (err.Fail()) { |
| 1828 | result.SetError(err); |
| 1829 | return false; |
| 1830 | } |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1831 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1832 | if (!warnings.empty()) |
| 1833 | result.AppendWarning(warnings.c_str()); |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1836 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1837 | return true; |
| 1838 | } |
| 1839 | |
| 1840 | CommandOptions m_options; |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1841 | }; |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 1842 | |
Richard Mitton | f86248d | 2013-09-12 02:20:34 +0000 | [diff] [blame] | 1843 | //------------------------------------------------------------------------- |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1844 | // Next are the subcommands of CommandObjectMultiwordThreadPlan |
| 1845 | //------------------------------------------------------------------------- |
| 1846 | |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1847 | //------------------------------------------------------------------------- |
| 1848 | // CommandObjectThreadPlanList |
| 1849 | //------------------------------------------------------------------------- |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 1850 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1851 | static constexpr OptionDefinition g_thread_plan_list_options[] = { |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1852 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1853 | { LLDB_OPT_SET_1, false, "verbose", 'v', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Display more information about the thread plans" }, |
| 1854 | { LLDB_OPT_SET_1, false, "internal", 'i', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Display internal as well as user thread plans" } |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1855 | // clang-format on |
| 1856 | }; |
| 1857 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1858 | class CommandObjectThreadPlanList : public CommandObjectIterateOverThreads { |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1859 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1860 | class CommandOptions : public Options { |
| 1861 | public: |
| 1862 | CommandOptions() : Options() { |
| 1863 | // Keep default values of all options in one place: OptionParsingStarting |
| 1864 | // () |
| 1865 | OptionParsingStarting(nullptr); |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1868 | ~CommandOptions() override = default; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1869 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1870 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 1871 | ExecutionContext *execution_context) override { |
| 1872 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1873 | const int short_option = m_getopt_table[option_idx].val; |
| 1874 | |
| 1875 | switch (short_option) { |
| 1876 | case 'i': |
| 1877 | m_internal = true; |
| 1878 | break; |
| 1879 | case 'v': |
| 1880 | m_verbose = true; |
| 1881 | break; |
| 1882 | default: |
| 1883 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 1884 | short_option); |
| 1885 | break; |
| 1886 | } |
| 1887 | return error; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1890 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 1891 | m_verbose = false; |
| 1892 | m_internal = false; |
| 1893 | } |
| 1894 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1895 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 1896 | return llvm::makeArrayRef(g_thread_plan_list_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1897 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1898 | |
| 1899 | // Instance variables to hold the values for command options. |
| 1900 | bool m_verbose; |
| 1901 | bool m_internal; |
| 1902 | }; |
| 1903 | |
| 1904 | CommandObjectThreadPlanList(CommandInterpreter &interpreter) |
| 1905 | : CommandObjectIterateOverThreads( |
| 1906 | interpreter, "thread plan list", |
| 1907 | "Show thread plans for one or more threads. If no threads are " |
| 1908 | "specified, show the " |
| 1909 | "current thread. Use the thread-index \"all\" to see all threads.", |
| 1910 | nullptr, |
| 1911 | eCommandRequiresProcess | eCommandRequiresThread | |
| 1912 | eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | |
| 1913 | eCommandProcessMustBePaused), |
| 1914 | m_options() {} |
| 1915 | |
| 1916 | ~CommandObjectThreadPlanList() override = default; |
| 1917 | |
| 1918 | Options *GetOptions() override { return &m_options; } |
| 1919 | |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1920 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1921 | bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { |
| 1922 | ThreadSP thread_sp = |
| 1923 | m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); |
| 1924 | if (!thread_sp) { |
| 1925 | result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n", |
| 1926 | tid); |
| 1927 | result.SetStatus(eReturnStatusFailed); |
| 1928 | return false; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1929 | } |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 1930 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1931 | Thread *thread = thread_sp.get(); |
| 1932 | |
| 1933 | Stream &strm = result.GetOutputStream(); |
| 1934 | DescriptionLevel desc_level = eDescriptionLevelFull; |
| 1935 | if (m_options.m_verbose) |
| 1936 | desc_level = eDescriptionLevelVerbose; |
| 1937 | |
| 1938 | thread->DumpThreadPlans(&strm, desc_level, m_options.m_internal, true); |
| 1939 | return true; |
| 1940 | } |
| 1941 | |
| 1942 | CommandOptions m_options; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1943 | }; |
| 1944 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1945 | class CommandObjectThreadPlanDiscard : public CommandObjectParsed { |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1946 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1947 | CommandObjectThreadPlanDiscard(CommandInterpreter &interpreter) |
| 1948 | : CommandObjectParsed(interpreter, "thread plan discard", |
| 1949 | "Discards thread plans up to and including the " |
| 1950 | "specified index (see 'thread plan list'.) " |
| 1951 | "Only user visible plans can be discarded.", |
| 1952 | nullptr, |
| 1953 | eCommandRequiresProcess | eCommandRequiresThread | |
| 1954 | eCommandTryTargetAPILock | |
| 1955 | eCommandProcessMustBeLaunched | |
| 1956 | eCommandProcessMustBePaused) { |
| 1957 | CommandArgumentEntry arg; |
| 1958 | CommandArgumentData plan_index_arg; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1959 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1960 | // Define the first (and only) variant of this arg. |
| 1961 | plan_index_arg.arg_type = eArgTypeUnsignedInteger; |
| 1962 | plan_index_arg.arg_repetition = eArgRepeatPlain; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1963 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1964 | // There is only one variant this argument could be; put it into the |
| 1965 | // argument entry. |
| 1966 | arg.push_back(plan_index_arg); |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1967 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1968 | // Push the data for the first argument into the m_arguments vector. |
| 1969 | m_arguments.push_back(arg); |
| 1970 | } |
| 1971 | |
| 1972 | ~CommandObjectThreadPlanDiscard() override = default; |
| 1973 | |
| 1974 | bool DoExecute(Args &args, CommandReturnObject &result) override { |
| 1975 | Thread *thread = m_exe_ctx.GetThreadPtr(); |
| 1976 | if (args.GetArgumentCount() != 1) { |
| 1977 | result.AppendErrorWithFormat("Too many arguments, expected one - the " |
| 1978 | "thread plan index - but got %zu.", |
| 1979 | args.GetArgumentCount()); |
| 1980 | result.SetStatus(eReturnStatusFailed); |
| 1981 | return false; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1984 | bool success; |
| 1985 | uint32_t thread_plan_idx = |
| 1986 | StringConvert::ToUInt32(args.GetArgumentAtIndex(0), 0, 0, &success); |
| 1987 | if (!success) { |
| 1988 | result.AppendErrorWithFormat( |
| 1989 | "Invalid thread index: \"%s\" - should be unsigned int.", |
| 1990 | args.GetArgumentAtIndex(0)); |
| 1991 | result.SetStatus(eReturnStatusFailed); |
| 1992 | return false; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 1993 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1994 | |
| 1995 | if (thread_plan_idx == 0) { |
| 1996 | result.AppendErrorWithFormat( |
| 1997 | "You wouldn't really want me to discard the base thread plan."); |
| 1998 | result.SetStatus(eReturnStatusFailed); |
| 1999 | return false; |
| 2000 | } |
| 2001 | |
| 2002 | if (thread->DiscardUserThreadPlansUpToIndex(thread_plan_idx)) { |
| 2003 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 2004 | return true; |
| 2005 | } else { |
| 2006 | result.AppendErrorWithFormat( |
| 2007 | "Could not find User thread plan with index %s.", |
| 2008 | args.GetArgumentAtIndex(0)); |
| 2009 | result.SetStatus(eReturnStatusFailed); |
| 2010 | return false; |
| 2011 | } |
| 2012 | } |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 2013 | }; |
| 2014 | |
| 2015 | //------------------------------------------------------------------------- |
| 2016 | // CommandObjectMultiwordThreadPlan |
| 2017 | //------------------------------------------------------------------------- |
| 2018 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2019 | class CommandObjectMultiwordThreadPlan : public CommandObjectMultiword { |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 2020 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2021 | CommandObjectMultiwordThreadPlan(CommandInterpreter &interpreter) |
| 2022 | : CommandObjectMultiword( |
| 2023 | interpreter, "plan", |
| 2024 | "Commands for managing thread plans that control execution.", |
| 2025 | "thread plan <subcommand> [<subcommand objects]") { |
| 2026 | LoadSubCommand( |
| 2027 | "list", CommandObjectSP(new CommandObjectThreadPlanList(interpreter))); |
| 2028 | LoadSubCommand( |
| 2029 | "discard", |
| 2030 | CommandObjectSP(new CommandObjectThreadPlanDiscard(interpreter))); |
| 2031 | } |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 2032 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2033 | ~CommandObjectMultiwordThreadPlan() override = default; |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 2034 | }; |
| 2035 | |
| 2036 | //------------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2037 | // CommandObjectMultiwordThread |
| 2038 | //------------------------------------------------------------------------- |
| 2039 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2040 | CommandObjectMultiwordThread::CommandObjectMultiwordThread( |
| 2041 | CommandInterpreter &interpreter) |
| 2042 | : CommandObjectMultiword(interpreter, "thread", "Commands for operating on " |
| 2043 | "one or more threads in " |
| 2044 | "the current process.", |
| 2045 | "thread <subcommand> [<subcommand-options>]") { |
| 2046 | LoadSubCommand("backtrace", CommandObjectSP(new CommandObjectThreadBacktrace( |
| 2047 | interpreter))); |
| 2048 | LoadSubCommand("continue", |
| 2049 | CommandObjectSP(new CommandObjectThreadContinue(interpreter))); |
| 2050 | LoadSubCommand("list", |
| 2051 | CommandObjectSP(new CommandObjectThreadList(interpreter))); |
| 2052 | LoadSubCommand("return", |
| 2053 | CommandObjectSP(new CommandObjectThreadReturn(interpreter))); |
| 2054 | LoadSubCommand("jump", |
| 2055 | CommandObjectSP(new CommandObjectThreadJump(interpreter))); |
| 2056 | LoadSubCommand("select", |
| 2057 | CommandObjectSP(new CommandObjectThreadSelect(interpreter))); |
| 2058 | LoadSubCommand("until", |
| 2059 | CommandObjectSP(new CommandObjectThreadUntil(interpreter))); |
| 2060 | LoadSubCommand("info", |
| 2061 | CommandObjectSP(new CommandObjectThreadInfo(interpreter))); |
| 2062 | LoadSubCommand("step-in", |
| 2063 | CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( |
| 2064 | interpreter, "thread step-in", |
| 2065 | "Source level single step, stepping into calls. Defaults " |
| 2066 | "to current thread unless specified.", |
| 2067 | nullptr, eStepTypeInto, eStepScopeSource))); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2068 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2069 | LoadSubCommand("step-out", |
| 2070 | CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( |
| 2071 | interpreter, "thread step-out", |
| 2072 | "Finish executing the current stack frame and stop after " |
| 2073 | "returning. Defaults to current thread unless specified.", |
| 2074 | nullptr, eStepTypeOut, eStepScopeSource))); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2075 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2076 | LoadSubCommand("step-over", |
| 2077 | CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( |
| 2078 | interpreter, "thread step-over", |
| 2079 | "Source level single step, stepping over calls. Defaults " |
| 2080 | "to current thread unless specified.", |
| 2081 | nullptr, eStepTypeOver, eStepScopeSource))); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 2082 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2083 | LoadSubCommand("step-inst", |
| 2084 | CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( |
| 2085 | interpreter, "thread step-inst", |
| 2086 | "Instruction level single step, stepping into calls. " |
| 2087 | "Defaults to current thread unless specified.", |
| 2088 | nullptr, eStepTypeTrace, eStepScopeInstruction))); |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 2089 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2090 | LoadSubCommand("step-inst-over", |
| 2091 | CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( |
| 2092 | interpreter, "thread step-inst-over", |
| 2093 | "Instruction level single step, stepping over calls. " |
| 2094 | "Defaults to current thread unless specified.", |
| 2095 | nullptr, eStepTypeTraceOver, eStepScopeInstruction))); |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 2096 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2097 | LoadSubCommand( |
| 2098 | "step-scripted", |
| 2099 | CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( |
| 2100 | interpreter, "thread step-scripted", |
| 2101 | "Step as instructed by the script class passed in the -C option.", |
| 2102 | nullptr, eStepTypeScripted, eStepScopeSource))); |
Jim Ingham | 2bdbfd5 | 2014-09-29 23:17:18 +0000 | [diff] [blame] | 2103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2104 | LoadSubCommand("plan", CommandObjectSP(new CommandObjectMultiwordThreadPlan( |
| 2105 | interpreter))); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Eugene Zelenko | 50ff9fe | 2016-02-25 23:46:36 +0000 | [diff] [blame] | 2108 | CommandObjectMultiwordThread::~CommandObjectMultiwordThread() = default; |