1 //===-- CommandObjectProcess.cpp --------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "CommandObjectProcess.h"
15 #include "lldb/Breakpoint/Breakpoint.h"
16 #include "lldb/Breakpoint/BreakpointLocation.h"
17 #include "lldb/Breakpoint/BreakpointSite.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/State.h"
21 #include "lldb/Host/Host.h"
22 #include "lldb/Host/OptionParser.h"
23 #include "lldb/Host/StringConvert.h"
24 #include "lldb/Interpreter/Args.h"
25 #include "lldb/Interpreter/CommandInterpreter.h"
26 #include "lldb/Interpreter/CommandReturnObject.h"
27 #include "lldb/Interpreter/Options.h"
28 #include "lldb/Target/Platform.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Target/StopInfo.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Target/Thread.h"
33 #include "lldb/Target/UnixSignals.h"
36 using namespace lldb_private;
38 class CommandObjectProcessLaunchOrAttach : public CommandObjectParsed {
40 CommandObjectProcessLaunchOrAttach(CommandInterpreter &interpreter,
41 const char *name, const char *help,
42 const char *syntax, uint32_t flags,
43 const char *new_process_action)
44 : CommandObjectParsed(interpreter, name, help, syntax, flags),
45 m_new_process_action(new_process_action) {}
47 ~CommandObjectProcessLaunchOrAttach() override = default;
50 bool StopProcessIfNecessary(Process *process, StateType &state,
51 CommandReturnObject &result) {
52 state = eStateInvalid;
54 state = process->GetState();
56 if (process->IsAlive() && state != eStateConnected) {
58 if (process->GetState() == eStateAttaching)
59 ::snprintf(message, sizeof(message),
60 "There is a pending attach, abort it and %s?",
61 m_new_process_action.c_str());
62 else if (process->GetShouldDetach())
63 ::snprintf(message, sizeof(message),
64 "There is a running process, detach from it and %s?",
65 m_new_process_action.c_str());
67 ::snprintf(message, sizeof(message),
68 "There is a running process, kill it and %s?",
69 m_new_process_action.c_str());
71 if (!m_interpreter.Confirm(message, true)) {
72 result.SetStatus(eReturnStatusFailed);
75 if (process->GetShouldDetach()) {
76 bool keep_stopped = false;
77 Status detach_error(process->Detach(keep_stopped));
78 if (detach_error.Success()) {
79 result.SetStatus(eReturnStatusSuccessFinishResult);
82 result.AppendErrorWithFormat(
83 "Failed to detach from process: %s\n",
84 detach_error.AsCString());
85 result.SetStatus(eReturnStatusFailed);
88 Status destroy_error(process->Destroy(false));
89 if (destroy_error.Success()) {
90 result.SetStatus(eReturnStatusSuccessFinishResult);
93 result.AppendErrorWithFormat("Failed to kill process: %s\n",
94 destroy_error.AsCString());
95 result.SetStatus(eReturnStatusFailed);
101 return result.Succeeded();
104 std::string m_new_process_action;
107 //-------------------------------------------------------------------------
108 // CommandObjectProcessLaunch
109 //-------------------------------------------------------------------------
110 #pragma mark CommandObjectProcessLaunch
111 class CommandObjectProcessLaunch : public CommandObjectProcessLaunchOrAttach {
113 CommandObjectProcessLaunch(CommandInterpreter &interpreter)
114 : CommandObjectProcessLaunchOrAttach(
115 interpreter, "process launch",
116 "Launch the executable in the debugger.", nullptr,
117 eCommandRequiresTarget, "restart"),
119 CommandArgumentEntry arg;
120 CommandArgumentData run_args_arg;
122 // Define the first (and only) variant of this arg.
123 run_args_arg.arg_type = eArgTypeRunArgs;
124 run_args_arg.arg_repetition = eArgRepeatOptional;
126 // There is only one variant this argument could be; put it into the
128 arg.push_back(run_args_arg);
130 // Push the data for the first argument into the m_arguments vector.
131 m_arguments.push_back(arg);
134 ~CommandObjectProcessLaunch() override = default;
136 int HandleArgumentCompletion(Args &input, int &cursor_index,
137 int &cursor_char_position,
138 OptionElementVector &opt_element_vector,
139 int match_start_point, int max_return_elements,
141 StringList &matches) override {
142 std::string completion_str(input.GetArgumentAtIndex(cursor_index));
143 completion_str.erase(cursor_char_position);
145 CommandCompletions::InvokeCommonCompletionCallbacks(
146 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
147 completion_str.c_str(), match_start_point, max_return_elements, nullptr,
148 word_complete, matches);
149 return matches.GetSize();
152 Options *GetOptions() override { return &m_options; }
154 const char *GetRepeatCommand(Args ¤t_command_args,
155 uint32_t index) override {
156 // No repeat for "process launch"...
161 bool DoExecute(Args &launch_args, CommandReturnObject &result) override {
162 Debugger &debugger = m_interpreter.GetDebugger();
163 Target *target = debugger.GetSelectedTarget().get();
164 // If our listener is nullptr, users aren't allows to launch
165 ModuleSP exe_module_sp = target->GetExecutableModule();
167 if (exe_module_sp == nullptr) {
168 result.AppendError("no file in target, create a debug target using the "
169 "'target create' command");
170 result.SetStatus(eReturnStatusFailed);
174 StateType state = eStateInvalid;
176 if (!StopProcessIfNecessary(m_exe_ctx.GetProcessPtr(), state, result))
179 llvm::StringRef target_settings_argv0 = target->GetArg0();
181 // Determine whether we will disable ASLR or leave it in the default state
182 // (i.e. enabled if the platform supports it).
183 // First check if the process launch options explicitly turn on/off
184 // disabling ASLR. If so, use that setting;
185 // otherwise, use the 'settings target.disable-aslr' setting.
186 bool disable_aslr = false;
187 if (m_options.disable_aslr != eLazyBoolCalculate) {
188 // The user specified an explicit setting on the process launch line. Use
190 disable_aslr = (m_options.disable_aslr == eLazyBoolYes);
192 // The user did not explicitly specify whether to disable ASLR. Fall back
193 // to the target.disable-aslr setting.
194 disable_aslr = target->GetDisableASLR();
198 m_options.launch_info.GetFlags().Set(eLaunchFlagDisableASLR);
200 m_options.launch_info.GetFlags().Clear(eLaunchFlagDisableASLR);
202 if (target->GetDetachOnError())
203 m_options.launch_info.GetFlags().Set(eLaunchFlagDetachOnError);
205 if (target->GetDisableSTDIO())
206 m_options.launch_info.GetFlags().Set(eLaunchFlagDisableSTDIO);
209 target->GetEnvironmentAsArgs(environment);
210 if (environment.GetArgumentCount() > 0)
211 m_options.launch_info.GetEnvironmentEntries().AppendArguments(
214 if (!target_settings_argv0.empty()) {
215 m_options.launch_info.GetArguments().AppendArgument(
216 target_settings_argv0);
217 m_options.launch_info.SetExecutableFile(
218 exe_module_sp->GetPlatformFileSpec(), false);
220 m_options.launch_info.SetExecutableFile(
221 exe_module_sp->GetPlatformFileSpec(), true);
224 if (launch_args.GetArgumentCount() == 0) {
225 m_options.launch_info.GetArguments().AppendArguments(
226 target->GetProcessLaunchInfo().GetArguments());
228 m_options.launch_info.GetArguments().AppendArguments(launch_args);
229 // Save the arguments for subsequent runs in the current target.
230 target->SetRunArguments(launch_args);
234 Status error = target->Launch(m_options.launch_info, &stream);
236 if (error.Success()) {
237 ProcessSP process_sp(target->GetProcessSP());
239 // There is a race condition where this thread will return up the call
240 // stack to the main command
241 // handler and show an (lldb) prompt before HandlePrivateEvent (from
242 // PrivateStateThread) has
243 // a chance to call PushProcessIOHandler().
244 process_sp->SyncIOHandler(0, 2000);
246 llvm::StringRef data = stream.GetString();
248 result.AppendMessage(data);
249 const char *archname =
250 exe_module_sp->GetArchitecture().GetArchitectureName();
251 result.AppendMessageWithFormat(
252 "Process %" PRIu64 " launched: '%s' (%s)\n", process_sp->GetID(),
253 exe_module_sp->GetFileSpec().GetPath().c_str(), archname);
254 result.SetStatus(eReturnStatusSuccessFinishResult);
255 result.SetDidChangeProcessState(true);
258 "no error returned from Target::Launch, and target has no process");
259 result.SetStatus(eReturnStatusFailed);
262 result.AppendError(error.AsCString());
263 result.SetStatus(eReturnStatusFailed);
265 return result.Succeeded();
269 ProcessLaunchCommandOptions m_options;
272 //#define SET1 LLDB_OPT_SET_1
273 //#define SET2 LLDB_OPT_SET_2
274 //#define SET3 LLDB_OPT_SET_3
277 // CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
279 // // clang-format off
280 // {SET1 | SET2 | SET3, false, "stop-at-entry", 's', OptionParser::eNoArgument,
281 // nullptr, 0, eArgTypeNone, "Stop at the entry point of the program
282 // when launching a process."},
283 // {SET1, false, "stdin", 'i',
284 // OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
285 // "Redirect stdin for the process to <path>."},
286 // {SET1, false, "stdout", 'o',
287 // OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
288 // "Redirect stdout for the process to <path>."},
289 // {SET1, false, "stderr", 'e',
290 // OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
291 // "Redirect stderr for the process to <path>."},
292 // {SET1 | SET2 | SET3, false, "plugin", 'p',
293 // OptionParser::eRequiredArgument, nullptr, 0, eArgTypePlugin, "Name of
294 // the process plugin you want to use."},
295 // { SET2, false, "tty", 't',
296 // OptionParser::eOptionalArgument, nullptr, 0, eArgTypeDirectoryName, "Start
297 // the process in a terminal. If <path> is specified, look for a terminal whose
298 // name contains <path>, else start the process in a new terminal."},
299 // { SET3, false, "no-stdio", 'n', OptionParser::eNoArgument,
300 // nullptr, 0, eArgTypeNone, "Do not set up for terminal I/O to go to
301 // running process."},
302 // {SET1 | SET2 | SET3, false, "working-dir", 'w',
303 // OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName, "Set the
304 // current working directory to <path> when running the inferior."},
305 // {0, false, nullptr, 0, 0, nullptr, 0, eArgTypeNone, nullptr}
306 // // clang-format on
313 //-------------------------------------------------------------------------
314 // CommandObjectProcessAttach
315 //-------------------------------------------------------------------------
317 static OptionDefinition g_process_attach_options[] = {
319 { LLDB_OPT_SET_ALL, false, "continue", 'c', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Immediately continue the process once attached." },
320 { LLDB_OPT_SET_ALL, false, "plugin", 'P', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePlugin, "Name of the process plugin you want to use." },
321 { LLDB_OPT_SET_1, false, "pid", 'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePid, "The process ID of an existing process to attach to." },
322 { LLDB_OPT_SET_2, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeProcessName, "The name of the process to attach to." },
323 { LLDB_OPT_SET_2, false, "include-existing", 'i', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Include existing processes when doing attach -w." },
324 { LLDB_OPT_SET_2, false, "waitfor", 'w', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Wait for the process with <process-name> to launch." },
328 #pragma mark CommandObjectProcessAttach
329 class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach {
331 class CommandOptions : public Options {
333 CommandOptions() : Options() {
334 // Keep default values of all options in one place: OptionParsingStarting
336 OptionParsingStarting(nullptr);
339 ~CommandOptions() override = default;
341 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
342 ExecutionContext *execution_context) override {
344 const int short_option = m_getopt_table[option_idx].val;
345 switch (short_option) {
347 attach_info.SetContinueOnceAttached(true);
352 if (option_arg.getAsInteger(0, pid)) {
353 error.SetErrorStringWithFormat("invalid process ID '%s'",
354 option_arg.str().c_str());
356 attach_info.SetProcessID(pid);
361 attach_info.SetProcessPluginName(option_arg);
365 attach_info.GetExecutableFile().SetFile(option_arg, false);
369 attach_info.SetWaitForLaunch(true);
373 attach_info.SetIgnoreExisting(false);
377 error.SetErrorStringWithFormat("invalid short option character '%c'",
384 void OptionParsingStarting(ExecutionContext *execution_context) override {
388 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
389 return llvm::makeArrayRef(g_process_attach_options);
392 bool HandleOptionArgumentCompletion(
393 Args &input, int cursor_index, int char_pos,
394 OptionElementVector &opt_element_vector, int opt_element_index,
395 int match_start_point, int max_return_elements,
396 CommandInterpreter &interpreter, bool &word_complete,
397 StringList &matches) override {
398 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
399 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
401 // We are only completing the name option for now...
403 if (GetDefinitions()[opt_defs_index].short_option == 'n') {
404 // Are we in the name?
406 // Look to see if there is a -P argument provided, and if so use that
408 // use the default plugin.
410 const char *partial_name = nullptr;
411 partial_name = input.GetArgumentAtIndex(opt_arg_pos);
413 PlatformSP platform_sp(interpreter.GetPlatform(true));
415 ProcessInstanceInfoList process_infos;
416 ProcessInstanceInfoMatch match_info;
418 match_info.GetProcessInfo().GetExecutableFile().SetFile(
419 partial_name, false);
420 match_info.SetNameMatchType(NameMatch::StartsWith);
422 platform_sp->FindProcesses(match_info, process_infos);
423 const size_t num_matches = process_infos.GetSize();
424 if (num_matches > 0) {
425 for (size_t i = 0; i < num_matches; ++i) {
426 matches.AppendString(
427 process_infos.GetProcessNameAtIndex(i),
428 process_infos.GetProcessNameLengthAtIndex(i));
437 // Instance variables to hold the values for command options.
439 ProcessAttachInfo attach_info;
442 CommandObjectProcessAttach(CommandInterpreter &interpreter)
443 : CommandObjectProcessLaunchOrAttach(
444 interpreter, "process attach", "Attach to a process.",
445 "process attach <cmd-options>", 0, "attach"),
448 ~CommandObjectProcessAttach() override = default;
450 Options *GetOptions() override { return &m_options; }
453 bool DoExecute(Args &command, CommandReturnObject &result) override {
454 PlatformSP platform_sp(
455 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
457 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
458 // N.B. The attach should be synchronous. It doesn't help much to get the
459 // prompt back between initiating the attach
460 // and the target actually stopping. So even if the interpreter is set to
461 // be asynchronous, we wait for the stop
464 StateType state = eStateInvalid;
465 Process *process = m_exe_ctx.GetProcessPtr();
467 if (!StopProcessIfNecessary(process, state, result))
470 if (target == nullptr) {
471 // If there isn't a current target create one.
472 TargetSP new_target_sp;
475 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget(
476 m_interpreter.GetDebugger(), "", "", false,
477 nullptr, // No platform options
479 target = new_target_sp.get();
480 if (target == nullptr || error.Fail()) {
481 result.AppendError(error.AsCString("Error creating target"));
484 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
487 // Record the old executable module, we want to issue a warning if the
488 // process of attaching changed the
489 // current executable (like somebody said "file foo" then attached to a PID
490 // whose executable was bar.)
492 ModuleSP old_exec_module_sp = target->GetExecutableModule();
493 ArchSpec old_arch_spec = target->GetArchitecture();
495 if (command.GetArgumentCount()) {
496 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: %s\n",
497 m_cmd_name.c_str(), m_cmd_syntax.c_str());
498 result.SetStatus(eReturnStatusFailed);
502 m_interpreter.UpdateExecutionContext(nullptr);
504 const auto error = target->Attach(m_options.attach_info, &stream);
505 if (error.Success()) {
506 ProcessSP process_sp(target->GetProcessSP());
508 result.AppendMessage(stream.GetString());
509 result.SetStatus(eReturnStatusSuccessFinishNoResult);
510 result.SetDidChangeProcessState(true);
511 result.SetAbnormalStopWasExpected(true);
514 "no error returned from Target::Attach, and target has no process");
515 result.SetStatus(eReturnStatusFailed);
518 result.AppendErrorWithFormat("attach failed: %s\n", error.AsCString());
519 result.SetStatus(eReturnStatusFailed);
522 if (!result.Succeeded())
525 // Okay, we're done. Last step is to warn if the executable module has
527 char new_path[PATH_MAX];
528 ModuleSP new_exec_module_sp(target->GetExecutableModule());
529 if (!old_exec_module_sp) {
530 // We might not have a module if we attached to a raw pid...
531 if (new_exec_module_sp) {
532 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
533 result.AppendMessageWithFormat("Executable module set to \"%s\".\n",
536 } else if (old_exec_module_sp->GetFileSpec() !=
537 new_exec_module_sp->GetFileSpec()) {
538 char old_path[PATH_MAX];
540 old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
541 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
543 result.AppendWarningWithFormat(
544 "Executable module changed from \"%s\" to \"%s\".\n", old_path,
548 if (!old_arch_spec.IsValid()) {
549 result.AppendMessageWithFormat(
550 "Architecture set to: %s.\n",
551 target->GetArchitecture().GetTriple().getTriple().c_str());
552 } else if (!old_arch_spec.IsExactMatch(target->GetArchitecture())) {
553 result.AppendWarningWithFormat(
554 "Architecture changed from %s to %s.\n",
555 old_arch_spec.GetTriple().getTriple().c_str(),
556 target->GetArchitecture().GetTriple().getTriple().c_str());
559 // This supports the use-case scenario of immediately continuing the process
561 if (m_options.attach_info.GetContinueOnceAttached())
562 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
564 return result.Succeeded();
567 CommandOptions m_options;
570 //-------------------------------------------------------------------------
571 // CommandObjectProcessContinue
572 //-------------------------------------------------------------------------
574 static OptionDefinition g_process_continue_options[] = {
576 { LLDB_OPT_SET_ALL, false, "ignore-count",'i', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Ignore <N> crossings of the breakpoint (if it exists) for the currently selected thread." }
580 #pragma mark CommandObjectProcessContinue
582 class CommandObjectProcessContinue : public CommandObjectParsed {
584 CommandObjectProcessContinue(CommandInterpreter &interpreter)
585 : CommandObjectParsed(
586 interpreter, "process continue",
587 "Continue execution of all threads in the current process.",
589 eCommandRequiresProcess | eCommandTryTargetAPILock |
590 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
593 ~CommandObjectProcessContinue() override = default;
596 class CommandOptions : public Options {
598 CommandOptions() : Options() {
599 // Keep default values of all options in one place: OptionParsingStarting
601 OptionParsingStarting(nullptr);
604 ~CommandOptions() override = default;
606 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
607 ExecutionContext *execution_context) override {
609 const int short_option = m_getopt_table[option_idx].val;
610 switch (short_option) {
612 if (option_arg.getAsInteger(0, m_ignore))
613 error.SetErrorStringWithFormat(
614 "invalid value for ignore option: \"%s\", should be a number.",
615 option_arg.str().c_str());
619 error.SetErrorStringWithFormat("invalid short option character '%c'",
626 void OptionParsingStarting(ExecutionContext *execution_context) override {
630 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
631 return llvm::makeArrayRef(g_process_continue_options);
637 bool DoExecute(Args &command, CommandReturnObject &result) override {
638 Process *process = m_exe_ctx.GetProcessPtr();
639 bool synchronous_execution = m_interpreter.GetSynchronous();
640 StateType state = process->GetState();
641 if (state == eStateStopped) {
642 if (command.GetArgumentCount() != 0) {
643 result.AppendErrorWithFormat(
644 "The '%s' command does not take any arguments.\n",
646 result.SetStatus(eReturnStatusFailed);
650 if (m_options.m_ignore > 0) {
651 ThreadSP sel_thread_sp(GetDefaultThread()->shared_from_this());
653 StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
655 stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
656 lldb::break_id_t bp_site_id =
657 (lldb::break_id_t)stop_info_sp->GetValue();
658 BreakpointSiteSP bp_site_sp(
659 process->GetBreakpointSiteList().FindByID(bp_site_id));
661 const size_t num_owners = bp_site_sp->GetNumberOfOwners();
662 for (size_t i = 0; i < num_owners; i++) {
664 bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
665 if (!bp_ref.IsInternal()) {
666 bp_ref.SetIgnoreCount(m_options.m_ignore);
674 { // Scope for thread list mutex:
675 std::lock_guard<std::recursive_mutex> guard(
676 process->GetThreadList().GetMutex());
677 const uint32_t num_threads = process->GetThreadList().GetSize();
679 // Set the actions that the threads should each take when resuming
680 for (uint32_t idx = 0; idx < num_threads; ++idx) {
681 const bool override_suspend = false;
682 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState(
683 eStateRunning, override_suspend);
687 const uint32_t iohandler_id = process->GetIOHandlerID();
691 if (synchronous_execution)
692 error = process->ResumeSynchronous(&stream);
694 error = process->Resume();
696 if (error.Success()) {
697 // There is a race condition where this thread will return up the call
698 // stack to the main command
699 // handler and show an (lldb) prompt before HandlePrivateEvent (from
700 // PrivateStateThread) has
701 // a chance to call PushProcessIOHandler().
702 process->SyncIOHandler(iohandler_id, 2000);
704 result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
706 if (synchronous_execution) {
707 // If any state changed events had anything to say, add that to the
709 result.AppendMessage(stream.GetString());
711 result.SetDidChangeProcessState(true);
712 result.SetStatus(eReturnStatusSuccessFinishNoResult);
714 result.SetStatus(eReturnStatusSuccessContinuingNoResult);
717 result.AppendErrorWithFormat("Failed to resume process: %s.\n",
719 result.SetStatus(eReturnStatusFailed);
722 result.AppendErrorWithFormat(
723 "Process cannot be continued from its current state (%s).\n",
724 StateAsCString(state));
725 result.SetStatus(eReturnStatusFailed);
727 return result.Succeeded();
730 Options *GetOptions() override { return &m_options; }
732 CommandOptions m_options;
735 //-------------------------------------------------------------------------
736 // CommandObjectProcessDetach
737 //-------------------------------------------------------------------------
738 static OptionDefinition g_process_detach_options[] = {
740 { LLDB_OPT_SET_1, false, "keep-stopped", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Whether or not the process should be kept stopped on detach (if possible)." },
744 #pragma mark CommandObjectProcessDetach
746 class CommandObjectProcessDetach : public CommandObjectParsed {
748 class CommandOptions : public Options {
750 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
752 ~CommandOptions() override = default;
754 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
755 ExecutionContext *execution_context) override {
757 const int short_option = m_getopt_table[option_idx].val;
759 switch (short_option) {
763 tmp_result = Args::StringToBoolean(option_arg, false, &success);
765 error.SetErrorStringWithFormat("invalid boolean option: \"%s\"",
766 option_arg.str().c_str());
769 m_keep_stopped = eLazyBoolYes;
771 m_keep_stopped = eLazyBoolNo;
775 error.SetErrorStringWithFormat("invalid short option character '%c'",
782 void OptionParsingStarting(ExecutionContext *execution_context) override {
783 m_keep_stopped = eLazyBoolCalculate;
786 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
787 return llvm::makeArrayRef(g_process_detach_options);
790 // Instance variables to hold the values for command options.
791 LazyBool m_keep_stopped;
794 CommandObjectProcessDetach(CommandInterpreter &interpreter)
795 : CommandObjectParsed(interpreter, "process detach",
796 "Detach from the current target process.",
798 eCommandRequiresProcess | eCommandTryTargetAPILock |
799 eCommandProcessMustBeLaunched),
802 ~CommandObjectProcessDetach() override = default;
804 Options *GetOptions() override { return &m_options; }
807 bool DoExecute(Args &command, CommandReturnObject &result) override {
808 Process *process = m_exe_ctx.GetProcessPtr();
809 // FIXME: This will be a Command Option:
811 if (m_options.m_keep_stopped == eLazyBoolCalculate) {
812 // Check the process default:
813 keep_stopped = process->GetDetachKeepsStopped();
814 } else if (m_options.m_keep_stopped == eLazyBoolYes)
817 keep_stopped = false;
819 Status error(process->Detach(keep_stopped));
820 if (error.Success()) {
821 result.SetStatus(eReturnStatusSuccessFinishResult);
823 result.AppendErrorWithFormat("Detach failed: %s\n", error.AsCString());
824 result.SetStatus(eReturnStatusFailed);
827 return result.Succeeded();
830 CommandOptions m_options;
833 //-------------------------------------------------------------------------
834 // CommandObjectProcessConnect
835 //-------------------------------------------------------------------------
837 static OptionDefinition g_process_connect_options[] = {
839 { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePlugin, "Name of the process plugin you want to use." },
843 #pragma mark CommandObjectProcessConnect
845 class CommandObjectProcessConnect : public CommandObjectParsed {
847 class CommandOptions : public Options {
849 CommandOptions() : Options() {
850 // Keep default values of all options in one place: OptionParsingStarting
852 OptionParsingStarting(nullptr);
855 ~CommandOptions() override = default;
857 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
858 ExecutionContext *execution_context) override {
860 const int short_option = m_getopt_table[option_idx].val;
862 switch (short_option) {
864 plugin_name.assign(option_arg);
868 error.SetErrorStringWithFormat("invalid short option character '%c'",
875 void OptionParsingStarting(ExecutionContext *execution_context) override {
879 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
880 return llvm::makeArrayRef(g_process_connect_options);
883 // Instance variables to hold the values for command options.
885 std::string plugin_name;
888 CommandObjectProcessConnect(CommandInterpreter &interpreter)
889 : CommandObjectParsed(interpreter, "process connect",
890 "Connect to a remote debug service.",
891 "process connect <remote-url>", 0),
894 ~CommandObjectProcessConnect() override = default;
896 Options *GetOptions() override { return &m_options; }
899 bool DoExecute(Args &command, CommandReturnObject &result) override {
900 if (command.GetArgumentCount() != 1) {
901 result.AppendErrorWithFormat(
902 "'%s' takes exactly one argument:\nUsage: %s\n", m_cmd_name.c_str(),
903 m_cmd_syntax.c_str());
904 result.SetStatus(eReturnStatusFailed);
908 Process *process = m_exe_ctx.GetProcessPtr();
909 if (process && process->IsAlive()) {
910 result.AppendErrorWithFormat(
912 " is currently being debugged, kill the process before connecting.\n",
914 result.SetStatus(eReturnStatusFailed);
918 const char *plugin_name = nullptr;
919 if (!m_options.plugin_name.empty())
920 plugin_name = m_options.plugin_name.c_str();
923 Debugger &debugger = m_interpreter.GetDebugger();
924 PlatformSP platform_sp = m_interpreter.GetPlatform(true);
925 ProcessSP process_sp = platform_sp->ConnectProcess(
926 command.GetArgumentAtIndex(0), plugin_name, debugger,
927 debugger.GetSelectedTarget().get(), error);
928 if (error.Fail() || process_sp == nullptr) {
929 result.AppendError(error.AsCString("Error connecting to the process"));
930 result.SetStatus(eReturnStatusFailed);
936 CommandOptions m_options;
939 //-------------------------------------------------------------------------
940 // CommandObjectProcessPlugin
941 //-------------------------------------------------------------------------
942 #pragma mark CommandObjectProcessPlugin
944 class CommandObjectProcessPlugin : public CommandObjectProxy {
946 CommandObjectProcessPlugin(CommandInterpreter &interpreter)
947 : CommandObjectProxy(
948 interpreter, "process plugin",
949 "Send a custom command to the current target process plug-in.",
950 "process plugin <args>", 0) {}
952 ~CommandObjectProcessPlugin() override = default;
954 CommandObject *GetProxyCommandObject() override {
955 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
957 return process->GetPluginCommandObject();
962 //-------------------------------------------------------------------------
963 // CommandObjectProcessLoad
964 //-------------------------------------------------------------------------
966 static OptionDefinition g_process_load_options[] = {
968 { LLDB_OPT_SET_ALL, false, "install", 'i', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypePath, "Install the shared library to the target. If specified without an argument then the library will installed in the current working directory." },
972 #pragma mark CommandObjectProcessLoad
974 class CommandObjectProcessLoad : public CommandObjectParsed {
976 class CommandOptions : public Options {
978 CommandOptions() : Options() {
979 // Keep default values of all options in one place: OptionParsingStarting
981 OptionParsingStarting(nullptr);
984 ~CommandOptions() override = default;
986 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
987 ExecutionContext *execution_context) override {
989 const int short_option = m_getopt_table[option_idx].val;
990 switch (short_option) {
993 if (!option_arg.empty())
994 install_path.SetFile(option_arg, false);
997 error.SetErrorStringWithFormat("invalid short option character '%c'",
1004 void OptionParsingStarting(ExecutionContext *execution_context) override {
1006 install_path.Clear();
1009 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1010 return llvm::makeArrayRef(g_process_load_options);
1013 // Instance variables to hold the values for command options.
1015 FileSpec install_path;
1018 CommandObjectProcessLoad(CommandInterpreter &interpreter)
1019 : CommandObjectParsed(interpreter, "process load",
1020 "Load a shared library into the current process.",
1021 "process load <filename> [<filename> ...]",
1022 eCommandRequiresProcess | eCommandTryTargetAPILock |
1023 eCommandProcessMustBeLaunched |
1024 eCommandProcessMustBePaused),
1027 ~CommandObjectProcessLoad() override = default;
1029 Options *GetOptions() override { return &m_options; }
1032 bool DoExecute(Args &command, CommandReturnObject &result) override {
1033 Process *process = m_exe_ctx.GetProcessPtr();
1035 for (auto &entry : command.entries()) {
1037 PlatformSP platform = process->GetTarget().GetPlatform();
1038 llvm::StringRef image_path = entry.ref;
1039 uint32_t image_token = LLDB_INVALID_IMAGE_TOKEN;
1041 if (!m_options.do_install) {
1042 FileSpec image_spec(image_path, false);
1043 platform->ResolveRemotePath(image_spec, image_spec);
1045 platform->LoadImage(process, FileSpec(), image_spec, error);
1046 } else if (m_options.install_path) {
1047 FileSpec image_spec(image_path, true);
1048 platform->ResolveRemotePath(m_options.install_path,
1049 m_options.install_path);
1050 image_token = platform->LoadImage(process, image_spec,
1051 m_options.install_path, error);
1053 FileSpec image_spec(image_path, true);
1055 platform->LoadImage(process, image_spec, FileSpec(), error);
1058 if (image_token != LLDB_INVALID_IMAGE_TOKEN) {
1059 result.AppendMessageWithFormat(
1060 "Loading \"%s\"...ok\nImage %u loaded.\n", image_path.str().c_str(),
1062 result.SetStatus(eReturnStatusSuccessFinishResult);
1064 result.AppendErrorWithFormat("failed to load '%s': %s",
1065 image_path.str().c_str(),
1067 result.SetStatus(eReturnStatusFailed);
1070 return result.Succeeded();
1073 CommandOptions m_options;
1076 //-------------------------------------------------------------------------
1077 // CommandObjectProcessUnload
1078 //-------------------------------------------------------------------------
1079 #pragma mark CommandObjectProcessUnload
1081 class CommandObjectProcessUnload : public CommandObjectParsed {
1083 CommandObjectProcessUnload(CommandInterpreter &interpreter)
1084 : CommandObjectParsed(
1085 interpreter, "process unload",
1086 "Unload a shared library from the current process using the index "
1087 "returned by a previous call to \"process load\".",
1088 "process unload <index>",
1089 eCommandRequiresProcess | eCommandTryTargetAPILock |
1090 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
1092 ~CommandObjectProcessUnload() override = default;
1095 bool DoExecute(Args &command, CommandReturnObject &result) override {
1096 Process *process = m_exe_ctx.GetProcessPtr();
1098 for (auto &entry : command.entries()) {
1099 uint32_t image_token;
1100 if (entry.ref.getAsInteger(0, image_token)) {
1101 result.AppendErrorWithFormat("invalid image index argument '%s'",
1102 entry.ref.str().c_str());
1103 result.SetStatus(eReturnStatusFailed);
1106 Status error(process->GetTarget().GetPlatform()->UnloadImage(
1107 process, image_token));
1108 if (error.Success()) {
1109 result.AppendMessageWithFormat(
1110 "Unloading shared library with index %u...ok\n", image_token);
1111 result.SetStatus(eReturnStatusSuccessFinishResult);
1113 result.AppendErrorWithFormat("failed to unload image: %s",
1115 result.SetStatus(eReturnStatusFailed);
1120 return result.Succeeded();
1124 //-------------------------------------------------------------------------
1125 // CommandObjectProcessSignal
1126 //-------------------------------------------------------------------------
1127 #pragma mark CommandObjectProcessSignal
1129 class CommandObjectProcessSignal : public CommandObjectParsed {
1131 CommandObjectProcessSignal(CommandInterpreter &interpreter)
1132 : CommandObjectParsed(interpreter, "process signal",
1133 "Send a UNIX signal to the current target process.",
1134 nullptr, eCommandRequiresProcess |
1135 eCommandTryTargetAPILock) {
1136 CommandArgumentEntry arg;
1137 CommandArgumentData signal_arg;
1139 // Define the first (and only) variant of this arg.
1140 signal_arg.arg_type = eArgTypeUnixSignal;
1141 signal_arg.arg_repetition = eArgRepeatPlain;
1143 // There is only one variant this argument could be; put it into the
1145 arg.push_back(signal_arg);
1147 // Push the data for the first argument into the m_arguments vector.
1148 m_arguments.push_back(arg);
1151 ~CommandObjectProcessSignal() override = default;
1154 bool DoExecute(Args &command, CommandReturnObject &result) override {
1155 Process *process = m_exe_ctx.GetProcessPtr();
1157 if (command.GetArgumentCount() == 1) {
1158 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1160 const char *signal_name = command.GetArgumentAtIndex(0);
1161 if (::isxdigit(signal_name[0]))
1163 StringConvert::ToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1165 signo = process->GetUnixSignals()->GetSignalNumberFromName(signal_name);
1167 if (signo == LLDB_INVALID_SIGNAL_NUMBER) {
1168 result.AppendErrorWithFormat("Invalid signal argument '%s'.\n",
1169 command.GetArgumentAtIndex(0));
1170 result.SetStatus(eReturnStatusFailed);
1172 Status error(process->Signal(signo));
1173 if (error.Success()) {
1174 result.SetStatus(eReturnStatusSuccessFinishResult);
1176 result.AppendErrorWithFormat("Failed to send signal %i: %s\n", signo,
1178 result.SetStatus(eReturnStatusFailed);
1182 result.AppendErrorWithFormat(
1183 "'%s' takes exactly one signal number argument:\nUsage: %s\n",
1184 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1185 result.SetStatus(eReturnStatusFailed);
1187 return result.Succeeded();
1191 //-------------------------------------------------------------------------
1192 // CommandObjectProcessInterrupt
1193 //-------------------------------------------------------------------------
1194 #pragma mark CommandObjectProcessInterrupt
1196 class CommandObjectProcessInterrupt : public CommandObjectParsed {
1198 CommandObjectProcessInterrupt(CommandInterpreter &interpreter)
1199 : CommandObjectParsed(interpreter, "process interrupt",
1200 "Interrupt the current target process.",
1201 "process interrupt",
1202 eCommandRequiresProcess | eCommandTryTargetAPILock |
1203 eCommandProcessMustBeLaunched) {}
1205 ~CommandObjectProcessInterrupt() override = default;
1208 bool DoExecute(Args &command, CommandReturnObject &result) override {
1209 Process *process = m_exe_ctx.GetProcessPtr();
1210 if (process == nullptr) {
1211 result.AppendError("no process to halt");
1212 result.SetStatus(eReturnStatusFailed);
1216 if (command.GetArgumentCount() == 0) {
1217 bool clear_thread_plans = true;
1218 Status error(process->Halt(clear_thread_plans));
1219 if (error.Success()) {
1220 result.SetStatus(eReturnStatusSuccessFinishResult);
1222 result.AppendErrorWithFormat("Failed to halt process: %s\n",
1224 result.SetStatus(eReturnStatusFailed);
1227 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
1228 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1229 result.SetStatus(eReturnStatusFailed);
1231 return result.Succeeded();
1235 //-------------------------------------------------------------------------
1236 // CommandObjectProcessKill
1237 //-------------------------------------------------------------------------
1238 #pragma mark CommandObjectProcessKill
1240 class CommandObjectProcessKill : public CommandObjectParsed {
1242 CommandObjectProcessKill(CommandInterpreter &interpreter)
1243 : CommandObjectParsed(interpreter, "process kill",
1244 "Terminate the current target process.",
1246 eCommandRequiresProcess | eCommandTryTargetAPILock |
1247 eCommandProcessMustBeLaunched) {}
1249 ~CommandObjectProcessKill() override = default;
1252 bool DoExecute(Args &command, CommandReturnObject &result) override {
1253 Process *process = m_exe_ctx.GetProcessPtr();
1254 if (process == nullptr) {
1255 result.AppendError("no process to kill");
1256 result.SetStatus(eReturnStatusFailed);
1260 if (command.GetArgumentCount() == 0) {
1261 Status error(process->Destroy(true));
1262 if (error.Success()) {
1263 result.SetStatus(eReturnStatusSuccessFinishResult);
1265 result.AppendErrorWithFormat("Failed to kill process: %s\n",
1267 result.SetStatus(eReturnStatusFailed);
1270 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
1271 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1272 result.SetStatus(eReturnStatusFailed);
1274 return result.Succeeded();
1278 //-------------------------------------------------------------------------
1279 // CommandObjectProcessSaveCore
1280 //-------------------------------------------------------------------------
1281 #pragma mark CommandObjectProcessSaveCore
1283 class CommandObjectProcessSaveCore : public CommandObjectParsed {
1285 CommandObjectProcessSaveCore(CommandInterpreter &interpreter)
1286 : CommandObjectParsed(interpreter, "process save-core",
1287 "Save the current process as a core file using an "
1288 "appropriate file type.",
1289 "process save-core FILE",
1290 eCommandRequiresProcess | eCommandTryTargetAPILock |
1291 eCommandProcessMustBeLaunched) {}
1293 ~CommandObjectProcessSaveCore() override = default;
1296 bool DoExecute(Args &command, CommandReturnObject &result) override {
1297 ProcessSP process_sp = m_exe_ctx.GetProcessSP();
1299 if (command.GetArgumentCount() == 1) {
1300 FileSpec output_file(command.GetArgumentAtIndex(0), false);
1301 Status error = PluginManager::SaveCore(process_sp, output_file);
1302 if (error.Success()) {
1303 result.SetStatus(eReturnStatusSuccessFinishResult);
1305 result.AppendErrorWithFormat(
1306 "Failed to save core file for process: %s\n", error.AsCString());
1307 result.SetStatus(eReturnStatusFailed);
1310 result.AppendErrorWithFormat("'%s' takes one arguments:\nUsage: %s\n",
1311 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1312 result.SetStatus(eReturnStatusFailed);
1315 result.AppendError("invalid process");
1316 result.SetStatus(eReturnStatusFailed);
1320 return result.Succeeded();
1324 //-------------------------------------------------------------------------
1325 // CommandObjectProcessStatus
1326 //-------------------------------------------------------------------------
1327 #pragma mark CommandObjectProcessStatus
1329 class CommandObjectProcessStatus : public CommandObjectParsed {
1331 CommandObjectProcessStatus(CommandInterpreter &interpreter)
1332 : CommandObjectParsed(
1333 interpreter, "process status",
1334 "Show status and stop location for the current target process.",
1336 eCommandRequiresProcess | eCommandTryTargetAPILock) {}
1338 ~CommandObjectProcessStatus() override = default;
1340 bool DoExecute(Args &command, CommandReturnObject &result) override {
1341 Stream &strm = result.GetOutputStream();
1342 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1343 // No need to check "process" for validity as eCommandRequiresProcess
1344 // ensures it is valid
1345 Process *process = m_exe_ctx.GetProcessPtr();
1346 const bool only_threads_with_stop_reason = true;
1347 const uint32_t start_frame = 0;
1348 const uint32_t num_frames = 1;
1349 const uint32_t num_frames_with_source = 1;
1350 const bool stop_format = true;
1351 process->GetStatus(strm);
1352 process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame,
1353 num_frames, num_frames_with_source, stop_format);
1354 return result.Succeeded();
1358 //-------------------------------------------------------------------------
1359 // CommandObjectProcessHandle
1360 //-------------------------------------------------------------------------
1362 static OptionDefinition g_process_handle_options[] = {
1364 { LLDB_OPT_SET_1, false, "stop", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Whether or not the process should be stopped if the signal is received." },
1365 { LLDB_OPT_SET_1, false, "notify", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Whether or not the debugger should notify the user if the signal is received." },
1366 { LLDB_OPT_SET_1, false, "pass", 'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." }
1370 #pragma mark CommandObjectProcessHandle
1372 class CommandObjectProcessHandle : public CommandObjectParsed {
1374 class CommandOptions : public Options {
1376 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
1378 ~CommandOptions() override = default;
1380 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1381 ExecutionContext *execution_context) override {
1383 const int short_option = m_getopt_table[option_idx].val;
1385 switch (short_option) {
1390 notify = option_arg;
1396 error.SetErrorStringWithFormat("invalid short option character '%c'",
1403 void OptionParsingStarting(ExecutionContext *execution_context) override {
1409 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1410 return llvm::makeArrayRef(g_process_handle_options);
1413 // Instance variables to hold the values for command options.
1420 CommandObjectProcessHandle(CommandInterpreter &interpreter)
1421 : CommandObjectParsed(interpreter, "process handle",
1422 "Manage LLDB handling of OS signals for the "
1423 "current target process. Defaults to showing "
1427 SetHelpLong("\nIf no signals are specified, update them all. If no update "
1428 "option is specified, list the current values.");
1429 CommandArgumentEntry arg;
1430 CommandArgumentData signal_arg;
1432 signal_arg.arg_type = eArgTypeUnixSignal;
1433 signal_arg.arg_repetition = eArgRepeatStar;
1435 arg.push_back(signal_arg);
1437 m_arguments.push_back(arg);
1440 ~CommandObjectProcessHandle() override = default;
1442 Options *GetOptions() override { return &m_options; }
1444 bool VerifyCommandOptionValue(const std::string &option, int &real_value) {
1446 bool success = false;
1447 bool tmp_value = Args::StringToBoolean(option, false, &success);
1449 if (success && tmp_value)
1451 else if (success && !tmp_value)
1454 // If the value isn't 'true' or 'false', it had better be 0 or 1.
1455 real_value = StringConvert::ToUInt32(option.c_str(), 3);
1456 if (real_value != 0 && real_value != 1)
1463 void PrintSignalHeader(Stream &str) {
1464 str.Printf("NAME PASS STOP NOTIFY\n");
1465 str.Printf("=========== ===== ===== ======\n");
1468 void PrintSignal(Stream &str, int32_t signo, const char *sig_name,
1469 const UnixSignalsSP &signals_sp) {
1474 str.Printf("%-11s ", sig_name);
1475 if (signals_sp->GetSignalInfo(signo, suppress, stop, notify)) {
1476 bool pass = !suppress;
1477 str.Printf("%s %s %s", (pass ? "true " : "false"),
1478 (stop ? "true " : "false"), (notify ? "true " : "false"));
1483 void PrintSignalInformation(Stream &str, Args &signal_args,
1484 int num_valid_signals,
1485 const UnixSignalsSP &signals_sp) {
1486 PrintSignalHeader(str);
1488 if (num_valid_signals > 0) {
1489 size_t num_args = signal_args.GetArgumentCount();
1490 for (size_t i = 0; i < num_args; ++i) {
1491 int32_t signo = signals_sp->GetSignalNumberFromName(
1492 signal_args.GetArgumentAtIndex(i));
1493 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1494 PrintSignal(str, signo, signal_args.GetArgumentAtIndex(i),
1497 } else // Print info for ALL signals
1499 int32_t signo = signals_sp->GetFirstSignalNumber();
1500 while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1501 PrintSignal(str, signo, signals_sp->GetSignalAsCString(signo),
1503 signo = signals_sp->GetNextSignalNumber(signo);
1509 bool DoExecute(Args &signal_args, CommandReturnObject &result) override {
1510 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1513 result.AppendError("No current target;"
1514 " cannot handle signals until you have a valid target "
1516 result.SetStatus(eReturnStatusFailed);
1520 ProcessSP process_sp = target_sp->GetProcessSP();
1523 result.AppendError("No current process; cannot handle signals until you "
1524 "have a valid process.\n");
1525 result.SetStatus(eReturnStatusFailed);
1529 int stop_action = -1; // -1 means leave the current setting alone
1530 int pass_action = -1; // -1 means leave the current setting alone
1531 int notify_action = -1; // -1 means leave the current setting alone
1533 if (!m_options.stop.empty() &&
1534 !VerifyCommandOptionValue(m_options.stop, stop_action)) {
1535 result.AppendError("Invalid argument for command option --stop; must be "
1536 "true or false.\n");
1537 result.SetStatus(eReturnStatusFailed);
1541 if (!m_options.notify.empty() &&
1542 !VerifyCommandOptionValue(m_options.notify, notify_action)) {
1543 result.AppendError("Invalid argument for command option --notify; must "
1544 "be true or false.\n");
1545 result.SetStatus(eReturnStatusFailed);
1549 if (!m_options.pass.empty() &&
1550 !VerifyCommandOptionValue(m_options.pass, pass_action)) {
1551 result.AppendError("Invalid argument for command option --pass; must be "
1552 "true or false.\n");
1553 result.SetStatus(eReturnStatusFailed);
1557 size_t num_args = signal_args.GetArgumentCount();
1558 UnixSignalsSP signals_sp = process_sp->GetUnixSignals();
1559 int num_signals_set = 0;
1562 for (const auto &arg : signal_args) {
1563 int32_t signo = signals_sp->GetSignalNumberFromName(arg.c_str());
1564 if (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1565 // Casting the actions as bools here should be okay, because
1566 // VerifyCommandOptionValue guarantees
1567 // the value is either 0 or 1.
1568 if (stop_action != -1)
1569 signals_sp->SetShouldStop(signo, stop_action);
1570 if (pass_action != -1) {
1571 bool suppress = !pass_action;
1572 signals_sp->SetShouldSuppress(signo, suppress);
1574 if (notify_action != -1)
1575 signals_sp->SetShouldNotify(signo, notify_action);
1578 result.AppendErrorWithFormat("Invalid signal name '%s'\n",
1583 // No signal specified, if any command options were specified, update ALL
1585 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1)) {
1586 if (m_interpreter.Confirm(
1587 "Do you really want to update all the signals?", false)) {
1588 int32_t signo = signals_sp->GetFirstSignalNumber();
1589 while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1590 if (notify_action != -1)
1591 signals_sp->SetShouldNotify(signo, notify_action);
1592 if (stop_action != -1)
1593 signals_sp->SetShouldStop(signo, stop_action);
1594 if (pass_action != -1) {
1595 bool suppress = !pass_action;
1596 signals_sp->SetShouldSuppress(signo, suppress);
1598 signo = signals_sp->GetNextSignalNumber(signo);
1604 PrintSignalInformation(result.GetOutputStream(), signal_args,
1605 num_signals_set, signals_sp);
1607 if (num_signals_set > 0)
1608 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1610 result.SetStatus(eReturnStatusFailed);
1612 return result.Succeeded();
1615 CommandOptions m_options;
1618 //-------------------------------------------------------------------------
1619 // CommandObjectMultiwordProcess
1620 //-------------------------------------------------------------------------
1622 CommandObjectMultiwordProcess::CommandObjectMultiwordProcess(
1623 CommandInterpreter &interpreter)
1624 : CommandObjectMultiword(
1625 interpreter, "process",
1626 "Commands for interacting with processes on the current platform.",
1627 "process <subcommand> [<subcommand-options>]") {
1628 LoadSubCommand("attach",
1629 CommandObjectSP(new CommandObjectProcessAttach(interpreter)));
1630 LoadSubCommand("launch",
1631 CommandObjectSP(new CommandObjectProcessLaunch(interpreter)));
1632 LoadSubCommand("continue", CommandObjectSP(new CommandObjectProcessContinue(
1634 LoadSubCommand("connect",
1635 CommandObjectSP(new CommandObjectProcessConnect(interpreter)));
1636 LoadSubCommand("detach",
1637 CommandObjectSP(new CommandObjectProcessDetach(interpreter)));
1638 LoadSubCommand("load",
1639 CommandObjectSP(new CommandObjectProcessLoad(interpreter)));
1640 LoadSubCommand("unload",
1641 CommandObjectSP(new CommandObjectProcessUnload(interpreter)));
1642 LoadSubCommand("signal",
1643 CommandObjectSP(new CommandObjectProcessSignal(interpreter)));
1644 LoadSubCommand("handle",
1645 CommandObjectSP(new CommandObjectProcessHandle(interpreter)));
1646 LoadSubCommand("status",
1647 CommandObjectSP(new CommandObjectProcessStatus(interpreter)));
1648 LoadSubCommand("interrupt", CommandObjectSP(new CommandObjectProcessInterrupt(
1650 LoadSubCommand("kill",
1651 CommandObjectSP(new CommandObjectProcessKill(interpreter)));
1652 LoadSubCommand("plugin",
1653 CommandObjectSP(new CommandObjectProcessPlugin(interpreter)));
1654 LoadSubCommand("save-core", CommandObjectSP(new CommandObjectProcessSaveCore(
1658 CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess() = default;