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/CommandInterpreter.h"
25 #include "lldb/Interpreter/CommandReturnObject.h"
26 #include "lldb/Interpreter/OptionArgParser.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"
34 #include "lldb/Utility/Args.h"
37 using namespace lldb_private;
39 class CommandObjectProcessLaunchOrAttach : public CommandObjectParsed {
41 CommandObjectProcessLaunchOrAttach(CommandInterpreter &interpreter,
42 const char *name, const char *help,
43 const char *syntax, uint32_t flags,
44 const char *new_process_action)
45 : CommandObjectParsed(interpreter, name, help, syntax, flags),
46 m_new_process_action(new_process_action) {}
48 ~CommandObjectProcessLaunchOrAttach() override = default;
51 bool StopProcessIfNecessary(Process *process, StateType &state,
52 CommandReturnObject &result) {
53 state = eStateInvalid;
55 state = process->GetState();
57 if (process->IsAlive() && state != eStateConnected) {
59 if (process->GetState() == eStateAttaching)
60 ::snprintf(message, sizeof(message),
61 "There is a pending attach, abort it and %s?",
62 m_new_process_action.c_str());
63 else if (process->GetShouldDetach())
64 ::snprintf(message, sizeof(message),
65 "There is a running process, detach from it and %s?",
66 m_new_process_action.c_str());
68 ::snprintf(message, sizeof(message),
69 "There is a running process, kill it and %s?",
70 m_new_process_action.c_str());
72 if (!m_interpreter.Confirm(message, true)) {
73 result.SetStatus(eReturnStatusFailed);
76 if (process->GetShouldDetach()) {
77 bool keep_stopped = false;
78 Status detach_error(process->Detach(keep_stopped));
79 if (detach_error.Success()) {
80 result.SetStatus(eReturnStatusSuccessFinishResult);
83 result.AppendErrorWithFormat(
84 "Failed to detach from process: %s\n",
85 detach_error.AsCString());
86 result.SetStatus(eReturnStatusFailed);
89 Status destroy_error(process->Destroy(false));
90 if (destroy_error.Success()) {
91 result.SetStatus(eReturnStatusSuccessFinishResult);
94 result.AppendErrorWithFormat("Failed to kill process: %s\n",
95 destroy_error.AsCString());
96 result.SetStatus(eReturnStatusFailed);
102 return result.Succeeded();
105 std::string m_new_process_action;
108 //-------------------------------------------------------------------------
109 // CommandObjectProcessLaunch
110 //-------------------------------------------------------------------------
111 #pragma mark CommandObjectProcessLaunch
112 class CommandObjectProcessLaunch : public CommandObjectProcessLaunchOrAttach {
114 CommandObjectProcessLaunch(CommandInterpreter &interpreter)
115 : CommandObjectProcessLaunchOrAttach(
116 interpreter, "process launch",
117 "Launch the executable in the debugger.", nullptr,
118 eCommandRequiresTarget, "restart"),
120 CommandArgumentEntry arg;
121 CommandArgumentData run_args_arg;
123 // Define the first (and only) variant of this arg.
124 run_args_arg.arg_type = eArgTypeRunArgs;
125 run_args_arg.arg_repetition = eArgRepeatOptional;
127 // There is only one variant this argument could be; put it into the
129 arg.push_back(run_args_arg);
131 // Push the data for the first argument into the m_arguments vector.
132 m_arguments.push_back(arg);
135 ~CommandObjectProcessLaunch() override = default;
137 int HandleArgumentCompletion(
138 CompletionRequest &request,
139 OptionElementVector &opt_element_vector) override {
141 CommandCompletions::InvokeCommonCompletionCallbacks(
142 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
144 return request.GetNumberOfMatches();
147 Options *GetOptions() override { return &m_options; }
149 const char *GetRepeatCommand(Args ¤t_command_args,
150 uint32_t index) override {
151 // No repeat for "process launch"...
156 bool DoExecute(Args &launch_args, CommandReturnObject &result) override {
157 Debugger &debugger = m_interpreter.GetDebugger();
158 Target *target = debugger.GetSelectedTarget().get();
159 // If our listener is nullptr, users aren't allows to launch
160 ModuleSP exe_module_sp = target->GetExecutableModule();
162 if (exe_module_sp == nullptr) {
163 result.AppendError("no file in target, create a debug target using the "
164 "'target create' command");
165 result.SetStatus(eReturnStatusFailed);
169 StateType state = eStateInvalid;
171 if (!StopProcessIfNecessary(m_exe_ctx.GetProcessPtr(), state, result))
174 llvm::StringRef target_settings_argv0 = target->GetArg0();
176 // Determine whether we will disable ASLR or leave it in the default state
177 // (i.e. enabled if the platform supports it). First check if the process
178 // launch options explicitly turn on/off
179 // disabling ASLR. If so, use that setting;
180 // otherwise, use the 'settings target.disable-aslr' setting.
181 bool disable_aslr = false;
182 if (m_options.disable_aslr != eLazyBoolCalculate) {
183 // The user specified an explicit setting on the process launch line.
185 disable_aslr = (m_options.disable_aslr == eLazyBoolYes);
187 // The user did not explicitly specify whether to disable ASLR. Fall
188 // back to the target.disable-aslr setting.
189 disable_aslr = target->GetDisableASLR();
193 m_options.launch_info.GetFlags().Set(eLaunchFlagDisableASLR);
195 m_options.launch_info.GetFlags().Clear(eLaunchFlagDisableASLR);
197 if (target->GetDetachOnError())
198 m_options.launch_info.GetFlags().Set(eLaunchFlagDetachOnError);
200 if (target->GetDisableSTDIO())
201 m_options.launch_info.GetFlags().Set(eLaunchFlagDisableSTDIO);
203 m_options.launch_info.GetEnvironment() = target->GetEnvironment();
205 if (!target_settings_argv0.empty()) {
206 m_options.launch_info.GetArguments().AppendArgument(
207 target_settings_argv0);
208 m_options.launch_info.SetExecutableFile(
209 exe_module_sp->GetPlatformFileSpec(), false);
211 m_options.launch_info.SetExecutableFile(
212 exe_module_sp->GetPlatformFileSpec(), true);
215 if (launch_args.GetArgumentCount() == 0) {
216 m_options.launch_info.GetArguments().AppendArguments(
217 target->GetProcessLaunchInfo().GetArguments());
219 m_options.launch_info.GetArguments().AppendArguments(launch_args);
220 // Save the arguments for subsequent runs in the current target.
221 target->SetRunArguments(launch_args);
225 Status error = target->Launch(m_options.launch_info, &stream);
227 if (error.Success()) {
228 ProcessSP process_sp(target->GetProcessSP());
230 // There is a race condition where this thread will return up the call
231 // stack to the main command handler and show an (lldb) prompt before
232 // HandlePrivateEvent (from PrivateStateThread) has a chance to call
233 // PushProcessIOHandler().
234 process_sp->SyncIOHandler(0, std::chrono::seconds(2));
236 llvm::StringRef data = stream.GetString();
238 result.AppendMessage(data);
239 const char *archname =
240 exe_module_sp->GetArchitecture().GetArchitectureName();
241 result.AppendMessageWithFormat(
242 "Process %" PRIu64 " launched: '%s' (%s)\n", process_sp->GetID(),
243 exe_module_sp->GetFileSpec().GetPath().c_str(), archname);
244 result.SetStatus(eReturnStatusSuccessFinishResult);
245 result.SetDidChangeProcessState(true);
248 "no error returned from Target::Launch, and target has no process");
249 result.SetStatus(eReturnStatusFailed);
252 result.AppendError(error.AsCString());
253 result.SetStatus(eReturnStatusFailed);
255 return result.Succeeded();
259 ProcessLaunchCommandOptions m_options;
262 //#define SET1 LLDB_OPT_SET_1
263 //#define SET2 LLDB_OPT_SET_2
264 //#define SET3 LLDB_OPT_SET_3
267 // CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
269 // // clang-format off
270 // {SET1 | SET2 | SET3, false, "stop-at-entry", 's', OptionParser::eNoArgument,
271 // nullptr, 0, eArgTypeNone, "Stop at the entry point of the program
272 // when launching a process."},
273 // {SET1, false, "stdin", 'i',
274 // OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
275 // "Redirect stdin for the process to <path>."},
276 // {SET1, false, "stdout", 'o',
277 // OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
278 // "Redirect stdout for the process to <path>."},
279 // {SET1, false, "stderr", 'e',
280 // OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
281 // "Redirect stderr for the process to <path>."},
282 // {SET1 | SET2 | SET3, false, "plugin", 'p',
283 // OptionParser::eRequiredArgument, nullptr, 0, eArgTypePlugin, "Name of
284 // the process plugin you want to use."},
285 // { SET2, false, "tty", 't',
286 // OptionParser::eOptionalArgument, nullptr, 0, eArgTypeDirectoryName, "Start
287 // the process in a terminal. If <path> is specified, look for a terminal whose
288 // name contains <path>, else start the process in a new terminal."},
289 // { SET3, false, "no-stdio", 'n', OptionParser::eNoArgument,
290 // nullptr, 0, eArgTypeNone, "Do not set up for terminal I/O to go to
291 // running process."},
292 // {SET1 | SET2 | SET3, false, "working-dir", 'w',
293 // OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName, "Set the
294 // current working directory to <path> when running the inferior."},
295 // {0, false, nullptr, 0, 0, nullptr, 0, eArgTypeNone, nullptr}
296 // // clang-format on
303 //-------------------------------------------------------------------------
304 // CommandObjectProcessAttach
305 //-------------------------------------------------------------------------
307 static OptionDefinition g_process_attach_options[] = {
309 { LLDB_OPT_SET_ALL, false, "continue", 'c', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Immediately continue the process once attached." },
310 { LLDB_OPT_SET_ALL, false, "plugin", 'P', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePlugin, "Name of the process plugin you want to use." },
311 { LLDB_OPT_SET_1, false, "pid", 'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePid, "The process ID of an existing process to attach to." },
312 { LLDB_OPT_SET_2, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeProcessName, "The name of the process to attach to." },
313 { LLDB_OPT_SET_2, false, "include-existing", 'i', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Include existing processes when doing attach -w." },
314 { LLDB_OPT_SET_2, false, "waitfor", 'w', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Wait for the process with <process-name> to launch." },
318 #pragma mark CommandObjectProcessAttach
319 class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach {
321 class CommandOptions : public Options {
323 CommandOptions() : Options() {
324 // Keep default values of all options in one place: OptionParsingStarting
326 OptionParsingStarting(nullptr);
329 ~CommandOptions() override = default;
331 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
332 ExecutionContext *execution_context) override {
334 const int short_option = m_getopt_table[option_idx].val;
335 switch (short_option) {
337 attach_info.SetContinueOnceAttached(true);
342 if (option_arg.getAsInteger(0, pid)) {
343 error.SetErrorStringWithFormat("invalid process ID '%s'",
344 option_arg.str().c_str());
346 attach_info.SetProcessID(pid);
351 attach_info.SetProcessPluginName(option_arg);
355 attach_info.GetExecutableFile().SetFile(option_arg, false,
356 FileSpec::Style::native);
360 attach_info.SetWaitForLaunch(true);
364 attach_info.SetIgnoreExisting(false);
368 error.SetErrorStringWithFormat("invalid short option character '%c'",
375 void OptionParsingStarting(ExecutionContext *execution_context) override {
379 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
380 return llvm::makeArrayRef(g_process_attach_options);
383 bool HandleOptionArgumentCompletion(
384 CompletionRequest &request, OptionElementVector &opt_element_vector,
385 int opt_element_index, CommandInterpreter &interpreter) override {
386 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
387 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
389 // We are only completing the name option for now...
391 if (GetDefinitions()[opt_defs_index].short_option == 'n') {
392 // Are we in the name?
394 // Look to see if there is a -P argument provided, and if so use that
395 // plugin, otherwise use the default plugin.
397 const char *partial_name = nullptr;
398 partial_name = request.GetParsedLine().GetArgumentAtIndex(opt_arg_pos);
400 PlatformSP platform_sp(interpreter.GetPlatform(true));
402 ProcessInstanceInfoList process_infos;
403 ProcessInstanceInfoMatch match_info;
405 match_info.GetProcessInfo().GetExecutableFile().SetFile(
406 partial_name, false, FileSpec::Style::native);
407 match_info.SetNameMatchType(NameMatch::StartsWith);
409 platform_sp->FindProcesses(match_info, process_infos);
410 const size_t num_matches = process_infos.GetSize();
411 if (num_matches > 0) {
412 for (size_t i = 0; i < num_matches; ++i) {
413 request.AddCompletion(llvm::StringRef(
414 process_infos.GetProcessNameAtIndex(i),
415 process_infos.GetProcessNameLengthAtIndex(i)));
424 // Instance variables to hold the values for command options.
426 ProcessAttachInfo attach_info;
429 CommandObjectProcessAttach(CommandInterpreter &interpreter)
430 : CommandObjectProcessLaunchOrAttach(
431 interpreter, "process attach", "Attach to a process.",
432 "process attach <cmd-options>", 0, "attach"),
435 ~CommandObjectProcessAttach() override = default;
437 Options *GetOptions() override { return &m_options; }
440 bool DoExecute(Args &command, CommandReturnObject &result) override {
441 PlatformSP platform_sp(
442 m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform());
444 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
445 // N.B. The attach should be synchronous. It doesn't help much to get the
446 // prompt back between initiating the attach and the target actually
447 // stopping. So even if the interpreter is set to be asynchronous, we wait
448 // for the stop ourselves here.
450 StateType state = eStateInvalid;
451 Process *process = m_exe_ctx.GetProcessPtr();
453 if (!StopProcessIfNecessary(process, state, result))
456 if (target == nullptr) {
457 // If there isn't a current target create one.
458 TargetSP new_target_sp;
461 error = m_interpreter.GetDebugger().GetTargetList().CreateTarget(
462 m_interpreter.GetDebugger(), "", "", false,
463 nullptr, // No platform options
465 target = new_target_sp.get();
466 if (target == nullptr || error.Fail()) {
467 result.AppendError(error.AsCString("Error creating target"));
470 m_interpreter.GetDebugger().GetTargetList().SetSelectedTarget(target);
473 // Record the old executable module, we want to issue a warning if the
474 // process of attaching changed the current executable (like somebody said
475 // "file foo" then attached to a PID whose executable was bar.)
477 ModuleSP old_exec_module_sp = target->GetExecutableModule();
478 ArchSpec old_arch_spec = target->GetArchitecture();
480 if (command.GetArgumentCount()) {
481 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: %s\n",
482 m_cmd_name.c_str(), m_cmd_syntax.c_str());
483 result.SetStatus(eReturnStatusFailed);
487 m_interpreter.UpdateExecutionContext(nullptr);
489 const auto error = target->Attach(m_options.attach_info, &stream);
490 if (error.Success()) {
491 ProcessSP process_sp(target->GetProcessSP());
493 result.AppendMessage(stream.GetString());
494 result.SetStatus(eReturnStatusSuccessFinishNoResult);
495 result.SetDidChangeProcessState(true);
496 result.SetAbnormalStopWasExpected(true);
499 "no error returned from Target::Attach, and target has no process");
500 result.SetStatus(eReturnStatusFailed);
503 result.AppendErrorWithFormat("attach failed: %s\n", error.AsCString());
504 result.SetStatus(eReturnStatusFailed);
507 if (!result.Succeeded())
510 // Okay, we're done. Last step is to warn if the executable module has
512 char new_path[PATH_MAX];
513 ModuleSP new_exec_module_sp(target->GetExecutableModule());
514 if (!old_exec_module_sp) {
515 // We might not have a module if we attached to a raw pid...
516 if (new_exec_module_sp) {
517 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
518 result.AppendMessageWithFormat("Executable module set to \"%s\".\n",
521 } else if (old_exec_module_sp->GetFileSpec() !=
522 new_exec_module_sp->GetFileSpec()) {
523 char old_path[PATH_MAX];
525 old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
526 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
528 result.AppendWarningWithFormat(
529 "Executable module changed from \"%s\" to \"%s\".\n", old_path,
533 if (!old_arch_spec.IsValid()) {
534 result.AppendMessageWithFormat(
535 "Architecture set to: %s.\n",
536 target->GetArchitecture().GetTriple().getTriple().c_str());
537 } else if (!old_arch_spec.IsExactMatch(target->GetArchitecture())) {
538 result.AppendWarningWithFormat(
539 "Architecture changed from %s to %s.\n",
540 old_arch_spec.GetTriple().getTriple().c_str(),
541 target->GetArchitecture().GetTriple().getTriple().c_str());
544 // This supports the use-case scenario of immediately continuing the
545 // process once attached.
546 if (m_options.attach_info.GetContinueOnceAttached())
547 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
549 return result.Succeeded();
552 CommandOptions m_options;
555 //-------------------------------------------------------------------------
556 // CommandObjectProcessContinue
557 //-------------------------------------------------------------------------
559 static OptionDefinition g_process_continue_options[] = {
561 { 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." }
565 #pragma mark CommandObjectProcessContinue
567 class CommandObjectProcessContinue : public CommandObjectParsed {
569 CommandObjectProcessContinue(CommandInterpreter &interpreter)
570 : CommandObjectParsed(
571 interpreter, "process continue",
572 "Continue execution of all threads in the current process.",
574 eCommandRequiresProcess | eCommandTryTargetAPILock |
575 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
578 ~CommandObjectProcessContinue() override = default;
581 class CommandOptions : public Options {
583 CommandOptions() : Options() {
584 // Keep default values of all options in one place: OptionParsingStarting
586 OptionParsingStarting(nullptr);
589 ~CommandOptions() override = default;
591 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
592 ExecutionContext *execution_context) override {
594 const int short_option = m_getopt_table[option_idx].val;
595 switch (short_option) {
597 if (option_arg.getAsInteger(0, m_ignore))
598 error.SetErrorStringWithFormat(
599 "invalid value for ignore option: \"%s\", should be a number.",
600 option_arg.str().c_str());
604 error.SetErrorStringWithFormat("invalid short option character '%c'",
611 void OptionParsingStarting(ExecutionContext *execution_context) override {
615 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
616 return llvm::makeArrayRef(g_process_continue_options);
622 bool DoExecute(Args &command, CommandReturnObject &result) override {
623 Process *process = m_exe_ctx.GetProcessPtr();
624 bool synchronous_execution = m_interpreter.GetSynchronous();
625 StateType state = process->GetState();
626 if (state == eStateStopped) {
627 if (command.GetArgumentCount() != 0) {
628 result.AppendErrorWithFormat(
629 "The '%s' command does not take any arguments.\n",
631 result.SetStatus(eReturnStatusFailed);
635 if (m_options.m_ignore > 0) {
636 ThreadSP sel_thread_sp(GetDefaultThread()->shared_from_this());
638 StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
640 stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
641 lldb::break_id_t bp_site_id =
642 (lldb::break_id_t)stop_info_sp->GetValue();
643 BreakpointSiteSP bp_site_sp(
644 process->GetBreakpointSiteList().FindByID(bp_site_id));
646 const size_t num_owners = bp_site_sp->GetNumberOfOwners();
647 for (size_t i = 0; i < num_owners; i++) {
649 bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
650 if (!bp_ref.IsInternal()) {
651 bp_ref.SetIgnoreCount(m_options.m_ignore);
659 { // Scope for thread list mutex:
660 std::lock_guard<std::recursive_mutex> guard(
661 process->GetThreadList().GetMutex());
662 const uint32_t num_threads = process->GetThreadList().GetSize();
664 // Set the actions that the threads should each take when resuming
665 for (uint32_t idx = 0; idx < num_threads; ++idx) {
666 const bool override_suspend = false;
667 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState(
668 eStateRunning, override_suspend);
672 const uint32_t iohandler_id = process->GetIOHandlerID();
676 if (synchronous_execution)
677 error = process->ResumeSynchronous(&stream);
679 error = process->Resume();
681 if (error.Success()) {
682 // There is a race condition where this thread will return up the call
683 // stack to the main command handler and show an (lldb) prompt before
684 // HandlePrivateEvent (from PrivateStateThread) has a chance to call
685 // PushProcessIOHandler().
686 process->SyncIOHandler(iohandler_id, std::chrono::seconds(2));
688 result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
690 if (synchronous_execution) {
691 // If any state changed events had anything to say, add that to the
693 result.AppendMessage(stream.GetString());
695 result.SetDidChangeProcessState(true);
696 result.SetStatus(eReturnStatusSuccessFinishNoResult);
698 result.SetStatus(eReturnStatusSuccessContinuingNoResult);
701 result.AppendErrorWithFormat("Failed to resume process: %s.\n",
703 result.SetStatus(eReturnStatusFailed);
706 result.AppendErrorWithFormat(
707 "Process cannot be continued from its current state (%s).\n",
708 StateAsCString(state));
709 result.SetStatus(eReturnStatusFailed);
711 return result.Succeeded();
714 Options *GetOptions() override { return &m_options; }
716 CommandOptions m_options;
719 //-------------------------------------------------------------------------
720 // CommandObjectProcessDetach
721 //-------------------------------------------------------------------------
722 static OptionDefinition g_process_detach_options[] = {
724 { 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)." },
728 #pragma mark CommandObjectProcessDetach
730 class CommandObjectProcessDetach : public CommandObjectParsed {
732 class CommandOptions : public Options {
734 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
736 ~CommandOptions() override = default;
738 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
739 ExecutionContext *execution_context) override {
741 const int short_option = m_getopt_table[option_idx].val;
743 switch (short_option) {
747 tmp_result = OptionArgParser::ToBoolean(option_arg, false, &success);
749 error.SetErrorStringWithFormat("invalid boolean option: \"%s\"",
750 option_arg.str().c_str());
753 m_keep_stopped = eLazyBoolYes;
755 m_keep_stopped = eLazyBoolNo;
759 error.SetErrorStringWithFormat("invalid short option character '%c'",
766 void OptionParsingStarting(ExecutionContext *execution_context) override {
767 m_keep_stopped = eLazyBoolCalculate;
770 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
771 return llvm::makeArrayRef(g_process_detach_options);
774 // Instance variables to hold the values for command options.
775 LazyBool m_keep_stopped;
778 CommandObjectProcessDetach(CommandInterpreter &interpreter)
779 : CommandObjectParsed(interpreter, "process detach",
780 "Detach from the current target process.",
782 eCommandRequiresProcess | eCommandTryTargetAPILock |
783 eCommandProcessMustBeLaunched),
786 ~CommandObjectProcessDetach() override = default;
788 Options *GetOptions() override { return &m_options; }
791 bool DoExecute(Args &command, CommandReturnObject &result) override {
792 Process *process = m_exe_ctx.GetProcessPtr();
793 // FIXME: This will be a Command Option:
795 if (m_options.m_keep_stopped == eLazyBoolCalculate) {
796 // Check the process default:
797 keep_stopped = process->GetDetachKeepsStopped();
798 } else if (m_options.m_keep_stopped == eLazyBoolYes)
801 keep_stopped = false;
803 Status error(process->Detach(keep_stopped));
804 if (error.Success()) {
805 result.SetStatus(eReturnStatusSuccessFinishResult);
807 result.AppendErrorWithFormat("Detach failed: %s\n", error.AsCString());
808 result.SetStatus(eReturnStatusFailed);
811 return result.Succeeded();
814 CommandOptions m_options;
817 //-------------------------------------------------------------------------
818 // CommandObjectProcessConnect
819 //-------------------------------------------------------------------------
821 static OptionDefinition g_process_connect_options[] = {
823 { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePlugin, "Name of the process plugin you want to use." },
827 #pragma mark CommandObjectProcessConnect
829 class CommandObjectProcessConnect : public CommandObjectParsed {
831 class CommandOptions : public Options {
833 CommandOptions() : Options() {
834 // Keep default values of all options in one place: OptionParsingStarting
836 OptionParsingStarting(nullptr);
839 ~CommandOptions() override = default;
841 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
842 ExecutionContext *execution_context) override {
844 const int short_option = m_getopt_table[option_idx].val;
846 switch (short_option) {
848 plugin_name.assign(option_arg);
852 error.SetErrorStringWithFormat("invalid short option character '%c'",
859 void OptionParsingStarting(ExecutionContext *execution_context) override {
863 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
864 return llvm::makeArrayRef(g_process_connect_options);
867 // Instance variables to hold the values for command options.
869 std::string plugin_name;
872 CommandObjectProcessConnect(CommandInterpreter &interpreter)
873 : CommandObjectParsed(interpreter, "process connect",
874 "Connect to a remote debug service.",
875 "process connect <remote-url>", 0),
878 ~CommandObjectProcessConnect() override = default;
880 Options *GetOptions() override { return &m_options; }
883 bool DoExecute(Args &command, CommandReturnObject &result) override {
884 if (command.GetArgumentCount() != 1) {
885 result.AppendErrorWithFormat(
886 "'%s' takes exactly one argument:\nUsage: %s\n", m_cmd_name.c_str(),
887 m_cmd_syntax.c_str());
888 result.SetStatus(eReturnStatusFailed);
892 Process *process = m_exe_ctx.GetProcessPtr();
893 if (process && process->IsAlive()) {
894 result.AppendErrorWithFormat(
896 " is currently being debugged, kill the process before connecting.\n",
898 result.SetStatus(eReturnStatusFailed);
902 const char *plugin_name = nullptr;
903 if (!m_options.plugin_name.empty())
904 plugin_name = m_options.plugin_name.c_str();
907 Debugger &debugger = m_interpreter.GetDebugger();
908 PlatformSP platform_sp = m_interpreter.GetPlatform(true);
909 ProcessSP process_sp = platform_sp->ConnectProcess(
910 command.GetArgumentAtIndex(0), plugin_name, debugger,
911 debugger.GetSelectedTarget().get(), error);
912 if (error.Fail() || process_sp == nullptr) {
913 result.AppendError(error.AsCString("Error connecting to the process"));
914 result.SetStatus(eReturnStatusFailed);
920 CommandOptions m_options;
923 //-------------------------------------------------------------------------
924 // CommandObjectProcessPlugin
925 //-------------------------------------------------------------------------
926 #pragma mark CommandObjectProcessPlugin
928 class CommandObjectProcessPlugin : public CommandObjectProxy {
930 CommandObjectProcessPlugin(CommandInterpreter &interpreter)
931 : CommandObjectProxy(
932 interpreter, "process plugin",
933 "Send a custom command to the current target process plug-in.",
934 "process plugin <args>", 0) {}
936 ~CommandObjectProcessPlugin() override = default;
938 CommandObject *GetProxyCommandObject() override {
939 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
941 return process->GetPluginCommandObject();
946 //-------------------------------------------------------------------------
947 // CommandObjectProcessLoad
948 //-------------------------------------------------------------------------
950 static OptionDefinition g_process_load_options[] = {
952 { 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." },
956 #pragma mark CommandObjectProcessLoad
958 class CommandObjectProcessLoad : public CommandObjectParsed {
960 class CommandOptions : public Options {
962 CommandOptions() : Options() {
963 // Keep default values of all options in one place: OptionParsingStarting
965 OptionParsingStarting(nullptr);
968 ~CommandOptions() override = default;
970 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
971 ExecutionContext *execution_context) override {
973 const int short_option = m_getopt_table[option_idx].val;
974 switch (short_option) {
977 if (!option_arg.empty())
978 install_path.SetFile(option_arg, false, FileSpec::Style::native);
981 error.SetErrorStringWithFormat("invalid short option character '%c'",
988 void OptionParsingStarting(ExecutionContext *execution_context) override {
990 install_path.Clear();
993 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
994 return llvm::makeArrayRef(g_process_load_options);
997 // Instance variables to hold the values for command options.
999 FileSpec install_path;
1002 CommandObjectProcessLoad(CommandInterpreter &interpreter)
1003 : CommandObjectParsed(interpreter, "process load",
1004 "Load a shared library into the current process.",
1005 "process load <filename> [<filename> ...]",
1006 eCommandRequiresProcess | eCommandTryTargetAPILock |
1007 eCommandProcessMustBeLaunched |
1008 eCommandProcessMustBePaused),
1011 ~CommandObjectProcessLoad() override = default;
1013 Options *GetOptions() override { return &m_options; }
1016 bool DoExecute(Args &command, CommandReturnObject &result) override {
1017 Process *process = m_exe_ctx.GetProcessPtr();
1019 for (auto &entry : command.entries()) {
1021 PlatformSP platform = process->GetTarget().GetPlatform();
1022 llvm::StringRef image_path = entry.ref;
1023 uint32_t image_token = LLDB_INVALID_IMAGE_TOKEN;
1025 if (!m_options.do_install) {
1026 FileSpec image_spec(image_path, false);
1027 platform->ResolveRemotePath(image_spec, image_spec);
1029 platform->LoadImage(process, FileSpec(), image_spec, error);
1030 } else if (m_options.install_path) {
1031 FileSpec image_spec(image_path, true);
1032 platform->ResolveRemotePath(m_options.install_path,
1033 m_options.install_path);
1034 image_token = platform->LoadImage(process, image_spec,
1035 m_options.install_path, error);
1037 FileSpec image_spec(image_path, true);
1039 platform->LoadImage(process, image_spec, FileSpec(), error);
1042 if (image_token != LLDB_INVALID_IMAGE_TOKEN) {
1043 result.AppendMessageWithFormat(
1044 "Loading \"%s\"...ok\nImage %u loaded.\n", image_path.str().c_str(),
1046 result.SetStatus(eReturnStatusSuccessFinishResult);
1048 result.AppendErrorWithFormat("failed to load '%s': %s",
1049 image_path.str().c_str(),
1051 result.SetStatus(eReturnStatusFailed);
1054 return result.Succeeded();
1057 CommandOptions m_options;
1060 //-------------------------------------------------------------------------
1061 // CommandObjectProcessUnload
1062 //-------------------------------------------------------------------------
1063 #pragma mark CommandObjectProcessUnload
1065 class CommandObjectProcessUnload : public CommandObjectParsed {
1067 CommandObjectProcessUnload(CommandInterpreter &interpreter)
1068 : CommandObjectParsed(
1069 interpreter, "process unload",
1070 "Unload a shared library from the current process using the index "
1071 "returned by a previous call to \"process load\".",
1072 "process unload <index>",
1073 eCommandRequiresProcess | eCommandTryTargetAPILock |
1074 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
1076 ~CommandObjectProcessUnload() override = default;
1079 bool DoExecute(Args &command, CommandReturnObject &result) override {
1080 Process *process = m_exe_ctx.GetProcessPtr();
1082 for (auto &entry : command.entries()) {
1083 uint32_t image_token;
1084 if (entry.ref.getAsInteger(0, image_token)) {
1085 result.AppendErrorWithFormat("invalid image index argument '%s'",
1086 entry.ref.str().c_str());
1087 result.SetStatus(eReturnStatusFailed);
1090 Status error(process->GetTarget().GetPlatform()->UnloadImage(
1091 process, image_token));
1092 if (error.Success()) {
1093 result.AppendMessageWithFormat(
1094 "Unloading shared library with index %u...ok\n", image_token);
1095 result.SetStatus(eReturnStatusSuccessFinishResult);
1097 result.AppendErrorWithFormat("failed to unload image: %s",
1099 result.SetStatus(eReturnStatusFailed);
1104 return result.Succeeded();
1108 //-------------------------------------------------------------------------
1109 // CommandObjectProcessSignal
1110 //-------------------------------------------------------------------------
1111 #pragma mark CommandObjectProcessSignal
1113 class CommandObjectProcessSignal : public CommandObjectParsed {
1115 CommandObjectProcessSignal(CommandInterpreter &interpreter)
1116 : CommandObjectParsed(interpreter, "process signal",
1117 "Send a UNIX signal to the current target process.",
1118 nullptr, eCommandRequiresProcess |
1119 eCommandTryTargetAPILock) {
1120 CommandArgumentEntry arg;
1121 CommandArgumentData signal_arg;
1123 // Define the first (and only) variant of this arg.
1124 signal_arg.arg_type = eArgTypeUnixSignal;
1125 signal_arg.arg_repetition = eArgRepeatPlain;
1127 // There is only one variant this argument could be; put it into the
1129 arg.push_back(signal_arg);
1131 // Push the data for the first argument into the m_arguments vector.
1132 m_arguments.push_back(arg);
1135 ~CommandObjectProcessSignal() override = default;
1138 bool DoExecute(Args &command, CommandReturnObject &result) override {
1139 Process *process = m_exe_ctx.GetProcessPtr();
1141 if (command.GetArgumentCount() == 1) {
1142 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1144 const char *signal_name = command.GetArgumentAtIndex(0);
1145 if (::isxdigit(signal_name[0]))
1147 StringConvert::ToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1149 signo = process->GetUnixSignals()->GetSignalNumberFromName(signal_name);
1151 if (signo == LLDB_INVALID_SIGNAL_NUMBER) {
1152 result.AppendErrorWithFormat("Invalid signal argument '%s'.\n",
1153 command.GetArgumentAtIndex(0));
1154 result.SetStatus(eReturnStatusFailed);
1156 Status error(process->Signal(signo));
1157 if (error.Success()) {
1158 result.SetStatus(eReturnStatusSuccessFinishResult);
1160 result.AppendErrorWithFormat("Failed to send signal %i: %s\n", signo,
1162 result.SetStatus(eReturnStatusFailed);
1166 result.AppendErrorWithFormat(
1167 "'%s' takes exactly one signal number argument:\nUsage: %s\n",
1168 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1169 result.SetStatus(eReturnStatusFailed);
1171 return result.Succeeded();
1175 //-------------------------------------------------------------------------
1176 // CommandObjectProcessInterrupt
1177 //-------------------------------------------------------------------------
1178 #pragma mark CommandObjectProcessInterrupt
1180 class CommandObjectProcessInterrupt : public CommandObjectParsed {
1182 CommandObjectProcessInterrupt(CommandInterpreter &interpreter)
1183 : CommandObjectParsed(interpreter, "process interrupt",
1184 "Interrupt the current target process.",
1185 "process interrupt",
1186 eCommandRequiresProcess | eCommandTryTargetAPILock |
1187 eCommandProcessMustBeLaunched) {}
1189 ~CommandObjectProcessInterrupt() override = default;
1192 bool DoExecute(Args &command, CommandReturnObject &result) override {
1193 Process *process = m_exe_ctx.GetProcessPtr();
1194 if (process == nullptr) {
1195 result.AppendError("no process to halt");
1196 result.SetStatus(eReturnStatusFailed);
1200 if (command.GetArgumentCount() == 0) {
1201 bool clear_thread_plans = true;
1202 Status error(process->Halt(clear_thread_plans));
1203 if (error.Success()) {
1204 result.SetStatus(eReturnStatusSuccessFinishResult);
1206 result.AppendErrorWithFormat("Failed to halt process: %s\n",
1208 result.SetStatus(eReturnStatusFailed);
1211 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
1212 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1213 result.SetStatus(eReturnStatusFailed);
1215 return result.Succeeded();
1219 //-------------------------------------------------------------------------
1220 // CommandObjectProcessKill
1221 //-------------------------------------------------------------------------
1222 #pragma mark CommandObjectProcessKill
1224 class CommandObjectProcessKill : public CommandObjectParsed {
1226 CommandObjectProcessKill(CommandInterpreter &interpreter)
1227 : CommandObjectParsed(interpreter, "process kill",
1228 "Terminate the current target process.",
1230 eCommandRequiresProcess | eCommandTryTargetAPILock |
1231 eCommandProcessMustBeLaunched) {}
1233 ~CommandObjectProcessKill() override = default;
1236 bool DoExecute(Args &command, CommandReturnObject &result) override {
1237 Process *process = m_exe_ctx.GetProcessPtr();
1238 if (process == nullptr) {
1239 result.AppendError("no process to kill");
1240 result.SetStatus(eReturnStatusFailed);
1244 if (command.GetArgumentCount() == 0) {
1245 Status error(process->Destroy(true));
1246 if (error.Success()) {
1247 result.SetStatus(eReturnStatusSuccessFinishResult);
1249 result.AppendErrorWithFormat("Failed to kill process: %s\n",
1251 result.SetStatus(eReturnStatusFailed);
1254 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
1255 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1256 result.SetStatus(eReturnStatusFailed);
1258 return result.Succeeded();
1262 //-------------------------------------------------------------------------
1263 // CommandObjectProcessSaveCore
1264 //-------------------------------------------------------------------------
1265 #pragma mark CommandObjectProcessSaveCore
1267 class CommandObjectProcessSaveCore : public CommandObjectParsed {
1269 CommandObjectProcessSaveCore(CommandInterpreter &interpreter)
1270 : CommandObjectParsed(interpreter, "process save-core",
1271 "Save the current process as a core file using an "
1272 "appropriate file type.",
1273 "process save-core FILE",
1274 eCommandRequiresProcess | eCommandTryTargetAPILock |
1275 eCommandProcessMustBeLaunched) {}
1277 ~CommandObjectProcessSaveCore() override = default;
1280 bool DoExecute(Args &command, CommandReturnObject &result) override {
1281 ProcessSP process_sp = m_exe_ctx.GetProcessSP();
1283 if (command.GetArgumentCount() == 1) {
1284 FileSpec output_file(command.GetArgumentAtIndex(0), false);
1285 Status error = PluginManager::SaveCore(process_sp, output_file);
1286 if (error.Success()) {
1287 result.SetStatus(eReturnStatusSuccessFinishResult);
1289 result.AppendErrorWithFormat(
1290 "Failed to save core file for process: %s\n", error.AsCString());
1291 result.SetStatus(eReturnStatusFailed);
1294 result.AppendErrorWithFormat("'%s' takes one arguments:\nUsage: %s\n",
1295 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1296 result.SetStatus(eReturnStatusFailed);
1299 result.AppendError("invalid process");
1300 result.SetStatus(eReturnStatusFailed);
1304 return result.Succeeded();
1308 //-------------------------------------------------------------------------
1309 // CommandObjectProcessStatus
1310 //-------------------------------------------------------------------------
1311 #pragma mark CommandObjectProcessStatus
1313 class CommandObjectProcessStatus : public CommandObjectParsed {
1315 CommandObjectProcessStatus(CommandInterpreter &interpreter)
1316 : CommandObjectParsed(
1317 interpreter, "process status",
1318 "Show status and stop location for the current target process.",
1320 eCommandRequiresProcess | eCommandTryTargetAPILock) {}
1322 ~CommandObjectProcessStatus() override = default;
1324 bool DoExecute(Args &command, CommandReturnObject &result) override {
1325 Stream &strm = result.GetOutputStream();
1326 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1327 // No need to check "process" for validity as eCommandRequiresProcess
1328 // ensures it is valid
1329 Process *process = m_exe_ctx.GetProcessPtr();
1330 const bool only_threads_with_stop_reason = true;
1331 const uint32_t start_frame = 0;
1332 const uint32_t num_frames = 1;
1333 const uint32_t num_frames_with_source = 1;
1334 const bool stop_format = true;
1335 process->GetStatus(strm);
1336 process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame,
1337 num_frames, num_frames_with_source, stop_format);
1338 return result.Succeeded();
1342 //-------------------------------------------------------------------------
1343 // CommandObjectProcessHandle
1344 //-------------------------------------------------------------------------
1346 static OptionDefinition g_process_handle_options[] = {
1348 { 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." },
1349 { 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." },
1350 { LLDB_OPT_SET_1, false, "pass", 'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." }
1354 #pragma mark CommandObjectProcessHandle
1356 class CommandObjectProcessHandle : public CommandObjectParsed {
1358 class CommandOptions : public Options {
1360 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
1362 ~CommandOptions() override = default;
1364 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1365 ExecutionContext *execution_context) override {
1367 const int short_option = m_getopt_table[option_idx].val;
1369 switch (short_option) {
1374 notify = option_arg;
1380 error.SetErrorStringWithFormat("invalid short option character '%c'",
1387 void OptionParsingStarting(ExecutionContext *execution_context) override {
1393 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1394 return llvm::makeArrayRef(g_process_handle_options);
1397 // Instance variables to hold the values for command options.
1404 CommandObjectProcessHandle(CommandInterpreter &interpreter)
1405 : CommandObjectParsed(interpreter, "process handle",
1406 "Manage LLDB handling of OS signals for the "
1407 "current target process. Defaults to showing "
1411 SetHelpLong("\nIf no signals are specified, update them all. If no update "
1412 "option is specified, list the current values.");
1413 CommandArgumentEntry arg;
1414 CommandArgumentData signal_arg;
1416 signal_arg.arg_type = eArgTypeUnixSignal;
1417 signal_arg.arg_repetition = eArgRepeatStar;
1419 arg.push_back(signal_arg);
1421 m_arguments.push_back(arg);
1424 ~CommandObjectProcessHandle() override = default;
1426 Options *GetOptions() override { return &m_options; }
1428 bool VerifyCommandOptionValue(const std::string &option, int &real_value) {
1430 bool success = false;
1431 bool tmp_value = OptionArgParser::ToBoolean(option, false, &success);
1433 if (success && tmp_value)
1435 else if (success && !tmp_value)
1438 // If the value isn't 'true' or 'false', it had better be 0 or 1.
1439 real_value = StringConvert::ToUInt32(option.c_str(), 3);
1440 if (real_value != 0 && real_value != 1)
1447 void PrintSignalHeader(Stream &str) {
1448 str.Printf("NAME PASS STOP NOTIFY\n");
1449 str.Printf("=========== ===== ===== ======\n");
1452 void PrintSignal(Stream &str, int32_t signo, const char *sig_name,
1453 const UnixSignalsSP &signals_sp) {
1458 str.Printf("%-11s ", sig_name);
1459 if (signals_sp->GetSignalInfo(signo, suppress, stop, notify)) {
1460 bool pass = !suppress;
1461 str.Printf("%s %s %s", (pass ? "true " : "false"),
1462 (stop ? "true " : "false"), (notify ? "true " : "false"));
1467 void PrintSignalInformation(Stream &str, Args &signal_args,
1468 int num_valid_signals,
1469 const UnixSignalsSP &signals_sp) {
1470 PrintSignalHeader(str);
1472 if (num_valid_signals > 0) {
1473 size_t num_args = signal_args.GetArgumentCount();
1474 for (size_t i = 0; i < num_args; ++i) {
1475 int32_t signo = signals_sp->GetSignalNumberFromName(
1476 signal_args.GetArgumentAtIndex(i));
1477 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1478 PrintSignal(str, signo, signal_args.GetArgumentAtIndex(i),
1481 } else // Print info for ALL signals
1483 int32_t signo = signals_sp->GetFirstSignalNumber();
1484 while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1485 PrintSignal(str, signo, signals_sp->GetSignalAsCString(signo),
1487 signo = signals_sp->GetNextSignalNumber(signo);
1493 bool DoExecute(Args &signal_args, CommandReturnObject &result) override {
1494 TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
1497 result.AppendError("No current target;"
1498 " cannot handle signals until you have a valid target "
1500 result.SetStatus(eReturnStatusFailed);
1504 ProcessSP process_sp = target_sp->GetProcessSP();
1507 result.AppendError("No current process; cannot handle signals until you "
1508 "have a valid process.\n");
1509 result.SetStatus(eReturnStatusFailed);
1513 int stop_action = -1; // -1 means leave the current setting alone
1514 int pass_action = -1; // -1 means leave the current setting alone
1515 int notify_action = -1; // -1 means leave the current setting alone
1517 if (!m_options.stop.empty() &&
1518 !VerifyCommandOptionValue(m_options.stop, stop_action)) {
1519 result.AppendError("Invalid argument for command option --stop; must be "
1520 "true or false.\n");
1521 result.SetStatus(eReturnStatusFailed);
1525 if (!m_options.notify.empty() &&
1526 !VerifyCommandOptionValue(m_options.notify, notify_action)) {
1527 result.AppendError("Invalid argument for command option --notify; must "
1528 "be true or false.\n");
1529 result.SetStatus(eReturnStatusFailed);
1533 if (!m_options.pass.empty() &&
1534 !VerifyCommandOptionValue(m_options.pass, pass_action)) {
1535 result.AppendError("Invalid argument for command option --pass; must be "
1536 "true or false.\n");
1537 result.SetStatus(eReturnStatusFailed);
1541 size_t num_args = signal_args.GetArgumentCount();
1542 UnixSignalsSP signals_sp = process_sp->GetUnixSignals();
1543 int num_signals_set = 0;
1546 for (const auto &arg : signal_args) {
1547 int32_t signo = signals_sp->GetSignalNumberFromName(arg.c_str());
1548 if (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1549 // Casting the actions as bools here should be okay, because
1550 // VerifyCommandOptionValue guarantees the value is either 0 or 1.
1551 if (stop_action != -1)
1552 signals_sp->SetShouldStop(signo, stop_action);
1553 if (pass_action != -1) {
1554 bool suppress = !pass_action;
1555 signals_sp->SetShouldSuppress(signo, suppress);
1557 if (notify_action != -1)
1558 signals_sp->SetShouldNotify(signo, notify_action);
1561 result.AppendErrorWithFormat("Invalid signal name '%s'\n",
1566 // No signal specified, if any command options were specified, update ALL
1568 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1)) {
1569 if (m_interpreter.Confirm(
1570 "Do you really want to update all the signals?", false)) {
1571 int32_t signo = signals_sp->GetFirstSignalNumber();
1572 while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1573 if (notify_action != -1)
1574 signals_sp->SetShouldNotify(signo, notify_action);
1575 if (stop_action != -1)
1576 signals_sp->SetShouldStop(signo, stop_action);
1577 if (pass_action != -1) {
1578 bool suppress = !pass_action;
1579 signals_sp->SetShouldSuppress(signo, suppress);
1581 signo = signals_sp->GetNextSignalNumber(signo);
1587 PrintSignalInformation(result.GetOutputStream(), signal_args,
1588 num_signals_set, signals_sp);
1590 if (num_signals_set > 0)
1591 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1593 result.SetStatus(eReturnStatusFailed);
1595 return result.Succeeded();
1598 CommandOptions m_options;
1601 //-------------------------------------------------------------------------
1602 // CommandObjectMultiwordProcess
1603 //-------------------------------------------------------------------------
1605 CommandObjectMultiwordProcess::CommandObjectMultiwordProcess(
1606 CommandInterpreter &interpreter)
1607 : CommandObjectMultiword(
1608 interpreter, "process",
1609 "Commands for interacting with processes on the current platform.",
1610 "process <subcommand> [<subcommand-options>]") {
1611 LoadSubCommand("attach",
1612 CommandObjectSP(new CommandObjectProcessAttach(interpreter)));
1613 LoadSubCommand("launch",
1614 CommandObjectSP(new CommandObjectProcessLaunch(interpreter)));
1615 LoadSubCommand("continue", CommandObjectSP(new CommandObjectProcessContinue(
1617 LoadSubCommand("connect",
1618 CommandObjectSP(new CommandObjectProcessConnect(interpreter)));
1619 LoadSubCommand("detach",
1620 CommandObjectSP(new CommandObjectProcessDetach(interpreter)));
1621 LoadSubCommand("load",
1622 CommandObjectSP(new CommandObjectProcessLoad(interpreter)));
1623 LoadSubCommand("unload",
1624 CommandObjectSP(new CommandObjectProcessUnload(interpreter)));
1625 LoadSubCommand("signal",
1626 CommandObjectSP(new CommandObjectProcessSignal(interpreter)));
1627 LoadSubCommand("handle",
1628 CommandObjectSP(new CommandObjectProcessHandle(interpreter)));
1629 LoadSubCommand("status",
1630 CommandObjectSP(new CommandObjectProcessStatus(interpreter)));
1631 LoadSubCommand("interrupt", CommandObjectSP(new CommandObjectProcessInterrupt(
1633 LoadSubCommand("kill",
1634 CommandObjectSP(new CommandObjectProcessKill(interpreter)));
1635 LoadSubCommand("plugin",
1636 CommandObjectSP(new CommandObjectProcessPlugin(interpreter)));
1637 LoadSubCommand("save-core", CommandObjectSP(new CommandObjectProcessSaveCore(
1641 CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess() = default;