blob: 10af1a35872d80d4cbdbf43ca612720386314ddd [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 Isemannae34ed22019-08-22 07:41:23 +0000130 void
131 HandleArgumentCompletion(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);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 }
138
139 Options *GetOptions() override { return &m_options; }
140
141 const char *GetRepeatCommand(Args &current_command_args,
142 uint32_t index) override {
143 // No repeat for "process launch"...
144 return "";
145 }
Jim Ingham5a988412012-06-08 21:56:10 +0000146
147protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 bool DoExecute(Args &launch_args, CommandReturnObject &result) override {
Jonas Devlieghere57179862019-04-27 06:19:42 +0000149 Debugger &debugger = GetDebugger();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 Target *target = debugger.GetSelectedTarget().get();
151 // If our listener is nullptr, users aren't allows to launch
152 ModuleSP exe_module_sp = target->GetExecutableModule();
Greg Clayton71337622011-02-24 22:24:29 +0000153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 if (exe_module_sp == nullptr) {
155 result.AppendError("no file in target, create a debug target using the "
156 "'target create' command");
157 result.SetStatus(eReturnStatusFailed);
158 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159 }
160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 StateType state = eStateInvalid;
162
163 if (!StopProcessIfNecessary(m_exe_ctx.GetProcessPtr(), state, result))
164 return false;
165
Zachary Turner31d97a52016-11-17 18:08:12 +0000166 llvm::StringRef target_settings_argv0 = target->GetArg0();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167
168 // Determine whether we will disable ASLR or leave it in the default state
Adrian Prantl05097242018-04-30 16:49:04 +0000169 // (i.e. enabled if the platform supports it). First check if the process
170 // launch options explicitly turn on/off
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 // disabling ASLR. If so, use that setting;
172 // otherwise, use the 'settings target.disable-aslr' setting.
173 bool disable_aslr = false;
174 if (m_options.disable_aslr != eLazyBoolCalculate) {
Adrian Prantl05097242018-04-30 16:49:04 +0000175 // The user specified an explicit setting on the process launch line.
176 // Use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 disable_aslr = (m_options.disable_aslr == eLazyBoolYes);
178 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000179 // The user did not explicitly specify whether to disable ASLR. Fall
180 // back to the target.disable-aslr setting.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 disable_aslr = target->GetDisableASLR();
182 }
183
184 if (disable_aslr)
185 m_options.launch_info.GetFlags().Set(eLaunchFlagDisableASLR);
186 else
187 m_options.launch_info.GetFlags().Clear(eLaunchFlagDisableASLR);
188
189 if (target->GetDetachOnError())
190 m_options.launch_info.GetFlags().Set(eLaunchFlagDetachOnError);
191
192 if (target->GetDisableSTDIO())
193 m_options.launch_info.GetFlags().Set(eLaunchFlagDisableSTDIO);
194
Jonas Devlieghereae54fc92019-05-13 19:17:48 +0000195 // Merge the launch info environment with the target environment.
196 Environment target_env = target->GetEnvironment();
197 m_options.launch_info.GetEnvironment().insert(target_env.begin(),
198 target_env.end());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199
Zachary Turner31d97a52016-11-17 18:08:12 +0000200 if (!target_settings_argv0.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 m_options.launch_info.GetArguments().AppendArgument(
Zachary Turner31d97a52016-11-17 18:08:12 +0000202 target_settings_argv0);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 m_options.launch_info.SetExecutableFile(
204 exe_module_sp->GetPlatformFileSpec(), false);
205 } else {
206 m_options.launch_info.SetExecutableFile(
207 exe_module_sp->GetPlatformFileSpec(), true);
208 }
209
210 if (launch_args.GetArgumentCount() == 0) {
211 m_options.launch_info.GetArguments().AppendArguments(
212 target->GetProcessLaunchInfo().GetArguments());
213 } else {
214 m_options.launch_info.GetArguments().AppendArguments(launch_args);
215 // Save the arguments for subsequent runs in the current target.
216 target->SetRunArguments(launch_args);
217 }
218
219 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +0000220 Status error = target->Launch(m_options.launch_info, &stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221
222 if (error.Success()) {
223 ProcessSP process_sp(target->GetProcessSP());
224 if (process_sp) {
225 // There is a race condition where this thread will return up the call
Adrian Prantl05097242018-04-30 16:49:04 +0000226 // stack to the main command handler and show an (lldb) prompt before
227 // HandlePrivateEvent (from PrivateStateThread) has a chance to call
228 // PushProcessIOHandler().
Pavel Labath3879fe02018-05-09 14:29:30 +0000229 process_sp->SyncIOHandler(0, std::chrono::seconds(2));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230
Zachary Turnerc1564272016-11-16 21:15:24 +0000231 llvm::StringRef data = stream.GetString();
232 if (!data.empty())
233 result.AppendMessage(data);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 const char *archname =
235 exe_module_sp->GetArchitecture().GetArchitectureName();
236 result.AppendMessageWithFormat(
237 "Process %" PRIu64 " launched: '%s' (%s)\n", process_sp->GetID(),
238 exe_module_sp->GetFileSpec().GetPath().c_str(), archname);
239 result.SetStatus(eReturnStatusSuccessFinishResult);
240 result.SetDidChangeProcessState(true);
241 } else {
242 result.AppendError(
243 "no error returned from Target::Launch, and target has no process");
244 result.SetStatus(eReturnStatusFailed);
245 }
246 } else {
247 result.AppendError(error.AsCString());
248 result.SetStatus(eReturnStatusFailed);
249 }
250 return result.Succeeded();
251 }
252
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 ProcessLaunchCommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255};
256
Raphael Isemann438dfcf2019-07-23 12:54:33 +0000257#define LLDB_OPTIONS_process_attach
258#include "CommandOptions.inc"
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000259
Jim Inghambb9caf72010-12-09 18:58:16 +0000260#pragma mark CommandObjectProcessAttach
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 class CommandOptions : public Options {
264 public:
265 CommandOptions() : Options() {
266 // Keep default values of all options in one place: OptionParsingStarting
267 // ()
268 OptionParsingStarting(nullptr);
Jim Ingham5aee1622010-08-09 23:31:02 +0000269 }
270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 ~CommandOptions() override = default;
Jim Ingham5aee1622010-08-09 23:31:02 +0000272
Zachary Turner97206d52017-05-12 04:51:55 +0000273 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
274 ExecutionContext *execution_context) override {
275 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 const int short_option = m_getopt_table[option_idx].val;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 switch (short_option) {
278 case 'c':
279 attach_info.SetContinueOnceAttached(true);
280 break;
281
282 case 'p': {
Zachary Turnerfe114832016-11-12 16:56:47 +0000283 lldb::pid_t pid;
284 if (option_arg.getAsInteger(0, pid)) {
285 error.SetErrorStringWithFormat("invalid process ID '%s'",
286 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287 } else {
288 attach_info.SetProcessID(pid);
289 }
290 } break;
291
292 case 'P':
293 attach_info.SetProcessPluginName(option_arg);
294 break;
295
296 case 'n':
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000297 attach_info.GetExecutableFile().SetFile(option_arg,
Jonas Devlieghere937348c2018-06-13 22:08:14 +0000298 FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 break;
300
301 case 'w':
302 attach_info.SetWaitForLaunch(true);
303 break;
304
305 case 'i':
306 attach_info.SetIgnoreExisting(false);
307 break;
308
309 default:
Raphael Isemann36162012019-08-22 08:08:05 +0000310 llvm_unreachable("Unimplemented option");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311 }
312 return error;
Jim Ingham5a988412012-06-08 21:56:10 +0000313 }
314
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315 void OptionParsingStarting(ExecutionContext *execution_context) override {
316 attach_info.Clear();
317 }
318
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000319 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000320 return llvm::makeArrayRef(g_process_attach_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000321 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322
323 bool HandleOptionArgumentCompletion(
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000324 CompletionRequest &request, OptionElementVector &opt_element_vector,
325 int opt_element_index, CommandInterpreter &interpreter) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos;
327 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
328
329 // We are only completing the name option for now...
330
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000331 if (GetDefinitions()[opt_defs_index].short_option == 'n') {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 // Are we in the name?
333
334 // Look to see if there is a -P argument provided, and if so use that
Adrian Prantl05097242018-04-30 16:49:04 +0000335 // plugin, otherwise use the default plugin.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336
337 const char *partial_name = nullptr;
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000338 partial_name = request.GetParsedLine().GetArgumentAtIndex(opt_arg_pos);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339
340 PlatformSP platform_sp(interpreter.GetPlatform(true));
341 if (platform_sp) {
342 ProcessInstanceInfoList process_infos;
343 ProcessInstanceInfoMatch match_info;
344 if (partial_name) {
345 match_info.GetProcessInfo().GetExecutableFile().SetFile(
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000346 partial_name, FileSpec::Style::native);
Pavel Labathc4a33952017-02-20 11:35:33 +0000347 match_info.SetNameMatchType(NameMatch::StartsWith);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 }
349 platform_sp->FindProcesses(match_info, process_infos);
350 const size_t num_matches = process_infos.GetSize();
351 if (num_matches > 0) {
352 for (size_t i = 0; i < num_matches; ++i) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000353 request.AddCompletion(llvm::StringRef(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 process_infos.GetProcessNameAtIndex(i),
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000355 process_infos.GetProcessNameLengthAtIndex(i)));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 }
357 }
358 }
359 }
360
361 return false;
362 }
363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 // Instance variables to hold the values for command options.
365
366 ProcessAttachInfo attach_info;
367 };
368
369 CommandObjectProcessAttach(CommandInterpreter &interpreter)
370 : CommandObjectProcessLaunchOrAttach(
371 interpreter, "process attach", "Attach to a process.",
372 "process attach <cmd-options>", 0, "attach"),
373 m_options() {}
374
375 ~CommandObjectProcessAttach() override = default;
376
377 Options *GetOptions() override { return &m_options; }
378
Jim Ingham5a988412012-06-08 21:56:10 +0000379protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 bool DoExecute(Args &command, CommandReturnObject &result) override {
381 PlatformSP platform_sp(
Jonas Devlieghere57179862019-04-27 06:19:42 +0000382 GetDebugger().GetPlatformList().GetSelectedPlatform());
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000383
Jonas Devlieghere57179862019-04-27 06:19:42 +0000384 Target *target = GetDebugger().GetSelectedTarget().get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 // N.B. The attach should be synchronous. It doesn't help much to get the
Adrian Prantl05097242018-04-30 16:49:04 +0000386 // prompt back between initiating the attach and the target actually
387 // stopping. So even if the interpreter is set to be asynchronous, we wait
388 // for the stop ourselves here.
Jim Ingham5aee1622010-08-09 23:31:02 +0000389
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390 StateType state = eStateInvalid;
391 Process *process = m_exe_ctx.GetProcessPtr();
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 if (!StopProcessIfNecessary(process, state, result))
394 return false;
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000395
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 if (target == nullptr) {
397 // If there isn't a current target create one.
398 TargetSP new_target_sp;
Zachary Turner97206d52017-05-12 04:51:55 +0000399 Status error;
Oleksiy Vyalov926af0c2015-02-03 00:04:35 +0000400
Jonas Devlieghere57179862019-04-27 06:19:42 +0000401 error = GetDebugger().GetTargetList().CreateTarget(
402 GetDebugger(), "", "", eLoadDependentsNo,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 nullptr, // No platform options
404 new_target_sp);
405 target = new_target_sp.get();
406 if (target == nullptr || error.Fail()) {
407 result.AppendError(error.AsCString("Error creating target"));
408 return false;
409 }
Jonas Devlieghere57179862019-04-27 06:19:42 +0000410 GetDebugger().GetTargetList().SetSelectedTarget(target);
Jim Ingham5aee1622010-08-09 23:31:02 +0000411 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412
413 // Record the old executable module, we want to issue a warning if the
Adrian Prantl05097242018-04-30 16:49:04 +0000414 // process of attaching changed the current executable (like somebody said
415 // "file foo" then attached to a PID whose executable was bar.)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416
417 ModuleSP old_exec_module_sp = target->GetExecutableModule();
418 ArchSpec old_arch_spec = target->GetArchitecture();
419
420 if (command.GetArgumentCount()) {
421 result.AppendErrorWithFormat("Invalid arguments for '%s'.\nUsage: %s\n",
422 m_cmd_name.c_str(), m_cmd_syntax.c_str());
423 result.SetStatus(eReturnStatusFailed);
424 return false;
425 }
426
427 m_interpreter.UpdateExecutionContext(nullptr);
428 StreamString stream;
429 const auto error = target->Attach(m_options.attach_info, &stream);
430 if (error.Success()) {
431 ProcessSP process_sp(target->GetProcessSP());
432 if (process_sp) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000433 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 result.SetStatus(eReturnStatusSuccessFinishNoResult);
435 result.SetDidChangeProcessState(true);
436 result.SetAbnormalStopWasExpected(true);
437 } else {
438 result.AppendError(
439 "no error returned from Target::Attach, and target has no process");
440 result.SetStatus(eReturnStatusFailed);
441 }
442 } else {
443 result.AppendErrorWithFormat("attach failed: %s\n", error.AsCString());
444 result.SetStatus(eReturnStatusFailed);
445 }
446
447 if (!result.Succeeded())
448 return false;
449
450 // Okay, we're done. Last step is to warn if the executable module has
451 // changed:
452 char new_path[PATH_MAX];
453 ModuleSP new_exec_module_sp(target->GetExecutableModule());
454 if (!old_exec_module_sp) {
455 // We might not have a module if we attached to a raw pid...
456 if (new_exec_module_sp) {
457 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
458 result.AppendMessageWithFormat("Executable module set to \"%s\".\n",
459 new_path);
460 }
461 } else if (old_exec_module_sp->GetFileSpec() !=
462 new_exec_module_sp->GetFileSpec()) {
463 char old_path[PATH_MAX];
464
465 old_exec_module_sp->GetFileSpec().GetPath(old_path, PATH_MAX);
466 new_exec_module_sp->GetFileSpec().GetPath(new_path, PATH_MAX);
467
468 result.AppendWarningWithFormat(
469 "Executable module changed from \"%s\" to \"%s\".\n", old_path,
470 new_path);
471 }
472
473 if (!old_arch_spec.IsValid()) {
474 result.AppendMessageWithFormat(
475 "Architecture set to: %s.\n",
476 target->GetArchitecture().GetTriple().getTriple().c_str());
477 } else if (!old_arch_spec.IsExactMatch(target->GetArchitecture())) {
478 result.AppendWarningWithFormat(
479 "Architecture changed from %s to %s.\n",
480 old_arch_spec.GetTriple().getTriple().c_str(),
481 target->GetArchitecture().GetTriple().getTriple().c_str());
482 }
483
Adrian Prantl05097242018-04-30 16:49:04 +0000484 // This supports the use-case scenario of immediately continuing the
485 // process once attached.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 if (m_options.attach_info.GetContinueOnceAttached())
487 m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
488
489 return result.Succeeded();
490 }
491
492 CommandOptions m_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493};
494
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495// CommandObjectProcessContinue
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000496
Raphael Isemann438dfcf2019-07-23 12:54:33 +0000497#define LLDB_OPTIONS_process_continue
498#include "CommandOptions.inc"
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000499
Jim Inghambb9caf72010-12-09 18:58:16 +0000500#pragma mark CommandObjectProcessContinue
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502class CommandObjectProcessContinue : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 CommandObjectProcessContinue(CommandInterpreter &interpreter)
505 : CommandObjectParsed(
506 interpreter, "process continue",
507 "Continue execution of all threads in the current process.",
508 "process continue",
509 eCommandRequiresProcess | eCommandTryTargetAPILock |
510 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
511 m_options() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 ~CommandObjectProcessContinue() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514
Jim Ingham5a988412012-06-08 21:56:10 +0000515protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 class CommandOptions : public Options {
517 public:
518 CommandOptions() : Options() {
519 // Keep default values of all options in one place: OptionParsingStarting
520 // ()
521 OptionParsingStarting(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522 }
Jim Ingham0e410842012-08-11 01:27:55 +0000523
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524 ~CommandOptions() override = default;
525
Zachary Turner97206d52017-05-12 04:51:55 +0000526 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
527 ExecutionContext *execution_context) override {
528 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 const int short_option = m_getopt_table[option_idx].val;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 switch (short_option) {
531 case 'i':
Zachary Turnerfe114832016-11-12 16:56:47 +0000532 if (option_arg.getAsInteger(0, m_ignore))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533 error.SetErrorStringWithFormat(
534 "invalid value for ignore option: \"%s\", should be a number.",
Zachary Turnerfe114832016-11-12 16:56:47 +0000535 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536 break;
537
538 default:
Raphael Isemann36162012019-08-22 08:08:05 +0000539 llvm_unreachable("Unimplemented option");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540 }
541 return error;
Jim Ingham0e410842012-08-11 01:27:55 +0000542 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543
544 void OptionParsingStarting(ExecutionContext *execution_context) override {
545 m_ignore = 0;
546 }
547
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000548 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000549 return llvm::makeArrayRef(g_process_continue_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000550 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551
552 uint32_t m_ignore;
553 };
554
555 bool DoExecute(Args &command, CommandReturnObject &result) override {
556 Process *process = m_exe_ctx.GetProcessPtr();
557 bool synchronous_execution = m_interpreter.GetSynchronous();
558 StateType state = process->GetState();
559 if (state == eStateStopped) {
560 if (command.GetArgumentCount() != 0) {
561 result.AppendErrorWithFormat(
562 "The '%s' command does not take any arguments.\n",
563 m_cmd_name.c_str());
564 result.SetStatus(eReturnStatusFailed);
565 return false;
566 }
567
568 if (m_options.m_ignore > 0) {
569 ThreadSP sel_thread_sp(GetDefaultThread()->shared_from_this());
570 if (sel_thread_sp) {
571 StopInfoSP stop_info_sp = sel_thread_sp->GetStopInfo();
572 if (stop_info_sp &&
573 stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
574 lldb::break_id_t bp_site_id =
575 (lldb::break_id_t)stop_info_sp->GetValue();
576 BreakpointSiteSP bp_site_sp(
577 process->GetBreakpointSiteList().FindByID(bp_site_id));
578 if (bp_site_sp) {
579 const size_t num_owners = bp_site_sp->GetNumberOfOwners();
580 for (size_t i = 0; i < num_owners; i++) {
581 Breakpoint &bp_ref =
582 bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
583 if (!bp_ref.IsInternal()) {
584 bp_ref.SetIgnoreCount(m_options.m_ignore);
585 }
586 }
587 }
588 }
589 }
590 }
591
592 { // Scope for thread list mutex:
593 std::lock_guard<std::recursive_mutex> guard(
594 process->GetThreadList().GetMutex());
595 const uint32_t num_threads = process->GetThreadList().GetSize();
596
597 // Set the actions that the threads should each take when resuming
598 for (uint32_t idx = 0; idx < num_threads; ++idx) {
599 const bool override_suspend = false;
600 process->GetThreadList().GetThreadAtIndex(idx)->SetResumeState(
601 eStateRunning, override_suspend);
602 }
603 }
604
605 const uint32_t iohandler_id = process->GetIOHandlerID();
606
607 StreamString stream;
Zachary Turner97206d52017-05-12 04:51:55 +0000608 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609 if (synchronous_execution)
610 error = process->ResumeSynchronous(&stream);
611 else
612 error = process->Resume();
613
614 if (error.Success()) {
615 // There is a race condition where this thread will return up the call
Adrian Prantl05097242018-04-30 16:49:04 +0000616 // stack to the main command handler and show an (lldb) prompt before
617 // HandlePrivateEvent (from PrivateStateThread) has a chance to call
618 // PushProcessIOHandler().
Pavel Labath3879fe02018-05-09 14:29:30 +0000619 process->SyncIOHandler(iohandler_id, std::chrono::seconds(2));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620
621 result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n",
622 process->GetID());
623 if (synchronous_execution) {
624 // If any state changed events had anything to say, add that to the
625 // result
Zachary Turnerc1564272016-11-16 21:15:24 +0000626 result.AppendMessage(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627
628 result.SetDidChangeProcessState(true);
629 result.SetStatus(eReturnStatusSuccessFinishNoResult);
630 } else {
631 result.SetStatus(eReturnStatusSuccessContinuingNoResult);
632 }
633 } else {
634 result.AppendErrorWithFormat("Failed to resume process: %s.\n",
635 error.AsCString());
636 result.SetStatus(eReturnStatusFailed);
637 }
638 } else {
639 result.AppendErrorWithFormat(
640 "Process cannot be continued from its current state (%s).\n",
641 StateAsCString(state));
642 result.SetStatus(eReturnStatusFailed);
643 }
644 return result.Succeeded();
645 }
646
647 Options *GetOptions() override { return &m_options; }
648
649 CommandOptions m_options;
Jim Ingham0e410842012-08-11 01:27:55 +0000650};
651
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652// CommandObjectProcessDetach
Raphael Isemann438dfcf2019-07-23 12:54:33 +0000653#define LLDB_OPTIONS_process_detach
654#include "CommandOptions.inc"
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000655
Jim Inghambb9caf72010-12-09 18:58:16 +0000656#pragma mark CommandObjectProcessDetach
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658class CommandObjectProcessDetach : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660 class CommandOptions : public Options {
661 public:
662 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
663
664 ~CommandOptions() override = default;
665
Zachary Turner97206d52017-05-12 04:51:55 +0000666 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
667 ExecutionContext *execution_context) override {
668 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 const int short_option = m_getopt_table[option_idx].val;
670
671 switch (short_option) {
672 case 's':
673 bool tmp_result;
674 bool success;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000675 tmp_result = OptionArgParser::ToBoolean(option_arg, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676 if (!success)
677 error.SetErrorStringWithFormat("invalid boolean option: \"%s\"",
Zachary Turnerfe114832016-11-12 16:56:47 +0000678 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679 else {
680 if (tmp_result)
681 m_keep_stopped = eLazyBoolYes;
682 else
683 m_keep_stopped = eLazyBoolNo;
Jim Inghamacff8952013-05-02 00:27:30 +0000684 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685 break;
686 default:
Raphael Isemann36162012019-08-22 08:08:05 +0000687 llvm_unreachable("Unimplemented option");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688 }
689 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690 }
691
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692 void OptionParsingStarting(ExecutionContext *execution_context) override {
693 m_keep_stopped = eLazyBoolCalculate;
Jim Inghamacff8952013-05-02 00:27:30 +0000694 }
695
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000696 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000697 return llvm::makeArrayRef(g_process_detach_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000698 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699
700 // Instance variables to hold the values for command options.
701 LazyBool m_keep_stopped;
702 };
703
704 CommandObjectProcessDetach(CommandInterpreter &interpreter)
705 : CommandObjectParsed(interpreter, "process detach",
706 "Detach from the current target process.",
707 "process detach",
708 eCommandRequiresProcess | eCommandTryTargetAPILock |
709 eCommandProcessMustBeLaunched),
710 m_options() {}
711
712 ~CommandObjectProcessDetach() override = default;
713
714 Options *GetOptions() override { return &m_options; }
715
Jim Ingham5a988412012-06-08 21:56:10 +0000716protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717 bool DoExecute(Args &command, CommandReturnObject &result) override {
718 Process *process = m_exe_ctx.GetProcessPtr();
719 // FIXME: This will be a Command Option:
720 bool keep_stopped;
721 if (m_options.m_keep_stopped == eLazyBoolCalculate) {
722 // Check the process default:
723 keep_stopped = process->GetDetachKeepsStopped();
724 } else if (m_options.m_keep_stopped == eLazyBoolYes)
725 keep_stopped = true;
726 else
727 keep_stopped = false;
Jim Inghamacff8952013-05-02 00:27:30 +0000728
Zachary Turner97206d52017-05-12 04:51:55 +0000729 Status error(process->Detach(keep_stopped));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730 if (error.Success()) {
731 result.SetStatus(eReturnStatusSuccessFinishResult);
732 } else {
733 result.AppendErrorWithFormat("Detach failed: %s\n", error.AsCString());
734 result.SetStatus(eReturnStatusFailed);
735 return false;
736 }
737 return result.Succeeded();
738 }
739
740 CommandOptions m_options;
Jim Inghamacff8952013-05-02 00:27:30 +0000741};
742
Greg Claytonb766a732011-02-04 01:58:07 +0000743// CommandObjectProcessConnect
Raphael Isemann438dfcf2019-07-23 12:54:33 +0000744#define LLDB_OPTIONS_process_connect
745#include "CommandOptions.inc"
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000746
Greg Claytonb766a732011-02-04 01:58:07 +0000747#pragma mark CommandObjectProcessConnect
748
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749class CommandObjectProcessConnect : public CommandObjectParsed {
Greg Claytonb766a732011-02-04 01:58:07 +0000750public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751 class CommandOptions : public Options {
752 public:
753 CommandOptions() : Options() {
754 // Keep default values of all options in one place: OptionParsingStarting
755 // ()
756 OptionParsingStarting(nullptr);
Greg Claytonb766a732011-02-04 01:58:07 +0000757 }
Greg Claytonb766a732011-02-04 01:58:07 +0000758
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759 ~CommandOptions() override = default;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000760
Zachary Turner97206d52017-05-12 04:51:55 +0000761 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
762 ExecutionContext *execution_context) override {
763 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 const int short_option = m_getopt_table[option_idx].val;
765
766 switch (short_option) {
767 case 'p':
768 plugin_name.assign(option_arg);
769 break;
770
771 default:
Raphael Isemann36162012019-08-22 08:08:05 +0000772 llvm_unreachable("Unimplemented option");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 }
774 return error;
Jim Ingham5a988412012-06-08 21:56:10 +0000775 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776
777 void OptionParsingStarting(ExecutionContext *execution_context) override {
778 plugin_name.clear();
779 }
780
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000781 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000782 return llvm::makeArrayRef(g_process_connect_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000783 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784
785 // Instance variables to hold the values for command options.
786
787 std::string plugin_name;
788 };
789
790 CommandObjectProcessConnect(CommandInterpreter &interpreter)
791 : CommandObjectParsed(interpreter, "process connect",
792 "Connect to a remote debug service.",
793 "process connect <remote-url>", 0),
794 m_options() {}
795
796 ~CommandObjectProcessConnect() override = default;
797
798 Options *GetOptions() override { return &m_options; }
799
Jim Ingham5a988412012-06-08 21:56:10 +0000800protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000801 bool DoExecute(Args &command, CommandReturnObject &result) override {
802 if (command.GetArgumentCount() != 1) {
803 result.AppendErrorWithFormat(
804 "'%s' takes exactly one argument:\nUsage: %s\n", m_cmd_name.c_str(),
805 m_cmd_syntax.c_str());
806 result.SetStatus(eReturnStatusFailed);
807 return false;
Greg Claytonb766a732011-02-04 01:58:07 +0000808 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +0000809
Kate Stoneb9c1b512016-09-06 20:57:50 +0000810 Process *process = m_exe_ctx.GetProcessPtr();
811 if (process && process->IsAlive()) {
812 result.AppendErrorWithFormat(
813 "Process %" PRIu64
814 " is currently being debugged, kill the process before connecting.\n",
815 process->GetID());
816 result.SetStatus(eReturnStatusFailed);
817 return false;
818 }
819
820 const char *plugin_name = nullptr;
821 if (!m_options.plugin_name.empty())
822 plugin_name = m_options.plugin_name.c_str();
823
Zachary Turner97206d52017-05-12 04:51:55 +0000824 Status error;
Jonas Devlieghere57179862019-04-27 06:19:42 +0000825 Debugger &debugger = GetDebugger();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000826 PlatformSP platform_sp = m_interpreter.GetPlatform(true);
827 ProcessSP process_sp = platform_sp->ConnectProcess(
828 command.GetArgumentAtIndex(0), plugin_name, debugger,
829 debugger.GetSelectedTarget().get(), error);
830 if (error.Fail() || process_sp == nullptr) {
831 result.AppendError(error.AsCString("Error connecting to the process"));
832 result.SetStatus(eReturnStatusFailed);
833 return false;
834 }
835 return true;
836 }
837
838 CommandOptions m_options;
Greg Claytonb766a732011-02-04 01:58:07 +0000839};
840
Greg Clayton998255b2012-10-13 02:07:45 +0000841// CommandObjectProcessPlugin
Greg Clayton998255b2012-10-13 02:07:45 +0000842#pragma mark CommandObjectProcessPlugin
843
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844class CommandObjectProcessPlugin : public CommandObjectProxy {
Greg Clayton998255b2012-10-13 02:07:45 +0000845public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000846 CommandObjectProcessPlugin(CommandInterpreter &interpreter)
847 : CommandObjectProxy(
848 interpreter, "process plugin",
849 "Send a custom command to the current target process plug-in.",
850 "process plugin <args>", 0) {}
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000851
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 ~CommandObjectProcessPlugin() override = default;
Greg Clayton998255b2012-10-13 02:07:45 +0000853
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854 CommandObject *GetProxyCommandObject() override {
855 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
856 if (process)
857 return process->GetPluginCommandObject();
858 return nullptr;
859 }
Greg Clayton998255b2012-10-13 02:07:45 +0000860};
861
Greg Clayton8f343b02010-11-04 01:54:29 +0000862// CommandObjectProcessLoad
Raphael Isemann438dfcf2019-07-23 12:54:33 +0000863#define LLDB_OPTIONS_process_load
864#include "CommandOptions.inc"
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000865
Jim Inghambb9caf72010-12-09 18:58:16 +0000866#pragma mark CommandObjectProcessLoad
Greg Clayton8f343b02010-11-04 01:54:29 +0000867
Kate Stoneb9c1b512016-09-06 20:57:50 +0000868class CommandObjectProcessLoad : public CommandObjectParsed {
Greg Clayton8f343b02010-11-04 01:54:29 +0000869public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870 class CommandOptions : public Options {
871 public:
872 CommandOptions() : Options() {
873 // Keep default values of all options in one place: OptionParsingStarting
874 // ()
875 OptionParsingStarting(nullptr);
Greg Clayton8f343b02010-11-04 01:54:29 +0000876 }
877
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878 ~CommandOptions() override = default;
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +0000879
Zachary Turner97206d52017-05-12 04:51:55 +0000880 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
881 ExecutionContext *execution_context) override {
882 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883 const int short_option = m_getopt_table[option_idx].val;
884 switch (short_option) {
885 case 'i':
886 do_install = true;
Zachary Turnerfe114832016-11-12 16:56:47 +0000887 if (!option_arg.empty())
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000888 install_path.SetFile(option_arg, FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000889 break;
890 default:
Raphael Isemann36162012019-08-22 08:08:05 +0000891 llvm_unreachable("Unimplemented option");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000892 }
893 return error;
Greg Clayton8f343b02010-11-04 01:54:29 +0000894 }
895
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896 void OptionParsingStarting(ExecutionContext *execution_context) override {
897 do_install = false;
898 install_path.Clear();
899 }
900
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000901 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000902 return llvm::makeArrayRef(g_process_load_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000903 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904
905 // Instance variables to hold the values for command options.
906 bool do_install;
907 FileSpec install_path;
908 };
909
910 CommandObjectProcessLoad(CommandInterpreter &interpreter)
911 : CommandObjectParsed(interpreter, "process load",
912 "Load a shared library into the current process.",
913 "process load <filename> [<filename> ...]",
914 eCommandRequiresProcess | eCommandTryTargetAPILock |
915 eCommandProcessMustBeLaunched |
916 eCommandProcessMustBePaused),
917 m_options() {}
918
919 ~CommandObjectProcessLoad() override = default;
920
921 Options *GetOptions() override { return &m_options; }
922
Jim Ingham5a988412012-06-08 21:56:10 +0000923protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924 bool DoExecute(Args &command, CommandReturnObject &result) override {
925 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton8f343b02010-11-04 01:54:29 +0000926
Zachary Turner97d2c402016-10-05 23:40:23 +0000927 for (auto &entry : command.entries()) {
Zachary Turner97206d52017-05-12 04:51:55 +0000928 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929 PlatformSP platform = process->GetTarget().GetPlatform();
Zachary Turner97d2c402016-10-05 23:40:23 +0000930 llvm::StringRef image_path = entry.ref;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000931 uint32_t image_token = LLDB_INVALID_IMAGE_TOKEN;
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +0000932
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933 if (!m_options.do_install) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000934 FileSpec image_spec(image_path);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000935 platform->ResolveRemotePath(image_spec, image_spec);
936 image_token =
937 platform->LoadImage(process, FileSpec(), image_spec, error);
938 } else if (m_options.install_path) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000939 FileSpec image_spec(image_path);
940 FileSystem::Instance().Resolve(image_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 platform->ResolveRemotePath(m_options.install_path,
942 m_options.install_path);
943 image_token = platform->LoadImage(process, image_spec,
944 m_options.install_path, error);
945 } else {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000946 FileSpec image_spec(image_path);
947 FileSystem::Instance().Resolve(image_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948 image_token =
949 platform->LoadImage(process, image_spec, FileSpec(), error);
950 }
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +0000951
Kate Stoneb9c1b512016-09-06 20:57:50 +0000952 if (image_token != LLDB_INVALID_IMAGE_TOKEN) {
953 result.AppendMessageWithFormat(
Zachary Turner97d2c402016-10-05 23:40:23 +0000954 "Loading \"%s\"...ok\nImage %u loaded.\n", image_path.str().c_str(),
955 image_token);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956 result.SetStatus(eReturnStatusSuccessFinishResult);
957 } else {
Zachary Turner97d2c402016-10-05 23:40:23 +0000958 result.AppendErrorWithFormat("failed to load '%s': %s",
959 image_path.str().c_str(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000960 error.AsCString());
961 result.SetStatus(eReturnStatusFailed);
962 }
Greg Clayton8f343b02010-11-04 01:54:29 +0000963 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000964 return result.Succeeded();
965 }
966
967 CommandOptions m_options;
Greg Clayton8f343b02010-11-04 01:54:29 +0000968};
969
Greg Clayton8f343b02010-11-04 01:54:29 +0000970// CommandObjectProcessUnload
Jim Inghambb9caf72010-12-09 18:58:16 +0000971#pragma mark CommandObjectProcessUnload
Greg Clayton8f343b02010-11-04 01:54:29 +0000972
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973class CommandObjectProcessUnload : public CommandObjectParsed {
Greg Clayton8f343b02010-11-04 01:54:29 +0000974public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975 CommandObjectProcessUnload(CommandInterpreter &interpreter)
976 : CommandObjectParsed(
977 interpreter, "process unload",
978 "Unload a shared library from the current process using the index "
979 "returned by a previous call to \"process load\".",
980 "process unload <index>",
981 eCommandRequiresProcess | eCommandTryTargetAPILock |
982 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {}
Greg Clayton8f343b02010-11-04 01:54:29 +0000983
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984 ~CommandObjectProcessUnload() override = default;
Greg Clayton8f343b02010-11-04 01:54:29 +0000985
Jim Ingham5a988412012-06-08 21:56:10 +0000986protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987 bool DoExecute(Args &command, CommandReturnObject &result) override {
988 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton8f343b02010-11-04 01:54:29 +0000989
Zachary Turner97d2c402016-10-05 23:40:23 +0000990 for (auto &entry : command.entries()) {
991 uint32_t image_token;
992 if (entry.ref.getAsInteger(0, image_token)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993 result.AppendErrorWithFormat("invalid image index argument '%s'",
Zachary Turner97d2c402016-10-05 23:40:23 +0000994 entry.ref.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995 result.SetStatus(eReturnStatusFailed);
996 break;
997 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000998 Status error(process->GetTarget().GetPlatform()->UnloadImage(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999 process, image_token));
1000 if (error.Success()) {
1001 result.AppendMessageWithFormat(
1002 "Unloading shared library with index %u...ok\n", image_token);
1003 result.SetStatus(eReturnStatusSuccessFinishResult);
1004 } else {
1005 result.AppendErrorWithFormat("failed to unload image: %s",
1006 error.AsCString());
1007 result.SetStatus(eReturnStatusFailed);
1008 break;
Greg Clayton8f343b02010-11-04 01:54:29 +00001009 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001011 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 return result.Succeeded();
1013 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001014};
1015
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016// CommandObjectProcessSignal
Jim Inghambb9caf72010-12-09 18:58:16 +00001017#pragma mark CommandObjectProcessSignal
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019class CommandObjectProcessSignal : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021 CommandObjectProcessSignal(CommandInterpreter &interpreter)
1022 : CommandObjectParsed(interpreter, "process signal",
1023 "Send a UNIX signal to the current target process.",
1024 nullptr, eCommandRequiresProcess |
1025 eCommandTryTargetAPILock) {
1026 CommandArgumentEntry arg;
1027 CommandArgumentData signal_arg;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 // Define the first (and only) variant of this arg.
1030 signal_arg.arg_type = eArgTypeUnixSignal;
1031 signal_arg.arg_repetition = eArgRepeatPlain;
1032
1033 // There is only one variant this argument could be; put it into the
1034 // argument entry.
1035 arg.push_back(signal_arg);
1036
1037 // Push the data for the first argument into the m_arguments vector.
1038 m_arguments.push_back(arg);
1039 }
1040
1041 ~CommandObjectProcessSignal() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042
Jim Ingham5a988412012-06-08 21:56:10 +00001043protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044 bool DoExecute(Args &command, CommandReturnObject &result) override {
1045 Process *process = m_exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046
Kate Stoneb9c1b512016-09-06 20:57:50 +00001047 if (command.GetArgumentCount() == 1) {
1048 int signo = LLDB_INVALID_SIGNAL_NUMBER;
1049
1050 const char *signal_name = command.GetArgumentAtIndex(0);
1051 if (::isxdigit(signal_name[0]))
1052 signo =
1053 StringConvert::ToSInt32(signal_name, LLDB_INVALID_SIGNAL_NUMBER, 0);
1054 else
1055 signo = process->GetUnixSignals()->GetSignalNumberFromName(signal_name);
1056
1057 if (signo == LLDB_INVALID_SIGNAL_NUMBER) {
1058 result.AppendErrorWithFormat("Invalid signal argument '%s'.\n",
1059 command.GetArgumentAtIndex(0));
1060 result.SetStatus(eReturnStatusFailed);
1061 } else {
Zachary Turner97206d52017-05-12 04:51:55 +00001062 Status error(process->Signal(signo));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063 if (error.Success()) {
1064 result.SetStatus(eReturnStatusSuccessFinishResult);
1065 } else {
1066 result.AppendErrorWithFormat("Failed to send signal %i: %s\n", signo,
1067 error.AsCString());
1068 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001069 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001070 }
1071 } else {
1072 result.AppendErrorWithFormat(
1073 "'%s' takes exactly one signal number argument:\nUsage: %s\n",
1074 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1075 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001077 return result.Succeeded();
1078 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079};
1080
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081// CommandObjectProcessInterrupt
Jim Inghambb9caf72010-12-09 18:58:16 +00001082#pragma mark CommandObjectProcessInterrupt
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084class CommandObjectProcessInterrupt : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001085public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 CommandObjectProcessInterrupt(CommandInterpreter &interpreter)
1087 : CommandObjectParsed(interpreter, "process interrupt",
1088 "Interrupt the current target process.",
1089 "process interrupt",
1090 eCommandRequiresProcess | eCommandTryTargetAPILock |
1091 eCommandProcessMustBeLaunched) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093 ~CommandObjectProcessInterrupt() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094
Jim Ingham5a988412012-06-08 21:56:10 +00001095protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001096 bool DoExecute(Args &command, CommandReturnObject &result) override {
1097 Process *process = m_exe_ctx.GetProcessPtr();
1098 if (process == nullptr) {
1099 result.AppendError("no process to halt");
1100 result.SetStatus(eReturnStatusFailed);
1101 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001102 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103
1104 if (command.GetArgumentCount() == 0) {
1105 bool clear_thread_plans = true;
Zachary Turner97206d52017-05-12 04:51:55 +00001106 Status error(process->Halt(clear_thread_plans));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001107 if (error.Success()) {
1108 result.SetStatus(eReturnStatusSuccessFinishResult);
1109 } else {
1110 result.AppendErrorWithFormat("Failed to halt process: %s\n",
1111 error.AsCString());
1112 result.SetStatus(eReturnStatusFailed);
1113 }
1114 } else {
1115 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
1116 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1117 result.SetStatus(eReturnStatusFailed);
1118 }
1119 return result.Succeeded();
1120 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121};
1122
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123// CommandObjectProcessKill
Jim Inghambb9caf72010-12-09 18:58:16 +00001124#pragma mark CommandObjectProcessKill
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126class CommandObjectProcessKill : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001128 CommandObjectProcessKill(CommandInterpreter &interpreter)
1129 : CommandObjectParsed(interpreter, "process kill",
1130 "Terminate the current target process.",
1131 "process kill",
1132 eCommandRequiresProcess | eCommandTryTargetAPILock |
1133 eCommandProcessMustBeLaunched) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135 ~CommandObjectProcessKill() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001136
Jim Ingham5a988412012-06-08 21:56:10 +00001137protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 bool DoExecute(Args &command, CommandReturnObject &result) override {
1139 Process *process = m_exe_ctx.GetProcessPtr();
1140 if (process == nullptr) {
1141 result.AppendError("no process to kill");
1142 result.SetStatus(eReturnStatusFailed);
1143 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145
1146 if (command.GetArgumentCount() == 0) {
Zachary Turner97206d52017-05-12 04:51:55 +00001147 Status error(process->Destroy(true));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001148 if (error.Success()) {
1149 result.SetStatus(eReturnStatusSuccessFinishResult);
1150 } else {
1151 result.AppendErrorWithFormat("Failed to kill process: %s\n",
1152 error.AsCString());
1153 result.SetStatus(eReturnStatusFailed);
1154 }
1155 } else {
1156 result.AppendErrorWithFormat("'%s' takes no arguments:\nUsage: %s\n",
1157 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1158 result.SetStatus(eReturnStatusFailed);
1159 }
1160 return result.Succeeded();
1161 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162};
1163
Greg Claytona2715cf2014-06-13 00:54:12 +00001164// CommandObjectProcessSaveCore
Greg Claytona2715cf2014-06-13 00:54:12 +00001165#pragma mark CommandObjectProcessSaveCore
1166
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167class CommandObjectProcessSaveCore : public CommandObjectParsed {
Greg Claytona2715cf2014-06-13 00:54:12 +00001168public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001169 CommandObjectProcessSaveCore(CommandInterpreter &interpreter)
1170 : CommandObjectParsed(interpreter, "process save-core",
1171 "Save the current process as a core file using an "
1172 "appropriate file type.",
1173 "process save-core FILE",
1174 eCommandRequiresProcess | eCommandTryTargetAPILock |
1175 eCommandProcessMustBeLaunched) {}
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00001176
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 ~CommandObjectProcessSaveCore() override = default;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00001178
Greg Claytona2715cf2014-06-13 00:54:12 +00001179protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180 bool DoExecute(Args &command, CommandReturnObject &result) override {
1181 ProcessSP process_sp = m_exe_ctx.GetProcessSP();
1182 if (process_sp) {
1183 if (command.GetArgumentCount() == 1) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001184 FileSpec output_file(command.GetArgumentAtIndex(0));
Zachary Turner97206d52017-05-12 04:51:55 +00001185 Status error = PluginManager::SaveCore(process_sp, output_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001186 if (error.Success()) {
1187 result.SetStatus(eReturnStatusSuccessFinishResult);
1188 } else {
1189 result.AppendErrorWithFormat(
1190 "Failed to save core file for process: %s\n", error.AsCString());
1191 result.SetStatus(eReturnStatusFailed);
Greg Claytona2715cf2014-06-13 00:54:12 +00001192 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193 } else {
1194 result.AppendErrorWithFormat("'%s' takes one arguments:\nUsage: %s\n",
1195 m_cmd_name.c_str(), m_cmd_syntax.c_str());
1196 result.SetStatus(eReturnStatusFailed);
1197 }
1198 } else {
1199 result.AppendError("invalid process");
1200 result.SetStatus(eReturnStatusFailed);
1201 return false;
Greg Claytona2715cf2014-06-13 00:54:12 +00001202 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001203
1204 return result.Succeeded();
1205 }
Greg Claytona2715cf2014-06-13 00:54:12 +00001206};
1207
Jim Ingham4b9bea82010-06-18 01:23:09 +00001208// CommandObjectProcessStatus
Jim Inghambb9caf72010-12-09 18:58:16 +00001209#pragma mark CommandObjectProcessStatus
1210
Kate Stoneb9c1b512016-09-06 20:57:50 +00001211class CommandObjectProcessStatus : public CommandObjectParsed {
Jim Ingham4b9bea82010-06-18 01:23:09 +00001212public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001213 CommandObjectProcessStatus(CommandInterpreter &interpreter)
1214 : CommandObjectParsed(
1215 interpreter, "process status",
1216 "Show status and stop location for the current target process.",
1217 "process status",
1218 eCommandRequiresProcess | eCommandTryTargetAPILock) {}
Jim Ingham4b9bea82010-06-18 01:23:09 +00001219
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220 ~CommandObjectProcessStatus() override = default;
Jim Ingham4b9bea82010-06-18 01:23:09 +00001221
Kate Stoneb9c1b512016-09-06 20:57:50 +00001222 bool DoExecute(Args &command, CommandReturnObject &result) override {
1223 Stream &strm = result.GetOutputStream();
1224 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1225 // No need to check "process" for validity as eCommandRequiresProcess
1226 // ensures it is valid
1227 Process *process = m_exe_ctx.GetProcessPtr();
1228 const bool only_threads_with_stop_reason = true;
1229 const uint32_t start_frame = 0;
1230 const uint32_t num_frames = 1;
1231 const uint32_t num_frames_with_source = 1;
Jim Ingham6a9767c2016-11-08 20:36:40 +00001232 const bool stop_format = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001233 process->GetStatus(strm);
1234 process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame,
Jim Ingham6a9767c2016-11-08 20:36:40 +00001235 num_frames, num_frames_with_source, stop_format);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 return result.Succeeded();
1237 }
Jim Ingham4b9bea82010-06-18 01:23:09 +00001238};
1239
Caroline Tice35731352010-10-13 20:44:39 +00001240// CommandObjectProcessHandle
Raphael Isemann438dfcf2019-07-23 12:54:33 +00001241#define LLDB_OPTIONS_process_handle
1242#include "CommandOptions.inc"
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001243
Jim Inghambb9caf72010-12-09 18:58:16 +00001244#pragma mark CommandObjectProcessHandle
Caroline Tice35731352010-10-13 20:44:39 +00001245
Kate Stoneb9c1b512016-09-06 20:57:50 +00001246class CommandObjectProcessHandle : public CommandObjectParsed {
Caroline Tice35731352010-10-13 20:44:39 +00001247public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001248 class CommandOptions : public Options {
1249 public:
1250 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
Caroline Tice35731352010-10-13 20:44:39 +00001251
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252 ~CommandOptions() override = default;
Caroline Tice35731352010-10-13 20:44:39 +00001253
Zachary Turner97206d52017-05-12 04:51:55 +00001254 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1255 ExecutionContext *execution_context) override {
1256 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257 const int short_option = m_getopt_table[option_idx].val;
Caroline Tice35731352010-10-13 20:44:39 +00001258
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 switch (short_option) {
1260 case 's':
1261 stop = option_arg;
1262 break;
1263 case 'n':
1264 notify = option_arg;
1265 break;
1266 case 'p':
1267 pass = option_arg;
1268 break;
1269 default:
Raphael Isemann36162012019-08-22 08:08:05 +00001270 llvm_unreachable("Unimplemented option");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 }
1272 return error;
Caroline Tice35731352010-10-13 20:44:39 +00001273 }
1274
Kate Stoneb9c1b512016-09-06 20:57:50 +00001275 void OptionParsingStarting(ExecutionContext *execution_context) override {
1276 stop.clear();
1277 notify.clear();
1278 pass.clear();
Caroline Tice35731352010-10-13 20:44:39 +00001279 }
1280
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001281 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001282 return llvm::makeArrayRef(g_process_handle_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001283 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284
1285 // Instance variables to hold the values for command options.
1286
1287 std::string stop;
1288 std::string notify;
1289 std::string pass;
1290 };
1291
1292 CommandObjectProcessHandle(CommandInterpreter &interpreter)
1293 : CommandObjectParsed(interpreter, "process handle",
1294 "Manage LLDB handling of OS signals for the "
1295 "current target process. Defaults to showing "
1296 "current policy.",
1297 nullptr),
1298 m_options() {
1299 SetHelpLong("\nIf no signals are specified, update them all. If no update "
1300 "option is specified, list the current values.");
1301 CommandArgumentEntry arg;
1302 CommandArgumentData signal_arg;
1303
1304 signal_arg.arg_type = eArgTypeUnixSignal;
1305 signal_arg.arg_repetition = eArgRepeatStar;
1306
1307 arg.push_back(signal_arg);
1308
1309 m_arguments.push_back(arg);
1310 }
1311
1312 ~CommandObjectProcessHandle() override = default;
1313
1314 Options *GetOptions() override { return &m_options; }
1315
1316 bool VerifyCommandOptionValue(const std::string &option, int &real_value) {
1317 bool okay = true;
1318 bool success = false;
Pavel Labath47cbf4a2018-04-10 09:03:59 +00001319 bool tmp_value = OptionArgParser::ToBoolean(option, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320
1321 if (success && tmp_value)
1322 real_value = 1;
1323 else if (success && !tmp_value)
1324 real_value = 0;
1325 else {
1326 // If the value isn't 'true' or 'false', it had better be 0 or 1.
1327 real_value = StringConvert::ToUInt32(option.c_str(), 3);
1328 if (real_value != 0 && real_value != 1)
1329 okay = false;
Caroline Tice35731352010-10-13 20:44:39 +00001330 }
1331
Kate Stoneb9c1b512016-09-06 20:57:50 +00001332 return okay;
1333 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001334
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335 void PrintSignalHeader(Stream &str) {
1336 str.Printf("NAME PASS STOP NOTIFY\n");
1337 str.Printf("=========== ===== ===== ======\n");
1338 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001339
Kate Stoneb9c1b512016-09-06 20:57:50 +00001340 void PrintSignal(Stream &str, int32_t signo, const char *sig_name,
1341 const UnixSignalsSP &signals_sp) {
1342 bool stop;
1343 bool suppress;
1344 bool notify;
1345
1346 str.Printf("%-11s ", sig_name);
1347 if (signals_sp->GetSignalInfo(signo, suppress, stop, notify)) {
1348 bool pass = !suppress;
1349 str.Printf("%s %s %s", (pass ? "true " : "false"),
1350 (stop ? "true " : "false"), (notify ? "true " : "false"));
Caroline Tice10ad7992010-10-14 21:31:13 +00001351 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001352 str.Printf("\n");
1353 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001354
Kate Stoneb9c1b512016-09-06 20:57:50 +00001355 void PrintSignalInformation(Stream &str, Args &signal_args,
1356 int num_valid_signals,
1357 const UnixSignalsSP &signals_sp) {
1358 PrintSignalHeader(str);
1359
1360 if (num_valid_signals > 0) {
1361 size_t num_args = signal_args.GetArgumentCount();
1362 for (size_t i = 0; i < num_args; ++i) {
1363 int32_t signo = signals_sp->GetSignalNumberFromName(
1364 signal_args.GetArgumentAtIndex(i));
1365 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
1366 PrintSignal(str, signo, signal_args.GetArgumentAtIndex(i),
1367 signals_sp);
1368 }
1369 } else // Print info for ALL signals
Caroline Tice10ad7992010-10-14 21:31:13 +00001370 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001371 int32_t signo = signals_sp->GetFirstSignalNumber();
1372 while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1373 PrintSignal(str, signo, signals_sp->GetSignalAsCString(signo),
1374 signals_sp);
1375 signo = signals_sp->GetNextSignalNumber(signo);
1376 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001377 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378 }
Caroline Tice10ad7992010-10-14 21:31:13 +00001379
Jim Ingham5a988412012-06-08 21:56:10 +00001380protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 bool DoExecute(Args &signal_args, CommandReturnObject &result) override {
Jonas Devlieghere57179862019-04-27 06:19:42 +00001382 TargetSP target_sp = GetDebugger().GetSelectedTarget();
Caroline Tice35731352010-10-13 20:44:39 +00001383
Kate Stoneb9c1b512016-09-06 20:57:50 +00001384 if (!target_sp) {
1385 result.AppendError("No current target;"
1386 " cannot handle signals until you have a valid target "
1387 "and process.\n");
1388 result.SetStatus(eReturnStatusFailed);
1389 return false;
Caroline Tice35731352010-10-13 20:44:39 +00001390 }
1391
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392 ProcessSP process_sp = target_sp->GetProcessSP();
1393
1394 if (!process_sp) {
1395 result.AppendError("No current process; cannot handle signals until you "
1396 "have a valid process.\n");
1397 result.SetStatus(eReturnStatusFailed);
1398 return false;
1399 }
1400
1401 int stop_action = -1; // -1 means leave the current setting alone
1402 int pass_action = -1; // -1 means leave the current setting alone
1403 int notify_action = -1; // -1 means leave the current setting alone
1404
1405 if (!m_options.stop.empty() &&
1406 !VerifyCommandOptionValue(m_options.stop, stop_action)) {
1407 result.AppendError("Invalid argument for command option --stop; must be "
1408 "true or false.\n");
1409 result.SetStatus(eReturnStatusFailed);
1410 return false;
1411 }
1412
1413 if (!m_options.notify.empty() &&
1414 !VerifyCommandOptionValue(m_options.notify, notify_action)) {
1415 result.AppendError("Invalid argument for command option --notify; must "
1416 "be true or false.\n");
1417 result.SetStatus(eReturnStatusFailed);
1418 return false;
1419 }
1420
1421 if (!m_options.pass.empty() &&
1422 !VerifyCommandOptionValue(m_options.pass, pass_action)) {
1423 result.AppendError("Invalid argument for command option --pass; must be "
1424 "true or false.\n");
1425 result.SetStatus(eReturnStatusFailed);
1426 return false;
1427 }
1428
1429 size_t num_args = signal_args.GetArgumentCount();
1430 UnixSignalsSP signals_sp = process_sp->GetUnixSignals();
1431 int num_signals_set = 0;
1432
1433 if (num_args > 0) {
Zachary Turnerd6a24752016-11-22 17:10:15 +00001434 for (const auto &arg : signal_args) {
1435 int32_t signo = signals_sp->GetSignalNumberFromName(arg.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436 if (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1437 // Casting the actions as bools here should be okay, because
Adrian Prantl05097242018-04-30 16:49:04 +00001438 // VerifyCommandOptionValue guarantees the value is either 0 or 1.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001439 if (stop_action != -1)
1440 signals_sp->SetShouldStop(signo, stop_action);
1441 if (pass_action != -1) {
1442 bool suppress = !pass_action;
1443 signals_sp->SetShouldSuppress(signo, suppress);
1444 }
1445 if (notify_action != -1)
1446 signals_sp->SetShouldNotify(signo, notify_action);
1447 ++num_signals_set;
1448 } else {
1449 result.AppendErrorWithFormat("Invalid signal name '%s'\n",
Zachary Turnerd6a24752016-11-22 17:10:15 +00001450 arg.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001451 }
1452 }
1453 } else {
1454 // No signal specified, if any command options were specified, update ALL
1455 // signals.
1456 if ((notify_action != -1) || (stop_action != -1) || (pass_action != -1)) {
1457 if (m_interpreter.Confirm(
1458 "Do you really want to update all the signals?", false)) {
1459 int32_t signo = signals_sp->GetFirstSignalNumber();
1460 while (signo != LLDB_INVALID_SIGNAL_NUMBER) {
1461 if (notify_action != -1)
1462 signals_sp->SetShouldNotify(signo, notify_action);
1463 if (stop_action != -1)
1464 signals_sp->SetShouldStop(signo, stop_action);
1465 if (pass_action != -1) {
1466 bool suppress = !pass_action;
1467 signals_sp->SetShouldSuppress(signo, suppress);
1468 }
1469 signo = signals_sp->GetNextSignalNumber(signo);
1470 }
1471 }
1472 }
1473 }
1474
1475 PrintSignalInformation(result.GetOutputStream(), signal_args,
1476 num_signals_set, signals_sp);
1477
1478 if (num_signals_set > 0)
1479 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1480 else
1481 result.SetStatus(eReturnStatusFailed);
1482
1483 return result.Succeeded();
1484 }
1485
1486 CommandOptions m_options;
Caroline Tice35731352010-10-13 20:44:39 +00001487};
1488
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489// CommandObjectMultiwordProcess
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001490
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491CommandObjectMultiwordProcess::CommandObjectMultiwordProcess(
1492 CommandInterpreter &interpreter)
1493 : CommandObjectMultiword(
1494 interpreter, "process",
1495 "Commands for interacting with processes on the current platform.",
1496 "process <subcommand> [<subcommand-options>]") {
1497 LoadSubCommand("attach",
1498 CommandObjectSP(new CommandObjectProcessAttach(interpreter)));
1499 LoadSubCommand("launch",
1500 CommandObjectSP(new CommandObjectProcessLaunch(interpreter)));
1501 LoadSubCommand("continue", CommandObjectSP(new CommandObjectProcessContinue(
1502 interpreter)));
1503 LoadSubCommand("connect",
1504 CommandObjectSP(new CommandObjectProcessConnect(interpreter)));
1505 LoadSubCommand("detach",
1506 CommandObjectSP(new CommandObjectProcessDetach(interpreter)));
1507 LoadSubCommand("load",
1508 CommandObjectSP(new CommandObjectProcessLoad(interpreter)));
1509 LoadSubCommand("unload",
1510 CommandObjectSP(new CommandObjectProcessUnload(interpreter)));
1511 LoadSubCommand("signal",
1512 CommandObjectSP(new CommandObjectProcessSignal(interpreter)));
1513 LoadSubCommand("handle",
1514 CommandObjectSP(new CommandObjectProcessHandle(interpreter)));
1515 LoadSubCommand("status",
1516 CommandObjectSP(new CommandObjectProcessStatus(interpreter)));
1517 LoadSubCommand("interrupt", CommandObjectSP(new CommandObjectProcessInterrupt(
1518 interpreter)));
1519 LoadSubCommand("kill",
1520 CommandObjectSP(new CommandObjectProcessKill(interpreter)));
1521 LoadSubCommand("plugin",
1522 CommandObjectSP(new CommandObjectProcessPlugin(interpreter)));
1523 LoadSubCommand("save-core", CommandObjectSP(new CommandObjectProcessSaveCore(
1524 interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001525}
1526
Eugene Zelenko49bcfd82016-02-23 01:43:44 +00001527CommandObjectMultiwordProcess::~CommandObjectMultiwordProcess() = default;