blob: c8fa71ba606665df0590912cb80c4199921fc129 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandObjectProcess.cpp --------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00009#include "CommandObjectProcess.h"
Jim Ingham0e410842012-08-11 01:27:55 +000010#include "lldb/Breakpoint/Breakpoint.h"
11#include "lldb/Breakpoint/BreakpointLocation.h"
12#include "lldb/Breakpoint/BreakpointSite.h"
Greg Clayton1f746072012-08-29 21:13:06 +000013#include "lldb/Core/Module.h"
Greg Claytona2715cf2014-06-13 00:54:12 +000014#include "lldb/Core/PluginManager.h"
Greg Clayton7260f622011-04-18 08:33:37 +000015#include "lldb/Host/Host.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000016#include "lldb/Host/OptionParser.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000017#include "lldb/Host/StringConvert.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Interpreter/CommandInterpreter.h"
19#include "lldb/Interpreter/CommandReturnObject.h"
Pavel Labath47cbf4a2018-04-10 09:03:59 +000020#include "lldb/Interpreter/OptionArgParser.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000021#include "lldb/Interpreter/Options.h"
Greg Claytone996fd32011-03-08 22:40:15 +000022#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Target/Process.h"
Jim Ingham0e410842012-08-11 01:27:55 +000024#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Target/Target.h"
26#include "lldb/Target/Thread.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000027#include "lldb/Target/UnixSignals.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000028#include "lldb/Utility/Args.h"
Pavel Labathd821c992018-08-07 11:07:21 +000029#include "lldb/Utility/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
31using namespace lldb;
32using namespace lldb_private;
33
Kate Stoneb9c1b512016-09-06 20:57:50 +000034class CommandObjectProcessLaunchOrAttach : public CommandObjectParsed {
Jim Inghamdcb1d852013-03-29 00:56:30 +000035public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 CommandObjectProcessLaunchOrAttach(CommandInterpreter &interpreter,
37 const char *name, const char *help,
38 const char *syntax, uint32_t flags,
39 const char *new_process_action)
40 : CommandObjectParsed(interpreter, name, help, syntax, flags),
41 m_new_process_action(new_process_action) {}
Eugene Zelenko49bcfd82016-02-23 01:43:44 +000042
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 ~CommandObjectProcessLaunchOrAttach() override = default;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +000044
Jim Inghamdcb1d852013-03-29 00:56:30 +000045protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +000046 bool StopProcessIfNecessary(Process *process, StateType &state,
47 CommandReturnObject &result) {
48 state = eStateInvalid;
49 if (process) {
50 state = process->GetState();
Eugene Zelenko49bcfd82016-02-23 01:43:44 +000051
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 if (process->IsAlive() && state != eStateConnected) {
53 char message[1024];
54 if (process->GetState() == eStateAttaching)
55 ::snprintf(message, sizeof(message),
56 "There is a pending attach, abort it and %s?",
57 m_new_process_action.c_str());
58 else if (process->GetShouldDetach())
59 ::snprintf(message, sizeof(message),
60 "There is a running process, detach from it and %s?",
61 m_new_process_action.c_str());
62 else
63 ::snprintf(message, sizeof(message),
64 "There is a running process, kill it and %s?",
65 m_new_process_action.c_str());
66
67 if (!m_interpreter.Confirm(message, true)) {
68 result.SetStatus(eReturnStatusFailed);
69 return false;
70 } else {
71 if (process->GetShouldDetach()) {
72 bool keep_stopped = false;
Zachary Turner97206d52017-05-12 04:51:55 +000073 Status detach_error(process->Detach(keep_stopped));
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 if (detach_error.Success()) {
75 result.SetStatus(eReturnStatusSuccessFinishResult);
76 process = nullptr;
77 } else {
78 result.AppendErrorWithFormat(
79 "Failed to detach from process: %s\n",
80 detach_error.AsCString());
81 result.SetStatus(eReturnStatusFailed);
82 }
83 } else {
Zachary Turner97206d52017-05-12 04:51:55 +000084 Status destroy_error(process->Destroy(false));
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 if (destroy_error.Success()) {
86 result.SetStatus(eReturnStatusSuccessFinishResult);
87 process = nullptr;
88 } else {
89 result.AppendErrorWithFormat("Failed to kill process: %s\n",
90 destroy_error.AsCString());
91 result.SetStatus(eReturnStatusFailed);
92 }
93 }
94 }
95 }
96 }
97 return result.Succeeded();
98 }
99
100 std::string m_new_process_action;
Jim Inghamdcb1d852013-03-29 00:56:30 +0000101};
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000102
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103// CommandObjectProcessLaunch
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000104#pragma mark CommandObjectProcessLaunch
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105class CommandObjectProcessLaunch : public CommandObjectProcessLaunchOrAttach {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 CommandObjectProcessLaunch(CommandInterpreter &interpreter)
108 : CommandObjectProcessLaunchOrAttach(
109 interpreter, "process launch",
110 "Launch the executable in the debugger.", nullptr,
111 eCommandRequiresTarget, "restart"),
112 m_options() {
113 CommandArgumentEntry arg;
114 CommandArgumentData run_args_arg;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 // Define the first (and only) variant of this arg.
117 run_args_arg.arg_type = eArgTypeRunArgs;
118 run_args_arg.arg_repetition = eArgRepeatOptional;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 // There is only one variant this argument could be; put it into the
121 // argument entry.
122 arg.push_back(run_args_arg);
Todd Fialae1cfbc72016-08-11 23:51:28 +0000123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 // Push the data for the first argument into the m_arguments vector.
125 m_arguments.push_back(arg);
126 }
Jim Inghame9ce62b2012-08-10 21:48:41 +0000127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 ~CommandObjectProcessLaunch() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129
Raphael Isemann2443bbd2018-07-02 21:29:56 +0000130 int HandleArgumentCompletion(
131 CompletionRequest &request,
132 OptionElementVector &opt_element_vector) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133
134 CommandCompletions::InvokeCommonCompletionCallbacks(
135 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000136 request, nullptr);
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000137 return request.GetNumberOfMatches();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 }
139
140 Options *GetOptions() override { return &m_options; }
141
142 const char *GetRepeatCommand(Args &current_command_args,
143 uint32_t index) override {
144 // No repeat for "process launch"...
145 return "";
146 }
Jim Ingham5a988412012-06-08 21:56:10 +0000147
148protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 bool DoExecute(Args &launch_args, CommandReturnObject &result) override {
Jonas Devlieghere57179862019-04-27 06:19:42 +0000150 Debugger &debugger = GetDebugger();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 Target *target = debugger.GetSelectedTarget().get();
152 // If our listener is nullptr, users aren't allows to launch
153 ModuleSP exe_module_sp = target->GetExecutableModule();
Greg Clayton71337622011-02-24 22:24:29 +0000154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 if (exe_module_sp == nullptr) {
156 result.AppendError("no file in target, create a debug target using the "
157 "'target create' command");
158 result.SetStatus(eReturnStatusFailed);
159 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160 }
161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 StateType state = eStateInvalid;
163
164 if (!StopProcessIfNecessary(m_exe_ctx.GetProcessPtr(), state, result))
165 return false;
166
Zachary Turner31d97a52016-11-17 18:08:12 +0000167 llvm::StringRef target_settings_argv0 = target->GetArg0();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168
169 // Determine whether we will disable ASLR or leave it in the default state
Adrian Prantl05097242018-04-30 16:49:04 +0000170 // (i.e. enabled if the platform supports it). First check if the process
171 // launch options explicitly turn on/off
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 // disabling ASLR. If so, use that setting;
173 // otherwise, use the 'settings target.disable-aslr' setting.
174 bool disable_aslr = false;
175 if (m_options.disable_aslr != eLazyBoolCalculate) {
Adrian Prantl05097242018-04-30 16:49:04 +0000176 // The user specified an explicit setting on the process launch line.
177 // Use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 disable_aslr = (m_options.disable_aslr == eLazyBoolYes);
179 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000180 // The user did not explicitly specify whether to disable ASLR. Fall
181 // back to the target.disable-aslr setting.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 disable_aslr = target->GetDisableASLR();
183 }
184
185 if (disable_aslr)
186 m_options.launch_info.GetFlags().Set(eLaunchFlagDisableASLR);
187 else
188 m_options.launch_info.GetFlags().Clear(eLaunchFlagDisableASLR);
189
190 if (target->GetDetachOnError())
191 m_options.launch_info.GetFlags().Set(eLaunchFlagDetachOnError);
192
193 if (target->GetDisableSTDIO())
194 m_options.launch_info.GetFlags().Set(eLaunchFlagDisableSTDIO);
195
Pavel Labath62930e52018-01-10 11:57:31 +0000196 m_options.launch_info.GetEnvironment() = target->GetEnvironment();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197
Zachary Turner31d97a52016-11-17 18:08:12 +0000198 if (!target_settings_argv0.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 m_options.launch_info.GetArguments().AppendArgument(
Zachary Turner31d97a52016-11-17 18:08:12 +0000200 target_settings_argv0);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 m_options.launch_info.SetExecutableFile(
202 exe_module_sp->GetPlatformFileSpec(), false);
203 } else {
204 m_options.launch_info.SetExecutableFile(
205 exe_module_sp->GetPlatformFileSpec(), true);
206 }
207
208 if (launch_args.GetArgumentCount() == 0) {
209 m_options.launch_info.GetArguments().AppendArguments(
210 target->GetProcessLaunchInfo().GetArguments());
211 } else {
212 m_options.launch_info.GetArguments().AppendArguments(launch_args);
213 // Save the arguments for subsequent runs in the current target.
214 target->SetRunArguments(launch_args);
215 }
216
217 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +0000218 Status error = target->Launch(m_options.launch_info, &stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219
220 if (error.Success()) {
221 ProcessSP process_sp(target->GetProcessSP());
222 if (process_sp) {
223 // There is a race condition where this thread will return up the call
Adrian Prantl05097242018-04-30 16:49:04 +0000224 // stack to the main command handler and show an (lldb) prompt before
225 // HandlePrivateEvent (from PrivateStateThread) has a chance to call
226 // PushProcessIOHandler().
Pavel Labath3879fe02018-05-09 14:29:30 +0000227 process_sp->SyncIOHandler(0, std::chrono::seconds(2));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228
Zachary Turnerc1564272016-11-16 21:15:24 +0000229 llvm::StringRef data = stream.GetString();
230 if (!data.empty())
231 result.AppendMessage(data);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232 const char *archname =
233 exe_module_sp->GetArchitecture().GetArchitectureName();
234 result.AppendMessageWithFormat(
235 "Process %" PRIu64 " launched: '%s' (%s)\n", process_sp->GetID(),
236 exe_module_sp->GetFileSpec().GetPath().c_str(), archname);
237 result.SetStatus(eReturnStatusSuccessFinishResult);
238 result.SetDidChangeProcessState(true);
239 } else {
240 result.AppendError(
241 "no error returned from Target::Launch, and target has no process");
242 result.SetStatus(eReturnStatusFailed);
243 }
244 } else {
245 result.AppendError(error.AsCString());
246 result.SetStatus(eReturnStatusFailed);
247 }
248 return result.Succeeded();
249 }
250
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 ProcessLaunchCommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253};
254
Greg Clayton982c9762011-11-03 21:22:33 +0000255//#define SET1 LLDB_OPT_SET_1
256//#define SET2 LLDB_OPT_SET_2
257//#define SET3 LLDB_OPT_SET_3
258//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259// OptionDefinition
260// CommandObjectProcessLaunch::CommandOptions::g_option_table[] =
Greg Clayton982c9762011-11-03 21:22:33 +0000261//{
Kate Stoneac9c3a62016-08-26 23:28:47 +0000262// // clang-format off
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263// {SET1 | SET2 | SET3, false, "stop-at-entry", 's', OptionParser::eNoArgument,
264// nullptr, 0, eArgTypeNone, "Stop at the entry point of the program
265// when launching a process."},
266// {SET1, false, "stdin", 'i',
267// OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
268// "Redirect stdin for the process to <path>."},
269// {SET1, false, "stdout", 'o',
270// OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
271// "Redirect stdout for the process to <path>."},
272// {SET1, false, "stderr", 'e',
273// OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName,
274// "Redirect stderr for the process to <path>."},
275// {SET1 | SET2 | SET3, false, "plugin", 'p',
276// OptionParser::eRequiredArgument, nullptr, 0, eArgTypePlugin, "Name of
277// the process plugin you want to use."},
278// { SET2, false, "tty", 't',
279// OptionParser::eOptionalArgument, nullptr, 0, eArgTypeDirectoryName, "Start
280// the process in a terminal. If <path> is specified, look for a terminal whose
281// name contains <path>, else start the process in a new terminal."},
282// { SET3, false, "no-stdio", 'n', OptionParser::eNoArgument,
283// nullptr, 0, eArgTypeNone, "Do not set up for terminal I/O to go to
284// running process."},
285// {SET1 | SET2 | SET3, false, "working-dir", 'w',
286// OptionParser::eRequiredArgument, nullptr, 0, eArgTypeDirectoryName, "Set the
287// current working directory to <path> when running the inferior."},
Kate Stoneac9c3a62016-08-26 23:28:47 +0000288// {0, false, nullptr, 0, 0, nullptr, 0, eArgTypeNone, nullptr}
289// // clang-format on
Greg Clayton982c9762011-11-03 21:22:33 +0000290//};
291//
292//#undef SET1
293//#undef SET2
294//#undef SET3
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296// CommandObjectProcessAttach
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000297
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000298static constexpr OptionDefinition g_process_attach_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000299 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000300 { LLDB_OPT_SET_ALL, false, "continue", 'c', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Immediately continue the process once attached." },
301 { LLDB_OPT_SET_ALL, false, "plugin", 'P', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePlugin, "Name of the process plugin you want to use." },
302 { LLDB_OPT_SET_1, false, "pid", 'p', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePid, "The process ID of an existing process to attach to." },
303 { LLDB_OPT_SET_2, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeProcessName, "The name of the process to attach to." },
304 { LLDB_OPT_SET_2, false, "include-existing", 'i', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Include existing processes when doing attach -w." },
305 { LLDB_OPT_SET_2, false, "waitfor", 'w', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Wait for the process with <process-name> to launch." },
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000306 // clang-format on
307};
308
Jim Inghambb9caf72010-12-09 18:58:16 +0000309#pragma mark CommandObjectProcessAttach
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 class CommandOptions : public Options {
313 public:
314 CommandOptions() : Options() {
315 // Keep default values of all options in one place: OptionParsingStarting
316 // ()
317 OptionParsingStarting(nullptr);
Jim Ingham5aee1622010-08-09 23:31:02 +0000318 }
319
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 ~CommandOptions() override = default;
Jim Ingham5aee1622010-08-09 23:31:02 +0000321
Zachary Turner97206d52017-05-12 04:51:55 +0000322 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
323 ExecutionContext *execution_context) override {
324 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325 const int short_option = m_getopt_table[option_idx].val;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 switch (short_option) {
327 case 'c':
328 attach_info.SetContinueOnceAttached(true);
329 break;
330
331 case 'p': {
Zachary Turnerfe114832016-11-12 16:56:47 +0000332 lldb::pid_t pid;
333 if (option_arg.getAsInteger(0, pid)) {
334 error.SetErrorStringWithFormat("invalid process ID '%s'",
335 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 } else {
337 attach_info.SetProcessID(pid);
338 }
339 } break;
340
341 case 'P':
342 attach_info.SetProcessPluginName(option_arg);
343 break;
344
345 case 'n':
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000346 attach_info.GetExecutableFile().SetFile(option_arg,
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000347 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 break;
349
350 case 'w':
351 attach_info.SetWaitForLaunch(true);
352 break;
353
354 case 'i':
355 attach_info.SetIgnoreExisting(false);
356 break;
357
358 default:
359 error.SetErrorStringWithFormat("invalid short option character '%c'",
360 short_option);
361 break;
362 }
363 return error;
Jim Ingham5a988412012-06-08 21:56:10 +0000364 }
365
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 void OptionParsingStarting(ExecutionContext *execution_context) override {
367 attach_info.Clear();
368 }
369
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000370 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000371 return llvm::makeArrayRef(g_process_attach_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000372 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373
374 bool HandleOptionArgumentCompletion(
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000375 CompletionRequest &request, OptionElementVector &opt_element_vector,
376 int opt_element_index, CommandInterpreter &interpreter) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
378 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
379
380 // We are only completing the name option for now...
381
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000382 if (GetDefinitions()[opt_defs_index].short_option == 'n') {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 // Are we in the name?
384
385 // Look to see if there is a -P argument provided, and if so use that
Adrian Prantl05097242018-04-30 16:49:04 +0000386 // plugin, otherwise use the default plugin.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387
388 const char *partial_name = nullptr;
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000389 partial_name = request.GetParsedLine().GetArgumentAtIndex(opt_arg_pos);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390
391 PlatformSP platform_sp(interpreter.GetPlatform(true));
392 if (platform_sp) {
393 ProcessInstanceInfoList process_infos;
394 ProcessInstanceInfoMatch match_info;
395 if (partial_name) {
396 match_info.GetProcessInfo().GetExecutableFile().SetFile(
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000397 partial_name, FileSpec::Style::native);
Pavel Labathc4a33952017-02-20 11:35:33 +0000398 match_info.SetNameMatchType(NameMatch::StartsWith);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 }
400 platform_sp->FindProcesses(match_info, process_infos);
401 const size_t num_matches = process_infos.GetSize();
402 if (num_matches > 0) {
403 for (size_t i = 0; i < num_matches; ++i) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000404 request.AddCompletion(llvm::StringRef(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 process_infos.GetProcessNameAtIndex(i),
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000406 process_infos.GetProcessNameLengthAtIndex(i)));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 }
408 }
409 }
410 }
411
412 return false;
413 }
414
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415 // Instance variables to hold the values for command options.
416
417 ProcessAttachInfo attach_info;
418 };
419
420 CommandObjectProcessAttach(CommandInterpreter &interpreter)
421 : CommandObjectProcessLaunchOrAttach(
422 interpreter, "process attach", "Attach to a process.",
423 "process attach <cmd-options>", 0, "attach"),
424 m_options() {}
425
426 ~CommandObjectProcessAttach() override = default;
427
428 Options *GetOptions() override { return &m_options; }
429
Jim Ingham5a988412012-06-08 21:56:10 +0000430protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 bool DoExecute(Args &command, CommandReturnObject &result) override {
432 PlatformSP platform_sp(
Jonas Devlieghere57179862019-04-27 06:19:42 +0000433 GetDebugger().GetPlatformList().GetSelectedPlatform());
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000434
Jonas Devlieghere57179862019-04-27 06:19:42 +0000435 Target *target = GetDebugger().GetSelectedTarget().get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 // N.B. The attach should be synchronous. It doesn't help much to get the
Adrian Prantl05097242018-04-30 16:49:04 +0000437 // prompt back between initiating the attach and the target actually
438 // stopping. So even if the interpreter is set to be asynchronous, we wait
439 // for the stop ourselves here.
Jim Ingham5aee1622010-08-09 23:31:02 +0000440
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 StateType state = eStateInvalid;
442 Process *process = m_exe_ctx.GetProcessPtr();
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 if (!StopProcessIfNecessary(process, state, result))
445 return false;
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000446
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 if (target == nullptr) {
448 // If there isn't a current target create one.
449 TargetSP new_target_sp;
Zachary Turner97206d52017-05-12 04:51:55 +0000450 Status error;
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000451
Jonas Devlieghere57179862019-04-27 06:19:42 +0000452 error = GetDebugger().GetTargetList().CreateTarget(
453 GetDebugger(), "", "", eLoadDependentsNo,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454 nullptr, // No platform options
455 new_target_sp);
456 target = new_target_sp.get();
457 if (target == nullptr || error.Fail()) {
458 result.AppendError(error.AsCString("Error creating target"));
459 return false;
460 }
Jonas Devlieghere57179862019-04-27 06:19:42 +0000461 GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham5aee1622010-08-09 23:31:02 +0000462 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463
464 // Record the old executable module, we want to issue a warning if the
Adrian Prantl05097242018-04-30 16:49:04 +0000465 // process of attaching changed the current executable (like somebody said
466 // "file foo" then attached to a PID whose executable was bar.)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467
468 ModuleSP old_exec_module_sp = target->GetExecutableModule();
469 ArchSpec old_arch_spec = target->GetArchitecture();
470
471 if (command.GetArgumentCount()) {
472 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: %s\n",
473 m_cmd_name.c_str(), m_cmd_syntax.c_str());
474 result.SetStatus(eReturnStatusFailed);
475 return false;
476 }
477
478 m_interpreter.UpdateExecutionContext(nullptr);
479 StreamString stream;
480 const auto error = target->Attach(m_options.attach_info, &stream);
481 if (error.Success()) {
482 ProcessSP process_sp(target->GetProcessSP());
483 if (process_sp) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000484 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485 result.SetStatus(eReturnStatusSuccessFinishNoResult);
486 result.SetDidChangeProcessState(true);
487 result.SetAbnormalStopWasExpected(true);
488 } else {
489 result.AppendError(
490 "no error returned from Target::Attach, and target has no process");
491 result.SetStatus(eReturnStatusFailed);
492 }
493 } else {
494 result.AppendErrorWithFormat("attach failed: %s\n", error.AsCString());
495 result.SetStatus(eReturnStatusFailed);
496 }
497
498 if (!result.Succeeded())
499 return false;
500
501 // Okay, we're done. Last step is to warn if the executable module has
502 // changed:
503 char new_path[PATH_MAX];
504 ModuleSP new_exec_module_sp(target->GetExecutableModule());
505 if (!old_exec_module_sp) {
506 // We might not have a module if we attached to a raw pid...
507 if (new_exec_module_sp) {
508 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
509 result.AppendMessageWithFormat("Executable module set to \"%s\".\n",
510 new_path);
511 }
512 } else if (old_exec_module_sp->GetFileSpec() !=
513 new_exec_module_sp->GetFileSpec()) {
514 char old_path[PATH_MAX];
515
516 old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
517 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
518
519 result.AppendWarningWithFormat(
520 "Executable module changed from \"%s\" to \"%s\".\n", old_path,
521 new_path);
522 }
523
524 if (!old_arch_spec.IsValid()) {
525 result.AppendMessageWithFormat(
526 "Architecture set to: %s.\n",
527 target->GetArchitecture().GetTriple().getTriple().c_str());
528 } else if (!old_arch_spec.IsExactMatch(target->GetArchitecture())) {
529 result.AppendWarningWithFormat(
530 "Architecture changed from %s to %s.\n",
531 old_arch_spec.GetTriple().getTriple().c_str(),
532 target->GetArchitecture().GetTriple().getTriple().c_str());
533 }
534
Adrian Prantl05097242018-04-30 16:49:04 +0000535 // This supports the use-case scenario of immediately continuing the
536 // process once attached.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537 if (m_options.attach_info.GetContinueOnceAttached())
538 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
539
540 return result.Succeeded();
541 }
542
543 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544};
545
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546// CommandObjectProcessContinue
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000547
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000548static constexpr OptionDefinition g_process_continue_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000549 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000550 { LLDB_OPT_SET_ALL, false, "ignore-count",'i', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeUnsignedInteger, "Ignore <N> crossings of the breakpoint (if it exists) for the currently selected thread." }
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000551 // clang-format on
552};
553
Jim Inghambb9caf72010-12-09 18:58:16 +0000554#pragma mark CommandObjectProcessContinue
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556class CommandObjectProcessContinue : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 CommandObjectProcessContinue(CommandInterpreter &interpreter)
559 : CommandObjectParsed(
560 interpreter, "process continue",
561 "Continue execution of all threads in the current process.",
562 "process continue",
563 eCommandRequiresProcess | eCommandTryTargetAPILock |
564 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
565 m_options() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000566
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567 ~CommandObjectProcessContinue() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568
Jim Ingham5a988412012-06-08 21:56:10 +0000569protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570 class CommandOptions : public Options {
571 public:
572 CommandOptions() : Options() {
573 // Keep default values of all options in one place: OptionParsingStarting
574 // ()
575 OptionParsingStarting(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576 }
Jim Ingham0e410842012-08-11 01:27:55 +0000577
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 ~CommandOptions() override = default;
579
Zachary Turner97206d52017-05-12 04:51:55 +0000580 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
581 ExecutionContext *execution_context) override {
582 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 const int short_option = m_getopt_table[option_idx].val;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584 switch (short_option) {
585 case 'i':
Zachary Turnerfe114832016-11-12 16:56:47 +0000586 if (option_arg.getAsInteger(0, m_ignore))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587 error.SetErrorStringWithFormat(
588 "invalid value for ignore option: \"%s\", should be a number.",
Zachary Turnerfe114832016-11-12 16:56:47 +0000589 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 break;
591
592 default:
593 error.SetErrorStringWithFormat("invalid short option character '%c'",
594 short_option);
595 break;
596 }
597 return error;
Jim Ingham0e410842012-08-11 01:27:55 +0000598 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599
600 void OptionParsingStarting(ExecutionContext *execution_context) override {
601 m_ignore = 0;
602 }
603
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000604 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000605 return llvm::makeArrayRef(g_process_continue_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000606 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607
608 uint32_t m_ignore;
609 };
610
611 bool DoExecute(Args &command, CommandReturnObject &result) override {
612 Process *process = m_exe_ctx.GetProcessPtr();
613 bool synchronous_execution = m_interpreter.GetSynchronous();
614 StateType state = process->GetState();
615 if (state == eStateStopped) {
616 if (command.GetArgumentCount() != 0) {
617 result.AppendErrorWithFormat(
618 "The '%s' command does not take any arguments.\n",
619 m_cmd_name.c_str());
620 result.SetStatus(eReturnStatusFailed);
621 return false;
622 }
623
624 if (m_options.m_ignore > 0) {
625 ThreadSP sel_thread_sp(GetDefaultThread()->shared_from_this());
626 if (sel_thread_sp) {
627 StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
628 if (stop_info_sp &&
629 stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
630 lldb::break_id_t bp_site_id =
631 (lldb::break_id_t)stop_info_sp->GetValue();
632 BreakpointSiteSP bp_site_sp(
633 process->GetBreakpointSiteList().FindByID(bp_site_id));
634 if (bp_site_sp) {
635 const size_t num_owners = bp_site_sp->GetNumberOfOwners();
636 for (size_t i = 0; i < num_owners; i++) {
637 Breakpoint &bp_ref =
638 bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
639 if (!bp_ref.IsInternal()) {
640 bp_ref.SetIgnoreCount(m_options.m_ignore);
641 }
642 }
643 }
644 }
645 }
646 }
647
648 { // Scope for thread list mutex:
649 std::lock_guard<std::recursive_mutex> guard(
650 process->GetThreadList().GetMutex());
651 const uint32_t num_threads = process->GetThreadList().GetSize();
652
653 // Set the actions that the threads should each take when resuming
654 for (uint32_t idx = 0; idx < num_threads; ++idx) {
655 const bool override_suspend = false;
656 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState(
657 eStateRunning, override_suspend);
658 }
659 }
660
661 const uint32_t iohandler_id = process->GetIOHandlerID();
662
663 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +0000664 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000665 if (synchronous_execution)
666 error = process->ResumeSynchronous(&stream);
667 else
668 error = process->Resume();
669
670 if (error.Success()) {
671 // There is a race condition where this thread will return up the call
Adrian Prantl05097242018-04-30 16:49:04 +0000672 // stack to the main command handler and show an (lldb) prompt before
673 // HandlePrivateEvent (from PrivateStateThread) has a chance to call
674 // PushProcessIOHandler().
Pavel Labath3879fe02018-05-09 14:29:30 +0000675 process->SyncIOHandler(iohandler_id, std::chrono::seconds(2));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676
677 result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
678 process->GetID());
679 if (synchronous_execution) {
680 // If any state changed events had anything to say, add that to the
681 // result
Zachary Turnerc1564272016-11-16 21:15:24 +0000682 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683
684 result.SetDidChangeProcessState(true);
685 result.SetStatus(eReturnStatusSuccessFinishNoResult);
686 } else {
687 result.SetStatus(eReturnStatusSuccessContinuingNoResult);
688 }
689 } else {
690 result.AppendErrorWithFormat("Failed to resume process: %s.\n",
691 error.AsCString());
692 result.SetStatus(eReturnStatusFailed);
693 }
694 } else {
695 result.AppendErrorWithFormat(
696 "Process cannot be continued from its current state (%s).\n",
697 StateAsCString(state));
698 result.SetStatus(eReturnStatusFailed);
699 }
700 return result.Succeeded();
701 }
702
703 Options *GetOptions() override { return &m_options; }
704
705 CommandOptions m_options;
Jim Ingham0e410842012-08-11 01:27:55 +0000706};
707
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708// CommandObjectProcessDetach
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000709static constexpr OptionDefinition g_process_detach_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000710 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000711 { LLDB_OPT_SET_1, false, "keep-stopped", 's', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Whether or not the process should be kept stopped on detach (if possible)." },
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000712 // clang-format on
713};
714
Jim Inghambb9caf72010-12-09 18:58:16 +0000715#pragma mark CommandObjectProcessDetach
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717class CommandObjectProcessDetach : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000719 class CommandOptions : public Options {
720 public:
721 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
722
723 ~CommandOptions() override = default;
724
Zachary Turner97206d52017-05-12 04:51:55 +0000725 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
726 ExecutionContext *execution_context) override {
727 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 const int short_option = m_getopt_table[option_idx].val;
729
730 switch (short_option) {
731 case 's':
732 bool tmp_result;
733 bool success;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000734 tmp_result = OptionArgParser::ToBoolean(option_arg, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 if (!success)
736 error.SetErrorStringWithFormat("invalid boolean option: \"%s\"",
Zachary Turnerfe114832016-11-12 16:56:47 +0000737 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 else {
739 if (tmp_result)
740 m_keep_stopped = eLazyBoolYes;
741 else
742 m_keep_stopped = eLazyBoolNo;
Jim Inghamacff8952013-05-02 00:27:30 +0000743 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 break;
745 default:
746 error.SetErrorStringWithFormat("invalid short option character '%c'",
747 short_option);
748 break;
749 }
750 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751 }
752
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753 void OptionParsingStarting(ExecutionContext *execution_context) override {
754 m_keep_stopped = eLazyBoolCalculate;
Jim Inghamacff8952013-05-02 00:27:30 +0000755 }
756
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000757 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000758 return llvm::makeArrayRef(g_process_detach_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000759 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760
761 // Instance variables to hold the values for command options.
762 LazyBool m_keep_stopped;
763 };
764
765 CommandObjectProcessDetach(CommandInterpreter &interpreter)
766 : CommandObjectParsed(interpreter, "process detach",
767 "Detach from the current target process.",
768 "process detach",
769 eCommandRequiresProcess | eCommandTryTargetAPILock |
770 eCommandProcessMustBeLaunched),
771 m_options() {}
772
773 ~CommandObjectProcessDetach() override = default;
774
775 Options *GetOptions() override { return &m_options; }
776
Jim Ingham5a988412012-06-08 21:56:10 +0000777protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 bool DoExecute(Args &command, CommandReturnObject &result) override {
779 Process *process = m_exe_ctx.GetProcessPtr();
780 // FIXME: This will be a Command Option:
781 bool keep_stopped;
782 if (m_options.m_keep_stopped == eLazyBoolCalculate) {
783 // Check the process default:
784 keep_stopped = process->GetDetachKeepsStopped();
785 } else if (m_options.m_keep_stopped == eLazyBoolYes)
786 keep_stopped = true;
787 else
788 keep_stopped = false;
Jim Inghamacff8952013-05-02 00:27:30 +0000789
Zachary Turner97206d52017-05-12 04:51:55 +0000790 Status error(process->Detach(keep_stopped));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000791 if (error.Success()) {
792 result.SetStatus(eReturnStatusSuccessFinishResult);
793 } else {
794 result.AppendErrorWithFormat("Detach failed: %s\n", error.AsCString());
795 result.SetStatus(eReturnStatusFailed);
796 return false;
797 }
798 return result.Succeeded();
799 }
800
801 CommandOptions m_options;
Jim Inghamacff8952013-05-02 00:27:30 +0000802};
803
Greg Claytonb766a732011-02-04 01:58:07 +0000804// CommandObjectProcessConnect
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000805
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000806static constexpr OptionDefinition g_process_connect_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000807 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000808 { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePlugin, "Name of the process plugin you want to use." },
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000809 // clang-format on
810};
811
Greg Claytonb766a732011-02-04 01:58:07 +0000812#pragma mark CommandObjectProcessConnect
813
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814class CommandObjectProcessConnect : public CommandObjectParsed {
Greg Claytonb766a732011-02-04 01:58:07 +0000815public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000816 class CommandOptions : public Options {
817 public:
818 CommandOptions() : Options() {
819 // Keep default values of all options in one place: OptionParsingStarting
820 // ()
821 OptionParsingStarting(nullptr);
Greg Claytonb766a732011-02-04 01:58:07 +0000822 }
Greg Claytonb766a732011-02-04 01:58:07 +0000823
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 ~CommandOptions() override = default;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000825
Zachary Turner97206d52017-05-12 04:51:55 +0000826 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
827 ExecutionContext *execution_context) override {
828 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829 const int short_option = m_getopt_table[option_idx].val;
830
831 switch (short_option) {
832 case 'p':
833 plugin_name.assign(option_arg);
834 break;
835
836 default:
837 error.SetErrorStringWithFormat("invalid short option character '%c'",
838 short_option);
839 break;
840 }
841 return error;
Jim Ingham5a988412012-06-08 21:56:10 +0000842 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000843
844 void OptionParsingStarting(ExecutionContext *execution_context) override {
845 plugin_name.clear();
846 }
847
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000848 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000849 return llvm::makeArrayRef(g_process_connect_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000850 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851
852 // Instance variables to hold the values for command options.
853
854 std::string plugin_name;
855 };
856
857 CommandObjectProcessConnect(CommandInterpreter &interpreter)
858 : CommandObjectParsed(interpreter, "process connect",
859 "Connect to a remote debug service.",
860 "process connect <remote-url>", 0),
861 m_options() {}
862
863 ~CommandObjectProcessConnect() override = default;
864
865 Options *GetOptions() override { return &m_options; }
866
Jim Ingham5a988412012-06-08 21:56:10 +0000867protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000868 bool DoExecute(Args &command, CommandReturnObject &result) override {
869 if (command.GetArgumentCount() != 1) {
870 result.AppendErrorWithFormat(
871 "'%s' takes exactly one argument:\nUsage: %s\n", m_cmd_name.c_str(),
872 m_cmd_syntax.c_str());
873 result.SetStatus(eReturnStatusFailed);
874 return false;
Greg Claytonb766a732011-02-04 01:58:07 +0000875 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +0000876
Kate Stoneb9c1b512016-09-06 20:57:50 +0000877 Process *process = m_exe_ctx.GetProcessPtr();
878 if (process && process->IsAlive()) {
879 result.AppendErrorWithFormat(
880 "Process %" PRIu64
881 " is currently being debugged, kill the process before connecting.\n",
882 process->GetID());
883 result.SetStatus(eReturnStatusFailed);
884 return false;
885 }
886
887 const char *plugin_name = nullptr;
888 if (!m_options.plugin_name.empty())
889 plugin_name = m_options.plugin_name.c_str();
890
Zachary Turner97206d52017-05-12 04:51:55 +0000891 Status error;
Jonas Devlieghere57179862019-04-27 06:19:42 +0000892 Debugger &debugger = GetDebugger();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 PlatformSP platform_sp = m_interpreter.GetPlatform(true);
894 ProcessSP process_sp = platform_sp->ConnectProcess(
895 command.GetArgumentAtIndex(0), plugin_name, debugger,
896 debugger.GetSelectedTarget().get(), error);
897 if (error.Fail() || process_sp == nullptr) {
898 result.AppendError(error.AsCString("Error connecting to the process"));
899 result.SetStatus(eReturnStatusFailed);
900 return false;
901 }
902 return true;
903 }
904
905 CommandOptions m_options;
Greg Claytonb766a732011-02-04 01:58:07 +0000906};
907
Greg Clayton998255b2012-10-13 02:07:45 +0000908// CommandObjectProcessPlugin
Greg Clayton998255b2012-10-13 02:07:45 +0000909#pragma mark CommandObjectProcessPlugin
910
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911class CommandObjectProcessPlugin : public CommandObjectProxy {
Greg Clayton998255b2012-10-13 02:07:45 +0000912public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000913 CommandObjectProcessPlugin(CommandInterpreter &interpreter)
914 : CommandObjectProxy(
915 interpreter, "process plugin",
916 "Send a custom command to the current target process plug-in.",
917 "process plugin <args>", 0) {}
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000918
Kate Stoneb9c1b512016-09-06 20:57:50 +0000919 ~CommandObjectProcessPlugin() override = default;
Greg Clayton998255b2012-10-13 02:07:45 +0000920
Kate Stoneb9c1b512016-09-06 20:57:50 +0000921 CommandObject *GetProxyCommandObject() override {
922 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
923 if (process)
924 return process->GetPluginCommandObject();
925 return nullptr;
926 }
Greg Clayton998255b2012-10-13 02:07:45 +0000927};
928
Greg Clayton8f343b02010-11-04 01:54:29 +0000929// CommandObjectProcessLoad
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000930
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000931static constexpr OptionDefinition g_process_load_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000932 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +0000933 { LLDB_OPT_SET_ALL, false, "install", 'i', OptionParser::eOptionalArgument, nullptr, {}, 0, eArgTypePath, "Install the shared library to the target. If specified without an argument then the library will installed in the current working directory." },
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000934 // clang-format on
935};
936
Jim Inghambb9caf72010-12-09 18:58:16 +0000937#pragma mark CommandObjectProcessLoad
Greg Clayton8f343b02010-11-04 01:54:29 +0000938
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939class CommandObjectProcessLoad : public CommandObjectParsed {
Greg Clayton8f343b02010-11-04 01:54:29 +0000940public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 class CommandOptions : public Options {
942 public:
943 CommandOptions() : Options() {
944 // Keep default values of all options in one place: OptionParsingStarting
945 // ()
946 OptionParsingStarting(nullptr);
Greg Clayton8f343b02010-11-04 01:54:29 +0000947 }
948
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949 ~CommandOptions() override = default;
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +0000950
Zachary Turner97206d52017-05-12 04:51:55 +0000951 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
952 ExecutionContext *execution_context) override {
953 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954 const int short_option = m_getopt_table[option_idx].val;
955 switch (short_option) {
956 case 'i':
957 do_install = true;
Zachary Turnerfe114832016-11-12 16:56:47 +0000958 if (!option_arg.empty())
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000959 install_path.SetFile(option_arg, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000960 break;
961 default:
962 error.SetErrorStringWithFormat("invalid short option character '%c'",
963 short_option);
964 break;
965 }
966 return error;
Greg Clayton8f343b02010-11-04 01:54:29 +0000967 }
968
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969 void OptionParsingStarting(ExecutionContext *execution_context) override {
970 do_install = false;
971 install_path.Clear();
972 }
973
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000974 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000975 return llvm::makeArrayRef(g_process_load_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000976 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977
978 // Instance variables to hold the values for command options.
979 bool do_install;
980 FileSpec install_path;
981 };
982
983 CommandObjectProcessLoad(CommandInterpreter &interpreter)
984 : CommandObjectParsed(interpreter, "process load",
985 "Load a shared library into the current process.",
986 "process load <filename> [<filename> ...]",
987 eCommandRequiresProcess | eCommandTryTargetAPILock |
988 eCommandProcessMustBeLaunched |
989 eCommandProcessMustBePaused),
990 m_options() {}
991
992 ~CommandObjectProcessLoad() override = default;
993
994 Options *GetOptions() override { return &m_options; }
995
Jim Ingham5a988412012-06-08 21:56:10 +0000996protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997 bool DoExecute(Args &command, CommandReturnObject &result) override {
998 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton8f343b02010-11-04 01:54:29 +0000999
Zachary Turner97d2c402016-10-05 23:40:23 +00001000 for (auto &entry : command.entries()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001001 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001002 PlatformSP platform = process->GetTarget().GetPlatform();
Zachary Turner97d2c402016-10-05 23:40:23 +00001003 llvm::StringRef image_path = entry.ref;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004 uint32_t image_token = LLDB_INVALID_IMAGE_TOKEN;
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001005
Kate Stoneb9c1b512016-09-06 20:57:50 +00001006 if (!m_options.do_install) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001007 FileSpec image_spec(image_path);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 platform->ResolveRemotePath(image_spec, image_spec);
1009 image_token =
1010 platform->LoadImage(process, FileSpec(), image_spec, error);
1011 } else if (m_options.install_path) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001012 FileSpec image_spec(image_path);
1013 FileSystem::Instance().Resolve(image_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 platform->ResolveRemotePath(m_options.install_path,
1015 m_options.install_path);
1016 image_token = platform->LoadImage(process, image_spec,
1017 m_options.install_path, error);
1018 } else {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001019 FileSpec image_spec(image_path);
1020 FileSystem::Instance().Resolve(image_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021 image_token =
1022 platform->LoadImage(process, image_spec, FileSpec(), error);
1023 }
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001024
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025 if (image_token != LLDB_INVALID_IMAGE_TOKEN) {
1026 result.AppendMessageWithFormat(
Zachary Turner97d2c402016-10-05 23:40:23 +00001027 "Loading \"%s\"...ok\nImage %u loaded.\n", image_path.str().c_str(),
1028 image_token);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 result.SetStatus(eReturnStatusSuccessFinishResult);
1030 } else {
Zachary Turner97d2c402016-10-05 23:40:23 +00001031 result.AppendErrorWithFormat("failed to load '%s': %s",
1032 image_path.str().c_str(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033 error.AsCString());
1034 result.SetStatus(eReturnStatusFailed);
1035 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001036 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037 return result.Succeeded();
1038 }
1039
1040 CommandOptions m_options;
Greg Clayton8f343b02010-11-04 01:54:29 +00001041};
1042
Greg Clayton8f343b02010-11-04 01:54:29 +00001043// CommandObjectProcessUnload
Jim Inghambb9caf72010-12-09 18:58:16 +00001044#pragma mark CommandObjectProcessUnload
Greg Clayton8f343b02010-11-04 01:54:29 +00001045
Kate Stoneb9c1b512016-09-06 20:57:50 +00001046class CommandObjectProcessUnload : public CommandObjectParsed {
Greg Clayton8f343b02010-11-04 01:54:29 +00001047public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048 CommandObjectProcessUnload(CommandInterpreter &interpreter)
1049 : CommandObjectParsed(
1050 interpreter, "process unload",
1051 "Unload a shared library from the current process using the index "
1052 "returned by a previous call to \"process load\".",
1053 "process unload <index>",
1054 eCommandRequiresProcess | eCommandTryTargetAPILock |
1055 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
Greg Clayton8f343b02010-11-04 01:54:29 +00001056
Kate Stoneb9c1b512016-09-06 20:57:50 +00001057 ~CommandObjectProcessUnload() override = default;
Greg Clayton8f343b02010-11-04 01:54:29 +00001058
Jim Ingham5a988412012-06-08 21:56:10 +00001059protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 bool DoExecute(Args &command, CommandReturnObject &result) override {
1061 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton8f343b02010-11-04 01:54:29 +00001062
Zachary Turner97d2c402016-10-05 23:40:23 +00001063 for (auto &entry : command.entries()) {
1064 uint32_t image_token;
1065 if (entry.ref.getAsInteger(0, image_token)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001066 result.AppendErrorWithFormat("invalid image index argument '%s'",
Zachary Turner97d2c402016-10-05 23:40:23 +00001067 entry.ref.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001068 result.SetStatus(eReturnStatusFailed);
1069 break;
1070 } else {
Zachary Turner97206d52017-05-12 04:51:55 +00001071 Status error(process->GetTarget().GetPlatform()->UnloadImage(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001072 process, image_token));
1073 if (error.Success()) {
1074 result.AppendMessageWithFormat(
1075 "Unloading shared library with index %u...ok\n", image_token);
1076 result.SetStatus(eReturnStatusSuccessFinishResult);
1077 } else {
1078 result.AppendErrorWithFormat("failed to unload image: %s",
1079 error.AsCString());
1080 result.SetStatus(eReturnStatusFailed);
1081 break;
Greg Clayton8f343b02010-11-04 01:54:29 +00001082 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001083 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001084 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001085 return result.Succeeded();
1086 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001087};
1088
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089// CommandObjectProcessSignal
Jim Inghambb9caf72010-12-09 18:58:16 +00001090#pragma mark CommandObjectProcessSignal
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001091
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092class CommandObjectProcessSignal : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001093public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 CommandObjectProcessSignal(CommandInterpreter &interpreter)
1095 : CommandObjectParsed(interpreter, "process signal",
1096 "Send a UNIX signal to the current target process.",
1097 nullptr, eCommandRequiresProcess |
1098 eCommandTryTargetAPILock) {
1099 CommandArgumentEntry arg;
1100 CommandArgumentData signal_arg;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 // Define the first (and only) variant of this arg.
1103 signal_arg.arg_type = eArgTypeUnixSignal;
1104 signal_arg.arg_repetition = eArgRepeatPlain;
1105
1106 // There is only one variant this argument could be; put it into the
1107 // argument entry.
1108 arg.push_back(signal_arg);
1109
1110 // Push the data for the first argument into the m_arguments vector.
1111 m_arguments.push_back(arg);
1112 }
1113
1114 ~CommandObjectProcessSignal() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115
Jim Ingham5a988412012-06-08 21:56:10 +00001116protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001117 bool DoExecute(Args &command, CommandReturnObject &result) override {
1118 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 if (command.GetArgumentCount() == 1) {
1121 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1122
1123 const char *signal_name = command.GetArgumentAtIndex(0);
1124 if (::isxdigit(signal_name[0]))
1125 signo =
1126 StringConvert::ToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1127 else
1128 signo = process->GetUnixSignals()->GetSignalNumberFromName(signal_name);
1129
1130 if (signo == LLDB_INVALID_SIGNAL_NUMBER) {
1131 result.AppendErrorWithFormat("Invalid signal argument '%s'.\n",
1132 command.GetArgumentAtIndex(0));
1133 result.SetStatus(eReturnStatusFailed);
1134 } else {
Zachary Turner97206d52017-05-12 04:51:55 +00001135 Status error(process->Signal(signo));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001136 if (error.Success()) {
1137 result.SetStatus(eReturnStatusSuccessFinishResult);
1138 } else {
1139 result.AppendErrorWithFormat("Failed to send signal %i: %s\n", signo,
1140 error.AsCString());
1141 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001142 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001143 }
1144 } else {
1145 result.AppendErrorWithFormat(
1146 "'%s' takes exactly one signal number argument:\nUsage: %s\n",
1147 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1148 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150 return result.Succeeded();
1151 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001152};
1153
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001154// CommandObjectProcessInterrupt
Jim Inghambb9caf72010-12-09 18:58:16 +00001155#pragma mark CommandObjectProcessInterrupt
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156
Kate Stoneb9c1b512016-09-06 20:57:50 +00001157class CommandObjectProcessInterrupt : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 CommandObjectProcessInterrupt(CommandInterpreter &interpreter)
1160 : CommandObjectParsed(interpreter, "process interrupt",
1161 "Interrupt the current target process.",
1162 "process interrupt",
1163 eCommandRequiresProcess | eCommandTryTargetAPILock |
1164 eCommandProcessMustBeLaunched) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165
Kate Stoneb9c1b512016-09-06 20:57:50 +00001166 ~CommandObjectProcessInterrupt() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167
Jim Ingham5a988412012-06-08 21:56:10 +00001168protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001169 bool DoExecute(Args &command, CommandReturnObject &result) override {
1170 Process *process = m_exe_ctx.GetProcessPtr();
1171 if (process == nullptr) {
1172 result.AppendError("no process to halt");
1173 result.SetStatus(eReturnStatusFailed);
1174 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001175 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001176
1177 if (command.GetArgumentCount() == 0) {
1178 bool clear_thread_plans = true;
Zachary Turner97206d52017-05-12 04:51:55 +00001179 Status error(process->Halt(clear_thread_plans));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180 if (error.Success()) {
1181 result.SetStatus(eReturnStatusSuccessFinishResult);
1182 } else {
1183 result.AppendErrorWithFormat("Failed to halt process: %s\n",
1184 error.AsCString());
1185 result.SetStatus(eReturnStatusFailed);
1186 }
1187 } else {
1188 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
1189 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1190 result.SetStatus(eReturnStatusFailed);
1191 }
1192 return result.Succeeded();
1193 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194};
1195
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196// CommandObjectProcessKill
Jim Inghambb9caf72010-12-09 18:58:16 +00001197#pragma mark CommandObjectProcessKill
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199class CommandObjectProcessKill : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001200public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001201 CommandObjectProcessKill(CommandInterpreter &interpreter)
1202 : CommandObjectParsed(interpreter, "process kill",
1203 "Terminate the current target process.",
1204 "process kill",
1205 eCommandRequiresProcess | eCommandTryTargetAPILock |
1206 eCommandProcessMustBeLaunched) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001207
Kate Stoneb9c1b512016-09-06 20:57:50 +00001208 ~CommandObjectProcessKill() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209
Jim Ingham5a988412012-06-08 21:56:10 +00001210protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001211 bool DoExecute(Args &command, CommandReturnObject &result) override {
1212 Process *process = m_exe_ctx.GetProcessPtr();
1213 if (process == nullptr) {
1214 result.AppendError("no process to kill");
1215 result.SetStatus(eReturnStatusFailed);
1216 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218
1219 if (command.GetArgumentCount() == 0) {
Zachary Turner97206d52017-05-12 04:51:55 +00001220 Status error(process->Destroy(true));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001221 if (error.Success()) {
1222 result.SetStatus(eReturnStatusSuccessFinishResult);
1223 } else {
1224 result.AppendErrorWithFormat("Failed to kill process: %s\n",
1225 error.AsCString());
1226 result.SetStatus(eReturnStatusFailed);
1227 }
1228 } else {
1229 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
1230 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1231 result.SetStatus(eReturnStatusFailed);
1232 }
1233 return result.Succeeded();
1234 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001235};
1236
Greg Claytona2715cf2014-06-13 00:54:12 +00001237// CommandObjectProcessSaveCore
Greg Claytona2715cf2014-06-13 00:54:12 +00001238#pragma mark CommandObjectProcessSaveCore
1239
Kate Stoneb9c1b512016-09-06 20:57:50 +00001240class CommandObjectProcessSaveCore : public CommandObjectParsed {
Greg Claytona2715cf2014-06-13 00:54:12 +00001241public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242 CommandObjectProcessSaveCore(CommandInterpreter &interpreter)
1243 : CommandObjectParsed(interpreter, "process save-core",
1244 "Save the current process as a core file using an "
1245 "appropriate file type.",
1246 "process save-core FILE",
1247 eCommandRequiresProcess | eCommandTryTargetAPILock |
1248 eCommandProcessMustBeLaunched) {}
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00001249
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 ~CommandObjectProcessSaveCore() override = default;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00001251
Greg Claytona2715cf2014-06-13 00:54:12 +00001252protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001253 bool DoExecute(Args &command, CommandReturnObject &result) override {
1254 ProcessSP process_sp = m_exe_ctx.GetProcessSP();
1255 if (process_sp) {
1256 if (command.GetArgumentCount() == 1) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001257 FileSpec output_file(command.GetArgumentAtIndex(0));
Zachary Turner97206d52017-05-12 04:51:55 +00001258 Status error = PluginManager::SaveCore(process_sp, output_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 if (error.Success()) {
1260 result.SetStatus(eReturnStatusSuccessFinishResult);
1261 } else {
1262 result.AppendErrorWithFormat(
1263 "Failed to save core file for process: %s\n", error.AsCString());
1264 result.SetStatus(eReturnStatusFailed);
Greg Claytona2715cf2014-06-13 00:54:12 +00001265 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001266 } else {
1267 result.AppendErrorWithFormat("'%s' takes one arguments:\nUsage: %s\n",
1268 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1269 result.SetStatus(eReturnStatusFailed);
1270 }
1271 } else {
1272 result.AppendError("invalid process");
1273 result.SetStatus(eReturnStatusFailed);
1274 return false;
Greg Claytona2715cf2014-06-13 00:54:12 +00001275 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276
1277 return result.Succeeded();
1278 }
Greg Claytona2715cf2014-06-13 00:54:12 +00001279};
1280
Jim Ingham4b9bea82010-06-18 01:23:09 +00001281// CommandObjectProcessStatus
Jim Inghambb9caf72010-12-09 18:58:16 +00001282#pragma mark CommandObjectProcessStatus
1283
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284class CommandObjectProcessStatus : public CommandObjectParsed {
Jim Ingham4b9bea82010-06-18 01:23:09 +00001285public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286 CommandObjectProcessStatus(CommandInterpreter &interpreter)
1287 : CommandObjectParsed(
1288 interpreter, "process status",
1289 "Show status and stop location for the current target process.",
1290 "process status",
1291 eCommandRequiresProcess | eCommandTryTargetAPILock) {}
Jim Ingham4b9bea82010-06-18 01:23:09 +00001292
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293 ~CommandObjectProcessStatus() override = default;
Jim Ingham4b9bea82010-06-18 01:23:09 +00001294
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295 bool DoExecute(Args &command, CommandReturnObject &result) override {
1296 Stream &strm = result.GetOutputStream();
1297 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1298 // No need to check "process" for validity as eCommandRequiresProcess
1299 // ensures it is valid
1300 Process *process = m_exe_ctx.GetProcessPtr();
1301 const bool only_threads_with_stop_reason = true;
1302 const uint32_t start_frame = 0;
1303 const uint32_t num_frames = 1;
1304 const uint32_t num_frames_with_source = 1;
Jim Ingham6a9767c2016-11-08 20:36:40 +00001305 const bool stop_format = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001306 process->GetStatus(strm);
1307 process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame,
Jim Ingham6a9767c2016-11-08 20:36:40 +00001308 num_frames, num_frames_with_source, stop_format);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309 return result.Succeeded();
1310 }
Jim Ingham4b9bea82010-06-18 01:23:09 +00001311};
1312
Caroline Tice35731352010-10-13 20:44:39 +00001313// CommandObjectProcessHandle
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001314
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001315static constexpr OptionDefinition g_process_handle_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001316 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001317 { LLDB_OPT_SET_1, false, "stop", 's', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Whether or not the process should be stopped if the signal is received." },
1318 { LLDB_OPT_SET_1, false, "notify", 'n', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Whether or not the debugger should notify the user if the signal is received." },
1319 { LLDB_OPT_SET_1, false, "pass", 'p', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Whether or not the signal should be passed to the process." }
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001320 // clang-format on
1321};
1322
Jim Inghambb9caf72010-12-09 18:58:16 +00001323#pragma mark CommandObjectProcessHandle
Caroline Tice35731352010-10-13 20:44:39 +00001324
Kate Stoneb9c1b512016-09-06 20:57:50 +00001325class CommandObjectProcessHandle : public CommandObjectParsed {
Caroline Tice35731352010-10-13 20:44:39 +00001326public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001327 class CommandOptions : public Options {
1328 public:
1329 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
Caroline Tice35731352010-10-13 20:44:39 +00001330
Kate Stoneb9c1b512016-09-06 20:57:50 +00001331 ~CommandOptions() override = default;
Caroline Tice35731352010-10-13 20:44:39 +00001332
Zachary Turner97206d52017-05-12 04:51:55 +00001333 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1334 ExecutionContext *execution_context) override {
1335 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 const int short_option = m_getopt_table[option_idx].val;
Caroline Tice35731352010-10-13 20:44:39 +00001337
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 switch (short_option) {
1339 case 's':
1340 stop = option_arg;
1341 break;
1342 case 'n':
1343 notify = option_arg;
1344 break;
1345 case 'p':
1346 pass = option_arg;
1347 break;
1348 default:
1349 error.SetErrorStringWithFormat("invalid short option character '%c'",
1350 short_option);
1351 break;
1352 }
1353 return error;
Caroline Tice35731352010-10-13 20:44:39 +00001354 }
1355
Kate Stoneb9c1b512016-09-06 20:57:50 +00001356 void OptionParsingStarting(ExecutionContext *execution_context) override {
1357 stop.clear();
1358 notify.clear();
1359 pass.clear();
Caroline Tice35731352010-10-13 20:44:39 +00001360 }
1361
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001362 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001363 return llvm::makeArrayRef(g_process_handle_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001364 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365
1366 // Instance variables to hold the values for command options.
1367
1368 std::string stop;
1369 std::string notify;
1370 std::string pass;
1371 };
1372
1373 CommandObjectProcessHandle(CommandInterpreter &interpreter)
1374 : CommandObjectParsed(interpreter, "process handle",
1375 "Manage LLDB handling of OS signals for the "
1376 "current target process. Defaults to showing "
1377 "current policy.",
1378 nullptr),
1379 m_options() {
1380 SetHelpLong("\nIf no signals are specified, update them all. If no update "
1381 "option is specified, list the current values.");
1382 CommandArgumentEntry arg;
1383 CommandArgumentData signal_arg;
1384
1385 signal_arg.arg_type = eArgTypeUnixSignal;
1386 signal_arg.arg_repetition = eArgRepeatStar;
1387
1388 arg.push_back(signal_arg);
1389
1390 m_arguments.push_back(arg);
1391 }
1392
1393 ~CommandObjectProcessHandle() override = default;
1394
1395 Options *GetOptions() override { return &m_options; }
1396
1397 bool VerifyCommandOptionValue(const std::string &option, int &real_value) {
1398 bool okay = true;
1399 bool success = false;
Pavel Labath47cbf4a2018-04-10 09:03:59 +00001400 bool tmp_value = OptionArgParser::ToBoolean(option, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001401
1402 if (success && tmp_value)
1403 real_value = 1;
1404 else if (success && !tmp_value)
1405 real_value = 0;
1406 else {
1407 // If the value isn't 'true' or 'false', it had better be 0 or 1.
1408 real_value = StringConvert::ToUInt32(option.c_str(), 3);
1409 if (real_value != 0 && real_value != 1)
1410 okay = false;
Caroline Tice35731352010-10-13 20:44:39 +00001411 }
1412
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413 return okay;
1414 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001415
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416 void PrintSignalHeader(Stream &str) {
1417 str.Printf("NAME PASS STOP NOTIFY\n");
1418 str.Printf("=========== ===== ===== ======\n");
1419 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001420
Kate Stoneb9c1b512016-09-06 20:57:50 +00001421 void PrintSignal(Stream &str, int32_t signo, const char *sig_name,
1422 const UnixSignalsSP &signals_sp) {
1423 bool stop;
1424 bool suppress;
1425 bool notify;
1426
1427 str.Printf("%-11s ", sig_name);
1428 if (signals_sp->GetSignalInfo(signo, suppress, stop, notify)) {
1429 bool pass = !suppress;
1430 str.Printf("%s %s %s", (pass ? "true " : "false"),
1431 (stop ? "true " : "false"), (notify ? "true " : "false"));
Caroline Tice10ad7992010-10-14 21:31:13 +00001432 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001433 str.Printf("\n");
1434 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001435
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436 void PrintSignalInformation(Stream &str, Args &signal_args,
1437 int num_valid_signals,
1438 const UnixSignalsSP &signals_sp) {
1439 PrintSignalHeader(str);
1440
1441 if (num_valid_signals > 0) {
1442 size_t num_args = signal_args.GetArgumentCount();
1443 for (size_t i = 0; i < num_args; ++i) {
1444 int32_t signo = signals_sp->GetSignalNumberFromName(
1445 signal_args.GetArgumentAtIndex(i));
1446 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1447 PrintSignal(str, signo, signal_args.GetArgumentAtIndex(i),
1448 signals_sp);
1449 }
1450 } else // Print info for ALL signals
Caroline Tice10ad7992010-10-14 21:31:13 +00001451 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001452 int32_t signo = signals_sp->GetFirstSignalNumber();
1453 while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1454 PrintSignal(str, signo, signals_sp->GetSignalAsCString(signo),
1455 signals_sp);
1456 signo = signals_sp->GetNextSignalNumber(signo);
1457 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001458 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001459 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001460
Jim Ingham5a988412012-06-08 21:56:10 +00001461protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462 bool DoExecute(Args &signal_args, CommandReturnObject &result) override {
Jonas Devlieghere57179862019-04-27 06:19:42 +00001463 TargetSP target_sp = GetDebugger().GetSelectedTarget();
Caroline Tice35731352010-10-13 20:44:39 +00001464
Kate Stoneb9c1b512016-09-06 20:57:50 +00001465 if (!target_sp) {
1466 result.AppendError("No current target;"
1467 " cannot handle signals until you have a valid target "
1468 "and process.\n");
1469 result.SetStatus(eReturnStatusFailed);
1470 return false;
Caroline Tice35731352010-10-13 20:44:39 +00001471 }
1472
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473 ProcessSP process_sp = target_sp->GetProcessSP();
1474
1475 if (!process_sp) {
1476 result.AppendError("No current process; cannot handle signals until you "
1477 "have a valid process.\n");
1478 result.SetStatus(eReturnStatusFailed);
1479 return false;
1480 }
1481
1482 int stop_action = -1; // -1 means leave the current setting alone
1483 int pass_action = -1; // -1 means leave the current setting alone
1484 int notify_action = -1; // -1 means leave the current setting alone
1485
1486 if (!m_options.stop.empty() &&
1487 !VerifyCommandOptionValue(m_options.stop, stop_action)) {
1488 result.AppendError("Invalid argument for command option --stop; must be "
1489 "true or false.\n");
1490 result.SetStatus(eReturnStatusFailed);
1491 return false;
1492 }
1493
1494 if (!m_options.notify.empty() &&
1495 !VerifyCommandOptionValue(m_options.notify, notify_action)) {
1496 result.AppendError("Invalid argument for command option --notify; must "
1497 "be true or false.\n");
1498 result.SetStatus(eReturnStatusFailed);
1499 return false;
1500 }
1501
1502 if (!m_options.pass.empty() &&
1503 !VerifyCommandOptionValue(m_options.pass, pass_action)) {
1504 result.AppendError("Invalid argument for command option --pass; must be "
1505 "true or false.\n");
1506 result.SetStatus(eReturnStatusFailed);
1507 return false;
1508 }
1509
1510 size_t num_args = signal_args.GetArgumentCount();
1511 UnixSignalsSP signals_sp = process_sp->GetUnixSignals();
1512 int num_signals_set = 0;
1513
1514 if (num_args > 0) {
Zachary Turnerd6a24752016-11-22 17:10:15 +00001515 for (const auto &arg : signal_args) {
1516 int32_t signo = signals_sp->GetSignalNumberFromName(arg.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001517 if (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1518 // Casting the actions as bools here should be okay, because
Adrian Prantl05097242018-04-30 16:49:04 +00001519 // VerifyCommandOptionValue guarantees the value is either 0 or 1.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001520 if (stop_action != -1)
1521 signals_sp->SetShouldStop(signo, stop_action);
1522 if (pass_action != -1) {
1523 bool suppress = !pass_action;
1524 signals_sp->SetShouldSuppress(signo, suppress);
1525 }
1526 if (notify_action != -1)
1527 signals_sp->SetShouldNotify(signo, notify_action);
1528 ++num_signals_set;
1529 } else {
1530 result.AppendErrorWithFormat("Invalid signal name '%s'\n",
Zachary Turnerd6a24752016-11-22 17:10:15 +00001531 arg.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001532 }
1533 }
1534 } else {
1535 // No signal specified, if any command options were specified, update ALL
1536 // signals.
1537 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1)) {
1538 if (m_interpreter.Confirm(
1539 "Do you really want to update all the signals?", false)) {
1540 int32_t signo = signals_sp->GetFirstSignalNumber();
1541 while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1542 if (notify_action != -1)
1543 signals_sp->SetShouldNotify(signo, notify_action);
1544 if (stop_action != -1)
1545 signals_sp->SetShouldStop(signo, stop_action);
1546 if (pass_action != -1) {
1547 bool suppress = !pass_action;
1548 signals_sp->SetShouldSuppress(signo, suppress);
1549 }
1550 signo = signals_sp->GetNextSignalNumber(signo);
1551 }
1552 }
1553 }
1554 }
1555
1556 PrintSignalInformation(result.GetOutputStream(), signal_args,
1557 num_signals_set, signals_sp);
1558
1559 if (num_signals_set > 0)
1560 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1561 else
1562 result.SetStatus(eReturnStatusFailed);
1563
1564 return result.Succeeded();
1565 }
1566
1567 CommandOptions m_options;
Caroline Tice35731352010-10-13 20:44:39 +00001568};
1569
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001570// CommandObjectMultiwordProcess
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571
Kate Stoneb9c1b512016-09-06 20:57:50 +00001572CommandObjectMultiwordProcess::CommandObjectMultiwordProcess(
1573 CommandInterpreter &interpreter)
1574 : CommandObjectMultiword(
1575 interpreter, "process",
1576 "Commands for interacting with processes on the current platform.",
1577 "process <subcommand> [<subcommand-options>]") {
1578 LoadSubCommand("attach",
1579 CommandObjectSP(new CommandObjectProcessAttach(interpreter)));
1580 LoadSubCommand("launch",
1581 CommandObjectSP(new CommandObjectProcessLaunch(interpreter)));
1582 LoadSubCommand("continue", CommandObjectSP(new CommandObjectProcessContinue(
1583 interpreter)));
1584 LoadSubCommand("connect",
1585 CommandObjectSP(new CommandObjectProcessConnect(interpreter)));
1586 LoadSubCommand("detach",
1587 CommandObjectSP(new CommandObjectProcessDetach(interpreter)));
1588 LoadSubCommand("load",
1589 CommandObjectSP(new CommandObjectProcessLoad(interpreter)));
1590 LoadSubCommand("unload",
1591 CommandObjectSP(new CommandObjectProcessUnload(interpreter)));
1592 LoadSubCommand("signal",
1593 CommandObjectSP(new CommandObjectProcessSignal(interpreter)));
1594 LoadSubCommand("handle",
1595 CommandObjectSP(new CommandObjectProcessHandle(interpreter)));
1596 LoadSubCommand("status",
1597 CommandObjectSP(new CommandObjectProcessStatus(interpreter)));
1598 LoadSubCommand("interrupt", CommandObjectSP(new CommandObjectProcessInterrupt(
1599 interpreter)));
1600 LoadSubCommand("kill",
1601 CommandObjectSP(new CommandObjectProcessKill(interpreter)));
1602 LoadSubCommand("plugin",
1603 CommandObjectSP(new CommandObjectProcessPlugin(interpreter)));
1604 LoadSubCommand("save-core", CommandObjectSP(new CommandObjectProcessSaveCore(
1605 interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001606}
1607
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00001608CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess() = default;