xref: /freebsd-src/contrib/llvm-project/lldb/source/Commands/CommandObjectProcess.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
15ffd83dbSDimitry Andric //===-- CommandObjectProcess.cpp ------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "CommandObjectProcess.h"
1081ad6265SDimitry Andric #include "CommandObjectBreakpoint.h"
11fcaf7f86SDimitry Andric #include "CommandObjectTrace.h"
1206c3fb27SDimitry Andric #include "CommandOptionsProcessAttach.h"
13e8d8bef9SDimitry Andric #include "CommandOptionsProcessLaunch.h"
140b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
1581ad6265SDimitry Andric #include "lldb/Breakpoint/BreakpointIDList.h"
160b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
1781ad6265SDimitry Andric #include "lldb/Breakpoint/BreakpointName.h"
180b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointSite.h"
190b57cec5SDimitry Andric #include "lldb/Core/Module.h"
200b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
210b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h"
220b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
23fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
240b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
250b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
26fe6060f1SDimitry Andric #include "lldb/Interpreter/OptionGroupPythonClassWithDict.h"
270b57cec5SDimitry Andric #include "lldb/Interpreter/Options.h"
280b57cec5SDimitry Andric #include "lldb/Target/Platform.h"
290b57cec5SDimitry Andric #include "lldb/Target/Process.h"
300b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
310b57cec5SDimitry Andric #include "lldb/Target/Target.h"
320b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
330b57cec5SDimitry Andric #include "lldb/Target/UnixSignals.h"
340b57cec5SDimitry Andric #include "lldb/Utility/Args.h"
3506c3fb27SDimitry Andric #include "lldb/Utility/ScriptedMetadata.h"
360b57cec5SDimitry Andric #include "lldb/Utility/State.h"
370b57cec5SDimitry Andric 
3881ad6265SDimitry Andric #include "llvm/ADT/ScopeExit.h"
3981ad6265SDimitry Andric 
40fe6060f1SDimitry Andric #include <bitset>
41bdd1243dSDimitry Andric #include <optional>
42fe6060f1SDimitry Andric 
430b57cec5SDimitry Andric using namespace lldb;
440b57cec5SDimitry Andric using namespace lldb_private;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric class CommandObjectProcessLaunchOrAttach : public CommandObjectParsed {
470b57cec5SDimitry Andric public:
480b57cec5SDimitry Andric   CommandObjectProcessLaunchOrAttach(CommandInterpreter &interpreter,
490b57cec5SDimitry Andric                                      const char *name, const char *help,
500b57cec5SDimitry Andric                                      const char *syntax, uint32_t flags,
510b57cec5SDimitry Andric                                      const char *new_process_action)
520b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, name, help, syntax, flags),
530b57cec5SDimitry Andric         m_new_process_action(new_process_action) {}
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   ~CommandObjectProcessLaunchOrAttach() override = default;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric protected:
580b57cec5SDimitry Andric   bool StopProcessIfNecessary(Process *process, StateType &state,
590b57cec5SDimitry Andric                               CommandReturnObject &result) {
600b57cec5SDimitry Andric     state = eStateInvalid;
610b57cec5SDimitry Andric     if (process) {
620b57cec5SDimitry Andric       state = process->GetState();
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric       if (process->IsAlive() && state != eStateConnected) {
65e8d8bef9SDimitry Andric         std::string message;
660b57cec5SDimitry Andric         if (process->GetState() == eStateAttaching)
67e8d8bef9SDimitry Andric           message =
68e8d8bef9SDimitry Andric               llvm::formatv("There is a pending attach, abort it and {0}?",
69e8d8bef9SDimitry Andric                             m_new_process_action);
700b57cec5SDimitry Andric         else if (process->GetShouldDetach())
71e8d8bef9SDimitry Andric           message = llvm::formatv(
72e8d8bef9SDimitry Andric               "There is a running process, detach from it and {0}?",
73e8d8bef9SDimitry Andric               m_new_process_action);
740b57cec5SDimitry Andric         else
75e8d8bef9SDimitry Andric           message =
76e8d8bef9SDimitry Andric               llvm::formatv("There is a running process, kill it and {0}?",
77e8d8bef9SDimitry Andric                             m_new_process_action);
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric         if (!m_interpreter.Confirm(message, true)) {
800b57cec5SDimitry Andric           result.SetStatus(eReturnStatusFailed);
810b57cec5SDimitry Andric           return false;
820b57cec5SDimitry Andric         } else {
830b57cec5SDimitry Andric           if (process->GetShouldDetach()) {
840b57cec5SDimitry Andric             bool keep_stopped = false;
850b57cec5SDimitry Andric             Status detach_error(process->Detach(keep_stopped));
860b57cec5SDimitry Andric             if (detach_error.Success()) {
870b57cec5SDimitry Andric               result.SetStatus(eReturnStatusSuccessFinishResult);
880b57cec5SDimitry Andric               process = nullptr;
890b57cec5SDimitry Andric             } else {
900b57cec5SDimitry Andric               result.AppendErrorWithFormat(
910b57cec5SDimitry Andric                   "Failed to detach from process: %s\n",
920b57cec5SDimitry Andric                   detach_error.AsCString());
930b57cec5SDimitry Andric             }
940b57cec5SDimitry Andric           } else {
950b57cec5SDimitry Andric             Status destroy_error(process->Destroy(false));
960b57cec5SDimitry Andric             if (destroy_error.Success()) {
970b57cec5SDimitry Andric               result.SetStatus(eReturnStatusSuccessFinishResult);
980b57cec5SDimitry Andric               process = nullptr;
990b57cec5SDimitry Andric             } else {
1000b57cec5SDimitry Andric               result.AppendErrorWithFormat("Failed to kill process: %s\n",
1010b57cec5SDimitry Andric                                            destroy_error.AsCString());
1020b57cec5SDimitry Andric             }
1030b57cec5SDimitry Andric           }
1040b57cec5SDimitry Andric         }
1050b57cec5SDimitry Andric       }
1060b57cec5SDimitry Andric     }
1070b57cec5SDimitry Andric     return result.Succeeded();
1080b57cec5SDimitry Andric   }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   std::string m_new_process_action;
1110b57cec5SDimitry Andric };
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric // CommandObjectProcessLaunch
1140b57cec5SDimitry Andric #pragma mark CommandObjectProcessLaunch
1150b57cec5SDimitry Andric class CommandObjectProcessLaunch : public CommandObjectProcessLaunchOrAttach {
1160b57cec5SDimitry Andric public:
1170b57cec5SDimitry Andric   CommandObjectProcessLaunch(CommandInterpreter &interpreter)
1180b57cec5SDimitry Andric       : CommandObjectProcessLaunchOrAttach(
1190b57cec5SDimitry Andric             interpreter, "process launch",
1200b57cec5SDimitry Andric             "Launch the executable in the debugger.", nullptr,
1210b57cec5SDimitry Andric             eCommandRequiresTarget, "restart"),
12204eeddc0SDimitry Andric 
12304eeddc0SDimitry Andric         m_class_options("scripted process", true, 'C', 'k', 'v', 0) {
124fe6060f1SDimitry Andric     m_all_options.Append(&m_options);
125fe6060f1SDimitry Andric     m_all_options.Append(&m_class_options, LLDB_OPT_SET_1 | LLDB_OPT_SET_2,
126fe6060f1SDimitry Andric                          LLDB_OPT_SET_ALL);
127fe6060f1SDimitry Andric     m_all_options.Finalize();
128fe6060f1SDimitry Andric 
129*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypeRunArgs, eArgRepeatOptional);
1300b57cec5SDimitry Andric   }
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   ~CommandObjectProcessLaunch() override = default;
1330b57cec5SDimitry Andric 
134fe6060f1SDimitry Andric   Options *GetOptions() override { return &m_all_options; }
1350b57cec5SDimitry Andric 
136bdd1243dSDimitry Andric   std::optional<std::string> GetRepeatCommand(Args &current_command_args,
1370b57cec5SDimitry Andric                                               uint32_t index) override {
1380b57cec5SDimitry Andric     // No repeat for "process launch"...
13981ad6265SDimitry Andric     return std::string("");
1400b57cec5SDimitry Andric   }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric protected:
1435f757f3fSDimitry Andric   void DoExecute(Args &launch_args, CommandReturnObject &result) override {
1440b57cec5SDimitry Andric     Debugger &debugger = GetDebugger();
1450b57cec5SDimitry Andric     Target *target = debugger.GetSelectedTarget().get();
1460b57cec5SDimitry Andric     // If our listener is nullptr, users aren't allows to launch
1470b57cec5SDimitry Andric     ModuleSP exe_module_sp = target->GetExecutableModule();
1480b57cec5SDimitry Andric 
149349cc55cSDimitry Andric     // If the target already has an executable module, then use that.  If it
150349cc55cSDimitry Andric     // doesn't then someone must be trying to launch using a path that will
151349cc55cSDimitry Andric     // make sense to the remote stub, but doesn't exist on the local host.
152349cc55cSDimitry Andric     // In that case use the ExecutableFile that was set in the target's
153349cc55cSDimitry Andric     // ProcessLaunchInfo.
154349cc55cSDimitry Andric     if (exe_module_sp == nullptr && !target->GetProcessLaunchInfo().GetExecutableFile()) {
1550b57cec5SDimitry Andric       result.AppendError("no file in target, create a debug target using the "
1560b57cec5SDimitry Andric                          "'target create' command");
1575f757f3fSDimitry Andric       return;
1580b57cec5SDimitry Andric     }
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric     StateType state = eStateInvalid;
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric     if (!StopProcessIfNecessary(m_exe_ctx.GetProcessPtr(), state, result))
1635f757f3fSDimitry Andric       return;
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric     // Determine whether we will disable ASLR or leave it in the default state
1660b57cec5SDimitry Andric     // (i.e. enabled if the platform supports it). First check if the process
1670b57cec5SDimitry Andric     // launch options explicitly turn on/off
1680b57cec5SDimitry Andric     // disabling ASLR.  If so, use that setting;
1690b57cec5SDimitry Andric     // otherwise, use the 'settings target.disable-aslr' setting.
1700b57cec5SDimitry Andric     bool disable_aslr = false;
1710b57cec5SDimitry Andric     if (m_options.disable_aslr != eLazyBoolCalculate) {
1720b57cec5SDimitry Andric       // The user specified an explicit setting on the process launch line.
1730b57cec5SDimitry Andric       // Use it.
1740b57cec5SDimitry Andric       disable_aslr = (m_options.disable_aslr == eLazyBoolYes);
1750b57cec5SDimitry Andric     } else {
1760b57cec5SDimitry Andric       // The user did not explicitly specify whether to disable ASLR.  Fall
1770b57cec5SDimitry Andric       // back to the target.disable-aslr setting.
1780b57cec5SDimitry Andric       disable_aslr = target->GetDisableASLR();
1790b57cec5SDimitry Andric     }
1800b57cec5SDimitry Andric 
181fe6060f1SDimitry Andric     if (!m_class_options.GetName().empty()) {
182fe6060f1SDimitry Andric       m_options.launch_info.SetProcessPluginName("ScriptedProcess");
18306c3fb27SDimitry Andric       ScriptedMetadataSP metadata_sp = std::make_shared<ScriptedMetadata>(
18406c3fb27SDimitry Andric           m_class_options.GetName(), m_class_options.GetStructuredData());
18506c3fb27SDimitry Andric       m_options.launch_info.SetScriptedMetadata(metadata_sp);
186fe6060f1SDimitry Andric       target->SetProcessLaunchInfo(m_options.launch_info);
187fe6060f1SDimitry Andric     }
188fe6060f1SDimitry Andric 
1890b57cec5SDimitry Andric     if (disable_aslr)
1900b57cec5SDimitry Andric       m_options.launch_info.GetFlags().Set(eLaunchFlagDisableASLR);
1910b57cec5SDimitry Andric     else
1920b57cec5SDimitry Andric       m_options.launch_info.GetFlags().Clear(eLaunchFlagDisableASLR);
1930b57cec5SDimitry Andric 
194e8d8bef9SDimitry Andric     if (target->GetInheritTCC())
195e8d8bef9SDimitry Andric       m_options.launch_info.GetFlags().Set(eLaunchFlagInheritTCCFromParent);
196e8d8bef9SDimitry Andric 
1970b57cec5SDimitry Andric     if (target->GetDetachOnError())
1980b57cec5SDimitry Andric       m_options.launch_info.GetFlags().Set(eLaunchFlagDetachOnError);
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric     if (target->GetDisableSTDIO())
2010b57cec5SDimitry Andric       m_options.launch_info.GetFlags().Set(eLaunchFlagDisableSTDIO);
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric     // Merge the launch info environment with the target environment.
2040b57cec5SDimitry Andric     Environment target_env = target->GetEnvironment();
2050b57cec5SDimitry Andric     m_options.launch_info.GetEnvironment().insert(target_env.begin(),
2060b57cec5SDimitry Andric                                                   target_env.end());
2070b57cec5SDimitry Andric 
208349cc55cSDimitry Andric     llvm::StringRef target_settings_argv0 = target->GetArg0();
209349cc55cSDimitry Andric 
2100b57cec5SDimitry Andric     if (!target_settings_argv0.empty()) {
2110b57cec5SDimitry Andric       m_options.launch_info.GetArguments().AppendArgument(
2120b57cec5SDimitry Andric           target_settings_argv0);
213349cc55cSDimitry Andric       if (exe_module_sp)
2140b57cec5SDimitry Andric         m_options.launch_info.SetExecutableFile(
2150b57cec5SDimitry Andric             exe_module_sp->GetPlatformFileSpec(), false);
216349cc55cSDimitry Andric       else
217349cc55cSDimitry Andric         m_options.launch_info.SetExecutableFile(target->GetProcessLaunchInfo().GetExecutableFile(), false);
2180b57cec5SDimitry Andric     } else {
219349cc55cSDimitry Andric       if (exe_module_sp)
2200b57cec5SDimitry Andric         m_options.launch_info.SetExecutableFile(
2210b57cec5SDimitry Andric             exe_module_sp->GetPlatformFileSpec(), true);
222349cc55cSDimitry Andric       else
223349cc55cSDimitry Andric         m_options.launch_info.SetExecutableFile(target->GetProcessLaunchInfo().GetExecutableFile(), true);
2240b57cec5SDimitry Andric     }
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric     if (launch_args.GetArgumentCount() == 0) {
2270b57cec5SDimitry Andric       m_options.launch_info.GetArguments().AppendArguments(
2280b57cec5SDimitry Andric           target->GetProcessLaunchInfo().GetArguments());
2290b57cec5SDimitry Andric     } else {
2300b57cec5SDimitry Andric       m_options.launch_info.GetArguments().AppendArguments(launch_args);
2310b57cec5SDimitry Andric       // Save the arguments for subsequent runs in the current target.
2320b57cec5SDimitry Andric       target->SetRunArguments(launch_args);
2330b57cec5SDimitry Andric     }
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric     StreamString stream;
2360b57cec5SDimitry Andric     Status error = target->Launch(m_options.launch_info, &stream);
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric     if (error.Success()) {
2390b57cec5SDimitry Andric       ProcessSP process_sp(target->GetProcessSP());
2400b57cec5SDimitry Andric       if (process_sp) {
2410b57cec5SDimitry Andric         // There is a race condition where this thread will return up the call
2420b57cec5SDimitry Andric         // stack to the main command handler and show an (lldb) prompt before
2430b57cec5SDimitry Andric         // HandlePrivateEvent (from PrivateStateThread) has a chance to call
2440b57cec5SDimitry Andric         // PushProcessIOHandler().
2450b57cec5SDimitry Andric         process_sp->SyncIOHandler(0, std::chrono::seconds(2));
2460b57cec5SDimitry Andric 
247349cc55cSDimitry Andric         // If we didn't have a local executable, then we wouldn't have had an
248349cc55cSDimitry Andric         // executable module before launch.
249349cc55cSDimitry Andric         if (!exe_module_sp)
250349cc55cSDimitry Andric           exe_module_sp = target->GetExecutableModule();
251349cc55cSDimitry Andric         if (!exe_module_sp) {
252349cc55cSDimitry Andric           result.AppendWarning("Could not get executable module after launch.");
253349cc55cSDimitry Andric         } else {
254349cc55cSDimitry Andric 
2550b57cec5SDimitry Andric           const char *archname =
2560b57cec5SDimitry Andric               exe_module_sp->GetArchitecture().GetArchitectureName();
2570b57cec5SDimitry Andric           result.AppendMessageWithFormat(
2580b57cec5SDimitry Andric               "Process %" PRIu64 " launched: '%s' (%s)\n", process_sp->GetID(),
2590b57cec5SDimitry Andric               exe_module_sp->GetFileSpec().GetPath().c_str(), archname);
260349cc55cSDimitry Andric         }
2610b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishResult);
2625f757f3fSDimitry Andric         // This message will refer to an event that happened after the process
2635f757f3fSDimitry Andric         // launched.
2645f757f3fSDimitry Andric         llvm::StringRef data = stream.GetString();
2655f757f3fSDimitry Andric         if (!data.empty())
2665f757f3fSDimitry Andric           result.AppendMessage(data);
2670b57cec5SDimitry Andric         result.SetDidChangeProcessState(true);
2680b57cec5SDimitry Andric       } else {
2690b57cec5SDimitry Andric         result.AppendError(
2700b57cec5SDimitry Andric             "no error returned from Target::Launch, and target has no process");
2710b57cec5SDimitry Andric       }
2720b57cec5SDimitry Andric     } else {
2730b57cec5SDimitry Andric       result.AppendError(error.AsCString());
2740b57cec5SDimitry Andric     }
2750b57cec5SDimitry Andric   }
2760b57cec5SDimitry Andric 
277e8d8bef9SDimitry Andric   CommandOptionsProcessLaunch m_options;
278fe6060f1SDimitry Andric   OptionGroupPythonClassWithDict m_class_options;
279fe6060f1SDimitry Andric   OptionGroupOptions m_all_options;
2800b57cec5SDimitry Andric };
2810b57cec5SDimitry Andric 
2829dba64beSDimitry Andric #define LLDB_OPTIONS_process_attach
2839dba64beSDimitry Andric #include "CommandOptions.inc"
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric #pragma mark CommandObjectProcessAttach
2860b57cec5SDimitry Andric class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach {
2870b57cec5SDimitry Andric public:
2880b57cec5SDimitry Andric   CommandObjectProcessAttach(CommandInterpreter &interpreter)
2890b57cec5SDimitry Andric       : CommandObjectProcessLaunchOrAttach(
2900b57cec5SDimitry Andric             interpreter, "process attach", "Attach to a process.",
29106c3fb27SDimitry Andric             "process attach <cmd-options>", 0, "attach"),
29206c3fb27SDimitry Andric         m_class_options("scripted process", true, 'C', 'k', 'v', 0) {
29306c3fb27SDimitry Andric     m_all_options.Append(&m_options);
29406c3fb27SDimitry Andric     m_all_options.Append(&m_class_options, LLDB_OPT_SET_1 | LLDB_OPT_SET_2,
29506c3fb27SDimitry Andric                          LLDB_OPT_SET_ALL);
29606c3fb27SDimitry Andric     m_all_options.Finalize();
29706c3fb27SDimitry Andric   }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   ~CommandObjectProcessAttach() override = default;
3000b57cec5SDimitry Andric 
30106c3fb27SDimitry Andric   Options *GetOptions() override { return &m_all_options; }
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric protected:
3045f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
3050b57cec5SDimitry Andric     PlatformSP platform_sp(
3060b57cec5SDimitry Andric         GetDebugger().GetPlatformList().GetSelectedPlatform());
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric     Target *target = GetDebugger().GetSelectedTarget().get();
3090b57cec5SDimitry Andric     // N.B. The attach should be synchronous.  It doesn't help much to get the
3100b57cec5SDimitry Andric     // prompt back between initiating the attach and the target actually
3110b57cec5SDimitry Andric     // stopping.  So even if the interpreter is set to be asynchronous, we wait
3120b57cec5SDimitry Andric     // for the stop ourselves here.
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric     StateType state = eStateInvalid;
3150b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric     if (!StopProcessIfNecessary(process, state, result))
3185f757f3fSDimitry Andric       return;
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric     if (target == nullptr) {
3210b57cec5SDimitry Andric       // If there isn't a current target create one.
3220b57cec5SDimitry Andric       TargetSP new_target_sp;
3230b57cec5SDimitry Andric       Status error;
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric       error = GetDebugger().GetTargetList().CreateTarget(
3260b57cec5SDimitry Andric           GetDebugger(), "", "", eLoadDependentsNo,
3270b57cec5SDimitry Andric           nullptr, // No platform options
3280b57cec5SDimitry Andric           new_target_sp);
3290b57cec5SDimitry Andric       target = new_target_sp.get();
3300b57cec5SDimitry Andric       if (target == nullptr || error.Fail()) {
3310b57cec5SDimitry Andric         result.AppendError(error.AsCString("Error creating target"));
3325f757f3fSDimitry Andric         return;
3330b57cec5SDimitry Andric       }
3340b57cec5SDimitry Andric     }
3350b57cec5SDimitry Andric 
33606c3fb27SDimitry Andric     if (!m_class_options.GetName().empty()) {
33706c3fb27SDimitry Andric       m_options.attach_info.SetProcessPluginName("ScriptedProcess");
33806c3fb27SDimitry Andric       ScriptedMetadataSP metadata_sp = std::make_shared<ScriptedMetadata>(
33906c3fb27SDimitry Andric           m_class_options.GetName(), m_class_options.GetStructuredData());
34006c3fb27SDimitry Andric       m_options.attach_info.SetScriptedMetadata(metadata_sp);
34106c3fb27SDimitry Andric     }
34206c3fb27SDimitry Andric 
3430b57cec5SDimitry Andric     // Record the old executable module, we want to issue a warning if the
3440b57cec5SDimitry Andric     // process of attaching changed the current executable (like somebody said
3450b57cec5SDimitry Andric     // "file foo" then attached to a PID whose executable was bar.)
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric     ModuleSP old_exec_module_sp = target->GetExecutableModule();
3480b57cec5SDimitry Andric     ArchSpec old_arch_spec = target->GetArchitecture();
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric     StreamString stream;
351349cc55cSDimitry Andric     ProcessSP process_sp;
3520b57cec5SDimitry Andric     const auto error = target->Attach(m_options.attach_info, &stream);
3530b57cec5SDimitry Andric     if (error.Success()) {
354349cc55cSDimitry Andric       process_sp = target->GetProcessSP();
3550b57cec5SDimitry Andric       if (process_sp) {
3560b57cec5SDimitry Andric         result.AppendMessage(stream.GetString());
3570b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishNoResult);
3580b57cec5SDimitry Andric         result.SetDidChangeProcessState(true);
3590b57cec5SDimitry Andric       } else {
3600b57cec5SDimitry Andric         result.AppendError(
3610b57cec5SDimitry Andric             "no error returned from Target::Attach, and target has no process");
3620b57cec5SDimitry Andric       }
3630b57cec5SDimitry Andric     } else {
3640b57cec5SDimitry Andric       result.AppendErrorWithFormat("attach failed: %s\n", error.AsCString());
3650b57cec5SDimitry Andric     }
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric     if (!result.Succeeded())
3685f757f3fSDimitry Andric       return;
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric     // Okay, we're done.  Last step is to warn if the executable module has
3710b57cec5SDimitry Andric     // changed:
3720b57cec5SDimitry Andric     char new_path[PATH_MAX];
3730b57cec5SDimitry Andric     ModuleSP new_exec_module_sp(target->GetExecutableModule());
3740b57cec5SDimitry Andric     if (!old_exec_module_sp) {
3750b57cec5SDimitry Andric       // We might not have a module if we attached to a raw pid...
3760b57cec5SDimitry Andric       if (new_exec_module_sp) {
3770b57cec5SDimitry Andric         new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
3780b57cec5SDimitry Andric         result.AppendMessageWithFormat("Executable module set to \"%s\".\n",
3790b57cec5SDimitry Andric                                        new_path);
3800b57cec5SDimitry Andric       }
3810b57cec5SDimitry Andric     } else if (old_exec_module_sp->GetFileSpec() !=
3820b57cec5SDimitry Andric                new_exec_module_sp->GetFileSpec()) {
3830b57cec5SDimitry Andric       char old_path[PATH_MAX];
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric       old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
3860b57cec5SDimitry Andric       new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric       result.AppendWarningWithFormat(
3890b57cec5SDimitry Andric           "Executable module changed from \"%s\" to \"%s\".\n", old_path,
3900b57cec5SDimitry Andric           new_path);
3910b57cec5SDimitry Andric     }
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric     if (!old_arch_spec.IsValid()) {
3940b57cec5SDimitry Andric       result.AppendMessageWithFormat(
3950b57cec5SDimitry Andric           "Architecture set to: %s.\n",
3960b57cec5SDimitry Andric           target->GetArchitecture().GetTriple().getTriple().c_str());
3970b57cec5SDimitry Andric     } else if (!old_arch_spec.IsExactMatch(target->GetArchitecture())) {
3980b57cec5SDimitry Andric       result.AppendWarningWithFormat(
3990b57cec5SDimitry Andric           "Architecture changed from %s to %s.\n",
4000b57cec5SDimitry Andric           old_arch_spec.GetTriple().getTriple().c_str(),
4010b57cec5SDimitry Andric           target->GetArchitecture().GetTriple().getTriple().c_str());
4020b57cec5SDimitry Andric     }
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric     // This supports the use-case scenario of immediately continuing the
4050b57cec5SDimitry Andric     // process once attached.
406349cc55cSDimitry Andric     if (m_options.attach_info.GetContinueOnceAttached()) {
407349cc55cSDimitry Andric       // We have made a process but haven't told the interpreter about it yet,
408349cc55cSDimitry Andric       // so CheckRequirements will fail for "process continue".  Set the override
409349cc55cSDimitry Andric       // here:
410349cc55cSDimitry Andric       ExecutionContext exe_ctx(process_sp);
411349cc55cSDimitry Andric       m_interpreter.HandleCommand("process continue", eLazyBoolNo, exe_ctx, result);
412349cc55cSDimitry Andric     }
4130b57cec5SDimitry Andric   }
4140b57cec5SDimitry Andric 
41506c3fb27SDimitry Andric   CommandOptionsProcessAttach m_options;
41606c3fb27SDimitry Andric   OptionGroupPythonClassWithDict m_class_options;
41706c3fb27SDimitry Andric   OptionGroupOptions m_all_options;
4180b57cec5SDimitry Andric };
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric // CommandObjectProcessContinue
4210b57cec5SDimitry Andric 
4229dba64beSDimitry Andric #define LLDB_OPTIONS_process_continue
4239dba64beSDimitry Andric #include "CommandOptions.inc"
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric #pragma mark CommandObjectProcessContinue
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric class CommandObjectProcessContinue : public CommandObjectParsed {
4280b57cec5SDimitry Andric public:
4290b57cec5SDimitry Andric   CommandObjectProcessContinue(CommandInterpreter &interpreter)
4300b57cec5SDimitry Andric       : CommandObjectParsed(
4310b57cec5SDimitry Andric             interpreter, "process continue",
4320b57cec5SDimitry Andric             "Continue execution of all threads in the current process.",
4330b57cec5SDimitry Andric             "process continue",
4340b57cec5SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
43504eeddc0SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric   ~CommandObjectProcessContinue() override = default;
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric protected:
4400b57cec5SDimitry Andric   class CommandOptions : public Options {
4410b57cec5SDimitry Andric   public:
44204eeddc0SDimitry Andric     CommandOptions() {
4430b57cec5SDimitry Andric       // Keep default values of all options in one place: OptionParsingStarting
4440b57cec5SDimitry Andric       // ()
4450b57cec5SDimitry Andric       OptionParsingStarting(nullptr);
4460b57cec5SDimitry Andric     }
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric     ~CommandOptions() override = default;
4490b57cec5SDimitry Andric 
4500b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
45181ad6265SDimitry Andric                           ExecutionContext *exe_ctx) override {
4520b57cec5SDimitry Andric       Status error;
4530b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
4540b57cec5SDimitry Andric       switch (short_option) {
4550b57cec5SDimitry Andric       case 'i':
4560b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, m_ignore))
4570b57cec5SDimitry Andric           error.SetErrorStringWithFormat(
4580b57cec5SDimitry Andric               "invalid value for ignore option: \"%s\", should be a number.",
4590b57cec5SDimitry Andric               option_arg.str().c_str());
4600b57cec5SDimitry Andric         break;
46181ad6265SDimitry Andric       case 'b':
46281ad6265SDimitry Andric         m_run_to_bkpt_args.AppendArgument(option_arg);
46381ad6265SDimitry Andric         m_any_bkpts_specified = true;
46481ad6265SDimitry Andric       break;
4650b57cec5SDimitry Andric       default:
4669dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
4670b57cec5SDimitry Andric       }
4680b57cec5SDimitry Andric       return error;
4690b57cec5SDimitry Andric     }
4700b57cec5SDimitry Andric 
4710b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
4720b57cec5SDimitry Andric       m_ignore = 0;
47381ad6265SDimitry Andric       m_run_to_bkpt_args.Clear();
47481ad6265SDimitry Andric       m_any_bkpts_specified = false;
4750b57cec5SDimitry Andric     }
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
478bdd1243dSDimitry Andric       return llvm::ArrayRef(g_process_continue_options);
4790b57cec5SDimitry Andric     }
4800b57cec5SDimitry Andric 
48181ad6265SDimitry Andric     uint32_t m_ignore = 0;
48281ad6265SDimitry Andric     Args m_run_to_bkpt_args;
48381ad6265SDimitry Andric     bool m_any_bkpts_specified = false;
4840b57cec5SDimitry Andric   };
4850b57cec5SDimitry Andric 
4865f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
4870b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
4880b57cec5SDimitry Andric     bool synchronous_execution = m_interpreter.GetSynchronous();
4890b57cec5SDimitry Andric     StateType state = process->GetState();
4900b57cec5SDimitry Andric     if (state == eStateStopped) {
4910b57cec5SDimitry Andric       if (m_options.m_ignore > 0) {
4920b57cec5SDimitry Andric         ThreadSP sel_thread_sp(GetDefaultThread()->shared_from_this());
4930b57cec5SDimitry Andric         if (sel_thread_sp) {
4940b57cec5SDimitry Andric           StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
4950b57cec5SDimitry Andric           if (stop_info_sp &&
4960b57cec5SDimitry Andric               stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
4970b57cec5SDimitry Andric             lldb::break_id_t bp_site_id =
4980b57cec5SDimitry Andric                 (lldb::break_id_t)stop_info_sp->GetValue();
4990b57cec5SDimitry Andric             BreakpointSiteSP bp_site_sp(
5000b57cec5SDimitry Andric                 process->GetBreakpointSiteList().FindByID(bp_site_id));
5010b57cec5SDimitry Andric             if (bp_site_sp) {
5025f757f3fSDimitry Andric               const size_t num_owners = bp_site_sp->GetNumberOfConstituents();
5030b57cec5SDimitry Andric               for (size_t i = 0; i < num_owners; i++) {
5040b57cec5SDimitry Andric                 Breakpoint &bp_ref =
5055f757f3fSDimitry Andric                     bp_site_sp->GetConstituentAtIndex(i)->GetBreakpoint();
5060b57cec5SDimitry Andric                 if (!bp_ref.IsInternal()) {
5070b57cec5SDimitry Andric                   bp_ref.SetIgnoreCount(m_options.m_ignore);
5080b57cec5SDimitry Andric                 }
5090b57cec5SDimitry Andric               }
5100b57cec5SDimitry Andric             }
5110b57cec5SDimitry Andric           }
5120b57cec5SDimitry Andric         }
5130b57cec5SDimitry Andric       }
5140b57cec5SDimitry Andric 
51581ad6265SDimitry Andric       Target *target = m_exe_ctx.GetTargetPtr();
51681ad6265SDimitry Andric       BreakpointIDList run_to_bkpt_ids;
51781ad6265SDimitry Andric       // Don't pass an empty run_to_breakpoint list, as Verify will look for the
51881ad6265SDimitry Andric       // default breakpoint.
51981ad6265SDimitry Andric       if (m_options.m_run_to_bkpt_args.GetArgumentCount() > 0)
52081ad6265SDimitry Andric         CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
52181ad6265SDimitry Andric             m_options.m_run_to_bkpt_args, target, result, &run_to_bkpt_ids,
52281ad6265SDimitry Andric             BreakpointName::Permissions::disablePerm);
52381ad6265SDimitry Andric       if (!result.Succeeded()) {
5245f757f3fSDimitry Andric         return;
52581ad6265SDimitry Andric       }
52681ad6265SDimitry Andric       result.Clear();
52781ad6265SDimitry Andric       if (m_options.m_any_bkpts_specified && run_to_bkpt_ids.GetSize() == 0) {
52881ad6265SDimitry Andric         result.AppendError("continue-to breakpoints did not specify any actual "
52981ad6265SDimitry Andric                            "breakpoints or locations");
5305f757f3fSDimitry Andric         return;
53181ad6265SDimitry Andric       }
53281ad6265SDimitry Andric 
53381ad6265SDimitry Andric       // First figure out which breakpoints & locations were specified by the
53481ad6265SDimitry Andric       // user:
53581ad6265SDimitry Andric       size_t num_run_to_bkpt_ids = run_to_bkpt_ids.GetSize();
53681ad6265SDimitry Andric       std::vector<break_id_t> bkpts_disabled;
53781ad6265SDimitry Andric       std::vector<BreakpointID> locs_disabled;
53881ad6265SDimitry Andric       if (num_run_to_bkpt_ids != 0) {
53981ad6265SDimitry Andric         // Go through the ID's specified, and separate the breakpoints from are
54081ad6265SDimitry Andric         // the breakpoint.location specifications since the latter require
54181ad6265SDimitry Andric         // special handling.  We also figure out whether there's at least one
54281ad6265SDimitry Andric         // specifier in the set that is enabled.
54381ad6265SDimitry Andric         BreakpointList &bkpt_list = target->GetBreakpointList();
54481ad6265SDimitry Andric         std::unordered_set<break_id_t> bkpts_seen;
54581ad6265SDimitry Andric         std::unordered_set<break_id_t> bkpts_with_locs_seen;
54681ad6265SDimitry Andric         BreakpointIDList with_locs;
54781ad6265SDimitry Andric         bool any_enabled = false;
54881ad6265SDimitry Andric 
54981ad6265SDimitry Andric         for (size_t idx = 0; idx < num_run_to_bkpt_ids; idx++) {
55081ad6265SDimitry Andric           BreakpointID bkpt_id = run_to_bkpt_ids.GetBreakpointIDAtIndex(idx);
55181ad6265SDimitry Andric           break_id_t bp_id = bkpt_id.GetBreakpointID();
55281ad6265SDimitry Andric           break_id_t loc_id = bkpt_id.GetLocationID();
55381ad6265SDimitry Andric           BreakpointSP bp_sp
55481ad6265SDimitry Andric               = bkpt_list.FindBreakpointByID(bp_id);
55581ad6265SDimitry Andric           // Note, VerifyBreakpointOrLocationIDs checks for existence, so we
55681ad6265SDimitry Andric           // don't need to do it again here.
55781ad6265SDimitry Andric           if (bp_sp->IsEnabled()) {
55881ad6265SDimitry Andric             if (loc_id == LLDB_INVALID_BREAK_ID) {
55981ad6265SDimitry Andric               // A breakpoint (without location) was specified.  Make sure that
56081ad6265SDimitry Andric               // at least one of the locations is enabled.
56181ad6265SDimitry Andric               size_t num_locations = bp_sp->GetNumLocations();
56281ad6265SDimitry Andric               for (size_t loc_idx = 0; loc_idx < num_locations; loc_idx++) {
56381ad6265SDimitry Andric                 BreakpointLocationSP loc_sp
56481ad6265SDimitry Andric                     = bp_sp->GetLocationAtIndex(loc_idx);
56581ad6265SDimitry Andric                 if (loc_sp->IsEnabled()) {
56681ad6265SDimitry Andric                   any_enabled = true;
56781ad6265SDimitry Andric                   break;
56881ad6265SDimitry Andric                 }
56981ad6265SDimitry Andric               }
57081ad6265SDimitry Andric             } else {
57181ad6265SDimitry Andric               // A location was specified, check if it was enabled:
57281ad6265SDimitry Andric               BreakpointLocationSP loc_sp = bp_sp->FindLocationByID(loc_id);
57381ad6265SDimitry Andric               if (loc_sp->IsEnabled())
57481ad6265SDimitry Andric                 any_enabled = true;
57581ad6265SDimitry Andric             }
57681ad6265SDimitry Andric 
57781ad6265SDimitry Andric             // Then sort the bp & bp.loc entries for later use:
57881ad6265SDimitry Andric             if (bkpt_id.GetLocationID() == LLDB_INVALID_BREAK_ID)
57981ad6265SDimitry Andric               bkpts_seen.insert(bkpt_id.GetBreakpointID());
58081ad6265SDimitry Andric             else {
58181ad6265SDimitry Andric               bkpts_with_locs_seen.insert(bkpt_id.GetBreakpointID());
58281ad6265SDimitry Andric               with_locs.AddBreakpointID(bkpt_id);
58381ad6265SDimitry Andric             }
58481ad6265SDimitry Andric           }
58581ad6265SDimitry Andric         }
58681ad6265SDimitry Andric         // Do all the error checking here so once we start disabling we don't
58781ad6265SDimitry Andric         // have to back out half-way through.
58881ad6265SDimitry Andric 
58981ad6265SDimitry Andric         // Make sure at least one of the specified breakpoints is enabled.
59081ad6265SDimitry Andric         if (!any_enabled) {
59181ad6265SDimitry Andric           result.AppendError("at least one of the continue-to breakpoints must "
59281ad6265SDimitry Andric                              "be enabled.");
5935f757f3fSDimitry Andric           return;
59481ad6265SDimitry Andric         }
59581ad6265SDimitry Andric 
59681ad6265SDimitry Andric         // Also, if you specify BOTH a breakpoint and one of it's locations,
59781ad6265SDimitry Andric         // we flag that as an error, since it won't do what you expect, the
59881ad6265SDimitry Andric         // breakpoint directive will mean "run to all locations", which is not
59981ad6265SDimitry Andric         // what the location directive means...
60081ad6265SDimitry Andric         for (break_id_t bp_id : bkpts_with_locs_seen) {
60181ad6265SDimitry Andric           if (bkpts_seen.count(bp_id)) {
60281ad6265SDimitry Andric             result.AppendErrorWithFormatv("can't specify both a breakpoint and "
60381ad6265SDimitry Andric                                "one of its locations: {0}", bp_id);
60481ad6265SDimitry Andric           }
60581ad6265SDimitry Andric         }
60681ad6265SDimitry Andric 
60781ad6265SDimitry Andric         // Now go through the breakpoints in the target, disabling all the ones
60881ad6265SDimitry Andric         // that the user didn't mention:
60981ad6265SDimitry Andric         for (BreakpointSP bp_sp : bkpt_list.Breakpoints()) {
61081ad6265SDimitry Andric           break_id_t bp_id = bp_sp->GetID();
61181ad6265SDimitry Andric           // Handle the case where no locations were specified.  Note we don't
61281ad6265SDimitry Andric           // have to worry about the case where a breakpoint and one of its
61381ad6265SDimitry Andric           // locations are both in the lists, we've already disallowed that.
61481ad6265SDimitry Andric           if (!bkpts_with_locs_seen.count(bp_id)) {
61581ad6265SDimitry Andric             if (!bkpts_seen.count(bp_id) && bp_sp->IsEnabled()) {
61681ad6265SDimitry Andric               bkpts_disabled.push_back(bp_id);
61781ad6265SDimitry Andric               bp_sp->SetEnabled(false);
61881ad6265SDimitry Andric             }
61981ad6265SDimitry Andric             continue;
62081ad6265SDimitry Andric           }
62181ad6265SDimitry Andric           // Next, handle the case where a location was specified:
62281ad6265SDimitry Andric           // Run through all the locations of this breakpoint and disable
62381ad6265SDimitry Andric           // the ones that aren't on our "with locations" BreakpointID list:
62481ad6265SDimitry Andric           size_t num_locations = bp_sp->GetNumLocations();
62581ad6265SDimitry Andric           BreakpointID tmp_id(bp_id, LLDB_INVALID_BREAK_ID);
62681ad6265SDimitry Andric           for (size_t loc_idx = 0; loc_idx < num_locations; loc_idx++) {
62781ad6265SDimitry Andric             BreakpointLocationSP loc_sp = bp_sp->GetLocationAtIndex(loc_idx);
62881ad6265SDimitry Andric             tmp_id.SetBreakpointLocationID(loc_idx);
629*0fca6ea1SDimitry Andric             if (!with_locs.Contains(tmp_id) && loc_sp->IsEnabled()) {
63081ad6265SDimitry Andric               locs_disabled.push_back(tmp_id);
63181ad6265SDimitry Andric               loc_sp->SetEnabled(false);
63281ad6265SDimitry Andric             }
63381ad6265SDimitry Andric           }
63481ad6265SDimitry Andric         }
63581ad6265SDimitry Andric       }
63681ad6265SDimitry Andric 
6370b57cec5SDimitry Andric       { // Scope for thread list mutex:
6380b57cec5SDimitry Andric         std::lock_guard<std::recursive_mutex> guard(
6390b57cec5SDimitry Andric             process->GetThreadList().GetMutex());
6400b57cec5SDimitry Andric         const uint32_t num_threads = process->GetThreadList().GetSize();
6410b57cec5SDimitry Andric 
6420b57cec5SDimitry Andric         // Set the actions that the threads should each take when resuming
6430b57cec5SDimitry Andric         for (uint32_t idx = 0; idx < num_threads; ++idx) {
6440b57cec5SDimitry Andric           const bool override_suspend = false;
6450b57cec5SDimitry Andric           process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState(
6460b57cec5SDimitry Andric               eStateRunning, override_suspend);
6470b57cec5SDimitry Andric         }
6480b57cec5SDimitry Andric       }
6490b57cec5SDimitry Andric 
6500b57cec5SDimitry Andric       const uint32_t iohandler_id = process->GetIOHandlerID();
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric       StreamString stream;
6530b57cec5SDimitry Andric       Status error;
65481ad6265SDimitry Andric       // For now we can only do -b with synchronous:
65581ad6265SDimitry Andric       bool old_sync = GetDebugger().GetAsyncExecution();
65681ad6265SDimitry Andric 
65781ad6265SDimitry Andric       if (run_to_bkpt_ids.GetSize() != 0) {
65881ad6265SDimitry Andric         GetDebugger().SetAsyncExecution(false);
65981ad6265SDimitry Andric         synchronous_execution = true;
66081ad6265SDimitry Andric       }
6610b57cec5SDimitry Andric       if (synchronous_execution)
6620b57cec5SDimitry Andric         error = process->ResumeSynchronous(&stream);
6630b57cec5SDimitry Andric       else
6640b57cec5SDimitry Andric         error = process->Resume();
6650b57cec5SDimitry Andric 
66681ad6265SDimitry Andric       if (run_to_bkpt_ids.GetSize() != 0) {
66781ad6265SDimitry Andric         GetDebugger().SetAsyncExecution(old_sync);
66881ad6265SDimitry Andric       }
66981ad6265SDimitry Andric 
67081ad6265SDimitry Andric       // Now re-enable the breakpoints we disabled:
67181ad6265SDimitry Andric       BreakpointList &bkpt_list = target->GetBreakpointList();
67281ad6265SDimitry Andric       for (break_id_t bp_id : bkpts_disabled) {
67381ad6265SDimitry Andric         BreakpointSP bp_sp = bkpt_list.FindBreakpointByID(bp_id);
67481ad6265SDimitry Andric         if (bp_sp)
67581ad6265SDimitry Andric           bp_sp->SetEnabled(true);
67681ad6265SDimitry Andric       }
67781ad6265SDimitry Andric       for (const BreakpointID &bkpt_id : locs_disabled) {
67881ad6265SDimitry Andric         BreakpointSP bp_sp
67981ad6265SDimitry Andric             = bkpt_list.FindBreakpointByID(bkpt_id.GetBreakpointID());
68081ad6265SDimitry Andric         if (bp_sp) {
68181ad6265SDimitry Andric           BreakpointLocationSP loc_sp
68281ad6265SDimitry Andric               = bp_sp->FindLocationByID(bkpt_id.GetLocationID());
68381ad6265SDimitry Andric           if (loc_sp)
68481ad6265SDimitry Andric             loc_sp->SetEnabled(true);
68581ad6265SDimitry Andric         }
68681ad6265SDimitry Andric       }
68781ad6265SDimitry Andric 
6880b57cec5SDimitry Andric       if (error.Success()) {
6890b57cec5SDimitry Andric         // There is a race condition where this thread will return up the call
6900b57cec5SDimitry Andric         // stack to the main command handler and show an (lldb) prompt before
6910b57cec5SDimitry Andric         // HandlePrivateEvent (from PrivateStateThread) has a chance to call
6920b57cec5SDimitry Andric         // PushProcessIOHandler().
6930b57cec5SDimitry Andric         process->SyncIOHandler(iohandler_id, std::chrono::seconds(2));
6940b57cec5SDimitry Andric 
6950b57cec5SDimitry Andric         result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
6960b57cec5SDimitry Andric                                        process->GetID());
6970b57cec5SDimitry Andric         if (synchronous_execution) {
6980b57cec5SDimitry Andric           // If any state changed events had anything to say, add that to the
6990b57cec5SDimitry Andric           // result
7000b57cec5SDimitry Andric           result.AppendMessage(stream.GetString());
7010b57cec5SDimitry Andric 
7020b57cec5SDimitry Andric           result.SetDidChangeProcessState(true);
7030b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessFinishNoResult);
7040b57cec5SDimitry Andric         } else {
7050b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessContinuingNoResult);
7060b57cec5SDimitry Andric         }
7070b57cec5SDimitry Andric       } else {
7080b57cec5SDimitry Andric         result.AppendErrorWithFormat("Failed to resume process: %s.\n",
7090b57cec5SDimitry Andric                                      error.AsCString());
7100b57cec5SDimitry Andric       }
7110b57cec5SDimitry Andric     } else {
7120b57cec5SDimitry Andric       result.AppendErrorWithFormat(
7130b57cec5SDimitry Andric           "Process cannot be continued from its current state (%s).\n",
7140b57cec5SDimitry Andric           StateAsCString(state));
7150b57cec5SDimitry Andric     }
7160b57cec5SDimitry Andric   }
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
7190b57cec5SDimitry Andric 
7200b57cec5SDimitry Andric   CommandOptions m_options;
7210b57cec5SDimitry Andric };
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric // CommandObjectProcessDetach
7249dba64beSDimitry Andric #define LLDB_OPTIONS_process_detach
7259dba64beSDimitry Andric #include "CommandOptions.inc"
7260b57cec5SDimitry Andric 
7270b57cec5SDimitry Andric #pragma mark CommandObjectProcessDetach
7280b57cec5SDimitry Andric 
7290b57cec5SDimitry Andric class CommandObjectProcessDetach : public CommandObjectParsed {
7300b57cec5SDimitry Andric public:
7310b57cec5SDimitry Andric   class CommandOptions : public Options {
7320b57cec5SDimitry Andric   public:
73304eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
7340b57cec5SDimitry Andric 
7350b57cec5SDimitry Andric     ~CommandOptions() override = default;
7360b57cec5SDimitry Andric 
7370b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
7380b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
7390b57cec5SDimitry Andric       Status error;
7400b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
7410b57cec5SDimitry Andric 
7420b57cec5SDimitry Andric       switch (short_option) {
7430b57cec5SDimitry Andric       case 's':
7440b57cec5SDimitry Andric         bool tmp_result;
7450b57cec5SDimitry Andric         bool success;
7460b57cec5SDimitry Andric         tmp_result = OptionArgParser::ToBoolean(option_arg, false, &success);
7470b57cec5SDimitry Andric         if (!success)
7480b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid boolean option: \"%s\"",
7490b57cec5SDimitry Andric                                          option_arg.str().c_str());
7500b57cec5SDimitry Andric         else {
7510b57cec5SDimitry Andric           if (tmp_result)
7520b57cec5SDimitry Andric             m_keep_stopped = eLazyBoolYes;
7530b57cec5SDimitry Andric           else
7540b57cec5SDimitry Andric             m_keep_stopped = eLazyBoolNo;
7550b57cec5SDimitry Andric         }
7560b57cec5SDimitry Andric         break;
7570b57cec5SDimitry Andric       default:
7589dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
7590b57cec5SDimitry Andric       }
7600b57cec5SDimitry Andric       return error;
7610b57cec5SDimitry Andric     }
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
7640b57cec5SDimitry Andric       m_keep_stopped = eLazyBoolCalculate;
7650b57cec5SDimitry Andric     }
7660b57cec5SDimitry Andric 
7670b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
768bdd1243dSDimitry Andric       return llvm::ArrayRef(g_process_detach_options);
7690b57cec5SDimitry Andric     }
7700b57cec5SDimitry Andric 
7710b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
7720b57cec5SDimitry Andric     LazyBool m_keep_stopped;
7730b57cec5SDimitry Andric   };
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric   CommandObjectProcessDetach(CommandInterpreter &interpreter)
7760b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "process detach",
7770b57cec5SDimitry Andric                             "Detach from the current target process.",
7780b57cec5SDimitry Andric                             "process detach",
7790b57cec5SDimitry Andric                             eCommandRequiresProcess | eCommandTryTargetAPILock |
78004eeddc0SDimitry Andric                                 eCommandProcessMustBeLaunched) {}
7810b57cec5SDimitry Andric 
7820b57cec5SDimitry Andric   ~CommandObjectProcessDetach() override = default;
7830b57cec5SDimitry Andric 
7840b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
7850b57cec5SDimitry Andric 
7860b57cec5SDimitry Andric protected:
7875f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
7880b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
7890b57cec5SDimitry Andric     // FIXME: This will be a Command Option:
7900b57cec5SDimitry Andric     bool keep_stopped;
7910b57cec5SDimitry Andric     if (m_options.m_keep_stopped == eLazyBoolCalculate) {
7920b57cec5SDimitry Andric       // Check the process default:
7930b57cec5SDimitry Andric       keep_stopped = process->GetDetachKeepsStopped();
7940b57cec5SDimitry Andric     } else if (m_options.m_keep_stopped == eLazyBoolYes)
7950b57cec5SDimitry Andric       keep_stopped = true;
7960b57cec5SDimitry Andric     else
7970b57cec5SDimitry Andric       keep_stopped = false;
7980b57cec5SDimitry Andric 
7990b57cec5SDimitry Andric     Status error(process->Detach(keep_stopped));
8000b57cec5SDimitry Andric     if (error.Success()) {
8010b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
8020b57cec5SDimitry Andric     } else {
8030b57cec5SDimitry Andric       result.AppendErrorWithFormat("Detach failed: %s\n", error.AsCString());
8040b57cec5SDimitry Andric     }
8050b57cec5SDimitry Andric   }
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric   CommandOptions m_options;
8080b57cec5SDimitry Andric };
8090b57cec5SDimitry Andric 
8100b57cec5SDimitry Andric // CommandObjectProcessConnect
8119dba64beSDimitry Andric #define LLDB_OPTIONS_process_connect
8129dba64beSDimitry Andric #include "CommandOptions.inc"
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric #pragma mark CommandObjectProcessConnect
8150b57cec5SDimitry Andric 
8160b57cec5SDimitry Andric class CommandObjectProcessConnect : public CommandObjectParsed {
8170b57cec5SDimitry Andric public:
8180b57cec5SDimitry Andric   class CommandOptions : public Options {
8190b57cec5SDimitry Andric   public:
82004eeddc0SDimitry Andric     CommandOptions() {
8210b57cec5SDimitry Andric       // Keep default values of all options in one place: OptionParsingStarting
8220b57cec5SDimitry Andric       // ()
8230b57cec5SDimitry Andric       OptionParsingStarting(nullptr);
8240b57cec5SDimitry Andric     }
8250b57cec5SDimitry Andric 
8260b57cec5SDimitry Andric     ~CommandOptions() override = default;
8270b57cec5SDimitry Andric 
8280b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
8290b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
8300b57cec5SDimitry Andric       Status error;
8310b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
8320b57cec5SDimitry Andric 
8330b57cec5SDimitry Andric       switch (short_option) {
8340b57cec5SDimitry Andric       case 'p':
8355ffd83dbSDimitry Andric         plugin_name.assign(std::string(option_arg));
8360b57cec5SDimitry Andric         break;
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric       default:
8399dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
8400b57cec5SDimitry Andric       }
8410b57cec5SDimitry Andric       return error;
8420b57cec5SDimitry Andric     }
8430b57cec5SDimitry Andric 
8440b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
8450b57cec5SDimitry Andric       plugin_name.clear();
8460b57cec5SDimitry Andric     }
8470b57cec5SDimitry Andric 
8480b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
849bdd1243dSDimitry Andric       return llvm::ArrayRef(g_process_connect_options);
8500b57cec5SDimitry Andric     }
8510b57cec5SDimitry Andric 
8520b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
8530b57cec5SDimitry Andric 
8540b57cec5SDimitry Andric     std::string plugin_name;
8550b57cec5SDimitry Andric   };
8560b57cec5SDimitry Andric 
8570b57cec5SDimitry Andric   CommandObjectProcessConnect(CommandInterpreter &interpreter)
8580b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "process connect",
8590b57cec5SDimitry Andric                             "Connect to a remote debug service.",
86081ad6265SDimitry Andric                             "process connect <remote-url>", 0) {
861*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypeConnectURL);
86281ad6265SDimitry Andric   }
8630b57cec5SDimitry Andric 
8640b57cec5SDimitry Andric   ~CommandObjectProcessConnect() override = default;
8650b57cec5SDimitry Andric 
8660b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
8670b57cec5SDimitry Andric 
8680b57cec5SDimitry Andric protected:
8695f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
8700b57cec5SDimitry Andric     if (command.GetArgumentCount() != 1) {
8710b57cec5SDimitry Andric       result.AppendErrorWithFormat(
8720b57cec5SDimitry Andric           "'%s' takes exactly one argument:\nUsage: %s\n", m_cmd_name.c_str(),
8730b57cec5SDimitry Andric           m_cmd_syntax.c_str());
8745f757f3fSDimitry Andric       return;
8750b57cec5SDimitry Andric     }
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
8780b57cec5SDimitry Andric     if (process && process->IsAlive()) {
8790b57cec5SDimitry Andric       result.AppendErrorWithFormat(
8800b57cec5SDimitry Andric           "Process %" PRIu64
8810b57cec5SDimitry Andric           " is currently being debugged, kill the process before connecting.\n",
8820b57cec5SDimitry Andric           process->GetID());
8835f757f3fSDimitry Andric       return;
8840b57cec5SDimitry Andric     }
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric     const char *plugin_name = nullptr;
8870b57cec5SDimitry Andric     if (!m_options.plugin_name.empty())
8880b57cec5SDimitry Andric       plugin_name = m_options.plugin_name.c_str();
8890b57cec5SDimitry Andric 
8900b57cec5SDimitry Andric     Status error;
8910b57cec5SDimitry Andric     Debugger &debugger = GetDebugger();
8920b57cec5SDimitry Andric     PlatformSP platform_sp = m_interpreter.GetPlatform(true);
8935ffd83dbSDimitry Andric     ProcessSP process_sp =
8945ffd83dbSDimitry Andric         debugger.GetAsyncExecution()
8955ffd83dbSDimitry Andric             ? platform_sp->ConnectProcess(
8960b57cec5SDimitry Andric                   command.GetArgumentAtIndex(0), plugin_name, debugger,
8975ffd83dbSDimitry Andric                   debugger.GetSelectedTarget().get(), error)
8985ffd83dbSDimitry Andric             : platform_sp->ConnectProcessSynchronous(
8995ffd83dbSDimitry Andric                   command.GetArgumentAtIndex(0), plugin_name, debugger,
9005ffd83dbSDimitry Andric                   result.GetOutputStream(), debugger.GetSelectedTarget().get(),
9015ffd83dbSDimitry Andric                   error);
9020b57cec5SDimitry Andric     if (error.Fail() || process_sp == nullptr) {
9030b57cec5SDimitry Andric       result.AppendError(error.AsCString("Error connecting to the process"));
9040b57cec5SDimitry Andric     }
9050b57cec5SDimitry Andric   }
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric   CommandOptions m_options;
9080b57cec5SDimitry Andric };
9090b57cec5SDimitry Andric 
9100b57cec5SDimitry Andric // CommandObjectProcessPlugin
9110b57cec5SDimitry Andric #pragma mark CommandObjectProcessPlugin
9120b57cec5SDimitry Andric 
9130b57cec5SDimitry Andric class CommandObjectProcessPlugin : public CommandObjectProxy {
9140b57cec5SDimitry Andric public:
9150b57cec5SDimitry Andric   CommandObjectProcessPlugin(CommandInterpreter &interpreter)
9160b57cec5SDimitry Andric       : CommandObjectProxy(
9170b57cec5SDimitry Andric             interpreter, "process plugin",
9180b57cec5SDimitry Andric             "Send a custom command to the current target process plug-in.",
9190b57cec5SDimitry Andric             "process plugin <args>", 0) {}
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric   ~CommandObjectProcessPlugin() override = default;
9220b57cec5SDimitry Andric 
9230b57cec5SDimitry Andric   CommandObject *GetProxyCommandObject() override {
9240b57cec5SDimitry Andric     Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
9250b57cec5SDimitry Andric     if (process)
9260b57cec5SDimitry Andric       return process->GetPluginCommandObject();
9270b57cec5SDimitry Andric     return nullptr;
9280b57cec5SDimitry Andric   }
9290b57cec5SDimitry Andric };
9300b57cec5SDimitry Andric 
9310b57cec5SDimitry Andric // CommandObjectProcessLoad
9329dba64beSDimitry Andric #define LLDB_OPTIONS_process_load
9339dba64beSDimitry Andric #include "CommandOptions.inc"
9340b57cec5SDimitry Andric 
9350b57cec5SDimitry Andric #pragma mark CommandObjectProcessLoad
9360b57cec5SDimitry Andric 
9370b57cec5SDimitry Andric class CommandObjectProcessLoad : public CommandObjectParsed {
9380b57cec5SDimitry Andric public:
9390b57cec5SDimitry Andric   class CommandOptions : public Options {
9400b57cec5SDimitry Andric   public:
94104eeddc0SDimitry Andric     CommandOptions() {
9420b57cec5SDimitry Andric       // Keep default values of all options in one place: OptionParsingStarting
9430b57cec5SDimitry Andric       // ()
9440b57cec5SDimitry Andric       OptionParsingStarting(nullptr);
9450b57cec5SDimitry Andric     }
9460b57cec5SDimitry Andric 
9470b57cec5SDimitry Andric     ~CommandOptions() override = default;
9480b57cec5SDimitry Andric 
9490b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
9500b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
9510b57cec5SDimitry Andric       Status error;
9520b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
953*0fca6ea1SDimitry Andric       ArchSpec arch =
954*0fca6ea1SDimitry Andric           execution_context->GetProcessPtr()->GetSystemArchitecture();
9550b57cec5SDimitry Andric       switch (short_option) {
9560b57cec5SDimitry Andric       case 'i':
9570b57cec5SDimitry Andric         do_install = true;
9580b57cec5SDimitry Andric         if (!option_arg.empty())
959*0fca6ea1SDimitry Andric           install_path.SetFile(option_arg, arch.GetTriple());
9600b57cec5SDimitry Andric         break;
9610b57cec5SDimitry Andric       default:
9629dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
9630b57cec5SDimitry Andric       }
9640b57cec5SDimitry Andric       return error;
9650b57cec5SDimitry Andric     }
9660b57cec5SDimitry Andric 
9670b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
9680b57cec5SDimitry Andric       do_install = false;
9690b57cec5SDimitry Andric       install_path.Clear();
9700b57cec5SDimitry Andric     }
9710b57cec5SDimitry Andric 
9720b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
973bdd1243dSDimitry Andric       return llvm::ArrayRef(g_process_load_options);
9740b57cec5SDimitry Andric     }
9750b57cec5SDimitry Andric 
9760b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
9770b57cec5SDimitry Andric     bool do_install;
9780b57cec5SDimitry Andric     FileSpec install_path;
9790b57cec5SDimitry Andric   };
9800b57cec5SDimitry Andric 
9810b57cec5SDimitry Andric   CommandObjectProcessLoad(CommandInterpreter &interpreter)
9820b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "process load",
9830b57cec5SDimitry Andric                             "Load a shared library into the current process.",
9840b57cec5SDimitry Andric                             "process load <filename> [<filename> ...]",
9850b57cec5SDimitry Andric                             eCommandRequiresProcess | eCommandTryTargetAPILock |
9860b57cec5SDimitry Andric                                 eCommandProcessMustBeLaunched |
98781ad6265SDimitry Andric                                 eCommandProcessMustBePaused) {
988*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypePath, eArgRepeatPlus);
98981ad6265SDimitry Andric   }
9900b57cec5SDimitry Andric 
9910b57cec5SDimitry Andric   ~CommandObjectProcessLoad() override = default;
9920b57cec5SDimitry Andric 
993e8d8bef9SDimitry Andric   void
994e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
995e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
996e8d8bef9SDimitry Andric     if (!m_exe_ctx.HasProcessScope())
997e8d8bef9SDimitry Andric       return;
998*0fca6ea1SDimitry Andric     CommandObject::HandleArgumentCompletion(request, opt_element_vector);
999e8d8bef9SDimitry Andric   }
1000e8d8bef9SDimitry Andric 
10010b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
10020b57cec5SDimitry Andric 
10030b57cec5SDimitry Andric protected:
10045f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
10050b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
10060b57cec5SDimitry Andric 
10070b57cec5SDimitry Andric     for (auto &entry : command.entries()) {
10080b57cec5SDimitry Andric       Status error;
10090b57cec5SDimitry Andric       PlatformSP platform = process->GetTarget().GetPlatform();
10109dba64beSDimitry Andric       llvm::StringRef image_path = entry.ref();
10110b57cec5SDimitry Andric       uint32_t image_token = LLDB_INVALID_IMAGE_TOKEN;
10120b57cec5SDimitry Andric 
10130b57cec5SDimitry Andric       if (!m_options.do_install) {
10140b57cec5SDimitry Andric         FileSpec image_spec(image_path);
10150b57cec5SDimitry Andric         platform->ResolveRemotePath(image_spec, image_spec);
10160b57cec5SDimitry Andric         image_token =
10170b57cec5SDimitry Andric             platform->LoadImage(process, FileSpec(), image_spec, error);
10180b57cec5SDimitry Andric       } else if (m_options.install_path) {
10190b57cec5SDimitry Andric         FileSpec image_spec(image_path);
10200b57cec5SDimitry Andric         FileSystem::Instance().Resolve(image_spec);
10210b57cec5SDimitry Andric         platform->ResolveRemotePath(m_options.install_path,
10220b57cec5SDimitry Andric                                     m_options.install_path);
10230b57cec5SDimitry Andric         image_token = platform->LoadImage(process, image_spec,
10240b57cec5SDimitry Andric                                           m_options.install_path, error);
10250b57cec5SDimitry Andric       } else {
10260b57cec5SDimitry Andric         FileSpec image_spec(image_path);
10270b57cec5SDimitry Andric         FileSystem::Instance().Resolve(image_spec);
10280b57cec5SDimitry Andric         image_token =
10290b57cec5SDimitry Andric             platform->LoadImage(process, image_spec, FileSpec(), error);
10300b57cec5SDimitry Andric       }
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric       if (image_token != LLDB_INVALID_IMAGE_TOKEN) {
10330b57cec5SDimitry Andric         result.AppendMessageWithFormat(
10340b57cec5SDimitry Andric             "Loading \"%s\"...ok\nImage %u loaded.\n", image_path.str().c_str(),
10350b57cec5SDimitry Andric             image_token);
10360b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishResult);
10370b57cec5SDimitry Andric       } else {
10380b57cec5SDimitry Andric         result.AppendErrorWithFormat("failed to load '%s': %s",
10390b57cec5SDimitry Andric                                      image_path.str().c_str(),
10400b57cec5SDimitry Andric                                      error.AsCString());
10410b57cec5SDimitry Andric       }
10420b57cec5SDimitry Andric     }
10430b57cec5SDimitry Andric   }
10440b57cec5SDimitry Andric 
10450b57cec5SDimitry Andric   CommandOptions m_options;
10460b57cec5SDimitry Andric };
10470b57cec5SDimitry Andric 
10480b57cec5SDimitry Andric // CommandObjectProcessUnload
10490b57cec5SDimitry Andric #pragma mark CommandObjectProcessUnload
10500b57cec5SDimitry Andric 
10510b57cec5SDimitry Andric class CommandObjectProcessUnload : public CommandObjectParsed {
10520b57cec5SDimitry Andric public:
10530b57cec5SDimitry Andric   CommandObjectProcessUnload(CommandInterpreter &interpreter)
10540b57cec5SDimitry Andric       : CommandObjectParsed(
10550b57cec5SDimitry Andric             interpreter, "process unload",
10560b57cec5SDimitry Andric             "Unload a shared library from the current process using the index "
10570b57cec5SDimitry Andric             "returned by a previous call to \"process load\".",
10580b57cec5SDimitry Andric             "process unload <index>",
10590b57cec5SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
106081ad6265SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {
1061*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypeUnsignedInteger);
106281ad6265SDimitry Andric   }
10630b57cec5SDimitry Andric 
10640b57cec5SDimitry Andric   ~CommandObjectProcessUnload() override = default;
10650b57cec5SDimitry Andric 
1066e8d8bef9SDimitry Andric   void
1067e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
1068e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
1069e8d8bef9SDimitry Andric 
1070e8d8bef9SDimitry Andric     if (request.GetCursorIndex() || !m_exe_ctx.HasProcessScope())
1071e8d8bef9SDimitry Andric       return;
1072e8d8bef9SDimitry Andric 
1073e8d8bef9SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
1074e8d8bef9SDimitry Andric 
1075e8d8bef9SDimitry Andric     const std::vector<lldb::addr_t> &tokens = process->GetImageTokens();
1076e8d8bef9SDimitry Andric     const size_t token_num = tokens.size();
1077e8d8bef9SDimitry Andric     for (size_t i = 0; i < token_num; ++i) {
1078e8d8bef9SDimitry Andric       if (tokens[i] == LLDB_INVALID_IMAGE_TOKEN)
1079e8d8bef9SDimitry Andric         continue;
1080e8d8bef9SDimitry Andric       request.TryCompleteCurrentArg(std::to_string(i));
1081e8d8bef9SDimitry Andric     }
1082e8d8bef9SDimitry Andric   }
1083e8d8bef9SDimitry Andric 
10840b57cec5SDimitry Andric protected:
10855f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
10860b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
10870b57cec5SDimitry Andric 
10880b57cec5SDimitry Andric     for (auto &entry : command.entries()) {
10890b57cec5SDimitry Andric       uint32_t image_token;
10909dba64beSDimitry Andric       if (entry.ref().getAsInteger(0, image_token)) {
10910b57cec5SDimitry Andric         result.AppendErrorWithFormat("invalid image index argument '%s'",
10929dba64beSDimitry Andric                                      entry.ref().str().c_str());
10930b57cec5SDimitry Andric         break;
10940b57cec5SDimitry Andric       } else {
10950b57cec5SDimitry Andric         Status error(process->GetTarget().GetPlatform()->UnloadImage(
10960b57cec5SDimitry Andric             process, image_token));
10970b57cec5SDimitry Andric         if (error.Success()) {
10980b57cec5SDimitry Andric           result.AppendMessageWithFormat(
10990b57cec5SDimitry Andric               "Unloading shared library with index %u...ok\n", image_token);
11000b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessFinishResult);
11010b57cec5SDimitry Andric         } else {
11020b57cec5SDimitry Andric           result.AppendErrorWithFormat("failed to unload image: %s",
11030b57cec5SDimitry Andric                                        error.AsCString());
11040b57cec5SDimitry Andric           break;
11050b57cec5SDimitry Andric         }
11060b57cec5SDimitry Andric       }
11070b57cec5SDimitry Andric     }
11080b57cec5SDimitry Andric   }
11090b57cec5SDimitry Andric };
11100b57cec5SDimitry Andric 
11110b57cec5SDimitry Andric // CommandObjectProcessSignal
11120b57cec5SDimitry Andric #pragma mark CommandObjectProcessSignal
11130b57cec5SDimitry Andric 
11140b57cec5SDimitry Andric class CommandObjectProcessSignal : public CommandObjectParsed {
11150b57cec5SDimitry Andric public:
11160b57cec5SDimitry Andric   CommandObjectProcessSignal(CommandInterpreter &interpreter)
1117480093f4SDimitry Andric       : CommandObjectParsed(
1118480093f4SDimitry Andric             interpreter, "process signal",
1119480093f4SDimitry Andric             "Send a UNIX signal to the current target process.", nullptr,
1120480093f4SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock) {
1121*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypeUnixSignal);
11220b57cec5SDimitry Andric   }
11230b57cec5SDimitry Andric 
11240b57cec5SDimitry Andric   ~CommandObjectProcessSignal() override = default;
11250b57cec5SDimitry Andric 
11265ffd83dbSDimitry Andric   void
11275ffd83dbSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
11285ffd83dbSDimitry Andric                            OptionElementVector &opt_element_vector) override {
11295ffd83dbSDimitry Andric     if (!m_exe_ctx.HasProcessScope() || request.GetCursorIndex() != 0)
11305ffd83dbSDimitry Andric       return;
11315ffd83dbSDimitry Andric 
11325ffd83dbSDimitry Andric     UnixSignalsSP signals = m_exe_ctx.GetProcessPtr()->GetUnixSignals();
11335ffd83dbSDimitry Andric     int signo = signals->GetFirstSignalNumber();
11345ffd83dbSDimitry Andric     while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
11355f757f3fSDimitry Andric       request.TryCompleteCurrentArg(signals->GetSignalAsStringRef(signo));
11365ffd83dbSDimitry Andric       signo = signals->GetNextSignalNumber(signo);
11375ffd83dbSDimitry Andric     }
11385ffd83dbSDimitry Andric   }
11395ffd83dbSDimitry Andric 
11400b57cec5SDimitry Andric protected:
11415f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
11420b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
11430b57cec5SDimitry Andric 
11440b57cec5SDimitry Andric     if (command.GetArgumentCount() == 1) {
11450b57cec5SDimitry Andric       int signo = LLDB_INVALID_SIGNAL_NUMBER;
11460b57cec5SDimitry Andric 
11470b57cec5SDimitry Andric       const char *signal_name = command.GetArgumentAtIndex(0);
11485ffd83dbSDimitry Andric       if (::isxdigit(signal_name[0])) {
11495ffd83dbSDimitry Andric         if (!llvm::to_integer(signal_name, signo))
11505ffd83dbSDimitry Andric           signo = LLDB_INVALID_SIGNAL_NUMBER;
11515ffd83dbSDimitry Andric       } else
11520b57cec5SDimitry Andric         signo = process->GetUnixSignals()->GetSignalNumberFromName(signal_name);
11530b57cec5SDimitry Andric 
11540b57cec5SDimitry Andric       if (signo == LLDB_INVALID_SIGNAL_NUMBER) {
11550b57cec5SDimitry Andric         result.AppendErrorWithFormat("Invalid signal argument '%s'.\n",
11560b57cec5SDimitry Andric                                      command.GetArgumentAtIndex(0));
11570b57cec5SDimitry Andric       } else {
11580b57cec5SDimitry Andric         Status error(process->Signal(signo));
11590b57cec5SDimitry Andric         if (error.Success()) {
11600b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessFinishResult);
11610b57cec5SDimitry Andric         } else {
11620b57cec5SDimitry Andric           result.AppendErrorWithFormat("Failed to send signal %i: %s\n", signo,
11630b57cec5SDimitry Andric                                        error.AsCString());
11640b57cec5SDimitry Andric         }
11650b57cec5SDimitry Andric       }
11660b57cec5SDimitry Andric     } else {
11670b57cec5SDimitry Andric       result.AppendErrorWithFormat(
11680b57cec5SDimitry Andric           "'%s' takes exactly one signal number argument:\nUsage: %s\n",
11690b57cec5SDimitry Andric           m_cmd_name.c_str(), m_cmd_syntax.c_str());
11700b57cec5SDimitry Andric     }
11710b57cec5SDimitry Andric   }
11720b57cec5SDimitry Andric };
11730b57cec5SDimitry Andric 
11740b57cec5SDimitry Andric // CommandObjectProcessInterrupt
11750b57cec5SDimitry Andric #pragma mark CommandObjectProcessInterrupt
11760b57cec5SDimitry Andric 
11770b57cec5SDimitry Andric class CommandObjectProcessInterrupt : public CommandObjectParsed {
11780b57cec5SDimitry Andric public:
11790b57cec5SDimitry Andric   CommandObjectProcessInterrupt(CommandInterpreter &interpreter)
11800b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "process interrupt",
11810b57cec5SDimitry Andric                             "Interrupt the current target process.",
11820b57cec5SDimitry Andric                             "process interrupt",
11830b57cec5SDimitry Andric                             eCommandRequiresProcess | eCommandTryTargetAPILock |
11840b57cec5SDimitry Andric                                 eCommandProcessMustBeLaunched) {}
11850b57cec5SDimitry Andric 
11860b57cec5SDimitry Andric   ~CommandObjectProcessInterrupt() override = default;
11870b57cec5SDimitry Andric 
11880b57cec5SDimitry Andric protected:
11895f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
11900b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
11910b57cec5SDimitry Andric     if (process == nullptr) {
11920b57cec5SDimitry Andric       result.AppendError("no process to halt");
11935f757f3fSDimitry Andric       return;
11940b57cec5SDimitry Andric     }
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric     bool clear_thread_plans = true;
11970b57cec5SDimitry Andric     Status error(process->Halt(clear_thread_plans));
11980b57cec5SDimitry Andric     if (error.Success()) {
11990b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
12000b57cec5SDimitry Andric     } else {
12010b57cec5SDimitry Andric       result.AppendErrorWithFormat("Failed to halt process: %s\n",
12020b57cec5SDimitry Andric                                    error.AsCString());
12030b57cec5SDimitry Andric     }
12040b57cec5SDimitry Andric   }
12050b57cec5SDimitry Andric };
12060b57cec5SDimitry Andric 
12070b57cec5SDimitry Andric // CommandObjectProcessKill
12080b57cec5SDimitry Andric #pragma mark CommandObjectProcessKill
12090b57cec5SDimitry Andric 
12100b57cec5SDimitry Andric class CommandObjectProcessKill : public CommandObjectParsed {
12110b57cec5SDimitry Andric public:
12120b57cec5SDimitry Andric   CommandObjectProcessKill(CommandInterpreter &interpreter)
12130b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "process kill",
12140b57cec5SDimitry Andric                             "Terminate the current target process.",
12150b57cec5SDimitry Andric                             "process kill",
12160b57cec5SDimitry Andric                             eCommandRequiresProcess | eCommandTryTargetAPILock |
12170b57cec5SDimitry Andric                                 eCommandProcessMustBeLaunched) {}
12180b57cec5SDimitry Andric 
12190b57cec5SDimitry Andric   ~CommandObjectProcessKill() override = default;
12200b57cec5SDimitry Andric 
12210b57cec5SDimitry Andric protected:
12225f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
12230b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
12240b57cec5SDimitry Andric     if (process == nullptr) {
12250b57cec5SDimitry Andric       result.AppendError("no process to kill");
12265f757f3fSDimitry Andric       return;
12270b57cec5SDimitry Andric     }
12280b57cec5SDimitry Andric 
12290b57cec5SDimitry Andric     Status error(process->Destroy(true));
12300b57cec5SDimitry Andric     if (error.Success()) {
12310b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
12320b57cec5SDimitry Andric     } else {
12330b57cec5SDimitry Andric       result.AppendErrorWithFormat("Failed to kill process: %s\n",
12340b57cec5SDimitry Andric                                    error.AsCString());
12350b57cec5SDimitry Andric     }
12360b57cec5SDimitry Andric   }
12370b57cec5SDimitry Andric };
12380b57cec5SDimitry Andric 
1239fe6060f1SDimitry Andric #define LLDB_OPTIONS_process_save_core
1240fe6060f1SDimitry Andric #include "CommandOptions.inc"
1241fe6060f1SDimitry Andric 
12420b57cec5SDimitry Andric class CommandObjectProcessSaveCore : public CommandObjectParsed {
12430b57cec5SDimitry Andric public:
12440b57cec5SDimitry Andric   CommandObjectProcessSaveCore(CommandInterpreter &interpreter)
1245349cc55cSDimitry Andric       : CommandObjectParsed(
1246349cc55cSDimitry Andric             interpreter, "process save-core",
12470b57cec5SDimitry Andric             "Save the current process as a core file using an "
12480b57cec5SDimitry Andric             "appropriate file type.",
1249349cc55cSDimitry Andric             "process save-core [-s corefile-style -p plugin-name] FILE",
12500b57cec5SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
125181ad6265SDimitry Andric                 eCommandProcessMustBeLaunched) {
1252*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypePath);
125381ad6265SDimitry Andric   }
12540b57cec5SDimitry Andric 
12550b57cec5SDimitry Andric   ~CommandObjectProcessSaveCore() override = default;
12560b57cec5SDimitry Andric 
1257fe6060f1SDimitry Andric   Options *GetOptions() override { return &m_options; }
1258fe6060f1SDimitry Andric 
1259fe6060f1SDimitry Andric   class CommandOptions : public Options {
1260fe6060f1SDimitry Andric   public:
126181ad6265SDimitry Andric     CommandOptions() = default;
1262fe6060f1SDimitry Andric 
1263fe6060f1SDimitry Andric     ~CommandOptions() override = default;
1264fe6060f1SDimitry Andric 
1265fe6060f1SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1266bdd1243dSDimitry Andric       return llvm::ArrayRef(g_process_save_core_options);
1267fe6060f1SDimitry Andric     }
1268fe6060f1SDimitry Andric 
1269fe6060f1SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1270fe6060f1SDimitry Andric                           ExecutionContext *execution_context) override {
1271fe6060f1SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
1272fe6060f1SDimitry Andric       Status error;
1273fe6060f1SDimitry Andric 
1274fe6060f1SDimitry Andric       switch (short_option) {
1275349cc55cSDimitry Andric       case 'p':
1276*0fca6ea1SDimitry Andric         error = m_core_dump_options.SetPluginName(option_arg.data());
1277349cc55cSDimitry Andric         break;
1278fe6060f1SDimitry Andric       case 's':
1279*0fca6ea1SDimitry Andric         m_core_dump_options.SetStyle(
1280fe6060f1SDimitry Andric             (lldb::SaveCoreStyle)OptionArgParser::ToOptionEnum(
1281fe6060f1SDimitry Andric                 option_arg, GetDefinitions()[option_idx].enum_values,
1282*0fca6ea1SDimitry Andric                 eSaveCoreUnspecified, error));
1283fe6060f1SDimitry Andric         break;
1284fe6060f1SDimitry Andric       default:
1285fe6060f1SDimitry Andric         llvm_unreachable("Unimplemented option");
1286fe6060f1SDimitry Andric       }
1287fe6060f1SDimitry Andric 
1288fe6060f1SDimitry Andric       return {};
1289fe6060f1SDimitry Andric     }
1290fe6060f1SDimitry Andric 
1291fe6060f1SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
1292*0fca6ea1SDimitry Andric       m_core_dump_options.Clear();
1293fe6060f1SDimitry Andric     }
1294fe6060f1SDimitry Andric 
1295fe6060f1SDimitry Andric     // Instance variables to hold the values for command options.
1296*0fca6ea1SDimitry Andric     SaveCoreOptions m_core_dump_options;
1297fe6060f1SDimitry Andric   };
1298fe6060f1SDimitry Andric 
12990b57cec5SDimitry Andric protected:
13005f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
13010b57cec5SDimitry Andric     ProcessSP process_sp = m_exe_ctx.GetProcessSP();
13020b57cec5SDimitry Andric     if (process_sp) {
13030b57cec5SDimitry Andric       if (command.GetArgumentCount() == 1) {
13040b57cec5SDimitry Andric         FileSpec output_file(command.GetArgumentAtIndex(0));
130506c3fb27SDimitry Andric         FileSystem::Instance().Resolve(output_file);
1306*0fca6ea1SDimitry Andric         auto &core_dump_options = m_options.m_core_dump_options;
1307*0fca6ea1SDimitry Andric         core_dump_options.SetOutputFile(output_file);
1308*0fca6ea1SDimitry Andric         Status error = PluginManager::SaveCore(process_sp, core_dump_options);
13090b57cec5SDimitry Andric         if (error.Success()) {
1310*0fca6ea1SDimitry Andric           if (core_dump_options.GetStyle() ==
1311*0fca6ea1SDimitry Andric                   SaveCoreStyle::eSaveCoreDirtyOnly ||
1312*0fca6ea1SDimitry Andric               core_dump_options.GetStyle() ==
1313*0fca6ea1SDimitry Andric                   SaveCoreStyle::eSaveCoreStackOnly) {
1314fe6060f1SDimitry Andric             result.AppendMessageWithFormat(
1315349cc55cSDimitry Andric                 "\nModified-memory or stack-memory only corefile "
1316349cc55cSDimitry Andric                 "created.  This corefile may \n"
1317349cc55cSDimitry Andric                 "not show library/framework/app binaries "
1318fe6060f1SDimitry Andric                 "on a different system, or when \n"
1319fe6060f1SDimitry Andric                 "those binaries have "
1320fe6060f1SDimitry Andric                 "been updated/modified. Copies are not included\n"
1321fe6060f1SDimitry Andric                 "in this corefile.  Use --style full to include all "
1322fe6060f1SDimitry Andric                 "process memory.\n");
1323fe6060f1SDimitry Andric           }
13240b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessFinishResult);
13250b57cec5SDimitry Andric         } else {
13260b57cec5SDimitry Andric           result.AppendErrorWithFormat(
13270b57cec5SDimitry Andric               "Failed to save core file for process: %s\n", error.AsCString());
13280b57cec5SDimitry Andric         }
13290b57cec5SDimitry Andric       } else {
13300b57cec5SDimitry Andric         result.AppendErrorWithFormat("'%s' takes one arguments:\nUsage: %s\n",
13310b57cec5SDimitry Andric                                      m_cmd_name.c_str(), m_cmd_syntax.c_str());
13320b57cec5SDimitry Andric       }
13330b57cec5SDimitry Andric     } else {
13340b57cec5SDimitry Andric       result.AppendError("invalid process");
13350b57cec5SDimitry Andric     }
13360b57cec5SDimitry Andric   }
1337fe6060f1SDimitry Andric 
1338fe6060f1SDimitry Andric   CommandOptions m_options;
13390b57cec5SDimitry Andric };
13400b57cec5SDimitry Andric 
13410b57cec5SDimitry Andric // CommandObjectProcessStatus
13420b57cec5SDimitry Andric #pragma mark CommandObjectProcessStatus
13435ffd83dbSDimitry Andric #define LLDB_OPTIONS_process_status
13445ffd83dbSDimitry Andric #include "CommandOptions.inc"
13450b57cec5SDimitry Andric 
13460b57cec5SDimitry Andric class CommandObjectProcessStatus : public CommandObjectParsed {
13470b57cec5SDimitry Andric public:
13480b57cec5SDimitry Andric   CommandObjectProcessStatus(CommandInterpreter &interpreter)
13490b57cec5SDimitry Andric       : CommandObjectParsed(
13500b57cec5SDimitry Andric             interpreter, "process status",
13510b57cec5SDimitry Andric             "Show status and stop location for the current target process.",
13520b57cec5SDimitry Andric             "process status",
135304eeddc0SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock) {}
13540b57cec5SDimitry Andric 
13550b57cec5SDimitry Andric   ~CommandObjectProcessStatus() override = default;
13560b57cec5SDimitry Andric 
13575ffd83dbSDimitry Andric   Options *GetOptions() override { return &m_options; }
13585ffd83dbSDimitry Andric 
13595ffd83dbSDimitry Andric   class CommandOptions : public Options {
13605ffd83dbSDimitry Andric   public:
136181ad6265SDimitry Andric     CommandOptions() = default;
13625ffd83dbSDimitry Andric 
13635ffd83dbSDimitry Andric     ~CommandOptions() override = default;
13645ffd83dbSDimitry Andric 
13655ffd83dbSDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
13665ffd83dbSDimitry Andric                           ExecutionContext *execution_context) override {
13675ffd83dbSDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
13685ffd83dbSDimitry Andric 
13695ffd83dbSDimitry Andric       switch (short_option) {
13705ffd83dbSDimitry Andric       case 'v':
13715ffd83dbSDimitry Andric         m_verbose = true;
13725ffd83dbSDimitry Andric         break;
13735ffd83dbSDimitry Andric       default:
13745ffd83dbSDimitry Andric         llvm_unreachable("Unimplemented option");
13755ffd83dbSDimitry Andric       }
13765ffd83dbSDimitry Andric 
13775ffd83dbSDimitry Andric       return {};
13785ffd83dbSDimitry Andric     }
13795ffd83dbSDimitry Andric 
13805ffd83dbSDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
13815ffd83dbSDimitry Andric       m_verbose = false;
13825ffd83dbSDimitry Andric     }
13835ffd83dbSDimitry Andric 
13845ffd83dbSDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1385bdd1243dSDimitry Andric       return llvm::ArrayRef(g_process_status_options);
13865ffd83dbSDimitry Andric     }
13875ffd83dbSDimitry Andric 
13885ffd83dbSDimitry Andric     // Instance variables to hold the values for command options.
1389fe6060f1SDimitry Andric     bool m_verbose = false;
13905ffd83dbSDimitry Andric   };
13915ffd83dbSDimitry Andric 
13925ffd83dbSDimitry Andric protected:
13935f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
13940b57cec5SDimitry Andric     Stream &strm = result.GetOutputStream();
13950b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishNoResult);
13965ffd83dbSDimitry Andric 
13970b57cec5SDimitry Andric     // No need to check "process" for validity as eCommandRequiresProcess
13980b57cec5SDimitry Andric     // ensures it is valid
13990b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
14000b57cec5SDimitry Andric     const bool only_threads_with_stop_reason = true;
14010b57cec5SDimitry Andric     const uint32_t start_frame = 0;
14020b57cec5SDimitry Andric     const uint32_t num_frames = 1;
14030b57cec5SDimitry Andric     const uint32_t num_frames_with_source = 1;
14040b57cec5SDimitry Andric     const bool stop_format = true;
14050b57cec5SDimitry Andric     process->GetStatus(strm);
14060b57cec5SDimitry Andric     process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame,
14070b57cec5SDimitry Andric                              num_frames, num_frames_with_source, stop_format);
14085ffd83dbSDimitry Andric 
14095ffd83dbSDimitry Andric     if (m_options.m_verbose) {
1410fe6060f1SDimitry Andric       addr_t code_mask = process->GetCodeAddressMask();
1411fe6060f1SDimitry Andric       addr_t data_mask = process->GetDataAddressMask();
1412*0fca6ea1SDimitry Andric       if (code_mask != LLDB_INVALID_ADDRESS_MASK) {
1413fe6060f1SDimitry Andric         int bits = std::bitset<64>(~code_mask).count();
1414fe6060f1SDimitry Andric         result.AppendMessageWithFormat(
1415fe6060f1SDimitry Andric             "Addressable code address mask: 0x%" PRIx64 "\n", code_mask);
1416fe6060f1SDimitry Andric         result.AppendMessageWithFormat(
1417fe6060f1SDimitry Andric             "Addressable data address mask: 0x%" PRIx64 "\n", data_mask);
1418fe6060f1SDimitry Andric         result.AppendMessageWithFormat(
1419fe6060f1SDimitry Andric             "Number of bits used in addressing (code): %d\n", bits);
1420fe6060f1SDimitry Andric       }
1421fe6060f1SDimitry Andric 
14225ffd83dbSDimitry Andric       PlatformSP platform_sp = process->GetTarget().GetPlatform();
14235ffd83dbSDimitry Andric       if (!platform_sp) {
14245ffd83dbSDimitry Andric         result.AppendError("Couldn'retrieve the target's platform");
14255f757f3fSDimitry Andric         return;
14260b57cec5SDimitry Andric       }
14275ffd83dbSDimitry Andric 
14285ffd83dbSDimitry Andric       auto expected_crash_info =
14295ffd83dbSDimitry Andric           platform_sp->FetchExtendedCrashInformation(*process);
14305ffd83dbSDimitry Andric 
14315ffd83dbSDimitry Andric       if (!expected_crash_info) {
14325ffd83dbSDimitry Andric         result.AppendError(llvm::toString(expected_crash_info.takeError()));
14335f757f3fSDimitry Andric         return;
14345ffd83dbSDimitry Andric       }
14355ffd83dbSDimitry Andric 
14365ffd83dbSDimitry Andric       StructuredData::DictionarySP crash_info_sp = *expected_crash_info;
14375ffd83dbSDimitry Andric 
14385ffd83dbSDimitry Andric       if (crash_info_sp) {
1439bdd1243dSDimitry Andric         strm.EOL();
14405ffd83dbSDimitry Andric         strm.PutCString("Extended Crash Information:\n");
1441bdd1243dSDimitry Andric         crash_info_sp->GetDescription(strm);
14425ffd83dbSDimitry Andric       }
14435ffd83dbSDimitry Andric     }
14445ffd83dbSDimitry Andric   }
14455ffd83dbSDimitry Andric 
14465ffd83dbSDimitry Andric private:
14475ffd83dbSDimitry Andric   CommandOptions m_options;
14480b57cec5SDimitry Andric };
14490b57cec5SDimitry Andric 
14500b57cec5SDimitry Andric // CommandObjectProcessHandle
14519dba64beSDimitry Andric #define LLDB_OPTIONS_process_handle
14529dba64beSDimitry Andric #include "CommandOptions.inc"
14530b57cec5SDimitry Andric 
14540b57cec5SDimitry Andric #pragma mark CommandObjectProcessHandle
14550b57cec5SDimitry Andric 
14560b57cec5SDimitry Andric class CommandObjectProcessHandle : public CommandObjectParsed {
14570b57cec5SDimitry Andric public:
14580b57cec5SDimitry Andric   class CommandOptions : public Options {
14590b57cec5SDimitry Andric   public:
146004eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
14610b57cec5SDimitry Andric 
14620b57cec5SDimitry Andric     ~CommandOptions() override = default;
14630b57cec5SDimitry Andric 
14640b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
14650b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
14660b57cec5SDimitry Andric       Status error;
14670b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
14680b57cec5SDimitry Andric 
14690b57cec5SDimitry Andric       switch (short_option) {
147081ad6265SDimitry Andric       case 'c':
147181ad6265SDimitry Andric         do_clear = true;
147281ad6265SDimitry Andric         break;
147381ad6265SDimitry Andric       case 'd':
147481ad6265SDimitry Andric         dummy = true;
147581ad6265SDimitry Andric         break;
14760b57cec5SDimitry Andric       case 's':
14775ffd83dbSDimitry Andric         stop = std::string(option_arg);
14780b57cec5SDimitry Andric         break;
14790b57cec5SDimitry Andric       case 'n':
14805ffd83dbSDimitry Andric         notify = std::string(option_arg);
14810b57cec5SDimitry Andric         break;
14820b57cec5SDimitry Andric       case 'p':
14835ffd83dbSDimitry Andric         pass = std::string(option_arg);
14840b57cec5SDimitry Andric         break;
148581ad6265SDimitry Andric       case 't':
148681ad6265SDimitry Andric         only_target_values = true;
148781ad6265SDimitry Andric         break;
14880b57cec5SDimitry Andric       default:
14899dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
14900b57cec5SDimitry Andric       }
14910b57cec5SDimitry Andric       return error;
14920b57cec5SDimitry Andric     }
14930b57cec5SDimitry Andric 
14940b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
14950b57cec5SDimitry Andric       stop.clear();
14960b57cec5SDimitry Andric       notify.clear();
14970b57cec5SDimitry Andric       pass.clear();
149881ad6265SDimitry Andric       only_target_values = false;
149981ad6265SDimitry Andric       do_clear = false;
150081ad6265SDimitry Andric       dummy = false;
15010b57cec5SDimitry Andric     }
15020b57cec5SDimitry Andric 
15030b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1504bdd1243dSDimitry Andric       return llvm::ArrayRef(g_process_handle_options);
15050b57cec5SDimitry Andric     }
15060b57cec5SDimitry Andric 
15070b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
15080b57cec5SDimitry Andric 
15090b57cec5SDimitry Andric     std::string stop;
15100b57cec5SDimitry Andric     std::string notify;
15110b57cec5SDimitry Andric     std::string pass;
151281ad6265SDimitry Andric     bool only_target_values = false;
151381ad6265SDimitry Andric     bool do_clear = false;
151481ad6265SDimitry Andric     bool dummy = false;
15150b57cec5SDimitry Andric   };
15160b57cec5SDimitry Andric 
15170b57cec5SDimitry Andric   CommandObjectProcessHandle(CommandInterpreter &interpreter)
15180b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "process handle",
15190b57cec5SDimitry Andric                             "Manage LLDB handling of OS signals for the "
15200b57cec5SDimitry Andric                             "current target process.  Defaults to showing "
15210b57cec5SDimitry Andric                             "current policy.",
152281ad6265SDimitry Andric                             nullptr) {
152381ad6265SDimitry Andric     SetHelpLong("\nIf no signals are specified but one or more actions are, "
152481ad6265SDimitry Andric                 "and there is a live process, update them all.  If no action "
152581ad6265SDimitry Andric                 "is specified, list the current values.\n"
152681ad6265SDimitry Andric                 "If you specify actions with no target (e.g. in an init file) "
152781ad6265SDimitry Andric                 "or in a target with no process "
152881ad6265SDimitry Andric                 "the values will get copied into subsequent targets, but "
152981ad6265SDimitry Andric                 "lldb won't be able to spell-check the options since it can't "
153081ad6265SDimitry Andric                 "know which signal set will later be in force."
153181ad6265SDimitry Andric                 "\nYou can see the signal modifications held by the target"
153281ad6265SDimitry Andric                 "by passing the -t option."
153381ad6265SDimitry Andric                 "\nYou can also clear the target modification for a signal"
153481ad6265SDimitry Andric                 "by passing the -c option");
1535*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypeUnixSignal, eArgRepeatStar);
15360b57cec5SDimitry Andric   }
15370b57cec5SDimitry Andric 
15380b57cec5SDimitry Andric   ~CommandObjectProcessHandle() override = default;
15390b57cec5SDimitry Andric 
15400b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
15410b57cec5SDimitry Andric 
15420b57cec5SDimitry Andric   void PrintSignalHeader(Stream &str) {
15430b57cec5SDimitry Andric     str.Printf("NAME         PASS   STOP   NOTIFY\n");
15440b57cec5SDimitry Andric     str.Printf("===========  =====  =====  ======\n");
15450b57cec5SDimitry Andric   }
15460b57cec5SDimitry Andric 
15475f757f3fSDimitry Andric   void PrintSignal(Stream &str, int32_t signo, llvm::StringRef sig_name,
15480b57cec5SDimitry Andric                    const UnixSignalsSP &signals_sp) {
15490b57cec5SDimitry Andric     bool stop;
15500b57cec5SDimitry Andric     bool suppress;
15510b57cec5SDimitry Andric     bool notify;
15520b57cec5SDimitry Andric 
15535f757f3fSDimitry Andric     str.Format("{0, -11}  ", sig_name);
15540b57cec5SDimitry Andric     if (signals_sp->GetSignalInfo(signo, suppress, stop, notify)) {
15550b57cec5SDimitry Andric       bool pass = !suppress;
15560b57cec5SDimitry Andric       str.Printf("%s  %s  %s", (pass ? "true " : "false"),
15570b57cec5SDimitry Andric                  (stop ? "true " : "false"), (notify ? "true " : "false"));
15580b57cec5SDimitry Andric     }
15590b57cec5SDimitry Andric     str.Printf("\n");
15600b57cec5SDimitry Andric   }
15610b57cec5SDimitry Andric 
15620b57cec5SDimitry Andric   void PrintSignalInformation(Stream &str, Args &signal_args,
15630b57cec5SDimitry Andric                               int num_valid_signals,
15640b57cec5SDimitry Andric                               const UnixSignalsSP &signals_sp) {
15650b57cec5SDimitry Andric     PrintSignalHeader(str);
15660b57cec5SDimitry Andric 
15670b57cec5SDimitry Andric     if (num_valid_signals > 0) {
15680b57cec5SDimitry Andric       size_t num_args = signal_args.GetArgumentCount();
15690b57cec5SDimitry Andric       for (size_t i = 0; i < num_args; ++i) {
15700b57cec5SDimitry Andric         int32_t signo = signals_sp->GetSignalNumberFromName(
15710b57cec5SDimitry Andric             signal_args.GetArgumentAtIndex(i));
15720b57cec5SDimitry Andric         if (signo != LLDB_INVALID_SIGNAL_NUMBER)
15730b57cec5SDimitry Andric           PrintSignal(str, signo, signal_args.GetArgumentAtIndex(i),
15740b57cec5SDimitry Andric                       signals_sp);
15750b57cec5SDimitry Andric       }
15760b57cec5SDimitry Andric     } else // Print info for ALL signals
15770b57cec5SDimitry Andric     {
15780b57cec5SDimitry Andric       int32_t signo = signals_sp->GetFirstSignalNumber();
15790b57cec5SDimitry Andric       while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
15805f757f3fSDimitry Andric         PrintSignal(str, signo, signals_sp->GetSignalAsStringRef(signo),
15810b57cec5SDimitry Andric                     signals_sp);
15820b57cec5SDimitry Andric         signo = signals_sp->GetNextSignalNumber(signo);
15830b57cec5SDimitry Andric       }
15840b57cec5SDimitry Andric     }
15850b57cec5SDimitry Andric   }
15860b57cec5SDimitry Andric 
15870b57cec5SDimitry Andric protected:
15885f757f3fSDimitry Andric   void DoExecute(Args &signal_args, CommandReturnObject &result) override {
158981ad6265SDimitry Andric     Target &target = GetSelectedOrDummyTarget();
15900b57cec5SDimitry Andric 
159181ad6265SDimitry Andric     // Any signals that are being set should be added to the Target's
159281ad6265SDimitry Andric     // DummySignals so they will get applied on rerun, etc.
159381ad6265SDimitry Andric     // If we have a process, however, we can do a more accurate job of vetting
159481ad6265SDimitry Andric     // the user's options.
159581ad6265SDimitry Andric     ProcessSP process_sp = target.GetProcessSP();
15960b57cec5SDimitry Andric 
1597*0fca6ea1SDimitry Andric     std::optional<bool> stop_action = {};
1598*0fca6ea1SDimitry Andric     std::optional<bool> pass_action = {};
1599*0fca6ea1SDimitry Andric     std::optional<bool> notify_action = {};
16000b57cec5SDimitry Andric 
1601*0fca6ea1SDimitry Andric     if (!m_options.stop.empty()) {
1602*0fca6ea1SDimitry Andric       bool success = false;
1603*0fca6ea1SDimitry Andric       bool value = OptionArgParser::ToBoolean(m_options.stop, false, &success);
1604*0fca6ea1SDimitry Andric       if (!success) {
1605*0fca6ea1SDimitry Andric         result.AppendError(
1606*0fca6ea1SDimitry Andric             "Invalid argument for command option --stop; must be "
16070b57cec5SDimitry Andric             "true or false.\n");
16085f757f3fSDimitry Andric         return;
16090b57cec5SDimitry Andric       }
16100b57cec5SDimitry Andric 
1611*0fca6ea1SDimitry Andric       stop_action = value;
1612*0fca6ea1SDimitry Andric     }
1613*0fca6ea1SDimitry Andric 
1614*0fca6ea1SDimitry Andric     if (!m_options.pass.empty()) {
1615*0fca6ea1SDimitry Andric       bool success = false;
1616*0fca6ea1SDimitry Andric       bool value = OptionArgParser::ToBoolean(m_options.pass, false, &success);
1617*0fca6ea1SDimitry Andric       if (!success) {
1618*0fca6ea1SDimitry Andric         result.AppendError(
1619*0fca6ea1SDimitry Andric             "Invalid argument for command option --pass; must be "
1620*0fca6ea1SDimitry Andric             "true or false.\n");
1621*0fca6ea1SDimitry Andric         return;
1622*0fca6ea1SDimitry Andric       }
1623*0fca6ea1SDimitry Andric       pass_action = value;
1624*0fca6ea1SDimitry Andric     }
1625*0fca6ea1SDimitry Andric 
1626*0fca6ea1SDimitry Andric     if (!m_options.notify.empty()) {
1627*0fca6ea1SDimitry Andric       bool success = false;
1628*0fca6ea1SDimitry Andric       bool value =
1629*0fca6ea1SDimitry Andric           OptionArgParser::ToBoolean(m_options.notify, false, &success);
1630*0fca6ea1SDimitry Andric       if (!success) {
16310b57cec5SDimitry Andric         result.AppendError("Invalid argument for command option --notify; must "
16320b57cec5SDimitry Andric                            "be true or false.\n");
16335f757f3fSDimitry Andric         return;
16340b57cec5SDimitry Andric       }
1635*0fca6ea1SDimitry Andric       notify_action = value;
16360b57cec5SDimitry Andric     }
16370b57cec5SDimitry Andric 
1638*0fca6ea1SDimitry Andric     if (!m_options.notify.empty() && !notify_action.has_value()) {
1639*0fca6ea1SDimitry Andric     }
1640*0fca6ea1SDimitry Andric 
1641*0fca6ea1SDimitry Andric     bool no_actions = (!stop_action.has_value() && !pass_action.has_value() &&
1642*0fca6ea1SDimitry Andric                        !notify_action.has_value());
164381ad6265SDimitry Andric     if (m_options.only_target_values && !no_actions) {
164481ad6265SDimitry Andric       result.AppendError("-t is for reporting, not setting, target values.");
16455f757f3fSDimitry Andric       return;
164681ad6265SDimitry Andric     }
164781ad6265SDimitry Andric 
16480b57cec5SDimitry Andric     size_t num_args = signal_args.GetArgumentCount();
164981ad6265SDimitry Andric     UnixSignalsSP signals_sp;
165081ad6265SDimitry Andric     if (process_sp)
165181ad6265SDimitry Andric       signals_sp = process_sp->GetUnixSignals();
165281ad6265SDimitry Andric 
16530b57cec5SDimitry Andric     int num_signals_set = 0;
16540b57cec5SDimitry Andric 
165581ad6265SDimitry Andric     // If we were just asked to print the target values, do that here and
165681ad6265SDimitry Andric     // return:
165781ad6265SDimitry Andric     if (m_options.only_target_values) {
165881ad6265SDimitry Andric       target.PrintDummySignals(result.GetOutputStream(), signal_args);
165981ad6265SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
16605f757f3fSDimitry Andric       return;
166181ad6265SDimitry Andric     }
166281ad6265SDimitry Andric 
166381ad6265SDimitry Andric     // This handles clearing values:
166481ad6265SDimitry Andric     if (m_options.do_clear) {
166581ad6265SDimitry Andric       target.ClearDummySignals(signal_args);
166681ad6265SDimitry Andric       if (m_options.dummy)
166781ad6265SDimitry Andric         GetDummyTarget().ClearDummySignals(signal_args);
166881ad6265SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
16695f757f3fSDimitry Andric       return;
167081ad6265SDimitry Andric     }
167181ad6265SDimitry Andric 
167281ad6265SDimitry Andric     // This rest handles setting values:
16730b57cec5SDimitry Andric     if (num_args > 0) {
16740b57cec5SDimitry Andric       for (const auto &arg : signal_args) {
167581ad6265SDimitry Andric         // Do the process first.  If we have a process we can catch
167681ad6265SDimitry Andric         // invalid signal names, which we do here.
167781ad6265SDimitry Andric         if (signals_sp) {
16780b57cec5SDimitry Andric           int32_t signo = signals_sp->GetSignalNumberFromName(arg.c_str());
16790b57cec5SDimitry Andric           if (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1680*0fca6ea1SDimitry Andric             if (stop_action.has_value())
1681*0fca6ea1SDimitry Andric               signals_sp->SetShouldStop(signo, *stop_action);
1682*0fca6ea1SDimitry Andric             if (pass_action.has_value()) {
1683*0fca6ea1SDimitry Andric               bool suppress = !*pass_action;
16840b57cec5SDimitry Andric               signals_sp->SetShouldSuppress(signo, suppress);
16850b57cec5SDimitry Andric             }
1686*0fca6ea1SDimitry Andric             if (notify_action.has_value())
1687*0fca6ea1SDimitry Andric               signals_sp->SetShouldNotify(signo, *notify_action);
16880b57cec5SDimitry Andric             ++num_signals_set;
16890b57cec5SDimitry Andric           } else {
16900b57cec5SDimitry Andric             result.AppendErrorWithFormat("Invalid signal name '%s'\n",
16910b57cec5SDimitry Andric                                           arg.c_str());
169281ad6265SDimitry Andric             continue;
16930b57cec5SDimitry Andric           }
169481ad6265SDimitry Andric         } else {
169581ad6265SDimitry Andric           // If there's no process we can't check, so we just set them all.
169681ad6265SDimitry Andric           // But since the map signal name -> signal number across all platforms
169781ad6265SDimitry Andric           // is not 1-1, we can't sensibly set signal actions by number before
169881ad6265SDimitry Andric           // we have a process.  Check that here:
169981ad6265SDimitry Andric           int32_t signo;
170081ad6265SDimitry Andric           if (llvm::to_integer(arg.c_str(), signo)) {
170181ad6265SDimitry Andric             result.AppendErrorWithFormat("Can't set signal handling by signal "
170281ad6265SDimitry Andric                                          "number with no process");
17035f757f3fSDimitry Andric             return;
170481ad6265SDimitry Andric           }
170581ad6265SDimitry Andric          num_signals_set = num_args;
170681ad6265SDimitry Andric         }
1707*0fca6ea1SDimitry Andric         auto set_lazy_bool = [](std::optional<bool> action) -> LazyBool {
1708*0fca6ea1SDimitry Andric           if (!action.has_value())
1709*0fca6ea1SDimitry Andric             return eLazyBoolCalculate;
1710*0fca6ea1SDimitry Andric           return (*action) ? eLazyBoolYes : eLazyBoolNo;
171181ad6265SDimitry Andric         };
171281ad6265SDimitry Andric 
171381ad6265SDimitry Andric         // If there were no actions, we're just listing, don't add the dummy:
171481ad6265SDimitry Andric         if (!no_actions)
1715*0fca6ea1SDimitry Andric           target.AddDummySignal(arg.ref(), set_lazy_bool(pass_action),
171681ad6265SDimitry Andric                                 set_lazy_bool(notify_action),
171781ad6265SDimitry Andric                                 set_lazy_bool(stop_action));
17180b57cec5SDimitry Andric       }
17190b57cec5SDimitry Andric     } else {
17200b57cec5SDimitry Andric       // No signal specified, if any command options were specified, update ALL
172181ad6265SDimitry Andric       // signals.  But we can't do this without a process since we don't know
172281ad6265SDimitry Andric       // all the possible signals that might be valid for this target.
1723*0fca6ea1SDimitry Andric       if ((notify_action.has_value() || stop_action.has_value() ||
1724*0fca6ea1SDimitry Andric            pass_action.has_value()) &&
1725*0fca6ea1SDimitry Andric           process_sp) {
17260b57cec5SDimitry Andric         if (m_interpreter.Confirm(
17270b57cec5SDimitry Andric                 "Do you really want to update all the signals?", false)) {
17280b57cec5SDimitry Andric           int32_t signo = signals_sp->GetFirstSignalNumber();
17290b57cec5SDimitry Andric           while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1730*0fca6ea1SDimitry Andric             if (notify_action.has_value())
1731*0fca6ea1SDimitry Andric               signals_sp->SetShouldNotify(signo, *notify_action);
1732*0fca6ea1SDimitry Andric             if (stop_action.has_value())
1733*0fca6ea1SDimitry Andric               signals_sp->SetShouldStop(signo, *stop_action);
1734*0fca6ea1SDimitry Andric             if (pass_action.has_value()) {
1735*0fca6ea1SDimitry Andric               bool suppress = !*pass_action;
17360b57cec5SDimitry Andric               signals_sp->SetShouldSuppress(signo, suppress);
17370b57cec5SDimitry Andric             }
17380b57cec5SDimitry Andric             signo = signals_sp->GetNextSignalNumber(signo);
17390b57cec5SDimitry Andric           }
17400b57cec5SDimitry Andric         }
17410b57cec5SDimitry Andric       }
17420b57cec5SDimitry Andric     }
17430b57cec5SDimitry Andric 
174481ad6265SDimitry Andric     if (signals_sp)
17450b57cec5SDimitry Andric       PrintSignalInformation(result.GetOutputStream(), signal_args,
17460b57cec5SDimitry Andric                              num_signals_set, signals_sp);
174781ad6265SDimitry Andric     else
174881ad6265SDimitry Andric       target.PrintDummySignals(result.GetOutputStream(),
174981ad6265SDimitry Andric           signal_args);
17500b57cec5SDimitry Andric 
17510b57cec5SDimitry Andric     if (num_signals_set > 0)
175281ad6265SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
17530b57cec5SDimitry Andric     else
17540b57cec5SDimitry Andric       result.SetStatus(eReturnStatusFailed);
17550b57cec5SDimitry Andric   }
17560b57cec5SDimitry Andric 
17570b57cec5SDimitry Andric   CommandOptions m_options;
17580b57cec5SDimitry Andric };
17590b57cec5SDimitry Andric 
1760fe6060f1SDimitry Andric // Next are the subcommands of CommandObjectMultiwordProcessTrace
1761fe6060f1SDimitry Andric 
1762fe6060f1SDimitry Andric // CommandObjectProcessTraceStart
1763fe6060f1SDimitry Andric class CommandObjectProcessTraceStart : public CommandObjectTraceProxy {
1764fe6060f1SDimitry Andric public:
1765fe6060f1SDimitry Andric   CommandObjectProcessTraceStart(CommandInterpreter &interpreter)
1766fe6060f1SDimitry Andric       : CommandObjectTraceProxy(
1767fe6060f1SDimitry Andric             /*live_debug_session_only*/ true, interpreter,
1768fe6060f1SDimitry Andric             "process trace start",
1769fe6060f1SDimitry Andric             "Start tracing this process with the corresponding trace "
1770fe6060f1SDimitry Andric             "plug-in.",
1771fe6060f1SDimitry Andric             "process trace start [<trace-options>]") {}
1772fe6060f1SDimitry Andric 
1773fe6060f1SDimitry Andric protected:
1774fe6060f1SDimitry Andric   lldb::CommandObjectSP GetDelegateCommand(Trace &trace) override {
1775fe6060f1SDimitry Andric     return trace.GetProcessTraceStartCommand(m_interpreter);
1776fe6060f1SDimitry Andric   }
1777fe6060f1SDimitry Andric };
1778fe6060f1SDimitry Andric 
1779fe6060f1SDimitry Andric // CommandObjectProcessTraceStop
1780fe6060f1SDimitry Andric class CommandObjectProcessTraceStop : public CommandObjectParsed {
1781fe6060f1SDimitry Andric public:
1782fe6060f1SDimitry Andric   CommandObjectProcessTraceStop(CommandInterpreter &interpreter)
1783fe6060f1SDimitry Andric       : CommandObjectParsed(interpreter, "process trace stop",
1784fe6060f1SDimitry Andric                             "Stop tracing this process. This does not affect "
1785fe6060f1SDimitry Andric                             "traces started with the "
1786fe6060f1SDimitry Andric                             "\"thread trace start\" command.",
1787fe6060f1SDimitry Andric                             "process trace stop",
1788fe6060f1SDimitry Andric                             eCommandRequiresProcess | eCommandTryTargetAPILock |
1789fe6060f1SDimitry Andric                                 eCommandProcessMustBeLaunched |
1790fe6060f1SDimitry Andric                                 eCommandProcessMustBePaused |
1791fe6060f1SDimitry Andric                                 eCommandProcessMustBeTraced) {}
1792fe6060f1SDimitry Andric 
1793fe6060f1SDimitry Andric   ~CommandObjectProcessTraceStop() override = default;
1794fe6060f1SDimitry Andric 
17955f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
1796fe6060f1SDimitry Andric     ProcessSP process_sp = m_exe_ctx.GetProcessSP();
1797fe6060f1SDimitry Andric 
1798fe6060f1SDimitry Andric     TraceSP trace_sp = process_sp->GetTarget().GetTrace();
1799fe6060f1SDimitry Andric 
1800fe6060f1SDimitry Andric     if (llvm::Error err = trace_sp->Stop())
1801fe6060f1SDimitry Andric       result.AppendError(toString(std::move(err)));
1802fe6060f1SDimitry Andric     else
1803fe6060f1SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
1804fe6060f1SDimitry Andric   }
1805fe6060f1SDimitry Andric };
1806fe6060f1SDimitry Andric 
1807fe6060f1SDimitry Andric // CommandObjectMultiwordProcessTrace
1808fe6060f1SDimitry Andric class CommandObjectMultiwordProcessTrace : public CommandObjectMultiword {
1809fe6060f1SDimitry Andric public:
1810fe6060f1SDimitry Andric   CommandObjectMultiwordProcessTrace(CommandInterpreter &interpreter)
1811fe6060f1SDimitry Andric       : CommandObjectMultiword(
1812fe6060f1SDimitry Andric             interpreter, "trace", "Commands for tracing the current process.",
1813fe6060f1SDimitry Andric             "process trace <subcommand> [<subcommand objects>]") {
1814fe6060f1SDimitry Andric     LoadSubCommand("start", CommandObjectSP(new CommandObjectProcessTraceStart(
1815fe6060f1SDimitry Andric                                 interpreter)));
1816fe6060f1SDimitry Andric     LoadSubCommand("stop", CommandObjectSP(
1817fe6060f1SDimitry Andric                                new CommandObjectProcessTraceStop(interpreter)));
1818fe6060f1SDimitry Andric   }
1819fe6060f1SDimitry Andric 
1820fe6060f1SDimitry Andric   ~CommandObjectMultiwordProcessTrace() override = default;
1821fe6060f1SDimitry Andric };
1822fe6060f1SDimitry Andric 
18230b57cec5SDimitry Andric // CommandObjectMultiwordProcess
18240b57cec5SDimitry Andric 
18250b57cec5SDimitry Andric CommandObjectMultiwordProcess::CommandObjectMultiwordProcess(
18260b57cec5SDimitry Andric     CommandInterpreter &interpreter)
18270b57cec5SDimitry Andric     : CommandObjectMultiword(
18280b57cec5SDimitry Andric           interpreter, "process",
18290b57cec5SDimitry Andric           "Commands for interacting with processes on the current platform.",
18300b57cec5SDimitry Andric           "process <subcommand> [<subcommand-options>]") {
18310b57cec5SDimitry Andric   LoadSubCommand("attach",
18320b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessAttach(interpreter)));
18330b57cec5SDimitry Andric   LoadSubCommand("launch",
18340b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessLaunch(interpreter)));
18350b57cec5SDimitry Andric   LoadSubCommand("continue", CommandObjectSP(new CommandObjectProcessContinue(
18360b57cec5SDimitry Andric                                  interpreter)));
18370b57cec5SDimitry Andric   LoadSubCommand("connect",
18380b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessConnect(interpreter)));
18390b57cec5SDimitry Andric   LoadSubCommand("detach",
18400b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessDetach(interpreter)));
18410b57cec5SDimitry Andric   LoadSubCommand("load",
18420b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessLoad(interpreter)));
18430b57cec5SDimitry Andric   LoadSubCommand("unload",
18440b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessUnload(interpreter)));
18450b57cec5SDimitry Andric   LoadSubCommand("signal",
18460b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessSignal(interpreter)));
18470b57cec5SDimitry Andric   LoadSubCommand("handle",
18480b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessHandle(interpreter)));
18490b57cec5SDimitry Andric   LoadSubCommand("status",
18500b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessStatus(interpreter)));
18510b57cec5SDimitry Andric   LoadSubCommand("interrupt", CommandObjectSP(new CommandObjectProcessInterrupt(
18520b57cec5SDimitry Andric                                   interpreter)));
18530b57cec5SDimitry Andric   LoadSubCommand("kill",
18540b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessKill(interpreter)));
18550b57cec5SDimitry Andric   LoadSubCommand("plugin",
18560b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessPlugin(interpreter)));
18570b57cec5SDimitry Andric   LoadSubCommand("save-core", CommandObjectSP(new CommandObjectProcessSaveCore(
18580b57cec5SDimitry Andric                                   interpreter)));
1859fe6060f1SDimitry Andric   LoadSubCommand(
1860fe6060f1SDimitry Andric       "trace",
1861fe6060f1SDimitry Andric       CommandObjectSP(new CommandObjectMultiwordProcessTrace(interpreter)));
18620b57cec5SDimitry Andric }
18630b57cec5SDimitry Andric 
18640b57cec5SDimitry Andric CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess() = default;
1865