15ffd83dbSDimitry Andric //===-- CommandObjectProcess.cpp ------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "CommandObjectProcess.h"
1081ad6265SDimitry Andric #include "CommandObjectBreakpoint.h"
11fcaf7f86SDimitry Andric #include "CommandObjectTrace.h"
12e8d8bef9SDimitry Andric #include "CommandOptionsProcessLaunch.h"
13*0b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
1481ad6265SDimitry Andric #include "lldb/Breakpoint/BreakpointIDList.h"
15*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
1681ad6265SDimitry Andric #include "lldb/Breakpoint/BreakpointName.h"
17*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointSite.h"
18*0b57cec5SDimitry Andric #include "lldb/Core/Module.h"
19*0b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
20*0b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h"
21*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
22fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
23*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
24*0b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
25fe6060f1SDimitry Andric #include "lldb/Interpreter/OptionGroupPythonClassWithDict.h"
26*0b57cec5SDimitry Andric #include "lldb/Interpreter/Options.h"
27*0b57cec5SDimitry Andric #include "lldb/Target/Platform.h"
28*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
29*0b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
30*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
31*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
32*0b57cec5SDimitry Andric #include "lldb/Target/UnixSignals.h"
33*0b57cec5SDimitry Andric #include "lldb/Utility/Args.h"
34*0b57cec5SDimitry Andric #include "lldb/Utility/State.h"
35*0b57cec5SDimitry Andric 
3681ad6265SDimitry Andric #include "llvm/ADT/ScopeExit.h"
3781ad6265SDimitry Andric 
38fe6060f1SDimitry Andric #include <bitset>
39fe6060f1SDimitry Andric 
40*0b57cec5SDimitry Andric using namespace lldb;
41*0b57cec5SDimitry Andric using namespace lldb_private;
42*0b57cec5SDimitry Andric 
43*0b57cec5SDimitry Andric class CommandObjectProcessLaunchOrAttach : public CommandObjectParsed {
44*0b57cec5SDimitry Andric public:
45*0b57cec5SDimitry Andric   CommandObjectProcessLaunchOrAttach(CommandInterpreter &interpreter,
46*0b57cec5SDimitry Andric                                      const char *name, const char *help,
47*0b57cec5SDimitry Andric                                      const char *syntax, uint32_t flags,
48*0b57cec5SDimitry Andric                                      const char *new_process_action)
49*0b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, name, help, syntax, flags),
50*0b57cec5SDimitry Andric         m_new_process_action(new_process_action) {}
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric   ~CommandObjectProcessLaunchOrAttach() override = default;
53*0b57cec5SDimitry Andric 
54*0b57cec5SDimitry Andric protected:
55*0b57cec5SDimitry Andric   bool StopProcessIfNecessary(Process *process, StateType &state,
56*0b57cec5SDimitry Andric                               CommandReturnObject &result) {
57*0b57cec5SDimitry Andric     state = eStateInvalid;
58*0b57cec5SDimitry Andric     if (process) {
59*0b57cec5SDimitry Andric       state = process->GetState();
60*0b57cec5SDimitry Andric 
61*0b57cec5SDimitry Andric       if (process->IsAlive() && state != eStateConnected) {
62e8d8bef9SDimitry Andric         std::string message;
63*0b57cec5SDimitry Andric         if (process->GetState() == eStateAttaching)
64e8d8bef9SDimitry Andric           message =
65e8d8bef9SDimitry Andric               llvm::formatv("There is a pending attach, abort it and {0}?",
66e8d8bef9SDimitry Andric                             m_new_process_action);
67*0b57cec5SDimitry Andric         else if (process->GetShouldDetach())
68e8d8bef9SDimitry Andric           message = llvm::formatv(
69e8d8bef9SDimitry Andric               "There is a running process, detach from it and {0}?",
70e8d8bef9SDimitry Andric               m_new_process_action);
71*0b57cec5SDimitry Andric         else
72e8d8bef9SDimitry Andric           message =
73e8d8bef9SDimitry Andric               llvm::formatv("There is a running process, kill it and {0}?",
74e8d8bef9SDimitry Andric                             m_new_process_action);
75*0b57cec5SDimitry Andric 
76*0b57cec5SDimitry Andric         if (!m_interpreter.Confirm(message, true)) {
77*0b57cec5SDimitry Andric           result.SetStatus(eReturnStatusFailed);
78*0b57cec5SDimitry Andric           return false;
79*0b57cec5SDimitry Andric         } else {
80*0b57cec5SDimitry Andric           if (process->GetShouldDetach()) {
81*0b57cec5SDimitry Andric             bool keep_stopped = false;
82*0b57cec5SDimitry Andric             Status detach_error(process->Detach(keep_stopped));
83*0b57cec5SDimitry Andric             if (detach_error.Success()) {
84*0b57cec5SDimitry Andric               result.SetStatus(eReturnStatusSuccessFinishResult);
85*0b57cec5SDimitry Andric               process = nullptr;
86*0b57cec5SDimitry Andric             } else {
87*0b57cec5SDimitry Andric               result.AppendErrorWithFormat(
88*0b57cec5SDimitry Andric                   "Failed to detach from process: %s\n",
89*0b57cec5SDimitry Andric                   detach_error.AsCString());
90*0b57cec5SDimitry Andric             }
91*0b57cec5SDimitry Andric           } else {
92*0b57cec5SDimitry Andric             Status destroy_error(process->Destroy(false));
93*0b57cec5SDimitry Andric             if (destroy_error.Success()) {
94*0b57cec5SDimitry Andric               result.SetStatus(eReturnStatusSuccessFinishResult);
95*0b57cec5SDimitry Andric               process = nullptr;
96*0b57cec5SDimitry Andric             } else {
97*0b57cec5SDimitry Andric               result.AppendErrorWithFormat("Failed to kill process: %s\n",
98*0b57cec5SDimitry Andric                                            destroy_error.AsCString());
99*0b57cec5SDimitry Andric             }
100*0b57cec5SDimitry Andric           }
101*0b57cec5SDimitry Andric         }
102*0b57cec5SDimitry Andric       }
103*0b57cec5SDimitry Andric     }
104*0b57cec5SDimitry Andric     return result.Succeeded();
105*0b57cec5SDimitry Andric   }
106*0b57cec5SDimitry Andric 
107*0b57cec5SDimitry Andric   std::string m_new_process_action;
108*0b57cec5SDimitry Andric };
109*0b57cec5SDimitry Andric 
110*0b57cec5SDimitry Andric // CommandObjectProcessLaunch
111*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessLaunch
112*0b57cec5SDimitry Andric class CommandObjectProcessLaunch : public CommandObjectProcessLaunchOrAttach {
113*0b57cec5SDimitry Andric public:
114*0b57cec5SDimitry Andric   CommandObjectProcessLaunch(CommandInterpreter &interpreter)
115*0b57cec5SDimitry Andric       : CommandObjectProcessLaunchOrAttach(
116*0b57cec5SDimitry Andric             interpreter, "process launch",
117*0b57cec5SDimitry Andric             "Launch the executable in the debugger.", nullptr,
118*0b57cec5SDimitry Andric             eCommandRequiresTarget, "restart"),
11904eeddc0SDimitry Andric 
12004eeddc0SDimitry Andric         m_class_options("scripted process", true, 'C', 'k', 'v', 0) {
121fe6060f1SDimitry Andric     m_all_options.Append(&m_options);
122fe6060f1SDimitry Andric     m_all_options.Append(&m_class_options, LLDB_OPT_SET_1 | LLDB_OPT_SET_2,
123fe6060f1SDimitry Andric                          LLDB_OPT_SET_ALL);
124fe6060f1SDimitry Andric     m_all_options.Finalize();
125fe6060f1SDimitry Andric 
126*0b57cec5SDimitry Andric     CommandArgumentEntry arg;
127*0b57cec5SDimitry Andric     CommandArgumentData run_args_arg;
128*0b57cec5SDimitry Andric 
129*0b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
130*0b57cec5SDimitry Andric     run_args_arg.arg_type = eArgTypeRunArgs;
131*0b57cec5SDimitry Andric     run_args_arg.arg_repetition = eArgRepeatOptional;
132*0b57cec5SDimitry Andric 
133*0b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
134*0b57cec5SDimitry Andric     // argument entry.
135*0b57cec5SDimitry Andric     arg.push_back(run_args_arg);
136*0b57cec5SDimitry Andric 
137*0b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
138*0b57cec5SDimitry Andric     m_arguments.push_back(arg);
139*0b57cec5SDimitry Andric   }
140*0b57cec5SDimitry Andric 
141*0b57cec5SDimitry Andric   ~CommandObjectProcessLaunch() override = default;
142*0b57cec5SDimitry Andric 
1439dba64beSDimitry Andric   void
1449dba64beSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
145*0b57cec5SDimitry Andric                            OptionElementVector &opt_element_vector) override {
146*0b57cec5SDimitry Andric 
147*0b57cec5SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
148*0b57cec5SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
149*0b57cec5SDimitry Andric         request, nullptr);
150*0b57cec5SDimitry Andric   }
151*0b57cec5SDimitry Andric 
152fe6060f1SDimitry Andric   Options *GetOptions() override { return &m_all_options; }
153*0b57cec5SDimitry Andric 
15481ad6265SDimitry Andric   llvm::Optional<std::string> GetRepeatCommand(Args &current_command_args,
155*0b57cec5SDimitry Andric                                                uint32_t index) override {
156*0b57cec5SDimitry Andric     // No repeat for "process launch"...
15781ad6265SDimitry Andric     return std::string("");
158*0b57cec5SDimitry Andric   }
159*0b57cec5SDimitry Andric 
160*0b57cec5SDimitry Andric protected:
161*0b57cec5SDimitry Andric   bool DoExecute(Args &launch_args, CommandReturnObject &result) override {
162*0b57cec5SDimitry Andric     Debugger &debugger = GetDebugger();
163*0b57cec5SDimitry Andric     Target *target = debugger.GetSelectedTarget().get();
164*0b57cec5SDimitry Andric     // If our listener is nullptr, users aren't allows to launch
165*0b57cec5SDimitry Andric     ModuleSP exe_module_sp = target->GetExecutableModule();
166*0b57cec5SDimitry Andric 
167349cc55cSDimitry Andric     // If the target already has an executable module, then use that.  If it
168349cc55cSDimitry Andric     // doesn't then someone must be trying to launch using a path that will
169349cc55cSDimitry Andric     // make sense to the remote stub, but doesn't exist on the local host.
170349cc55cSDimitry Andric     // In that case use the ExecutableFile that was set in the target's
171349cc55cSDimitry Andric     // ProcessLaunchInfo.
172349cc55cSDimitry Andric     if (exe_module_sp == nullptr && !target->GetProcessLaunchInfo().GetExecutableFile()) {
173*0b57cec5SDimitry Andric       result.AppendError("no file in target, create a debug target using the "
174*0b57cec5SDimitry Andric                          "'target create' command");
175*0b57cec5SDimitry Andric       return false;
176*0b57cec5SDimitry Andric     }
177*0b57cec5SDimitry Andric 
178*0b57cec5SDimitry Andric     StateType state = eStateInvalid;
179*0b57cec5SDimitry Andric 
180*0b57cec5SDimitry Andric     if (!StopProcessIfNecessary(m_exe_ctx.GetProcessPtr(), state, result))
181*0b57cec5SDimitry Andric       return false;
182*0b57cec5SDimitry Andric 
183*0b57cec5SDimitry Andric     // Determine whether we will disable ASLR or leave it in the default state
184*0b57cec5SDimitry Andric     // (i.e. enabled if the platform supports it). First check if the process
185*0b57cec5SDimitry Andric     // launch options explicitly turn on/off
186*0b57cec5SDimitry Andric     // disabling ASLR.  If so, use that setting;
187*0b57cec5SDimitry Andric     // otherwise, use the 'settings target.disable-aslr' setting.
188*0b57cec5SDimitry Andric     bool disable_aslr = false;
189*0b57cec5SDimitry Andric     if (m_options.disable_aslr != eLazyBoolCalculate) {
190*0b57cec5SDimitry Andric       // The user specified an explicit setting on the process launch line.
191*0b57cec5SDimitry Andric       // Use it.
192*0b57cec5SDimitry Andric       disable_aslr = (m_options.disable_aslr == eLazyBoolYes);
193*0b57cec5SDimitry Andric     } else {
194*0b57cec5SDimitry Andric       // The user did not explicitly specify whether to disable ASLR.  Fall
195*0b57cec5SDimitry Andric       // back to the target.disable-aslr setting.
196*0b57cec5SDimitry Andric       disable_aslr = target->GetDisableASLR();
197*0b57cec5SDimitry Andric     }
198*0b57cec5SDimitry Andric 
199fe6060f1SDimitry Andric     if (!m_class_options.GetName().empty()) {
200fe6060f1SDimitry Andric       m_options.launch_info.SetProcessPluginName("ScriptedProcess");
201fe6060f1SDimitry Andric       m_options.launch_info.SetScriptedProcessClassName(
202fe6060f1SDimitry Andric           m_class_options.GetName());
203fe6060f1SDimitry Andric       m_options.launch_info.SetScriptedProcessDictionarySP(
204fe6060f1SDimitry Andric           m_class_options.GetStructuredData());
205fe6060f1SDimitry Andric       target->SetProcessLaunchInfo(m_options.launch_info);
206fe6060f1SDimitry Andric     }
207fe6060f1SDimitry Andric 
208*0b57cec5SDimitry Andric     if (disable_aslr)
209*0b57cec5SDimitry Andric       m_options.launch_info.GetFlags().Set(eLaunchFlagDisableASLR);
210*0b57cec5SDimitry Andric     else
211*0b57cec5SDimitry Andric       m_options.launch_info.GetFlags().Clear(eLaunchFlagDisableASLR);
212*0b57cec5SDimitry Andric 
213e8d8bef9SDimitry Andric     if (target->GetInheritTCC())
214e8d8bef9SDimitry Andric       m_options.launch_info.GetFlags().Set(eLaunchFlagInheritTCCFromParent);
215e8d8bef9SDimitry Andric 
216*0b57cec5SDimitry Andric     if (target->GetDetachOnError())
217*0b57cec5SDimitry Andric       m_options.launch_info.GetFlags().Set(eLaunchFlagDetachOnError);
218*0b57cec5SDimitry Andric 
219*0b57cec5SDimitry Andric     if (target->GetDisableSTDIO())
220*0b57cec5SDimitry Andric       m_options.launch_info.GetFlags().Set(eLaunchFlagDisableSTDIO);
221*0b57cec5SDimitry Andric 
222*0b57cec5SDimitry Andric     // Merge the launch info environment with the target environment.
223*0b57cec5SDimitry Andric     Environment target_env = target->GetEnvironment();
224*0b57cec5SDimitry Andric     m_options.launch_info.GetEnvironment().insert(target_env.begin(),
225*0b57cec5SDimitry Andric                                                   target_env.end());
226*0b57cec5SDimitry Andric 
227349cc55cSDimitry Andric     llvm::StringRef target_settings_argv0 = target->GetArg0();
228349cc55cSDimitry Andric 
229*0b57cec5SDimitry Andric     if (!target_settings_argv0.empty()) {
230*0b57cec5SDimitry Andric       m_options.launch_info.GetArguments().AppendArgument(
231*0b57cec5SDimitry Andric           target_settings_argv0);
232349cc55cSDimitry Andric       if (exe_module_sp)
233*0b57cec5SDimitry Andric         m_options.launch_info.SetExecutableFile(
234*0b57cec5SDimitry Andric             exe_module_sp->GetPlatformFileSpec(), false);
235349cc55cSDimitry Andric       else
236349cc55cSDimitry Andric         m_options.launch_info.SetExecutableFile(target->GetProcessLaunchInfo().GetExecutableFile(), false);
237*0b57cec5SDimitry Andric     } else {
238349cc55cSDimitry Andric       if (exe_module_sp)
239*0b57cec5SDimitry Andric         m_options.launch_info.SetExecutableFile(
240*0b57cec5SDimitry Andric             exe_module_sp->GetPlatformFileSpec(), true);
241349cc55cSDimitry Andric       else
242349cc55cSDimitry Andric         m_options.launch_info.SetExecutableFile(target->GetProcessLaunchInfo().GetExecutableFile(), true);
243*0b57cec5SDimitry Andric     }
244*0b57cec5SDimitry Andric 
245*0b57cec5SDimitry Andric     if (launch_args.GetArgumentCount() == 0) {
246*0b57cec5SDimitry Andric       m_options.launch_info.GetArguments().AppendArguments(
247*0b57cec5SDimitry Andric           target->GetProcessLaunchInfo().GetArguments());
248*0b57cec5SDimitry Andric     } else {
249*0b57cec5SDimitry Andric       m_options.launch_info.GetArguments().AppendArguments(launch_args);
250*0b57cec5SDimitry Andric       // Save the arguments for subsequent runs in the current target.
251*0b57cec5SDimitry Andric       target->SetRunArguments(launch_args);
252*0b57cec5SDimitry Andric     }
253*0b57cec5SDimitry Andric 
254*0b57cec5SDimitry Andric     StreamString stream;
255*0b57cec5SDimitry Andric     Status error = target->Launch(m_options.launch_info, &stream);
256*0b57cec5SDimitry Andric 
257*0b57cec5SDimitry Andric     if (error.Success()) {
258*0b57cec5SDimitry Andric       ProcessSP process_sp(target->GetProcessSP());
259*0b57cec5SDimitry Andric       if (process_sp) {
260*0b57cec5SDimitry Andric         // There is a race condition where this thread will return up the call
261*0b57cec5SDimitry Andric         // stack to the main command handler and show an (lldb) prompt before
262*0b57cec5SDimitry Andric         // HandlePrivateEvent (from PrivateStateThread) has a chance to call
263*0b57cec5SDimitry Andric         // PushProcessIOHandler().
264*0b57cec5SDimitry Andric         process_sp->SyncIOHandler(0, std::chrono::seconds(2));
265*0b57cec5SDimitry Andric 
266*0b57cec5SDimitry Andric         llvm::StringRef data = stream.GetString();
267*0b57cec5SDimitry Andric         if (!data.empty())
268*0b57cec5SDimitry Andric           result.AppendMessage(data);
269349cc55cSDimitry Andric         // If we didn't have a local executable, then we wouldn't have had an
270349cc55cSDimitry Andric         // executable module before launch.
271349cc55cSDimitry Andric         if (!exe_module_sp)
272349cc55cSDimitry Andric           exe_module_sp = target->GetExecutableModule();
273349cc55cSDimitry Andric         if (!exe_module_sp) {
274349cc55cSDimitry Andric           result.AppendWarning("Could not get executable module after launch.");
275349cc55cSDimitry Andric         } else {
276349cc55cSDimitry Andric 
277*0b57cec5SDimitry Andric           const char *archname =
278*0b57cec5SDimitry Andric               exe_module_sp->GetArchitecture().GetArchitectureName();
279*0b57cec5SDimitry Andric           result.AppendMessageWithFormat(
280*0b57cec5SDimitry Andric               "Process %" PRIu64 " launched: '%s' (%s)\n", process_sp->GetID(),
281*0b57cec5SDimitry Andric               exe_module_sp->GetFileSpec().GetPath().c_str(), archname);
282349cc55cSDimitry Andric         }
283*0b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishResult);
284*0b57cec5SDimitry Andric         result.SetDidChangeProcessState(true);
285*0b57cec5SDimitry Andric       } else {
286*0b57cec5SDimitry Andric         result.AppendError(
287*0b57cec5SDimitry Andric             "no error returned from Target::Launch, and target has no process");
288*0b57cec5SDimitry Andric       }
289*0b57cec5SDimitry Andric     } else {
290*0b57cec5SDimitry Andric       result.AppendError(error.AsCString());
291*0b57cec5SDimitry Andric     }
292*0b57cec5SDimitry Andric     return result.Succeeded();
293*0b57cec5SDimitry Andric   }
294*0b57cec5SDimitry Andric 
295e8d8bef9SDimitry Andric   CommandOptionsProcessLaunch m_options;
296fe6060f1SDimitry Andric   OptionGroupPythonClassWithDict m_class_options;
297fe6060f1SDimitry Andric   OptionGroupOptions m_all_options;
298*0b57cec5SDimitry Andric };
299*0b57cec5SDimitry Andric 
3009dba64beSDimitry Andric #define LLDB_OPTIONS_process_attach
3019dba64beSDimitry Andric #include "CommandOptions.inc"
302*0b57cec5SDimitry Andric 
303*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessAttach
304*0b57cec5SDimitry Andric class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach {
305*0b57cec5SDimitry Andric public:
306*0b57cec5SDimitry Andric   class CommandOptions : public Options {
307*0b57cec5SDimitry Andric   public:
30804eeddc0SDimitry Andric     CommandOptions() {
309*0b57cec5SDimitry Andric       // Keep default values of all options in one place: OptionParsingStarting
310*0b57cec5SDimitry Andric       // ()
311*0b57cec5SDimitry Andric       OptionParsingStarting(nullptr);
312*0b57cec5SDimitry Andric     }
313*0b57cec5SDimitry Andric 
314*0b57cec5SDimitry Andric     ~CommandOptions() override = default;
315*0b57cec5SDimitry Andric 
316*0b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
317*0b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
318*0b57cec5SDimitry Andric       Status error;
319*0b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
320*0b57cec5SDimitry Andric       switch (short_option) {
321*0b57cec5SDimitry Andric       case 'c':
322*0b57cec5SDimitry Andric         attach_info.SetContinueOnceAttached(true);
323*0b57cec5SDimitry Andric         break;
324*0b57cec5SDimitry Andric 
325*0b57cec5SDimitry Andric       case 'p': {
326*0b57cec5SDimitry Andric         lldb::pid_t pid;
327*0b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, pid)) {
328*0b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid process ID '%s'",
329*0b57cec5SDimitry Andric                                          option_arg.str().c_str());
330*0b57cec5SDimitry Andric         } else {
331*0b57cec5SDimitry Andric           attach_info.SetProcessID(pid);
332*0b57cec5SDimitry Andric         }
333*0b57cec5SDimitry Andric       } break;
334*0b57cec5SDimitry Andric 
335*0b57cec5SDimitry Andric       case 'P':
336*0b57cec5SDimitry Andric         attach_info.SetProcessPluginName(option_arg);
337*0b57cec5SDimitry Andric         break;
338*0b57cec5SDimitry Andric 
339*0b57cec5SDimitry Andric       case 'n':
340*0b57cec5SDimitry Andric         attach_info.GetExecutableFile().SetFile(option_arg,
341*0b57cec5SDimitry Andric                                                 FileSpec::Style::native);
342*0b57cec5SDimitry Andric         break;
343*0b57cec5SDimitry Andric 
344*0b57cec5SDimitry Andric       case 'w':
345*0b57cec5SDimitry Andric         attach_info.SetWaitForLaunch(true);
346*0b57cec5SDimitry Andric         break;
347*0b57cec5SDimitry Andric 
348*0b57cec5SDimitry Andric       case 'i':
349*0b57cec5SDimitry Andric         attach_info.SetIgnoreExisting(false);
350*0b57cec5SDimitry Andric         break;
351*0b57cec5SDimitry Andric 
352*0b57cec5SDimitry Andric       default:
3539dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
354*0b57cec5SDimitry Andric       }
355*0b57cec5SDimitry Andric       return error;
356*0b57cec5SDimitry Andric     }
357*0b57cec5SDimitry Andric 
358*0b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
359*0b57cec5SDimitry Andric       attach_info.Clear();
360*0b57cec5SDimitry Andric     }
361*0b57cec5SDimitry Andric 
362*0b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
363*0b57cec5SDimitry Andric       return llvm::makeArrayRef(g_process_attach_options);
364*0b57cec5SDimitry Andric     }
365*0b57cec5SDimitry Andric 
366*0b57cec5SDimitry Andric     ProcessAttachInfo attach_info;
367*0b57cec5SDimitry Andric   };
368*0b57cec5SDimitry Andric 
369*0b57cec5SDimitry Andric   CommandObjectProcessAttach(CommandInterpreter &interpreter)
370*0b57cec5SDimitry Andric       : CommandObjectProcessLaunchOrAttach(
371*0b57cec5SDimitry Andric             interpreter, "process attach", "Attach to a process.",
37204eeddc0SDimitry Andric             "process attach <cmd-options>", 0, "attach") {}
373*0b57cec5SDimitry Andric 
374*0b57cec5SDimitry Andric   ~CommandObjectProcessAttach() override = default;
375*0b57cec5SDimitry Andric 
376*0b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
377*0b57cec5SDimitry Andric 
378*0b57cec5SDimitry Andric protected:
379*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
380*0b57cec5SDimitry Andric     PlatformSP platform_sp(
381*0b57cec5SDimitry Andric         GetDebugger().GetPlatformList().GetSelectedPlatform());
382*0b57cec5SDimitry Andric 
383*0b57cec5SDimitry Andric     Target *target = GetDebugger().GetSelectedTarget().get();
384*0b57cec5SDimitry Andric     // N.B. The attach should be synchronous.  It doesn't help much to get the
385*0b57cec5SDimitry Andric     // prompt back between initiating the attach and the target actually
386*0b57cec5SDimitry Andric     // stopping.  So even if the interpreter is set to be asynchronous, we wait
387*0b57cec5SDimitry Andric     // for the stop ourselves here.
388*0b57cec5SDimitry Andric 
389*0b57cec5SDimitry Andric     StateType state = eStateInvalid;
390*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
391*0b57cec5SDimitry Andric 
392*0b57cec5SDimitry Andric     if (!StopProcessIfNecessary(process, state, result))
393*0b57cec5SDimitry Andric       return false;
394*0b57cec5SDimitry Andric 
395*0b57cec5SDimitry Andric     if (target == nullptr) {
396*0b57cec5SDimitry Andric       // If there isn't a current target create one.
397*0b57cec5SDimitry Andric       TargetSP new_target_sp;
398*0b57cec5SDimitry Andric       Status error;
399*0b57cec5SDimitry Andric 
400*0b57cec5SDimitry Andric       error = GetDebugger().GetTargetList().CreateTarget(
401*0b57cec5SDimitry Andric           GetDebugger(), "", "", eLoadDependentsNo,
402*0b57cec5SDimitry Andric           nullptr, // No platform options
403*0b57cec5SDimitry Andric           new_target_sp);
404*0b57cec5SDimitry Andric       target = new_target_sp.get();
405*0b57cec5SDimitry Andric       if (target == nullptr || error.Fail()) {
406*0b57cec5SDimitry Andric         result.AppendError(error.AsCString("Error creating target"));
407*0b57cec5SDimitry Andric         return false;
408*0b57cec5SDimitry Andric       }
409*0b57cec5SDimitry Andric     }
410*0b57cec5SDimitry Andric 
411*0b57cec5SDimitry Andric     // Record the old executable module, we want to issue a warning if the
412*0b57cec5SDimitry Andric     // process of attaching changed the current executable (like somebody said
413*0b57cec5SDimitry Andric     // "file foo" then attached to a PID whose executable was bar.)
414*0b57cec5SDimitry Andric 
415*0b57cec5SDimitry Andric     ModuleSP old_exec_module_sp = target->GetExecutableModule();
416*0b57cec5SDimitry Andric     ArchSpec old_arch_spec = target->GetArchitecture();
417*0b57cec5SDimitry Andric 
418*0b57cec5SDimitry Andric     StreamString stream;
419349cc55cSDimitry Andric     ProcessSP process_sp;
420*0b57cec5SDimitry Andric     const auto error = target->Attach(m_options.attach_info, &stream);
421*0b57cec5SDimitry Andric     if (error.Success()) {
422349cc55cSDimitry Andric       process_sp = target->GetProcessSP();
423*0b57cec5SDimitry Andric       if (process_sp) {
424*0b57cec5SDimitry Andric         result.AppendMessage(stream.GetString());
425*0b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishNoResult);
426*0b57cec5SDimitry Andric         result.SetDidChangeProcessState(true);
427*0b57cec5SDimitry Andric       } else {
428*0b57cec5SDimitry Andric         result.AppendError(
429*0b57cec5SDimitry Andric             "no error returned from Target::Attach, and target has no process");
430*0b57cec5SDimitry Andric       }
431*0b57cec5SDimitry Andric     } else {
432*0b57cec5SDimitry Andric       result.AppendErrorWithFormat("attach failed: %s\n", error.AsCString());
433*0b57cec5SDimitry Andric     }
434*0b57cec5SDimitry Andric 
435*0b57cec5SDimitry Andric     if (!result.Succeeded())
436*0b57cec5SDimitry Andric       return false;
437*0b57cec5SDimitry Andric 
438*0b57cec5SDimitry Andric     // Okay, we're done.  Last step is to warn if the executable module has
439*0b57cec5SDimitry Andric     // changed:
440*0b57cec5SDimitry Andric     char new_path[PATH_MAX];
441*0b57cec5SDimitry Andric     ModuleSP new_exec_module_sp(target->GetExecutableModule());
442*0b57cec5SDimitry Andric     if (!old_exec_module_sp) {
443*0b57cec5SDimitry Andric       // We might not have a module if we attached to a raw pid...
444*0b57cec5SDimitry Andric       if (new_exec_module_sp) {
445*0b57cec5SDimitry Andric         new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
446*0b57cec5SDimitry Andric         result.AppendMessageWithFormat("Executable module set to \"%s\".\n",
447*0b57cec5SDimitry Andric                                        new_path);
448*0b57cec5SDimitry Andric       }
449*0b57cec5SDimitry Andric     } else if (old_exec_module_sp->GetFileSpec() !=
450*0b57cec5SDimitry Andric                new_exec_module_sp->GetFileSpec()) {
451*0b57cec5SDimitry Andric       char old_path[PATH_MAX];
452*0b57cec5SDimitry Andric 
453*0b57cec5SDimitry Andric       old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
454*0b57cec5SDimitry Andric       new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
455*0b57cec5SDimitry Andric 
456*0b57cec5SDimitry Andric       result.AppendWarningWithFormat(
457*0b57cec5SDimitry Andric           "Executable module changed from \"%s\" to \"%s\".\n", old_path,
458*0b57cec5SDimitry Andric           new_path);
459*0b57cec5SDimitry Andric     }
460*0b57cec5SDimitry Andric 
461*0b57cec5SDimitry Andric     if (!old_arch_spec.IsValid()) {
462*0b57cec5SDimitry Andric       result.AppendMessageWithFormat(
463*0b57cec5SDimitry Andric           "Architecture set to: %s.\n",
464*0b57cec5SDimitry Andric           target->GetArchitecture().GetTriple().getTriple().c_str());
465*0b57cec5SDimitry Andric     } else if (!old_arch_spec.IsExactMatch(target->GetArchitecture())) {
466*0b57cec5SDimitry Andric       result.AppendWarningWithFormat(
467*0b57cec5SDimitry Andric           "Architecture changed from %s to %s.\n",
468*0b57cec5SDimitry Andric           old_arch_spec.GetTriple().getTriple().c_str(),
469*0b57cec5SDimitry Andric           target->GetArchitecture().GetTriple().getTriple().c_str());
470*0b57cec5SDimitry Andric     }
471*0b57cec5SDimitry Andric 
472*0b57cec5SDimitry Andric     // This supports the use-case scenario of immediately continuing the
473*0b57cec5SDimitry Andric     // process once attached.
474349cc55cSDimitry Andric     if (m_options.attach_info.GetContinueOnceAttached()) {
475349cc55cSDimitry Andric       // We have made a process but haven't told the interpreter about it yet,
476349cc55cSDimitry Andric       // so CheckRequirements will fail for "process continue".  Set the override
477349cc55cSDimitry Andric       // here:
478349cc55cSDimitry Andric       ExecutionContext exe_ctx(process_sp);
479349cc55cSDimitry Andric       m_interpreter.HandleCommand("process continue", eLazyBoolNo, exe_ctx, result);
480349cc55cSDimitry Andric     }
481*0b57cec5SDimitry Andric 
482*0b57cec5SDimitry Andric     return result.Succeeded();
483*0b57cec5SDimitry Andric   }
484*0b57cec5SDimitry Andric 
485*0b57cec5SDimitry Andric   CommandOptions m_options;
486*0b57cec5SDimitry Andric };
487*0b57cec5SDimitry Andric 
488*0b57cec5SDimitry Andric // CommandObjectProcessContinue
489*0b57cec5SDimitry Andric 
4909dba64beSDimitry Andric #define LLDB_OPTIONS_process_continue
4919dba64beSDimitry Andric #include "CommandOptions.inc"
492*0b57cec5SDimitry Andric 
493*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessContinue
494*0b57cec5SDimitry Andric 
495*0b57cec5SDimitry Andric class CommandObjectProcessContinue : public CommandObjectParsed {
496*0b57cec5SDimitry Andric public:
497*0b57cec5SDimitry Andric   CommandObjectProcessContinue(CommandInterpreter &interpreter)
498*0b57cec5SDimitry Andric       : CommandObjectParsed(
499*0b57cec5SDimitry Andric             interpreter, "process continue",
500*0b57cec5SDimitry Andric             "Continue execution of all threads in the current process.",
501*0b57cec5SDimitry Andric             "process continue",
502*0b57cec5SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
50304eeddc0SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
504*0b57cec5SDimitry Andric 
505*0b57cec5SDimitry Andric   ~CommandObjectProcessContinue() override = default;
506*0b57cec5SDimitry Andric 
507*0b57cec5SDimitry Andric protected:
508*0b57cec5SDimitry Andric   class CommandOptions : public Options {
509*0b57cec5SDimitry Andric   public:
51004eeddc0SDimitry Andric     CommandOptions() {
511*0b57cec5SDimitry Andric       // Keep default values of all options in one place: OptionParsingStarting
512*0b57cec5SDimitry Andric       // ()
513*0b57cec5SDimitry Andric       OptionParsingStarting(nullptr);
514*0b57cec5SDimitry Andric     }
515*0b57cec5SDimitry Andric 
516*0b57cec5SDimitry Andric     ~CommandOptions() override = default;
517*0b57cec5SDimitry Andric 
518*0b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
51981ad6265SDimitry Andric                           ExecutionContext *exe_ctx) override {
520*0b57cec5SDimitry Andric       Status error;
521*0b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
522*0b57cec5SDimitry Andric       switch (short_option) {
523*0b57cec5SDimitry Andric       case 'i':
524*0b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, m_ignore))
525*0b57cec5SDimitry Andric           error.SetErrorStringWithFormat(
526*0b57cec5SDimitry Andric               "invalid value for ignore option: \"%s\", should be a number.",
527*0b57cec5SDimitry Andric               option_arg.str().c_str());
528*0b57cec5SDimitry Andric         break;
52981ad6265SDimitry Andric       case 'b':
53081ad6265SDimitry Andric         m_run_to_bkpt_args.AppendArgument(option_arg);
53181ad6265SDimitry Andric         m_any_bkpts_specified = true;
53281ad6265SDimitry Andric       break;
533*0b57cec5SDimitry Andric       default:
5349dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
535*0b57cec5SDimitry Andric       }
536*0b57cec5SDimitry Andric       return error;
537*0b57cec5SDimitry Andric     }
538*0b57cec5SDimitry Andric 
539*0b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
540*0b57cec5SDimitry Andric       m_ignore = 0;
54181ad6265SDimitry Andric       m_run_to_bkpt_args.Clear();
54281ad6265SDimitry Andric       m_any_bkpts_specified = false;
543*0b57cec5SDimitry Andric     }
544*0b57cec5SDimitry Andric 
545*0b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
546*0b57cec5SDimitry Andric       return llvm::makeArrayRef(g_process_continue_options);
547*0b57cec5SDimitry Andric     }
548*0b57cec5SDimitry Andric 
54981ad6265SDimitry Andric     uint32_t m_ignore = 0;
55081ad6265SDimitry Andric     Args m_run_to_bkpt_args;
55181ad6265SDimitry Andric     bool m_any_bkpts_specified = false;
552*0b57cec5SDimitry Andric   };
553*0b57cec5SDimitry Andric 
55481ad6265SDimitry Andric 
555*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
556*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
557*0b57cec5SDimitry Andric     bool synchronous_execution = m_interpreter.GetSynchronous();
558*0b57cec5SDimitry Andric     StateType state = process->GetState();
559*0b57cec5SDimitry Andric     if (state == eStateStopped) {
560*0b57cec5SDimitry Andric       if (m_options.m_ignore > 0) {
561*0b57cec5SDimitry Andric         ThreadSP sel_thread_sp(GetDefaultThread()->shared_from_this());
562*0b57cec5SDimitry Andric         if (sel_thread_sp) {
563*0b57cec5SDimitry Andric           StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
564*0b57cec5SDimitry Andric           if (stop_info_sp &&
565*0b57cec5SDimitry Andric               stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
566*0b57cec5SDimitry Andric             lldb::break_id_t bp_site_id =
567*0b57cec5SDimitry Andric                 (lldb::break_id_t)stop_info_sp->GetValue();
568*0b57cec5SDimitry Andric             BreakpointSiteSP bp_site_sp(
569*0b57cec5SDimitry Andric                 process->GetBreakpointSiteList().FindByID(bp_site_id));
570*0b57cec5SDimitry Andric             if (bp_site_sp) {
571*0b57cec5SDimitry Andric               const size_t num_owners = bp_site_sp->GetNumberOfOwners();
572*0b57cec5SDimitry Andric               for (size_t i = 0; i < num_owners; i++) {
573*0b57cec5SDimitry Andric                 Breakpoint &bp_ref =
574*0b57cec5SDimitry Andric                     bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
575*0b57cec5SDimitry Andric                 if (!bp_ref.IsInternal()) {
576*0b57cec5SDimitry Andric                   bp_ref.SetIgnoreCount(m_options.m_ignore);
577*0b57cec5SDimitry Andric                 }
578*0b57cec5SDimitry Andric               }
579*0b57cec5SDimitry Andric             }
580*0b57cec5SDimitry Andric           }
581*0b57cec5SDimitry Andric         }
582*0b57cec5SDimitry Andric       }
583*0b57cec5SDimitry Andric 
58481ad6265SDimitry Andric       Target *target = m_exe_ctx.GetTargetPtr();
58581ad6265SDimitry Andric       BreakpointIDList run_to_bkpt_ids;
58681ad6265SDimitry Andric       // Don't pass an empty run_to_breakpoint list, as Verify will look for the
58781ad6265SDimitry Andric       // default breakpoint.
58881ad6265SDimitry Andric       if (m_options.m_run_to_bkpt_args.GetArgumentCount() > 0)
58981ad6265SDimitry Andric         CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
59081ad6265SDimitry Andric             m_options.m_run_to_bkpt_args, target, result, &run_to_bkpt_ids,
59181ad6265SDimitry Andric             BreakpointName::Permissions::disablePerm);
59281ad6265SDimitry Andric       if (!result.Succeeded()) {
59381ad6265SDimitry Andric         return false;
59481ad6265SDimitry Andric       }
59581ad6265SDimitry Andric       result.Clear();
59681ad6265SDimitry Andric       if (m_options.m_any_bkpts_specified && run_to_bkpt_ids.GetSize() == 0) {
59781ad6265SDimitry Andric         result.AppendError("continue-to breakpoints did not specify any actual "
59881ad6265SDimitry Andric                            "breakpoints or locations");
59981ad6265SDimitry Andric         return false;
60081ad6265SDimitry Andric       }
60181ad6265SDimitry Andric 
60281ad6265SDimitry Andric       // First figure out which breakpoints & locations were specified by the
60381ad6265SDimitry Andric       // user:
60481ad6265SDimitry Andric       size_t num_run_to_bkpt_ids = run_to_bkpt_ids.GetSize();
60581ad6265SDimitry Andric       std::vector<break_id_t> bkpts_disabled;
60681ad6265SDimitry Andric       std::vector<BreakpointID> locs_disabled;
60781ad6265SDimitry Andric       if (num_run_to_bkpt_ids != 0) {
60881ad6265SDimitry Andric         // Go through the ID's specified, and separate the breakpoints from are
60981ad6265SDimitry Andric         // the breakpoint.location specifications since the latter require
61081ad6265SDimitry Andric         // special handling.  We also figure out whether there's at least one
61181ad6265SDimitry Andric         // specifier in the set that is enabled.
61281ad6265SDimitry Andric         BreakpointList &bkpt_list = target->GetBreakpointList();
61381ad6265SDimitry Andric         std::unordered_set<break_id_t> bkpts_seen;
61481ad6265SDimitry Andric         std::unordered_set<break_id_t> bkpts_with_locs_seen;
61581ad6265SDimitry Andric         BreakpointIDList with_locs;
61681ad6265SDimitry Andric         bool any_enabled = false;
61781ad6265SDimitry Andric 
61881ad6265SDimitry Andric         for (size_t idx = 0; idx < num_run_to_bkpt_ids; idx++) {
61981ad6265SDimitry Andric           BreakpointID bkpt_id = run_to_bkpt_ids.GetBreakpointIDAtIndex(idx);
62081ad6265SDimitry Andric           break_id_t bp_id = bkpt_id.GetBreakpointID();
62181ad6265SDimitry Andric           break_id_t loc_id = bkpt_id.GetLocationID();
62281ad6265SDimitry Andric           BreakpointSP bp_sp
62381ad6265SDimitry Andric               = bkpt_list.FindBreakpointByID(bp_id);
62481ad6265SDimitry Andric           // Note, VerifyBreakpointOrLocationIDs checks for existence, so we
62581ad6265SDimitry Andric           // don't need to do it again here.
62681ad6265SDimitry Andric           if (bp_sp->IsEnabled()) {
62781ad6265SDimitry Andric             if (loc_id == LLDB_INVALID_BREAK_ID) {
62881ad6265SDimitry Andric               // A breakpoint (without location) was specified.  Make sure that
62981ad6265SDimitry Andric               // at least one of the locations is enabled.
63081ad6265SDimitry Andric               size_t num_locations = bp_sp->GetNumLocations();
63181ad6265SDimitry Andric               for (size_t loc_idx = 0; loc_idx < num_locations; loc_idx++) {
63281ad6265SDimitry Andric                 BreakpointLocationSP loc_sp
63381ad6265SDimitry Andric                     = bp_sp->GetLocationAtIndex(loc_idx);
63481ad6265SDimitry Andric                 if (loc_sp->IsEnabled()) {
63581ad6265SDimitry Andric                   any_enabled = true;
63681ad6265SDimitry Andric                   break;
63781ad6265SDimitry Andric                 }
63881ad6265SDimitry Andric               }
63981ad6265SDimitry Andric             } else {
64081ad6265SDimitry Andric               // A location was specified, check if it was enabled:
64181ad6265SDimitry Andric               BreakpointLocationSP loc_sp = bp_sp->FindLocationByID(loc_id);
64281ad6265SDimitry Andric               if (loc_sp->IsEnabled())
64381ad6265SDimitry Andric                 any_enabled = true;
64481ad6265SDimitry Andric             }
64581ad6265SDimitry Andric 
64681ad6265SDimitry Andric             // Then sort the bp & bp.loc entries for later use:
64781ad6265SDimitry Andric             if (bkpt_id.GetLocationID() == LLDB_INVALID_BREAK_ID)
64881ad6265SDimitry Andric               bkpts_seen.insert(bkpt_id.GetBreakpointID());
64981ad6265SDimitry Andric             else {
65081ad6265SDimitry Andric               bkpts_with_locs_seen.insert(bkpt_id.GetBreakpointID());
65181ad6265SDimitry Andric               with_locs.AddBreakpointID(bkpt_id);
65281ad6265SDimitry Andric             }
65381ad6265SDimitry Andric           }
65481ad6265SDimitry Andric         }
65581ad6265SDimitry Andric         // Do all the error checking here so once we start disabling we don't
65681ad6265SDimitry Andric         // have to back out half-way through.
65781ad6265SDimitry Andric 
65881ad6265SDimitry Andric         // Make sure at least one of the specified breakpoints is enabled.
65981ad6265SDimitry Andric         if (!any_enabled) {
66081ad6265SDimitry Andric           result.AppendError("at least one of the continue-to breakpoints must "
66181ad6265SDimitry Andric                              "be enabled.");
66281ad6265SDimitry Andric           return false;
66381ad6265SDimitry Andric         }
66481ad6265SDimitry Andric 
66581ad6265SDimitry Andric         // Also, if you specify BOTH a breakpoint and one of it's locations,
66681ad6265SDimitry Andric         // we flag that as an error, since it won't do what you expect, the
66781ad6265SDimitry Andric         // breakpoint directive will mean "run to all locations", which is not
66881ad6265SDimitry Andric         // what the location directive means...
66981ad6265SDimitry Andric         for (break_id_t bp_id : bkpts_with_locs_seen) {
67081ad6265SDimitry Andric           if (bkpts_seen.count(bp_id)) {
67181ad6265SDimitry Andric             result.AppendErrorWithFormatv("can't specify both a breakpoint and "
67281ad6265SDimitry Andric                                "one of its locations: {0}", bp_id);
67381ad6265SDimitry Andric           }
67481ad6265SDimitry Andric         }
67581ad6265SDimitry Andric 
67681ad6265SDimitry Andric         // Now go through the breakpoints in the target, disabling all the ones
67781ad6265SDimitry Andric         // that the user didn't mention:
67881ad6265SDimitry Andric         for (BreakpointSP bp_sp : bkpt_list.Breakpoints()) {
67981ad6265SDimitry Andric           break_id_t bp_id = bp_sp->GetID();
68081ad6265SDimitry Andric           // Handle the case where no locations were specified.  Note we don't
68181ad6265SDimitry Andric           // have to worry about the case where a breakpoint and one of its
68281ad6265SDimitry Andric           // locations are both in the lists, we've already disallowed that.
68381ad6265SDimitry Andric           if (!bkpts_with_locs_seen.count(bp_id)) {
68481ad6265SDimitry Andric             if (!bkpts_seen.count(bp_id) && bp_sp->IsEnabled()) {
68581ad6265SDimitry Andric               bkpts_disabled.push_back(bp_id);
68681ad6265SDimitry Andric               bp_sp->SetEnabled(false);
68781ad6265SDimitry Andric             }
68881ad6265SDimitry Andric             continue;
68981ad6265SDimitry Andric           }
69081ad6265SDimitry Andric           // Next, handle the case where a location was specified:
69181ad6265SDimitry Andric           // Run through all the locations of this breakpoint and disable
69281ad6265SDimitry Andric           // the ones that aren't on our "with locations" BreakpointID list:
69381ad6265SDimitry Andric           size_t num_locations = bp_sp->GetNumLocations();
69481ad6265SDimitry Andric           BreakpointID tmp_id(bp_id, LLDB_INVALID_BREAK_ID);
69581ad6265SDimitry Andric           for (size_t loc_idx = 0; loc_idx < num_locations; loc_idx++) {
69681ad6265SDimitry Andric             BreakpointLocationSP loc_sp = bp_sp->GetLocationAtIndex(loc_idx);
69781ad6265SDimitry Andric             tmp_id.SetBreakpointLocationID(loc_idx);
69881ad6265SDimitry Andric             size_t position = 0;
69981ad6265SDimitry Andric             if (!with_locs.FindBreakpointID(tmp_id, &position)
70081ad6265SDimitry Andric                 && loc_sp->IsEnabled()) {
70181ad6265SDimitry Andric               locs_disabled.push_back(tmp_id);
70281ad6265SDimitry Andric               loc_sp->SetEnabled(false);
70381ad6265SDimitry Andric             }
70481ad6265SDimitry Andric           }
70581ad6265SDimitry Andric         }
70681ad6265SDimitry Andric       }
70781ad6265SDimitry Andric 
708*0b57cec5SDimitry Andric       { // Scope for thread list mutex:
709*0b57cec5SDimitry Andric         std::lock_guard<std::recursive_mutex> guard(
710*0b57cec5SDimitry Andric             process->GetThreadList().GetMutex());
711*0b57cec5SDimitry Andric         const uint32_t num_threads = process->GetThreadList().GetSize();
712*0b57cec5SDimitry Andric 
713*0b57cec5SDimitry Andric         // Set the actions that the threads should each take when resuming
714*0b57cec5SDimitry Andric         for (uint32_t idx = 0; idx < num_threads; ++idx) {
715*0b57cec5SDimitry Andric           const bool override_suspend = false;
716*0b57cec5SDimitry Andric           process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState(
717*0b57cec5SDimitry Andric               eStateRunning, override_suspend);
718*0b57cec5SDimitry Andric         }
719*0b57cec5SDimitry Andric       }
720*0b57cec5SDimitry Andric 
721*0b57cec5SDimitry Andric       const uint32_t iohandler_id = process->GetIOHandlerID();
722*0b57cec5SDimitry Andric 
723*0b57cec5SDimitry Andric       StreamString stream;
724*0b57cec5SDimitry Andric       Status error;
72581ad6265SDimitry Andric       // For now we can only do -b with synchronous:
72681ad6265SDimitry Andric       bool old_sync = GetDebugger().GetAsyncExecution();
72781ad6265SDimitry Andric 
72881ad6265SDimitry Andric       if (run_to_bkpt_ids.GetSize() != 0) {
72981ad6265SDimitry Andric         GetDebugger().SetAsyncExecution(false);
73081ad6265SDimitry Andric         synchronous_execution = true;
73181ad6265SDimitry Andric       }
732*0b57cec5SDimitry Andric       if (synchronous_execution)
733*0b57cec5SDimitry Andric         error = process->ResumeSynchronous(&stream);
734*0b57cec5SDimitry Andric       else
735*0b57cec5SDimitry Andric         error = process->Resume();
736*0b57cec5SDimitry Andric 
73781ad6265SDimitry Andric       if (run_to_bkpt_ids.GetSize() != 0) {
73881ad6265SDimitry Andric         GetDebugger().SetAsyncExecution(old_sync);
73981ad6265SDimitry Andric       }
74081ad6265SDimitry Andric 
74181ad6265SDimitry Andric       // Now re-enable the breakpoints we disabled:
74281ad6265SDimitry Andric       BreakpointList &bkpt_list = target->GetBreakpointList();
74381ad6265SDimitry Andric       for (break_id_t bp_id : bkpts_disabled) {
74481ad6265SDimitry Andric         BreakpointSP bp_sp = bkpt_list.FindBreakpointByID(bp_id);
74581ad6265SDimitry Andric         if (bp_sp)
74681ad6265SDimitry Andric           bp_sp->SetEnabled(true);
74781ad6265SDimitry Andric       }
74881ad6265SDimitry Andric       for (const BreakpointID &bkpt_id : locs_disabled) {
74981ad6265SDimitry Andric         BreakpointSP bp_sp
75081ad6265SDimitry Andric             = bkpt_list.FindBreakpointByID(bkpt_id.GetBreakpointID());
75181ad6265SDimitry Andric         if (bp_sp) {
75281ad6265SDimitry Andric           BreakpointLocationSP loc_sp
75381ad6265SDimitry Andric               = bp_sp->FindLocationByID(bkpt_id.GetLocationID());
75481ad6265SDimitry Andric           if (loc_sp)
75581ad6265SDimitry Andric             loc_sp->SetEnabled(true);
75681ad6265SDimitry Andric         }
75781ad6265SDimitry Andric       }
75881ad6265SDimitry Andric 
759*0b57cec5SDimitry Andric       if (error.Success()) {
760*0b57cec5SDimitry Andric         // There is a race condition where this thread will return up the call
761*0b57cec5SDimitry Andric         // stack to the main command handler and show an (lldb) prompt before
762*0b57cec5SDimitry Andric         // HandlePrivateEvent (from PrivateStateThread) has a chance to call
763*0b57cec5SDimitry Andric         // PushProcessIOHandler().
764*0b57cec5SDimitry Andric         process->SyncIOHandler(iohandler_id, std::chrono::seconds(2));
765*0b57cec5SDimitry Andric 
766*0b57cec5SDimitry Andric         result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
767*0b57cec5SDimitry Andric                                        process->GetID());
768*0b57cec5SDimitry Andric         if (synchronous_execution) {
769*0b57cec5SDimitry Andric           // If any state changed events had anything to say, add that to the
770*0b57cec5SDimitry Andric           // result
771*0b57cec5SDimitry Andric           result.AppendMessage(stream.GetString());
772*0b57cec5SDimitry Andric 
773*0b57cec5SDimitry Andric           result.SetDidChangeProcessState(true);
774*0b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessFinishNoResult);
775*0b57cec5SDimitry Andric         } else {
776*0b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessContinuingNoResult);
777*0b57cec5SDimitry Andric         }
778*0b57cec5SDimitry Andric       } else {
779*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("Failed to resume process: %s.\n",
780*0b57cec5SDimitry Andric                                      error.AsCString());
781*0b57cec5SDimitry Andric       }
782*0b57cec5SDimitry Andric     } else {
783*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
784*0b57cec5SDimitry Andric           "Process cannot be continued from its current state (%s).\n",
785*0b57cec5SDimitry Andric           StateAsCString(state));
786*0b57cec5SDimitry Andric     }
787*0b57cec5SDimitry Andric     return result.Succeeded();
788*0b57cec5SDimitry Andric   }
789*0b57cec5SDimitry Andric 
790*0b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
791*0b57cec5SDimitry Andric 
792*0b57cec5SDimitry Andric   CommandOptions m_options;
793*0b57cec5SDimitry Andric };
794*0b57cec5SDimitry Andric 
795*0b57cec5SDimitry Andric // CommandObjectProcessDetach
7969dba64beSDimitry Andric #define LLDB_OPTIONS_process_detach
7979dba64beSDimitry Andric #include "CommandOptions.inc"
798*0b57cec5SDimitry Andric 
799*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessDetach
800*0b57cec5SDimitry Andric 
801*0b57cec5SDimitry Andric class CommandObjectProcessDetach : public CommandObjectParsed {
802*0b57cec5SDimitry Andric public:
803*0b57cec5SDimitry Andric   class CommandOptions : public Options {
804*0b57cec5SDimitry Andric   public:
80504eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
806*0b57cec5SDimitry Andric 
807*0b57cec5SDimitry Andric     ~CommandOptions() override = default;
808*0b57cec5SDimitry Andric 
809*0b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
810*0b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
811*0b57cec5SDimitry Andric       Status error;
812*0b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
813*0b57cec5SDimitry Andric 
814*0b57cec5SDimitry Andric       switch (short_option) {
815*0b57cec5SDimitry Andric       case 's':
816*0b57cec5SDimitry Andric         bool tmp_result;
817*0b57cec5SDimitry Andric         bool success;
818*0b57cec5SDimitry Andric         tmp_result = OptionArgParser::ToBoolean(option_arg, false, &success);
819*0b57cec5SDimitry Andric         if (!success)
820*0b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid boolean option: \"%s\"",
821*0b57cec5SDimitry Andric                                          option_arg.str().c_str());
822*0b57cec5SDimitry Andric         else {
823*0b57cec5SDimitry Andric           if (tmp_result)
824*0b57cec5SDimitry Andric             m_keep_stopped = eLazyBoolYes;
825*0b57cec5SDimitry Andric           else
826*0b57cec5SDimitry Andric             m_keep_stopped = eLazyBoolNo;
827*0b57cec5SDimitry Andric         }
828*0b57cec5SDimitry Andric         break;
829*0b57cec5SDimitry Andric       default:
8309dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
831*0b57cec5SDimitry Andric       }
832*0b57cec5SDimitry Andric       return error;
833*0b57cec5SDimitry Andric     }
834*0b57cec5SDimitry Andric 
835*0b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
836*0b57cec5SDimitry Andric       m_keep_stopped = eLazyBoolCalculate;
837*0b57cec5SDimitry Andric     }
838*0b57cec5SDimitry Andric 
839*0b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
840*0b57cec5SDimitry Andric       return llvm::makeArrayRef(g_process_detach_options);
841*0b57cec5SDimitry Andric     }
842*0b57cec5SDimitry Andric 
843*0b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
844*0b57cec5SDimitry Andric     LazyBool m_keep_stopped;
845*0b57cec5SDimitry Andric   };
846*0b57cec5SDimitry Andric 
847*0b57cec5SDimitry Andric   CommandObjectProcessDetach(CommandInterpreter &interpreter)
848*0b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "process detach",
849*0b57cec5SDimitry Andric                             "Detach from the current target process.",
850*0b57cec5SDimitry Andric                             "process detach",
851*0b57cec5SDimitry Andric                             eCommandRequiresProcess | eCommandTryTargetAPILock |
85204eeddc0SDimitry Andric                                 eCommandProcessMustBeLaunched) {}
853*0b57cec5SDimitry Andric 
854*0b57cec5SDimitry Andric   ~CommandObjectProcessDetach() override = default;
855*0b57cec5SDimitry Andric 
856*0b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
857*0b57cec5SDimitry Andric 
858*0b57cec5SDimitry Andric protected:
859*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
860*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
861*0b57cec5SDimitry Andric     // FIXME: This will be a Command Option:
862*0b57cec5SDimitry Andric     bool keep_stopped;
863*0b57cec5SDimitry Andric     if (m_options.m_keep_stopped == eLazyBoolCalculate) {
864*0b57cec5SDimitry Andric       // Check the process default:
865*0b57cec5SDimitry Andric       keep_stopped = process->GetDetachKeepsStopped();
866*0b57cec5SDimitry Andric     } else if (m_options.m_keep_stopped == eLazyBoolYes)
867*0b57cec5SDimitry Andric       keep_stopped = true;
868*0b57cec5SDimitry Andric     else
869*0b57cec5SDimitry Andric       keep_stopped = false;
870*0b57cec5SDimitry Andric 
871*0b57cec5SDimitry Andric     Status error(process->Detach(keep_stopped));
872*0b57cec5SDimitry Andric     if (error.Success()) {
873*0b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
874*0b57cec5SDimitry Andric     } else {
875*0b57cec5SDimitry Andric       result.AppendErrorWithFormat("Detach failed: %s\n", error.AsCString());
876*0b57cec5SDimitry Andric       return false;
877*0b57cec5SDimitry Andric     }
878*0b57cec5SDimitry Andric     return result.Succeeded();
879*0b57cec5SDimitry Andric   }
880*0b57cec5SDimitry Andric 
881*0b57cec5SDimitry Andric   CommandOptions m_options;
882*0b57cec5SDimitry Andric };
883*0b57cec5SDimitry Andric 
884*0b57cec5SDimitry Andric // CommandObjectProcessConnect
8859dba64beSDimitry Andric #define LLDB_OPTIONS_process_connect
8869dba64beSDimitry Andric #include "CommandOptions.inc"
887*0b57cec5SDimitry Andric 
888*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessConnect
889*0b57cec5SDimitry Andric 
890*0b57cec5SDimitry Andric class CommandObjectProcessConnect : public CommandObjectParsed {
891*0b57cec5SDimitry Andric public:
892*0b57cec5SDimitry Andric   class CommandOptions : public Options {
893*0b57cec5SDimitry Andric   public:
89404eeddc0SDimitry Andric     CommandOptions() {
895*0b57cec5SDimitry Andric       // Keep default values of all options in one place: OptionParsingStarting
896*0b57cec5SDimitry Andric       // ()
897*0b57cec5SDimitry Andric       OptionParsingStarting(nullptr);
898*0b57cec5SDimitry Andric     }
899*0b57cec5SDimitry Andric 
900*0b57cec5SDimitry Andric     ~CommandOptions() override = default;
901*0b57cec5SDimitry Andric 
902*0b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
903*0b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
904*0b57cec5SDimitry Andric       Status error;
905*0b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
906*0b57cec5SDimitry Andric 
907*0b57cec5SDimitry Andric       switch (short_option) {
908*0b57cec5SDimitry Andric       case 'p':
9095ffd83dbSDimitry Andric         plugin_name.assign(std::string(option_arg));
910*0b57cec5SDimitry Andric         break;
911*0b57cec5SDimitry Andric 
912*0b57cec5SDimitry Andric       default:
9139dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
914*0b57cec5SDimitry Andric       }
915*0b57cec5SDimitry Andric       return error;
916*0b57cec5SDimitry Andric     }
917*0b57cec5SDimitry Andric 
918*0b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
919*0b57cec5SDimitry Andric       plugin_name.clear();
920*0b57cec5SDimitry Andric     }
921*0b57cec5SDimitry Andric 
922*0b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
923*0b57cec5SDimitry Andric       return llvm::makeArrayRef(g_process_connect_options);
924*0b57cec5SDimitry Andric     }
925*0b57cec5SDimitry Andric 
926*0b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
927*0b57cec5SDimitry Andric 
928*0b57cec5SDimitry Andric     std::string plugin_name;
929*0b57cec5SDimitry Andric   };
930*0b57cec5SDimitry Andric 
931*0b57cec5SDimitry Andric   CommandObjectProcessConnect(CommandInterpreter &interpreter)
932*0b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "process connect",
933*0b57cec5SDimitry Andric                             "Connect to a remote debug service.",
93481ad6265SDimitry Andric                             "process connect <remote-url>", 0) {
93581ad6265SDimitry Andric     CommandArgumentData connect_arg{eArgTypeConnectURL, eArgRepeatPlain};
93681ad6265SDimitry Andric     m_arguments.push_back({connect_arg});
93781ad6265SDimitry Andric   }
938*0b57cec5SDimitry Andric 
939*0b57cec5SDimitry Andric   ~CommandObjectProcessConnect() override = default;
940*0b57cec5SDimitry Andric 
941*0b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
942*0b57cec5SDimitry Andric 
943*0b57cec5SDimitry Andric protected:
944*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
945*0b57cec5SDimitry Andric     if (command.GetArgumentCount() != 1) {
946*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
947*0b57cec5SDimitry Andric           "'%s' takes exactly one argument:\nUsage: %s\n", m_cmd_name.c_str(),
948*0b57cec5SDimitry Andric           m_cmd_syntax.c_str());
949*0b57cec5SDimitry Andric       return false;
950*0b57cec5SDimitry Andric     }
951*0b57cec5SDimitry Andric 
952*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
953*0b57cec5SDimitry Andric     if (process && process->IsAlive()) {
954*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
955*0b57cec5SDimitry Andric           "Process %" PRIu64
956*0b57cec5SDimitry Andric           " is currently being debugged, kill the process before connecting.\n",
957*0b57cec5SDimitry Andric           process->GetID());
958*0b57cec5SDimitry Andric       return false;
959*0b57cec5SDimitry Andric     }
960*0b57cec5SDimitry Andric 
961*0b57cec5SDimitry Andric     const char *plugin_name = nullptr;
962*0b57cec5SDimitry Andric     if (!m_options.plugin_name.empty())
963*0b57cec5SDimitry Andric       plugin_name = m_options.plugin_name.c_str();
964*0b57cec5SDimitry Andric 
965*0b57cec5SDimitry Andric     Status error;
966*0b57cec5SDimitry Andric     Debugger &debugger = GetDebugger();
967*0b57cec5SDimitry Andric     PlatformSP platform_sp = m_interpreter.GetPlatform(true);
9685ffd83dbSDimitry Andric     ProcessSP process_sp =
9695ffd83dbSDimitry Andric         debugger.GetAsyncExecution()
9705ffd83dbSDimitry Andric             ? platform_sp->ConnectProcess(
971*0b57cec5SDimitry Andric                   command.GetArgumentAtIndex(0), plugin_name, debugger,
9725ffd83dbSDimitry Andric                   debugger.GetSelectedTarget().get(), error)
9735ffd83dbSDimitry Andric             : platform_sp->ConnectProcessSynchronous(
9745ffd83dbSDimitry Andric                   command.GetArgumentAtIndex(0), plugin_name, debugger,
9755ffd83dbSDimitry Andric                   result.GetOutputStream(), debugger.GetSelectedTarget().get(),
9765ffd83dbSDimitry Andric                   error);
977*0b57cec5SDimitry Andric     if (error.Fail() || process_sp == nullptr) {
978*0b57cec5SDimitry Andric       result.AppendError(error.AsCString("Error connecting to the process"));
979*0b57cec5SDimitry Andric       return false;
980*0b57cec5SDimitry Andric     }
981*0b57cec5SDimitry Andric     return true;
982*0b57cec5SDimitry Andric   }
983*0b57cec5SDimitry Andric 
984*0b57cec5SDimitry Andric   CommandOptions m_options;
985*0b57cec5SDimitry Andric };
986*0b57cec5SDimitry Andric 
987*0b57cec5SDimitry Andric // CommandObjectProcessPlugin
988*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessPlugin
989*0b57cec5SDimitry Andric 
990*0b57cec5SDimitry Andric class CommandObjectProcessPlugin : public CommandObjectProxy {
991*0b57cec5SDimitry Andric public:
992*0b57cec5SDimitry Andric   CommandObjectProcessPlugin(CommandInterpreter &interpreter)
993*0b57cec5SDimitry Andric       : CommandObjectProxy(
994*0b57cec5SDimitry Andric             interpreter, "process plugin",
995*0b57cec5SDimitry Andric             "Send a custom command to the current target process plug-in.",
996*0b57cec5SDimitry Andric             "process plugin <args>", 0) {}
997*0b57cec5SDimitry Andric 
998*0b57cec5SDimitry Andric   ~CommandObjectProcessPlugin() override = default;
999*0b57cec5SDimitry Andric 
1000*0b57cec5SDimitry Andric   CommandObject *GetProxyCommandObject() override {
1001*0b57cec5SDimitry Andric     Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
1002*0b57cec5SDimitry Andric     if (process)
1003*0b57cec5SDimitry Andric       return process->GetPluginCommandObject();
1004*0b57cec5SDimitry Andric     return nullptr;
1005*0b57cec5SDimitry Andric   }
1006*0b57cec5SDimitry Andric };
1007*0b57cec5SDimitry Andric 
1008*0b57cec5SDimitry Andric // CommandObjectProcessLoad
10099dba64beSDimitry Andric #define LLDB_OPTIONS_process_load
10109dba64beSDimitry Andric #include "CommandOptions.inc"
1011*0b57cec5SDimitry Andric 
1012*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessLoad
1013*0b57cec5SDimitry Andric 
1014*0b57cec5SDimitry Andric class CommandObjectProcessLoad : public CommandObjectParsed {
1015*0b57cec5SDimitry Andric public:
1016*0b57cec5SDimitry Andric   class CommandOptions : public Options {
1017*0b57cec5SDimitry Andric   public:
101804eeddc0SDimitry Andric     CommandOptions() {
1019*0b57cec5SDimitry Andric       // Keep default values of all options in one place: OptionParsingStarting
1020*0b57cec5SDimitry Andric       // ()
1021*0b57cec5SDimitry Andric       OptionParsingStarting(nullptr);
1022*0b57cec5SDimitry Andric     }
1023*0b57cec5SDimitry Andric 
1024*0b57cec5SDimitry Andric     ~CommandOptions() override = default;
1025*0b57cec5SDimitry Andric 
1026*0b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1027*0b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
1028*0b57cec5SDimitry Andric       Status error;
1029*0b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
1030*0b57cec5SDimitry Andric       switch (short_option) {
1031*0b57cec5SDimitry Andric       case 'i':
1032*0b57cec5SDimitry Andric         do_install = true;
1033*0b57cec5SDimitry Andric         if (!option_arg.empty())
1034*0b57cec5SDimitry Andric           install_path.SetFile(option_arg, FileSpec::Style::native);
1035*0b57cec5SDimitry Andric         break;
1036*0b57cec5SDimitry Andric       default:
10379dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
1038*0b57cec5SDimitry Andric       }
1039*0b57cec5SDimitry Andric       return error;
1040*0b57cec5SDimitry Andric     }
1041*0b57cec5SDimitry Andric 
1042*0b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
1043*0b57cec5SDimitry Andric       do_install = false;
1044*0b57cec5SDimitry Andric       install_path.Clear();
1045*0b57cec5SDimitry Andric     }
1046*0b57cec5SDimitry Andric 
1047*0b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1048*0b57cec5SDimitry Andric       return llvm::makeArrayRef(g_process_load_options);
1049*0b57cec5SDimitry Andric     }
1050*0b57cec5SDimitry Andric 
1051*0b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
1052*0b57cec5SDimitry Andric     bool do_install;
1053*0b57cec5SDimitry Andric     FileSpec install_path;
1054*0b57cec5SDimitry Andric   };
1055*0b57cec5SDimitry Andric 
1056*0b57cec5SDimitry Andric   CommandObjectProcessLoad(CommandInterpreter &interpreter)
1057*0b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "process load",
1058*0b57cec5SDimitry Andric                             "Load a shared library into the current process.",
1059*0b57cec5SDimitry Andric                             "process load <filename> [<filename> ...]",
1060*0b57cec5SDimitry Andric                             eCommandRequiresProcess | eCommandTryTargetAPILock |
1061*0b57cec5SDimitry Andric                                 eCommandProcessMustBeLaunched |
106281ad6265SDimitry Andric                                 eCommandProcessMustBePaused) {
106381ad6265SDimitry Andric     CommandArgumentData file_arg{eArgTypePath, eArgRepeatPlus};
106481ad6265SDimitry Andric     m_arguments.push_back({file_arg});
106581ad6265SDimitry Andric   }
1066*0b57cec5SDimitry Andric 
1067*0b57cec5SDimitry Andric   ~CommandObjectProcessLoad() override = default;
1068*0b57cec5SDimitry Andric 
1069e8d8bef9SDimitry Andric   void
1070e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
1071e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
1072e8d8bef9SDimitry Andric     if (!m_exe_ctx.HasProcessScope())
1073e8d8bef9SDimitry Andric       return;
1074e8d8bef9SDimitry Andric 
1075e8d8bef9SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
1076e8d8bef9SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
1077e8d8bef9SDimitry Andric         request, nullptr);
1078e8d8bef9SDimitry Andric   }
1079e8d8bef9SDimitry Andric 
1080*0b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
1081*0b57cec5SDimitry Andric 
1082*0b57cec5SDimitry Andric protected:
1083*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
1084*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
1085*0b57cec5SDimitry Andric 
1086*0b57cec5SDimitry Andric     for (auto &entry : command.entries()) {
1087*0b57cec5SDimitry Andric       Status error;
1088*0b57cec5SDimitry Andric       PlatformSP platform = process->GetTarget().GetPlatform();
10899dba64beSDimitry Andric       llvm::StringRef image_path = entry.ref();
1090*0b57cec5SDimitry Andric       uint32_t image_token = LLDB_INVALID_IMAGE_TOKEN;
1091*0b57cec5SDimitry Andric 
1092*0b57cec5SDimitry Andric       if (!m_options.do_install) {
1093*0b57cec5SDimitry Andric         FileSpec image_spec(image_path);
1094*0b57cec5SDimitry Andric         platform->ResolveRemotePath(image_spec, image_spec);
1095*0b57cec5SDimitry Andric         image_token =
1096*0b57cec5SDimitry Andric             platform->LoadImage(process, FileSpec(), image_spec, error);
1097*0b57cec5SDimitry Andric       } else if (m_options.install_path) {
1098*0b57cec5SDimitry Andric         FileSpec image_spec(image_path);
1099*0b57cec5SDimitry Andric         FileSystem::Instance().Resolve(image_spec);
1100*0b57cec5SDimitry Andric         platform->ResolveRemotePath(m_options.install_path,
1101*0b57cec5SDimitry Andric                                     m_options.install_path);
1102*0b57cec5SDimitry Andric         image_token = platform->LoadImage(process, image_spec,
1103*0b57cec5SDimitry Andric                                           m_options.install_path, error);
1104*0b57cec5SDimitry Andric       } else {
1105*0b57cec5SDimitry Andric         FileSpec image_spec(image_path);
1106*0b57cec5SDimitry Andric         FileSystem::Instance().Resolve(image_spec);
1107*0b57cec5SDimitry Andric         image_token =
1108*0b57cec5SDimitry Andric             platform->LoadImage(process, image_spec, FileSpec(), error);
1109*0b57cec5SDimitry Andric       }
1110*0b57cec5SDimitry Andric 
1111*0b57cec5SDimitry Andric       if (image_token != LLDB_INVALID_IMAGE_TOKEN) {
1112*0b57cec5SDimitry Andric         result.AppendMessageWithFormat(
1113*0b57cec5SDimitry Andric             "Loading \"%s\"...ok\nImage %u loaded.\n", image_path.str().c_str(),
1114*0b57cec5SDimitry Andric             image_token);
1115*0b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishResult);
1116*0b57cec5SDimitry Andric       } else {
1117*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("failed to load '%s': %s",
1118*0b57cec5SDimitry Andric                                      image_path.str().c_str(),
1119*0b57cec5SDimitry Andric                                      error.AsCString());
1120*0b57cec5SDimitry Andric       }
1121*0b57cec5SDimitry Andric     }
1122*0b57cec5SDimitry Andric     return result.Succeeded();
1123*0b57cec5SDimitry Andric   }
1124*0b57cec5SDimitry Andric 
1125*0b57cec5SDimitry Andric   CommandOptions m_options;
1126*0b57cec5SDimitry Andric };
1127*0b57cec5SDimitry Andric 
1128*0b57cec5SDimitry Andric // CommandObjectProcessUnload
1129*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessUnload
1130*0b57cec5SDimitry Andric 
1131*0b57cec5SDimitry Andric class CommandObjectProcessUnload : public CommandObjectParsed {
1132*0b57cec5SDimitry Andric public:
1133*0b57cec5SDimitry Andric   CommandObjectProcessUnload(CommandInterpreter &interpreter)
1134*0b57cec5SDimitry Andric       : CommandObjectParsed(
1135*0b57cec5SDimitry Andric             interpreter, "process unload",
1136*0b57cec5SDimitry Andric             "Unload a shared library from the current process using the index "
1137*0b57cec5SDimitry Andric             "returned by a previous call to \"process load\".",
1138*0b57cec5SDimitry Andric             "process unload <index>",
1139*0b57cec5SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
114081ad6265SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {
114181ad6265SDimitry Andric     CommandArgumentData load_idx_arg{eArgTypeUnsignedInteger, eArgRepeatPlain};
114281ad6265SDimitry Andric     m_arguments.push_back({load_idx_arg});
114381ad6265SDimitry Andric   }
1144*0b57cec5SDimitry Andric 
1145*0b57cec5SDimitry Andric   ~CommandObjectProcessUnload() override = default;
1146*0b57cec5SDimitry Andric 
1147e8d8bef9SDimitry Andric   void
1148e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
1149e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
1150e8d8bef9SDimitry Andric 
1151e8d8bef9SDimitry Andric     if (request.GetCursorIndex() || !m_exe_ctx.HasProcessScope())
1152e8d8bef9SDimitry Andric       return;
1153e8d8bef9SDimitry Andric 
1154e8d8bef9SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
1155e8d8bef9SDimitry Andric 
1156e8d8bef9SDimitry Andric     const std::vector<lldb::addr_t> &tokens = process->GetImageTokens();
1157e8d8bef9SDimitry Andric     const size_t token_num = tokens.size();
1158e8d8bef9SDimitry Andric     for (size_t i = 0; i < token_num; ++i) {
1159e8d8bef9SDimitry Andric       if (tokens[i] == LLDB_INVALID_IMAGE_TOKEN)
1160e8d8bef9SDimitry Andric         continue;
1161e8d8bef9SDimitry Andric       request.TryCompleteCurrentArg(std::to_string(i));
1162e8d8bef9SDimitry Andric     }
1163e8d8bef9SDimitry Andric   }
1164e8d8bef9SDimitry Andric 
1165*0b57cec5SDimitry Andric protected:
1166*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
1167*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
1168*0b57cec5SDimitry Andric 
1169*0b57cec5SDimitry Andric     for (auto &entry : command.entries()) {
1170*0b57cec5SDimitry Andric       uint32_t image_token;
11719dba64beSDimitry Andric       if (entry.ref().getAsInteger(0, image_token)) {
1172*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("invalid image index argument '%s'",
11739dba64beSDimitry Andric                                      entry.ref().str().c_str());
1174*0b57cec5SDimitry Andric         break;
1175*0b57cec5SDimitry Andric       } else {
1176*0b57cec5SDimitry Andric         Status error(process->GetTarget().GetPlatform()->UnloadImage(
1177*0b57cec5SDimitry Andric             process, image_token));
1178*0b57cec5SDimitry Andric         if (error.Success()) {
1179*0b57cec5SDimitry Andric           result.AppendMessageWithFormat(
1180*0b57cec5SDimitry Andric               "Unloading shared library with index %u...ok\n", image_token);
1181*0b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessFinishResult);
1182*0b57cec5SDimitry Andric         } else {
1183*0b57cec5SDimitry Andric           result.AppendErrorWithFormat("failed to unload image: %s",
1184*0b57cec5SDimitry Andric                                        error.AsCString());
1185*0b57cec5SDimitry Andric           break;
1186*0b57cec5SDimitry Andric         }
1187*0b57cec5SDimitry Andric       }
1188*0b57cec5SDimitry Andric     }
1189*0b57cec5SDimitry Andric     return result.Succeeded();
1190*0b57cec5SDimitry Andric   }
1191*0b57cec5SDimitry Andric };
1192*0b57cec5SDimitry Andric 
1193*0b57cec5SDimitry Andric // CommandObjectProcessSignal
1194*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessSignal
1195*0b57cec5SDimitry Andric 
1196*0b57cec5SDimitry Andric class CommandObjectProcessSignal : public CommandObjectParsed {
1197*0b57cec5SDimitry Andric public:
1198*0b57cec5SDimitry Andric   CommandObjectProcessSignal(CommandInterpreter &interpreter)
1199480093f4SDimitry Andric       : CommandObjectParsed(
1200480093f4SDimitry Andric             interpreter, "process signal",
1201480093f4SDimitry Andric             "Send a UNIX signal to the current target process.", nullptr,
1202480093f4SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock) {
1203*0b57cec5SDimitry Andric     CommandArgumentEntry arg;
1204*0b57cec5SDimitry Andric     CommandArgumentData signal_arg;
1205*0b57cec5SDimitry Andric 
1206*0b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
1207*0b57cec5SDimitry Andric     signal_arg.arg_type = eArgTypeUnixSignal;
1208*0b57cec5SDimitry Andric     signal_arg.arg_repetition = eArgRepeatPlain;
1209*0b57cec5SDimitry Andric 
1210*0b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
1211*0b57cec5SDimitry Andric     // argument entry.
1212*0b57cec5SDimitry Andric     arg.push_back(signal_arg);
1213*0b57cec5SDimitry Andric 
1214*0b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
1215*0b57cec5SDimitry Andric     m_arguments.push_back(arg);
1216*0b57cec5SDimitry Andric   }
1217*0b57cec5SDimitry Andric 
1218*0b57cec5SDimitry Andric   ~CommandObjectProcessSignal() override = default;
1219*0b57cec5SDimitry Andric 
12205ffd83dbSDimitry Andric   void
12215ffd83dbSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
12225ffd83dbSDimitry Andric                            OptionElementVector &opt_element_vector) override {
12235ffd83dbSDimitry Andric     if (!m_exe_ctx.HasProcessScope() || request.GetCursorIndex() != 0)
12245ffd83dbSDimitry Andric       return;
12255ffd83dbSDimitry Andric 
12265ffd83dbSDimitry Andric     UnixSignalsSP signals = m_exe_ctx.GetProcessPtr()->GetUnixSignals();
12275ffd83dbSDimitry Andric     int signo = signals->GetFirstSignalNumber();
12285ffd83dbSDimitry Andric     while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1229fe6060f1SDimitry Andric       request.TryCompleteCurrentArg(signals->GetSignalAsCString(signo));
12305ffd83dbSDimitry Andric       signo = signals->GetNextSignalNumber(signo);
12315ffd83dbSDimitry Andric     }
12325ffd83dbSDimitry Andric   }
12335ffd83dbSDimitry Andric 
1234*0b57cec5SDimitry Andric protected:
1235*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
1236*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
1237*0b57cec5SDimitry Andric 
1238*0b57cec5SDimitry Andric     if (command.GetArgumentCount() == 1) {
1239*0b57cec5SDimitry Andric       int signo = LLDB_INVALID_SIGNAL_NUMBER;
1240*0b57cec5SDimitry Andric 
1241*0b57cec5SDimitry Andric       const char *signal_name = command.GetArgumentAtIndex(0);
12425ffd83dbSDimitry Andric       if (::isxdigit(signal_name[0])) {
12435ffd83dbSDimitry Andric         if (!llvm::to_integer(signal_name, signo))
12445ffd83dbSDimitry Andric           signo = LLDB_INVALID_SIGNAL_NUMBER;
12455ffd83dbSDimitry Andric       } else
1246*0b57cec5SDimitry Andric         signo = process->GetUnixSignals()->GetSignalNumberFromName(signal_name);
1247*0b57cec5SDimitry Andric 
1248*0b57cec5SDimitry Andric       if (signo == LLDB_INVALID_SIGNAL_NUMBER) {
1249*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("Invalid signal argument '%s'.\n",
1250*0b57cec5SDimitry Andric                                      command.GetArgumentAtIndex(0));
1251*0b57cec5SDimitry Andric       } else {
1252*0b57cec5SDimitry Andric         Status error(process->Signal(signo));
1253*0b57cec5SDimitry Andric         if (error.Success()) {
1254*0b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessFinishResult);
1255*0b57cec5SDimitry Andric         } else {
1256*0b57cec5SDimitry Andric           result.AppendErrorWithFormat("Failed to send signal %i: %s\n", signo,
1257*0b57cec5SDimitry Andric                                        error.AsCString());
1258*0b57cec5SDimitry Andric         }
1259*0b57cec5SDimitry Andric       }
1260*0b57cec5SDimitry Andric     } else {
1261*0b57cec5SDimitry Andric       result.AppendErrorWithFormat(
1262*0b57cec5SDimitry Andric           "'%s' takes exactly one signal number argument:\nUsage: %s\n",
1263*0b57cec5SDimitry Andric           m_cmd_name.c_str(), m_cmd_syntax.c_str());
1264*0b57cec5SDimitry Andric     }
1265*0b57cec5SDimitry Andric     return result.Succeeded();
1266*0b57cec5SDimitry Andric   }
1267*0b57cec5SDimitry Andric };
1268*0b57cec5SDimitry Andric 
1269*0b57cec5SDimitry Andric // CommandObjectProcessInterrupt
1270*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessInterrupt
1271*0b57cec5SDimitry Andric 
1272*0b57cec5SDimitry Andric class CommandObjectProcessInterrupt : public CommandObjectParsed {
1273*0b57cec5SDimitry Andric public:
1274*0b57cec5SDimitry Andric   CommandObjectProcessInterrupt(CommandInterpreter &interpreter)
1275*0b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "process interrupt",
1276*0b57cec5SDimitry Andric                             "Interrupt the current target process.",
1277*0b57cec5SDimitry Andric                             "process interrupt",
1278*0b57cec5SDimitry Andric                             eCommandRequiresProcess | eCommandTryTargetAPILock |
1279*0b57cec5SDimitry Andric                                 eCommandProcessMustBeLaunched) {}
1280*0b57cec5SDimitry Andric 
1281*0b57cec5SDimitry Andric   ~CommandObjectProcessInterrupt() override = default;
1282*0b57cec5SDimitry Andric 
1283*0b57cec5SDimitry Andric protected:
1284*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
1285*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
1286*0b57cec5SDimitry Andric     if (process == nullptr) {
1287*0b57cec5SDimitry Andric       result.AppendError("no process to halt");
1288*0b57cec5SDimitry Andric       return false;
1289*0b57cec5SDimitry Andric     }
1290*0b57cec5SDimitry Andric 
1291*0b57cec5SDimitry Andric     bool clear_thread_plans = true;
1292*0b57cec5SDimitry Andric     Status error(process->Halt(clear_thread_plans));
1293*0b57cec5SDimitry Andric     if (error.Success()) {
1294*0b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
1295*0b57cec5SDimitry Andric     } else {
1296*0b57cec5SDimitry Andric       result.AppendErrorWithFormat("Failed to halt process: %s\n",
1297*0b57cec5SDimitry Andric                                    error.AsCString());
1298*0b57cec5SDimitry Andric     }
1299*0b57cec5SDimitry Andric     return result.Succeeded();
1300*0b57cec5SDimitry Andric   }
1301*0b57cec5SDimitry Andric };
1302*0b57cec5SDimitry Andric 
1303*0b57cec5SDimitry Andric // CommandObjectProcessKill
1304*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessKill
1305*0b57cec5SDimitry Andric 
1306*0b57cec5SDimitry Andric class CommandObjectProcessKill : public CommandObjectParsed {
1307*0b57cec5SDimitry Andric public:
1308*0b57cec5SDimitry Andric   CommandObjectProcessKill(CommandInterpreter &interpreter)
1309*0b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "process kill",
1310*0b57cec5SDimitry Andric                             "Terminate the current target process.",
1311*0b57cec5SDimitry Andric                             "process kill",
1312*0b57cec5SDimitry Andric                             eCommandRequiresProcess | eCommandTryTargetAPILock |
1313*0b57cec5SDimitry Andric                                 eCommandProcessMustBeLaunched) {}
1314*0b57cec5SDimitry Andric 
1315*0b57cec5SDimitry Andric   ~CommandObjectProcessKill() override = default;
1316*0b57cec5SDimitry Andric 
1317*0b57cec5SDimitry Andric protected:
1318*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
1319*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
1320*0b57cec5SDimitry Andric     if (process == nullptr) {
1321*0b57cec5SDimitry Andric       result.AppendError("no process to kill");
1322*0b57cec5SDimitry Andric       return false;
1323*0b57cec5SDimitry Andric     }
1324*0b57cec5SDimitry Andric 
1325*0b57cec5SDimitry Andric     Status error(process->Destroy(true));
1326*0b57cec5SDimitry Andric     if (error.Success()) {
1327*0b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
1328*0b57cec5SDimitry Andric     } else {
1329*0b57cec5SDimitry Andric       result.AppendErrorWithFormat("Failed to kill process: %s\n",
1330*0b57cec5SDimitry Andric                                    error.AsCString());
1331*0b57cec5SDimitry Andric     }
1332*0b57cec5SDimitry Andric     return result.Succeeded();
1333*0b57cec5SDimitry Andric   }
1334*0b57cec5SDimitry Andric };
1335*0b57cec5SDimitry Andric 
1336fe6060f1SDimitry Andric #define LLDB_OPTIONS_process_save_core
1337fe6060f1SDimitry Andric #include "CommandOptions.inc"
1338fe6060f1SDimitry Andric 
1339*0b57cec5SDimitry Andric class CommandObjectProcessSaveCore : public CommandObjectParsed {
1340*0b57cec5SDimitry Andric public:
1341*0b57cec5SDimitry Andric   CommandObjectProcessSaveCore(CommandInterpreter &interpreter)
1342349cc55cSDimitry Andric       : CommandObjectParsed(
1343349cc55cSDimitry Andric             interpreter, "process save-core",
1344*0b57cec5SDimitry Andric             "Save the current process as a core file using an "
1345*0b57cec5SDimitry Andric             "appropriate file type.",
1346349cc55cSDimitry Andric             "process save-core [-s corefile-style -p plugin-name] FILE",
1347*0b57cec5SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
134881ad6265SDimitry Andric                 eCommandProcessMustBeLaunched) {
134981ad6265SDimitry Andric     CommandArgumentData file_arg{eArgTypePath, eArgRepeatPlain};
135081ad6265SDimitry Andric     m_arguments.push_back({file_arg});
135181ad6265SDimitry Andric   }
1352*0b57cec5SDimitry Andric 
1353*0b57cec5SDimitry Andric   ~CommandObjectProcessSaveCore() override = default;
1354*0b57cec5SDimitry Andric 
1355fe6060f1SDimitry Andric   Options *GetOptions() override { return &m_options; }
1356fe6060f1SDimitry Andric 
1357fe6060f1SDimitry Andric   class CommandOptions : public Options {
1358fe6060f1SDimitry Andric   public:
135981ad6265SDimitry Andric     CommandOptions() = default;
1360fe6060f1SDimitry Andric 
1361fe6060f1SDimitry Andric     ~CommandOptions() override = default;
1362fe6060f1SDimitry Andric 
1363fe6060f1SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1364fe6060f1SDimitry Andric       return llvm::makeArrayRef(g_process_save_core_options);
1365fe6060f1SDimitry Andric     }
1366fe6060f1SDimitry Andric 
1367fe6060f1SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1368fe6060f1SDimitry Andric                           ExecutionContext *execution_context) override {
1369fe6060f1SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
1370fe6060f1SDimitry Andric       Status error;
1371fe6060f1SDimitry Andric 
1372fe6060f1SDimitry Andric       switch (short_option) {
1373349cc55cSDimitry Andric       case 'p':
1374349cc55cSDimitry Andric         m_requested_plugin_name = option_arg.str();
1375349cc55cSDimitry Andric         break;
1376fe6060f1SDimitry Andric       case 's':
1377fe6060f1SDimitry Andric         m_requested_save_core_style =
1378fe6060f1SDimitry Andric             (lldb::SaveCoreStyle)OptionArgParser::ToOptionEnum(
1379fe6060f1SDimitry Andric                 option_arg, GetDefinitions()[option_idx].enum_values,
1380fe6060f1SDimitry Andric                 eSaveCoreUnspecified, error);
1381fe6060f1SDimitry Andric         break;
1382fe6060f1SDimitry Andric       default:
1383fe6060f1SDimitry Andric         llvm_unreachable("Unimplemented option");
1384fe6060f1SDimitry Andric       }
1385fe6060f1SDimitry Andric 
1386fe6060f1SDimitry Andric       return {};
1387fe6060f1SDimitry Andric     }
1388fe6060f1SDimitry Andric 
1389fe6060f1SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
1390fe6060f1SDimitry Andric       m_requested_save_core_style = eSaveCoreUnspecified;
1391349cc55cSDimitry Andric       m_requested_plugin_name.clear();
1392fe6060f1SDimitry Andric     }
1393fe6060f1SDimitry Andric 
1394fe6060f1SDimitry Andric     // Instance variables to hold the values for command options.
139581ad6265SDimitry Andric     SaveCoreStyle m_requested_save_core_style = eSaveCoreUnspecified;
1396349cc55cSDimitry Andric     std::string m_requested_plugin_name;
1397fe6060f1SDimitry Andric   };
1398fe6060f1SDimitry Andric 
1399*0b57cec5SDimitry Andric protected:
1400*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
1401*0b57cec5SDimitry Andric     ProcessSP process_sp = m_exe_ctx.GetProcessSP();
1402*0b57cec5SDimitry Andric     if (process_sp) {
1403*0b57cec5SDimitry Andric       if (command.GetArgumentCount() == 1) {
1404*0b57cec5SDimitry Andric         FileSpec output_file(command.GetArgumentAtIndex(0));
1405fe6060f1SDimitry Andric         SaveCoreStyle corefile_style = m_options.m_requested_save_core_style;
1406fe6060f1SDimitry Andric         Status error =
1407349cc55cSDimitry Andric             PluginManager::SaveCore(process_sp, output_file, corefile_style,
1408349cc55cSDimitry Andric                                     m_options.m_requested_plugin_name);
1409*0b57cec5SDimitry Andric         if (error.Success()) {
1410349cc55cSDimitry Andric           if (corefile_style == SaveCoreStyle::eSaveCoreDirtyOnly ||
1411349cc55cSDimitry Andric               corefile_style == SaveCoreStyle::eSaveCoreStackOnly) {
1412fe6060f1SDimitry Andric             result.AppendMessageWithFormat(
1413349cc55cSDimitry Andric                 "\nModified-memory or stack-memory only corefile "
1414349cc55cSDimitry Andric                 "created.  This corefile may \n"
1415349cc55cSDimitry Andric                 "not show library/framework/app binaries "
1416fe6060f1SDimitry Andric                 "on a different system, or when \n"
1417fe6060f1SDimitry Andric                 "those binaries have "
1418fe6060f1SDimitry Andric                 "been updated/modified. Copies are not included\n"
1419fe6060f1SDimitry Andric                 "in this corefile.  Use --style full to include all "
1420fe6060f1SDimitry Andric                 "process memory.\n");
1421fe6060f1SDimitry Andric           }
1422*0b57cec5SDimitry Andric           result.SetStatus(eReturnStatusSuccessFinishResult);
1423*0b57cec5SDimitry Andric         } else {
1424*0b57cec5SDimitry Andric           result.AppendErrorWithFormat(
1425*0b57cec5SDimitry Andric               "Failed to save core file for process: %s\n", error.AsCString());
1426*0b57cec5SDimitry Andric         }
1427*0b57cec5SDimitry Andric       } else {
1428*0b57cec5SDimitry Andric         result.AppendErrorWithFormat("'%s' takes one arguments:\nUsage: %s\n",
1429*0b57cec5SDimitry Andric                                      m_cmd_name.c_str(), m_cmd_syntax.c_str());
1430*0b57cec5SDimitry Andric       }
1431*0b57cec5SDimitry Andric     } else {
1432*0b57cec5SDimitry Andric       result.AppendError("invalid process");
1433*0b57cec5SDimitry Andric       return false;
1434*0b57cec5SDimitry Andric     }
1435*0b57cec5SDimitry Andric 
1436*0b57cec5SDimitry Andric     return result.Succeeded();
1437*0b57cec5SDimitry Andric   }
1438fe6060f1SDimitry Andric 
1439fe6060f1SDimitry Andric   CommandOptions m_options;
1440*0b57cec5SDimitry Andric };
1441*0b57cec5SDimitry Andric 
1442*0b57cec5SDimitry Andric // CommandObjectProcessStatus
1443*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessStatus
14445ffd83dbSDimitry Andric #define LLDB_OPTIONS_process_status
14455ffd83dbSDimitry Andric #include "CommandOptions.inc"
1446*0b57cec5SDimitry Andric 
1447*0b57cec5SDimitry Andric class CommandObjectProcessStatus : public CommandObjectParsed {
1448*0b57cec5SDimitry Andric public:
1449*0b57cec5SDimitry Andric   CommandObjectProcessStatus(CommandInterpreter &interpreter)
1450*0b57cec5SDimitry Andric       : CommandObjectParsed(
1451*0b57cec5SDimitry Andric             interpreter, "process status",
1452*0b57cec5SDimitry Andric             "Show status and stop location for the current target process.",
1453*0b57cec5SDimitry Andric             "process status",
145404eeddc0SDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock) {}
1455*0b57cec5SDimitry Andric 
1456*0b57cec5SDimitry Andric   ~CommandObjectProcessStatus() override = default;
1457*0b57cec5SDimitry Andric 
14585ffd83dbSDimitry Andric   Options *GetOptions() override { return &m_options; }
14595ffd83dbSDimitry Andric 
14605ffd83dbSDimitry Andric   class CommandOptions : public Options {
14615ffd83dbSDimitry Andric   public:
146281ad6265SDimitry Andric     CommandOptions() = default;
14635ffd83dbSDimitry Andric 
14645ffd83dbSDimitry Andric     ~CommandOptions() override = default;
14655ffd83dbSDimitry Andric 
14665ffd83dbSDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
14675ffd83dbSDimitry Andric                           ExecutionContext *execution_context) override {
14685ffd83dbSDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
14695ffd83dbSDimitry Andric 
14705ffd83dbSDimitry Andric       switch (short_option) {
14715ffd83dbSDimitry Andric       case 'v':
14725ffd83dbSDimitry Andric         m_verbose = true;
14735ffd83dbSDimitry Andric         break;
14745ffd83dbSDimitry Andric       default:
14755ffd83dbSDimitry Andric         llvm_unreachable("Unimplemented option");
14765ffd83dbSDimitry Andric       }
14775ffd83dbSDimitry Andric 
14785ffd83dbSDimitry Andric       return {};
14795ffd83dbSDimitry Andric     }
14805ffd83dbSDimitry Andric 
14815ffd83dbSDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
14825ffd83dbSDimitry Andric       m_verbose = false;
14835ffd83dbSDimitry Andric     }
14845ffd83dbSDimitry Andric 
14855ffd83dbSDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
14865ffd83dbSDimitry Andric       return llvm::makeArrayRef(g_process_status_options);
14875ffd83dbSDimitry Andric     }
14885ffd83dbSDimitry Andric 
14895ffd83dbSDimitry Andric     // Instance variables to hold the values for command options.
1490fe6060f1SDimitry Andric     bool m_verbose = false;
14915ffd83dbSDimitry Andric   };
14925ffd83dbSDimitry Andric 
14935ffd83dbSDimitry Andric protected:
1494*0b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
1495*0b57cec5SDimitry Andric     Stream &strm = result.GetOutputStream();
1496*0b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishNoResult);
14975ffd83dbSDimitry Andric 
1498*0b57cec5SDimitry Andric     // No need to check "process" for validity as eCommandRequiresProcess
1499*0b57cec5SDimitry Andric     // ensures it is valid
1500*0b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
1501*0b57cec5SDimitry Andric     const bool only_threads_with_stop_reason = true;
1502*0b57cec5SDimitry Andric     const uint32_t start_frame = 0;
1503*0b57cec5SDimitry Andric     const uint32_t num_frames = 1;
1504*0b57cec5SDimitry Andric     const uint32_t num_frames_with_source = 1;
1505*0b57cec5SDimitry Andric     const bool stop_format = true;
1506*0b57cec5SDimitry Andric     process->GetStatus(strm);
1507*0b57cec5SDimitry Andric     process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame,
1508*0b57cec5SDimitry Andric                              num_frames, num_frames_with_source, stop_format);
15095ffd83dbSDimitry Andric 
15105ffd83dbSDimitry Andric     if (m_options.m_verbose) {
1511fe6060f1SDimitry Andric       addr_t code_mask = process->GetCodeAddressMask();
1512fe6060f1SDimitry Andric       addr_t data_mask = process->GetDataAddressMask();
1513fe6060f1SDimitry Andric       if (code_mask != 0) {
1514fe6060f1SDimitry Andric         int bits = std::bitset<64>(~code_mask).count();
1515fe6060f1SDimitry Andric         result.AppendMessageWithFormat(
1516fe6060f1SDimitry Andric             "Addressable code address mask: 0x%" PRIx64 "\n", code_mask);
1517fe6060f1SDimitry Andric         result.AppendMessageWithFormat(
1518fe6060f1SDimitry Andric             "Addressable data address mask: 0x%" PRIx64 "\n", data_mask);
1519fe6060f1SDimitry Andric         result.AppendMessageWithFormat(
1520fe6060f1SDimitry Andric             "Number of bits used in addressing (code): %d\n", bits);
1521fe6060f1SDimitry Andric       }
1522fe6060f1SDimitry Andric 
15235ffd83dbSDimitry Andric       PlatformSP platform_sp = process->GetTarget().GetPlatform();
15245ffd83dbSDimitry Andric       if (!platform_sp) {
15255ffd83dbSDimitry Andric         result.AppendError("Couldn'retrieve the target's platform");
1526*0b57cec5SDimitry Andric         return result.Succeeded();
1527*0b57cec5SDimitry Andric       }
15285ffd83dbSDimitry Andric 
15295ffd83dbSDimitry Andric       auto expected_crash_info =
15305ffd83dbSDimitry Andric           platform_sp->FetchExtendedCrashInformation(*process);
15315ffd83dbSDimitry Andric 
15325ffd83dbSDimitry Andric       if (!expected_crash_info) {
15335ffd83dbSDimitry Andric         result.AppendError(llvm::toString(expected_crash_info.takeError()));
15345ffd83dbSDimitry Andric         return result.Succeeded();
15355ffd83dbSDimitry Andric       }
15365ffd83dbSDimitry Andric 
15375ffd83dbSDimitry Andric       StructuredData::DictionarySP crash_info_sp = *expected_crash_info;
15385ffd83dbSDimitry Andric 
15395ffd83dbSDimitry Andric       if (crash_info_sp) {
15405ffd83dbSDimitry Andric         strm.PutCString("Extended Crash Information:\n");
15415ffd83dbSDimitry Andric         crash_info_sp->Dump(strm);
15425ffd83dbSDimitry Andric       }
15435ffd83dbSDimitry Andric     }
15445ffd83dbSDimitry Andric 
15455ffd83dbSDimitry Andric     return result.Succeeded();
15465ffd83dbSDimitry Andric   }
15475ffd83dbSDimitry Andric 
15485ffd83dbSDimitry Andric private:
15495ffd83dbSDimitry Andric   CommandOptions m_options;
1550*0b57cec5SDimitry Andric };
1551*0b57cec5SDimitry Andric 
1552*0b57cec5SDimitry Andric // CommandObjectProcessHandle
15539dba64beSDimitry Andric #define LLDB_OPTIONS_process_handle
15549dba64beSDimitry Andric #include "CommandOptions.inc"
1555*0b57cec5SDimitry Andric 
1556*0b57cec5SDimitry Andric #pragma mark CommandObjectProcessHandle
1557*0b57cec5SDimitry Andric 
1558*0b57cec5SDimitry Andric class CommandObjectProcessHandle : public CommandObjectParsed {
1559*0b57cec5SDimitry Andric public:
1560*0b57cec5SDimitry Andric   class CommandOptions : public Options {
1561*0b57cec5SDimitry Andric   public:
156204eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
1563*0b57cec5SDimitry Andric 
1564*0b57cec5SDimitry Andric     ~CommandOptions() override = default;
1565*0b57cec5SDimitry Andric 
1566*0b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1567*0b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
1568*0b57cec5SDimitry Andric       Status error;
1569*0b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
1570*0b57cec5SDimitry Andric 
1571*0b57cec5SDimitry Andric       switch (short_option) {
157281ad6265SDimitry Andric       case 'c':
157381ad6265SDimitry Andric         do_clear = true;
157481ad6265SDimitry Andric         break;
157581ad6265SDimitry Andric       case 'd':
157681ad6265SDimitry Andric         dummy = true;
157781ad6265SDimitry Andric         break;
1578*0b57cec5SDimitry Andric       case 's':
15795ffd83dbSDimitry Andric         stop = std::string(option_arg);
1580*0b57cec5SDimitry Andric         break;
1581*0b57cec5SDimitry Andric       case 'n':
15825ffd83dbSDimitry Andric         notify = std::string(option_arg);
1583*0b57cec5SDimitry Andric         break;
1584*0b57cec5SDimitry Andric       case 'p':
15855ffd83dbSDimitry Andric         pass = std::string(option_arg);
1586*0b57cec5SDimitry Andric         break;
158781ad6265SDimitry Andric       case 't':
158881ad6265SDimitry Andric         only_target_values = true;
158981ad6265SDimitry Andric         break;
1590*0b57cec5SDimitry Andric       default:
15919dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
1592*0b57cec5SDimitry Andric       }
1593*0b57cec5SDimitry Andric       return error;
1594*0b57cec5SDimitry Andric     }
1595*0b57cec5SDimitry Andric 
1596*0b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
1597*0b57cec5SDimitry Andric       stop.clear();
1598*0b57cec5SDimitry Andric       notify.clear();
1599*0b57cec5SDimitry Andric       pass.clear();
160081ad6265SDimitry Andric       only_target_values = false;
160181ad6265SDimitry Andric       do_clear = false;
160281ad6265SDimitry Andric       dummy = false;
1603*0b57cec5SDimitry Andric     }
1604*0b57cec5SDimitry Andric 
1605*0b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1606*0b57cec5SDimitry Andric       return llvm::makeArrayRef(g_process_handle_options);
1607*0b57cec5SDimitry Andric     }
1608*0b57cec5SDimitry Andric 
1609*0b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
1610*0b57cec5SDimitry Andric 
1611*0b57cec5SDimitry Andric     std::string stop;
1612*0b57cec5SDimitry Andric     std::string notify;
1613*0b57cec5SDimitry Andric     std::string pass;
161481ad6265SDimitry Andric     bool only_target_values = false;
161581ad6265SDimitry Andric     bool do_clear = false;
161681ad6265SDimitry Andric     bool dummy = false;
1617*0b57cec5SDimitry Andric   };
1618*0b57cec5SDimitry Andric 
1619*0b57cec5SDimitry Andric   CommandObjectProcessHandle(CommandInterpreter &interpreter)
1620*0b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "process handle",
1621*0b57cec5SDimitry Andric                             "Manage LLDB handling of OS signals for the "
1622*0b57cec5SDimitry Andric                             "current target process.  Defaults to showing "
1623*0b57cec5SDimitry Andric                             "current policy.",
162481ad6265SDimitry Andric                             nullptr) {
162581ad6265SDimitry Andric     SetHelpLong("\nIf no signals are specified but one or more actions are, "
162681ad6265SDimitry Andric                 "and there is a live process, update them all.  If no action "
162781ad6265SDimitry Andric                 "is specified, list the current values.\n"
162881ad6265SDimitry Andric                 "If you specify actions with no target (e.g. in an init file) "
162981ad6265SDimitry Andric                 "or in a target with no process "
163081ad6265SDimitry Andric                 "the values will get copied into subsequent targets, but "
163181ad6265SDimitry Andric                 "lldb won't be able to spell-check the options since it can't "
163281ad6265SDimitry Andric                 "know which signal set will later be in force."
163381ad6265SDimitry Andric                 "\nYou can see the signal modifications held by the target"
163481ad6265SDimitry Andric                 "by passing the -t option."
163581ad6265SDimitry Andric                 "\nYou can also clear the target modification for a signal"
163681ad6265SDimitry Andric                 "by passing the -c option");
1637*0b57cec5SDimitry Andric     CommandArgumentEntry arg;
1638*0b57cec5SDimitry Andric     CommandArgumentData signal_arg;
1639*0b57cec5SDimitry Andric 
1640*0b57cec5SDimitry Andric     signal_arg.arg_type = eArgTypeUnixSignal;
1641*0b57cec5SDimitry Andric     signal_arg.arg_repetition = eArgRepeatStar;
1642*0b57cec5SDimitry Andric 
1643*0b57cec5SDimitry Andric     arg.push_back(signal_arg);
1644*0b57cec5SDimitry Andric 
1645*0b57cec5SDimitry Andric     m_arguments.push_back(arg);
1646*0b57cec5SDimitry Andric   }
1647*0b57cec5SDimitry Andric 
1648*0b57cec5SDimitry Andric   ~CommandObjectProcessHandle() override = default;
1649*0b57cec5SDimitry Andric 
1650*0b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
1651*0b57cec5SDimitry Andric 
1652*0b57cec5SDimitry Andric   bool VerifyCommandOptionValue(const std::string &option, int &real_value) {
1653*0b57cec5SDimitry Andric     bool okay = true;
1654*0b57cec5SDimitry Andric     bool success = false;
1655*0b57cec5SDimitry Andric     bool tmp_value = OptionArgParser::ToBoolean(option, false, &success);
1656*0b57cec5SDimitry Andric 
1657*0b57cec5SDimitry Andric     if (success && tmp_value)
1658*0b57cec5SDimitry Andric       real_value = 1;
1659*0b57cec5SDimitry Andric     else if (success && !tmp_value)
1660*0b57cec5SDimitry Andric       real_value = 0;
1661*0b57cec5SDimitry Andric     else {
1662*0b57cec5SDimitry Andric       // If the value isn't 'true' or 'false', it had better be 0 or 1.
16635ffd83dbSDimitry Andric       if (!llvm::to_integer(option, real_value))
16645ffd83dbSDimitry Andric         real_value = 3;
1665*0b57cec5SDimitry Andric       if (real_value != 0 && real_value != 1)
1666*0b57cec5SDimitry Andric         okay = false;
1667*0b57cec5SDimitry Andric     }
1668*0b57cec5SDimitry Andric 
1669*0b57cec5SDimitry Andric     return okay;
1670*0b57cec5SDimitry Andric   }
1671*0b57cec5SDimitry Andric 
1672*0b57cec5SDimitry Andric   void PrintSignalHeader(Stream &str) {
1673*0b57cec5SDimitry Andric     str.Printf("NAME         PASS   STOP   NOTIFY\n");
1674*0b57cec5SDimitry Andric     str.Printf("===========  =====  =====  ======\n");
1675*0b57cec5SDimitry Andric   }
1676*0b57cec5SDimitry Andric 
1677*0b57cec5SDimitry Andric   void PrintSignal(Stream &str, int32_t signo, const char *sig_name,
1678*0b57cec5SDimitry Andric                    const UnixSignalsSP &signals_sp) {
1679*0b57cec5SDimitry Andric     bool stop;
1680*0b57cec5SDimitry Andric     bool suppress;
1681*0b57cec5SDimitry Andric     bool notify;
1682*0b57cec5SDimitry Andric 
1683*0b57cec5SDimitry Andric     str.Printf("%-11s  ", sig_name);
1684*0b57cec5SDimitry Andric     if (signals_sp->GetSignalInfo(signo, suppress, stop, notify)) {
1685*0b57cec5SDimitry Andric       bool pass = !suppress;
1686*0b57cec5SDimitry Andric       str.Printf("%s  %s  %s", (pass ? "true " : "false"),
1687*0b57cec5SDimitry Andric                  (stop ? "true " : "false"), (notify ? "true " : "false"));
1688*0b57cec5SDimitry Andric     }
1689*0b57cec5SDimitry Andric     str.Printf("\n");
1690*0b57cec5SDimitry Andric   }
1691*0b57cec5SDimitry Andric 
1692*0b57cec5SDimitry Andric   void PrintSignalInformation(Stream &str, Args &signal_args,
1693*0b57cec5SDimitry Andric                               int num_valid_signals,
1694*0b57cec5SDimitry Andric                               const UnixSignalsSP &signals_sp) {
1695*0b57cec5SDimitry Andric     PrintSignalHeader(str);
1696*0b57cec5SDimitry Andric 
1697*0b57cec5SDimitry Andric     if (num_valid_signals > 0) {
1698*0b57cec5SDimitry Andric       size_t num_args = signal_args.GetArgumentCount();
1699*0b57cec5SDimitry Andric       for (size_t i = 0; i < num_args; ++i) {
1700*0b57cec5SDimitry Andric         int32_t signo = signals_sp->GetSignalNumberFromName(
1701*0b57cec5SDimitry Andric             signal_args.GetArgumentAtIndex(i));
1702*0b57cec5SDimitry Andric         if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1703*0b57cec5SDimitry Andric           PrintSignal(str, signo, signal_args.GetArgumentAtIndex(i),
1704*0b57cec5SDimitry Andric                       signals_sp);
1705*0b57cec5SDimitry Andric       }
1706*0b57cec5SDimitry Andric     } else // Print info for ALL signals
1707*0b57cec5SDimitry Andric     {
1708*0b57cec5SDimitry Andric       int32_t signo = signals_sp->GetFirstSignalNumber();
1709*0b57cec5SDimitry Andric       while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1710*0b57cec5SDimitry Andric         PrintSignal(str, signo, signals_sp->GetSignalAsCString(signo),
1711*0b57cec5SDimitry Andric                     signals_sp);
1712*0b57cec5SDimitry Andric         signo = signals_sp->GetNextSignalNumber(signo);
1713*0b57cec5SDimitry Andric       }
1714*0b57cec5SDimitry Andric     }
1715*0b57cec5SDimitry Andric   }
1716*0b57cec5SDimitry Andric 
1717*0b57cec5SDimitry Andric protected:
1718*0b57cec5SDimitry Andric   bool DoExecute(Args &signal_args, CommandReturnObject &result) override {
171981ad6265SDimitry Andric     Target &target = GetSelectedOrDummyTarget();
1720*0b57cec5SDimitry Andric 
172181ad6265SDimitry Andric     // Any signals that are being set should be added to the Target's
172281ad6265SDimitry Andric     // DummySignals so they will get applied on rerun, etc.
172381ad6265SDimitry Andric     // If we have a process, however, we can do a more accurate job of vetting
172481ad6265SDimitry Andric     // the user's options.
172581ad6265SDimitry Andric     ProcessSP process_sp = target.GetProcessSP();
1726*0b57cec5SDimitry Andric 
1727*0b57cec5SDimitry Andric     int stop_action = -1;   // -1 means leave the current setting alone
1728*0b57cec5SDimitry Andric     int pass_action = -1;   // -1 means leave the current setting alone
1729*0b57cec5SDimitry Andric     int notify_action = -1; // -1 means leave the current setting alone
1730*0b57cec5SDimitry Andric 
1731*0b57cec5SDimitry Andric     if (!m_options.stop.empty() &&
1732*0b57cec5SDimitry Andric         !VerifyCommandOptionValue(m_options.stop, stop_action)) {
1733*0b57cec5SDimitry Andric       result.AppendError("Invalid argument for command option --stop; must be "
1734*0b57cec5SDimitry Andric                          "true or false.\n");
1735*0b57cec5SDimitry Andric       return false;
1736*0b57cec5SDimitry Andric     }
1737*0b57cec5SDimitry Andric 
1738*0b57cec5SDimitry Andric     if (!m_options.notify.empty() &&
1739*0b57cec5SDimitry Andric         !VerifyCommandOptionValue(m_options.notify, notify_action)) {
1740*0b57cec5SDimitry Andric       result.AppendError("Invalid argument for command option --notify; must "
1741*0b57cec5SDimitry Andric                          "be true or false.\n");
1742*0b57cec5SDimitry Andric       return false;
1743*0b57cec5SDimitry Andric     }
1744*0b57cec5SDimitry Andric 
1745*0b57cec5SDimitry Andric     if (!m_options.pass.empty() &&
1746*0b57cec5SDimitry Andric         !VerifyCommandOptionValue(m_options.pass, pass_action)) {
1747*0b57cec5SDimitry Andric       result.AppendError("Invalid argument for command option --pass; must be "
1748*0b57cec5SDimitry Andric                          "true or false.\n");
1749*0b57cec5SDimitry Andric       return false;
1750*0b57cec5SDimitry Andric     }
1751*0b57cec5SDimitry Andric 
175281ad6265SDimitry Andric     bool no_actions = (stop_action == -1 && pass_action == -1
175381ad6265SDimitry Andric         && notify_action == -1);
175481ad6265SDimitry Andric     if (m_options.only_target_values && !no_actions) {
175581ad6265SDimitry Andric       result.AppendError("-t is for reporting, not setting, target values.");
175681ad6265SDimitry Andric       return false;
175781ad6265SDimitry Andric     }
175881ad6265SDimitry Andric 
1759*0b57cec5SDimitry Andric     size_t num_args = signal_args.GetArgumentCount();
176081ad6265SDimitry Andric     UnixSignalsSP signals_sp;
176181ad6265SDimitry Andric     if (process_sp)
176281ad6265SDimitry Andric       signals_sp = process_sp->GetUnixSignals();
176381ad6265SDimitry Andric 
1764*0b57cec5SDimitry Andric     int num_signals_set = 0;
1765*0b57cec5SDimitry Andric 
176681ad6265SDimitry Andric     // If we were just asked to print the target values, do that here and
176781ad6265SDimitry Andric     // return:
176881ad6265SDimitry Andric     if (m_options.only_target_values) {
176981ad6265SDimitry Andric       target.PrintDummySignals(result.GetOutputStream(), signal_args);
177081ad6265SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
177181ad6265SDimitry Andric       return true;
177281ad6265SDimitry Andric     }
177381ad6265SDimitry Andric 
177481ad6265SDimitry Andric     // This handles clearing values:
177581ad6265SDimitry Andric     if (m_options.do_clear) {
177681ad6265SDimitry Andric       target.ClearDummySignals(signal_args);
177781ad6265SDimitry Andric       if (m_options.dummy)
177881ad6265SDimitry Andric         GetDummyTarget().ClearDummySignals(signal_args);
177981ad6265SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
178081ad6265SDimitry Andric       return true;
178181ad6265SDimitry Andric     }
178281ad6265SDimitry Andric 
178381ad6265SDimitry Andric     // This rest handles setting values:
1784*0b57cec5SDimitry Andric     if (num_args > 0) {
1785*0b57cec5SDimitry Andric       for (const auto &arg : signal_args) {
178681ad6265SDimitry Andric         // Do the process first.  If we have a process we can catch
178781ad6265SDimitry Andric         // invalid signal names, which we do here.
178881ad6265SDimitry Andric         if (signals_sp) {
1789*0b57cec5SDimitry Andric           int32_t signo = signals_sp->GetSignalNumberFromName(arg.c_str());
1790*0b57cec5SDimitry Andric           if (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1791*0b57cec5SDimitry Andric             // Casting the actions as bools here should be okay, because
1792*0b57cec5SDimitry Andric             // VerifyCommandOptionValue guarantees the value is either 0 or 1.
1793*0b57cec5SDimitry Andric             if (stop_action != -1)
1794*0b57cec5SDimitry Andric               signals_sp->SetShouldStop(signo, stop_action);
1795*0b57cec5SDimitry Andric             if (pass_action != -1) {
1796*0b57cec5SDimitry Andric               bool suppress = !pass_action;
1797*0b57cec5SDimitry Andric               signals_sp->SetShouldSuppress(signo, suppress);
1798*0b57cec5SDimitry Andric             }
1799*0b57cec5SDimitry Andric             if (notify_action != -1)
1800*0b57cec5SDimitry Andric               signals_sp->SetShouldNotify(signo, notify_action);
1801*0b57cec5SDimitry Andric             ++num_signals_set;
1802*0b57cec5SDimitry Andric           } else {
1803*0b57cec5SDimitry Andric             result.AppendErrorWithFormat("Invalid signal name '%s'\n",
1804*0b57cec5SDimitry Andric                                           arg.c_str());
180581ad6265SDimitry Andric             continue;
1806*0b57cec5SDimitry Andric           }
180781ad6265SDimitry Andric         } else {
180881ad6265SDimitry Andric           // If there's no process we can't check, so we just set them all.
180981ad6265SDimitry Andric           // But since the map signal name -> signal number across all platforms
181081ad6265SDimitry Andric           // is not 1-1, we can't sensibly set signal actions by number before
181181ad6265SDimitry Andric           // we have a process.  Check that here:
181281ad6265SDimitry Andric           int32_t signo;
181381ad6265SDimitry Andric           if (llvm::to_integer(arg.c_str(), signo)) {
181481ad6265SDimitry Andric             result.AppendErrorWithFormat("Can't set signal handling by signal "
181581ad6265SDimitry Andric                                          "number with no process");
181681ad6265SDimitry Andric             return false;
181781ad6265SDimitry Andric           }
181881ad6265SDimitry Andric          num_signals_set = num_args;
181981ad6265SDimitry Andric         }
182081ad6265SDimitry Andric         auto set_lazy_bool = [] (int action) -> LazyBool {
182181ad6265SDimitry Andric           LazyBool lazy;
182281ad6265SDimitry Andric           if (action == -1)
182381ad6265SDimitry Andric             lazy = eLazyBoolCalculate;
182481ad6265SDimitry Andric           else if (action)
182581ad6265SDimitry Andric             lazy = eLazyBoolYes;
182681ad6265SDimitry Andric           else
182781ad6265SDimitry Andric             lazy = eLazyBoolNo;
182881ad6265SDimitry Andric           return lazy;
182981ad6265SDimitry Andric         };
183081ad6265SDimitry Andric 
183181ad6265SDimitry Andric         // If there were no actions, we're just listing, don't add the dummy:
183281ad6265SDimitry Andric         if (!no_actions)
183381ad6265SDimitry Andric           target.AddDummySignal(arg.ref(),
183481ad6265SDimitry Andric                                 set_lazy_bool(pass_action),
183581ad6265SDimitry Andric                                 set_lazy_bool(notify_action),
183681ad6265SDimitry Andric                                 set_lazy_bool(stop_action));
1837*0b57cec5SDimitry Andric       }
1838*0b57cec5SDimitry Andric     } else {
1839*0b57cec5SDimitry Andric       // No signal specified, if any command options were specified, update ALL
184081ad6265SDimitry Andric       // signals.  But we can't do this without a process since we don't know
184181ad6265SDimitry Andric       // all the possible signals that might be valid for this target.
184281ad6265SDimitry Andric       if (((notify_action != -1) || (stop_action != -1) || (pass_action != -1))
184381ad6265SDimitry Andric           && process_sp) {
1844*0b57cec5SDimitry Andric         if (m_interpreter.Confirm(
1845*0b57cec5SDimitry Andric                 "Do you really want to update all the signals?", false)) {
1846*0b57cec5SDimitry Andric           int32_t signo = signals_sp->GetFirstSignalNumber();
1847*0b57cec5SDimitry Andric           while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1848*0b57cec5SDimitry Andric             if (notify_action != -1)
1849*0b57cec5SDimitry Andric               signals_sp->SetShouldNotify(signo, notify_action);
1850*0b57cec5SDimitry Andric             if (stop_action != -1)
1851*0b57cec5SDimitry Andric               signals_sp->SetShouldStop(signo, stop_action);
1852*0b57cec5SDimitry Andric             if (pass_action != -1) {
1853*0b57cec5SDimitry Andric               bool suppress = !pass_action;
1854*0b57cec5SDimitry Andric               signals_sp->SetShouldSuppress(signo, suppress);
1855*0b57cec5SDimitry Andric             }
1856*0b57cec5SDimitry Andric             signo = signals_sp->GetNextSignalNumber(signo);
1857*0b57cec5SDimitry Andric           }
1858*0b57cec5SDimitry Andric         }
1859*0b57cec5SDimitry Andric       }
1860*0b57cec5SDimitry Andric     }
1861*0b57cec5SDimitry Andric 
186281ad6265SDimitry Andric     if (signals_sp)
1863*0b57cec5SDimitry Andric       PrintSignalInformation(result.GetOutputStream(), signal_args,
1864*0b57cec5SDimitry Andric                              num_signals_set, signals_sp);
186581ad6265SDimitry Andric     else
186681ad6265SDimitry Andric       target.PrintDummySignals(result.GetOutputStream(),
186781ad6265SDimitry Andric           signal_args);
1868*0b57cec5SDimitry Andric 
1869*0b57cec5SDimitry Andric     if (num_signals_set > 0)
187081ad6265SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
1871*0b57cec5SDimitry Andric     else
1872*0b57cec5SDimitry Andric       result.SetStatus(eReturnStatusFailed);
1873*0b57cec5SDimitry Andric 
1874*0b57cec5SDimitry Andric     return result.Succeeded();
1875*0b57cec5SDimitry Andric   }
1876*0b57cec5SDimitry Andric 
1877*0b57cec5SDimitry Andric   CommandOptions m_options;
1878*0b57cec5SDimitry Andric };
1879*0b57cec5SDimitry Andric 
1880fe6060f1SDimitry Andric // Next are the subcommands of CommandObjectMultiwordProcessTrace
1881fe6060f1SDimitry Andric 
1882fe6060f1SDimitry Andric // CommandObjectProcessTraceStart
1883fe6060f1SDimitry Andric class CommandObjectProcessTraceStart : public CommandObjectTraceProxy {
1884fe6060f1SDimitry Andric public:
1885fe6060f1SDimitry Andric   CommandObjectProcessTraceStart(CommandInterpreter &interpreter)
1886fe6060f1SDimitry Andric       : CommandObjectTraceProxy(
1887fe6060f1SDimitry Andric             /*live_debug_session_only*/ true, interpreter,
1888fe6060f1SDimitry Andric             "process trace start",
1889fe6060f1SDimitry Andric             "Start tracing this process with the corresponding trace "
1890fe6060f1SDimitry Andric             "plug-in.",
1891fe6060f1SDimitry Andric             "process trace start [<trace-options>]") {}
1892fe6060f1SDimitry Andric 
1893fe6060f1SDimitry Andric protected:
1894fe6060f1SDimitry Andric   lldb::CommandObjectSP GetDelegateCommand(Trace &trace) override {
1895fe6060f1SDimitry Andric     return trace.GetProcessTraceStartCommand(m_interpreter);
1896fe6060f1SDimitry Andric   }
1897fe6060f1SDimitry Andric };
1898fe6060f1SDimitry Andric 
1899fe6060f1SDimitry Andric // CommandObjectProcessTraceStop
1900fe6060f1SDimitry Andric class CommandObjectProcessTraceStop : public CommandObjectParsed {
1901fe6060f1SDimitry Andric public:
1902fe6060f1SDimitry Andric   CommandObjectProcessTraceStop(CommandInterpreter &interpreter)
1903fe6060f1SDimitry Andric       : CommandObjectParsed(interpreter, "process trace stop",
1904fe6060f1SDimitry Andric                             "Stop tracing this process. This does not affect "
1905fe6060f1SDimitry Andric                             "traces started with the "
1906fe6060f1SDimitry Andric                             "\"thread trace start\" command.",
1907fe6060f1SDimitry Andric                             "process trace stop",
1908fe6060f1SDimitry Andric                             eCommandRequiresProcess | eCommandTryTargetAPILock |
1909fe6060f1SDimitry Andric                                 eCommandProcessMustBeLaunched |
1910fe6060f1SDimitry Andric                                 eCommandProcessMustBePaused |
1911fe6060f1SDimitry Andric                                 eCommandProcessMustBeTraced) {}
1912fe6060f1SDimitry Andric 
1913fe6060f1SDimitry Andric   ~CommandObjectProcessTraceStop() override = default;
1914fe6060f1SDimitry Andric 
1915fe6060f1SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
1916fe6060f1SDimitry Andric     ProcessSP process_sp = m_exe_ctx.GetProcessSP();
1917fe6060f1SDimitry Andric 
1918fe6060f1SDimitry Andric     TraceSP trace_sp = process_sp->GetTarget().GetTrace();
1919fe6060f1SDimitry Andric 
1920fe6060f1SDimitry Andric     if (llvm::Error err = trace_sp->Stop())
1921fe6060f1SDimitry Andric       result.AppendError(toString(std::move(err)));
1922fe6060f1SDimitry Andric     else
1923fe6060f1SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
1924fe6060f1SDimitry Andric 
1925fe6060f1SDimitry Andric     return result.Succeeded();
1926fe6060f1SDimitry Andric   }
1927fe6060f1SDimitry Andric };
1928fe6060f1SDimitry Andric 
1929fe6060f1SDimitry Andric // CommandObjectMultiwordProcessTrace
1930fe6060f1SDimitry Andric class CommandObjectMultiwordProcessTrace : public CommandObjectMultiword {
1931fe6060f1SDimitry Andric public:
1932fe6060f1SDimitry Andric   CommandObjectMultiwordProcessTrace(CommandInterpreter &interpreter)
1933fe6060f1SDimitry Andric       : CommandObjectMultiword(
1934fe6060f1SDimitry Andric             interpreter, "trace", "Commands for tracing the current process.",
1935fe6060f1SDimitry Andric             "process trace <subcommand> [<subcommand objects>]") {
1936fe6060f1SDimitry Andric     LoadSubCommand("start", CommandObjectSP(new CommandObjectProcessTraceStart(
1937fe6060f1SDimitry Andric                                 interpreter)));
1938fe6060f1SDimitry Andric     LoadSubCommand("stop", CommandObjectSP(
1939fe6060f1SDimitry Andric                                new CommandObjectProcessTraceStop(interpreter)));
1940fe6060f1SDimitry Andric   }
1941fe6060f1SDimitry Andric 
1942fe6060f1SDimitry Andric   ~CommandObjectMultiwordProcessTrace() override = default;
1943fe6060f1SDimitry Andric };
1944fe6060f1SDimitry Andric 
1945*0b57cec5SDimitry Andric // CommandObjectMultiwordProcess
1946*0b57cec5SDimitry Andric 
1947*0b57cec5SDimitry Andric CommandObjectMultiwordProcess::CommandObjectMultiwordProcess(
1948*0b57cec5SDimitry Andric     CommandInterpreter &interpreter)
1949*0b57cec5SDimitry Andric     : CommandObjectMultiword(
1950*0b57cec5SDimitry Andric           interpreter, "process",
1951*0b57cec5SDimitry Andric           "Commands for interacting with processes on the current platform.",
1952*0b57cec5SDimitry Andric           "process <subcommand> [<subcommand-options>]") {
1953*0b57cec5SDimitry Andric   LoadSubCommand("attach",
1954*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessAttach(interpreter)));
1955*0b57cec5SDimitry Andric   LoadSubCommand("launch",
1956*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessLaunch(interpreter)));
1957*0b57cec5SDimitry Andric   LoadSubCommand("continue", CommandObjectSP(new CommandObjectProcessContinue(
1958*0b57cec5SDimitry Andric                                  interpreter)));
1959*0b57cec5SDimitry Andric   LoadSubCommand("connect",
1960*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessConnect(interpreter)));
1961*0b57cec5SDimitry Andric   LoadSubCommand("detach",
1962*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessDetach(interpreter)));
1963*0b57cec5SDimitry Andric   LoadSubCommand("load",
1964*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessLoad(interpreter)));
1965*0b57cec5SDimitry Andric   LoadSubCommand("unload",
1966*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessUnload(interpreter)));
1967*0b57cec5SDimitry Andric   LoadSubCommand("signal",
1968*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessSignal(interpreter)));
1969*0b57cec5SDimitry Andric   LoadSubCommand("handle",
1970*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessHandle(interpreter)));
1971*0b57cec5SDimitry Andric   LoadSubCommand("status",
1972*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessStatus(interpreter)));
1973*0b57cec5SDimitry Andric   LoadSubCommand("interrupt", CommandObjectSP(new CommandObjectProcessInterrupt(
1974*0b57cec5SDimitry Andric                                   interpreter)));
1975*0b57cec5SDimitry Andric   LoadSubCommand("kill",
1976*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessKill(interpreter)));
1977*0b57cec5SDimitry Andric   LoadSubCommand("plugin",
1978*0b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectProcessPlugin(interpreter)));
1979*0b57cec5SDimitry Andric   LoadSubCommand("save-core", CommandObjectSP(new CommandObjectProcessSaveCore(
1980*0b57cec5SDimitry Andric                                   interpreter)));
1981fe6060f1SDimitry Andric   LoadSubCommand(
1982fe6060f1SDimitry Andric       "trace",
1983fe6060f1SDimitry Andric       CommandObjectSP(new CommandObjectMultiwordProcessTrace(interpreter)));
1984*0b57cec5SDimitry Andric }
1985*0b57cec5SDimitry Andric 
1986*0b57cec5SDimitry Andric CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess() = default;
1987