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