1 | //===-- CommandObjectThread.cpp -------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "CommandObjectThread.h" |
10 | |
11 | #include <memory> |
12 | #include <optional> |
13 | #include <sstream> |
14 | |
15 | #include "CommandObjectThreadUtil.h" |
16 | #include "CommandObjectTrace.h" |
17 | #include "lldb/Core/PluginManager.h" |
18 | #include "lldb/Core/ValueObject.h" |
19 | #include "lldb/Host/OptionParser.h" |
20 | #include "lldb/Interpreter/CommandInterpreter.h" |
21 | #include "lldb/Interpreter/CommandOptionArgumentTable.h" |
22 | #include "lldb/Interpreter/CommandReturnObject.h" |
23 | #include "lldb/Interpreter/OptionArgParser.h" |
24 | #include "lldb/Interpreter/OptionGroupPythonClassWithDict.h" |
25 | #include "lldb/Interpreter/Options.h" |
26 | #include "lldb/Symbol/CompileUnit.h" |
27 | #include "lldb/Symbol/Function.h" |
28 | #include "lldb/Symbol/LineEntry.h" |
29 | #include "lldb/Symbol/LineTable.h" |
30 | #include "lldb/Target/Process.h" |
31 | #include "lldb/Target/RegisterContext.h" |
32 | #include "lldb/Target/SystemRuntime.h" |
33 | #include "lldb/Target/Target.h" |
34 | #include "lldb/Target/Thread.h" |
35 | #include "lldb/Target/ThreadPlan.h" |
36 | #include "lldb/Target/ThreadPlanStepInRange.h" |
37 | #include "lldb/Target/Trace.h" |
38 | #include "lldb/Target/TraceDumper.h" |
39 | #include "lldb/Utility/State.h" |
40 | |
41 | using namespace lldb; |
42 | using namespace lldb_private; |
43 | |
44 | // CommandObjectThreadBacktrace |
45 | #define LLDB_OPTIONS_thread_backtrace |
46 | #include "CommandOptions.inc" |
47 | |
48 | class CommandObjectThreadBacktrace : public CommandObjectIterateOverThreads { |
49 | public: |
50 | class CommandOptions : public Options { |
51 | public: |
52 | CommandOptions() { |
53 | // Keep default values of all options in one place: OptionParsingStarting |
54 | // () |
55 | OptionParsingStarting(execution_context: nullptr); |
56 | } |
57 | |
58 | ~CommandOptions() override = default; |
59 | |
60 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
61 | ExecutionContext *execution_context) override { |
62 | Status error; |
63 | const int short_option = m_getopt_table[option_idx].val; |
64 | |
65 | switch (short_option) { |
66 | case 'c': |
67 | if (option_arg.getAsInteger(0, m_count)) { |
68 | m_count = UINT32_MAX; |
69 | error.SetErrorStringWithFormat( |
70 | "invalid integer value for option '%c'" , short_option); |
71 | } |
72 | break; |
73 | case 's': |
74 | if (option_arg.getAsInteger(0, m_start)) |
75 | error.SetErrorStringWithFormat( |
76 | "invalid integer value for option '%c'" , short_option); |
77 | break; |
78 | case 'e': { |
79 | bool success; |
80 | m_extended_backtrace = |
81 | OptionArgParser::ToBoolean(s: option_arg, fail_value: false, success_ptr: &success); |
82 | if (!success) |
83 | error.SetErrorStringWithFormat( |
84 | "invalid boolean value for option '%c'" , short_option); |
85 | } break; |
86 | default: |
87 | llvm_unreachable("Unimplemented option" ); |
88 | } |
89 | return error; |
90 | } |
91 | |
92 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
93 | m_count = UINT32_MAX; |
94 | m_start = 0; |
95 | m_extended_backtrace = false; |
96 | } |
97 | |
98 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
99 | return llvm::ArrayRef(g_thread_backtrace_options); |
100 | } |
101 | |
102 | // Instance variables to hold the values for command options. |
103 | uint32_t m_count; |
104 | uint32_t m_start; |
105 | bool m_extended_backtrace; |
106 | }; |
107 | |
108 | CommandObjectThreadBacktrace(CommandInterpreter &interpreter) |
109 | : CommandObjectIterateOverThreads( |
110 | interpreter, "thread backtrace" , |
111 | "Show thread call stacks. Defaults to the current thread, thread " |
112 | "indexes can be specified as arguments.\n" |
113 | "Use the thread-index \"all\" to see all threads.\n" |
114 | "Use the thread-index \"unique\" to see threads grouped by unique " |
115 | "call stacks.\n" |
116 | "Use 'settings set frame-format' to customize the printing of " |
117 | "frames in the backtrace and 'settings set thread-format' to " |
118 | "customize the thread header." , |
119 | nullptr, |
120 | eCommandRequiresProcess | eCommandRequiresThread | |
121 | eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | |
122 | eCommandProcessMustBePaused) {} |
123 | |
124 | ~CommandObjectThreadBacktrace() override = default; |
125 | |
126 | Options *GetOptions() override { return &m_options; } |
127 | |
128 | std::optional<std::string> GetRepeatCommand(Args ¤t_args, |
129 | uint32_t idx) override { |
130 | llvm::StringRef count_opt("--count" ); |
131 | llvm::StringRef start_opt("--start" ); |
132 | |
133 | // If no "count" was provided, we are dumping the entire backtrace, so |
134 | // there isn't a repeat command. So we search for the count option in |
135 | // the args, and if we find it, we make a copy and insert or modify the |
136 | // start option's value to start count indices greater. |
137 | |
138 | Args copy_args(current_args); |
139 | size_t num_entries = copy_args.GetArgumentCount(); |
140 | // These two point at the index of the option value if found. |
141 | size_t count_idx = 0; |
142 | size_t start_idx = 0; |
143 | size_t count_val = 0; |
144 | size_t start_val = 0; |
145 | |
146 | for (size_t idx = 0; idx < num_entries; idx++) { |
147 | llvm::StringRef arg_string = copy_args[idx].ref(); |
148 | if (arg_string.equals(RHS: "-c" ) || count_opt.starts_with(Prefix: arg_string)) { |
149 | idx++; |
150 | if (idx == num_entries) |
151 | return std::nullopt; |
152 | count_idx = idx; |
153 | if (copy_args[idx].ref().getAsInteger(0, count_val)) |
154 | return std::nullopt; |
155 | } else if (arg_string.equals(RHS: "-s" ) || start_opt.starts_with(Prefix: arg_string)) { |
156 | idx++; |
157 | if (idx == num_entries) |
158 | return std::nullopt; |
159 | start_idx = idx; |
160 | if (copy_args[idx].ref().getAsInteger(0, start_val)) |
161 | return std::nullopt; |
162 | } |
163 | } |
164 | if (count_idx == 0) |
165 | return std::nullopt; |
166 | |
167 | std::string new_start_val = llvm::formatv("{0}" , start_val + count_val); |
168 | if (start_idx == 0) { |
169 | copy_args.AppendArgument(arg_str: start_opt); |
170 | copy_args.AppendArgument(arg_str: new_start_val); |
171 | } else { |
172 | copy_args.ReplaceArgumentAtIndex(idx: start_idx, arg_str: new_start_val); |
173 | } |
174 | std::string repeat_command; |
175 | if (!copy_args.GetQuotedCommandString(command&: repeat_command)) |
176 | return std::nullopt; |
177 | return repeat_command; |
178 | } |
179 | |
180 | protected: |
181 | void DoExtendedBacktrace(Thread *thread, CommandReturnObject &result) { |
182 | SystemRuntime *runtime = thread->GetProcess()->GetSystemRuntime(); |
183 | if (runtime) { |
184 | Stream &strm = result.GetOutputStream(); |
185 | const std::vector<ConstString> &types = |
186 | runtime->GetExtendedBacktraceTypes(); |
187 | for (auto type : types) { |
188 | ThreadSP ext_thread_sp = runtime->GetExtendedBacktraceThread( |
189 | thread->shared_from_this(), type); |
190 | if (ext_thread_sp && ext_thread_sp->IsValid()) { |
191 | const uint32_t num_frames_with_source = 0; |
192 | const bool stop_format = false; |
193 | strm.PutChar('\n'); |
194 | if (ext_thread_sp->GetStatus(strm, m_options.m_start, |
195 | m_options.m_count, |
196 | num_frames_with_source, stop_format)) { |
197 | DoExtendedBacktrace(ext_thread_sp.get(), result); |
198 | } |
199 | } |
200 | } |
201 | } |
202 | } |
203 | |
204 | bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { |
205 | ThreadSP thread_sp = |
206 | m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); |
207 | if (!thread_sp) { |
208 | result.AppendErrorWithFormat( |
209 | format: "thread disappeared while computing backtraces: 0x%" PRIx64 "\n" , |
210 | tid); |
211 | return false; |
212 | } |
213 | |
214 | Thread *thread = thread_sp.get(); |
215 | |
216 | Stream &strm = result.GetOutputStream(); |
217 | |
218 | // Only dump stack info if we processing unique stacks. |
219 | const bool only_stacks = m_unique_stacks; |
220 | |
221 | // Don't show source context when doing backtraces. |
222 | const uint32_t num_frames_with_source = 0; |
223 | const bool stop_format = true; |
224 | if (!thread->GetStatus(strm, start_frame: m_options.m_start, num_frames: m_options.m_count, |
225 | num_frames_with_source, stop_format, only_stacks)) { |
226 | result.AppendErrorWithFormat( |
227 | format: "error displaying backtrace for thread: \"0x%4.4x\"\n" , |
228 | thread->GetIndexID()); |
229 | return false; |
230 | } |
231 | if (m_options.m_extended_backtrace) { |
232 | if (!INTERRUPT_REQUESTED(GetDebugger(), |
233 | "Interrupt skipped extended backtrace" )) { |
234 | DoExtendedBacktrace(thread, result); |
235 | } |
236 | } |
237 | |
238 | return true; |
239 | } |
240 | |
241 | CommandOptions m_options; |
242 | }; |
243 | |
244 | enum StepScope { eStepScopeSource, eStepScopeInstruction }; |
245 | |
246 | #define LLDB_OPTIONS_thread_step_scope |
247 | #include "CommandOptions.inc" |
248 | |
249 | class ThreadStepScopeOptionGroup : public OptionGroup { |
250 | public: |
251 | ThreadStepScopeOptionGroup() { |
252 | // Keep default values of all options in one place: OptionParsingStarting |
253 | // () |
254 | OptionParsingStarting(execution_context: nullptr); |
255 | } |
256 | |
257 | ~ThreadStepScopeOptionGroup() override = default; |
258 | |
259 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
260 | return llvm::ArrayRef(g_thread_step_scope_options); |
261 | } |
262 | |
263 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
264 | ExecutionContext *execution_context) override { |
265 | Status error; |
266 | const int short_option = |
267 | g_thread_step_scope_options[option_idx].short_option; |
268 | |
269 | switch (short_option) { |
270 | case 'a': { |
271 | bool success; |
272 | bool avoid_no_debug = |
273 | OptionArgParser::ToBoolean(s: option_arg, fail_value: true, success_ptr: &success); |
274 | if (!success) |
275 | error.SetErrorStringWithFormat("invalid boolean value for option '%c'" , |
276 | short_option); |
277 | else { |
278 | m_step_in_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo; |
279 | } |
280 | } break; |
281 | |
282 | case 'A': { |
283 | bool success; |
284 | bool avoid_no_debug = |
285 | OptionArgParser::ToBoolean(s: option_arg, fail_value: true, success_ptr: &success); |
286 | if (!success) |
287 | error.SetErrorStringWithFormat("invalid boolean value for option '%c'" , |
288 | short_option); |
289 | else { |
290 | m_step_out_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo; |
291 | } |
292 | } break; |
293 | |
294 | case 'c': |
295 | if (option_arg.getAsInteger(0, m_step_count)) |
296 | error.SetErrorStringWithFormat("invalid step count '%s'" , |
297 | option_arg.str().c_str()); |
298 | break; |
299 | |
300 | case 'm': { |
301 | auto enum_values = GetDefinitions()[option_idx].enum_values; |
302 | m_run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum( |
303 | s: option_arg, enum_values, fail_value: eOnlyDuringStepping, error); |
304 | } break; |
305 | |
306 | case 'e': |
307 | if (option_arg == "block" ) { |
308 | m_end_line_is_block_end = true; |
309 | break; |
310 | } |
311 | if (option_arg.getAsInteger(0, m_end_line)) |
312 | error.SetErrorStringWithFormat("invalid end line number '%s'" , |
313 | option_arg.str().c_str()); |
314 | break; |
315 | |
316 | case 'r': |
317 | m_avoid_regexp.clear(); |
318 | m_avoid_regexp.assign(str: std::string(option_arg)); |
319 | break; |
320 | |
321 | case 't': |
322 | m_step_in_target.clear(); |
323 | m_step_in_target.assign(str: std::string(option_arg)); |
324 | break; |
325 | |
326 | default: |
327 | llvm_unreachable("Unimplemented option" ); |
328 | } |
329 | return error; |
330 | } |
331 | |
332 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
333 | m_step_in_avoid_no_debug = eLazyBoolCalculate; |
334 | m_step_out_avoid_no_debug = eLazyBoolCalculate; |
335 | m_run_mode = eOnlyDuringStepping; |
336 | |
337 | // Check if we are in Non-Stop mode |
338 | TargetSP target_sp = |
339 | execution_context ? execution_context->GetTargetSP() : TargetSP(); |
340 | ProcessSP process_sp = |
341 | execution_context ? execution_context->GetProcessSP() : ProcessSP(); |
342 | if (process_sp && process_sp->GetSteppingRunsAllThreads()) |
343 | m_run_mode = eAllThreads; |
344 | |
345 | m_avoid_regexp.clear(); |
346 | m_step_in_target.clear(); |
347 | m_step_count = 1; |
348 | m_end_line = LLDB_INVALID_LINE_NUMBER; |
349 | m_end_line_is_block_end = false; |
350 | } |
351 | |
352 | // Instance variables to hold the values for command options. |
353 | LazyBool m_step_in_avoid_no_debug; |
354 | LazyBool m_step_out_avoid_no_debug; |
355 | RunMode m_run_mode; |
356 | std::string m_avoid_regexp; |
357 | std::string m_step_in_target; |
358 | uint32_t m_step_count; |
359 | uint32_t m_end_line; |
360 | bool m_end_line_is_block_end; |
361 | }; |
362 | |
363 | class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed { |
364 | public: |
365 | CommandObjectThreadStepWithTypeAndScope(CommandInterpreter &interpreter, |
366 | const char *name, const char *help, |
367 | const char *syntax, |
368 | StepType step_type, |
369 | StepScope step_scope) |
370 | : CommandObjectParsed(interpreter, name, help, syntax, |
371 | eCommandRequiresProcess | eCommandRequiresThread | |
372 | eCommandTryTargetAPILock | |
373 | eCommandProcessMustBeLaunched | |
374 | eCommandProcessMustBePaused), |
375 | m_step_type(step_type), m_step_scope(step_scope), |
376 | m_class_options("scripted step" ) { |
377 | CommandArgumentEntry arg; |
378 | CommandArgumentData thread_id_arg; |
379 | |
380 | // Define the first (and only) variant of this arg. |
381 | thread_id_arg.arg_type = eArgTypeThreadID; |
382 | thread_id_arg.arg_repetition = eArgRepeatOptional; |
383 | |
384 | // There is only one variant this argument could be; put it into the |
385 | // argument entry. |
386 | arg.push_back(x: thread_id_arg); |
387 | |
388 | // Push the data for the first argument into the m_arguments vector. |
389 | m_arguments.push_back(x: arg); |
390 | |
391 | if (step_type == eStepTypeScripted) { |
392 | m_all_options.Append(group: &m_class_options, LLDB_OPT_SET_1 | LLDB_OPT_SET_2, |
393 | LLDB_OPT_SET_1); |
394 | } |
395 | m_all_options.Append(group: &m_options); |
396 | m_all_options.Finalize(); |
397 | } |
398 | |
399 | ~CommandObjectThreadStepWithTypeAndScope() override = default; |
400 | |
401 | void |
402 | HandleArgumentCompletion(CompletionRequest &request, |
403 | OptionElementVector &opt_element_vector) override { |
404 | if (request.GetCursorIndex()) |
405 | return; |
406 | |
407 | lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks( |
408 | interpreter&: GetCommandInterpreter(), completion_mask: lldb::eThreadIndexCompletion, request, |
409 | searcher: nullptr); |
410 | } |
411 | |
412 | Options *GetOptions() override { return &m_all_options; } |
413 | |
414 | protected: |
415 | void DoExecute(Args &command, CommandReturnObject &result) override { |
416 | Process *process = m_exe_ctx.GetProcessPtr(); |
417 | bool synchronous_execution = m_interpreter.GetSynchronous(); |
418 | |
419 | const uint32_t num_threads = process->GetThreadList().GetSize(); |
420 | Thread *thread = nullptr; |
421 | |
422 | if (command.GetArgumentCount() == 0) { |
423 | thread = GetDefaultThread(); |
424 | |
425 | if (thread == nullptr) { |
426 | result.AppendError(in_string: "no selected thread in process" ); |
427 | return; |
428 | } |
429 | } else { |
430 | const char *thread_idx_cstr = command.GetArgumentAtIndex(idx: 0); |
431 | uint32_t step_thread_idx; |
432 | |
433 | if (!llvm::to_integer(S: thread_idx_cstr, Num&: step_thread_idx)) { |
434 | result.AppendErrorWithFormat(format: "invalid thread index '%s'.\n" , |
435 | thread_idx_cstr); |
436 | return; |
437 | } |
438 | thread = |
439 | process->GetThreadList().FindThreadByIndexID(index_id: step_thread_idx).get(); |
440 | if (thread == nullptr) { |
441 | result.AppendErrorWithFormat( |
442 | format: "Thread index %u is out of range (valid values are 0 - %u).\n" , |
443 | step_thread_idx, num_threads); |
444 | return; |
445 | } |
446 | } |
447 | |
448 | if (m_step_type == eStepTypeScripted) { |
449 | if (m_class_options.GetName().empty()) { |
450 | result.AppendErrorWithFormat(format: "empty class name for scripted step." ); |
451 | return; |
452 | } else if (!GetDebugger().GetScriptInterpreter()->CheckObjectExists( |
453 | name: m_class_options.GetName().c_str())) { |
454 | result.AppendErrorWithFormat( |
455 | format: "class for scripted step: \"%s\" does not exist." , |
456 | m_class_options.GetName().c_str()); |
457 | return; |
458 | } |
459 | } |
460 | |
461 | if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER && |
462 | m_step_type != eStepTypeInto) { |
463 | result.AppendErrorWithFormat( |
464 | format: "end line option is only valid for step into" ); |
465 | return; |
466 | } |
467 | |
468 | const bool abort_other_plans = false; |
469 | const lldb::RunMode stop_other_threads = m_options.m_run_mode; |
470 | |
471 | // This is a bit unfortunate, but not all the commands in this command |
472 | // object support only while stepping, so I use the bool for them. |
473 | bool bool_stop_other_threads; |
474 | if (m_options.m_run_mode == eAllThreads) |
475 | bool_stop_other_threads = false; |
476 | else if (m_options.m_run_mode == eOnlyDuringStepping) |
477 | bool_stop_other_threads = (m_step_type != eStepTypeOut); |
478 | else |
479 | bool_stop_other_threads = true; |
480 | |
481 | ThreadPlanSP new_plan_sp; |
482 | Status new_plan_status; |
483 | |
484 | if (m_step_type == eStepTypeInto) { |
485 | StackFrame *frame = thread->GetStackFrameAtIndex(idx: 0).get(); |
486 | assert(frame != nullptr); |
487 | |
488 | if (frame->HasDebugInformation()) { |
489 | AddressRange range; |
490 | SymbolContext sc = frame->GetSymbolContext(resolve_scope: eSymbolContextEverything); |
491 | if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER) { |
492 | Status error; |
493 | if (!sc.GetAddressRangeFromHereToEndLine(end_line: m_options.m_end_line, range, |
494 | error)) { |
495 | result.AppendErrorWithFormat(format: "invalid end-line option: %s." , |
496 | error.AsCString()); |
497 | return; |
498 | } |
499 | } else if (m_options.m_end_line_is_block_end) { |
500 | Status error; |
501 | Block *block = frame->GetSymbolContext(resolve_scope: eSymbolContextBlock).block; |
502 | if (!block) { |
503 | result.AppendErrorWithFormat(format: "Could not find the current block." ); |
504 | return; |
505 | } |
506 | |
507 | AddressRange block_range; |
508 | Address pc_address = frame->GetFrameCodeAddress(); |
509 | block->GetRangeContainingAddress(addr: pc_address, range&: block_range); |
510 | if (!block_range.GetBaseAddress().IsValid()) { |
511 | result.AppendErrorWithFormat( |
512 | format: "Could not find the current block address." ); |
513 | return; |
514 | } |
515 | lldb::addr_t pc_offset_in_block = |
516 | pc_address.GetFileAddress() - |
517 | block_range.GetBaseAddress().GetFileAddress(); |
518 | lldb::addr_t range_length = |
519 | block_range.GetByteSize() - pc_offset_in_block; |
520 | range = AddressRange(pc_address, range_length); |
521 | } else { |
522 | range = sc.line_entry.range; |
523 | } |
524 | |
525 | new_plan_sp = thread->QueueThreadPlanForStepInRange( |
526 | abort_other_plans, range, |
527 | addr_context: frame->GetSymbolContext(resolve_scope: eSymbolContextEverything), |
528 | step_in_target: m_options.m_step_in_target.c_str(), stop_other_threads, |
529 | status&: new_plan_status, step_in_avoids_code_without_debug_info: m_options.m_step_in_avoid_no_debug, |
530 | step_out_avoids_code_without_debug_info: m_options.m_step_out_avoid_no_debug); |
531 | |
532 | if (new_plan_sp && !m_options.m_avoid_regexp.empty()) { |
533 | ThreadPlanStepInRange *step_in_range_plan = |
534 | static_cast<ThreadPlanStepInRange *>(new_plan_sp.get()); |
535 | step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str()); |
536 | } |
537 | } else |
538 | new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( |
539 | step_over: false, abort_other_plans, stop_other_threads: bool_stop_other_threads, status&: new_plan_status); |
540 | } else if (m_step_type == eStepTypeOver) { |
541 | StackFrame *frame = thread->GetStackFrameAtIndex(idx: 0).get(); |
542 | |
543 | if (frame->HasDebugInformation()) |
544 | new_plan_sp = thread->QueueThreadPlanForStepOverRange( |
545 | abort_other_plans, |
546 | line_entry: frame->GetSymbolContext(resolve_scope: eSymbolContextEverything).line_entry, |
547 | addr_context: frame->GetSymbolContext(resolve_scope: eSymbolContextEverything), |
548 | stop_other_threads, status&: new_plan_status, |
549 | step_out_avoids_code_without_debug_info: m_options.m_step_out_avoid_no_debug); |
550 | else |
551 | new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( |
552 | step_over: true, abort_other_plans, stop_other_threads: bool_stop_other_threads, status&: new_plan_status); |
553 | } else if (m_step_type == eStepTypeTrace) { |
554 | new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( |
555 | step_over: false, abort_other_plans, stop_other_threads: bool_stop_other_threads, status&: new_plan_status); |
556 | } else if (m_step_type == eStepTypeTraceOver) { |
557 | new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( |
558 | step_over: true, abort_other_plans, stop_other_threads: bool_stop_other_threads, status&: new_plan_status); |
559 | } else if (m_step_type == eStepTypeOut) { |
560 | new_plan_sp = thread->QueueThreadPlanForStepOut( |
561 | abort_other_plans, addr_context: nullptr, first_insn: false, stop_other_threads: bool_stop_other_threads, report_stop_vote: eVoteYes, |
562 | report_run_vote: eVoteNoOpinion, |
563 | frame_idx: thread->GetSelectedFrameIndex(select_most_relevant: DoNoSelectMostRelevantFrame), |
564 | status&: new_plan_status, step_out_avoids_code_without_debug_info: m_options.m_step_out_avoid_no_debug); |
565 | } else if (m_step_type == eStepTypeScripted) { |
566 | new_plan_sp = thread->QueueThreadPlanForStepScripted( |
567 | abort_other_plans, class_name: m_class_options.GetName().c_str(), |
568 | extra_args_sp: m_class_options.GetStructuredData(), stop_other_threads: bool_stop_other_threads, |
569 | status&: new_plan_status); |
570 | } else { |
571 | result.AppendError(in_string: "step type is not supported" ); |
572 | return; |
573 | } |
574 | |
575 | // If we got a new plan, then set it to be a controlling plan (User level |
576 | // Plans should be controlling plans so that they can be interruptible). |
577 | // Then resume the process. |
578 | |
579 | if (new_plan_sp) { |
580 | new_plan_sp->SetIsControllingPlan(true); |
581 | new_plan_sp->SetOkayToDiscard(false); |
582 | |
583 | if (m_options.m_step_count > 1) { |
584 | if (!new_plan_sp->SetIterationCount(m_options.m_step_count)) { |
585 | result.AppendWarning( |
586 | in_string: "step operation does not support iteration count." ); |
587 | } |
588 | } |
589 | |
590 | process->GetThreadList().SetSelectedThreadByID(tid: thread->GetID()); |
591 | |
592 | const uint32_t iohandler_id = process->GetIOHandlerID(); |
593 | |
594 | StreamString stream; |
595 | Status error; |
596 | if (synchronous_execution) |
597 | error = process->ResumeSynchronous(stream: &stream); |
598 | else |
599 | error = process->Resume(); |
600 | |
601 | if (!error.Success()) { |
602 | result.AppendMessage(in_string: error.AsCString()); |
603 | return; |
604 | } |
605 | |
606 | // There is a race condition where this thread will return up the call |
607 | // stack to the main command handler and show an (lldb) prompt before |
608 | // HandlePrivateEvent (from PrivateStateThread) has a chance to call |
609 | // PushProcessIOHandler(). |
610 | process->SyncIOHandler(iohandler_id, timeout: std::chrono::seconds(2)); |
611 | |
612 | if (synchronous_execution) { |
613 | // If any state changed events had anything to say, add that to the |
614 | // result |
615 | if (stream.GetSize() > 0) |
616 | result.AppendMessage(in_string: stream.GetString()); |
617 | |
618 | process->GetThreadList().SetSelectedThreadByID(tid: thread->GetID()); |
619 | result.SetDidChangeProcessState(true); |
620 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
621 | } else { |
622 | result.SetStatus(eReturnStatusSuccessContinuingNoResult); |
623 | } |
624 | } else { |
625 | result.SetError(error: new_plan_status); |
626 | } |
627 | } |
628 | |
629 | StepType m_step_type; |
630 | StepScope m_step_scope; |
631 | ThreadStepScopeOptionGroup m_options; |
632 | OptionGroupPythonClassWithDict m_class_options; |
633 | OptionGroupOptions m_all_options; |
634 | }; |
635 | |
636 | // CommandObjectThreadContinue |
637 | |
638 | class CommandObjectThreadContinue : public CommandObjectParsed { |
639 | public: |
640 | CommandObjectThreadContinue(CommandInterpreter &interpreter) |
641 | : CommandObjectParsed( |
642 | interpreter, "thread continue" , |
643 | "Continue execution of the current target process. One " |
644 | "or more threads may be specified, by default all " |
645 | "threads continue." , |
646 | nullptr, |
647 | eCommandRequiresThread | eCommandTryTargetAPILock | |
648 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) { |
649 | CommandArgumentEntry arg; |
650 | CommandArgumentData thread_idx_arg; |
651 | |
652 | // Define the first (and only) variant of this arg. |
653 | thread_idx_arg.arg_type = eArgTypeThreadIndex; |
654 | thread_idx_arg.arg_repetition = eArgRepeatPlus; |
655 | |
656 | // There is only one variant this argument could be; put it into the |
657 | // argument entry. |
658 | arg.push_back(x: thread_idx_arg); |
659 | |
660 | // Push the data for the first argument into the m_arguments vector. |
661 | m_arguments.push_back(x: arg); |
662 | } |
663 | |
664 | ~CommandObjectThreadContinue() override = default; |
665 | |
666 | void |
667 | HandleArgumentCompletion(CompletionRequest &request, |
668 | OptionElementVector &opt_element_vector) override { |
669 | lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks( |
670 | interpreter&: GetCommandInterpreter(), completion_mask: lldb::eThreadIndexCompletion, request, |
671 | searcher: nullptr); |
672 | } |
673 | |
674 | void DoExecute(Args &command, CommandReturnObject &result) override { |
675 | bool synchronous_execution = m_interpreter.GetSynchronous(); |
676 | |
677 | Process *process = m_exe_ctx.GetProcessPtr(); |
678 | if (process == nullptr) { |
679 | result.AppendError(in_string: "no process exists. Cannot continue" ); |
680 | return; |
681 | } |
682 | |
683 | StateType state = process->GetState(); |
684 | if ((state == eStateCrashed) || (state == eStateStopped) || |
685 | (state == eStateSuspended)) { |
686 | const size_t argc = command.GetArgumentCount(); |
687 | if (argc > 0) { |
688 | // These two lines appear at the beginning of both blocks in this |
689 | // if..else, but that is because we need to release the lock before |
690 | // calling process->Resume below. |
691 | std::lock_guard<std::recursive_mutex> guard( |
692 | process->GetThreadList().GetMutex()); |
693 | const uint32_t num_threads = process->GetThreadList().GetSize(); |
694 | std::vector<Thread *> resume_threads; |
695 | for (auto &entry : command.entries()) { |
696 | uint32_t thread_idx; |
697 | if (entry.ref().getAsInteger(Radix: 0, Result&: thread_idx)) { |
698 | result.AppendErrorWithFormat( |
699 | format: "invalid thread index argument: \"%s\".\n" , entry.c_str()); |
700 | return; |
701 | } |
702 | Thread *thread = |
703 | process->GetThreadList().FindThreadByIndexID(index_id: thread_idx).get(); |
704 | |
705 | if (thread) { |
706 | resume_threads.push_back(x: thread); |
707 | } else { |
708 | result.AppendErrorWithFormat(format: "invalid thread index %u.\n" , |
709 | thread_idx); |
710 | return; |
711 | } |
712 | } |
713 | |
714 | if (resume_threads.empty()) { |
715 | result.AppendError(in_string: "no valid thread indexes were specified" ); |
716 | return; |
717 | } else { |
718 | if (resume_threads.size() == 1) |
719 | result.AppendMessageWithFormat(format: "Resuming thread: " ); |
720 | else |
721 | result.AppendMessageWithFormat(format: "Resuming threads: " ); |
722 | |
723 | for (uint32_t idx = 0; idx < num_threads; ++idx) { |
724 | Thread *thread = |
725 | process->GetThreadList().GetThreadAtIndex(idx).get(); |
726 | std::vector<Thread *>::iterator this_thread_pos = |
727 | find(first: resume_threads.begin(), last: resume_threads.end(), val: thread); |
728 | |
729 | if (this_thread_pos != resume_threads.end()) { |
730 | resume_threads.erase(position: this_thread_pos); |
731 | if (!resume_threads.empty()) |
732 | result.AppendMessageWithFormat(format: "%u, " , thread->GetIndexID()); |
733 | else |
734 | result.AppendMessageWithFormat(format: "%u " , thread->GetIndexID()); |
735 | |
736 | const bool override_suspend = true; |
737 | thread->SetResumeState(state: eStateRunning, override_suspend); |
738 | } else { |
739 | thread->SetResumeState(state: eStateSuspended); |
740 | } |
741 | } |
742 | result.AppendMessageWithFormat(format: "in process %" PRIu64 "\n" , |
743 | process->GetID()); |
744 | } |
745 | } else { |
746 | // These two lines appear at the beginning of both blocks in this |
747 | // if..else, but that is because we need to release the lock before |
748 | // calling process->Resume below. |
749 | std::lock_guard<std::recursive_mutex> guard( |
750 | process->GetThreadList().GetMutex()); |
751 | const uint32_t num_threads = process->GetThreadList().GetSize(); |
752 | Thread *current_thread = GetDefaultThread(); |
753 | if (current_thread == nullptr) { |
754 | result.AppendError(in_string: "the process doesn't have a current thread" ); |
755 | return; |
756 | } |
757 | // Set the actions that the threads should each take when resuming |
758 | for (uint32_t idx = 0; idx < num_threads; ++idx) { |
759 | Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get(); |
760 | if (thread == current_thread) { |
761 | result.AppendMessageWithFormat(format: "Resuming thread 0x%4.4" PRIx64 |
762 | " in process %" PRIu64 "\n" , |
763 | thread->GetID(), process->GetID()); |
764 | const bool override_suspend = true; |
765 | thread->SetResumeState(state: eStateRunning, override_suspend); |
766 | } else { |
767 | thread->SetResumeState(state: eStateSuspended); |
768 | } |
769 | } |
770 | } |
771 | |
772 | StreamString stream; |
773 | Status error; |
774 | if (synchronous_execution) |
775 | error = process->ResumeSynchronous(stream: &stream); |
776 | else |
777 | error = process->Resume(); |
778 | |
779 | // We should not be holding the thread list lock when we do this. |
780 | if (error.Success()) { |
781 | result.AppendMessageWithFormat(format: "Process %" PRIu64 " resuming\n" , |
782 | process->GetID()); |
783 | if (synchronous_execution) { |
784 | // If any state changed events had anything to say, add that to the |
785 | // result |
786 | if (stream.GetSize() > 0) |
787 | result.AppendMessage(in_string: stream.GetString()); |
788 | |
789 | result.SetDidChangeProcessState(true); |
790 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
791 | } else { |
792 | result.SetStatus(eReturnStatusSuccessContinuingNoResult); |
793 | } |
794 | } else { |
795 | result.AppendErrorWithFormat(format: "Failed to resume process: %s\n" , |
796 | error.AsCString()); |
797 | } |
798 | } else { |
799 | result.AppendErrorWithFormat( |
800 | format: "Process cannot be continued from its current state (%s).\n" , |
801 | StateAsCString(state)); |
802 | } |
803 | } |
804 | }; |
805 | |
806 | // CommandObjectThreadUntil |
807 | |
808 | #define LLDB_OPTIONS_thread_until |
809 | #include "CommandOptions.inc" |
810 | |
811 | class CommandObjectThreadUntil : public CommandObjectParsed { |
812 | public: |
813 | class CommandOptions : public Options { |
814 | public: |
815 | uint32_t m_thread_idx = LLDB_INVALID_THREAD_ID; |
816 | uint32_t m_frame_idx = LLDB_INVALID_FRAME_ID; |
817 | |
818 | CommandOptions() { |
819 | // Keep default values of all options in one place: OptionParsingStarting |
820 | // () |
821 | OptionParsingStarting(execution_context: nullptr); |
822 | } |
823 | |
824 | ~CommandOptions() override = default; |
825 | |
826 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
827 | ExecutionContext *execution_context) override { |
828 | Status error; |
829 | const int short_option = m_getopt_table[option_idx].val; |
830 | |
831 | switch (short_option) { |
832 | case 'a': { |
833 | lldb::addr_t tmp_addr = OptionArgParser::ToAddress( |
834 | exe_ctx: execution_context, s: option_arg, LLDB_INVALID_ADDRESS, error_ptr: &error); |
835 | if (error.Success()) |
836 | m_until_addrs.push_back(tmp_addr); |
837 | } break; |
838 | case 't': |
839 | if (option_arg.getAsInteger(0, m_thread_idx)) { |
840 | m_thread_idx = LLDB_INVALID_INDEX32; |
841 | error.SetErrorStringWithFormat("invalid thread index '%s'" , |
842 | option_arg.str().c_str()); |
843 | } |
844 | break; |
845 | case 'f': |
846 | if (option_arg.getAsInteger(0, m_frame_idx)) { |
847 | m_frame_idx = LLDB_INVALID_FRAME_ID; |
848 | error.SetErrorStringWithFormat("invalid frame index '%s'" , |
849 | option_arg.str().c_str()); |
850 | } |
851 | break; |
852 | case 'm': { |
853 | auto enum_values = GetDefinitions()[option_idx].enum_values; |
854 | lldb::RunMode run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum( |
855 | s: option_arg, enum_values, fail_value: eOnlyDuringStepping, error); |
856 | |
857 | if (error.Success()) { |
858 | if (run_mode == eAllThreads) |
859 | m_stop_others = false; |
860 | else |
861 | m_stop_others = true; |
862 | } |
863 | } break; |
864 | default: |
865 | llvm_unreachable("Unimplemented option" ); |
866 | } |
867 | return error; |
868 | } |
869 | |
870 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
871 | m_thread_idx = LLDB_INVALID_THREAD_ID; |
872 | m_frame_idx = 0; |
873 | m_stop_others = false; |
874 | m_until_addrs.clear(); |
875 | } |
876 | |
877 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
878 | return llvm::ArrayRef(g_thread_until_options); |
879 | } |
880 | |
881 | uint32_t m_step_thread_idx = LLDB_INVALID_THREAD_ID; |
882 | bool m_stop_others = false; |
883 | std::vector<lldb::addr_t> m_until_addrs; |
884 | |
885 | // Instance variables to hold the values for command options. |
886 | }; |
887 | |
888 | CommandObjectThreadUntil(CommandInterpreter &interpreter) |
889 | : CommandObjectParsed( |
890 | interpreter, "thread until" , |
891 | "Continue until a line number or address is reached by the " |
892 | "current or specified thread. Stops when returning from " |
893 | "the current function as a safety measure. " |
894 | "The target line number(s) are given as arguments, and if more " |
895 | "than one" |
896 | " is provided, stepping will stop when the first one is hit." , |
897 | nullptr, |
898 | eCommandRequiresThread | eCommandTryTargetAPILock | |
899 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) { |
900 | CommandArgumentEntry arg; |
901 | CommandArgumentData line_num_arg; |
902 | |
903 | // Define the first (and only) variant of this arg. |
904 | line_num_arg.arg_type = eArgTypeLineNum; |
905 | line_num_arg.arg_repetition = eArgRepeatPlain; |
906 | |
907 | // There is only one variant this argument could be; put it into the |
908 | // argument entry. |
909 | arg.push_back(x: line_num_arg); |
910 | |
911 | // Push the data for the first argument into the m_arguments vector. |
912 | m_arguments.push_back(x: arg); |
913 | } |
914 | |
915 | ~CommandObjectThreadUntil() override = default; |
916 | |
917 | Options *GetOptions() override { return &m_options; } |
918 | |
919 | protected: |
920 | void DoExecute(Args &command, CommandReturnObject &result) override { |
921 | bool synchronous_execution = m_interpreter.GetSynchronous(); |
922 | |
923 | Target *target = &GetSelectedTarget(); |
924 | |
925 | Process *process = m_exe_ctx.GetProcessPtr(); |
926 | if (process == nullptr) { |
927 | result.AppendError(in_string: "need a valid process to step" ); |
928 | } else { |
929 | Thread *thread = nullptr; |
930 | std::vector<uint32_t> line_numbers; |
931 | |
932 | if (command.GetArgumentCount() >= 1) { |
933 | size_t num_args = command.GetArgumentCount(); |
934 | for (size_t i = 0; i < num_args; i++) { |
935 | uint32_t line_number; |
936 | if (!llvm::to_integer(command.GetArgumentAtIndex(idx: i), line_number)) { |
937 | result.AppendErrorWithFormat(format: "invalid line number: '%s'.\n" , |
938 | command.GetArgumentAtIndex(idx: i)); |
939 | return; |
940 | } else |
941 | line_numbers.push_back(line_number); |
942 | } |
943 | } else if (m_options.m_until_addrs.empty()) { |
944 | result.AppendErrorWithFormat(format: "No line number or address provided:\n%s" , |
945 | GetSyntax().str().c_str()); |
946 | return; |
947 | } |
948 | |
949 | if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID) { |
950 | thread = GetDefaultThread(); |
951 | } else { |
952 | thread = process->GetThreadList() |
953 | .FindThreadByIndexID(m_options.m_thread_idx) |
954 | .get(); |
955 | } |
956 | |
957 | if (thread == nullptr) { |
958 | const uint32_t num_threads = process->GetThreadList().GetSize(); |
959 | result.AppendErrorWithFormat( |
960 | "Thread index %u is out of range (valid values are 0 - %u).\n" , |
961 | m_options.m_thread_idx, num_threads); |
962 | return; |
963 | } |
964 | |
965 | const bool abort_other_plans = false; |
966 | |
967 | StackFrame *frame = |
968 | thread->GetStackFrameAtIndex(m_options.m_frame_idx).get(); |
969 | if (frame == nullptr) { |
970 | result.AppendErrorWithFormat( |
971 | "Frame index %u is out of range for thread id %" PRIu64 ".\n" , |
972 | m_options.m_frame_idx, thread->GetID()); |
973 | return; |
974 | } |
975 | |
976 | ThreadPlanSP new_plan_sp; |
977 | Status new_plan_status; |
978 | |
979 | if (frame->HasDebugInformation()) { |
980 | // Finally we got here... Translate the given line number to a bunch |
981 | // of addresses: |
982 | SymbolContext sc(frame->GetSymbolContext(resolve_scope: eSymbolContextCompUnit)); |
983 | LineTable *line_table = nullptr; |
984 | if (sc.comp_unit) |
985 | line_table = sc.comp_unit->GetLineTable(); |
986 | |
987 | if (line_table == nullptr) { |
988 | result.AppendErrorWithFormat("Failed to resolve the line table for " |
989 | "frame %u of thread id %" PRIu64 ".\n" , |
990 | m_options.m_frame_idx, thread->GetID()); |
991 | return; |
992 | } |
993 | |
994 | LineEntry function_start; |
995 | uint32_t index_ptr = 0, end_ptr = UINT32_MAX; |
996 | std::vector<addr_t> address_list; |
997 | |
998 | // Find the beginning & end index of the function, but first make |
999 | // sure it is valid: |
1000 | if (!sc.function) { |
1001 | result.AppendErrorWithFormat(format: "Have debug information but no " |
1002 | "function info - can't get until range." ); |
1003 | return; |
1004 | } |
1005 | |
1006 | AddressRange fun_addr_range = sc.function->GetAddressRange(); |
1007 | Address fun_start_addr = fun_addr_range.GetBaseAddress(); |
1008 | line_table->FindLineEntryByAddress(so_addr: fun_start_addr, line_entry&: function_start, |
1009 | index_ptr: &index_ptr); |
1010 | |
1011 | Address fun_end_addr(fun_start_addr.GetSection(), |
1012 | fun_start_addr.GetOffset() + |
1013 | fun_addr_range.GetByteSize()); |
1014 | |
1015 | bool all_in_function = true; |
1016 | |
1017 | line_table->FindLineEntryByAddress(so_addr: fun_end_addr, line_entry&: function_start, |
1018 | index_ptr: &end_ptr); |
1019 | |
1020 | // Since not all source lines will contribute code, check if we are |
1021 | // setting the breakpoint on the exact line number or the nearest |
1022 | // subsequent line number and set breakpoints at all the line table |
1023 | // entries of the chosen line number (exact or nearest subsequent). |
1024 | for (uint32_t line_number : line_numbers) { |
1025 | LineEntry line_entry; |
1026 | bool exact = false; |
1027 | uint32_t start_idx_ptr = index_ptr; |
1028 | start_idx_ptr = sc.comp_unit->FindLineEntry( |
1029 | index_ptr, line_number, nullptr, exact, &line_entry); |
1030 | if (start_idx_ptr != UINT32_MAX) |
1031 | line_number = line_entry.line; |
1032 | exact = true; |
1033 | start_idx_ptr = index_ptr; |
1034 | while (start_idx_ptr <= end_ptr) { |
1035 | start_idx_ptr = sc.comp_unit->FindLineEntry( |
1036 | start_idx_ptr, line_number, nullptr, exact, &line_entry); |
1037 | if (start_idx_ptr == UINT32_MAX) |
1038 | break; |
1039 | |
1040 | addr_t address = |
1041 | line_entry.range.GetBaseAddress().GetLoadAddress(target); |
1042 | if (address != LLDB_INVALID_ADDRESS) { |
1043 | if (fun_addr_range.ContainsLoadAddress(address, target)) |
1044 | address_list.push_back(address); |
1045 | else |
1046 | all_in_function = false; |
1047 | } |
1048 | start_idx_ptr++; |
1049 | } |
1050 | } |
1051 | |
1052 | for (lldb::addr_t address : m_options.m_until_addrs) { |
1053 | if (fun_addr_range.ContainsLoadAddress(address, target)) |
1054 | address_list.push_back(address); |
1055 | else |
1056 | all_in_function = false; |
1057 | } |
1058 | |
1059 | if (address_list.empty()) { |
1060 | if (all_in_function) |
1061 | result.AppendErrorWithFormat( |
1062 | format: "No line entries matching until target.\n" ); |
1063 | else |
1064 | result.AppendErrorWithFormat( |
1065 | format: "Until target outside of the current function.\n" ); |
1066 | |
1067 | return; |
1068 | } |
1069 | |
1070 | new_plan_sp = thread->QueueThreadPlanForStepUntil( |
1071 | abort_other_plans, &address_list.front(), address_list.size(), |
1072 | m_options.m_stop_others, m_options.m_frame_idx, new_plan_status); |
1073 | if (new_plan_sp) { |
1074 | // User level plans should be controlling plans so they can be |
1075 | // interrupted |
1076 | // (e.g. by hitting a breakpoint) and other plans executed by the |
1077 | // user (stepping around the breakpoint) and then a "continue" will |
1078 | // resume the original plan. |
1079 | new_plan_sp->SetIsControllingPlan(true); |
1080 | new_plan_sp->SetOkayToDiscard(false); |
1081 | } else { |
1082 | result.SetError(error: new_plan_status); |
1083 | return; |
1084 | } |
1085 | } else { |
1086 | result.AppendErrorWithFormat("Frame index %u of thread id %" PRIu64 |
1087 | " has no debug information.\n" , |
1088 | m_options.m_frame_idx, thread->GetID()); |
1089 | return; |
1090 | } |
1091 | |
1092 | if (!process->GetThreadList().SetSelectedThreadByID(tid: thread->GetID())) { |
1093 | result.AppendErrorWithFormat( |
1094 | format: "Failed to set the selected thread to thread id %" PRIu64 ".\n" , |
1095 | thread->GetID()); |
1096 | return; |
1097 | } |
1098 | |
1099 | StreamString stream; |
1100 | Status error; |
1101 | if (synchronous_execution) |
1102 | error = process->ResumeSynchronous(stream: &stream); |
1103 | else |
1104 | error = process->Resume(); |
1105 | |
1106 | if (error.Success()) { |
1107 | result.AppendMessageWithFormat(format: "Process %" PRIu64 " resuming\n" , |
1108 | process->GetID()); |
1109 | if (synchronous_execution) { |
1110 | // If any state changed events had anything to say, add that to the |
1111 | // result |
1112 | if (stream.GetSize() > 0) |
1113 | result.AppendMessage(in_string: stream.GetString()); |
1114 | |
1115 | result.SetDidChangeProcessState(true); |
1116 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
1117 | } else { |
1118 | result.SetStatus(eReturnStatusSuccessContinuingNoResult); |
1119 | } |
1120 | } else { |
1121 | result.AppendErrorWithFormat(format: "Failed to resume process: %s.\n" , |
1122 | error.AsCString()); |
1123 | } |
1124 | } |
1125 | } |
1126 | |
1127 | CommandOptions m_options; |
1128 | }; |
1129 | |
1130 | // CommandObjectThreadSelect |
1131 | |
1132 | #define LLDB_OPTIONS_thread_select |
1133 | #include "CommandOptions.inc" |
1134 | |
1135 | class CommandObjectThreadSelect : public CommandObjectParsed { |
1136 | public: |
1137 | class OptionGroupThreadSelect : public OptionGroup { |
1138 | public: |
1139 | OptionGroupThreadSelect() { OptionParsingStarting(execution_context: nullptr); } |
1140 | |
1141 | ~OptionGroupThreadSelect() override = default; |
1142 | |
1143 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
1144 | m_thread_id = LLDB_INVALID_THREAD_ID; |
1145 | } |
1146 | |
1147 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
1148 | ExecutionContext *execution_context) override { |
1149 | const int short_option = g_thread_select_options[option_idx].short_option; |
1150 | switch (short_option) { |
1151 | case 't': { |
1152 | if (option_arg.getAsInteger(Radix: 0, Result&: m_thread_id)) { |
1153 | m_thread_id = LLDB_INVALID_THREAD_ID; |
1154 | return Status("Invalid thread ID: '%s'." , option_arg.str().c_str()); |
1155 | } |
1156 | break; |
1157 | } |
1158 | |
1159 | default: |
1160 | llvm_unreachable("Unimplemented option" ); |
1161 | } |
1162 | |
1163 | return {}; |
1164 | } |
1165 | |
1166 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
1167 | return llvm::ArrayRef(g_thread_select_options); |
1168 | } |
1169 | |
1170 | lldb::tid_t m_thread_id; |
1171 | }; |
1172 | |
1173 | CommandObjectThreadSelect(CommandInterpreter &interpreter) |
1174 | : CommandObjectParsed(interpreter, "thread select" , |
1175 | "Change the currently selected thread." , |
1176 | "thread select <thread-index> (or -t <thread-id>)" , |
1177 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
1178 | eCommandProcessMustBeLaunched | |
1179 | eCommandProcessMustBePaused) { |
1180 | CommandArgumentEntry arg; |
1181 | CommandArgumentData thread_idx_arg; |
1182 | |
1183 | // Define the first (and only) variant of this arg. |
1184 | thread_idx_arg.arg_type = eArgTypeThreadIndex; |
1185 | thread_idx_arg.arg_repetition = eArgRepeatPlain; |
1186 | thread_idx_arg.arg_opt_set_association = LLDB_OPT_SET_1; |
1187 | |
1188 | // There is only one variant this argument could be; put it into the |
1189 | // argument entry. |
1190 | arg.push_back(x: thread_idx_arg); |
1191 | |
1192 | // Push the data for the first argument into the m_arguments vector. |
1193 | m_arguments.push_back(x: arg); |
1194 | |
1195 | m_option_group.Append(group: &m_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_2); |
1196 | m_option_group.Finalize(); |
1197 | } |
1198 | |
1199 | ~CommandObjectThreadSelect() override = default; |
1200 | |
1201 | void |
1202 | HandleArgumentCompletion(CompletionRequest &request, |
1203 | OptionElementVector &opt_element_vector) override { |
1204 | if (request.GetCursorIndex()) |
1205 | return; |
1206 | |
1207 | lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks( |
1208 | interpreter&: GetCommandInterpreter(), completion_mask: lldb::eThreadIndexCompletion, request, |
1209 | searcher: nullptr); |
1210 | } |
1211 | |
1212 | Options *GetOptions() override { return &m_option_group; } |
1213 | |
1214 | protected: |
1215 | void DoExecute(Args &command, CommandReturnObject &result) override { |
1216 | Process *process = m_exe_ctx.GetProcessPtr(); |
1217 | if (process == nullptr) { |
1218 | result.AppendError(in_string: "no process" ); |
1219 | return; |
1220 | } else if (m_options.m_thread_id == LLDB_INVALID_THREAD_ID && |
1221 | command.GetArgumentCount() != 1) { |
1222 | result.AppendErrorWithFormat( |
1223 | format: "'%s' takes exactly one thread index argument, or a thread ID " |
1224 | "option:\nUsage: %s\n" , |
1225 | m_cmd_name.c_str(), m_cmd_syntax.c_str()); |
1226 | return; |
1227 | } else if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID && |
1228 | command.GetArgumentCount() != 0) { |
1229 | result.AppendErrorWithFormat(format: "'%s' cannot take both a thread ID option " |
1230 | "and a thread index argument:\nUsage: %s\n" , |
1231 | m_cmd_name.c_str(), m_cmd_syntax.c_str()); |
1232 | return; |
1233 | } |
1234 | |
1235 | Thread *new_thread = nullptr; |
1236 | if (command.GetArgumentCount() == 1) { |
1237 | uint32_t index_id; |
1238 | if (!llvm::to_integer(S: command.GetArgumentAtIndex(idx: 0), Num&: index_id)) { |
1239 | result.AppendErrorWithFormat(format: "Invalid thread index '%s'" , |
1240 | command.GetArgumentAtIndex(idx: 0)); |
1241 | return; |
1242 | } |
1243 | new_thread = process->GetThreadList().FindThreadByIndexID(index_id).get(); |
1244 | if (new_thread == nullptr) { |
1245 | result.AppendErrorWithFormat(format: "Invalid thread index #%s.\n" , |
1246 | command.GetArgumentAtIndex(idx: 0)); |
1247 | return; |
1248 | } |
1249 | } else { |
1250 | new_thread = |
1251 | process->GetThreadList().FindThreadByID(tid: m_options.m_thread_id).get(); |
1252 | if (new_thread == nullptr) { |
1253 | result.AppendErrorWithFormat(format: "Invalid thread ID %" PRIu64 ".\n" , |
1254 | m_options.m_thread_id); |
1255 | return; |
1256 | } |
1257 | } |
1258 | |
1259 | process->GetThreadList().SetSelectedThreadByID(tid: new_thread->GetID(), notify: true); |
1260 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
1261 | } |
1262 | |
1263 | OptionGroupThreadSelect m_options; |
1264 | OptionGroupOptions m_option_group; |
1265 | }; |
1266 | |
1267 | // CommandObjectThreadList |
1268 | |
1269 | class CommandObjectThreadList : public CommandObjectParsed { |
1270 | public: |
1271 | CommandObjectThreadList(CommandInterpreter &interpreter) |
1272 | : CommandObjectParsed( |
1273 | interpreter, "thread list" , |
1274 | "Show a summary of each thread in the current target process. " |
1275 | "Use 'settings set thread-format' to customize the individual " |
1276 | "thread listings." , |
1277 | "thread list" , |
1278 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
1279 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} |
1280 | |
1281 | ~CommandObjectThreadList() override = default; |
1282 | |
1283 | protected: |
1284 | void DoExecute(Args &command, CommandReturnObject &result) override { |
1285 | Stream &strm = result.GetOutputStream(); |
1286 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
1287 | Process *process = m_exe_ctx.GetProcessPtr(); |
1288 | const bool only_threads_with_stop_reason = false; |
1289 | const uint32_t start_frame = 0; |
1290 | const uint32_t num_frames = 0; |
1291 | const uint32_t num_frames_with_source = 0; |
1292 | process->GetStatus(ostrm&: strm); |
1293 | process->GetThreadStatus(ostrm&: strm, only_threads_with_stop_reason, start_frame, |
1294 | num_frames, num_frames_with_source, stop_format: false); |
1295 | } |
1296 | }; |
1297 | |
1298 | // CommandObjectThreadInfo |
1299 | #define LLDB_OPTIONS_thread_info |
1300 | #include "CommandOptions.inc" |
1301 | |
1302 | class CommandObjectThreadInfo : public CommandObjectIterateOverThreads { |
1303 | public: |
1304 | class CommandOptions : public Options { |
1305 | public: |
1306 | CommandOptions() { OptionParsingStarting(execution_context: nullptr); } |
1307 | |
1308 | ~CommandOptions() override = default; |
1309 | |
1310 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
1311 | m_json_thread = false; |
1312 | m_json_stopinfo = false; |
1313 | } |
1314 | |
1315 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
1316 | ExecutionContext *execution_context) override { |
1317 | const int short_option = m_getopt_table[option_idx].val; |
1318 | Status error; |
1319 | |
1320 | switch (short_option) { |
1321 | case 'j': |
1322 | m_json_thread = true; |
1323 | break; |
1324 | |
1325 | case 's': |
1326 | m_json_stopinfo = true; |
1327 | break; |
1328 | |
1329 | default: |
1330 | llvm_unreachable("Unimplemented option" ); |
1331 | } |
1332 | return error; |
1333 | } |
1334 | |
1335 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
1336 | return llvm::ArrayRef(g_thread_info_options); |
1337 | } |
1338 | |
1339 | bool m_json_thread; |
1340 | bool m_json_stopinfo; |
1341 | }; |
1342 | |
1343 | CommandObjectThreadInfo(CommandInterpreter &interpreter) |
1344 | : CommandObjectIterateOverThreads( |
1345 | interpreter, "thread info" , |
1346 | "Show an extended summary of one or " |
1347 | "more threads. Defaults to the " |
1348 | "current thread." , |
1349 | "thread info" , |
1350 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
1351 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) { |
1352 | m_add_return = false; |
1353 | } |
1354 | |
1355 | ~CommandObjectThreadInfo() override = default; |
1356 | |
1357 | void |
1358 | HandleArgumentCompletion(CompletionRequest &request, |
1359 | OptionElementVector &opt_element_vector) override { |
1360 | lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks( |
1361 | interpreter&: GetCommandInterpreter(), completion_mask: lldb::eThreadIndexCompletion, request, |
1362 | searcher: nullptr); |
1363 | } |
1364 | |
1365 | Options *GetOptions() override { return &m_options; } |
1366 | |
1367 | bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { |
1368 | ThreadSP thread_sp = |
1369 | m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); |
1370 | if (!thread_sp) { |
1371 | result.AppendErrorWithFormat(format: "thread no longer exists: 0x%" PRIx64 "\n" , |
1372 | tid); |
1373 | return false; |
1374 | } |
1375 | |
1376 | Thread *thread = thread_sp.get(); |
1377 | |
1378 | Stream &strm = result.GetOutputStream(); |
1379 | if (!thread->GetDescription(s&: strm, level: eDescriptionLevelFull, |
1380 | print_json_thread: m_options.m_json_thread, |
1381 | print_json_stopinfo: m_options.m_json_stopinfo)) { |
1382 | result.AppendErrorWithFormat(format: "error displaying info for thread: \"%d\"\n" , |
1383 | thread->GetIndexID()); |
1384 | return false; |
1385 | } |
1386 | return true; |
1387 | } |
1388 | |
1389 | CommandOptions m_options; |
1390 | }; |
1391 | |
1392 | // CommandObjectThreadException |
1393 | |
1394 | class CommandObjectThreadException : public CommandObjectIterateOverThreads { |
1395 | public: |
1396 | CommandObjectThreadException(CommandInterpreter &interpreter) |
1397 | : CommandObjectIterateOverThreads( |
1398 | interpreter, "thread exception" , |
1399 | "Display the current exception object for a thread. Defaults to " |
1400 | "the current thread." , |
1401 | "thread exception" , |
1402 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
1403 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} |
1404 | |
1405 | ~CommandObjectThreadException() override = default; |
1406 | |
1407 | void |
1408 | HandleArgumentCompletion(CompletionRequest &request, |
1409 | OptionElementVector &opt_element_vector) override { |
1410 | lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks( |
1411 | interpreter&: GetCommandInterpreter(), completion_mask: lldb::eThreadIndexCompletion, request, |
1412 | searcher: nullptr); |
1413 | } |
1414 | |
1415 | bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { |
1416 | ThreadSP thread_sp = |
1417 | m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); |
1418 | if (!thread_sp) { |
1419 | result.AppendErrorWithFormat(format: "thread no longer exists: 0x%" PRIx64 "\n" , |
1420 | tid); |
1421 | return false; |
1422 | } |
1423 | |
1424 | Stream &strm = result.GetOutputStream(); |
1425 | ValueObjectSP exception_object_sp = thread_sp->GetCurrentException(); |
1426 | if (exception_object_sp) { |
1427 | exception_object_sp->Dump(s&: strm); |
1428 | } |
1429 | |
1430 | ThreadSP exception_thread_sp = thread_sp->GetCurrentExceptionBacktrace(); |
1431 | if (exception_thread_sp && exception_thread_sp->IsValid()) { |
1432 | const uint32_t num_frames_with_source = 0; |
1433 | const bool stop_format = false; |
1434 | exception_thread_sp->GetStatus(strm, start_frame: 0, UINT32_MAX, |
1435 | num_frames_with_source, stop_format); |
1436 | } |
1437 | |
1438 | return true; |
1439 | } |
1440 | }; |
1441 | |
1442 | class CommandObjectThreadSiginfo : public CommandObjectIterateOverThreads { |
1443 | public: |
1444 | CommandObjectThreadSiginfo(CommandInterpreter &interpreter) |
1445 | : CommandObjectIterateOverThreads( |
1446 | interpreter, "thread siginfo" , |
1447 | "Display the current siginfo object for a thread. Defaults to " |
1448 | "the current thread." , |
1449 | "thread siginfo" , |
1450 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
1451 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} |
1452 | |
1453 | ~CommandObjectThreadSiginfo() override = default; |
1454 | |
1455 | void |
1456 | HandleArgumentCompletion(CompletionRequest &request, |
1457 | OptionElementVector &opt_element_vector) override { |
1458 | lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks( |
1459 | interpreter&: GetCommandInterpreter(), completion_mask: lldb::eThreadIndexCompletion, request, |
1460 | searcher: nullptr); |
1461 | } |
1462 | |
1463 | bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { |
1464 | ThreadSP thread_sp = |
1465 | m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); |
1466 | if (!thread_sp) { |
1467 | result.AppendErrorWithFormat(format: "thread no longer exists: 0x%" PRIx64 "\n" , |
1468 | tid); |
1469 | return false; |
1470 | } |
1471 | |
1472 | Stream &strm = result.GetOutputStream(); |
1473 | if (!thread_sp->GetDescription(s&: strm, level: eDescriptionLevelFull, print_json_thread: false, print_json_stopinfo: false)) { |
1474 | result.AppendErrorWithFormat(format: "error displaying info for thread: \"%d\"\n" , |
1475 | thread_sp->GetIndexID()); |
1476 | return false; |
1477 | } |
1478 | ValueObjectSP exception_object_sp = thread_sp->GetSiginfoValue(); |
1479 | if (exception_object_sp) |
1480 | exception_object_sp->Dump(s&: strm); |
1481 | else |
1482 | strm.Printf(format: "(no siginfo)\n" ); |
1483 | strm.PutChar(ch: '\n'); |
1484 | |
1485 | return true; |
1486 | } |
1487 | }; |
1488 | |
1489 | // CommandObjectThreadReturn |
1490 | #define LLDB_OPTIONS_thread_return |
1491 | #include "CommandOptions.inc" |
1492 | |
1493 | class CommandObjectThreadReturn : public CommandObjectRaw { |
1494 | public: |
1495 | class CommandOptions : public Options { |
1496 | public: |
1497 | CommandOptions() { |
1498 | // Keep default values of all options in one place: OptionParsingStarting |
1499 | // () |
1500 | OptionParsingStarting(execution_context: nullptr); |
1501 | } |
1502 | |
1503 | ~CommandOptions() override = default; |
1504 | |
1505 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
1506 | ExecutionContext *execution_context) override { |
1507 | Status error; |
1508 | const int short_option = m_getopt_table[option_idx].val; |
1509 | |
1510 | switch (short_option) { |
1511 | case 'x': { |
1512 | bool success; |
1513 | bool tmp_value = |
1514 | OptionArgParser::ToBoolean(s: option_arg, fail_value: false, success_ptr: &success); |
1515 | if (success) |
1516 | m_from_expression = tmp_value; |
1517 | else { |
1518 | error.SetErrorStringWithFormat( |
1519 | "invalid boolean value '%s' for 'x' option" , |
1520 | option_arg.str().c_str()); |
1521 | } |
1522 | } break; |
1523 | default: |
1524 | llvm_unreachable("Unimplemented option" ); |
1525 | } |
1526 | return error; |
1527 | } |
1528 | |
1529 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
1530 | m_from_expression = false; |
1531 | } |
1532 | |
1533 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
1534 | return llvm::ArrayRef(g_thread_return_options); |
1535 | } |
1536 | |
1537 | bool m_from_expression = false; |
1538 | |
1539 | // Instance variables to hold the values for command options. |
1540 | }; |
1541 | |
1542 | CommandObjectThreadReturn(CommandInterpreter &interpreter) |
1543 | : CommandObjectRaw(interpreter, "thread return" , |
1544 | "Prematurely return from a stack frame, " |
1545 | "short-circuiting execution of newer frames " |
1546 | "and optionally yielding a specified value. Defaults " |
1547 | "to the exiting the current stack " |
1548 | "frame." , |
1549 | "thread return" , |
1550 | eCommandRequiresFrame | eCommandTryTargetAPILock | |
1551 | eCommandProcessMustBeLaunched | |
1552 | eCommandProcessMustBePaused) { |
1553 | CommandArgumentEntry arg; |
1554 | CommandArgumentData expression_arg; |
1555 | |
1556 | // Define the first (and only) variant of this arg. |
1557 | expression_arg.arg_type = eArgTypeExpression; |
1558 | expression_arg.arg_repetition = eArgRepeatOptional; |
1559 | |
1560 | // There is only one variant this argument could be; put it into the |
1561 | // argument entry. |
1562 | arg.push_back(x: expression_arg); |
1563 | |
1564 | // Push the data for the first argument into the m_arguments vector. |
1565 | m_arguments.push_back(x: arg); |
1566 | } |
1567 | |
1568 | ~CommandObjectThreadReturn() override = default; |
1569 | |
1570 | Options *GetOptions() override { return &m_options; } |
1571 | |
1572 | protected: |
1573 | void DoExecute(llvm::StringRef command, |
1574 | CommandReturnObject &result) override { |
1575 | // I am going to handle this by hand, because I don't want you to have to |
1576 | // say: |
1577 | // "thread return -- -5". |
1578 | if (command.starts_with(Prefix: "-x" )) { |
1579 | if (command.size() != 2U) |
1580 | result.AppendWarning(in_string: "Return values ignored when returning from user " |
1581 | "called expressions" ); |
1582 | |
1583 | Thread *thread = m_exe_ctx.GetThreadPtr(); |
1584 | Status error; |
1585 | error = thread->UnwindInnermostExpression(); |
1586 | if (!error.Success()) { |
1587 | result.AppendErrorWithFormat(format: "Unwinding expression failed - %s." , |
1588 | error.AsCString()); |
1589 | } else { |
1590 | bool success = |
1591 | thread->SetSelectedFrameByIndexNoisily(frame_idx: 0, output_stream&: result.GetOutputStream()); |
1592 | if (success) { |
1593 | m_exe_ctx.SetFrameSP( |
1594 | thread->GetSelectedFrame(select_most_relevant: DoNoSelectMostRelevantFrame)); |
1595 | result.SetStatus(eReturnStatusSuccessFinishResult); |
1596 | } else { |
1597 | result.AppendErrorWithFormat( |
1598 | format: "Could not select 0th frame after unwinding expression." ); |
1599 | } |
1600 | } |
1601 | return; |
1602 | } |
1603 | |
1604 | ValueObjectSP return_valobj_sp; |
1605 | |
1606 | StackFrameSP frame_sp = m_exe_ctx.GetFrameSP(); |
1607 | uint32_t frame_idx = frame_sp->GetFrameIndex(); |
1608 | |
1609 | if (frame_sp->IsInlined()) { |
1610 | result.AppendError(in_string: "Don't know how to return from inlined frames." ); |
1611 | return; |
1612 | } |
1613 | |
1614 | if (!command.empty()) { |
1615 | Target *target = m_exe_ctx.GetTargetPtr(); |
1616 | EvaluateExpressionOptions options; |
1617 | |
1618 | options.SetUnwindOnError(true); |
1619 | options.SetUseDynamic(eNoDynamicValues); |
1620 | |
1621 | ExpressionResults exe_results = eExpressionSetupError; |
1622 | exe_results = target->EvaluateExpression(expression: command, exe_scope: frame_sp.get(), |
1623 | result_valobj_sp&: return_valobj_sp, options); |
1624 | if (exe_results != eExpressionCompleted) { |
1625 | if (return_valobj_sp) |
1626 | result.AppendErrorWithFormat( |
1627 | format: "Error evaluating result expression: %s" , |
1628 | return_valobj_sp->GetError().AsCString()); |
1629 | else |
1630 | result.AppendErrorWithFormat( |
1631 | format: "Unknown error evaluating result expression." ); |
1632 | return; |
1633 | } |
1634 | } |
1635 | |
1636 | Status error; |
1637 | ThreadSP thread_sp = m_exe_ctx.GetThreadSP(); |
1638 | const bool broadcast = true; |
1639 | error = thread_sp->ReturnFromFrame(frame_sp, return_value_sp: return_valobj_sp, broadcast); |
1640 | if (!error.Success()) { |
1641 | result.AppendErrorWithFormat( |
1642 | format: "Error returning from frame %d of thread %d: %s." , frame_idx, |
1643 | thread_sp->GetIndexID(), error.AsCString()); |
1644 | return; |
1645 | } |
1646 | |
1647 | result.SetStatus(eReturnStatusSuccessFinishResult); |
1648 | } |
1649 | |
1650 | CommandOptions m_options; |
1651 | }; |
1652 | |
1653 | // CommandObjectThreadJump |
1654 | #define LLDB_OPTIONS_thread_jump |
1655 | #include "CommandOptions.inc" |
1656 | |
1657 | class CommandObjectThreadJump : public CommandObjectParsed { |
1658 | public: |
1659 | class CommandOptions : public Options { |
1660 | public: |
1661 | CommandOptions() { OptionParsingStarting(execution_context: nullptr); } |
1662 | |
1663 | ~CommandOptions() override = default; |
1664 | |
1665 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
1666 | m_filenames.Clear(); |
1667 | m_line_num = 0; |
1668 | m_line_offset = 0; |
1669 | m_load_addr = LLDB_INVALID_ADDRESS; |
1670 | m_force = false; |
1671 | } |
1672 | |
1673 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
1674 | ExecutionContext *execution_context) override { |
1675 | const int short_option = m_getopt_table[option_idx].val; |
1676 | Status error; |
1677 | |
1678 | switch (short_option) { |
1679 | case 'f': |
1680 | m_filenames.AppendIfUnique(file: FileSpec(option_arg)); |
1681 | if (m_filenames.GetSize() > 1) |
1682 | return Status("only one source file expected." ); |
1683 | break; |
1684 | case 'l': |
1685 | if (option_arg.getAsInteger(Radix: 0, Result&: m_line_num)) |
1686 | return Status("invalid line number: '%s'." , option_arg.str().c_str()); |
1687 | break; |
1688 | case 'b': |
1689 | if (option_arg.getAsInteger(Radix: 0, Result&: m_line_offset)) |
1690 | return Status("invalid line offset: '%s'." , option_arg.str().c_str()); |
1691 | break; |
1692 | case 'a': |
1693 | m_load_addr = OptionArgParser::ToAddress(exe_ctx: execution_context, s: option_arg, |
1694 | LLDB_INVALID_ADDRESS, error_ptr: &error); |
1695 | break; |
1696 | case 'r': |
1697 | m_force = true; |
1698 | break; |
1699 | default: |
1700 | llvm_unreachable("Unimplemented option" ); |
1701 | } |
1702 | return error; |
1703 | } |
1704 | |
1705 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
1706 | return llvm::ArrayRef(g_thread_jump_options); |
1707 | } |
1708 | |
1709 | FileSpecList m_filenames; |
1710 | uint32_t m_line_num; |
1711 | int32_t m_line_offset; |
1712 | lldb::addr_t m_load_addr; |
1713 | bool m_force; |
1714 | }; |
1715 | |
1716 | CommandObjectThreadJump(CommandInterpreter &interpreter) |
1717 | : CommandObjectParsed( |
1718 | interpreter, "thread jump" , |
1719 | "Sets the program counter to a new address." , "thread jump" , |
1720 | eCommandRequiresFrame | eCommandTryTargetAPILock | |
1721 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} |
1722 | |
1723 | ~CommandObjectThreadJump() override = default; |
1724 | |
1725 | Options *GetOptions() override { return &m_options; } |
1726 | |
1727 | protected: |
1728 | void DoExecute(Args &args, CommandReturnObject &result) override { |
1729 | RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext(); |
1730 | StackFrame *frame = m_exe_ctx.GetFramePtr(); |
1731 | Thread *thread = m_exe_ctx.GetThreadPtr(); |
1732 | Target *target = m_exe_ctx.GetTargetPtr(); |
1733 | const SymbolContext &sym_ctx = |
1734 | frame->GetSymbolContext(resolve_scope: eSymbolContextLineEntry); |
1735 | |
1736 | if (m_options.m_load_addr != LLDB_INVALID_ADDRESS) { |
1737 | // Use this address directly. |
1738 | Address dest = Address(m_options.m_load_addr); |
1739 | |
1740 | lldb::addr_t callAddr = dest.GetCallableLoadAddress(target); |
1741 | if (callAddr == LLDB_INVALID_ADDRESS) { |
1742 | result.AppendErrorWithFormat(format: "Invalid destination address." ); |
1743 | return; |
1744 | } |
1745 | |
1746 | if (!reg_ctx->SetPC(callAddr)) { |
1747 | result.AppendErrorWithFormat(format: "Error changing PC value for thread %d." , |
1748 | thread->GetIndexID()); |
1749 | return; |
1750 | } |
1751 | } else { |
1752 | // Pick either the absolute line, or work out a relative one. |
1753 | int32_t line = (int32_t)m_options.m_line_num; |
1754 | if (line == 0) |
1755 | line = sym_ctx.line_entry.line + m_options.m_line_offset; |
1756 | |
1757 | // Try the current file, but override if asked. |
1758 | FileSpec file = sym_ctx.line_entry.file; |
1759 | if (m_options.m_filenames.GetSize() == 1) |
1760 | file = m_options.m_filenames.GetFileSpecAtIndex(idx: 0); |
1761 | |
1762 | if (!file) { |
1763 | result.AppendErrorWithFormat( |
1764 | format: "No source file available for the current location." ); |
1765 | return; |
1766 | } |
1767 | |
1768 | std::string warnings; |
1769 | Status err = thread->JumpToLine(file, line, can_leave_function: m_options.m_force, warnings: &warnings); |
1770 | |
1771 | if (err.Fail()) { |
1772 | result.SetError(error: err); |
1773 | return; |
1774 | } |
1775 | |
1776 | if (!warnings.empty()) |
1777 | result.AppendWarning(in_string: warnings.c_str()); |
1778 | } |
1779 | |
1780 | result.SetStatus(eReturnStatusSuccessFinishResult); |
1781 | } |
1782 | |
1783 | CommandOptions m_options; |
1784 | }; |
1785 | |
1786 | // Next are the subcommands of CommandObjectMultiwordThreadPlan |
1787 | |
1788 | // CommandObjectThreadPlanList |
1789 | #define LLDB_OPTIONS_thread_plan_list |
1790 | #include "CommandOptions.inc" |
1791 | |
1792 | class CommandObjectThreadPlanList : public CommandObjectIterateOverThreads { |
1793 | public: |
1794 | class CommandOptions : public Options { |
1795 | public: |
1796 | CommandOptions() { |
1797 | // Keep default values of all options in one place: OptionParsingStarting |
1798 | // () |
1799 | OptionParsingStarting(execution_context: nullptr); |
1800 | } |
1801 | |
1802 | ~CommandOptions() override = default; |
1803 | |
1804 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
1805 | ExecutionContext *execution_context) override { |
1806 | const int short_option = m_getopt_table[option_idx].val; |
1807 | |
1808 | switch (short_option) { |
1809 | case 'i': |
1810 | m_internal = true; |
1811 | break; |
1812 | case 't': |
1813 | lldb::tid_t tid; |
1814 | if (option_arg.getAsInteger(0, tid)) |
1815 | return Status("invalid tid: '%s'." , option_arg.str().c_str()); |
1816 | m_tids.push_back(tid); |
1817 | break; |
1818 | case 'u': |
1819 | m_unreported = false; |
1820 | break; |
1821 | case 'v': |
1822 | m_verbose = true; |
1823 | break; |
1824 | default: |
1825 | llvm_unreachable("Unimplemented option" ); |
1826 | } |
1827 | return {}; |
1828 | } |
1829 | |
1830 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
1831 | m_verbose = false; |
1832 | m_internal = false; |
1833 | m_unreported = true; // The variable is "skip unreported" and we want to |
1834 | // skip unreported by default. |
1835 | m_tids.clear(); |
1836 | } |
1837 | |
1838 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
1839 | return llvm::ArrayRef(g_thread_plan_list_options); |
1840 | } |
1841 | |
1842 | // Instance variables to hold the values for command options. |
1843 | bool m_verbose; |
1844 | bool m_internal; |
1845 | bool m_unreported; |
1846 | std::vector<lldb::tid_t> m_tids; |
1847 | }; |
1848 | |
1849 | CommandObjectThreadPlanList(CommandInterpreter &interpreter) |
1850 | : CommandObjectIterateOverThreads( |
1851 | interpreter, "thread plan list" , |
1852 | "Show thread plans for one or more threads. If no threads are " |
1853 | "specified, show the " |
1854 | "current thread. Use the thread-index \"all\" to see all threads." , |
1855 | nullptr, |
1856 | eCommandRequiresProcess | eCommandRequiresThread | |
1857 | eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | |
1858 | eCommandProcessMustBePaused) {} |
1859 | |
1860 | ~CommandObjectThreadPlanList() override = default; |
1861 | |
1862 | Options *GetOptions() override { return &m_options; } |
1863 | |
1864 | void DoExecute(Args &command, CommandReturnObject &result) override { |
1865 | // If we are reporting all threads, dispatch to the Process to do that: |
1866 | if (command.GetArgumentCount() == 0 && m_options.m_tids.empty()) { |
1867 | Stream &strm = result.GetOutputStream(); |
1868 | DescriptionLevel desc_level = m_options.m_verbose |
1869 | ? eDescriptionLevelVerbose |
1870 | : eDescriptionLevelFull; |
1871 | m_exe_ctx.GetProcessPtr()->DumpThreadPlans( |
1872 | strm, desc_level, m_options.m_internal, true, m_options.m_unreported); |
1873 | result.SetStatus(eReturnStatusSuccessFinishResult); |
1874 | return; |
1875 | } else { |
1876 | // Do any TID's that the user may have specified as TID, then do any |
1877 | // Thread Indexes... |
1878 | if (!m_options.m_tids.empty()) { |
1879 | Process *process = m_exe_ctx.GetProcessPtr(); |
1880 | StreamString tmp_strm; |
1881 | for (lldb::tid_t tid : m_options.m_tids) { |
1882 | bool success = process->DumpThreadPlansForTID( |
1883 | tmp_strm, tid, eDescriptionLevelFull, m_options.m_internal, |
1884 | true /* condense_trivial */, m_options.m_unreported); |
1885 | // If we didn't find a TID, stop here and return an error. |
1886 | if (!success) { |
1887 | result.AppendError("Error dumping plans:" ); |
1888 | result.AppendError(tmp_strm.GetString()); |
1889 | return; |
1890 | } |
1891 | // Otherwise, add our data to the output: |
1892 | result.GetOutputStream() << tmp_strm.GetString(); |
1893 | } |
1894 | } |
1895 | return CommandObjectIterateOverThreads::DoExecute(command, result); |
1896 | } |
1897 | } |
1898 | |
1899 | protected: |
1900 | bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { |
1901 | // If we have already handled this from a -t option, skip it here. |
1902 | if (llvm::is_contained(m_options.m_tids, tid)) |
1903 | return true; |
1904 | |
1905 | Process *process = m_exe_ctx.GetProcessPtr(); |
1906 | |
1907 | Stream &strm = result.GetOutputStream(); |
1908 | DescriptionLevel desc_level = eDescriptionLevelFull; |
1909 | if (m_options.m_verbose) |
1910 | desc_level = eDescriptionLevelVerbose; |
1911 | |
1912 | process->DumpThreadPlansForTID(strm, tid, desc_level, m_options.m_internal, |
1913 | true /* condense_trivial */, |
1914 | m_options.m_unreported); |
1915 | return true; |
1916 | } |
1917 | |
1918 | CommandOptions m_options; |
1919 | }; |
1920 | |
1921 | class CommandObjectThreadPlanDiscard : public CommandObjectParsed { |
1922 | public: |
1923 | CommandObjectThreadPlanDiscard(CommandInterpreter &interpreter) |
1924 | : CommandObjectParsed(interpreter, "thread plan discard" , |
1925 | "Discards thread plans up to and including the " |
1926 | "specified index (see 'thread plan list'.) " |
1927 | "Only user visible plans can be discarded." , |
1928 | nullptr, |
1929 | eCommandRequiresProcess | eCommandRequiresThread | |
1930 | eCommandTryTargetAPILock | |
1931 | eCommandProcessMustBeLaunched | |
1932 | eCommandProcessMustBePaused) { |
1933 | CommandArgumentEntry arg; |
1934 | CommandArgumentData plan_index_arg; |
1935 | |
1936 | // Define the first (and only) variant of this arg. |
1937 | plan_index_arg.arg_type = eArgTypeUnsignedInteger; |
1938 | plan_index_arg.arg_repetition = eArgRepeatPlain; |
1939 | |
1940 | // There is only one variant this argument could be; put it into the |
1941 | // argument entry. |
1942 | arg.push_back(x: plan_index_arg); |
1943 | |
1944 | // Push the data for the first argument into the m_arguments vector. |
1945 | m_arguments.push_back(x: arg); |
1946 | } |
1947 | |
1948 | ~CommandObjectThreadPlanDiscard() override = default; |
1949 | |
1950 | void |
1951 | HandleArgumentCompletion(CompletionRequest &request, |
1952 | OptionElementVector &opt_element_vector) override { |
1953 | if (!m_exe_ctx.HasThreadScope() || request.GetCursorIndex()) |
1954 | return; |
1955 | |
1956 | m_exe_ctx.GetThreadPtr()->AutoCompleteThreadPlans(request); |
1957 | } |
1958 | |
1959 | void DoExecute(Args &args, CommandReturnObject &result) override { |
1960 | Thread *thread = m_exe_ctx.GetThreadPtr(); |
1961 | if (args.GetArgumentCount() != 1) { |
1962 | result.AppendErrorWithFormat(format: "Too many arguments, expected one - the " |
1963 | "thread plan index - but got %zu." , |
1964 | args.GetArgumentCount()); |
1965 | return; |
1966 | } |
1967 | |
1968 | uint32_t thread_plan_idx; |
1969 | if (!llvm::to_integer(S: args.GetArgumentAtIndex(idx: 0), Num&: thread_plan_idx)) { |
1970 | result.AppendErrorWithFormat( |
1971 | format: "Invalid thread index: \"%s\" - should be unsigned int." , |
1972 | args.GetArgumentAtIndex(idx: 0)); |
1973 | return; |
1974 | } |
1975 | |
1976 | if (thread_plan_idx == 0) { |
1977 | result.AppendErrorWithFormat( |
1978 | format: "You wouldn't really want me to discard the base thread plan." ); |
1979 | return; |
1980 | } |
1981 | |
1982 | if (thread->DiscardUserThreadPlansUpToIndex(thread_index: thread_plan_idx)) { |
1983 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
1984 | } else { |
1985 | result.AppendErrorWithFormat( |
1986 | format: "Could not find User thread plan with index %s." , |
1987 | args.GetArgumentAtIndex(idx: 0)); |
1988 | } |
1989 | } |
1990 | }; |
1991 | |
1992 | class CommandObjectThreadPlanPrune : public CommandObjectParsed { |
1993 | public: |
1994 | CommandObjectThreadPlanPrune(CommandInterpreter &interpreter) |
1995 | : CommandObjectParsed(interpreter, "thread plan prune" , |
1996 | "Removes any thread plans associated with " |
1997 | "currently unreported threads. " |
1998 | "Specify one or more TID's to remove, or if no " |
1999 | "TID's are provides, remove threads for all " |
2000 | "unreported threads" , |
2001 | nullptr, |
2002 | eCommandRequiresProcess | |
2003 | eCommandTryTargetAPILock | |
2004 | eCommandProcessMustBeLaunched | |
2005 | eCommandProcessMustBePaused) { |
2006 | CommandArgumentEntry arg; |
2007 | CommandArgumentData tid_arg; |
2008 | |
2009 | // Define the first (and only) variant of this arg. |
2010 | tid_arg.arg_type = eArgTypeThreadID; |
2011 | tid_arg.arg_repetition = eArgRepeatStar; |
2012 | |
2013 | // There is only one variant this argument could be; put it into the |
2014 | // argument entry. |
2015 | arg.push_back(x: tid_arg); |
2016 | |
2017 | // Push the data for the first argument into the m_arguments vector. |
2018 | m_arguments.push_back(x: arg); |
2019 | } |
2020 | |
2021 | ~CommandObjectThreadPlanPrune() override = default; |
2022 | |
2023 | void DoExecute(Args &args, CommandReturnObject &result) override { |
2024 | Process *process = m_exe_ctx.GetProcessPtr(); |
2025 | |
2026 | if (args.GetArgumentCount() == 0) { |
2027 | process->PruneThreadPlans(); |
2028 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
2029 | return; |
2030 | } |
2031 | |
2032 | const size_t num_args = args.GetArgumentCount(); |
2033 | |
2034 | std::lock_guard<std::recursive_mutex> guard( |
2035 | process->GetThreadList().GetMutex()); |
2036 | |
2037 | for (size_t i = 0; i < num_args; i++) { |
2038 | lldb::tid_t tid; |
2039 | if (!llvm::to_integer(S: args.GetArgumentAtIndex(idx: i), Num&: tid)) { |
2040 | result.AppendErrorWithFormat(format: "invalid thread specification: \"%s\"\n" , |
2041 | args.GetArgumentAtIndex(idx: i)); |
2042 | return; |
2043 | } |
2044 | if (!process->PruneThreadPlansForTID(tid)) { |
2045 | result.AppendErrorWithFormat(format: "Could not find unreported tid: \"%s\"\n" , |
2046 | args.GetArgumentAtIndex(idx: i)); |
2047 | return; |
2048 | } |
2049 | } |
2050 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
2051 | } |
2052 | }; |
2053 | |
2054 | // CommandObjectMultiwordThreadPlan |
2055 | |
2056 | class CommandObjectMultiwordThreadPlan : public CommandObjectMultiword { |
2057 | public: |
2058 | CommandObjectMultiwordThreadPlan(CommandInterpreter &interpreter) |
2059 | : CommandObjectMultiword( |
2060 | interpreter, "plan" , |
2061 | "Commands for managing thread plans that control execution." , |
2062 | "thread plan <subcommand> [<subcommand objects]" ) { |
2063 | LoadSubCommand( |
2064 | cmd_name: "list" , command_obj: CommandObjectSP(new CommandObjectThreadPlanList(interpreter))); |
2065 | LoadSubCommand( |
2066 | cmd_name: "discard" , |
2067 | command_obj: CommandObjectSP(new CommandObjectThreadPlanDiscard(interpreter))); |
2068 | LoadSubCommand( |
2069 | cmd_name: "prune" , |
2070 | command_obj: CommandObjectSP(new CommandObjectThreadPlanPrune(interpreter))); |
2071 | } |
2072 | |
2073 | ~CommandObjectMultiwordThreadPlan() override = default; |
2074 | }; |
2075 | |
2076 | // Next are the subcommands of CommandObjectMultiwordTrace |
2077 | |
2078 | // CommandObjectTraceExport |
2079 | |
2080 | class CommandObjectTraceExport : public CommandObjectMultiword { |
2081 | public: |
2082 | CommandObjectTraceExport(CommandInterpreter &interpreter) |
2083 | : CommandObjectMultiword( |
2084 | interpreter, "trace thread export" , |
2085 | "Commands for exporting traces of the threads in the current " |
2086 | "process to different formats." , |
2087 | "thread trace export <export-plugin> [<subcommand objects>]" ) { |
2088 | |
2089 | unsigned i = 0; |
2090 | for (llvm::StringRef plugin_name = |
2091 | PluginManager::GetTraceExporterPluginNameAtIndex(index: i); |
2092 | !plugin_name.empty(); |
2093 | plugin_name = PluginManager::GetTraceExporterPluginNameAtIndex(index: i++)) { |
2094 | if (ThreadTraceExportCommandCreator command_creator = |
2095 | PluginManager::GetThreadTraceExportCommandCreatorAtIndex(index: i)) { |
2096 | LoadSubCommand(cmd_name: plugin_name, command_obj: command_creator(interpreter)); |
2097 | } |
2098 | } |
2099 | } |
2100 | }; |
2101 | |
2102 | // CommandObjectTraceStart |
2103 | |
2104 | class CommandObjectTraceStart : public CommandObjectTraceProxy { |
2105 | public: |
2106 | CommandObjectTraceStart(CommandInterpreter &interpreter) |
2107 | : CommandObjectTraceProxy( |
2108 | /*live_debug_session_only=*/true, interpreter, "thread trace start" , |
2109 | "Start tracing threads with the corresponding trace " |
2110 | "plug-in for the current process." , |
2111 | "thread trace start [<trace-options>]" ) {} |
2112 | |
2113 | protected: |
2114 | lldb::CommandObjectSP GetDelegateCommand(Trace &trace) override { |
2115 | return trace.GetThreadTraceStartCommand(interpreter&: m_interpreter); |
2116 | } |
2117 | }; |
2118 | |
2119 | // CommandObjectTraceStop |
2120 | |
2121 | class CommandObjectTraceStop : public CommandObjectMultipleThreads { |
2122 | public: |
2123 | CommandObjectTraceStop(CommandInterpreter &interpreter) |
2124 | : CommandObjectMultipleThreads( |
2125 | interpreter, "thread trace stop" , |
2126 | "Stop tracing threads, including the ones traced with the " |
2127 | "\"process trace start\" command." |
2128 | "Defaults to the current thread. Thread indices can be " |
2129 | "specified as arguments.\n Use the thread-index \"all\" to stop " |
2130 | "tracing " |
2131 | "for all existing threads." , |
2132 | "thread trace stop [<thread-index> <thread-index> ...]" , |
2133 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
2134 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | |
2135 | eCommandProcessMustBeTraced) {} |
2136 | |
2137 | ~CommandObjectTraceStop() override = default; |
2138 | |
2139 | bool DoExecuteOnThreads(Args &command, CommandReturnObject &result, |
2140 | llvm::ArrayRef<lldb::tid_t> tids) override { |
2141 | ProcessSP process_sp = m_exe_ctx.GetProcessSP(); |
2142 | |
2143 | TraceSP trace_sp = process_sp->GetTarget().GetTrace(); |
2144 | |
2145 | if (llvm::Error err = trace_sp->Stop(tids)) |
2146 | result.AppendError(in_string: toString(E: std::move(err))); |
2147 | else |
2148 | result.SetStatus(eReturnStatusSuccessFinishResult); |
2149 | |
2150 | return result.Succeeded(); |
2151 | } |
2152 | }; |
2153 | |
2154 | static ThreadSP GetSingleThreadFromArgs(ExecutionContext &exe_ctx, Args &args, |
2155 | CommandReturnObject &result) { |
2156 | if (args.GetArgumentCount() == 0) |
2157 | return exe_ctx.GetThreadSP(); |
2158 | |
2159 | const char *arg = args.GetArgumentAtIndex(idx: 0); |
2160 | uint32_t thread_idx; |
2161 | |
2162 | if (!llvm::to_integer(S: arg, Num&: thread_idx)) { |
2163 | result.AppendErrorWithFormat(format: "invalid thread specification: \"%s\"\n" , arg); |
2164 | return nullptr; |
2165 | } |
2166 | ThreadSP thread_sp = |
2167 | exe_ctx.GetProcessRef().GetThreadList().FindThreadByIndexID(index_id: thread_idx); |
2168 | if (!thread_sp) |
2169 | result.AppendErrorWithFormat(format: "no thread with index: \"%s\"\n" , arg); |
2170 | return thread_sp; |
2171 | } |
2172 | |
2173 | // CommandObjectTraceDumpFunctionCalls |
2174 | #define LLDB_OPTIONS_thread_trace_dump_function_calls |
2175 | #include "CommandOptions.inc" |
2176 | |
2177 | class CommandObjectTraceDumpFunctionCalls : public CommandObjectParsed { |
2178 | public: |
2179 | class CommandOptions : public Options { |
2180 | public: |
2181 | CommandOptions() { OptionParsingStarting(execution_context: nullptr); } |
2182 | |
2183 | ~CommandOptions() override = default; |
2184 | |
2185 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
2186 | ExecutionContext *execution_context) override { |
2187 | Status error; |
2188 | const int short_option = m_getopt_table[option_idx].val; |
2189 | |
2190 | switch (short_option) { |
2191 | case 'j': { |
2192 | m_dumper_options.json = true; |
2193 | break; |
2194 | } |
2195 | case 'J': { |
2196 | m_dumper_options.json = true; |
2197 | m_dumper_options.pretty_print_json = true; |
2198 | break; |
2199 | } |
2200 | case 'F': { |
2201 | m_output_file.emplace(option_arg); |
2202 | break; |
2203 | } |
2204 | default: |
2205 | llvm_unreachable("Unimplemented option" ); |
2206 | } |
2207 | return error; |
2208 | } |
2209 | |
2210 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
2211 | m_dumper_options = {}; |
2212 | m_output_file = std::nullopt; |
2213 | } |
2214 | |
2215 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
2216 | return llvm::ArrayRef(g_thread_trace_dump_function_calls_options); |
2217 | } |
2218 | |
2219 | static const size_t kDefaultCount = 20; |
2220 | |
2221 | // Instance variables to hold the values for command options. |
2222 | TraceDumperOptions m_dumper_options; |
2223 | std::optional<FileSpec> m_output_file; |
2224 | }; |
2225 | |
2226 | CommandObjectTraceDumpFunctionCalls(CommandInterpreter &interpreter) |
2227 | : CommandObjectParsed( |
2228 | interpreter, "thread trace dump function-calls" , |
2229 | "Dump the traced function-calls for one thread. If no " |
2230 | "thread is specified, the current thread is used." , |
2231 | nullptr, |
2232 | eCommandRequiresProcess | eCommandRequiresThread | |
2233 | eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | |
2234 | eCommandProcessMustBePaused | eCommandProcessMustBeTraced) { |
2235 | CommandArgumentData thread_arg{eArgTypeThreadIndex, eArgRepeatOptional}; |
2236 | m_arguments.push_back({thread_arg}); |
2237 | } |
2238 | |
2239 | ~CommandObjectTraceDumpFunctionCalls() override = default; |
2240 | |
2241 | Options *GetOptions() override { return &m_options; } |
2242 | |
2243 | protected: |
2244 | void DoExecute(Args &args, CommandReturnObject &result) override { |
2245 | ThreadSP thread_sp = GetSingleThreadFromArgs(exe_ctx&: m_exe_ctx, args, result); |
2246 | if (!thread_sp) { |
2247 | result.AppendError(in_string: "invalid thread\n" ); |
2248 | return; |
2249 | } |
2250 | |
2251 | llvm::Expected<TraceCursorSP> cursor_or_error = |
2252 | m_exe_ctx.GetTargetSP()->GetTrace()->CreateNewCursor(*thread_sp); |
2253 | |
2254 | if (!cursor_or_error) { |
2255 | result.AppendError(in_string: llvm::toString(cursor_or_error.takeError())); |
2256 | return; |
2257 | } |
2258 | TraceCursorSP &cursor_sp = *cursor_or_error; |
2259 | |
2260 | std::optional<StreamFile> out_file; |
2261 | if (m_options.m_output_file) { |
2262 | out_file.emplace(m_options.m_output_file->GetPath().c_str(), |
2263 | File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate | |
2264 | File::eOpenOptionTruncate); |
2265 | } |
2266 | |
2267 | m_options.m_dumper_options.forwards = true; |
2268 | |
2269 | TraceDumper dumper(std::move(cursor_sp), |
2270 | out_file ? *out_file : result.GetOutputStream(), |
2271 | m_options.m_dumper_options); |
2272 | |
2273 | dumper.DumpFunctionCalls(); |
2274 | } |
2275 | |
2276 | CommandOptions m_options; |
2277 | }; |
2278 | |
2279 | // CommandObjectTraceDumpInstructions |
2280 | #define LLDB_OPTIONS_thread_trace_dump_instructions |
2281 | #include "CommandOptions.inc" |
2282 | |
2283 | class CommandObjectTraceDumpInstructions : public CommandObjectParsed { |
2284 | public: |
2285 | class CommandOptions : public Options { |
2286 | public: |
2287 | CommandOptions() { OptionParsingStarting(execution_context: nullptr); } |
2288 | |
2289 | ~CommandOptions() override = default; |
2290 | |
2291 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
2292 | ExecutionContext *execution_context) override { |
2293 | Status error; |
2294 | const int short_option = m_getopt_table[option_idx].val; |
2295 | |
2296 | switch (short_option) { |
2297 | case 'c': { |
2298 | int32_t count; |
2299 | if (option_arg.empty() || option_arg.getAsInteger(Radix: 0, Result&: count) || |
2300 | count < 0) |
2301 | error.SetErrorStringWithFormat( |
2302 | "invalid integer value for option '%s'" , |
2303 | option_arg.str().c_str()); |
2304 | else |
2305 | m_count = count; |
2306 | break; |
2307 | } |
2308 | case 'a': { |
2309 | m_count = std::numeric_limits<decltype(m_count)>::max(); |
2310 | break; |
2311 | } |
2312 | case 's': { |
2313 | int32_t skip; |
2314 | if (option_arg.empty() || option_arg.getAsInteger(Radix: 0, Result&: skip) || skip < 0) |
2315 | error.SetErrorStringWithFormat( |
2316 | "invalid integer value for option '%s'" , |
2317 | option_arg.str().c_str()); |
2318 | else |
2319 | m_dumper_options.skip = skip; |
2320 | break; |
2321 | } |
2322 | case 'i': { |
2323 | uint64_t id; |
2324 | if (option_arg.empty() || option_arg.getAsInteger(Radix: 0, Result&: id)) |
2325 | error.SetErrorStringWithFormat( |
2326 | "invalid integer value for option '%s'" , |
2327 | option_arg.str().c_str()); |
2328 | else |
2329 | m_dumper_options.id = id; |
2330 | break; |
2331 | } |
2332 | case 'F': { |
2333 | m_output_file.emplace(args&: option_arg); |
2334 | break; |
2335 | } |
2336 | case 'r': { |
2337 | m_dumper_options.raw = true; |
2338 | break; |
2339 | } |
2340 | case 'f': { |
2341 | m_dumper_options.forwards = true; |
2342 | break; |
2343 | } |
2344 | case 'k': { |
2345 | m_dumper_options.show_control_flow_kind = true; |
2346 | break; |
2347 | } |
2348 | case 't': { |
2349 | m_dumper_options.show_timestamps = true; |
2350 | break; |
2351 | } |
2352 | case 'e': { |
2353 | m_dumper_options.show_events = true; |
2354 | break; |
2355 | } |
2356 | case 'j': { |
2357 | m_dumper_options.json = true; |
2358 | break; |
2359 | } |
2360 | case 'J': { |
2361 | m_dumper_options.pretty_print_json = true; |
2362 | m_dumper_options.json = true; |
2363 | break; |
2364 | } |
2365 | case 'E': { |
2366 | m_dumper_options.only_events = true; |
2367 | m_dumper_options.show_events = true; |
2368 | break; |
2369 | } |
2370 | case 'C': { |
2371 | m_continue = true; |
2372 | break; |
2373 | } |
2374 | default: |
2375 | llvm_unreachable("Unimplemented option" ); |
2376 | } |
2377 | return error; |
2378 | } |
2379 | |
2380 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
2381 | m_count = kDefaultCount; |
2382 | m_continue = false; |
2383 | m_output_file = std::nullopt; |
2384 | m_dumper_options = {}; |
2385 | } |
2386 | |
2387 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
2388 | return llvm::ArrayRef(g_thread_trace_dump_instructions_options); |
2389 | } |
2390 | |
2391 | static const size_t kDefaultCount = 20; |
2392 | |
2393 | // Instance variables to hold the values for command options. |
2394 | size_t m_count; |
2395 | size_t m_continue; |
2396 | std::optional<FileSpec> m_output_file; |
2397 | TraceDumperOptions m_dumper_options; |
2398 | }; |
2399 | |
2400 | CommandObjectTraceDumpInstructions(CommandInterpreter &interpreter) |
2401 | : CommandObjectParsed( |
2402 | interpreter, "thread trace dump instructions" , |
2403 | "Dump the traced instructions for one thread. If no " |
2404 | "thread is specified, show the current thread." , |
2405 | nullptr, |
2406 | eCommandRequiresProcess | eCommandRequiresThread | |
2407 | eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | |
2408 | eCommandProcessMustBePaused | eCommandProcessMustBeTraced) { |
2409 | CommandArgumentData thread_arg{eArgTypeThreadIndex, eArgRepeatOptional}; |
2410 | m_arguments.push_back(x: {thread_arg}); |
2411 | } |
2412 | |
2413 | ~CommandObjectTraceDumpInstructions() override = default; |
2414 | |
2415 | Options *GetOptions() override { return &m_options; } |
2416 | |
2417 | std::optional<std::string> GetRepeatCommand(Args ¤t_command_args, |
2418 | uint32_t index) override { |
2419 | std::string cmd; |
2420 | current_command_args.GetCommandString(command&: cmd); |
2421 | if (cmd.find(s: " --continue" ) == std::string::npos) |
2422 | cmd += " --continue" ; |
2423 | return cmd; |
2424 | } |
2425 | |
2426 | protected: |
2427 | void DoExecute(Args &args, CommandReturnObject &result) override { |
2428 | ThreadSP thread_sp = GetSingleThreadFromArgs(exe_ctx&: m_exe_ctx, args, result); |
2429 | if (!thread_sp) { |
2430 | result.AppendError(in_string: "invalid thread\n" ); |
2431 | return; |
2432 | } |
2433 | |
2434 | if (m_options.m_continue && m_last_id) { |
2435 | // We set up the options to continue one instruction past where |
2436 | // the previous iteration stopped. |
2437 | m_options.m_dumper_options.skip = 1; |
2438 | m_options.m_dumper_options.id = m_last_id; |
2439 | } |
2440 | |
2441 | llvm::Expected<TraceCursorSP> cursor_or_error = |
2442 | m_exe_ctx.GetTargetSP()->GetTrace()->CreateNewCursor(thread&: *thread_sp); |
2443 | |
2444 | if (!cursor_or_error) { |
2445 | result.AppendError(in_string: llvm::toString(E: cursor_or_error.takeError())); |
2446 | return; |
2447 | } |
2448 | TraceCursorSP &cursor_sp = *cursor_or_error; |
2449 | |
2450 | if (m_options.m_dumper_options.id && |
2451 | !cursor_sp->HasId(id: *m_options.m_dumper_options.id)) { |
2452 | result.AppendError(in_string: "invalid instruction id\n" ); |
2453 | return; |
2454 | } |
2455 | |
2456 | std::optional<StreamFile> out_file; |
2457 | if (m_options.m_output_file) { |
2458 | out_file.emplace(args: m_options.m_output_file->GetPath().c_str(), |
2459 | args: File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate | |
2460 | File::eOpenOptionTruncate); |
2461 | } |
2462 | |
2463 | if (m_options.m_continue && !m_last_id) { |
2464 | // We need to stop processing data when we already ran out of instructions |
2465 | // in a previous command. We can fake this by setting the cursor past the |
2466 | // end of the trace. |
2467 | cursor_sp->Seek(offset: 1, origin: lldb::eTraceCursorSeekTypeEnd); |
2468 | } |
2469 | |
2470 | TraceDumper dumper(std::move(cursor_sp), |
2471 | out_file ? *out_file : result.GetOutputStream(), |
2472 | m_options.m_dumper_options); |
2473 | |
2474 | m_last_id = dumper.DumpInstructions(count: m_options.m_count); |
2475 | } |
2476 | |
2477 | CommandOptions m_options; |
2478 | // Last traversed id used to continue a repeat command. std::nullopt means |
2479 | // that all the trace has been consumed. |
2480 | std::optional<lldb::user_id_t> m_last_id; |
2481 | }; |
2482 | |
2483 | // CommandObjectTraceDumpInfo |
2484 | #define LLDB_OPTIONS_thread_trace_dump_info |
2485 | #include "CommandOptions.inc" |
2486 | |
2487 | class CommandObjectTraceDumpInfo : public CommandObjectIterateOverThreads { |
2488 | public: |
2489 | class CommandOptions : public Options { |
2490 | public: |
2491 | CommandOptions() { OptionParsingStarting(execution_context: nullptr); } |
2492 | |
2493 | ~CommandOptions() override = default; |
2494 | |
2495 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
2496 | ExecutionContext *execution_context) override { |
2497 | Status error; |
2498 | const int short_option = m_getopt_table[option_idx].val; |
2499 | |
2500 | switch (short_option) { |
2501 | case 'v': { |
2502 | m_verbose = true; |
2503 | break; |
2504 | } |
2505 | case 'j': { |
2506 | m_json = true; |
2507 | break; |
2508 | } |
2509 | default: |
2510 | llvm_unreachable("Unimplemented option" ); |
2511 | } |
2512 | return error; |
2513 | } |
2514 | |
2515 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
2516 | m_verbose = false; |
2517 | m_json = false; |
2518 | } |
2519 | |
2520 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
2521 | return llvm::ArrayRef(g_thread_trace_dump_info_options); |
2522 | } |
2523 | |
2524 | // Instance variables to hold the values for command options. |
2525 | bool m_verbose; |
2526 | bool m_json; |
2527 | }; |
2528 | |
2529 | CommandObjectTraceDumpInfo(CommandInterpreter &interpreter) |
2530 | : CommandObjectIterateOverThreads( |
2531 | interpreter, "thread trace dump info" , |
2532 | "Dump the traced information for one or more threads. If no " |
2533 | "threads are specified, show the current thread. Use the " |
2534 | "thread-index \"all\" to see all threads." , |
2535 | nullptr, |
2536 | eCommandRequiresProcess | eCommandTryTargetAPILock | |
2537 | eCommandProcessMustBeLaunched | eCommandProcessMustBePaused | |
2538 | eCommandProcessMustBeTraced) {} |
2539 | |
2540 | ~CommandObjectTraceDumpInfo() override = default; |
2541 | |
2542 | Options *GetOptions() override { return &m_options; } |
2543 | |
2544 | protected: |
2545 | bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { |
2546 | const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace(); |
2547 | ThreadSP thread_sp = |
2548 | m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); |
2549 | trace_sp->DumpTraceInfo(thread&: *thread_sp, s&: result.GetOutputStream(), |
2550 | verbose: m_options.m_verbose, json: m_options.m_json); |
2551 | return true; |
2552 | } |
2553 | |
2554 | CommandOptions m_options; |
2555 | }; |
2556 | |
2557 | // CommandObjectMultiwordTraceDump |
2558 | class CommandObjectMultiwordTraceDump : public CommandObjectMultiword { |
2559 | public: |
2560 | CommandObjectMultiwordTraceDump(CommandInterpreter &interpreter) |
2561 | : CommandObjectMultiword( |
2562 | interpreter, "dump" , |
2563 | "Commands for displaying trace information of the threads " |
2564 | "in the current process." , |
2565 | "thread trace dump <subcommand> [<subcommand objects>]" ) { |
2566 | LoadSubCommand( |
2567 | cmd_name: "instructions" , |
2568 | command_obj: CommandObjectSP(new CommandObjectTraceDumpInstructions(interpreter))); |
2569 | LoadSubCommand( |
2570 | cmd_name: "function-calls" , |
2571 | command_obj: CommandObjectSP(new CommandObjectTraceDumpFunctionCalls(interpreter))); |
2572 | LoadSubCommand( |
2573 | cmd_name: "info" , command_obj: CommandObjectSP(new CommandObjectTraceDumpInfo(interpreter))); |
2574 | } |
2575 | ~CommandObjectMultiwordTraceDump() override = default; |
2576 | }; |
2577 | |
2578 | // CommandObjectMultiwordTrace |
2579 | class CommandObjectMultiwordTrace : public CommandObjectMultiword { |
2580 | public: |
2581 | CommandObjectMultiwordTrace(CommandInterpreter &interpreter) |
2582 | : CommandObjectMultiword( |
2583 | interpreter, "trace" , |
2584 | "Commands for operating on traces of the threads in the current " |
2585 | "process." , |
2586 | "thread trace <subcommand> [<subcommand objects>]" ) { |
2587 | LoadSubCommand(cmd_name: "dump" , command_obj: CommandObjectSP(new CommandObjectMultiwordTraceDump( |
2588 | interpreter))); |
2589 | LoadSubCommand(cmd_name: "start" , |
2590 | command_obj: CommandObjectSP(new CommandObjectTraceStart(interpreter))); |
2591 | LoadSubCommand(cmd_name: "stop" , |
2592 | command_obj: CommandObjectSP(new CommandObjectTraceStop(interpreter))); |
2593 | LoadSubCommand(cmd_name: "export" , |
2594 | command_obj: CommandObjectSP(new CommandObjectTraceExport(interpreter))); |
2595 | } |
2596 | |
2597 | ~CommandObjectMultiwordTrace() override = default; |
2598 | }; |
2599 | |
2600 | // CommandObjectMultiwordThread |
2601 | |
2602 | CommandObjectMultiwordThread::CommandObjectMultiwordThread( |
2603 | CommandInterpreter &interpreter) |
2604 | : CommandObjectMultiword(interpreter, "thread" , |
2605 | "Commands for operating on " |
2606 | "one or more threads in " |
2607 | "the current process." , |
2608 | "thread <subcommand> [<subcommand-options>]" ) { |
2609 | LoadSubCommand(cmd_name: "backtrace" , command_obj: CommandObjectSP(new CommandObjectThreadBacktrace( |
2610 | interpreter))); |
2611 | LoadSubCommand(cmd_name: "continue" , |
2612 | command_obj: CommandObjectSP(new CommandObjectThreadContinue(interpreter))); |
2613 | LoadSubCommand(cmd_name: "list" , |
2614 | command_obj: CommandObjectSP(new CommandObjectThreadList(interpreter))); |
2615 | LoadSubCommand(cmd_name: "return" , |
2616 | command_obj: CommandObjectSP(new CommandObjectThreadReturn(interpreter))); |
2617 | LoadSubCommand(cmd_name: "jump" , |
2618 | command_obj: CommandObjectSP(new CommandObjectThreadJump(interpreter))); |
2619 | LoadSubCommand(cmd_name: "select" , |
2620 | command_obj: CommandObjectSP(new CommandObjectThreadSelect(interpreter))); |
2621 | LoadSubCommand(cmd_name: "until" , |
2622 | command_obj: CommandObjectSP(new CommandObjectThreadUntil(interpreter))); |
2623 | LoadSubCommand(cmd_name: "info" , |
2624 | command_obj: CommandObjectSP(new CommandObjectThreadInfo(interpreter))); |
2625 | LoadSubCommand(cmd_name: "exception" , command_obj: CommandObjectSP(new CommandObjectThreadException( |
2626 | interpreter))); |
2627 | LoadSubCommand(cmd_name: "siginfo" , |
2628 | command_obj: CommandObjectSP(new CommandObjectThreadSiginfo(interpreter))); |
2629 | LoadSubCommand(cmd_name: "step-in" , |
2630 | command_obj: CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( |
2631 | interpreter, "thread step-in" , |
2632 | "Source level single step, stepping into calls. Defaults " |
2633 | "to current thread unless specified." , |
2634 | nullptr, eStepTypeInto, eStepScopeSource))); |
2635 | |
2636 | LoadSubCommand(cmd_name: "step-out" , |
2637 | command_obj: CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( |
2638 | interpreter, "thread step-out" , |
2639 | "Finish executing the current stack frame and stop after " |
2640 | "returning. Defaults to current thread unless specified." , |
2641 | nullptr, eStepTypeOut, eStepScopeSource))); |
2642 | |
2643 | LoadSubCommand(cmd_name: "step-over" , |
2644 | command_obj: CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( |
2645 | interpreter, "thread step-over" , |
2646 | "Source level single step, stepping over calls. Defaults " |
2647 | "to current thread unless specified." , |
2648 | nullptr, eStepTypeOver, eStepScopeSource))); |
2649 | |
2650 | LoadSubCommand(cmd_name: "step-inst" , |
2651 | command_obj: CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( |
2652 | interpreter, "thread step-inst" , |
2653 | "Instruction level single step, stepping into calls. " |
2654 | "Defaults to current thread unless specified." , |
2655 | nullptr, eStepTypeTrace, eStepScopeInstruction))); |
2656 | |
2657 | LoadSubCommand(cmd_name: "step-inst-over" , |
2658 | command_obj: CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( |
2659 | interpreter, "thread step-inst-over" , |
2660 | "Instruction level single step, stepping over calls. " |
2661 | "Defaults to current thread unless specified." , |
2662 | nullptr, eStepTypeTraceOver, eStepScopeInstruction))); |
2663 | |
2664 | LoadSubCommand( |
2665 | cmd_name: "step-scripted" , |
2666 | command_obj: CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( |
2667 | interpreter, "thread step-scripted" , |
2668 | "Step as instructed by the script class passed in the -C option. " |
2669 | "You can also specify a dictionary of key (-k) and value (-v) pairs " |
2670 | "that will be used to populate an SBStructuredData Dictionary, which " |
2671 | "will be passed to the constructor of the class implementing the " |
2672 | "scripted step. See the Python Reference for more details." , |
2673 | nullptr, eStepTypeScripted, eStepScopeSource))); |
2674 | |
2675 | LoadSubCommand(cmd_name: "plan" , command_obj: CommandObjectSP(new CommandObjectMultiwordThreadPlan( |
2676 | interpreter))); |
2677 | LoadSubCommand(cmd_name: "trace" , |
2678 | command_obj: CommandObjectSP(new CommandObjectMultiwordTrace(interpreter))); |
2679 | } |
2680 | |
2681 | CommandObjectMultiwordThread::~CommandObjectMultiwordThread() = default; |
2682 | |